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;
66 // function isvalid_email_redirection() {{{1
67 /** vérifie si une adresse email convient comme adresse de redirection
68 * @param $email l'adresse email a verifier
71 function isvalid_email_redirection($email)
73 return isvalid_email($email) &&
74 !preg_match("/@(polytechnique\.(org|edu)|melix\.(org|net)|m4x\.org)$/", $email);
78 // The Bogo class represents a spam filtering level in plat/al architecture.
85 private $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams');
89 public function __construct($uid)
96 $res = XDB
::query('SELECT email FROM emails WHERE uid={?} AND flags="filter"', $uid);
97 if ($res->numRows()) {
98 $this->state
= $res->fetchOneCell();
100 $this->state
= 'tag_and_drop_spams';
101 $res = XDB
::query("INSERT INTO emails (uid,email,rewrite,panne,flags)
102 VALUES ({?},'tag_and_drop_spams','','0000-00-00','filter')", $uid);
106 // public function change() {{{2
108 public function change($state)
110 $this->state
= is_int($state) ?
$this->_states
[$state] : $state;
111 XDB
::execute('UPDATE emails SET email={?} WHERE uid={?} AND flags = "filter"',
112 $this->state
, $this->uid
);
115 // pubic function level() {{{2
117 public function level()
119 return array_search($this->state
, $this->_states
);
124 // Represents an "email address" used as final recipient for plat/al-managed
125 // addresses; it can be subclasses a Redirection emails (third-party) or as
126 // Storage emails (Polytechnique.org).
131 // Basic email properties; $sufficient indicates if the email can be used as
132 // an unique redirection; $email contains the delivery email address.
136 public $display_email;
138 // Redirection status properties.
143 public $allow_rewrite;
146 // Redirection bounces stats.
151 // Activates the email address as a redirection.
152 public abstract function activate();
154 // Deactivates the email address as a redirection.
155 public abstract function deactivate();
157 // Sets the rewrite rule for the given address.
158 public abstract function set_rewrite($rewrite);
160 // Resets the error counts associated with the redirection.
161 public abstract function clean_errors();
163 // Email backend capabilities ('rewrite' refers to From: rewrite for mails
164 // forwarded by Polytechnique.org's MXs; 'removable' indicates if the email
165 // can be definitively removed; 'disable' indicates if the email has a third
166 // status 'disabled' in addition to 'active' and 'inactive').
167 public abstract function has_rewrite();
168 public abstract function is_removable();
169 public abstract function has_disable();
172 // class EmailRedirection {{{1
173 // Implementation of Email for third-party redirection (redirection of emails to
174 // external user-supplied addresses).
175 class EmailRedirection
extends Email
179 public function __construct($uid, $row)
182 $this->sufficient
= true
;
184 list($this->email
, $flags, $this->rewrite
, $this->allow_rewrite
, $this->hash
, $this->panne
, $this->last
, $this->panne_level
) = $row;
185 $this->display_email
= $this->email
;
186 $this->active
= ($flags == 'active');
187 $this->broken
= ($flags == 'panne');
188 $this->disabled
= ($flags == 'disable');
191 // public function activate() {{{2
193 public function activate()
195 if (!$this->active
) {
196 XDB
::execute("UPDATE emails
197 SET panne_level = IF(flags = 'panne', panne_level - 1, panne_level),
199 WHERE uid={?} AND email={?}", $this->uid
, $this->email
);
200 S
::logger()->log("email_on", $this->email
.($this->uid
!=S
::v('uid') ?
"(admin on {$this->uid})" : ""));
201 $this->active
= true
;
202 $this->broken
= false
;
206 // public function deactivate() {{{2
208 public function deactivate()
211 XDB
::execute("UPDATE emails SET flags =''
212 WHERE uid={?} AND email={?}", $this->uid
, $this->email
);
213 S
::logger()->log("email_off",$this->email
.($this->uid
!= S
::v('uid') ?
"(admin on {$this->uid})" : "") );
214 $this->active
= false
;
218 // public function set_rewrite() {{{2
220 public function set_rewrite($rewrite)
222 if ($this->rewrite
== $rewrite) {
225 if (!$rewrite ||
!isvalid_email($rewrite)) {
228 XDB
::execute('UPDATE emails SET rewrite={?} WHERE uid={?} AND email={?}', $rewrite, $this->uid
, $this->email
);
229 $this->rewrite
= $rewrite;
230 if (!$this->allow_rewrite
) {
232 if (empty($this->hash
)) {
233 $this->hash
= rand_url_id();
234 XDB
::execute("UPDATE emails
236 WHERE uid = {?} AND email = {?}", $this->hash
, $this->uid
, $this->email
);
238 $res = XDB
::query("SELECT IF(u.nom_usage = '', u.nom, u.nom_usage) AS nom, u.prenom, FIND_IN_SET('femme', u.flags) AS sex,
239 q.core_mail_fmt, a.alias AS forlife, a2.alias AS bestalias
240 FROM auth_user_md5 AS u
241 INNER JOIN auth_user_quick AS q ON (u.user_id = q.user_id)
242 INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type = 'a_vie')
243 INNER JOIN aliases AS a2 ON (a2.id = u.user_id AND FIND_IN_SET('bestalias', a2.flags))
244 WHERE u.user_id = {?}", $this->uid
);
245 list($nom, $prenom, $sexe, $fmt, $forlife, $bestalias) = $res->fetchOneRow();
246 $mail = new PlMailer('emails/rewrite-in.mail.tpl');
247 $mail->assign('mail', $this);
248 $mail->assign('nom', $nom);
249 $mail->assign('prenom', $prenom);
250 $mail->assign('sexe', $sexe);
251 $mail->assign('forlife', $forlife);
252 $mail->assign('baseurl', $globals->baseurl
);
253 $mail->assign('sitename', $globals->core
->sitename
);
254 $mail->assign('to', $this->email
);
255 $mail->send($fmt == 'html');
260 // public function clean_errors() {{{2
262 public function clean_errors()
264 if (!S
::has_perms()) {
268 $this->panne_level
= 0;
270 return XDB
::execute("UPDATE emails
271 SET panne_level = 0, panne = 0, last = 0
272 WHERE uid = {?} AND email = {?}",
273 $this->uid
, $this->email
);
276 // public function has_rewrite() {{{2
278 public function has_rewrite()
283 // public function is_removable() {{{2
285 public function is_removable()
290 // public function has_disable() {{{2
292 public function has_disable()
298 // class EmailStorage {{{1
299 // Implementation of Email for email storage backends from Polytechnique.org.
300 class EmailStorage
extends Email
302 // Shortname to realname mapping for known mail storage backends.
303 private $display_names = array(
304 'imap' => 'Accès de secours aux emails (IMAP)',
305 'googleapps' => 'Compte Google Apps',
308 // Retrieves the current list of actives storages.
309 private function get_storages()
311 $res = XDB
::query("SELECT mail_storage
313 WHERE user_id = {?}", $this->uid
);
314 return new PlFlagSet($res->fetchOneCell());
317 // Updates the list of active storages.
318 private function set_storages($storages)
320 XDB
::execute("UPDATE auth_user_md5
321 SET mail_storage = {?}
322 WHERE user_id = {?}", $storages, $this->uid
);
325 // Returns the list of allowed storages for the @p user.
326 static public function get_allowed_storages($uid)
331 // Google Apps storage is available for users with valid Google Apps account.
332 require_once 'googleapps.inc.php';
333 if ($globals->mailstorage
->googleapps_domain
&&
334 GoogleAppsAccount
::account_status($uid) == 'active') {
335 $storages[] = 'googleapps';
338 // IMAP storage is always visible to administrators, and is allowed for
339 // everyone when the service is marked as 'active'.
340 if ($globals->mailstorage
->imap_active || S
::has_perms()) {
341 $storages[] = 'imap';
348 public function __construct($uid, $name)
351 $this->email
= $name;
352 $this->display_email
= (isset($this->display_names
[$name]) ?
$this->display_names
[$name] : $name);
354 $storages = $this->get_storages();
355 $this->sufficient
= ($name == 'googleapps');
356 $this->active
= $storages->hasFlag($name);
357 $this->broken
= false
;
358 $this->disabled
= false
;
360 $this->panne
= $this->last
= $this->panne_level
= 0;
363 public function activate()
365 if (!$this->active
) {
366 $storages = $this->get_storages();
367 $storages->addFlag($this->email
);
368 $this->set_storages($storages);
369 $this->active
= true
;
373 public function deactivate()
376 $storages = $this->get_storages();
377 $storages->rmFlag($this->email
);
378 $this->set_storages($storages);
379 $this->active
= false
;
384 // Source rewrite can't be enabled for email storage addresses.
385 public function set_rewrite($rewrite) {}
387 // Email storage are not supposed to be broken, hence not supposed to be
389 public function clean_errors() {}
392 public function has_rewrite() { return false
; }
393 public function is_removable() { return false
; }
394 public function has_disable() { return false
; }
397 // class Redirect {{{1
398 // Redirect is a placeholder class for an user's active redirections (third-party
399 // redirection email, or Polytechnique.org mail storages).
404 private $flag_active = 'active';
412 public function __construct($_uid)
415 $this->bogo
= new Bogo($_uid);
417 // Adds third-party email redirections.
418 $res = XDB
::iterRow("SELECT email, flags, rewrite, allow_rewrite, hash, panne, last, panne_level
420 WHERE uid = {?} AND flags != 'filter'", $_uid);
421 $this->emails
= Array();
422 while ($row = $res->next()) {
423 $this->emails
[] = new EmailRedirection($_uid, $row);
426 // Adds local email storage backends.
427 foreach (EmailStorage
::get_allowed_storages($_uid) as $storage) {
428 $this->emails
[] = new EmailStorage($_uid, $storage);
432 // public function other_active() {{{2
434 public function other_active($email)
436 foreach ($this->emails
as $mail) {
437 if ($mail->email
!= $email && $mail->active
&& $mail->sufficient
) {
444 // public function delete_email() {{{2
446 public function delete_email($email)
448 if (!$this->other_active($email)) {
449 return ERROR_INACTIVE_REDIRECTION
;
451 XDB
::execute('DELETE FROM emails WHERE uid={?} AND email={?}', $this->uid
, $email);
452 S
::logger()->log('email_del',$email.($this->uid
!=S
::v('uid') ?
" (admin on {$this->uid})" : ""));
453 foreach ($this->emails
as $i => $mail) {
454 if ($email == $mail->email
) {
455 unset($this->emails
[$i]);
458 check_redirect($this);
462 // public function add_email() {{{2
464 public function add_email($email)
466 $email_stripped = strtolower(trim($email));
467 if (!isvalid_email($email_stripped)) {
468 return ERROR_INVALID_EMAIL
;
470 if (!isvalid_email_redirection($email_stripped)) {
471 return ERROR_LOOP_EMAIL
;
473 XDB
::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->uid
, $email);
474 if ($logger = S
::v('log', null
)) { // may be absent --> step4.php
475 S
::logger()->log('email_add',$email.($this->uid
!=S
::v('uid') ?
" (admin on {$this->uid})" : ""));
477 foreach ($this->emails
as $mail) {
478 if ($mail->email
== $email_stripped) {
482 $this->emails
[] = new EmailRedirection($this->uid
, array($email, 'active', '', 0, null
, '0000-00-00', '0000-00-00', 0));
485 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->uid
);
486 check_redirect($this);
490 // public function modify_email() {{{2
492 public function modify_email($emails_actifs, $emails_rewrite)
494 foreach ($this->emails
as &$mail) {
495 if (in_array($mail->email
, $emails_actifs)) {
500 $mail->set_rewrite($emails_rewrite[$mail->email
]);
502 check_redirect($this);
505 // public function modify_one_email() {{{2
507 public function modify_one_email($email, $activate)
511 foreach ($this->emails
as $i=>$mail) {
512 if ($mail->email
== $email) {
515 $allinactive &= !$mail->active ||
!$mail->sufficient ||
$mail->email
== $email;
517 if ($thisone === false
) {
518 return ERROR_INVALID_EMAIL
;
520 if ($allinactive ||
$activate) {
521 $this->emails
[$thisone]->activate();
523 $this->emails
[$thisone]->deactivate();
525 check_redirect($this);
526 if ($allinactive && !$activate) {
527 return ERROR_INACTIVE_REDIRECTION
;
533 // public function modify_one_email_redirect() {{{2
535 public function modify_one_email_redirect($email, $redirect)
537 foreach ($this->emails
as &$mail) {
538 if ($mail->email
== $email) {
539 $mail->set_rewrite($redirect);
540 check_redirect($this);
546 // function clean_errors() {{{2
548 public function clean_errors($email)
550 foreach ($this->emails
as &$mail) {
551 if ($mail->email
== $email) {
552 check_redirect($this);
553 return $mail->clean_errors();
559 // function disable() {{{2
561 public function disable()
563 XDB
::execute("UPDATE emails
564 SET flags = 'disable'
565 WHERE flags = 'active' AND uid = {?}", $this->uid
);
566 foreach ($this->emails
as &$mail) {
567 if ($mail->active
&& $mail->has_disable()) {
568 $mail->disabled
= true
;
569 $mail->active
= false
;
572 check_redirect($this);
575 // function enable() {{{2
577 public function enable()
579 XDB
::execute("UPDATE emails
581 WHERE flags = 'disable' AND uid = {?}", $this->uid
);
582 foreach ($this->emails
as &$mail) {
583 if ($mail->disabled
) {
584 $mail->active
= true
;
585 $mail->disabled
= false
;
587 check_redirect($this);
591 // function get_broken_mx() {{{2
593 public function get_broken_mx()
595 $res = XDB
::query("SELECT host, text
597 WHERE state != 'ok'");
598 if (!$res->numRows()) {
601 $mxs = $res->fetchAllAssoc();
603 foreach ($this->emails
as &$mail) {
604 if ($mail->active
&& strstr($mail->email
, '@') !== false
) {
605 list(,$domain) = explode('@', $mail->email
);
606 getmxrr($domain, $lcl_mxs);
607 if (empty($lcl_mxs)) {
608 $lcl_mxs = array($domain);
611 foreach ($mxs as &$mx) {
612 foreach ($lcl_mxs as $lcl) {
613 if (fnmatch($mx['host'], $lcl)) {
614 $broken = $mx['text'];
619 $mails[] = array('mail' => $mail->email
, 'text' => $broken);
628 // function active_emails() {{{2
630 public function active_emails()
633 foreach ($this->emails
as $mail) {
641 // function get_uid() {{{2
643 public function get_uid()
649 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: