2 /***************************************************************************
3 * Copyright (C) 2003-2009 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(User
&$user)
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)
44 LIMIT 1", $user->id());
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);
77 // function idsFromMails() {{{1
78 /** Converts an array of emails to an array of email => uid
79 * @param $emails : array of emails
80 * @return array of ($email => $uid)
82 function idsFromMails($emails)
85 $domain_mails = array();
86 $alias_mails = array();
87 $other_mails = array();
88 /* Find type of email address */
89 foreach ($emails as $email) {
90 if (strpos($email, '@') === false
) {
92 $domain = $globals->mail
->domain2
;
94 list($user, $domain) = explode('@', $email);
96 if ($domain == $globals->mail
->alias_dom ||
$domain == $globals->mail
->alias_dom2
) {
97 list($user) = explode('+', $user);
98 list($user) = explode('_', $user);
99 $alias_mails[$user] = $email;
100 } elseif ($domain == $globals->mail
->domain ||
$domain == $globals->mail
->domain2
) {
101 list($user) = explode('+', $user);
102 list($user) = explode('_', $user);
103 $domain_mails[$user] = $email;
105 $other_mails[] = $email;
110 if (count($domain_mails)) {
111 $domain_users = array();
112 foreach (array_keys($domain_mails) as $user) {
113 $domain_users[] = XDB
::escape($user);
115 $list = implode(',', $domain_users);
116 $res = XDB
::query("SELECT alias, id
118 WHERE alias IN ($list)");
119 foreach ($res->fetchAllRow() as $row) {
120 list ($alias, $id) = $row;
121 $uids[$domain_mails[$alias]] = $id;
126 if (count($alias_mails)) {
127 $alias_users = array();
128 foreach (array_keys($alias_mails) as $user) {
129 $alias_users[] = XDB
::escape($user."@".$globals->mail
->alias_dom
);
131 $list = implode(',', $alias_users);
132 $res = XDB
::query("SELECT v.alias, a.id
134 INNER JOIN virtual_redirect AS r USING(vid)
135 INNER JOIN aliases AS a ON (a.type = 'a_vie'
136 AND r.redirect = CONCAT(a.alias, '@{$globals->mail->domain2}'))
137 WHERE v.alias IN ($list)");
138 foreach ($res->fetchAllRow() as $row) {
139 list ($alias, $id) = $row;
140 $uids[$alias_mails[$alias]] = $id;
145 if (count($other_mails)) {
146 $other_users = array();
147 foreach (array_keys($other_mails) as $user) {
148 $other_users[] = XDB
::escape($user);
150 $list = implode(',', $other_users);
151 $res = XDB
::query("SELECT email, uid
153 WHERE email IN ($list)");
154 foreach ($res->fetchAllRow() as $row) {
155 list ($email, $uid) = $row;
156 $uids[$other_mails[$email]] = $uid;
164 // The Bogo class represents a spam filtering level in plat/al architecture.
171 private $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams');
175 public function __construct(User
&$user)
181 $this->user
= &$user;
182 $res = XDB
::query('SELECT email FROM emails WHERE uid = {?} AND flags = "filter"', $user->id());
183 if ($res->numRows()) {
184 $this->state
= $res->fetchOneCell();
186 $this->state
= 'tag_and_drop_spams';
188 "INSERT INTO emails (uid, email, rewrite, panne, flags)
189 VALUES ({?}, 'tag_and_drop_spams', '', '0000-00-00', 'filter')",
194 // public function change() {{{2
196 public function change($state)
198 $this->state
= is_int($state) ?
$this->_states
[$state] : $state;
199 XDB
::execute('UPDATE emails SET email = {?} WHERE uid = {?} AND flags = "filter"',
200 $this->state
, $this->user
->id());
203 // pubic function level() {{{2
205 public function level()
207 return array_search($this->state
, $this->_states
);
212 // Represents an "email address" used as final recipient for plat/al-managed
213 // addresses; it can be subclasses a Redirection emails (third-party) or as
214 // Storage emails (Polytechnique.org).
219 // Basic email properties; $sufficient indicates if the email can be used as
220 // an unique redirection; $email contains the delivery email address.
224 public $display_email;
226 // Redirection status properties.
231 public $allow_rewrite;
234 // Redirection bounces stats.
239 // Activates the email address as a redirection.
240 public abstract function activate();
242 // Deactivates the email address as a redirection.
243 public abstract function deactivate();
245 // Sets the rewrite rule for the given address.
246 public abstract function set_rewrite($rewrite);
248 // Resets the error counts associated with the redirection.
249 public abstract function clean_errors();
251 // Email backend capabilities ('rewrite' refers to From: rewrite for mails
252 // forwarded by Polytechnique.org's MXs; 'removable' indicates if the email
253 // can be definitively removed; 'disable' indicates if the email has a third
254 // status 'disabled' in addition to 'active' and 'inactive').
255 public abstract function has_rewrite();
256 public abstract function is_removable();
257 public abstract function has_disable();
260 // class EmailRedirection {{{1
261 // Implementation of Email for third-party redirection (redirection of emails to
262 // external user-supplied addresses).
263 class EmailRedirection
extends Email
267 public function __construct(User
&$user, $row)
269 $this->user
= &$user;
270 $this->sufficient
= true
;
272 list($this->email
, $flags, $this->rewrite
, $this->allow_rewrite
, $this->hash
, $this->panne
, $this->last
, $this->panne_level
) = $row;
273 $this->display_email
= $this->email
;
274 $this->active
= ($flags == 'active');
275 $this->broken
= ($flags == 'panne');
276 $this->disabled
= ($flags == 'disable');
279 // public function activate() {{{2
281 public function activate()
283 if (!$this->active
) {
284 XDB
::execute("UPDATE emails
285 SET panne_level = IF(flags = 'panne', panne_level - 1, panne_level),
287 WHERE uid = {?} AND email = {?}", $this->user
->id(), $this->email
);
288 S
::logger()->log("email_on", $this->email
. ($this->user
->id() != S
::v('uid') ?
"(admin on {$this->user->login()})" : ""));
289 $this->active
= true
;
290 $this->broken
= false
;
294 // public function deactivate() {{{2
296 public function deactivate()
299 XDB
::execute("UPDATE emails SET flags =''
300 WHERE uid = {?} AND email = {?}", $this->user
->id(), $this->email
);
301 S
::logger()->log("email_off", $this->email
. ($this->user
->id() != S
::v('uid') ?
"(admin on {$this->user->login()})" : "") );
302 $this->active
= false
;
306 // public function set_rewrite() {{{2
308 public function set_rewrite($rewrite)
310 if ($this->rewrite
== $rewrite) {
313 if (!$rewrite ||
!isvalid_email($rewrite)) {
316 XDB
::execute('UPDATE emails SET rewrite = {?} WHERE uid = {?} AND email = {?}', $rewrite, $this->user
->id(), $this->email
);
317 $this->rewrite
= $rewrite;
318 if (!$this->allow_rewrite
) {
320 if (empty($this->hash
)) {
321 $this->hash
= rand_url_id();
322 XDB
::execute("UPDATE emails
324 WHERE uid = {?} AND email = {?}", $this->hash
, $this->user
->id(), $this->email
);
326 $mail = new PlMailer('emails/rewrite-in.mail.tpl');
327 $mail->assign('mail', $this);
328 $mail->assign('user', $this->user
);
329 $mail->assign('baseurl', $globals->baseurl
);
330 $mail->assign('sitename', $globals->core
->sitename
);
331 $mail->assign('to', $this->email
);
332 $mail->send($this->user
->isEmailFormatHtml());
337 // public function clean_errors() {{{2
339 public function clean_errors()
345 $this->panne_level
= 0;
347 return XDB
::execute("UPDATE emails
348 SET panne_level = 0, panne = 0, last = 0
349 WHERE uid = {?} AND email = {?}",
350 $this->user
->id(), $this->email
);
353 // public function has_rewrite() {{{2
355 public function has_rewrite()
360 // public function is_removable() {{{2
362 public function is_removable()
367 // public function has_disable() {{{2
369 public function has_disable()
375 // class EmailStorage {{{1
376 // Implementation of Email for email storage backends from Polytechnique.org.
377 class EmailStorage
extends Email
379 // Shortname to realname mapping for known mail storage backends.
380 private $display_names = array(
381 'imap' => 'Accès de secours aux emails (IMAP)',
382 'googleapps' => 'Compte Google Apps',
385 // Retrieves the current list of actives storages.
386 private function get_storages()
388 $res = XDB
::query("SELECT mail_storage
390 WHERE user_id = {?}", $this->user
->id());
391 return new PlFlagSet($res->fetchOneCell());
394 // Updates the list of active storages.
395 private function set_storages($storages)
397 XDB
::execute("UPDATE auth_user_md5
398 SET mail_storage = {?}
399 WHERE user_id = {?}", $storages, $this->user
->id());
402 // Returns the list of allowed storages for the @p user.
403 static public function get_allowed_storages(User
&$user)
408 // Google Apps storage is available for users with valid Google Apps account.
409 require_once 'googleapps.inc.php';
410 if ($globals->mailstorage
->googleapps_domain
&&
411 GoogleAppsAccount
::account_status($user->id()) == 'active') {
412 $storages[] = 'googleapps';
415 // IMAP storage is always visible to administrators, and is allowed for
416 // everyone when the service is marked as 'active'.
417 if ($globals->mailstorage
->imap_active || S
::admin()) {
418 $storages[] = 'imap';
425 public function __construct(User
&$user, $name)
427 $this->user
= &$user;
428 $this->email
= $name;
429 $this->display_email
= (isset($this->display_names
[$name]) ?
$this->display_names
[$name] : $name);
431 $storages = $this->get_storages();
432 $this->sufficient
= ($name == 'googleapps');
433 $this->active
= $storages->hasFlag($name);
434 $this->broken
= false
;
435 $this->disabled
= false
;
437 $this->panne
= $this->last
= $this->panne_level
= 0;
440 public function activate()
442 if (!$this->active
) {
443 $storages = $this->get_storages();
444 $storages->addFlag($this->email
);
445 $this->set_storages($storages);
446 $this->active
= true
;
450 public function deactivate()
453 $storages = $this->get_storages();
454 $storages->rmFlag($this->email
);
455 $this->set_storages($storages);
456 $this->active
= false
;
461 // Source rewrite can't be enabled for email storage addresses.
462 public function set_rewrite($rewrite) {}
464 // Email storage are not supposed to be broken, hence not supposed to be
466 public function clean_errors() {}
469 public function has_rewrite() { return false
; }
470 public function is_removable() { return false
; }
471 public function has_disable() { return false
; }
474 // class Redirect {{{1
475 // Redirect is a placeholder class for an user's active redirections (third-party
476 // redirection email, or Polytechnique.org mail storages).
481 private $flag_active = 'active';
489 public function __construct(User
&$user)
491 $this->user
= &$user;
492 $this->bogo
= new Bogo($user);
494 // Adds third-party email redirections.
495 $res = XDB
::iterRow("SELECT email, flags, rewrite, allow_rewrite, hash, panne, last, panne_level
497 WHERE uid = {?} AND flags != 'filter'", $user->id());
498 $this->emails
= Array();
499 while ($row = $res->next()) {
500 $this->emails
[] = new EmailRedirection($user, $row);
503 // Adds local email storage backends.
504 foreach (EmailStorage
::get_allowed_storages($user) as $storage) {
505 $this->emails
[] = new EmailStorage($user, $storage);
509 // public function other_active() {{{2
511 public function other_active($email)
513 foreach ($this->emails
as $mail) {
514 if ($mail->email
!= $email && $mail->active
&& $mail->sufficient
) {
521 // public function delete_email() {{{2
523 public function delete_email($email)
525 if (!$this->other_active($email)) {
526 return ERROR_INACTIVE_REDIRECTION
;
528 XDB
::execute('DELETE FROM emails WHERE uid = {?} AND email = {?}', $this->user
->id(), $email);
529 S
::logger()->log('email_del', $email . ($this->user
->id() != S
::v('uid') ?
" (admin on {$this->user->login()})" : ""));
530 foreach ($this->emails
as $i => $mail) {
531 if ($email == $mail->email
) {
532 unset($this->emails
[$i]);
535 check_redirect($this);
539 // public function add_email() {{{2
541 public function add_email($email)
543 $email_stripped = strtolower(trim($email));
544 if (!isvalid_email($email_stripped)) {
545 return ERROR_INVALID_EMAIL
;
547 if (!isvalid_email_redirection($email_stripped)) {
548 return ERROR_LOOP_EMAIL
;
550 XDB
::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->user
->id(), $email);
551 if ($logger = S
::v('log', null
)) { // may be absent --> step4.php
552 S
::logger()->log('email_add', $email . ($this->user
->id() != S
::v('uid') ?
" (admin on {$this->user->login()})" : ""));
554 foreach ($this->emails
as $mail) {
555 if ($mail->email
== $email_stripped) {
559 $this->emails
[] = new EmailRedirection($this->user
, array($email, 'active', '', 0, null
, '0000-00-00', '0000-00-00', 0));
562 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->user
->login());
563 check_redirect($this);
567 // public function modify_email() {{{2
569 public function modify_email($emails_actifs, $emails_rewrite)
571 foreach ($this->emails
as &$mail) {
572 if (in_array($mail->email
, $emails_actifs)) {
577 $mail->set_rewrite($emails_rewrite[$mail->email
]);
579 check_redirect($this);
582 // public function modify_one_email() {{{2
584 public function modify_one_email($email, $activate)
588 foreach ($this->emails
as $i=>$mail) {
589 if ($mail->email
== $email) {
592 $allinactive &= !$mail->active ||
!$mail->sufficient ||
$mail->email
== $email;
594 if ($thisone === false
) {
595 return ERROR_INVALID_EMAIL
;
597 if ($allinactive ||
$activate) {
598 $this->emails
[$thisone]->activate();
600 $this->emails
[$thisone]->deactivate();
602 check_redirect($this);
603 if ($allinactive && !$activate) {
604 return ERROR_INACTIVE_REDIRECTION
;
610 // public function modify_one_email_redirect() {{{2
612 public function modify_one_email_redirect($email, $redirect)
614 foreach ($this->emails
as &$mail) {
615 if ($mail->email
== $email) {
616 $mail->set_rewrite($redirect);
617 check_redirect($this);
623 // function clean_errors() {{{2
625 public function clean_errors($email)
627 foreach ($this->emails
as &$mail) {
628 if ($mail->email
== $email) {
629 check_redirect($this);
630 return $mail->clean_errors();
636 // function disable() {{{2
638 public function disable()
640 XDB
::execute("UPDATE emails
641 SET flags = 'disable'
642 WHERE flags = 'active' AND uid = {?}", $this->user
->id());
643 foreach ($this->emails
as &$mail) {
644 if ($mail->active
&& $mail->has_disable()) {
645 $mail->disabled
= true
;
646 $mail->active
= false
;
649 check_redirect($this);
652 // function enable() {{{2
654 public function enable()
656 XDB
::execute("UPDATE emails
658 WHERE flags = 'disable' AND uid = {?}", $this->user
->id());
659 foreach ($this->emails
as &$mail) {
660 if ($mail->disabled
) {
661 $mail->active
= true
;
662 $mail->disabled
= false
;
664 check_redirect($this);
668 // function get_broken_mx() {{{2
670 public function get_broken_mx()
672 $res = XDB
::query("SELECT host, text
674 WHERE state != 'ok'");
675 if (!$res->numRows()) {
678 $mxs = $res->fetchAllAssoc();
680 foreach ($this->emails
as &$mail) {
681 if ($mail->active
&& strstr($mail->email
, '@') !== false
) {
682 list(,$domain) = explode('@', $mail->email
);
683 getmxrr($domain, $lcl_mxs);
684 if (empty($lcl_mxs)) {
685 $lcl_mxs = array($domain);
688 foreach ($mxs as &$mx) {
689 foreach ($lcl_mxs as $lcl) {
690 if (fnmatch($mx['host'], $lcl)) {
691 $broken = $mx['text'];
696 $mails[] = array('mail' => $mail->email
, 'text' => $broken);
705 // function active_emails() {{{2
707 public function active_emails()
710 foreach ($this->emails
as $mail) {
718 // function get_uid() {{{2
720 public function get_uid()
722 return $this->user
->id();
726 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: