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, COUNT(r2.uid) AS nb_mails,
144 s.email AS alias, DATE_ADD(r1.last, INTERVAL 14 DAY) < CURDATE() as notify
145 FROM email_redirect_account AS r1
146 INNER JOIN accounts AS a ON (a.uid = r1.uid)
147 INNER JOIN email_source_account AS s ON (a.uid = s.uid AND s.flags = \'bestalias\')
148 LEFT JOIN email_redirect_account AS r2 ON (a.uid = r2.uid AND r1.redirect != r2.redirect AND
149 r2.broken_level = 0 AND r2.flags = \'active\' AND
150 (r2.type = \'smtp\' OR r2.type = \'googleapps\'))
151 WHERE r1.redirect = {?}
152 GROUP BY r1.uid', $email);
155 // Mark address as broken.
156 if (!$user['broken']) {
157 XDB
::execute('UPDATE email_redirect_account
158 SET broken_date = NOW(), last = NOW(), broken_level = 1
159 WHERE redirect = {?}', $email);
161 XDB
::execute('UPDATE email_redirect_account
162 SET last = CURDATE(), broken_level = broken_level + 1
163 WHERE redirect = {?} AND DATE_ADD(last, INTERVAL 14 DAY) < CURDATE()',
166 XDB
::execute('UPDATE email_redirect_account
168 WHERE redirect = {?} AND broken_level = 0', $email);
175 // function fix_bestalias() {{{1
176 // Checks for an existing 'bestalias' among the the current user's aliases, and
177 // eventually selects a new bestalias when required.
178 function fix_bestalias(User
$user)
180 // First check if the bestalias is properly set.
181 $alias_count = XDB
::fetchOneCell('SELECT COUNT(*)
182 FROM email_source_account
183 WHERE uid = {?} AND FIND_IN_SET(\'bestalias\', flags) AND expire IS NULL',
186 if ($alias_count > 1) {
187 // If too many bestaliases, delete the bestalias flag from all this
188 // user's emails (this should never happen).
189 XDB
::execute("UPDATE email_source_account
190 SET flags = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', flags, ','), ',bestalias,', ','))
194 if ($alias_count != 1) {
195 // If no bestalias is selected, we choose the shortest email which is not
196 // related to a usage name and contains a '.'.
197 XDB
::execute("UPDATE email_source_account
198 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'bestalias')
199 WHERE uid = {?} AND expire IS NULL
200 ORDER BY NOT FIND_IN_SET('usage', flags), email LIKE '%.%', LENGTH(email)
205 // First check if best_domain is properly set.
206 $domain_count = XDB
::fetchOneCell('SELECT COUNT(*)
208 INNER JOIN email_source_account AS s ON (s.uid = a.uid AND FIND_IN_SET(\'bestalias\', s.flags))
209 INNER JOIN email_virtual_domains AS d ON (d.id = a.best_domain)
210 INNER JOIN email_virtual_domains AS m ON (d.aliasing = m.id)
211 INNER JOIN email_virtual_domains AS v ON (v.aliasing = m.id AND v.id = s.domain)
212 WHERE a.uid = {?} AND (m.name = {?} OR m.name = {?})',
213 $user->id(), $user->mainEmailDomain(), Platal
::globals()->mail
->alias_dom
);
215 if ($domain_count == 0) {
216 XDB
::execute('UPDATE accounts AS a
217 INNER JOIN email_source_account AS s ON (s.uid = a.uid AND FIND_IN_SET(\'bestalias\', s.flags))
218 INNER JOIN email_virtual_domains AS d ON (d.aliasing = s.domain AND (d.name = {?} OR d.name = {?}))
219 SET a.best_domain = d.id
221 $user->mainEmailDomain(), Platal
::globals()->mail
->alias_dom
, $user->id());
227 // function valide_email() {{{1
228 // Returns a cleaned-up version of the @p email string. It removes garbage
229 // characters, and determines the canonical form (without _ and +) for
230 // Polytechnique.org email addresses.
231 function valide_email($str)
235 $em = trim(rtrim($str));
236 $em = str_replace('<', '', $em);
237 $em = str_replace('>', '', $em);
238 if (strpos($em, '@') === false
) {
241 list($ident, $dom) = explode('@', $em);
242 if (User
::isMainMailDomain($dom)) {
243 list($ident1) = explode('_', $ident);
244 list($ident) = explode('+', $ident1);
246 return $ident . '@' . $dom;
249 // function isvalid_email_redirection() {{{1
250 /** Checks if an email is a suitable redirection.
251 * @param $email the email to check
254 function isvalid_email_redirection($email)
256 return isvalid_email($email) && !preg_match("/@polytechnique\.edu$/", $email) && User
::isForeignEmailAddress($email);
259 // function ids_from_mails() {{{1
260 // Converts an array of emails to an array of email => uid, where email is the
261 // given email when we found a matching user.
262 function ids_from_mails(array $emails)
264 // Removes duplicates, if any.
265 $emails = array_unique($emails);
267 // Formats and splits by domain type (locally managed or external) emails.
268 $main_domain_emails = array();
269 $aux_domain_emails = array();
270 $other_emails = array();
271 foreach ($emails as $email) {
272 if (strpos($email, '@') === false
) {
273 $main_domain_emails[] = $email;
275 if (User
::isForeignEmailAddress($email)) {
276 $other_emails[$email] = strtolower($user . '@' . $domain);
278 list($local_part, $domain) = explode('@', $email);
279 list($local_part) = explode('+', $local_part);
280 list($local_part) = explode('_', $local_part);
281 if (User
::isMainMailDomain($domain)) {
282 $main_domain_emails[$email] = strtolower($local_part);
283 } elseif (User
::isAliasMailDomain($domain)) {
284 $aux_domain_emails[$email] = strtolower($local_part);
290 // Retrieves emails from our domains.
291 $main_domain_uids = XDB
::fetchAllAssoc('email',
293 FROM email_source_account
294 WHERE email IN {?} AND type != \'alias_aux\'',
295 array_unique($main_domain_emails));
296 $aux_domain_uids = XDB
::fetchAllAssoc('email',
298 FROM email_source_account
299 WHERE email IN {?} AND type = \'alias_aux\'',
300 array_unique($aux_domain_emails));
302 // Retrieves emails from redirections.
303 $other_uids = XDB
::fetchAllAssoc('redirect',
304 'SELECT redirect, uid
305 FROM email_redirect_account
306 WHERE redirect IN {?}',
307 array_unique($other_emails));
309 // Associates given emails with the corresponding uid.
311 foreach ($main_domain_emails as $email => $key) {
312 $uids[$email] = $main_domain_uids[$key];
314 foreach ($aux_domain_emails as $email => $key) {
315 $uids[$email] = $aux_domain_uids[$key];
317 foreach ($other_emails as $email => $key) {
318 $uids[$email] = $other_uids[$key];
321 return array_unique($uids);
325 // The Bogo class represents a spam filtering level in plat/al architecture.
328 public static $states = array(
332 3 => 'tag_and_drop_spams',
338 public $single_state;
339 public $redirections;
340 public $single_redirection;
342 public function __construct(User
$user)
348 $this->user
= &$user;
349 $res = XDB
::fetchOneAssoc('SELECT COUNT(DISTINCT(action)) AS action_count, COUNT(redirect) AS redirect_count, action
350 FROM email_redirect_account
351 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\') AND flags = \'active\'',
353 if ($res['redirect_count'] == 0) {
357 $this->single_redirection
= ($res['redirect_count'] == 1);
358 $this->redirections
= XDB
::fetchAllAssoc('SELECT IF(type = \'googleapps\', type, redirect) AS redirect, type, action
359 FROM email_redirect_account
360 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')
361 ORDER BY type, redirect',
364 foreach ($this->redirections
AS &$redirection) {
365 $redirection['filter'] = array_search($redirection['action'], self
::$states);
367 if ($res['action_count'] == 1) {
368 $this->state
= array_search($res['action'], self
::$states);
369 $this->single_state
= true
;
371 $this->single_state
= $this->state
= false
;
375 public function changeAll($state)
377 Platal
::assert($state >= 0 && $state < count(self
::$states), 'Unknown antispam level.');
379 $this->state
= $state;
380 XDB
::execute('UPDATE email_redirect_account
382 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')',
383 self
::$states[$this->state
], $this->user
->id());
386 public function change($redirection, $state)
388 Platal
::assert($state >= 0 && $state < count(self
::$states), 'Unknown antispam level.');
390 XDB
::execute('UPDATE email_redirect_account
392 WHERE uid = {?} AND (type = {?} OR redirect = {?})',
393 self
::$states[$state], $this->user
->id(), $redirection, $redirection);
398 // Represents an "email address" used as final recipient for plat/al-managed
402 // Lists fields to load automatically.
403 static private $field_names = array('rewrite', 'type', 'action', 'broken_date', 'broken_level', 'last', 'hash', 'allow_rewrite');
405 // Shortname to realname mapping for known mail storage backends.
406 static private $display_names = array(
407 'imap' => 'Accès de secours aux emails (IMAP)',
408 'googleapps' => 'Compte Google Apps',
410 static private $storage_domains = array(
417 // Basic email properties; $sufficient indicates if the email can be used as
418 // an unique redirection; $redirect contains the delivery email address.
422 public $display_email;
425 public $filter_level;
427 // Redirection status properties.
433 public $allow_rewrite;
436 // Redirection bounces stats.
438 public $broken_level;
441 public function __construct(User
$user, array $row)
443 foreach (self
::$field_names as $field) {
444 if (array_key_exists($field, $row)) {
445 $this->$field = $row[$field];
448 $this->email
= $row['redirect'];
450 if (array_key_exists($this->type
, Email
::$display_names)) {
451 $this->display_email
= self
::$display_names[$this->type
];
453 $this->display_email
= $this->email
;
455 foreach (array('active', 'inactive', 'broken', 'disabled') as $status) {
456 $this->$status = ($status == $row['flags']);
458 $this->sufficient
= ($this->type
== 'smtp' ||
$this->type
== 'googleapps');
459 $this->filter_level
= ($this->type
== 'imap') ? null
: array_search($this->action
, Bogo
::$states);
460 $this->user
= &$user;
463 // Activates the email address as a redirection.
464 public function activate()
466 if ($this->inactive
) {
467 XDB
::execute('UPDATE email_redirect_account
468 SET broken_level = IF(flags = \'broken\', broken_level - 1, broken_level), flags = \'active\'
469 WHERE uid = {?} AND redirect = {?}',
470 $this->user
->id(), $this->email
);
471 S
::logger()->log('email_on', $this->email
. ($this->user
->id() != S
::v('uid') ?
"(admin on {$this->user->login()})" : ''));
472 $this->inactive
= false
;
473 $this->active
= true
;
477 // Deactivates the email address as a redirection.
478 public function deactivate()
481 XDB
::execute('UPDATE email_redirect_account
482 SET flags = \'inactive\'
483 WHERE uid = {?} AND redirect = {?}',
484 $this->user
->id(), $this->email
);
485 S
::logger()->log('email_off', $this->email
. ($this->user
->id() != S
::v('uid') ?
"(admin on {$this->user->login()})" : "") );
486 $this->inactive
= true
;
487 $this->active
= false
;
492 // Sets the rewrite rule for the given address.
493 public function set_rewrite($rewrite)
495 if ($this->type
!= 'smtp' ||
$this->rewrite
== $rewrite) {
498 if (!$rewrite ||
!isvalid_email($rewrite)) {
501 XDB
::execute('UPDATE email_redirect_account
503 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
504 $rewrite, $this->user
->id(), $this->email
);
505 $this->rewrite
= $rewrite;
506 if (!$this->allow_rewrite
) {
508 if (empty($this->hash
)) {
509 $this->hash
= rand_url_id();
510 XDB
::execute('UPDATE email_redirect_account
512 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
513 $this->hash
, $this->user
->id(), $this->email
);
515 $mail = new PlMailer('emails/rewrite-in.mail.tpl');
516 $mail->assign('mail', $this);
517 $mail->assign('user', $this->user
);
518 $mail->assign('baseurl', $globals->baseurl
);
519 $mail->assign('sitename', $globals->core
->sitename
);
520 $mail->assign('to', $this->email
);
521 $mail->send($this->user
->isEmailFormatHtml());
526 // Resets the error counts associated with the redirection.
527 public function clean_errors()
529 if ($this->type
!= 'smtp') {
536 $this->broken_level
= 0;
538 return XDB
::execute('UPDATE email_redirect_account
539 SET broken_level = 0, broken_date = 0, last = 0
540 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
541 $this->user
->id(), $this->email
);
545 // Email backend capabilities ('rewrite' refers to From: rewrite for mails
546 // forwarded by Polytechnique.org's MXs; 'removable' indicates if the email
547 // can be definitively removed; 'disable' indicates if the email has a third
548 // status 'disabled' in addition to 'active' and 'inactive').
549 public function has_rewrite()
551 return ($this->type
== 'smtp');
554 public function is_removable()
556 return ($this->type
== 'smtp');
559 public function has_disable()
564 public function is_redirection()
566 return ($this->type
== 'smtp');
569 // Returns the list of allowed storages for the @p user.
570 static private function get_allowed_storages(User
$user)
575 // Google Apps storage is available for users with valid Google Apps account.
576 require_once 'googleapps.inc.php';
577 if ($globals->mailstorage
->googleapps_domain
&&
578 GoogleAppsAccount
::account_status($user->id()) == 'active') {
579 $storages[] = 'googleapps';
582 // IMAP storage is always visible to administrators, and is allowed for
583 // everyone when the service is marked as 'active'.
584 if ($globals->mailstorage
->imap_active || S
::admin()) {
585 $storages[] = 'imap';
591 static public function activate_storage(User
$user, $storage)
593 Platal
::assert(in_array($storage, self
::get_allowed_storages($user)));
595 if (!self
::is_active_storage($user, $storage)) {
598 XDB
::execute('INSERT INTO email_redirect_account (uid, type, redirect, flags)
599 VALUES ({?}, {?}, {?}, \'active\')',
600 $user->id(), $storage,
601 $user->hruid
. '@' . self
::$storage_domains[$storage] . '.' . $globals->mail
->domain
);
605 static public function deactivate_storage(User
$user, $storage)
607 if (in_array($storage, self
::$storage_domains)) {
608 XDB
::execute('DELETE FROM email_redirect_account
609 WHERE uid = {?} AND type = {?}',
610 $user->id(), $storage);
614 static public function is_active_storage(User
$user, $storage)
616 if (!in_array($storage, self
::$storage_domains)) {
619 $res = XDB
::fetchOneCell('SELECT COUNT(*)
620 FROM email_redirect_account
621 WHERE uid = {?} AND type = {?} AND flags = \'active\'',
622 $user->id(), $storage);
623 return !is_null($res) && $res > 0;
626 // class Redirect {{{1
627 // Redirect is a placeholder class for an user's active redirections (third-party
628 // redirection email, or Polytechnique.org mail storages).
631 private $flags = 'active';
636 public function __construct(User
$user)
638 $this->user
= &$user;
640 // Adds third-party email redirections.
641 $res = XDB
::iterator('SELECT redirect, rewrite, type, action, broken_date, broken_level, last, flags, hash, allow_rewrite
642 FROM email_redirect_account
643 WHERE uid = {?} AND type != \'homonym\'',
645 $this->emails
= array();
646 while ($row = $res->next()) {
647 $this->emails
[] = new Email($user, $row);
651 public function other_active($email)
653 foreach ($this->emails
as $mail) {
654 if ($mail->email
!= $email && $mail->active
&& $mail->sufficient
) {
661 public function delete_email($email)
663 if (!$this->other_active($email)) {
664 return ERROR_INACTIVE_REDIRECTION
;
666 XDB
::execute('DELETE FROM email_redirect_account
667 WHERE uid = {?} AND redirect = {?} AND type != \'homonym\'',
668 $this->user
->id(), $email);
669 S
::logger()->log('email_del', $email . ($this->user
->id() != S
::v('uid') ?
" (admin on {$this->user->login()})" : ""));
670 foreach ($this->emails
as $i => $mail) {
671 if ($email == $mail->email
) {
672 unset($this->emails
[$i]);
675 check_redirect($this);
676 $this->update_imap();
680 public function add_email($email)
682 $email_stripped = strtolower(trim($email));
683 if (!isvalid_email($email_stripped)) {
684 return ERROR_INVALID_EMAIL
;
686 if (!isvalid_email_redirection($email_stripped)) {
687 return ERROR_LOOP_EMAIL
;
689 // We first need to retrieve the value for the antispam filter: it is
690 // either the user's redirections common value, or if they differ, our
692 $bogo = new Bogo($this->user
);
693 $filter = ($bogo->single_state ? Bogo
::$states[$bogo->state
] : Bogo
::$states[0]);
694 // If the email was already present for this user, we reset it to the default values, we thus use REPLACE INTO.
695 XDB
::execute('REPLACE INTO email_redirect_account (uid, redirect, flags, action)
696 VALUES ({?}, {?}, \'active\', {?})',
697 $this->user
->id(), $email, $filter);
698 if ($logger = S
::v('log', null
)) { // may be absent --> step4.php
699 S
::logger()->log('email_add', $email . ($this->user
->id() != S
::v('uid') ?
" (admin on {$this->user->login()})" : ""));
701 foreach ($this->emails
as $mail) {
702 if ($mail->email
== $email_stripped) {
706 $this->emails
[] = new Email($this->user
, array(
707 'redirect' => $email,
711 'broken_date' => '0000-00-00',
713 'last' => '0000-00-00',
720 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->user
->login());
721 check_redirect($this);
722 $this->update_imap();
726 public function modify_email($emails_actifs, $emails_rewrite)
728 foreach ($this->emails
as &$email) {
729 if (in_array($email->email
, $emails_actifs)) {
732 $email->deactivate();
734 $email->set_rewrite($emails_rewrite[$email->email
]);
736 check_redirect($this);
737 $this->update_imap();
741 public function modify_one_email($email, $activate)
745 foreach ($this->emails
as $i=>$mail) {
746 if ($mail->email
== $email) {
749 $allinactive &= !$mail->active ||
!$mail->sufficient ||
$mail->email
== $email;
751 if ($thisone === false
) {
752 return ERROR_INVALID_EMAIL
;
754 if ($allinactive ||
$activate) {
755 $this->emails
[$thisone]->activate();
757 $this->emails
[$thisone]->deactivate();
759 check_redirect($this);
760 $this->update_imap();
761 if ($allinactive && !$activate) {
762 return ERROR_INACTIVE_REDIRECTION
;
767 public function modify_one_email_redirect($email, $redirect)
769 foreach ($this->emails
as &$mail) {
770 if ($mail->email
== $email) {
771 $mail->set_rewrite($redirect);
772 check_redirect($this);
773 $this->update_imap();
779 public function clean_errors($email)
781 foreach ($this->emails
as &$mail) {
782 if ($mail->email
== $email) {
783 check_redirect($this);
784 $this->update_imap();
785 return $mail->clean_errors();
791 public function disable()
793 XDB
::execute("UPDATE email_redirect_account
794 SET flags = 'disable'
795 WHERE flags = 'active' AND uid = {?}", $this->user
->id());
796 foreach ($this->emails
as &$mail) {
797 if ($mail->active
&& $mail->has_disable()) {
798 $mail->disabled
= true
;
799 $mail->active
= false
;
802 check_redirect($this);
803 $this->update_imap();
806 public function enable()
808 XDB
::execute("UPDATE email_redirect_account
810 WHERE flags = 'disable' AND uid = {?}", $this->user
->id());
811 foreach ($this->emails
as &$mail) {
812 if ($mail->disabled
) {
813 $mail->disabled
= false
;
814 $mail->active
= true
;
816 check_redirect($this);
818 $this->update_imap();
821 public function get_broken_mx()
823 $res = XDB
::query("SELECT host, text
825 WHERE state != 'ok'");
826 if (!$res->numRows()) {
829 $mxs = $res->fetchAllAssoc();
831 foreach ($this->emails
as &$mail) {
832 if ($mail->active
&& strstr($mail->email
, '@') !== false
) {
833 list(,$domain) = explode('@', $mail->email
);
834 getmxrr($domain, $lcl_mxs);
835 if (empty($lcl_mxs)) {
836 $lcl_mxs = array($domain);
839 foreach ($mxs as &$mx) {
840 foreach ($lcl_mxs as $lcl) {
841 if (fnmatch($mx['host'], $lcl)) {
842 $broken = $mx['text'];
847 $mails[] = array('mail' => $mail->email
, 'text' => $broken);
856 public function active_emails()
859 foreach ($this->emails
as $mail) {
867 public function get_uid()
869 return $this->user
->id();
872 private function update_imap()
874 // Imaps must bounce if and only if the user has no active redirection.
875 if (!$this->other_active('')) {
876 XDB
::execute('UPDATE email_redirect_account
877 SET action = \'imap_and_bounce\'
878 WHERE type = \'imap\' AND uid = {?}',
881 XDB
::execute('UPDATE email_redirect_account
882 SET action = \'let_spams\'
883 WHERE type = \'imap\' AND uid = {?}',
889 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: