2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 // Checks if an email update is required in MLs and aliases.
28 // This occurs when the user don't have email permissions and her email has changed.
29 function require_email_update(User
$user, $new_email)
31 Platal
::assert(!is_null($user), 'User cannot be null.');
33 $is_new = !$user->checkPerms(User
::PERM_MAIL
) && $new_email != strtolower($user->email
);
34 if ($new_email && $is_new) {
35 $already = XDB
::fetchOneCell('SELECT hruid
37 WHERE email = {?} AND uid != {?}',
38 $new_email, $user->id());
40 Platal
::page()->trigError("L'email ne peut pas être utilisé pour ce compte car il correspond déjà au compte : "
41 . $already . ". Si l'utilisateur courant et cette personne ne sont en fait qu'une "
42 . "seul et même personne, ou en cas de problème, contacter : contact@polytechnique.org");
49 function format_email_alias($email)
51 if ($user = User
::getSilent($email)) {
52 return $user->forlifeEmail();
54 if (isvalid_email($email)) {
60 function add_to_list_alias($email, $local_part, $domain, $type = 'alias')
62 $email = format_email_alias($email);
63 if (is_null($email)) {
67 XDB
::execute('INSERT IGNORE INTO email_virtual (email, domain, redirect, type)
68 SELECT {?}, id, {?}, {?}
69 FROM email_virtual_domains
71 $local_part, $email, $type, $domain);
75 function delete_from_list_alias($email, $local_part, $domain, $type = 'alias')
77 $email = format_email_alias($email);
78 if (is_null($email)) {
82 XDB
::execute('DELETE v
83 FROM email_virtual AS v
84 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
85 INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id)
86 WHERE v.email = {?} AND d.name = {?} AND v.redirect = {?} AND type = {?}',
87 $local_part, $domain, $email, $type);
91 function update_list_alias($email, $former_email, $local_part, $domain, $type = 'alias')
93 $email = format_email_alias($email);
94 if (is_null($email)) {
98 XDB
::execute('UPDATE email_virtual AS v
99 INNER JOIN email_virtual_domains AS d ON (v.domain = d.id)
101 WHERE v.redirect = {?} AND d.name = {?} AND v.email = {?} AND v.type = {?}',
102 $email, $former_email, $domain, $local_part, $type);
106 // Updates an email in all aliases (groups and events).
107 function update_alias_user($former_email, $new_email)
109 XDB
::execute('UPDATE email_virtual
111 WHERE redirect = {?} AND (type = \'alias\' OR type = \'event\')',
112 $new_email, $former_email);
115 function list_alias_members($local_part, $domain)
117 $emails = XDB
::fetchColumn('SELECT DISTINCT(redirect)
118 FROM email_virtual AS v
119 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
120 INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id)
121 WHERE v.email = {?} AND d.name = {?} AND type = \'alias\'',
122 $local_part, $domain);
126 foreach ($emails as $email) {
127 if ($user = User
::getSilent($email)) {
130 $nonusers[] = $email;
136 'nonusers' => $nonusers
140 function delete_list_alias($local_part, $domain)
142 XDB
::execute('DELETE v
143 FROM email_virtual AS v
144 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
145 INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id)
146 WHERE v.email = {?} AND d.name = {?} AND type = \'alias\'',
147 $local_part, $domain);
150 function iterate_list_alias($domain)
152 return XDB
::fetchColumn('SELECT CONCAT(v.email, \'@\', m.name)
153 FROM email_virtual AS v
154 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
155 WHERE m.name = {?} AND v.type = \'alias\'
160 function create_list($local_part, $domain)
164 $redirect = $domain . '_' . $local_part . '+';
165 foreach(array('post', 'owner', 'admin', 'bounces', 'unsubscribe') as $suffix) {
166 XDB
::execute('INSERT IGNORE INTO email_virtual (email, domain, redirect, type)
167 SELECT {?}, id, {?}, \'list\'
168 FROM email_virtual_domains
170 ($suffix == 'post') ?
$local_part : $local_part . '-' . $suffix,
171 $redirect . $suffix . '@' . $globals->lists
->redirect_domain
, $domain);
175 function delete_list($local_part, $domain)
179 $redirect = $domain . '_' . $local_part . '+';
180 foreach(array('post', 'owner', 'admin', 'bounces', 'unsubscribe') as $suffix) {
181 XDB
::execute('DELETE FROM email_virtual
182 WHERE redirect = {?} AND type = \'list\'',
183 $redirect . $suffix . '@' . $globals->lists
->redirect_domain
);
187 function list_exist($local_part, $domain)
189 return XDB
::fetchOneCell('SELECT COUNT(*)
190 FROM email_virtual AS v
191 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
192 INNER JOIN email_virtual_domains AS d ON (m.id = d.aliasing)
193 WHERE v.email = {?} AND d.name = {?}',
194 $local_part, $domain);
197 // function mark_broken_email() {{{1
198 function mark_broken_email($email, $admin = false
)
200 $email = valide_email($email);
201 if (empty($email) ||
$email == '@') {
205 $user = XDB
::fetchOneAssoc('SELECT r1.uid, a.hruid, a.full_name, r1.broken_level != 0 AS broken, COUNT(r2.uid) AS nb_mails,
206 s.email AS alias, DATE_ADD(r1.last, INTERVAL 14 DAY) < CURDATE() as notify
207 FROM email_redirect_account AS r1
208 INNER JOIN accounts AS a ON (a.uid = r1.uid)
209 INNER JOIN email_source_account AS s ON (a.uid = s.uid AND s.flags = \'bestalias\')
210 LEFT JOIN email_redirect_account AS r2 ON (a.uid = r2.uid AND r1.redirect != r2.redirect AND
211 r2.broken_level = 0 AND r2.flags = \'active\' AND
212 (r2.type = \'smtp\' OR r2.type = \'googleapps\'))
213 WHERE r1.redirect = {?}
214 GROUP BY r1.uid', $email);
217 // Mark address as broken.
218 if (!$user['broken']) {
219 XDB
::execute('UPDATE email_redirect_account
220 SET broken_date = NOW(), last = NOW(), broken_level = 1
221 WHERE redirect = {?}', $email);
223 XDB
::execute('UPDATE email_redirect_account
224 SET last = CURDATE(), broken_level = broken_level + 1
225 WHERE redirect = {?} AND DATE_ADD(last, INTERVAL 14 DAY) < CURDATE()',
228 XDB
::execute('UPDATE email_redirect_account
230 WHERE redirect = {?} AND broken_level = 0', $email);
237 // function fix_bestalias() {{{1
238 // Checks for an existing 'bestalias' among the the current user's aliases, and
239 // eventually selects a new bestalias when required.
240 function fix_bestalias(User
$user)
242 // First check if the bestalias is properly set.
243 $alias_count = XDB
::fetchOneCell('SELECT COUNT(*)
244 FROM email_source_account
245 WHERE uid = {?} AND FIND_IN_SET(\'bestalias\', flags) AND expire IS NULL',
248 if ($alias_count > 1) {
249 // If too many bestaliases, delete the bestalias flag from all this
250 // user's emails (this should never happen).
251 XDB
::execute("UPDATE email_source_account
252 SET flags = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', flags, ','), ',bestalias,', ','))
256 if ($alias_count != 1) {
257 // If no bestalias is selected, we choose the shortest email which is not
258 // related to a usage name and contains a '.'.
259 XDB
::execute("UPDATE email_source_account
260 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'bestalias')
261 WHERE uid = {?} AND expire IS NULL
262 ORDER BY NOT FIND_IN_SET('usage', flags), email LIKE '%.%', LENGTH(email)
267 // First check if best_domain is properly set.
268 $domain_count = XDB
::fetchOneCell('SELECT COUNT(*)
270 INNER JOIN email_source_account AS s ON (s.uid = a.uid AND FIND_IN_SET(\'bestalias\', s.flags))
271 INNER JOIN email_virtual_domains AS d ON (d.id = a.best_domain)
272 INNER JOIN email_virtual_domains AS m ON (d.aliasing = m.id)
273 INNER JOIN email_virtual_domains AS v ON (v.aliasing = m.id AND v.id = s.domain)
274 WHERE a.uid = {?} AND (m.name = {?} OR m.name = {?})',
275 $user->id(), $user->mainEmailDomain(), Platal
::globals()->mail
->alias_dom
);
277 if ($domain_count == 0) {
278 XDB
::execute('UPDATE accounts AS a
279 INNER JOIN email_source_account AS s ON (s.uid = a.uid AND FIND_IN_SET(\'bestalias\', s.flags))
280 INNER JOIN email_virtual_domains AS d ON (d.aliasing = s.domain AND (d.name = {?} OR d.name = {?}))
281 SET a.best_domain = d.id
283 $user->mainEmailDomain(), Platal
::globals()->mail
->alias_dom
, $user->id());
289 // function valide_email() {{{1
290 // Returns a cleaned-up version of the @p email string. It removes garbage
291 // characters, and determines the canonical form (without _ and +) for
292 // Polytechnique.org email addresses.
293 function valide_email($str)
297 $em = trim(rtrim($str));
298 $em = str_replace('<', '', $em);
299 $em = str_replace('>', '', $em);
300 if (strpos($em, '@') === false
) {
303 list($ident, $dom) = explode('@', $em);
304 if (User
::isMainMailDomain($dom)) {
305 list($ident1) = explode('_', $ident);
306 list($ident) = explode('+', $ident1);
308 return $ident . '@' . $dom;
311 // function isvalid_email_redirection() {{{1
312 /** Checks if an email is a suitable redirection.
313 * @param $email the email to check
314 * @param $user the user asking for the redirection
317 function isvalid_email_redirection($email, User
$user)
319 $valid = isvalid_email($email) && User
::isForeignEmailAddress($email);
320 if (!$user->hasProfile() ||
($user->profile()->grad_year
> date('Y') - 3)) {
321 return $valid && !preg_match("/@polytechnique\.edu$/", $email);
327 // function ids_from_mails() {{{1
328 // Converts an array of emails to an array of email => uid, where email is the
329 // given email when we found a matching user.
330 function ids_from_mails(array $emails)
332 // Removes duplicates, if any.
333 $emails = array_unique($emails);
335 // Formats and splits by domain type (locally managed or external) emails.
336 $main_domain_emails = array();
337 $aux_domain_emails = array();
338 $other_emails = array();
339 foreach ($emails as $email) {
340 if (strpos($email, '@') === false
) {
341 $main_domain_emails[] = $email;
343 if (User
::isForeignEmailAddress($email)) {
344 $other_emails[$email] = strtolower($user . '@' . $domain);
346 list($local_part, $domain) = explode('@', $email);
347 list($local_part) = explode('+', $local_part);
348 list($local_part) = explode('_', $local_part);
349 if (User
::isMainMailDomain($domain)) {
350 $main_domain_emails[$email] = strtolower($local_part);
351 } elseif (User
::isAliasMailDomain($domain)) {
352 $aux_domain_emails[$email] = strtolower($local_part);
358 // Retrieves emails from our domains.
359 $main_domain_uids = XDB
::fetchAllAssoc('email',
361 FROM email_source_account
362 WHERE email IN {?} AND type != \'alias_aux\'',
363 array_unique($main_domain_emails));
364 $aux_domain_uids = XDB
::fetchAllAssoc('email',
366 FROM email_source_account
367 WHERE email IN {?} AND type = \'alias_aux\'',
368 array_unique($aux_domain_emails));
370 // Retrieves emails from redirections.
371 $other_uids = XDB
::fetchAllAssoc('redirect',
372 'SELECT redirect, uid
373 FROM email_redirect_account
374 WHERE redirect IN {?}',
375 array_unique($other_emails));
377 // Associates given emails with the corresponding uid.
379 foreach ($main_domain_emails as $email => $key) {
380 $uids[$email] = $main_domain_uids[$key];
382 foreach ($aux_domain_emails as $email => $key) {
383 $uids[$email] = $aux_domain_uids[$key];
385 foreach ($other_emails as $email => $key) {
386 $uids[$email] = $other_uids[$key];
389 return array_unique($uids);
393 // The Bogo class represents a spam filtering level in plat/al architecture.
396 const MAIN_DEFAULT
= 'default';
397 const IMAP_DEFAULT
= 'let_spams';
399 public static $states = array(
403 3 => 'tag_and_drop_spams',
409 public $single_state;
410 public $redirections;
411 public $single_redirection;
413 public function __construct(User
$user)
419 $this->user
= &$user;
420 $res = XDB
::fetchOneAssoc('SELECT COUNT(DISTINCT(action)) AS action_count, COUNT(redirect) AS redirect_count, action
421 FROM email_redirect_account
422 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\') AND flags = \'active\'
425 if ($res['redirect_count'] == 0) {
429 $this->single_redirection
= ($res['redirect_count'] == 1);
430 $this->redirections
= XDB
::fetchAllAssoc('SELECT IF(type = \'googleapps\', type, redirect) AS redirect, type, action
431 FROM email_redirect_account
432 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')
433 ORDER BY type, redirect',
436 foreach ($this->redirections
AS &$redirection) {
437 $redirection['filter'] = array_search($redirection['action'], self
::$states);
439 if ($res['action_count'] == 1) {
440 $this->state
= array_search($res['action'], self
::$states);
441 $this->single_state
= true
;
443 $this->single_state
= $this->state
= false
;
447 public function changeAll($state)
449 Platal
::assert($state >= 0 && $state < count(self
::$states), 'Unknown antispam level.');
451 $this->state
= $state;
452 XDB
::execute('UPDATE email_redirect_account
454 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')',
455 self
::$states[$this->state
], $this->user
->id());
458 public function change($redirection, $state)
460 Platal
::assert($state >= 0 && $state < count(self
::$states), 'Unknown antispam level.');
462 XDB
::execute('UPDATE email_redirect_account
464 WHERE uid = {?} AND (type = {?} OR redirect = {?})',
465 self
::$states[$state], $this->user
->id(), $redirection, $redirection);
470 // Represents an "email address" used as final recipient for plat/al-managed
474 // Lists fields to load automatically.
475 static private $field_names = array('rewrite', 'type', 'action', 'broken_date', 'broken_level', 'last', 'hash', 'allow_rewrite');
477 // Shortname to realname mapping for known mail storage backends.
478 static private $display_names = array(
479 'imap' => 'Accès de secours aux emails (IMAP)',
480 'googleapps' => 'Compte Google Apps',
482 static private $storage_domains = array(
489 // Basic email properties; $sufficient indicates if the email can be used as
490 // an unique redirection; $redirect contains the delivery email address.
495 public $display_email;
498 public $filter_level;
500 // Redirection status properties.
506 public $allow_rewrite;
509 // Redirection bounces stats.
511 public $broken_level;
514 public function __construct(User
$user, array $row)
516 foreach (self
::$field_names as $field) {
517 if (array_key_exists($field, $row)) {
518 $this->$field = $row[$field];
521 $this->email
= $row['redirect'];
523 if (array_key_exists($this->type
, Email
::$display_names)) {
524 $this->display_email
= self
::$display_names[$this->type
];
526 $this->display_email
= $this->email
;
528 foreach (array('active', 'inactive', 'broken', 'disabled') as $status) {
529 $this->$status = ($status == $row['flags']);
531 $this->sufficient
= ($this->type
== 'smtp' ||
$this->type
== 'googleapps');
532 $this->filter_level
= ($this->type
== 'imap') ? null
: array_search($this->action
, Bogo
::$states);
533 if (array_key_exists($this->type
, self
::$storage_domains)) {
534 $this->id
= $this->type
;
536 $this->id
= str_replace(array('@', '.'), array('_at_', '_dot_'), $this->email
);
538 $this->user
= &$user;
541 // Activates the email address as a redirection.
542 public function activate()
544 if ($this->inactive
) {
545 if (in_array($this->type
, self
::get_allowed_storages($this->user
))) {
546 self
::activate_storage($this->user
, $this->type
, $this->action
);
548 XDB
::execute('UPDATE email_redirect_account
549 SET broken_level = IF(flags = \'broken\', broken_level - 1, broken_level), flags = \'active\'
550 WHERE uid = {?} AND redirect = {?}',
551 $this->user
->id(), $this->email
);
553 S
::logger()->log('email_on', $this->email
. ($this->user
->id() != S
::v('uid') ?
"(admin on {$this->user->login()})" : ''));
554 $this->inactive
= false
;
555 $this->active
= true
;
559 // Deactivates the email address as a redirection.
560 public function deactivate()
563 if (in_array($this->type
, self
::get_allowed_storages($this->user
))) {
564 self
::deactivate_storage($this->user
, $this->type
);
566 XDB
::execute('UPDATE email_redirect_account
567 SET flags = \'inactive\'
568 WHERE uid = {?} AND redirect = {?}',
569 $this->user
->id(), $this->email
);
571 S
::logger()->log('email_off', $this->email
. ($this->user
->id() != S
::v('uid') ?
"(admin on {$this->user->login()})" : "") );
572 $this->inactive
= true
;
573 $this->active
= false
;
578 // Sets the rewrite rule for the given address.
579 public function set_rewrite($rewrite)
581 if ($this->type
!= 'smtp' ||
$this->rewrite
== $rewrite) {
584 if (!$rewrite ||
!isvalid_email($rewrite)) {
587 XDB
::execute('UPDATE email_redirect_account
589 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
590 $rewrite, $this->user
->id(), $this->email
);
591 $this->rewrite
= $rewrite;
592 if (!$this->allow_rewrite
) {
594 if (empty($this->hash
)) {
595 $this->hash
= rand_url_id();
596 XDB
::execute('UPDATE email_redirect_account
598 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
599 $this->hash
, $this->user
->id(), $this->email
);
601 $mail = new PlMailer('emails/rewrite-in.mail.tpl');
602 $mail->assign('mail', $this);
603 $mail->assign('user', $this->user
);
604 $mail->assign('baseurl', $globals->baseurl
);
605 $mail->assign('sitename', $globals->core
->sitename
);
606 $mail->assign('to', $this->email
);
607 $mail->send($this->user
->isEmailFormatHtml());
612 // Resets the error counts associated with the redirection.
613 public function clean_errors()
615 if ($this->type
!= 'smtp') {
622 $this->broken_level
= 0;
624 return XDB
::execute('UPDATE email_redirect_account
625 SET broken_level = 0, broken_date = 0, last = 0
626 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
627 $this->user
->id(), $this->email
);
631 // Email backend capabilities ('rewrite' refers to From: rewrite for mails
632 // forwarded by Polytechnique.org's MXs; 'removable' indicates if the email
633 // can be definitively removed; 'disable' indicates if the email has a third
634 // status 'disabled' in addition to 'active' and 'inactive').
635 public function has_rewrite()
637 return ($this->type
== 'smtp');
640 public function is_removable()
642 return ($this->type
== 'smtp');
645 public function has_disable()
650 public function is_redirection()
652 return ($this->type
== 'smtp');
655 // Returns the list of allowed storages for the @p user.
656 static public function get_allowed_storages(User
$user)
661 // Google Apps storage is available for users with valid Google Apps account.
662 require_once 'googleapps.inc.php';
663 if ($user->checkPerms('gapps') &&
664 $globals->mailstorage
->googleapps_domain
&&
665 GoogleAppsAccount
::account_status($user->id()) == 'active') {
666 $storages[] = 'googleapps';
669 // IMAP storage is always visible to administrators, and is allowed for
670 // everyone when the service is marked as 'active'.
671 if ($globals->mailstorage
->imap_active || S
::admin()) {
672 $storages[] = 'imap';
678 static public function make_storage_redirection(User
$user, $storage)
680 return $user->hruid
. '@' . self
::$storage_domains[$storage] . '.' . Platal
::globals()->mail
->domain
;
683 static public function activate_storage(User
$user, $storage, $action = null
)
685 Platal
::assert(in_array($storage, self
::get_allowed_storages($user)), 'Unknown storage.');
687 // We first need to retrieve the value for the antispam filter if not
688 // provided: it is either the user's redirections common value, or if
689 // they differ, our default value.
690 if (is_null($action)) {
691 $bogo = new Bogo($user);
692 $action = ($bogo->single_state ? Bogo
::$states[$bogo->state
] : Bogo
::MAIN_DEFAULT
);
695 if (!self
::is_active_storage($user, $storage)) {
696 XDB
::execute('INSERT INTO email_redirect_account (uid, type, action, redirect, flags)
697 VALUES ({?}, {?}, {?}, {?}, \'active\')',
698 $user->id(), $storage, $action, self
::make_storage_redirection($user, $storage));
702 static public function deactivate_storage(User
$user, $storage)
704 if (in_array($storage, self
::get_allowed_storages($user))) {
705 XDB
::execute('DELETE FROM email_redirect_account
706 WHERE uid = {?} AND type = {?}',
707 $user->id(), $storage);
711 static public function is_active_storage(User
$user, $storage)
713 if (!in_array($storage, self
::get_allowed_storages($user))) {
716 $res = XDB
::fetchOneCell('SELECT COUNT(*)
717 FROM email_redirect_account
718 WHERE uid = {?} AND type = {?} AND flags = \'active\'',
719 $user->id(), $storage);
720 return !is_null($res) && $res > 0;
723 // class Redirect {{{1
724 // Redirect is a placeholder class for an user's active redirections (third-party
725 // redirection email, or Polytechnique.org mail storages).
728 private $flags = 'active';
733 public function __construct(User
$user)
735 $this->user
= &$user;
737 // Adds third-party email redirections.
738 $res = XDB
::iterator('SELECT redirect, rewrite, type, action, broken_date, broken_level, last, flags, hash, allow_rewrite
739 FROM email_redirect_account
740 WHERE uid = {?} AND type != \'homonym\'',
742 $this->emails
= array();
743 while ($row = $res->next()) {
744 $this->emails
[] = new Email($user, $row);
747 if ($storages = Email
::get_allowed_storages($user)) {
748 // We first need to retrieve the value for the antispam filter: it is
749 // either the user's redirections common value, or if they differ, our
751 $bogo = new Bogo($user);
752 $filter = ($bogo->single_state ? Bogo
::$states[$bogo->state
] : Bogo
::MAIN_DEFAULT
);
754 foreach ($storages as $storage) {
755 if (!Email
::is_active_storage($user, $storage)) {
756 $this->emails
[] = new Email($user, array(
757 'redirect' => Email
::make_storage_redirection($user, $storage),
762 'broken_level' => '0000-00-00',
763 'last' => '0000-00-00',
764 'flags' => 'inactive',
766 'allow_rewrite' => 0));
772 public function other_active($email)
774 foreach ($this->emails
as $mail) {
775 if ($mail->email
!= $email && $mail->active
&& $mail->sufficient
) {
782 public function delete_email($email)
784 if (!$this->other_active($email)) {
785 return ERROR_INACTIVE_REDIRECTION
;
787 XDB
::execute('DELETE FROM email_redirect_account
788 WHERE uid = {?} AND redirect = {?} AND type != \'homonym\'',
789 $this->user
->id(), $email);
790 S
::logger()->log('email_del', $email . ($this->user
->id() != S
::v('uid') ?
" (admin on {$this->user->login()})" : ""));
791 foreach ($this->emails
as $i => $mail) {
792 if ($email == $mail->email
) {
793 unset($this->emails
[$i]);
796 check_redirect($this);
797 $this->update_imap();
801 public function add_email($email)
803 $email_stripped = strtolower(trim($email));
804 if (!isvalid_email($email_stripped)) {
805 return ERROR_INVALID_EMAIL
;
807 if (!isvalid_email_redirection($email_stripped, $this->user
)) {
808 return ERROR_LOOP_EMAIL
;
810 // We first need to retrieve the value for the antispam filter: it is
811 // either the user's redirections common value, or if they differ, our
813 $bogo = new Bogo($this->user
);
814 $filter = ($bogo->single_state ? Bogo
::$states[$bogo->state
] : Bogo
::MAIN_DEFAULT
);
815 // If the email was already present for this user, we reset it to the default values, we thus use REPLACE INTO.
816 XDB
::execute('REPLACE INTO email_redirect_account (uid, redirect, flags, action)
817 VALUES ({?}, {?}, \'active\', {?})',
818 $this->user
->id(), $email, $filter);
819 $listClient = new MMList(S
::user());
820 $listClient->change_user_email($this->user
->forlifeEmail(), $new_email);
821 update_alias_user($this->user
->forlifeEmail(), $new_email);
822 if ($logger = S
::v('log', null
)) { // may be absent --> step4.php
823 S
::logger()->log('email_add', $email . ($this->user
->id() != S
::v('uid') ?
" (admin on {$this->user->login()})" : ""));
825 foreach ($this->emails
as $mail) {
826 if ($mail->email
== $email_stripped) {
830 $this->emails
[] = new Email($this->user
, array(
831 'redirect' => $email,
835 'broken_date' => '0000-00-00',
837 'last' => '0000-00-00',
844 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->user
->login());
845 check_redirect($this);
846 $this->update_imap();
850 public function modify_email($emails_actifs, $emails_rewrite)
852 foreach ($this->emails
as &$email) {
853 if (in_array($email->email
, $emails_actifs)) {
856 $email->deactivate();
858 $email->set_rewrite($emails_rewrite[$email->email
]);
860 check_redirect($this);
861 $this->update_imap();
865 public function modify_one_email($email, $activate)
869 foreach ($this->emails
as $i=>$mail) {
870 if ($mail->email
== $email) {
873 $allinactive &= !$mail->active ||
!$mail->sufficient ||
$mail->email
== $email;
875 if ($thisone === false
) {
876 return ERROR_INVALID_EMAIL
;
878 if ($allinactive ||
$activate) {
879 $this->emails
[$thisone]->activate();
881 $this->emails
[$thisone]->deactivate();
883 check_redirect($this);
884 $this->update_imap();
885 if ($allinactive && !$activate) {
886 return ERROR_INACTIVE_REDIRECTION
;
891 public function modify_one_email_redirect($email, $redirect)
893 foreach ($this->emails
as &$mail) {
894 if ($mail->email
== $email) {
895 $mail->set_rewrite($redirect);
896 check_redirect($this);
897 $this->update_imap();
903 public function clean_errors($email)
905 foreach ($this->emails
as &$mail) {
906 if ($mail->email
== $email) {
907 check_redirect($this);
908 $this->update_imap();
909 return $mail->clean_errors();
915 public function disable()
917 XDB
::execute("UPDATE email_redirect_account
918 SET flags = 'disable'
919 WHERE flags = 'active' AND uid = {?}", $this->user
->id());
920 foreach ($this->emails
as &$mail) {
921 if ($mail->active
&& $mail->has_disable()) {
922 $mail->disabled
= true
;
923 $mail->active
= false
;
926 check_redirect($this);
927 $this->update_imap();
930 public function enable()
932 XDB
::execute("UPDATE email_redirect_account
934 WHERE flags = 'disable' AND uid = {?}", $this->user
->id());
935 foreach ($this->emails
as &$mail) {
936 if ($mail->disabled
) {
937 $mail->disabled
= false
;
938 $mail->active
= true
;
940 check_redirect($this);
942 $this->update_imap();
945 public function get_broken_mx()
947 $res = XDB
::query("SELECT host, text
949 WHERE state != 'ok'");
950 if (!$res->numRows()) {
953 $mxs = $res->fetchAllAssoc();
955 foreach ($this->emails
as &$mail) {
956 if ($mail->active
&& strstr($mail->email
, '@') !== false
) {
957 list(,$domain) = explode('@', $mail->email
);
958 getmxrr($domain, $lcl_mxs);
959 if (empty($lcl_mxs)) {
960 $lcl_mxs = array($domain);
963 foreach ($mxs as &$mx) {
964 foreach ($lcl_mxs as $lcl) {
965 if (fnmatch($mx['host'], $lcl)) {
966 $broken = $mx['text'];
971 $mails[] = array('mail' => $mail->email
, 'text' => $broken);
980 public function active_emails()
983 foreach ($this->emails
as $mail) {
991 public function get_uid()
993 return $this->user
->id();
996 private function update_imap()
998 // Imaps must bounce if and only if the user has no active redirection.
999 if (!$this->other_active('')) {
1000 XDB
::execute('UPDATE email_redirect_account
1001 SET action = \'imap_and_bounce\'
1002 WHERE type = \'imap\' AND uid = {?}',
1005 XDB
::execute('UPDATE email_redirect_account
1006 SET action = \'let_spams\'
1007 WHERE type = \'imap\' AND uid = {?}',
1013 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: