2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 ***************************************************************************/
23 define("ERROR_INACTIVE_REDIRECTION", 2);
24 define("ERROR_INVALID_EMAIL", 3);
25 define("ERROR_LOOP_EMAIL", 4);
27 // function fix_bestalias() {{{1
28 // Checks for an existing 'bestalias' among the the current user's aliases, and
29 // eventually selects a new bestalias when required.
30 function fix_bestalias($uid)
32 $res = XDB
::query("SELECT COUNT(*)
34 WHERE id = {?} AND FIND_IN_SET('bestalias', flags) AND type != 'homonyme'",
36 if ($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)
47 // function valide_email() {{{1
48 // Returns a cleaned-up version of the @p email string. It removes garbage
49 // characters, and determines the canonical form (without _ and +) for
50 // Polytechnique.org email addresses.
51 function valide_email($str)
55 $em = trim(rtrim($str));
56 $em = str_replace('<', '', $em);
57 $em = str_replace('>', '', $em);
58 list($ident, $dom) = explode('@', $em);
59 if ($dom == $globals->mail
->domain
or $dom == $globals->mail
->domain2
) {
60 list($ident1) = explode('_', $ident);
61 list($ident) = explode('+', $ident1);
63 return $ident . '@' . $dom;
67 // The Bogo class represents a spam filtering level in plat/al architecture.
74 private $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams');
78 public function __construct($uid)
85 $res = XDB
::query('SELECT email FROM emails WHERE uid={?} AND flags="filter"', $uid);
86 if ($res->numRows()) {
87 $this->state
= $res->fetchOneCell();
89 $this->state
= 'tag_and_drop_spams';
90 $res = XDB
::query("INSERT INTO emails (uid,email,rewrite,panne,flags)
91 VALUES ({?},'tag_and_drop_spams','','0000-00-00','filter')", $uid);
95 // public function change() {{{2
97 public function change($state)
99 $this->state
= is_int($state) ?
$this->_states
[$state] : $state;
100 XDB
::execute('UPDATE emails SET email={?} WHERE uid={?} AND flags = "filter"',
101 $this->state
, $this->uid
);
104 // pubic function level() {{{2
106 public function level()
108 return array_search($this->state
, $this->_states
);
113 // Represents an "email address" used as final recipient for plat/al-managed
114 // addresses; it can be subclasses a Redirection emails (third-party) or as
115 // Storage emails (Polytechnique.org).
120 // Basic email properties; $sufficient indicates if the email can be used as
121 // an unique redirection; $email contains the delivery email address.
125 public $display_email;
127 // Redirection status properties.
133 // Redirection bounces stats.
138 // Activates the email address as a redirection.
139 public abstract function activate();
141 // Deactivates the email address as a redirection.
142 public abstract function deactivate();
144 // Sets the rewrite rule for the given address.
145 public abstract function set_rewrite($rewrite);
147 // Resets the error counts associated with the redirection.
148 public abstract function clean_errors();
150 // Email backend capabilities ('rewrite' refers to From: rewrite for mails
151 // forwarded by Polytechnique.org's MXs; 'removable' indicates if the email
152 // can be definitively removed; 'disable' indicates if the email has a third
153 // status 'disabled' in addition to 'active' and 'inactive').
154 public abstract function has_rewrite();
155 public abstract function is_removable();
156 public abstract function has_disable();
159 // class EmailRedirection {{{1
160 // Implementation of Email for third-party redirection (redirection of emails to
161 // external user-supplied addresses).
162 class EmailRedirection
extends Email
166 public function __construct($uid, $row)
169 $this->sufficient
= true
;
171 list($this->email
, $flags, $this->rewrite
, $this->panne
, $this->last
, $this->panne_level
) = $row;
172 $this->display_email
= $this->email
;
173 $this->active
= ($flags == 'active');
174 $this->broken
= ($flags == 'panne');
175 $this->disabled
= ($flags == 'disable');
178 // public function activate() {{{2
180 public function activate()
182 if (!$this->active
) {
183 XDB
::execute("UPDATE emails
184 SET panne_level = IF(flags = 'panne', panne_level - 1, panne_level),
186 WHERE uid={?} AND email={?}", $this->uid
, $this->email
);
187 S
::logger()->log("email_on", $this->email
.($this->uid
!=S
::v('uid') ?
"(admin on {$this->uid})" : ""));
188 $this->active
= true
;
189 $this->broken
= false
;
193 // public function deactivate() {{{2
195 public function deactivate()
198 XDB
::execute("UPDATE emails SET flags =''
199 WHERE uid={?} AND email={?}", $this->uid
, $this->email
);
200 S
::logger()->log("email_off",$this->email
.($this->uid
!= S
::v('uid') ?
"(admin on {$this->uid})" : "") );
201 $this->active
= false
;
205 // public function set_rewrite() {{{2
207 public function set_rewrite($rewrite)
209 if ($this->rewrite
== $rewrite) {
212 if (!$rewrite ||
!isvalid_email($rewrite)) {
215 XDB
::execute('UPDATE emails SET rewrite={?} WHERE uid={?} AND email={?}', $rewrite, $this->uid
, $this->email
);
216 $this->rewrite
= $rewrite;
220 // public function clean_errors() {{{2
222 public function clean_errors()
224 if (!S
::has_perms()) {
228 $this->panne_level
= 0;
230 return XDB
::execute("UPDATE emails
231 SET panne_level = 0, panne = 0, last = 0
232 WHERE uid = {?} AND email = {?}",
233 $this->uid
, $this->email
);
236 // public function has_rewrite() {{{2
238 public function has_rewrite()
243 // public function is_removable() {{{2
245 public function is_removable()
250 // public function has_disable() {{{2
252 public function has_disable()
258 // class EmailStorage {{{1
259 // Implementation of Email for email storage backends from Polytechnique.org.
260 class EmailStorage
extends Email
262 // Shortname to realname mapping for known mail storage backends.
263 private $display_names = array(
264 'imap' => 'Accès de secours aux emails (IMAP)',
265 'googleapps' => 'Compte Google Apps',
268 // Retrieves the current list of actives storages.
269 private function get_storages()
271 $res = XDB
::query("SELECT mail_storage
273 WHERE user_id = {?}", $this->uid
);
274 return new PlFlagSet($res->fetchOneCell());
277 // Updates the list of active storages.
278 private function set_storages($storages)
280 XDB
::execute("UPDATE auth_user_md5
281 SET mail_storage = {?}
282 WHERE user_id = {?}", $storages, $this->uid
);
285 // Returns the list of allowed storages for the @p user.
286 static public function get_allowed_storages($uid)
291 // Google Apps storage is available for users with valid Google Apps account.
292 require_once 'googleapps.inc.php';
293 if ($globals->mailstorage
->googleapps_domain
&&
294 GoogleAppsAccount
::account_status($uid) == 'active') {
295 $storages[] = 'googleapps';
298 // IMAP storage is always visible to administrators, and is allowed for
299 // everyone when the service is marked as 'active'.
300 if ($globals->mailstorage
->imap_active || S
::has_perms()) {
301 $storages[] = 'imap';
308 public function __construct($uid, $name)
311 $this->email
= $name;
312 $this->display_email
= (isset($this->display_names
[$name]) ?
$this->display_names
[$name] : $name);
314 $storages = $this->get_storages();
315 $this->sufficient
= ($name == 'googleapps');
316 $this->active
= $storages->hasFlag($name);
317 $this->broken
= false
;
318 $this->disabled
= false
;
320 $this->panne
= $this->last
= $this->panne_level
= 0;
323 public function activate()
325 if (!$this->active
) {
326 $storages = $this->get_storages();
327 $storages->addFlag($this->email
);
328 $this->set_storages($storages);
329 $this->active
= true
;
333 public function deactivate()
336 $storages = $this->get_storages();
337 $storages->rmFlag($this->email
);
338 $this->set_storages($storages);
339 $this->active
= false
;
344 // Source rewrite can't be enabled for email storage addresses.
345 public function set_rewrite($rewrite) {}
347 // Email storage are not supposed to be broken, hence not supposed to be
349 public function clean_errors() {}
352 public function has_rewrite() { return false
; }
353 public function is_removable() { return false
; }
354 public function has_disable() { return false
; }
357 // class Redirect {{{1
358 // Redirect is a placeholder class for an user's active redirections (third-party
359 // redirection email, or Polytechnique.org mail storages).
364 private $flag_active = 'active';
372 public function __construct($_uid)
375 $this->bogo
= new Bogo($_uid);
377 // Adds third-party email redirections.
378 $res = XDB
::iterRow("SELECT email, flags, rewrite, panne, last, panne_level
380 WHERE uid = {?} AND flags != 'filter'", $_uid);
381 $this->emails
= Array();
382 while ($row = $res->next()) {
383 $this->emails
[] = new EmailRedirection($_uid, $row);
386 // Adds local email storage backends.
387 foreach (EmailStorage
::get_allowed_storages($_uid) as $storage) {
388 $this->emails
[] = new EmailStorage($_uid, $storage);
392 // public function other_active() {{{2
394 public function other_active($email)
396 foreach ($this->emails
as $mail) {
397 if ($mail->email
!= $email && $mail->active
&& $mail->sufficient
) {
404 // public function delete_email() {{{2
406 public function delete_email($email)
408 if (!$this->other_active($email)) {
409 return ERROR_INACTIVE_REDIRECTION
;
411 XDB
::execute('DELETE FROM emails WHERE uid={?} AND email={?}', $this->uid
, $email);
412 S
::logger()->log('email_del',$email.($this->uid
!=S
::v('uid') ?
" (admin on {$this->uid})" : ""));
413 foreach ($this->emails
as $i => $mail) {
414 if ($email == $mail->email
) {
415 unset($this->emails
[$i]);
418 check_redirect($this);
422 // public function add_email() {{{2
424 public function add_email($email)
426 $email_stripped = strtolower(trim($email));
427 if (!isvalid_email($email_stripped)) {
428 return ERROR_INVALID_EMAIL
;
430 if (!isvalid_email_redirection($email_stripped)) {
431 return ERROR_LOOP_EMAIL
;
433 XDB
::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->uid
, $email);
434 if ($logger = S
::v('log', null
)) { // may be absent --> step4.php
435 S
::logger()->log('email_add',$email.($this->uid
!=S
::v('uid') ?
" (admin on {$this->uid})" : ""));
437 foreach ($this->emails
as $mail) {
438 if ($mail->email
== $email_stripped) {
442 $this->emails
[] = new EmailRedirection($this->uid
, array($email, 'active', '', '0000-00-00', '0000-00-00', 0));
445 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->uid
);
446 check_redirect($this);
450 // public function modify_email() {{{2
452 public function modify_email($emails_actifs, $emails_rewrite)
454 foreach ($this->emails
as &$mail) {
455 if (in_array($mail->email
, $emails_actifs)) {
460 $mail->set_rewrite($emails_rewrite[$mail->email
]);
462 check_redirect($this);
465 // public function modify_one_email() {{{2
467 public function modify_one_email($email, $activate)
471 foreach ($this->emails
as $i=>$mail) {
472 if ($mail->email
== $email) {
475 $allinactive &= !$mail->active ||
!$mail->sufficient ||
$mail->email
== $email;
477 if ($thisone === false
) {
478 return ERROR_INVALID_EMAIL
;
480 if ($allinactive ||
$activate) {
481 $this->emails
[$thisone]->activate();
483 $this->emails
[$thisone]->deactivate();
485 check_redirect($this);
486 if ($allinactive && !$activate) {
487 return ERROR_INACTIVE_REDIRECTION
;
493 // public function modify_one_email_redirect() {{{2
495 public function modify_one_email_redirect($email, $redirect)
497 foreach ($this->emails
as &$mail) {
498 if ($mail->email
== $email) {
499 $mail->set_rewrite($redirect);
500 check_redirect($this);
506 // function clean_errors() {{{2
508 public function clean_errors($email)
510 foreach ($this->emails
as &$mail) {
511 if ($mail->email
== $email) {
512 check_redirect($this);
513 return $mail->clean_errors();
519 // function disable() {{{2
521 public function disable()
523 XDB
::execute("UPDATE emails
524 SET flags = 'disable'
525 WHERE flags = 'active' AND uid = {?}", $this->uid
);
526 foreach ($this->emails
as &$mail) {
527 if ($mail->active
&& $mail->has_disable()) {
528 $mail->disabled
= true
;
529 $mail->active
= false
;
532 check_redirect($this);
535 // function enable() {{{2
537 public function enable()
539 XDB
::execute("UPDATE emails
541 WHERE flags = 'disable' AND uid = {?}", $this->uid
);
542 foreach ($this->emails
as &$mail) {
543 if ($mail->disabled
) {
544 $mail->active
= true
;
545 $mail->disabled
= false
;
547 check_redirect($this);
551 // function get_broken_mx() {{{2
553 public function get_broken_mx()
555 $res = XDB
::query("SELECT host, text
557 WHERE state != 'ok'");
558 if (!$res->numRows()) {
561 $mxs = $res->fetchAllAssoc();
563 foreach ($this->emails
as &$mail) {
564 if ($mail->active
&& strstr($mail->email
, '@') !== false
) {
565 list(,$domain) = explode('@', $mail->email
);
566 getmxrr($domain, $lcl_mxs);
567 if (empty($lcl_mxs)) {
568 $lcl_mxs = array($domain);
571 foreach ($mxs as &$mx) {
572 foreach ($lcl_mxs as $lcl) {
573 if (fnmatch($mx['host'], $lcl)) {
574 $broken = $mx['text'];
579 $mails[] = array('mail' => $mail->email
, 'text' => $broken);
588 // function active_emails() {{{2
590 public function active_emails()
593 foreach ($this->emails
as $mail) {
601 // function get_uid() {{{2
603 public function get_uid()
609 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: