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 function add_to_list_alias(User
$user, $local_part, $domain, $type = 'alias')
29 Platal
::assert($user !== null
);
31 XDB
::execute('INSERT IGNORE INTO email_virtual (email, domain, redirect, type)
32 SELECT {?}, id, {?}, {?}
33 FROM email_virtual_domains
35 $local_part, $user->forlifeEmail(), $type, $domain);
38 function delete_from_list_alias(User
$user, $local_part, $domain, $type = 'alias')
40 Platal
::assert($user !== null
);
42 XDB
::execute('DELETE v
43 FROM email_virtual AS v
44 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
45 INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id)
46 WHERE v.email = {?} AND d.name = {?} AND v.redirect = {?} AND type = {?}',
47 $local_part, $domain, $user->forlifeEmail(), $type);
50 function update_list_alias(User
$user, $former_email, $local_part, $domain, $type = 'alias')
52 Platal
::assert($user !== null
);
54 XDB
::execute('UPDATE email_virtual AS v
55 INNER JOIN email_virtual_domains AS d ON (v.domain = d.id)
57 WHERE v.redirect = {?} AND d.name = {?} AND v.email = {?} AND v.type = {?}',
58 $user->forlifeEmail(), $former_email, $domain, $local_part, $type);
61 function list_alias_members($local_part, $domain)
63 $emails = XDB
::fetchColumn('SELECT DISTINCT(redirect)
64 FROM email_virtual AS v
65 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
66 INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id)
67 WHERE v.email = {?} AND d.name = {?} AND type = \'alias\'',
68 $local_part, $domain);
71 foreach ($emails as $email) {
72 $members[] = User
::getSilent($email);
78 function delete_list_alias($local_part, $domain)
80 XDB
::execute('DELETE v
81 FROM email_virtual AS v
82 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
83 INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id)
84 WHERE v.email = {?} AND d.name = {?} AND type = \'alias\'',
85 $local_part, $domain);
88 function iterate_list_alias($domain)
90 return XDB
::fetchColumn('SELECT CONCAT(v.email, \'@\', m.name)
91 FROM email_virtual AS v
92 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
93 WHERE m.name = {?} AND v.type = \'alias\'
98 function create_list($local_part, $domain)
102 $redirect = $domain . '_' . $local_part . '+';
103 foreach(array('post', 'owner', 'admin', 'bounces', 'unsubscribe') as $suffix) {
104 XDB
::execute('INSERT IGNORE INTO email_virtual (email, domain, redirect, type)
105 SELECT {?}, id, {?}, \'list\'
106 FROM email_virtual_domains
108 ($suffix == 'post') ?
$local_part : $local_part . '-' . $suffix,
109 $redirect . $suffix . '@' . $globals->lists
->redirect_domain
, $domain);
113 function delete_list($local_part, $domain)
117 $redirect = $domain . '_' . $local_part . '+';
118 foreach(array('post', 'owner', 'admin', 'bounces', 'unsubscribe') as $suffix) {
119 XDB
::execute('DELETE email_virtual
120 WHERE redirect = {?} AND type = \'list\'',
121 $redirect . $suffix . '@' . $globals->lists
->redirect_domain
);
125 function list_exist($local_part, $domain)
127 return XDB
::fetchOneCell('SELECT COUNT(*)
128 FROM email_virtual AS v
129 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
130 INNER JOIN email_virtual_domains AS d ON (m.id = d.aliasing)
131 WHERE v.email = {?} AND d.name = {?}',
132 $local_part, $domain);
135 // function mark_broken_email() {{{1
136 function mark_broken_email($email, $admin = false
)
138 $email = valide_email($email);
139 if (empty($email) ||
$email == '@') {
143 $user = XDB
::fetchOneAssoc('SELECT r1.uid, r1.broken_level != 0 AS broken, a.hruid, COUNT(r2.uid) AS nb_mails, a.full_name, s.email AS alias
144 FROM email_redirect_account AS r1
145 INNER JOIN accounts AS a ON (a.uid = r1.uid)
146 INNER JOIN email_source_account AS s ON (a.uid = s.uid AND s.flags = \'bestalias\')
147 LEFT JOIN email_redirect_account AS r2 ON (a.uid = r2.uid AND r1.redirect != r2.redirect AND
148 r2.broken_level = 0 AND r2.flags = \'active\' AND
149 (r2.type = \'smtp\' OR r2.type = \'googleapps\'))
150 WHERE r1.redirect = {?}
151 GROUP BY r1.uid', $email);
154 // Mark address as broken.
155 if (!$user['broken']) {
156 XDB
::execute('UPDATE email_redirect_account
157 SET broken_date = NOW(), last = NOW(), broken_level = 1
158 WHERE redirect = {?}', $email);
160 XDB
::execute('UPDATE email_redirect_account
161 SET last = CURDATE(), broken_level = broken_level + 1
162 WHERE redirect = {?} AND DATE_ADD(last, INTERVAL 14 DAY) < CURDATE()',
165 XDB
::execute('UPDATE email_redirect_account
167 WHERE redirect = {?} AND broken_level = 0', $email);
174 // function fix_bestalias() {{{1
175 // Checks for an existing 'bestalias' among the the current user's aliases, and
176 // eventually selects a new bestalias when required.
177 function fix_bestalias(User
$user)
179 // First check if the bestalias is properly set.
180 $alias_count = XDB
::fetchOneCell('SELECT COUNT(*)
181 FROM email_source_account
182 WHERE uid = {?} AND FIND_IN_SET(\'bestalias\', flags) AND expire IS NULL',
185 if ($alias_count > 1) {
186 // If too many bestaliases, delete the bestalias flag from all this
187 // user's emails (this should never happen).
188 XDB
::execute("UPDATE email_source_account
189 SET flags = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', flags, ','), ',bestalias,', ','))
193 if ($alias_count != 1) {
194 // If no bestalias is selected, we choose the shortest email which is not
195 // related to a usage name and contains a '.'.
196 XDB
::execute("UPDATE email_source_account
197 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'bestalias')
198 WHERE uid = {?} AND expire IS NULL
199 ORDER BY NOT FIND_IN_SET('usage', flags), email LIKE '%.%', LENGTH(email)
204 // First check if best_domain is properly set.
205 $domain_count = XDB
::fetchOneCell('SELECT COUNT(*)
207 INNER JOIN email_source_account AS s ON (s.uid = a.uid AND FIND_IN_SET(\'bestalias\', s.flags))
208 INNER JOIN email_virtual_domains AS d ON (d.id = a.best_domain)
209 INNER JOIN email_virtual_domains AS m ON (d.aliasing = m.id)
210 INNER JOIN email_virtual_domains AS v ON (v.aliasing = m.id AND v.id = s.domain)
211 WHERE a.uid = {?} AND (m.name = {?} OR m.name = {?})',
212 $user->id(), $user->mainEmailDomain(), Platal
::globals()->mail
->alias_dom
);
214 if ($domain_count == 0) {
215 XDB
::execute('UPDATE accounts AS a
216 INNER JOIN email_source_account AS s ON (s.uid = a.uid AND FIND_IN_SET(\'bestalias\', s.flags))
217 INNER JOIN email_virtual_domains AS d ON (d.aliasing = s.domain AND (d.name = {?} OR d.name = {?}))
218 SET a.best_domain = d.id
220 $user->mainEmailDomain(), Platal
::globals()->mail
->alias_dom
, $user->id());
226 // function valide_email() {{{1
227 // Returns a cleaned-up version of the @p email string. It removes garbage
228 // characters, and determines the canonical form (without _ and +) for
229 // Polytechnique.org email addresses.
230 function valide_email($str)
234 $em = trim(rtrim($str));
235 $em = str_replace('<', '', $em);
236 $em = str_replace('>', '', $em);
237 if (strpos($em, '@') === false
) {
240 list($ident, $dom) = explode('@', $em);
241 if (User
::isMainMailDomain($dom)) {
242 list($ident1) = explode('_', $ident);
243 list($ident) = explode('+', $ident1);
245 return $ident . '@' . $dom;
248 // function isvalid_email_redirection() {{{1
249 /** Checks if an email is a suitable redirection.
250 * @param $email the email to check
253 function isvalid_email_redirection($email)
255 return isvalid_email($email) && !preg_match("/@polytechnique\.edu$/", $email) && User
::isForeignEmailAddress($email);
258 // function ids_from_mails() {{{1
259 // Converts an array of emails to an array of email => uid, where email is the
260 // given email when we found a matching user.
261 function ids_from_mails(array $emails)
263 // Removes duplicates, if any.
264 $emails = array_unique($emails);
266 // Formats and splits by domain type (locally managed or external) emails.
267 $main_domain_emails = array();
268 $aux_domain_emails = array();
269 $other_emails = array();
270 foreach ($emails as $email) {
271 if (strpos($email, '@') === false
) {
272 $main_domain_emails[] = $email;
274 if (User
::isForeignEmailAddress($email)) {
275 $other_emails[$email] = strtolower($user . '@' . $domain);
277 list($local_part, $domain) = explode('@', $email);
278 list($local_part) = explode('+', $local_part);
279 list($local_part) = explode('_', $local_part);
280 if (User
::isMainMailDomain($domain)) {
281 $main_domain_emails[$email] = strtolower($local_part);
282 } elseif (User
::isAliasMailDomain($domain)) {
283 $aux_domain_emails[$email] = strtolower($local_part);
289 // Retrieves emails from our domains.
290 $main_domain_uids = XDB
::fetchAllAssoc('email',
292 FROM email_source_account
293 WHERE email IN {?} AND type != \'alias_aux\'',
294 array_unique($main_domain_emails));
295 $aux_domain_uids = XDB
::fetchAllAssoc('email',
297 FROM email_source_account
298 WHERE email IN {?} AND type = \'alias_aux\'',
299 array_unique($aux_domain_emails));
301 // Retrieves emails from redirections.
302 $other_uids = XDB
::fetchAllAssoc('redirect',
303 'SELECT redirect, uid
304 FROM email_redirect_account
305 WHERE redirect IN {?}',
306 array_unique($other_emails));
308 // Associates given emails with the corresponding uid.
310 foreach ($main_domain_emails as $email => $key) {
311 $uids[$email] = $main_domain_uids[$key];
313 foreach ($aux_domain_emails as $email => $key) {
314 $uids[$email] = $aux_domain_uids[$key];
316 foreach ($other_emails as $email => $key) {
317 $uids[$email] = $other_uids[$key];
320 return array_unique($uids);
324 // The Bogo class represents a spam filtering level in plat/al architecture.
327 public static $states = array(
331 3 => 'tag_and_drop_spams',
337 public $single_state;
338 public $redirections;
339 public $single_redirection;
341 public function __construct(User
$user)
347 $this->user
= &$user;
348 $res = XDB
::fetchOneAssoc('SELECT COUNT(DISTINCT(action)) AS action_count, COUNT(redirect) AS redirect_count, action
349 FROM email_redirect_account
350 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\') AND flags = \'active\'',
352 if ($res['redirect_count'] == 0) {
356 $this->single_redirection
= ($res['redirect_count'] == 1);
357 $this->redirections
= XDB
::fetchAllAssoc('SELECT IF(type = \'googleapps\', type, redirect) AS redirect, type, action
358 FROM email_redirect_account
359 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')
360 ORDER BY type, redirect',
363 foreach ($this->redirections
AS &$redirection) {
364 $redirection['filter'] = array_search($redirection['action'], self
::$states);
366 if ($res['action_count'] == 1) {
367 $this->state
= array_search($res['action'], self
::$states);
368 $this->single_state
= true
;
370 $this->single_state
= $this->state
= false
;
374 public function changeAll($state)
376 Platal
::assert($state >= 0 && $state < count(self
::$states), 'Unknown antispam level.');
378 $this->state
= $state;
379 XDB
::execute('UPDATE email_redirect_account
381 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')',
382 self
::$states[$this->state
], $this->user
->id());
385 public function change($redirection, $state)
387 Platal
::assert($state >= 0 && $state < count(self
::$states), 'Unknown antispam level.');
389 XDB
::execute('UPDATE email_redirect_account
391 WHERE uid = {?} AND (type = {?} OR redirect = {?})',
392 self
::$states[$state], $this->user
->id(), $redirection, $redirection);
397 // Represents an "email address" used as final recipient for plat/al-managed
401 // Lists fields to load automatically.
402 static private $field_names = array('rewrite', 'type', 'action', 'broken_date', 'broken_level', 'last', 'hash', 'allow_rewrite');
404 // Shortname to realname mapping for known mail storage backends.
405 static private $display_names = array(
406 'imap' => 'Accès de secours aux emails (IMAP)',
407 'googleapps' => 'Compte Google Apps',
409 static private $storage_domains = array(
416 // Basic email properties; $sufficient indicates if the email can be used as
417 // an unique redirection; $redirect contains the delivery email address.
421 public $display_email;
424 public $filter_level;
426 // Redirection status properties.
432 public $allow_rewrite;
435 // Redirection bounces stats.
437 public $broken_level;
440 public function __construct(User
$user, array $row)
442 foreach (self
::$field_names as $field) {
443 if (array_key_exists($field, $row)) {
444 $this->$field = $row[$field];
447 $this->email
= $row['redirect'];
449 if (array_key_exists($this->type
, Email
::$display_names)) {
450 $this->display_email
= self
::$display_names[$this->type
];
452 $this->display_email
= $this->email
;
454 foreach (array('active', 'inactive', 'broken', 'disabled') as $status) {
455 $this->$status = ($status == $row['flags']);
457 $this->sufficient
= ($this->type
== 'smtp' ||
$this->type
== 'googleapps');
458 $this->filter_level
= ($this->type
== 'imap') ? null
: array_search($this->action
, Bogo
::$states);
459 $this->user
= &$user;
462 // Activates the email address as a redirection.
463 public function activate()
465 if ($this->inactive
) {
466 XDB
::execute('UPDATE email_redirect_account
467 SET broken_level = IF(flags = \'broken\', broken_level - 1, broken_level), flags = \'active\'
468 WHERE uid = {?} AND redirect = {?}',
469 $this->user
->id(), $this->email
);
470 S
::logger()->log('email_on', $this->email
. ($this->user
->id() != S
::v('uid') ?
"(admin on {$this->user->login()})" : ''));
471 $this->inactive
= false
;
472 $this->active
= true
;
476 // Deactivates the email address as a redirection.
477 public function deactivate()
480 XDB
::execute('UPDATE email_redirect_account
481 SET flags = \'inactive\'
482 WHERE uid = {?} AND redirect = {?}',
483 $this->user
->id(), $this->email
);
484 S
::logger()->log('email_off', $this->email
. ($this->user
->id() != S
::v('uid') ?
"(admin on {$this->user->login()})" : "") );
485 $this->inactive
= true
;
486 $this->active
= false
;
491 // Sets the rewrite rule for the given address.
492 public function set_rewrite($rewrite)
494 if ($this->type
!= 'smtp' ||
$this->rewrite
== $rewrite) {
497 if (!$rewrite ||
!isvalid_email($rewrite)) {
500 XDB
::execute('UPDATE email_redirect_account
502 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
503 $rewrite, $this->user
->id(), $this->email
);
504 $this->rewrite
= $rewrite;
505 if (!$this->allow_rewrite
) {
507 if (empty($this->hash
)) {
508 $this->hash
= rand_url_id();
509 XDB
::execute('UPDATE email_redirect_account
511 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
512 $this->hash
, $this->user
->id(), $this->email
);
514 $mail = new PlMailer('emails/rewrite-in.mail.tpl');
515 $mail->assign('mail', $this);
516 $mail->assign('user', $this->user
);
517 $mail->assign('baseurl', $globals->baseurl
);
518 $mail->assign('sitename', $globals->core
->sitename
);
519 $mail->assign('to', $this->email
);
520 $mail->send($this->user
->isEmailFormatHtml());
525 // Resets the error counts associated with the redirection.
526 public function clean_errors()
528 if ($this->type
!= 'smtp') {
535 $this->broken_level
= 0;
537 return XDB
::execute('UPDATE email_redirect_account
538 SET broken_level = 0, broken_date = 0, last = 0
539 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
540 $this->user
->id(), $this->email
);
544 // Email backend capabilities ('rewrite' refers to From: rewrite for mails
545 // forwarded by Polytechnique.org's MXs; 'removable' indicates if the email
546 // can be definitively removed; 'disable' indicates if the email has a third
547 // status 'disabled' in addition to 'active' and 'inactive').
548 public function has_rewrite()
550 return ($this->type
== 'smtp');
553 public function is_removable()
555 return ($this->type
== 'smtp');
558 public function has_disable()
563 public function is_redirection()
565 return ($this->type
== 'smtp');
568 // Returns the list of allowed storages for the @p user.
569 static private function get_allowed_storages(User
$user)
574 // Google Apps storage is available for users with valid Google Apps account.
575 require_once 'googleapps.inc.php';
576 if ($globals->mailstorage
->googleapps_domain
&&
577 GoogleAppsAccount
::account_status($user->id()) == 'active') {
578 $storages[] = 'googleapps';
581 // IMAP storage is always visible to administrators, and is allowed for
582 // everyone when the service is marked as 'active'.
583 if ($globals->mailstorage
->imap_active || S
::admin()) {
584 $storages[] = 'imap';
590 static public function activate_storage(User
$user, $storage)
592 Platal
::assert(in_array($storage, self
::get_allowed_storages($user)));
594 if (!self
::is_active_storage($user, $storage)) {
597 XDB
::execute('INSERT INTO email_redirect_account (uid, type, redirect, flags)
598 VALUES ({?}, {?}, {?}, \'active\')',
599 $user->id(), $storage,
600 $user->hruid
. '@' . self
::$storage_domains[$storage] . '.' . $globals->mail
->domain
);
604 static public function deactivate_storage(User
$user, $storage)
606 if (in_array($storage, self
::$storage_domains)) {
607 XDB
::execute('DELETE FROM email_redirect_account
608 WHERE uid = {?} AND type = {?}',
609 $user->id(), $storage);
613 static public function is_active_storage(User
$user, $storage)
615 if (!in_array($storage, self
::$storage_domains)) {
618 $res = XDB
::fetchOneCell('SELECT COUNT(*)
619 FROM email_redirect_account
620 WHERE uid = {?} AND type = {?} AND flags = \'active\'',
621 $user->id(), $storage);
622 return !is_null($res) && $res > 0;
625 // class Redirect {{{1
626 // Redirect is a placeholder class for an user's active redirections (third-party
627 // redirection email, or Polytechnique.org mail storages).
630 private $flags = 'active';
635 public function __construct(User
$user)
637 $this->user
= &$user;
639 // Adds third-party email redirections.
640 $res = XDB
::iterator('SELECT redirect, rewrite, type, action, broken_date, broken_level, last, flags, hash, allow_rewrite
641 FROM email_redirect_account
642 WHERE uid = {?} AND type != \'homonym\'',
644 $this->emails
= array();
645 while ($row = $res->next()) {
646 $this->emails
[] = new Email($user, $row);
650 public function other_active($email)
652 foreach ($this->emails
as $mail) {
653 if ($mail->email
!= $email && $mail->active
&& $mail->sufficient
) {
660 public function delete_email($email)
662 if (!$this->other_active($email)) {
663 return ERROR_INACTIVE_REDIRECTION
;
665 XDB
::execute('DELETE FROM email_redirect_account
666 WHERE uid = {?} AND redirect = {?} AND type != \'homonym\'',
667 $this->user
->id(), $email);
668 S
::logger()->log('email_del', $email . ($this->user
->id() != S
::v('uid') ?
" (admin on {$this->user->login()})" : ""));
669 foreach ($this->emails
as $i => $mail) {
670 if ($email == $mail->email
) {
671 unset($this->emails
[$i]);
674 check_redirect($this);
675 $this->update_imap();
679 public function add_email($email)
681 $email_stripped = strtolower(trim($email));
682 if (!isvalid_email($email_stripped)) {
683 return ERROR_INVALID_EMAIL
;
685 if (!isvalid_email_redirection($email_stripped)) {
686 return ERROR_LOOP_EMAIL
;
688 // We first need to retrieve the value for the antispam filter: it is
689 // either the user's redirections common value, or if they differ, our
691 $bogo = new Bogo($this->user
);
692 $filter = ($bogo->single_state ? Bogo
::$states[$bogo->state
] : Bogo
::$states[0]);
693 // If the email was already present for this user, we reset it to the default values, we thus use REPLACE INTO.
694 XDB
::execute('REPLACE INTO email_redirect_account (uid, redirect, flags, action)
695 VALUES ({?}, {?}, \'active\', {?})',
696 $this->user
->id(), $email, $filter);
697 if ($logger = S
::v('log', null
)) { // may be absent --> step4.php
698 S
::logger()->log('email_add', $email . ($this->user
->id() != S
::v('uid') ?
" (admin on {$this->user->login()})" : ""));
700 foreach ($this->emails
as $mail) {
701 if ($mail->email
== $email_stripped) {
705 $this->emails
[] = new Email($this->user
, array(
706 'redirect' => $email,
710 'broken_date' => '0000-00-00',
712 'last' => '0000-00-00',
719 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->user
->login());
720 check_redirect($this);
721 $this->update_imap();
725 public function modify_email($emails_actifs, $emails_rewrite)
727 foreach ($this->emails
as &$email) {
728 if (in_array($email->email
, $emails_actifs)) {
731 $email->deactivate();
733 $email->set_rewrite($emails_rewrite[$email->email
]);
735 check_redirect($this);
736 $this->update_imap();
740 public function modify_one_email($email, $activate)
744 foreach ($this->emails
as $i=>$mail) {
745 if ($mail->email
== $email) {
748 $allinactive &= !$mail->active ||
!$mail->sufficient ||
$mail->email
== $email;
750 if ($thisone === false
) {
751 return ERROR_INVALID_EMAIL
;
753 if ($allinactive ||
$activate) {
754 $this->emails
[$thisone]->activate();
756 $this->emails
[$thisone]->deactivate();
758 check_redirect($this);
759 $this->update_imap();
760 if ($allinactive && !$activate) {
761 return ERROR_INACTIVE_REDIRECTION
;
766 public function modify_one_email_redirect($email, $redirect)
768 foreach ($this->emails
as &$mail) {
769 if ($mail->email
== $email) {
770 $mail->set_rewrite($redirect);
771 check_redirect($this);
772 $this->update_imap();
778 public function clean_errors($email)
780 foreach ($this->emails
as &$mail) {
781 if ($mail->email
== $email) {
782 check_redirect($this);
783 $this->update_imap();
784 return $mail->clean_errors();
790 public function disable()
792 XDB
::execute("UPDATE email_redirect_account
793 SET flags = 'disable'
794 WHERE flags = 'active' AND uid = {?}", $this->user
->id());
795 foreach ($this->emails
as &$mail) {
796 if ($mail->active
&& $mail->has_disable()) {
797 $mail->disabled
= true
;
798 $mail->active
= false
;
801 check_redirect($this);
802 $this->update_imap();
805 public function enable()
807 XDB
::execute("UPDATE email_redirect_account
809 WHERE flags = 'disable' AND uid = {?}", $this->user
->id());
810 foreach ($this->emails
as &$mail) {
811 if ($mail->disabled
) {
812 $mail->disabled
= false
;
813 $mail->active
= true
;
815 check_redirect($this);
817 $this->update_imap();
820 public function get_broken_mx()
822 $res = XDB
::query("SELECT host, text
824 WHERE state != 'ok'");
825 if (!$res->numRows()) {
828 $mxs = $res->fetchAllAssoc();
830 foreach ($this->emails
as &$mail) {
831 if ($mail->active
&& strstr($mail->email
, '@') !== false
) {
832 list(,$domain) = explode('@', $mail->email
);
833 getmxrr($domain, $lcl_mxs);
834 if (empty($lcl_mxs)) {
835 $lcl_mxs = array($domain);
838 foreach ($mxs as &$mx) {
839 foreach ($lcl_mxs as $lcl) {
840 if (fnmatch($mx['host'], $lcl)) {
841 $broken = $mx['text'];
846 $mails[] = array('mail' => $mail->email
, 'text' => $broken);
855 public function active_emails()
858 foreach ($this->emails
as $mail) {
866 public function get_uid()
868 return $this->user
->id();
871 private function update_imap()
873 // Imaps must bounce if and only if the user has no active redirection.
874 if (!$this->other_active('')) {
875 XDB
::execute('UPDATE email_redirect_account
876 SET action = \'imap_and_bounce\'
877 WHERE type = \'imap\' AND uid = {?}',
880 XDB
::execute('UPDATE email_redirect_account
881 SET action = \'let_spams\'
882 WHERE type = \'imap\' AND uid = {?}',
888 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: