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 format_email_alias($email)
29 if ($user = User
::getSilent($email)) {
30 return $user->forlifeEmail();
32 if (isvalid_email($email)) {
38 function add_to_list_alias($email, $local_part, $domain, $type = 'alias')
40 $email = format_email_alias($email);
41 if (is_null($email)) {
45 XDB
::execute('INSERT IGNORE INTO email_virtual (email, domain, redirect, type)
46 SELECT {?}, id, {?}, {?}
47 FROM email_virtual_domains
49 $local_part, $email, $type, $domain);
53 function delete_from_list_alias($email, $local_part, $domain, $type = 'alias')
55 $email = format_email_alias($email);
56 if (is_null($email)) {
60 XDB
::execute('DELETE v
61 FROM email_virtual AS v
62 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
63 INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id)
64 WHERE v.email = {?} AND d.name = {?} AND v.redirect = {?} AND type = {?}',
65 $local_part, $domain, $email, $type);
69 function update_list_alias($email, $former_email, $local_part, $domain, $type = 'alias')
71 $email = format_email_alias($email);
72 if (is_null($email)) {
76 XDB
::execute('UPDATE email_virtual AS v
77 INNER JOIN email_virtual_domains AS d ON (v.domain = d.id)
79 WHERE v.redirect = {?} AND d.name = {?} AND v.email = {?} AND v.type = {?}',
80 $email, $former_email, $domain, $local_part, $type);
84 function list_alias_members($local_part, $domain)
86 $emails = XDB
::fetchColumn('SELECT DISTINCT(redirect)
87 FROM email_virtual AS v
88 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
89 INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id)
90 WHERE v.email = {?} AND d.name = {?} AND type = \'alias\'',
91 $local_part, $domain);
95 foreach ($emails as $email) {
96 if ($user = User
::getSilent($email)) {
105 'nonusers' => $nonusers
109 function delete_list_alias($local_part, $domain)
111 XDB
::execute('DELETE v
112 FROM email_virtual AS v
113 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
114 INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id)
115 WHERE v.email = {?} AND d.name = {?} AND type = \'alias\'',
116 $local_part, $domain);
119 function iterate_list_alias($domain)
121 return XDB
::fetchColumn('SELECT CONCAT(v.email, \'@\', m.name)
122 FROM email_virtual AS v
123 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
124 WHERE m.name = {?} AND v.type = \'alias\'
129 function create_list($local_part, $domain)
133 $redirect = $domain . '_' . $local_part . '+';
134 foreach(array('post', 'owner', 'admin', 'bounces', 'unsubscribe') as $suffix) {
135 XDB
::execute('INSERT IGNORE INTO email_virtual (email, domain, redirect, type)
136 SELECT {?}, id, {?}, \'list\'
137 FROM email_virtual_domains
139 ($suffix == 'post') ?
$local_part : $local_part . '-' . $suffix,
140 $redirect . $suffix . '@' . $globals->lists
->redirect_domain
, $domain);
144 function delete_list($local_part, $domain)
148 $redirect = $domain . '_' . $local_part . '+';
149 foreach(array('post', 'owner', 'admin', 'bounces', 'unsubscribe') as $suffix) {
150 XDB
::execute('DELETE email_virtual
151 WHERE redirect = {?} AND type = \'list\'',
152 $redirect . $suffix . '@' . $globals->lists
->redirect_domain
);
156 function list_exist($local_part, $domain)
158 return XDB
::fetchOneCell('SELECT COUNT(*)
159 FROM email_virtual AS v
160 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
161 INNER JOIN email_virtual_domains AS d ON (m.id = d.aliasing)
162 WHERE v.email = {?} AND d.name = {?}',
163 $local_part, $domain);
166 // function mark_broken_email() {{{1
167 function mark_broken_email($email, $admin = false
)
169 $email = valide_email($email);
170 if (empty($email) ||
$email == '@') {
174 $user = XDB
::fetchOneAssoc('SELECT r1.uid, r1.broken_level != 0 AS broken, COUNT(r2.uid) AS nb_mails,
175 s.email AS alias, DATE_ADD(r1.last, INTERVAL 14 DAY) < CURDATE() as notify
176 FROM email_redirect_account AS r1
177 INNER JOIN accounts AS a ON (a.uid = r1.uid)
178 INNER JOIN email_source_account AS s ON (a.uid = s.uid AND s.flags = \'bestalias\')
179 LEFT JOIN email_redirect_account AS r2 ON (a.uid = r2.uid AND r1.redirect != r2.redirect AND
180 r2.broken_level = 0 AND r2.flags = \'active\' AND
181 (r2.type = \'smtp\' OR r2.type = \'googleapps\'))
182 WHERE r1.redirect = {?}
183 GROUP BY r1.uid', $email);
186 // Mark address as broken.
187 if (!$user['broken']) {
188 XDB
::execute('UPDATE email_redirect_account
189 SET broken_date = NOW(), last = NOW(), broken_level = 1
190 WHERE redirect = {?}', $email);
192 XDB
::execute('UPDATE email_redirect_account
193 SET last = CURDATE(), broken_level = broken_level + 1
194 WHERE redirect = {?} AND DATE_ADD(last, INTERVAL 14 DAY) < CURDATE()',
197 XDB
::execute('UPDATE email_redirect_account
199 WHERE redirect = {?} AND broken_level = 0', $email);
206 // function fix_bestalias() {{{1
207 // Checks for an existing 'bestalias' among the the current user's aliases, and
208 // eventually selects a new bestalias when required.
209 function fix_bestalias(User
$user)
211 // First check if the bestalias is properly set.
212 $alias_count = XDB
::fetchOneCell('SELECT COUNT(*)
213 FROM email_source_account
214 WHERE uid = {?} AND FIND_IN_SET(\'bestalias\', flags) AND expire IS NULL',
217 if ($alias_count > 1) {
218 // If too many bestaliases, delete the bestalias flag from all this
219 // user's emails (this should never happen).
220 XDB
::execute("UPDATE email_source_account
221 SET flags = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', flags, ','), ',bestalias,', ','))
225 if ($alias_count != 1) {
226 // If no bestalias is selected, we choose the shortest email which is not
227 // related to a usage name and contains a '.'.
228 XDB
::execute("UPDATE email_source_account
229 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'bestalias')
230 WHERE uid = {?} AND expire IS NULL
231 ORDER BY NOT FIND_IN_SET('usage', flags), email LIKE '%.%', LENGTH(email)
236 // First check if best_domain is properly set.
237 $domain_count = XDB
::fetchOneCell('SELECT COUNT(*)
239 INNER JOIN email_source_account AS s ON (s.uid = a.uid AND FIND_IN_SET(\'bestalias\', s.flags))
240 INNER JOIN email_virtual_domains AS d ON (d.id = a.best_domain)
241 INNER JOIN email_virtual_domains AS m ON (d.aliasing = m.id)
242 INNER JOIN email_virtual_domains AS v ON (v.aliasing = m.id AND v.id = s.domain)
243 WHERE a.uid = {?} AND (m.name = {?} OR m.name = {?})',
244 $user->id(), $user->mainEmailDomain(), Platal
::globals()->mail
->alias_dom
);
246 if ($domain_count == 0) {
247 XDB
::execute('UPDATE accounts AS a
248 INNER JOIN email_source_account AS s ON (s.uid = a.uid AND FIND_IN_SET(\'bestalias\', s.flags))
249 INNER JOIN email_virtual_domains AS d ON (d.aliasing = s.domain AND (d.name = {?} OR d.name = {?}))
250 SET a.best_domain = d.id
252 $user->mainEmailDomain(), Platal
::globals()->mail
->alias_dom
, $user->id());
258 // function valide_email() {{{1
259 // Returns a cleaned-up version of the @p email string. It removes garbage
260 // characters, and determines the canonical form (without _ and +) for
261 // Polytechnique.org email addresses.
262 function valide_email($str)
266 $em = trim(rtrim($str));
267 $em = str_replace('<', '', $em);
268 $em = str_replace('>', '', $em);
269 if (strpos($em, '@') === false
) {
272 list($ident, $dom) = explode('@', $em);
273 if (User
::isMainMailDomain($dom)) {
274 list($ident1) = explode('_', $ident);
275 list($ident) = explode('+', $ident1);
277 return $ident . '@' . $dom;
280 // function isvalid_email_redirection() {{{1
281 /** Checks if an email is a suitable redirection.
282 * @param $email the email to check
285 function isvalid_email_redirection($email)
287 return isvalid_email($email) && !preg_match("/@polytechnique\.edu$/", $email) && User
::isForeignEmailAddress($email);
290 // function ids_from_mails() {{{1
291 // Converts an array of emails to an array of email => uid, where email is the
292 // given email when we found a matching user.
293 function ids_from_mails(array $emails)
295 // Removes duplicates, if any.
296 $emails = array_unique($emails);
298 // Formats and splits by domain type (locally managed or external) emails.
299 $main_domain_emails = array();
300 $aux_domain_emails = array();
301 $other_emails = array();
302 foreach ($emails as $email) {
303 if (strpos($email, '@') === false
) {
304 $main_domain_emails[] = $email;
306 if (User
::isForeignEmailAddress($email)) {
307 $other_emails[$email] = strtolower($user . '@' . $domain);
309 list($local_part, $domain) = explode('@', $email);
310 list($local_part) = explode('+', $local_part);
311 list($local_part) = explode('_', $local_part);
312 if (User
::isMainMailDomain($domain)) {
313 $main_domain_emails[$email] = strtolower($local_part);
314 } elseif (User
::isAliasMailDomain($domain)) {
315 $aux_domain_emails[$email] = strtolower($local_part);
321 // Retrieves emails from our domains.
322 $main_domain_uids = XDB
::fetchAllAssoc('email',
324 FROM email_source_account
325 WHERE email IN {?} AND type != \'alias_aux\'',
326 array_unique($main_domain_emails));
327 $aux_domain_uids = XDB
::fetchAllAssoc('email',
329 FROM email_source_account
330 WHERE email IN {?} AND type = \'alias_aux\'',
331 array_unique($aux_domain_emails));
333 // Retrieves emails from redirections.
334 $other_uids = XDB
::fetchAllAssoc('redirect',
335 'SELECT redirect, uid
336 FROM email_redirect_account
337 WHERE redirect IN {?}',
338 array_unique($other_emails));
340 // Associates given emails with the corresponding uid.
342 foreach ($main_domain_emails as $email => $key) {
343 $uids[$email] = $main_domain_uids[$key];
345 foreach ($aux_domain_emails as $email => $key) {
346 $uids[$email] = $aux_domain_uids[$key];
348 foreach ($other_emails as $email => $key) {
349 $uids[$email] = $other_uids[$key];
352 return array_unique($uids);
356 // The Bogo class represents a spam filtering level in plat/al architecture.
359 public static $states = array(
363 3 => 'tag_and_drop_spams',
369 public $single_state;
370 public $redirections;
371 public $single_redirection;
373 public function __construct(User
$user)
379 $this->user
= &$user;
380 $res = XDB
::fetchOneAssoc('SELECT COUNT(DISTINCT(action)) AS action_count, COUNT(redirect) AS redirect_count, action
381 FROM email_redirect_account
382 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\') AND flags = \'active\'
385 if ($res['redirect_count'] == 0) {
389 $this->single_redirection
= ($res['redirect_count'] == 1);
390 $this->redirections
= XDB
::fetchAllAssoc('SELECT IF(type = \'googleapps\', type, redirect) AS redirect, type, action
391 FROM email_redirect_account
392 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')
393 ORDER BY type, redirect',
396 foreach ($this->redirections
AS &$redirection) {
397 $redirection['filter'] = array_search($redirection['action'], self
::$states);
399 if ($res['action_count'] == 1) {
400 $this->state
= array_search($res['action'], self
::$states);
401 $this->single_state
= true
;
403 $this->single_state
= $this->state
= false
;
407 public function changeAll($state)
409 Platal
::assert($state >= 0 && $state < count(self
::$states), 'Unknown antispam level.');
411 $this->state
= $state;
412 XDB
::execute('UPDATE email_redirect_account
414 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')',
415 self
::$states[$this->state
], $this->user
->id());
418 public function change($redirection, $state)
420 Platal
::assert($state >= 0 && $state < count(self
::$states), 'Unknown antispam level.');
422 XDB
::execute('UPDATE email_redirect_account
424 WHERE uid = {?} AND (type = {?} OR redirect = {?})',
425 self
::$states[$state], $this->user
->id(), $redirection, $redirection);
430 // Represents an "email address" used as final recipient for plat/al-managed
434 // Lists fields to load automatically.
435 static private $field_names = array('rewrite', 'type', 'action', 'broken_date', 'broken_level', 'last', 'hash', 'allow_rewrite');
437 // Shortname to realname mapping for known mail storage backends.
438 static private $display_names = array(
439 'imap' => 'Accès de secours aux emails (IMAP)',
440 'googleapps' => 'Compte Google Apps',
442 static private $storage_domains = array(
449 // Basic email properties; $sufficient indicates if the email can be used as
450 // an unique redirection; $redirect contains the delivery email address.
454 public $display_email;
457 public $filter_level;
459 // Redirection status properties.
465 public $allow_rewrite;
468 // Redirection bounces stats.
470 public $broken_level;
473 public function __construct(User
$user, array $row)
475 foreach (self
::$field_names as $field) {
476 if (array_key_exists($field, $row)) {
477 $this->$field = $row[$field];
480 $this->email
= $row['redirect'];
482 if (array_key_exists($this->type
, Email
::$display_names)) {
483 $this->display_email
= self
::$display_names[$this->type
];
485 $this->display_email
= $this->email
;
487 foreach (array('active', 'inactive', 'broken', 'disabled') as $status) {
488 $this->$status = ($status == $row['flags']);
490 $this->sufficient
= ($this->type
== 'smtp' ||
$this->type
== 'googleapps');
491 $this->filter_level
= ($this->type
== 'imap') ? null
: array_search($this->action
, Bogo
::$states);
492 $this->user
= &$user;
495 // Activates the email address as a redirection.
496 public function activate()
498 if ($this->inactive
) {
499 XDB
::execute('UPDATE email_redirect_account
500 SET broken_level = IF(flags = \'broken\', broken_level - 1, broken_level), flags = \'active\'
501 WHERE uid = {?} AND redirect = {?}',
502 $this->user
->id(), $this->email
);
503 S
::logger()->log('email_on', $this->email
. ($this->user
->id() != S
::v('uid') ?
"(admin on {$this->user->login()})" : ''));
504 $this->inactive
= false
;
505 $this->active
= true
;
509 // Deactivates the email address as a redirection.
510 public function deactivate()
513 XDB
::execute('UPDATE email_redirect_account
514 SET flags = \'inactive\'
515 WHERE uid = {?} AND redirect = {?}',
516 $this->user
->id(), $this->email
);
517 S
::logger()->log('email_off', $this->email
. ($this->user
->id() != S
::v('uid') ?
"(admin on {$this->user->login()})" : "") );
518 $this->inactive
= true
;
519 $this->active
= false
;
524 // Sets the rewrite rule for the given address.
525 public function set_rewrite($rewrite)
527 if ($this->type
!= 'smtp' ||
$this->rewrite
== $rewrite) {
530 if (!$rewrite ||
!isvalid_email($rewrite)) {
533 XDB
::execute('UPDATE email_redirect_account
535 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
536 $rewrite, $this->user
->id(), $this->email
);
537 $this->rewrite
= $rewrite;
538 if (!$this->allow_rewrite
) {
540 if (empty($this->hash
)) {
541 $this->hash
= rand_url_id();
542 XDB
::execute('UPDATE email_redirect_account
544 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
545 $this->hash
, $this->user
->id(), $this->email
);
547 $mail = new PlMailer('emails/rewrite-in.mail.tpl');
548 $mail->assign('mail', $this);
549 $mail->assign('user', $this->user
);
550 $mail->assign('baseurl', $globals->baseurl
);
551 $mail->assign('sitename', $globals->core
->sitename
);
552 $mail->assign('to', $this->email
);
553 $mail->send($this->user
->isEmailFormatHtml());
558 // Resets the error counts associated with the redirection.
559 public function clean_errors()
561 if ($this->type
!= 'smtp') {
568 $this->broken_level
= 0;
570 return XDB
::execute('UPDATE email_redirect_account
571 SET broken_level = 0, broken_date = 0, last = 0
572 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
573 $this->user
->id(), $this->email
);
577 // Email backend capabilities ('rewrite' refers to From: rewrite for mails
578 // forwarded by Polytechnique.org's MXs; 'removable' indicates if the email
579 // can be definitively removed; 'disable' indicates if the email has a third
580 // status 'disabled' in addition to 'active' and 'inactive').
581 public function has_rewrite()
583 return ($this->type
== 'smtp');
586 public function is_removable()
588 return ($this->type
== 'smtp');
591 public function has_disable()
596 public function is_redirection()
598 return ($this->type
== 'smtp');
601 // Returns the list of allowed storages for the @p user.
602 static private function get_allowed_storages(User
$user)
607 // Google Apps storage is available for users with valid Google Apps account.
608 require_once 'googleapps.inc.php';
609 if ($user->checkPerms('gapps') &&
610 $globals->mailstorage
->googleapps_domain
&&
611 GoogleAppsAccount
::account_status($user->id()) == 'active') {
612 $storages[] = 'googleapps';
615 // IMAP storage is always visible to administrators, and is allowed for
616 // everyone when the service is marked as 'active'.
617 if ($globals->mailstorage
->imap_active || S
::admin()) {
618 $storages[] = 'imap';
624 static public function activate_storage(User
$user, $storage)
626 Platal
::assert(in_array($storage, self
::get_allowed_storages($user)), 'Unknown storage.');
628 if (!self
::is_active_storage($user, $storage)) {
631 XDB
::execute('INSERT INTO email_redirect_account (uid, type, redirect, flags)
632 VALUES ({?}, {?}, {?}, \'active\')',
633 $user->id(), $storage,
634 $user->hruid
. '@' . self
::$storage_domains[$storage] . '.' . $globals->mail
->domain
);
638 static public function deactivate_storage(User
$user, $storage)
640 if (in_array($storage, self
::$storage_domains)) {
641 XDB
::execute('DELETE FROM email_redirect_account
642 WHERE uid = {?} AND type = {?}',
643 $user->id(), $storage);
647 static public function is_active_storage(User
$user, $storage)
649 if (!in_array($storage, self
::$storage_domains)) {
652 $res = XDB
::fetchOneCell('SELECT COUNT(*)
653 FROM email_redirect_account
654 WHERE uid = {?} AND type = {?} AND flags = \'active\'',
655 $user->id(), $storage);
656 return !is_null($res) && $res > 0;
659 // class Redirect {{{1
660 // Redirect is a placeholder class for an user's active redirections (third-party
661 // redirection email, or Polytechnique.org mail storages).
664 private $flags = 'active';
669 public function __construct(User
$user)
671 $this->user
= &$user;
673 // Adds third-party email redirections.
674 $res = XDB
::iterator('SELECT redirect, rewrite, type, action, broken_date, broken_level, last, flags, hash, allow_rewrite
675 FROM email_redirect_account
676 WHERE uid = {?} AND type != \'homonym\'',
678 $this->emails
= array();
679 while ($row = $res->next()) {
680 $this->emails
[] = new Email($user, $row);
684 public function other_active($email)
686 foreach ($this->emails
as $mail) {
687 if ($mail->email
!= $email && $mail->active
&& $mail->sufficient
) {
694 public function delete_email($email)
696 if (!$this->other_active($email)) {
697 return ERROR_INACTIVE_REDIRECTION
;
699 XDB
::execute('DELETE FROM email_redirect_account
700 WHERE uid = {?} AND redirect = {?} AND type != \'homonym\'',
701 $this->user
->id(), $email);
702 S
::logger()->log('email_del', $email . ($this->user
->id() != S
::v('uid') ?
" (admin on {$this->user->login()})" : ""));
703 foreach ($this->emails
as $i => $mail) {
704 if ($email == $mail->email
) {
705 unset($this->emails
[$i]);
708 check_redirect($this);
709 $this->update_imap();
713 public function add_email($email)
715 $email_stripped = strtolower(trim($email));
716 if (!isvalid_email($email_stripped)) {
717 return ERROR_INVALID_EMAIL
;
719 if (!isvalid_email_redirection($email_stripped)) {
720 return ERROR_LOOP_EMAIL
;
722 // We first need to retrieve the value for the antispam filter: it is
723 // either the user's redirections common value, or if they differ, our
725 $bogo = new Bogo($this->user
);
726 $filter = ($bogo->single_state ? Bogo
::$states[$bogo->state
] : Bogo
::$states[0]);
727 // If the email was already present for this user, we reset it to the default values, we thus use REPLACE INTO.
728 XDB
::execute('REPLACE INTO email_redirect_account (uid, redirect, flags, action)
729 VALUES ({?}, {?}, \'active\', {?})',
730 $this->user
->id(), $email, $filter);
731 if ($logger = S
::v('log', null
)) { // may be absent --> step4.php
732 S
::logger()->log('email_add', $email . ($this->user
->id() != S
::v('uid') ?
" (admin on {$this->user->login()})" : ""));
734 foreach ($this->emails
as $mail) {
735 if ($mail->email
== $email_stripped) {
739 $this->emails
[] = new Email($this->user
, array(
740 'redirect' => $email,
744 'broken_date' => '0000-00-00',
746 'last' => '0000-00-00',
753 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->user
->login());
754 check_redirect($this);
755 $this->update_imap();
759 public function modify_email($emails_actifs, $emails_rewrite)
761 foreach ($this->emails
as &$email) {
762 if (in_array($email->email
, $emails_actifs)) {
765 $email->deactivate();
767 $email->set_rewrite($emails_rewrite[$email->email
]);
769 check_redirect($this);
770 $this->update_imap();
774 public function modify_one_email($email, $activate)
778 foreach ($this->emails
as $i=>$mail) {
779 if ($mail->email
== $email) {
782 $allinactive &= !$mail->active ||
!$mail->sufficient ||
$mail->email
== $email;
784 if ($thisone === false
) {
785 return ERROR_INVALID_EMAIL
;
787 if ($allinactive ||
$activate) {
788 $this->emails
[$thisone]->activate();
790 $this->emails
[$thisone]->deactivate();
792 check_redirect($this);
793 $this->update_imap();
794 if ($allinactive && !$activate) {
795 return ERROR_INACTIVE_REDIRECTION
;
800 public function modify_one_email_redirect($email, $redirect)
802 foreach ($this->emails
as &$mail) {
803 if ($mail->email
== $email) {
804 $mail->set_rewrite($redirect);
805 check_redirect($this);
806 $this->update_imap();
812 public function clean_errors($email)
814 foreach ($this->emails
as &$mail) {
815 if ($mail->email
== $email) {
816 check_redirect($this);
817 $this->update_imap();
818 return $mail->clean_errors();
824 public function disable()
826 XDB
::execute("UPDATE email_redirect_account
827 SET flags = 'disable'
828 WHERE flags = 'active' AND uid = {?}", $this->user
->id());
829 foreach ($this->emails
as &$mail) {
830 if ($mail->active
&& $mail->has_disable()) {
831 $mail->disabled
= true
;
832 $mail->active
= false
;
835 check_redirect($this);
836 $this->update_imap();
839 public function enable()
841 XDB
::execute("UPDATE email_redirect_account
843 WHERE flags = 'disable' AND uid = {?}", $this->user
->id());
844 foreach ($this->emails
as &$mail) {
845 if ($mail->disabled
) {
846 $mail->disabled
= false
;
847 $mail->active
= true
;
849 check_redirect($this);
851 $this->update_imap();
854 public function get_broken_mx()
856 $res = XDB
::query("SELECT host, text
858 WHERE state != 'ok'");
859 if (!$res->numRows()) {
862 $mxs = $res->fetchAllAssoc();
864 foreach ($this->emails
as &$mail) {
865 if ($mail->active
&& strstr($mail->email
, '@') !== false
) {
866 list(,$domain) = explode('@', $mail->email
);
867 getmxrr($domain, $lcl_mxs);
868 if (empty($lcl_mxs)) {
869 $lcl_mxs = array($domain);
872 foreach ($mxs as &$mx) {
873 foreach ($lcl_mxs as $lcl) {
874 if (fnmatch($mx['host'], $lcl)) {
875 $broken = $mx['text'];
880 $mails[] = array('mail' => $mail->email
, 'text' => $broken);
889 public function active_emails()
892 foreach ($this->emails
as $mail) {
900 public function get_uid()
902 return $this->user
->id();
905 private function update_imap()
907 // Imaps must bounce if and only if the user has no active redirection.
908 if (!$this->other_active('')) {
909 XDB
::execute('UPDATE email_redirect_account
910 SET action = \'imap_and_bounce\'
911 WHERE type = \'imap\' AND uid = {?}',
914 XDB
::execute('UPDATE email_redirect_account
915 SET action = \'let_spams\'
916 WHERE type = \'imap\' AND uid = {?}',
922 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: