2 /***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 require_once("xorg.misc.inc.php");
25 define("ERROR_INACTIVE_REDIRECTION", 2);
26 define("ERROR_INVALID_EMAIL", 3);
27 define("ERROR_LOOP_EMAIL", 4);
29 // function fix_bestalias() {{{1
31 function fix_bestalias($uid)
33 $res = XDB
::query("SELECT COUNT(*) FROM aliases WHERE id={?} AND FIND_IN_SET('bestalias',flags) AND type!='homonyme'", $uid);
34 if ($n = $res->fetchOneCell()) {
37 XDB
::execute("UPDATE aliases
38 SET flags=CONCAT(flags,',','bestalias')
39 WHERE id={?} AND type!='homonyme'
40 ORDER BY !FIND_IN_SET('usage',flags),alias LIKE '%.%', LENGTH(alias)
44 // function valide_email() {{{1
46 function valide_email($str)
50 $em = trim(rtrim($str));
51 $em = str_replace('<', '', $em);
52 $em = str_replace('>', '', $em);
53 list($ident, $dom) = explode('@', $em);
54 if ($dom == $globals->mail
->domain
or $dom == $globals->mail
->domain2
) {
55 list($ident1) = explode('_', $ident);
56 list($ident) = explode('+', $ident1);
58 return $ident . '@' . $dom;
68 private $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams');
72 public function __construct($uid)
77 $res = XDB
::query('SELECT email FROM emails WHERE uid={?} AND flags="filter"', $uid);
78 if ($res->numRows()) {
79 $this->state
= $res->fetchOneCell();
81 $this->state
= 'tag_and_drop_spams';
82 $res = XDB
::query("INSERT INTO emails (uid,email,rewrite,panne,flags)
83 VALUES ({?},'tag_and_drop_spams','','0000-00-00','filter')", $uid);
87 // public function change() {{{2
89 public function change($uid, $state)
91 $this->state
= is_int($state) ?
$this->_states
[$state] : $state;
92 XDB
::execute('UPDATE emails SET email={?} WHERE uid={?} AND flags = "filter"',
96 // pubic function level() {{{2
98 public function level()
100 return array_search($this->state
, $this->_states
);
121 public function __construct($row)
123 list($this->email
, $flags, $this->rewrite
, $this->panne
, $this->last
, $this->panne_level
) = $row;
124 $this->active
= ($flags == 'active');
125 $this->broken
= ($flags == 'panne');
126 $this->disabled
= ($flags == 'disable');
129 // public function activate() {{{2
131 public function activate($uid)
133 if (!$this->active
) {
134 XDB
::execute("UPDATE emails
135 SET panne_level = IF(flags = 'panne', panne_level - 1, panne_level),
137 WHERE uid={?} AND email={?}", $uid, $this->email
);
138 $_SESSION['log']->log("email_on", $this->email
.($uid!=S
::v('uid') ?
"(admin on $uid)" : ""));
139 $this->active
= true
;
140 $this->broken
= false
;
144 // public function deactivate() {{{2
146 public function deactivate($uid)
149 XDB
::execute("UPDATE emails SET flags =''
150 WHERE uid={?} AND email={?}", $uid, $this->email
);
151 $_SESSION['log']->log("email_off",$this->email
.($uid!=S
::v('uid') ?
"(admin on $uid)" : "") );
152 $this->active
= false
;
156 // public function rewrite() {{{2
158 public function rewrite($rew, $uid)
160 if ($this->rewrite
== $rew) {
163 if (!$rew ||
!isvalid_email($rew)) {
166 XDB
::execute('UPDATE emails SET rewrite={?} WHERE uid={?} AND email={?}', $rew, $uid, $this->email
);
167 $this->rewrite
= $rew;
171 // function cleanErrors() {{{2
173 public function cleanErrors($uid)
175 if (!S
::has_perms()) {
179 $this->panne_level
= 0;
181 return XDB
::execute("UPDATE emails
182 SET panne_level = 0, panne = 0, last = 0
183 WHERE uid = {?} AND email = {?}",
188 // class Redirect {{{1
194 private $flag_active = 'active';
202 public function __construct($_uid)
205 $res = XDB
::iterRow("SELECT email, flags, rewrite, panne, last, panne_level
207 WHERE uid = {?} AND flags != 'filter'", $_uid);
208 $this->emails
=Array();
209 while ($row = $res->next()) {
210 $this->emails
[] = new Email($row);
212 $this->bogo
= new Bogo($_uid);
215 // public function other_active() {{{2
217 public function other_active($email)
219 foreach ($this->emails
as $mail) {
220 if ($mail->email
!=$email && $mail->active
) {
227 // public function delete_email() {{{2
229 public function delete_email($email)
231 if (!$this->other_active($email)) {
232 return ERROR_INACTIVE_REDIRECTION
;
234 XDB
::execute('DELETE FROM emails WHERE uid={?} AND email={?}', $this->uid
, $email);
235 $_SESSION['log']->log('email_del',$email.($this->uid
!=S
::v('uid') ?
" (admin on {$this->uid})" : ""));
236 foreach ($this->emails
as $i=>$mail) {
237 if ($email==$mail->email
) {
238 unset($this->emails
[$i]);
241 check_redirect($this);
245 // public function add_email() {{{2
247 public function add_email($email)
249 $email_stripped = strtolower(trim($email));
250 if (!isvalid_email($email_stripped)) {
251 return ERROR_INVALID_EMAIL
;
253 if (!isvalid_email_redirection($email_stripped)) {
254 return ERROR_LOOP_EMAIL
;
256 XDB
::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->uid
, $email);
257 if ($logger = S
::v('log', null
)) { // may be absent --> step4.php
258 $logger->log('email_add',$email.($this->uid
!=S
::v('uid') ?
" (admin on {$this->uid})" : ""));
260 foreach ($this->emails
as $mail) {
261 if ($mail->email
== $email_stripped) {
265 $this->emails
[] = new Email(array($email, 'active', '', '0000-00-00', '0000-00-00', 0));
268 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->uid
);
269 check_redirect($this);
273 // public function modify_email() {{{2
275 public function modify_email($emails_actifs, $emails_rewrite)
277 foreach ($this->emails
as &$mail) {
278 if (in_array($mail->email
, $emails_actifs)) {
279 $mail->activate($this->uid
);
281 $mail->deactivate($this->uid
);
283 $mail->rewrite($emails_rewrite[$mail->email
], $this->uid
);
285 check_redirect($this);
288 // public function modify_one_email() {{{2
290 public function modify_one_email($email, $activate)
294 foreach ($this->emails
as $i=>$mail) {
295 if ($mail->email
== $email) {
298 $allinactive &= !$mail->active ||
$mail->email
== $email;
300 if ($thisone === false
) {
301 return ERROR_INVALID_EMAIL
;
303 if ($allinactive ||
$activate) {
304 $this->emails
[$thisone]->activate($this->uid
);
306 $this->emails
[$thisone]->deactivate($this->uid
);
308 check_redirect($this);
309 if ($allinactive && !$activate) {
310 return ERROR_INACTIVE_REDIRECTION
;
316 // public function modify_one_email_redirect() {{{2
318 public function modify_one_email_redirect($email, $redirect)
320 foreach ($this->emails
as &$mail) {
321 if ($mail->email
== $email) {
322 $mail->rewrite($redirect, $this->uid
);
323 check_redirect($this);
329 // function cleanErrors() {{{2
331 public function cleanErrors($email)
333 foreach ($this->emails
as &$mail) {
334 if ($mail->email
== $email) {
335 return $mail->cleanErrors($this->uid
);
341 // function disable() {{{2
343 public function disable()
345 XDB
::execute("UPDATE emails
346 SET flags = 'disable'
347 WHERE flags = 'active' AND uid = {?}", $this->uid
);
348 foreach ($this->emails
as &$mail) {
350 $mail->disabled
= true
;
351 $mail->active
= false
;
356 // function enable() {{{2
358 public function enable()
360 XDB
::execute("UPDATE emails
362 WHERE flags = 'disable' AND uid = {?}", $this->uid
);
363 foreach ($this->emails
as &$mail) {
364 if ($mail->disabled
) {
365 $mail->active
= true
;
366 $mail->disabled
= false
;
371 // function get_broken_mx() {{{2
373 public function get_broken_mx()
375 $res = XDB
::query("SELECT host, text
377 WHERE state != 'ok'");
378 if (!$res->numRows()) {
381 $mxs = $res->fetchAllAssoc();
383 foreach ($this->emails
as &$mail) {
385 list(,$domain) = explode('@', $mail->email
);
386 getmxrr($domain, $lcl_mxs);
387 if (empty($lcl_mxs)) {
388 $lcl_mxs = array($domain);
391 foreach ($mxs as &$mx) {
392 foreach ($lcl_mxs as $lcl) {
393 if (fnmatch($mx['host'], $lcl)) {
394 $broken = $mx['text'];
399 $mails[] = array('mail' => $mail->email
, 'text' => $broken);
408 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: