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");
27 define("ERROR_INACTIVE_REDIRECTION", 2);
28 define("ERROR_INVALID_EMAIL", 3);
29 define("ERROR_LOOP_EMAIL", 4);
32 // {{{ function fix_bestalias()
34 function fix_bestalias($uid)
36 $res = XDB
::query("SELECT COUNT(*) FROM aliases WHERE id={?} AND FIND_IN_SET('bestalias',flags) AND type!='homonyme'", $uid);
37 if ($n = $res->fetchOneCell()) {
40 XDB
::execute("UPDATE aliases
41 SET flags=CONCAT(flags,',','bestalias')
42 WHERE id={?} AND type!='homonyme'
43 ORDER BY !FIND_IN_SET('usage',flags),alias LIKE '%.%', LENGTH(alias)
48 // {{{ function valide_email()
50 function valide_email($str)
54 $em = trim(rtrim($str));
55 $em = str_replace('<', '', $em);
56 $em = str_replace('>', '', $em);
57 list($ident, $dom) = explode('@', $em);
58 if ($dom == $globals->mail
->domain
or $dom == $globals->mail
->domain2
) {
59 list($ident1) = explode('_', $ident);
60 list($ident) = explode('+', $ident1);
62 return $ident . '@' . $dom;
73 var $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams');
83 $res = XDB
::query('SELECT email FROM emails WHERE uid={?} AND flags="filter"', $uid);
84 if ($res->numRows()) {
85 $this->state
= $res->fetchOneCell();
87 $this->state
= 'tag_and_drop_spams';
88 $res = XDB
::query("INSERT INTO emails (uid,email,rewrite,panne,flags)
89 VALUES ({?},'tag_and_drop_spams','','0000-00-00','filter')", $uid);
94 // {{{ function change()
96 function change($uid, $state)
98 $this->state
= is_int($state) ?
$this->_states
[$state] : $state;
99 XDB
::execute('UPDATE emails SET email={?} WHERE uid={?} AND flags = "filter"',
104 // {{{ function level()
107 { return array_search($this->state
, $this->_states
); }
132 list($this->email
, $flags, $this->rewrite
, $this->panne
, $this->last
, $this->panne_level
) = $row;
133 $this->active
= ($flags == 'active');
134 $this->broken
= ($flags == 'panne');
138 // {{{ function activate()
140 function activate($uid)
142 if (!$this->active
) {
143 XDB
::execute("UPDATE emails
144 SET panne_level = IF(flags = 'panne', panne_level - 1, panne_level),
146 WHERE uid={?} AND email={?}", $uid, $this->email
);
147 $_SESSION['log']->log("email_on", $this->email
.($uid!=S
::v('uid') ?
"(admin on $uid)" : ""));
148 $this->active
= true
;
149 $this->broken
= false
;
154 // {{{ function deactivate()
156 function deactivate($uid)
159 XDB
::execute("UPDATE emails SET flags =''
160 WHERE uid={?} AND email={?}", $uid, $this->email
);
161 $_SESSION['log']->log("email_off",$this->email
.($uid!=S
::v('uid') ?
"(admin on $uid)" : "") );
162 $this->active
= false
;
167 // {{{ function rewrite()
169 function rewrite($rew, $uid)
171 if ($this->rewrite
== $rew) {
174 XDB
::execute('UPDATE emails SET rewrite={?} WHERE uid={?} AND email={?}', $rew, $uid, $this->email
);
175 $this->rewrite
= $rew;
183 // {{{ class Redirect
189 var $flag_active = 'active';
195 // {{{ function Redirect()
197 function Redirect($_uid)
200 $res = XDB
::iterRow("
201 SELECT email, flags, rewrite, panne, last, panne_level
202 FROM emails WHERE uid = {?} AND flags != 'filter'", $_uid);
203 $this->emails
=Array();
204 while ($row = $res->next()) {
205 $this->emails
[] = new Email($row);
207 $this->bogo
= new Bogo($_uid);
211 // {{{ function other_active()
213 function other_active($email)
215 foreach ($this->emails
as $mail) {
216 if ($mail->email
!=$email && $mail->active
) {
224 // {{{ function delete_email()
226 function delete_email($email)
228 if (!$this->other_active($email)) {
229 return ERROR_INACTIVE_REDIRECTION
;
231 XDB
::execute('DELETE FROM emails WHERE uid={?} AND email={?}', $this->uid
, $email);
232 $_SESSION['log']->log('email_del',$email.($this->uid
!=S
::v('uid') ?
" (admin on {$this->uid})" : ""));
233 foreach ($this->emails
as $i=>$mail) {
234 if ($email==$mail->email
) {
235 unset($this->emails
[$i]);
238 check_redirect($this);
243 // {{{ function add_email()
245 function add_email($email)
247 $email_stripped = strtolower(trim($email));
248 if (!isvalid_email($email_stripped)) {
249 return ERROR_INVALID_EMAIL
;
251 if (!isvalid_email_redirection($email_stripped)) {
252 return ERROR_LOOP_EMAIL
;
254 XDB
::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->uid
, $email);
255 if ($logger = S
::v('log', null
)) { // may be absent --> step4.php
256 $logger->log('email_add',$email.($this->uid
!=S
::v('uid') ?
" (admin on {$this->uid})" : ""));
258 foreach ($this->emails
as $mail) {
259 if ($mail->email
== $email_stripped) {
263 $this->emails
[] = new Email(array($email, 'active', '', '0000-00-00', '0000-00-00', 0));
266 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->uid
);
267 check_redirect($this);
272 // {{{ function modify_email()
274 function modify_email($emails_actifs,$emails_rewrite)
276 foreach ($this->emails
as $i=>$mail) {
277 if (in_array($mail->email
,$emails_actifs)) {
278 $this->emails
[$i]->activate($this->uid
);
280 $this->emails
[$i]->deactivate($this->uid
);
282 $this->emails
[$i]->rewrite($emails_rewrite[$mail->email
], $this->uid
);
284 check_redirect($this);
287 function modify_one_email($email, $activate)
291 foreach ($this->emails
as $i=>$mail) {
292 if ($mail->email
== $email) {
295 $allinactive &= !$mail->active ||
$mail->email
== $email;
297 if ($thisone === false
) {
298 return ERROR_INVALID_EMAIL
;
300 if ($allinactive ||
$activate) {
301 $this->emails
[$thisone]->activate($this->uid
);
303 $this->emails
[$thisone]->deactivate($this->uid
);
305 check_redirect($this);
306 if ($allinactive && !$activate) {
307 return ERROR_INACTIVE_REDIRECTION
;
313 function modify_one_email_redirect($email, $redirect) {
314 foreach ($this->emails
as $i=>$mail) {
315 if ($mail->email
== $email) {
316 $this->emails
[$i]->rewrite($redirect, $this->uid
);
317 check_redirect($this);
323 // {{{ function get_broken_mx()
325 function get_broken_mx()
327 $res = XDB
::iterRow("SELECT host, text
329 WHERE state != 'ok'");
330 if (!$res->total()) {
334 while (list($host, $text) = $res->next()) {
335 $host = preg_quote($host, '/');
336 $host = str_replace('\*', '.*', $host);
337 $host = str_replace('\?', '.', $host);
338 $mxs[] = array('host' => "/^$host\\.?$/", 'text' => $text);
341 foreach ($this->emails
as &$mail) {
343 list(,$domain) = explode('@', $mail->email
);
344 getmxrr($domain, $lcl_mxs);
345 if (empty($lcl_mxs)) {
346 $lcl_mxs = array($domain);
349 foreach ($mxs as &$mx) {
350 foreach ($lcl_mxs as $lcl) {
351 if (preg_match($mx['host'], $lcl)) {
352 $broken = $mx['text'];
357 $mails[] = array('mail' => $mail->email
, 'text' => $broken);
370 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: