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 ***************************************************************************/
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
30 // Checks for an existing 'bestalias' among the the current user's aliases, and
31 // eventually selects a new bestalias when required.
32 function fix_bestalias($uid)
34 $res = XDB
::query("SELECT COUNT(*)
36 WHERE id = {?} AND FIND_IN_SET('bestalias', flags) AND type != 'homonyme'",
38 if ($res->fetchOneCell()) {
42 XDB
::execute("UPDATE aliases
43 SET flags=CONCAT(flags,',','bestalias')
44 WHERE id={?} AND type!='homonyme'
45 ORDER BY !FIND_IN_SET('usage',flags),alias LIKE '%.%', LENGTH(alias)
49 // function valide_email() {{{1
50 // Returns a cleaned-up version of the @p email string. It removes garbage
51 // characters, and determines the canonical form (without _ and +) for
52 // Polytechnique.org email addresses.
53 function valide_email($str)
57 $em = trim(rtrim($str));
58 $em = str_replace('<', '', $em);
59 $em = str_replace('>', '', $em);
60 list($ident, $dom) = explode('@', $em);
61 if ($dom == $globals->mail
->domain
or $dom == $globals->mail
->domain2
) {
62 list($ident1) = explode('_', $ident);
63 list($ident) = explode('+', $ident1);
65 return $ident . '@' . $dom;
69 // The Bogo class represents a spam filtering level in plat/al architecture.
76 private $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams');
80 public function __construct($uid)
87 $res = XDB
::query('SELECT email FROM emails WHERE uid={?} AND flags="filter"', $uid);
88 if ($res->numRows()) {
89 $this->state
= $res->fetchOneCell();
91 $this->state
= 'tag_and_drop_spams';
92 $res = XDB
::query("INSERT INTO emails (uid,email,rewrite,panne,flags)
93 VALUES ({?},'tag_and_drop_spams','','0000-00-00','filter')", $uid);
97 // public function change() {{{2
99 public function change($state)
101 $this->state
= is_int($state) ?
$this->_states
[$state] : $state;
102 XDB
::execute('UPDATE emails SET email={?} WHERE uid={?} AND flags = "filter"',
103 $this->state
, $this->uid
);
106 // pubic function level() {{{2
108 public function level()
110 return array_search($this->state
, $this->_states
);
115 // Represents an "email address" used as final recipient for plat/al-managed
116 // addresses; it can be subclasses a Redirection emails (third-party) or as
117 // Storage emails (Polytechnique.org).
122 // Basic email properties; $sufficient indicates if the email can be used as
123 // an unique redirection; $email contains the delivery email address.
127 public $display_email;
129 // Redirection status properties.
135 // Redirection bounces stats.
140 // Activates the email address as a redirection.
141 public abstract function activate();
143 // Deactivates the email address as a redirection.
144 public abstract function deactivate();
146 // Sets the rewrite rule for the given address.
147 public abstract function set_rewrite($rewrite);
149 // Resets the error counts associated with the redirection.
150 public abstract function clean_errors();
152 // Email backend capabilities ('rewrite' refers to From: rewrite for mails
153 // forwarded by Polytechnique.org's MXs; 'removable' indicates if the email
154 // can be definitively removed; 'disable' indicates if the email has a third
155 // status 'disabled' in addition to 'active' and 'inactive').
156 public abstract function has_rewrite();
157 public abstract function is_removable();
158 public abstract function has_disable();
161 // class EmailRedirection {{{1
162 // Implementation of Email for third-party redirection (redirection of emails to
163 // external user-supplied addresses).
164 class EmailRedirection
extends Email
168 public function __construct($uid, $row)
171 $this->sufficient
= true
;
173 list($this->email
, $flags, $this->rewrite
, $this->panne
, $this->last
, $this->panne_level
) = $row;
174 $this->display_email
= $this->email
;
175 $this->active
= ($flags == 'active');
176 $this->broken
= ($flags == 'panne');
177 $this->disabled
= ($flags == 'disable');
180 // public function activate() {{{2
182 public function activate()
184 if (!$this->active
) {
185 XDB
::execute("UPDATE emails
186 SET panne_level = IF(flags = 'panne', panne_level - 1, panne_level),
188 WHERE uid={?} AND email={?}", $this->uid
, $this->email
);
189 $_SESSION['log']->log("email_on", $this->email
.($this->uid
!=S
::v('uid') ?
"(admin on {$this->uid})" : ""));
190 $this->active
= true
;
191 $this->broken
= false
;
195 // public function deactivate() {{{2
197 public function deactivate()
200 XDB
::execute("UPDATE emails SET flags =''
201 WHERE uid={?} AND email={?}", $this->uid
, $this->email
);
202 $_SESSION['log']->log("email_off",$this->email
.($this->uid
!= S
::v('uid') ?
"(admin on {$this->uid})" : "") );
203 $this->active
= false
;
207 // public function set_rewrite() {{{2
209 public function set_rewrite($rewrite)
211 if ($this->rewrite
== $rewrite) {
214 if (!$rewrite ||
!isvalid_email($rewrite)) {
217 XDB
::execute('UPDATE emails SET rewrite={?} WHERE uid={?} AND email={?}', $rewrite, $this->uid
, $this->email
);
218 $this->rewrite
= $rewrite;
222 // public function clean_errors() {{{2
224 public function clean_errors()
226 if (!S
::has_perms()) {
230 $this->panne_level
= 0;
232 return XDB
::execute("UPDATE emails
233 SET panne_level = 0, panne = 0, last = 0
234 WHERE uid = {?} AND email = {?}",
235 $this->uid
, $this->email
);
238 // public function has_rewrite() {{{2
240 public function has_rewrite()
245 // public function is_removable() {{{2
247 public function is_removable()
252 // public function has_disable() {{{2
254 public function has_disable()
260 // class EmailStorage {{{1
261 // Implementation of Email for email storage backends from Polytechnique.org.
262 class EmailStorage
extends Email
264 // Shortname to realname mapping for known mail storage backends.
265 private $display_names = array(
266 'imap' => 'Accès de secours aux emails (IMAP)',
267 'googleapps' => 'Compte Google Apps',
270 // Retrieves the current list of actives storages.
271 private function get_storages()
273 $res = XDB
::query("SELECT mail_storage
275 WHERE user_id = {?}", $this->uid
);
276 return new FlagSet($res->fetchOneCell());
279 // Updates the list of active storages.
280 private function set_storages($storages)
282 XDB
::execute("UPDATE auth_user_md5
283 SET mail_storage = {?}
284 WHERE user_id = {?}", $storages->flags(), $this->uid
);
287 // Returns the list of allowed storages for the @p user.
288 static public function get_allowed_storages($uid)
293 // Google Apps storage is available for users with valid Google Apps account.
294 require_once 'googleapps.inc.php';
295 if ($globals->mailstorage
->googleapps_domain
&&
296 GoogleAppsAccount
::account_status($uid) == 'active') {
297 $storages[] = 'googleapps';
300 // IMAP storage is always visible to administrators, and is allowed for
301 // everyone when the service is marked as 'active'.
302 if ($globals->mailstorage
->imap_active || S
::has_perms()) {
303 $storages[] = 'imap';
310 public function __construct($uid, $name)
313 $this->email
= $name;
314 $this->display_email
= (isset($this->display_names
[$name]) ?
$this->display_names
[$name] : $name);
316 $storages = $this->get_storages();
317 $this->sufficient
= ($name == 'googleapps');
318 $this->active
= $storages->hasFlag($name);
319 $this->broken
= false
;
320 $this->disabled
= false
;
322 $this->panne
= $this->last
= $this->panne_level
= 0;
325 public function activate()
327 if (!$this->active
) {
328 $storages = $this->get_storages();
329 $storages->addFlag($this->email
);
330 $this->set_storages($storages);
331 $this->active
= true
;
335 public function deactivate()
338 $storages = $this->get_storages();
339 $storages->rmFlag($this->email
);
340 $this->set_storages($storages);
341 $this->active
= false
;
346 // Source rewrite can't be enabled for email storage addresses.
347 public function set_rewrite($rewrite) {}
349 // Email storage are not supposed to be broken, hence not supposed to be
351 public function clean_errors() {}
354 public function has_rewrite() { return false
; }
355 public function is_removable() { return false
; }
356 public function has_disable() { return false
; }
359 // class Redirect {{{1
360 // Redirect is a placeholder class for an user's active redirections (third-party
361 // redirection email, or Polytechnique.org mail storages).
366 private $flag_active = 'active';
374 public function __construct($_uid)
377 $this->bogo
= new Bogo($_uid);
379 // Adds third-party email redirections.
380 $res = XDB
::iterRow("SELECT email, flags, rewrite, panne, last, panne_level
382 WHERE uid = {?} AND flags != 'filter'", $_uid);
383 $this->emails
= Array();
384 while ($row = $res->next()) {
385 $this->emails
[] = new EmailRedirection($_uid, $row);
388 // Adds local email storage backends.
389 foreach (EmailStorage
::get_allowed_storages($_uid) as $storage) {
390 $this->emails
[] = new EmailStorage($_uid, $storage);
394 // public function other_active() {{{2
396 public function other_active($email)
398 foreach ($this->emails
as $mail) {
399 if ($mail->email
!= $email && $mail->active
&& $mail->sufficient
) {
406 // public function delete_email() {{{2
408 public function delete_email($email)
410 if (!$this->other_active($email)) {
411 return ERROR_INACTIVE_REDIRECTION
;
413 XDB
::execute('DELETE FROM emails WHERE uid={?} AND email={?}', $this->uid
, $email);
414 $_SESSION['log']->log('email_del',$email.($this->uid
!=S
::v('uid') ?
" (admin on {$this->uid})" : ""));
415 foreach ($this->emails
as $i => $mail) {
416 if ($email == $mail->email
) {
417 unset($this->emails
[$i]);
420 check_redirect($this);
424 // public function add_email() {{{2
426 public function add_email($email)
428 $email_stripped = strtolower(trim($email));
429 if (!isvalid_email($email_stripped)) {
430 return ERROR_INVALID_EMAIL
;
432 if (!isvalid_email_redirection($email_stripped)) {
433 return ERROR_LOOP_EMAIL
;
435 XDB
::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->uid
, $email);
436 if ($logger = S
::v('log', null
)) { // may be absent --> step4.php
437 $logger->log('email_add',$email.($this->uid
!=S
::v('uid') ?
" (admin on {$this->uid})" : ""));
439 foreach ($this->emails
as $mail) {
440 if ($mail->email
== $email_stripped) {
444 $this->emails
[] = new EmailRedirection($this->uid
, array($email, 'active', '', '0000-00-00', '0000-00-00', 0));
447 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->uid
);
448 check_redirect($this);
452 // public function modify_email() {{{2
454 public function modify_email($emails_actifs, $emails_rewrite)
456 foreach ($this->emails
as &$mail) {
457 if (in_array($mail->email
, $emails_actifs)) {
462 $mail->set_rewrite($emails_rewrite[$mail->email
]);
464 check_redirect($this);
467 // public function modify_one_email() {{{2
469 public function modify_one_email($email, $activate)
473 foreach ($this->emails
as $i=>$mail) {
474 if ($mail->email
== $email) {
477 $allinactive &= !$mail->active ||
!$mail->sufficient ||
$mail->email
== $email;
479 if ($thisone === false
) {
480 return ERROR_INVALID_EMAIL
;
482 if ($allinactive ||
$activate) {
483 $this->emails
[$thisone]->activate();
485 $this->emails
[$thisone]->deactivate();
487 check_redirect($this);
488 if ($allinactive && !$activate) {
489 return ERROR_INACTIVE_REDIRECTION
;
495 // public function modify_one_email_redirect() {{{2
497 public function modify_one_email_redirect($email, $redirect)
499 foreach ($this->emails
as &$mail) {
500 if ($mail->email
== $email) {
501 $mail->set_rewrite($redirect);
502 check_redirect($this);
508 // function clean_errors() {{{2
510 public function clean_errors($email)
512 foreach ($this->emails
as &$mail) {
513 if ($mail->email
== $email) {
514 check_redirect($this);
515 return $mail->clean_errors();
521 // function disable() {{{2
523 public function disable()
525 XDB
::execute("UPDATE emails
526 SET flags = 'disable'
527 WHERE flags = 'active' AND uid = {?}", $this->uid
);
528 foreach ($this->emails
as &$mail) {
529 if ($mail->active
&& $mail->has_disable()) {
530 $mail->disabled
= true
;
531 $mail->active
= false
;
534 check_redirect($this);
537 // function enable() {{{2
539 public function enable()
541 XDB
::execute("UPDATE emails
543 WHERE flags = 'disable' AND uid = {?}", $this->uid
);
544 foreach ($this->emails
as &$mail) {
545 if ($mail->disabled
) {
546 $mail->active
= true
;
547 $mail->disabled
= false
;
549 check_redirect($this);
553 // function get_broken_mx() {{{2
555 public function get_broken_mx()
557 $res = XDB
::query("SELECT host, text
559 WHERE state != 'ok'");
560 if (!$res->numRows()) {
563 $mxs = $res->fetchAllAssoc();
565 foreach ($this->emails
as &$mail) {
566 if ($mail->active
&& strstr($mail->email
, '@') !== false
) {
567 list(,$domain) = explode('@', $mail->email
);
568 getmxrr($domain, $lcl_mxs);
569 if (empty($lcl_mxs)) {
570 $lcl_mxs = array($domain);
573 foreach ($mxs as &$mx) {
574 foreach ($lcl_mxs as $lcl) {
575 if (fnmatch($mx['host'], $lcl)) {
576 $broken = $mx['text'];
581 $mails[] = array('mail' => $mail->email
, 'text' => $broken);
590 // function active_emails() {{{2
592 public function active_emails()
595 foreach ($this->emails
as $mail) {
603 // function get_uid() {{{2
605 public function get_uid()
611 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: