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
, 'User should not be 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
, 'User should not be 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
, 'User should not be 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\'
354 if ($res['redirect_count'] == 0) {
358 $this->single_redirection
= ($res['redirect_count'] == 1);
359 $this->redirections
= XDB
::fetchAllAssoc('SELECT IF(type = \'googleapps\', type, redirect) AS redirect, type, action
360 FROM email_redirect_account
361 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')
362 ORDER BY type, redirect',
365 foreach ($this->redirections
AS &$redirection) {
366 $redirection['filter'] = array_search($redirection['action'], self
::$states);
368 if ($res['action_count'] == 1) {
369 $this->state
= array_search($res['action'], self
::$states);
370 $this->single_state
= true
;
372 $this->single_state
= $this->state
= false
;
376 public function changeAll($state)
378 Platal
::assert($state >= 0 && $state < count(self
::$states), 'Unknown antispam level.');
380 $this->state
= $state;
381 XDB
::execute('UPDATE email_redirect_account
383 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')',
384 self
::$states[$this->state
], $this->user
->id());
387 public function change($redirection, $state)
389 Platal
::assert($state >= 0 && $state < count(self
::$states), 'Unknown antispam level.');
391 XDB
::execute('UPDATE email_redirect_account
393 WHERE uid = {?} AND (type = {?} OR redirect = {?})',
394 self
::$states[$state], $this->user
->id(), $redirection, $redirection);
399 // Represents an "email address" used as final recipient for plat/al-managed
403 // Lists fields to load automatically.
404 static private $field_names = array('rewrite', 'type', 'action', 'broken_date', 'broken_level', 'last', 'hash', 'allow_rewrite');
406 // Shortname to realname mapping for known mail storage backends.
407 static private $display_names = array(
408 'imap' => 'Accès de secours aux emails (IMAP)',
409 'googleapps' => 'Compte Google Apps',
411 static private $storage_domains = array(
418 // Basic email properties; $sufficient indicates if the email can be used as
419 // an unique redirection; $redirect contains the delivery email address.
423 public $display_email;
426 public $filter_level;
428 // Redirection status properties.
434 public $allow_rewrite;
437 // Redirection bounces stats.
439 public $broken_level;
442 public function __construct(User
$user, array $row)
444 foreach (self
::$field_names as $field) {
445 if (array_key_exists($field, $row)) {
446 $this->$field = $row[$field];
449 $this->email
= $row['redirect'];
451 if (array_key_exists($this->type
, Email
::$display_names)) {
452 $this->display_email
= self
::$display_names[$this->type
];
454 $this->display_email
= $this->email
;
456 foreach (array('active', 'inactive', 'broken', 'disabled') as $status) {
457 $this->$status = ($status == $row['flags']);
459 $this->sufficient
= ($this->type
== 'smtp' ||
$this->type
== 'googleapps');
460 $this->filter_level
= ($this->type
== 'imap') ? null
: array_search($this->action
, Bogo
::$states);
461 $this->user
= &$user;
464 // Activates the email address as a redirection.
465 public function activate()
467 if ($this->inactive
) {
468 XDB
::execute('UPDATE email_redirect_account
469 SET broken_level = IF(flags = \'broken\', broken_level - 1, broken_level), flags = \'active\'
470 WHERE uid = {?} AND redirect = {?}',
471 $this->user
->id(), $this->email
);
472 S
::logger()->log('email_on', $this->email
. ($this->user
->id() != S
::v('uid') ?
"(admin on {$this->user->login()})" : ''));
473 $this->inactive
= false
;
474 $this->active
= true
;
478 // Deactivates the email address as a redirection.
479 public function deactivate()
482 XDB
::execute('UPDATE email_redirect_account
483 SET flags = \'inactive\'
484 WHERE uid = {?} AND redirect = {?}',
485 $this->user
->id(), $this->email
);
486 S
::logger()->log('email_off', $this->email
. ($this->user
->id() != S
::v('uid') ?
"(admin on {$this->user->login()})" : "") );
487 $this->inactive
= true
;
488 $this->active
= false
;
493 // Sets the rewrite rule for the given address.
494 public function set_rewrite($rewrite)
496 if ($this->type
!= 'smtp' ||
$this->rewrite
== $rewrite) {
499 if (!$rewrite ||
!isvalid_email($rewrite)) {
502 XDB
::execute('UPDATE email_redirect_account
504 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
505 $rewrite, $this->user
->id(), $this->email
);
506 $this->rewrite
= $rewrite;
507 if (!$this->allow_rewrite
) {
509 if (empty($this->hash
)) {
510 $this->hash
= rand_url_id();
511 XDB
::execute('UPDATE email_redirect_account
513 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
514 $this->hash
, $this->user
->id(), $this->email
);
516 $mail = new PlMailer('emails/rewrite-in.mail.tpl');
517 $mail->assign('mail', $this);
518 $mail->assign('user', $this->user
);
519 $mail->assign('baseurl', $globals->baseurl
);
520 $mail->assign('sitename', $globals->core
->sitename
);
521 $mail->assign('to', $this->email
);
522 $mail->send($this->user
->isEmailFormatHtml());
527 // Resets the error counts associated with the redirection.
528 public function clean_errors()
530 if ($this->type
!= 'smtp') {
537 $this->broken_level
= 0;
539 return XDB
::execute('UPDATE email_redirect_account
540 SET broken_level = 0, broken_date = 0, last = 0
541 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
542 $this->user
->id(), $this->email
);
546 // Email backend capabilities ('rewrite' refers to From: rewrite for mails
547 // forwarded by Polytechnique.org's MXs; 'removable' indicates if the email
548 // can be definitively removed; 'disable' indicates if the email has a third
549 // status 'disabled' in addition to 'active' and 'inactive').
550 public function has_rewrite()
552 return ($this->type
== 'smtp');
555 public function is_removable()
557 return ($this->type
== 'smtp');
560 public function has_disable()
565 public function is_redirection()
567 return ($this->type
== 'smtp');
570 // Returns the list of allowed storages for the @p user.
571 static private function get_allowed_storages(User
$user)
576 // Google Apps storage is available for users with valid Google Apps account.
577 require_once 'googleapps.inc.php';
578 if ($globals->mailstorage
->googleapps_domain
&&
579 GoogleAppsAccount
::account_status($user->id()) == 'active') {
580 $storages[] = 'googleapps';
583 // IMAP storage is always visible to administrators, and is allowed for
584 // everyone when the service is marked as 'active'.
585 if ($globals->mailstorage
->imap_active || S
::admin()) {
586 $storages[] = 'imap';
592 static public function activate_storage(User
$user, $storage)
594 Platal
::assert(in_array($storage, self
::get_allowed_storages($user)), 'Unknown storage.');
596 if (!self
::is_active_storage($user, $storage)) {
599 XDB
::execute('INSERT INTO email_redirect_account (uid, type, redirect, flags)
600 VALUES ({?}, {?}, {?}, \'active\')',
601 $user->id(), $storage,
602 $user->hruid
. '@' . self
::$storage_domains[$storage] . '.' . $globals->mail
->domain
);
606 static public function deactivate_storage(User
$user, $storage)
608 if (in_array($storage, self
::$storage_domains)) {
609 XDB
::execute('DELETE FROM email_redirect_account
610 WHERE uid = {?} AND type = {?}',
611 $user->id(), $storage);
615 static public function is_active_storage(User
$user, $storage)
617 if (!in_array($storage, self
::$storage_domains)) {
620 $res = XDB
::fetchOneCell('SELECT COUNT(*)
621 FROM email_redirect_account
622 WHERE uid = {?} AND type = {?} AND flags = \'active\'',
623 $user->id(), $storage);
624 return !is_null($res) && $res > 0;
627 // class Redirect {{{1
628 // Redirect is a placeholder class for an user's active redirections (third-party
629 // redirection email, or Polytechnique.org mail storages).
632 private $flags = 'active';
637 public function __construct(User
$user)
639 $this->user
= &$user;
641 // Adds third-party email redirections.
642 $res = XDB
::iterator('SELECT redirect, rewrite, type, action, broken_date, broken_level, last, flags, hash, allow_rewrite
643 FROM email_redirect_account
644 WHERE uid = {?} AND type != \'homonym\'',
646 $this->emails
= array();
647 while ($row = $res->next()) {
648 $this->emails
[] = new Email($user, $row);
652 public function other_active($email)
654 foreach ($this->emails
as $mail) {
655 if ($mail->email
!= $email && $mail->active
&& $mail->sufficient
) {
662 public function delete_email($email)
664 if (!$this->other_active($email)) {
665 return ERROR_INACTIVE_REDIRECTION
;
667 XDB
::execute('DELETE FROM email_redirect_account
668 WHERE uid = {?} AND redirect = {?} AND type != \'homonym\'',
669 $this->user
->id(), $email);
670 S
::logger()->log('email_del', $email . ($this->user
->id() != S
::v('uid') ?
" (admin on {$this->user->login()})" : ""));
671 foreach ($this->emails
as $i => $mail) {
672 if ($email == $mail->email
) {
673 unset($this->emails
[$i]);
676 check_redirect($this);
677 $this->update_imap();
681 public function add_email($email)
683 $email_stripped = strtolower(trim($email));
684 if (!isvalid_email($email_stripped)) {
685 return ERROR_INVALID_EMAIL
;
687 if (!isvalid_email_redirection($email_stripped)) {
688 return ERROR_LOOP_EMAIL
;
690 // We first need to retrieve the value for the antispam filter: it is
691 // either the user's redirections common value, or if they differ, our
693 $bogo = new Bogo($this->user
);
694 $filter = ($bogo->single_state ? Bogo
::$states[$bogo->state
] : Bogo
::$states[0]);
695 // If the email was already present for this user, we reset it to the default values, we thus use REPLACE INTO.
696 XDB
::execute('REPLACE INTO email_redirect_account (uid, redirect, flags, action)
697 VALUES ({?}, {?}, \'active\', {?})',
698 $this->user
->id(), $email, $filter);
699 if ($logger = S
::v('log', null
)) { // may be absent --> step4.php
700 S
::logger()->log('email_add', $email . ($this->user
->id() != S
::v('uid') ?
" (admin on {$this->user->login()})" : ""));
702 foreach ($this->emails
as $mail) {
703 if ($mail->email
== $email_stripped) {
707 $this->emails
[] = new Email($this->user
, array(
708 'redirect' => $email,
712 'broken_date' => '0000-00-00',
714 'last' => '0000-00-00',
721 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->user
->login());
722 check_redirect($this);
723 $this->update_imap();
727 public function modify_email($emails_actifs, $emails_rewrite)
729 foreach ($this->emails
as &$email) {
730 if (in_array($email->email
, $emails_actifs)) {
733 $email->deactivate();
735 $email->set_rewrite($emails_rewrite[$email->email
]);
737 check_redirect($this);
738 $this->update_imap();
742 public function modify_one_email($email, $activate)
746 foreach ($this->emails
as $i=>$mail) {
747 if ($mail->email
== $email) {
750 $allinactive &= !$mail->active ||
!$mail->sufficient ||
$mail->email
== $email;
752 if ($thisone === false
) {
753 return ERROR_INVALID_EMAIL
;
755 if ($allinactive ||
$activate) {
756 $this->emails
[$thisone]->activate();
758 $this->emails
[$thisone]->deactivate();
760 check_redirect($this);
761 $this->update_imap();
762 if ($allinactive && !$activate) {
763 return ERROR_INACTIVE_REDIRECTION
;
768 public function modify_one_email_redirect($email, $redirect)
770 foreach ($this->emails
as &$mail) {
771 if ($mail->email
== $email) {
772 $mail->set_rewrite($redirect);
773 check_redirect($this);
774 $this->update_imap();
780 public function clean_errors($email)
782 foreach ($this->emails
as &$mail) {
783 if ($mail->email
== $email) {
784 check_redirect($this);
785 $this->update_imap();
786 return $mail->clean_errors();
792 public function disable()
794 XDB
::execute("UPDATE email_redirect_account
795 SET flags = 'disable'
796 WHERE flags = 'active' AND uid = {?}", $this->user
->id());
797 foreach ($this->emails
as &$mail) {
798 if ($mail->active
&& $mail->has_disable()) {
799 $mail->disabled
= true
;
800 $mail->active
= false
;
803 check_redirect($this);
804 $this->update_imap();
807 public function enable()
809 XDB
::execute("UPDATE email_redirect_account
811 WHERE flags = 'disable' AND uid = {?}", $this->user
->id());
812 foreach ($this->emails
as &$mail) {
813 if ($mail->disabled
) {
814 $mail->disabled
= false
;
815 $mail->active
= true
;
817 check_redirect($this);
819 $this->update_imap();
822 public function get_broken_mx()
824 $res = XDB
::query("SELECT host, text
826 WHERE state != 'ok'");
827 if (!$res->numRows()) {
830 $mxs = $res->fetchAllAssoc();
832 foreach ($this->emails
as &$mail) {
833 if ($mail->active
&& strstr($mail->email
, '@') !== false
) {
834 list(,$domain) = explode('@', $mail->email
);
835 getmxrr($domain, $lcl_mxs);
836 if (empty($lcl_mxs)) {
837 $lcl_mxs = array($domain);
840 foreach ($mxs as &$mx) {
841 foreach ($lcl_mxs as $lcl) {
842 if (fnmatch($mx['host'], $lcl)) {
843 $broken = $mx['text'];
848 $mails[] = array('mail' => $mail->email
, 'text' => $broken);
857 public function active_emails()
860 foreach ($this->emails
as $mail) {
868 public function get_uid()
870 return $this->user
->id();
873 private function update_imap()
875 // Imaps must bounce if and only if the user has no active redirection.
876 if (!$this->other_active('')) {
877 XDB
::execute('UPDATE email_redirect_account
878 SET action = \'imap_and_bounce\'
879 WHERE type = \'imap\' AND uid = {?}',
882 XDB
::execute('UPDATE email_redirect_account
883 SET action = \'let_spams\'
884 WHERE type = \'imap\' AND uid = {?}',
890 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: