2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 uid = {?} 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 uid={?} 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 if (strpos($em, '@') === false
) {
61 list($ident, $dom) = explode('@', $em);
62 if ($dom == $globals->mail
->domain ||
$dom == $globals->mail
->domain2
) {
63 list($ident1) = explode('_', $ident);
64 list($ident) = explode('+', $ident1);
66 return $ident . '@' . $dom;
69 // function isvalid_email_redirection() {{{1
70 /** vérifie si une adresse email convient comme adresse de redirection
71 * @param $email l'adresse email a verifier
74 function isvalid_email_redirection($email)
76 return isvalid_email($email) &&
77 !preg_match("/@(polytechnique\.(org|edu)|melix\.(org|net)|m4x\.org)$/", $email);
80 // function ids_from_mails() {{{1
81 // Converts an array of emails to an array of email => uid
82 function ids_from_mails(array $emails)
85 $domain_mails = array();
86 $alias_mails = array();
87 $other_mails = array();
89 // Determine the type of the email adresses. It can eiher be a domain
90 // email (@polytechnique.org), an alias email (@melix.net) or any other
91 // email (potentially used as a redirection by one user)
92 foreach ($emails as $email) {
93 if (strpos($email, '@') === false
) {
95 $domain = $globals->mail
->domain2
;
97 list($user, $domain) = explode('@', $email);
99 if ($domain == $globals->mail
->alias_dom ||
$domain == $globals->mail
->alias_dom2
) {
100 list($user) = explode('+', $user);
101 list($user) = explode('_', $user);
102 $alias_mails[$email] = $user . "@" . $globals->mail
->alias_dom
;
103 } elseif ($domain == $globals->mail
->domain ||
$domain == $globals->mail
->domain2
) {
104 list($user) = explode('+', $user);
105 list($user) = explode('_', $user);
106 $domain_mails[$email] = $user;
108 $other_mails[] = $email;
113 // Look up user ids for addresses in domain
114 $alias_uids = array();
115 if (count($domain_mails)) {
116 $res = XDB
::query("SELECT alias, uid
118 WHERE alias IN {?}", array_unique($domain_mails));
119 foreach ($res->fetchAllRow() as $row) {
120 list ($alias, $id) = $row;
121 $domain_uids[$alias] = $id;
124 // Connect emails with uids
125 foreach ($domain_mails as $email => $user) {
126 $uids[$email] = $domain_uids[$user];
129 // Look up user ids for addresses in our alias domain
130 $alias_uids = array();
131 if (count($alias_mails)) {
132 $res = XDB
::query("SELECT v.alias, a.uid
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 {?}", array_unique($alias_mails));
138 foreach ($res->fetchAllRow() as $row) {
139 list ($alias, $id) = $row;
140 $alias_uids[$alias] = $id;
143 // Connect emails with uids
144 foreach ($alias_mails as $email => $user) {
145 $uids[$email] = $alias_uids[$user];
148 // Look up user ids for other addresses in the email redirection list
149 if (count($other_mails)) {
150 $lowerupper = array();
151 foreach ($other_mails as $mail) {
152 $lowerupper[strtolower($mail)] = $mail;
154 $res = XDB
::query("SELECT email, uid
156 WHERE email IN {?}", $other_mails);
157 foreach ($res->fetchAllRow() as $row) {
158 list ($email, $uid) = $row;
159 $uids[$lowerupper[strtolower($email)]] = $uid;
167 // The Bogo class represents a spam filtering level in plat/al architecture.
174 private $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams');
178 public function __construct(User
&$user)
184 $this->user
= &$user;
185 $res = XDB
::query('SELECT email FROM emails WHERE uid = {?} AND flags = "filter"', $user->id());
186 if ($res->numRows()) {
187 $this->state
= $res->fetchOneCell();
189 $this->state
= 'tag_and_drop_spams';
191 "INSERT INTO emails (uid, email, rewrite, panne, flags)
192 VALUES ({?}, 'tag_and_drop_spams', '', '0000-00-00', 'filter')",
197 // public function change() {{{2
199 public function change($state)
201 $this->state
= is_int($state) ?
$this->_states
[$state] : $state;
202 XDB
::execute('UPDATE emails SET email = {?} WHERE uid = {?} AND flags = "filter"',
203 $this->state
, $this->user
->id());
206 // pubic function level() {{{2
208 public function level()
210 return array_search($this->state
, $this->_states
);
215 // Represents an "email address" used as final recipient for plat/al-managed
216 // addresses; it can be subclasses a Redirection emails (third-party) or as
217 // Storage emails (Polytechnique.org).
222 // Basic email properties; $sufficient indicates if the email can be used as
223 // an unique redirection; $email contains the delivery email address.
227 public $display_email;
229 // Redirection status properties.
234 public $allow_rewrite;
237 // Redirection bounces stats.
242 // Activates the email address as a redirection.
243 public abstract function activate();
245 // Deactivates the email address as a redirection.
246 public abstract function deactivate();
248 // Sets the rewrite rule for the given address.
249 public abstract function set_rewrite($rewrite);
251 // Resets the error counts associated with the redirection.
252 public abstract function clean_errors();
254 // Email backend capabilities ('rewrite' refers to From: rewrite for mails
255 // forwarded by Polytechnique.org's MXs; 'removable' indicates if the email
256 // can be definitively removed; 'disable' indicates if the email has a third
257 // status 'disabled' in addition to 'active' and 'inactive').
258 public abstract function has_rewrite();
259 public abstract function is_removable();
260 public abstract function has_disable();
263 // class EmailRedirection {{{1
264 // Implementation of Email for third-party redirection (redirection of emails to
265 // external user-supplied addresses).
266 class EmailRedirection
extends Email
270 public function __construct(User
&$user, $row)
272 $this->user
= &$user;
273 $this->sufficient
= true
;
275 list($this->email
, $flags, $this->rewrite
, $this->allow_rewrite
, $this->hash
, $this->panne
, $this->last
, $this->panne_level
) = $row;
276 $this->display_email
= $this->email
;
277 $this->active
= ($flags == 'active');
278 $this->broken
= ($flags == 'panne');
279 $this->disabled
= ($flags == 'disable');
282 // public function activate() {{{2
284 public function activate()
286 if (!$this->active
) {
287 XDB
::execute("UPDATE emails
288 SET panne_level = IF(flags = 'panne', panne_level - 1, panne_level),
290 WHERE uid = {?} AND email = {?}", $this->user
->id(), $this->email
);
291 S
::logger()->log("email_on", $this->email
. ($this->user
->id() != S
::v('uid') ?
"(admin on {$this->user->login()})" : ""));
292 $this->active
= true
;
293 $this->broken
= false
;
297 // public function deactivate() {{{2
299 public function deactivate()
302 XDB
::execute("UPDATE emails SET flags =''
303 WHERE uid = {?} AND email = {?}", $this->user
->id(), $this->email
);
304 S
::logger()->log("email_off", $this->email
. ($this->user
->id() != S
::v('uid') ?
"(admin on {$this->user->login()})" : "") );
305 $this->active
= false
;
309 // public function set_rewrite() {{{2
311 public function set_rewrite($rewrite)
313 if ($this->rewrite
== $rewrite) {
316 if (!$rewrite ||
!isvalid_email($rewrite)) {
319 XDB
::execute('UPDATE emails SET rewrite = {?} WHERE uid = {?} AND email = {?}', $rewrite, $this->user
->id(), $this->email
);
320 $this->rewrite
= $rewrite;
321 if (!$this->allow_rewrite
) {
323 if (empty($this->hash
)) {
324 $this->hash
= rand_url_id();
325 XDB
::execute("UPDATE emails
327 WHERE uid = {?} AND email = {?}", $this->hash
, $this->user
->id(), $this->email
);
329 $mail = new PlMailer('emails/rewrite-in.mail.tpl');
330 $mail->assign('mail', $this);
331 $mail->assign('user', $this->user
);
332 $mail->assign('baseurl', $globals->baseurl
);
333 $mail->assign('sitename', $globals->core
->sitename
);
334 $mail->assign('to', $this->email
);
335 $mail->send($this->user
->isEmailFormatHtml());
340 // public function clean_errors() {{{2
342 public function clean_errors()
348 $this->panne_level
= 0;
350 return XDB
::execute("UPDATE emails
351 SET panne_level = 0, panne = 0, last = 0
352 WHERE uid = {?} AND email = {?}",
353 $this->user
->id(), $this->email
);
356 // public function has_rewrite() {{{2
358 public function has_rewrite()
363 // public function is_removable() {{{2
365 public function is_removable()
370 // public function has_disable() {{{2
372 public function has_disable()
378 // class EmailStorage {{{1
379 // Implementation of Email for email storage backends from Polytechnique.org.
380 class EmailStorage
extends Email
382 // Shortname to realname mapping for known mail storage backends.
383 private $display_names = array(
384 'imap' => 'Accès de secours aux emails (IMAP)',
385 'googleapps' => 'Compte Google Apps',
388 // Retrieves the current list of actives storages.
389 private function get_storages()
391 return new PlFlagSet(XDB
::fetchOneCell('SELECT storage
397 // Updates the list of active storages.
398 private function set_storages($storages)
400 XDB
::execute("UPDATE email_options
402 WHERE uid = {?}", $storages, $this->user
->id());
405 // Returns the list of allowed storages for the @p user.
406 static public function get_allowed_storages(User
&$user)
411 // Google Apps storage is available for users with valid Google Apps account.
412 require_once 'googleapps.inc.php';
413 if ($globals->mailstorage
->googleapps_domain
&&
414 GoogleAppsAccount
::account_status($user->id()) == 'active') {
415 $storages[] = 'googleapps';
418 // IMAP storage is always visible to administrators, and is allowed for
419 // everyone when the service is marked as 'active'.
420 if ($globals->mailstorage
->imap_active || S
::admin()) {
421 $storages[] = 'imap';
428 public function __construct(User
&$user, $name)
430 $this->user
= &$user;
431 $this->email
= $name;
432 $this->display_email
= (isset($this->display_names
[$name]) ?
$this->display_names
[$name] : $name);
434 $storages = $this->get_storages();
435 $this->sufficient
= ($name == 'googleapps');
436 $this->active
= $storages->hasFlag($name);
437 $this->broken
= false
;
438 $this->disabled
= false
;
440 $this->panne
= $this->last
= $this->panne_level
= 0;
443 public function activate()
445 if (!$this->active
) {
446 $storages = $this->get_storages();
447 $storages->addFlag($this->email
);
448 $this->set_storages($storages);
449 $this->active
= true
;
453 public function deactivate()
456 $storages = $this->get_storages();
457 $storages->rmFlag($this->email
);
458 $this->set_storages($storages);
459 $this->active
= false
;
464 // Source rewrite can't be enabled for email storage addresses.
465 public function set_rewrite($rewrite) {}
467 // Email storage are not supposed to be broken, hence not supposed to be
469 public function clean_errors() {}
472 public function has_rewrite() { return false
; }
473 public function is_removable() { return false
; }
474 public function has_disable() { return false
; }
477 // class Redirect {{{1
478 // Redirect is a placeholder class for an user's active redirections (third-party
479 // redirection email, or Polytechnique.org mail storages).
484 private $flag_active = 'active';
492 public function __construct(User
&$user)
494 $this->user
= &$user;
495 $this->bogo
= new Bogo($user);
497 // Adds third-party email redirections.
498 $res = XDB
::iterRow("SELECT email, flags, rewrite, allow_rewrite, hash, panne, last, panne_level
500 WHERE uid = {?} AND flags != 'filter'", $user->id());
501 $this->emails
= Array();
502 while ($row = $res->next()) {
503 $this->emails
[] = new EmailRedirection($user, $row);
506 // Adds local email storage backends.
507 foreach (EmailStorage
::get_allowed_storages($user) as $storage) {
508 $this->emails
[] = new EmailStorage($user, $storage);
512 // public function other_active() {{{2
514 public function other_active($email)
516 foreach ($this->emails
as $mail) {
517 if ($mail->email
!= $email && $mail->active
&& $mail->sufficient
) {
524 // public function delete_email() {{{2
526 public function delete_email($email)
528 if (!$this->other_active($email)) {
529 return ERROR_INACTIVE_REDIRECTION
;
531 XDB
::execute('DELETE FROM emails WHERE uid = {?} AND email = {?}', $this->user
->id(), $email);
532 S
::logger()->log('email_del', $email . ($this->user
->id() != S
::v('uid') ?
" (admin on {$this->user->login()})" : ""));
533 foreach ($this->emails
as $i => $mail) {
534 if ($email == $mail->email
) {
535 unset($this->emails
[$i]);
538 check_redirect($this);
542 // public function add_email() {{{2
544 public function add_email($email)
546 $email_stripped = strtolower(trim($email));
547 if (!isvalid_email($email_stripped)) {
548 return ERROR_INVALID_EMAIL
;
550 if (!isvalid_email_redirection($email_stripped)) {
551 return ERROR_LOOP_EMAIL
;
553 XDB
::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->user
->id(), $email);
554 if ($logger = S
::v('log', null
)) { // may be absent --> step4.php
555 S
::logger()->log('email_add', $email . ($this->user
->id() != S
::v('uid') ?
" (admin on {$this->user->login()})" : ""));
557 foreach ($this->emails
as $mail) {
558 if ($mail->email
== $email_stripped) {
562 $this->emails
[] = new EmailRedirection($this->user
, array($email, 'active', '', 0, null
, '0000-00-00', '0000-00-00', 0));
565 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->user
->login());
566 check_redirect($this);
570 // public function modify_email() {{{2
572 public function modify_email($emails_actifs, $emails_rewrite)
574 foreach ($this->emails
as &$mail) {
575 if (in_array($mail->email
, $emails_actifs)) {
580 $mail->set_rewrite($emails_rewrite[$mail->email
]);
582 check_redirect($this);
585 // public function modify_one_email() {{{2
587 public function modify_one_email($email, $activate)
591 foreach ($this->emails
as $i=>$mail) {
592 if ($mail->email
== $email) {
595 $allinactive &= !$mail->active ||
!$mail->sufficient ||
$mail->email
== $email;
597 if ($thisone === false
) {
598 return ERROR_INVALID_EMAIL
;
600 if ($allinactive ||
$activate) {
601 $this->emails
[$thisone]->activate();
603 $this->emails
[$thisone]->deactivate();
605 check_redirect($this);
606 if ($allinactive && !$activate) {
607 return ERROR_INACTIVE_REDIRECTION
;
613 // public function modify_one_email_redirect() {{{2
615 public function modify_one_email_redirect($email, $redirect)
617 foreach ($this->emails
as &$mail) {
618 if ($mail->email
== $email) {
619 $mail->set_rewrite($redirect);
620 check_redirect($this);
626 // function clean_errors() {{{2
628 public function clean_errors($email)
630 foreach ($this->emails
as &$mail) {
631 if ($mail->email
== $email) {
632 check_redirect($this);
633 return $mail->clean_errors();
639 // function disable() {{{2
641 public function disable()
643 XDB
::execute("UPDATE emails
644 SET flags = 'disable'
645 WHERE flags = 'active' AND uid = {?}", $this->user
->id());
646 foreach ($this->emails
as &$mail) {
647 if ($mail->active
&& $mail->has_disable()) {
648 $mail->disabled
= true
;
649 $mail->active
= false
;
652 check_redirect($this);
655 // function enable() {{{2
657 public function enable()
659 XDB
::execute("UPDATE emails
661 WHERE flags = 'disable' AND uid = {?}", $this->user
->id());
662 foreach ($this->emails
as &$mail) {
663 if ($mail->disabled
) {
664 $mail->active
= true
;
665 $mail->disabled
= false
;
667 check_redirect($this);
671 // function get_broken_mx() {{{2
673 public function get_broken_mx()
675 $res = XDB
::query("SELECT host, text
677 WHERE state != 'ok'");
678 if (!$res->numRows()) {
681 $mxs = $res->fetchAllAssoc();
683 foreach ($this->emails
as &$mail) {
684 if ($mail->active
&& strstr($mail->email
, '@') !== false
) {
685 list(,$domain) = explode('@', $mail->email
);
686 getmxrr($domain, $lcl_mxs);
687 if (empty($lcl_mxs)) {
688 $lcl_mxs = array($domain);
691 foreach ($mxs as &$mx) {
692 foreach ($lcl_mxs as $lcl) {
693 if (fnmatch($mx['host'], $lcl)) {
694 $broken = $mx['text'];
699 $mails[] = array('mail' => $mail->email
, 'text' => $broken);
708 // function active_emails() {{{2
710 public function active_emails()
713 foreach ($this->emails
as $mail) {
721 // function get_uid() {{{2
723 public function get_uid()
725 return $this->user
->id();
729 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: