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);
72 foreach ($emails as $email) {
73 if ($user = User
::getSilent($email)) {
82 'nonusers' => $nonusers
86 function delete_list_alias($local_part, $domain)
88 XDB
::execute('DELETE v
89 FROM email_virtual AS v
90 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
91 INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id)
92 WHERE v.email = {?} AND d.name = {?} AND type = \'alias\'',
93 $local_part, $domain);
96 function iterate_list_alias($domain)
98 return XDB
::fetchColumn('SELECT CONCAT(v.email, \'@\', m.name)
99 FROM email_virtual AS v
100 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
101 WHERE m.name = {?} AND v.type = \'alias\'
106 function create_list($local_part, $domain)
110 $redirect = $domain . '_' . $local_part . '+';
111 foreach(array('post', 'owner', 'admin', 'bounces', 'unsubscribe') as $suffix) {
112 XDB
::execute('INSERT IGNORE INTO email_virtual (email, domain, redirect, type)
113 SELECT {?}, id, {?}, \'list\'
114 FROM email_virtual_domains
116 ($suffix == 'post') ?
$local_part : $local_part . '-' . $suffix,
117 $redirect . $suffix . '@' . $globals->lists
->redirect_domain
, $domain);
121 function delete_list($local_part, $domain)
125 $redirect = $domain . '_' . $local_part . '+';
126 foreach(array('post', 'owner', 'admin', 'bounces', 'unsubscribe') as $suffix) {
127 XDB
::execute('DELETE email_virtual
128 WHERE redirect = {?} AND type = \'list\'',
129 $redirect . $suffix . '@' . $globals->lists
->redirect_domain
);
133 function list_exist($local_part, $domain)
135 return XDB
::fetchOneCell('SELECT COUNT(*)
136 FROM email_virtual AS v
137 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
138 INNER JOIN email_virtual_domains AS d ON (m.id = d.aliasing)
139 WHERE v.email = {?} AND d.name = {?}',
140 $local_part, $domain);
143 // function mark_broken_email() {{{1
144 function mark_broken_email($email, $admin = false
)
146 $email = valide_email($email);
147 if (empty($email) ||
$email == '@') {
151 $user = XDB
::fetchOneAssoc('SELECT r1.uid, r1.broken_level != 0 AS broken, COUNT(r2.uid) AS nb_mails,
152 s.email AS alias, DATE_ADD(r1.last, INTERVAL 14 DAY) < CURDATE() as notify
153 FROM email_redirect_account AS r1
154 INNER JOIN accounts AS a ON (a.uid = r1.uid)
155 INNER JOIN email_source_account AS s ON (a.uid = s.uid AND s.flags = \'bestalias\')
156 LEFT JOIN email_redirect_account AS r2 ON (a.uid = r2.uid AND r1.redirect != r2.redirect AND
157 r2.broken_level = 0 AND r2.flags = \'active\' AND
158 (r2.type = \'smtp\' OR r2.type = \'googleapps\'))
159 WHERE r1.redirect = {?}
160 GROUP BY r1.uid', $email);
163 // Mark address as broken.
164 if (!$user['broken']) {
165 XDB
::execute('UPDATE email_redirect_account
166 SET broken_date = NOW(), last = NOW(), broken_level = 1
167 WHERE redirect = {?}', $email);
169 XDB
::execute('UPDATE email_redirect_account
170 SET last = CURDATE(), broken_level = broken_level + 1
171 WHERE redirect = {?} AND DATE_ADD(last, INTERVAL 14 DAY) < CURDATE()',
174 XDB
::execute('UPDATE email_redirect_account
176 WHERE redirect = {?} AND broken_level = 0', $email);
183 // function fix_bestalias() {{{1
184 // Checks for an existing 'bestalias' among the the current user's aliases, and
185 // eventually selects a new bestalias when required.
186 function fix_bestalias(User
$user)
188 // First check if the bestalias is properly set.
189 $alias_count = XDB
::fetchOneCell('SELECT COUNT(*)
190 FROM email_source_account
191 WHERE uid = {?} AND FIND_IN_SET(\'bestalias\', flags) AND expire IS NULL',
194 if ($alias_count > 1) {
195 // If too many bestaliases, delete the bestalias flag from all this
196 // user's emails (this should never happen).
197 XDB
::execute("UPDATE email_source_account
198 SET flags = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', flags, ','), ',bestalias,', ','))
202 if ($alias_count != 1) {
203 // If no bestalias is selected, we choose the shortest email which is not
204 // related to a usage name and contains a '.'.
205 XDB
::execute("UPDATE email_source_account
206 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'bestalias')
207 WHERE uid = {?} AND expire IS NULL
208 ORDER BY NOT FIND_IN_SET('usage', flags), email LIKE '%.%', LENGTH(email)
213 // First check if best_domain is properly set.
214 $domain_count = XDB
::fetchOneCell('SELECT COUNT(*)
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.id = a.best_domain)
218 INNER JOIN email_virtual_domains AS m ON (d.aliasing = m.id)
219 INNER JOIN email_virtual_domains AS v ON (v.aliasing = m.id AND v.id = s.domain)
220 WHERE a.uid = {?} AND (m.name = {?} OR m.name = {?})',
221 $user->id(), $user->mainEmailDomain(), Platal
::globals()->mail
->alias_dom
);
223 if ($domain_count == 0) {
224 XDB
::execute('UPDATE accounts AS a
225 INNER JOIN email_source_account AS s ON (s.uid = a.uid AND FIND_IN_SET(\'bestalias\', s.flags))
226 INNER JOIN email_virtual_domains AS d ON (d.aliasing = s.domain AND (d.name = {?} OR d.name = {?}))
227 SET a.best_domain = d.id
229 $user->mainEmailDomain(), Platal
::globals()->mail
->alias_dom
, $user->id());
235 // function valide_email() {{{1
236 // Returns a cleaned-up version of the @p email string. It removes garbage
237 // characters, and determines the canonical form (without _ and +) for
238 // Polytechnique.org email addresses.
239 function valide_email($str)
243 $em = trim(rtrim($str));
244 $em = str_replace('<', '', $em);
245 $em = str_replace('>', '', $em);
246 if (strpos($em, '@') === false
) {
249 list($ident, $dom) = explode('@', $em);
250 if (User
::isMainMailDomain($dom)) {
251 list($ident1) = explode('_', $ident);
252 list($ident) = explode('+', $ident1);
254 return $ident . '@' . $dom;
257 // function isvalid_email_redirection() {{{1
258 /** Checks if an email is a suitable redirection.
259 * @param $email the email to check
262 function isvalid_email_redirection($email)
264 return isvalid_email($email) && !preg_match("/@polytechnique\.edu$/", $email) && User
::isForeignEmailAddress($email);
267 // function ids_from_mails() {{{1
268 // Converts an array of emails to an array of email => uid, where email is the
269 // given email when we found a matching user.
270 function ids_from_mails(array $emails)
272 // Removes duplicates, if any.
273 $emails = array_unique($emails);
275 // Formats and splits by domain type (locally managed or external) emails.
276 $main_domain_emails = array();
277 $aux_domain_emails = array();
278 $other_emails = array();
279 foreach ($emails as $email) {
280 if (strpos($email, '@') === false
) {
281 $main_domain_emails[] = $email;
283 if (User
::isForeignEmailAddress($email)) {
284 $other_emails[$email] = strtolower($user . '@' . $domain);
286 list($local_part, $domain) = explode('@', $email);
287 list($local_part) = explode('+', $local_part);
288 list($local_part) = explode('_', $local_part);
289 if (User
::isMainMailDomain($domain)) {
290 $main_domain_emails[$email] = strtolower($local_part);
291 } elseif (User
::isAliasMailDomain($domain)) {
292 $aux_domain_emails[$email] = strtolower($local_part);
298 // Retrieves emails from our domains.
299 $main_domain_uids = XDB
::fetchAllAssoc('email',
301 FROM email_source_account
302 WHERE email IN {?} AND type != \'alias_aux\'',
303 array_unique($main_domain_emails));
304 $aux_domain_uids = XDB
::fetchAllAssoc('email',
306 FROM email_source_account
307 WHERE email IN {?} AND type = \'alias_aux\'',
308 array_unique($aux_domain_emails));
310 // Retrieves emails from redirections.
311 $other_uids = XDB
::fetchAllAssoc('redirect',
312 'SELECT redirect, uid
313 FROM email_redirect_account
314 WHERE redirect IN {?}',
315 array_unique($other_emails));
317 // Associates given emails with the corresponding uid.
319 foreach ($main_domain_emails as $email => $key) {
320 $uids[$email] = $main_domain_uids[$key];
322 foreach ($aux_domain_emails as $email => $key) {
323 $uids[$email] = $aux_domain_uids[$key];
325 foreach ($other_emails as $email => $key) {
326 $uids[$email] = $other_uids[$key];
329 return array_unique($uids);
333 // The Bogo class represents a spam filtering level in plat/al architecture.
336 public static $states = array(
340 3 => 'tag_and_drop_spams',
346 public $single_state;
347 public $redirections;
348 public $single_redirection;
350 public function __construct(User
$user)
356 $this->user
= &$user;
357 $res = XDB
::fetchOneAssoc('SELECT COUNT(DISTINCT(action)) AS action_count, COUNT(redirect) AS redirect_count, action
358 FROM email_redirect_account
359 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\') AND flags = \'active\'
362 if ($res['redirect_count'] == 0) {
366 $this->single_redirection
= ($res['redirect_count'] == 1);
367 $this->redirections
= XDB
::fetchAllAssoc('SELECT IF(type = \'googleapps\', type, redirect) AS redirect, type, action
368 FROM email_redirect_account
369 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')
370 ORDER BY type, redirect',
373 foreach ($this->redirections
AS &$redirection) {
374 $redirection['filter'] = array_search($redirection['action'], self
::$states);
376 if ($res['action_count'] == 1) {
377 $this->state
= array_search($res['action'], self
::$states);
378 $this->single_state
= true
;
380 $this->single_state
= $this->state
= false
;
384 public function changeAll($state)
386 Platal
::assert($state >= 0 && $state < count(self
::$states), 'Unknown antispam level.');
388 $this->state
= $state;
389 XDB
::execute('UPDATE email_redirect_account
391 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')',
392 self
::$states[$this->state
], $this->user
->id());
395 public function change($redirection, $state)
397 Platal
::assert($state >= 0 && $state < count(self
::$states), 'Unknown antispam level.');
399 XDB
::execute('UPDATE email_redirect_account
401 WHERE uid = {?} AND (type = {?} OR redirect = {?})',
402 self
::$states[$state], $this->user
->id(), $redirection, $redirection);
407 // Represents an "email address" used as final recipient for plat/al-managed
411 // Lists fields to load automatically.
412 static private $field_names = array('rewrite', 'type', 'action', 'broken_date', 'broken_level', 'last', 'hash', 'allow_rewrite');
414 // Shortname to realname mapping for known mail storage backends.
415 static private $display_names = array(
416 'imap' => 'Accès de secours aux emails (IMAP)',
417 'googleapps' => 'Compte Google Apps',
419 static private $storage_domains = array(
426 // Basic email properties; $sufficient indicates if the email can be used as
427 // an unique redirection; $redirect contains the delivery email address.
431 public $display_email;
434 public $filter_level;
436 // Redirection status properties.
442 public $allow_rewrite;
445 // Redirection bounces stats.
447 public $broken_level;
450 public function __construct(User
$user, array $row)
452 foreach (self
::$field_names as $field) {
453 if (array_key_exists($field, $row)) {
454 $this->$field = $row[$field];
457 $this->email
= $row['redirect'];
459 if (array_key_exists($this->type
, Email
::$display_names)) {
460 $this->display_email
= self
::$display_names[$this->type
];
462 $this->display_email
= $this->email
;
464 foreach (array('active', 'inactive', 'broken', 'disabled') as $status) {
465 $this->$status = ($status == $row['flags']);
467 $this->sufficient
= ($this->type
== 'smtp' ||
$this->type
== 'googleapps');
468 $this->filter_level
= ($this->type
== 'imap') ? null
: array_search($this->action
, Bogo
::$states);
469 $this->user
= &$user;
472 // Activates the email address as a redirection.
473 public function activate()
475 if ($this->inactive
) {
476 XDB
::execute('UPDATE email_redirect_account
477 SET broken_level = IF(flags = \'broken\', broken_level - 1, broken_level), flags = \'active\'
478 WHERE uid = {?} AND redirect = {?}',
479 $this->user
->id(), $this->email
);
480 S
::logger()->log('email_on', $this->email
. ($this->user
->id() != S
::v('uid') ?
"(admin on {$this->user->login()})" : ''));
481 $this->inactive
= false
;
482 $this->active
= true
;
486 // Deactivates the email address as a redirection.
487 public function deactivate()
490 XDB
::execute('UPDATE email_redirect_account
491 SET flags = \'inactive\'
492 WHERE uid = {?} AND redirect = {?}',
493 $this->user
->id(), $this->email
);
494 S
::logger()->log('email_off', $this->email
. ($this->user
->id() != S
::v('uid') ?
"(admin on {$this->user->login()})" : "") );
495 $this->inactive
= true
;
496 $this->active
= false
;
501 // Sets the rewrite rule for the given address.
502 public function set_rewrite($rewrite)
504 if ($this->type
!= 'smtp' ||
$this->rewrite
== $rewrite) {
507 if (!$rewrite ||
!isvalid_email($rewrite)) {
510 XDB
::execute('UPDATE email_redirect_account
512 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
513 $rewrite, $this->user
->id(), $this->email
);
514 $this->rewrite
= $rewrite;
515 if (!$this->allow_rewrite
) {
517 if (empty($this->hash
)) {
518 $this->hash
= rand_url_id();
519 XDB
::execute('UPDATE email_redirect_account
521 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
522 $this->hash
, $this->user
->id(), $this->email
);
524 $mail = new PlMailer('emails/rewrite-in.mail.tpl');
525 $mail->assign('mail', $this);
526 $mail->assign('user', $this->user
);
527 $mail->assign('baseurl', $globals->baseurl
);
528 $mail->assign('sitename', $globals->core
->sitename
);
529 $mail->assign('to', $this->email
);
530 $mail->send($this->user
->isEmailFormatHtml());
535 // Resets the error counts associated with the redirection.
536 public function clean_errors()
538 if ($this->type
!= 'smtp') {
545 $this->broken_level
= 0;
547 return XDB
::execute('UPDATE email_redirect_account
548 SET broken_level = 0, broken_date = 0, last = 0
549 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
550 $this->user
->id(), $this->email
);
554 // Email backend capabilities ('rewrite' refers to From: rewrite for mails
555 // forwarded by Polytechnique.org's MXs; 'removable' indicates if the email
556 // can be definitively removed; 'disable' indicates if the email has a third
557 // status 'disabled' in addition to 'active' and 'inactive').
558 public function has_rewrite()
560 return ($this->type
== 'smtp');
563 public function is_removable()
565 return ($this->type
== 'smtp');
568 public function has_disable()
573 public function is_redirection()
575 return ($this->type
== 'smtp');
578 // Returns the list of allowed storages for the @p user.
579 static private function get_allowed_storages(User
$user)
584 // Google Apps storage is available for users with valid Google Apps account.
585 require_once 'googleapps.inc.php';
586 if ($globals->mailstorage
->googleapps_domain
&&
587 GoogleAppsAccount
::account_status($user->id()) == 'active') {
588 $storages[] = 'googleapps';
591 // IMAP storage is always visible to administrators, and is allowed for
592 // everyone when the service is marked as 'active'.
593 if ($globals->mailstorage
->imap_active || S
::admin()) {
594 $storages[] = 'imap';
600 static public function activate_storage(User
$user, $storage)
602 Platal
::assert(in_array($storage, self
::get_allowed_storages($user)), 'Unknown storage.');
604 if (!self
::is_active_storage($user, $storage)) {
607 XDB
::execute('INSERT INTO email_redirect_account (uid, type, redirect, flags)
608 VALUES ({?}, {?}, {?}, \'active\')',
609 $user->id(), $storage,
610 $user->hruid
. '@' . self
::$storage_domains[$storage] . '.' . $globals->mail
->domain
);
614 static public function deactivate_storage(User
$user, $storage)
616 if (in_array($storage, self
::$storage_domains)) {
617 XDB
::execute('DELETE FROM email_redirect_account
618 WHERE uid = {?} AND type = {?}',
619 $user->id(), $storage);
623 static public function is_active_storage(User
$user, $storage)
625 if (!in_array($storage, self
::$storage_domains)) {
628 $res = XDB
::fetchOneCell('SELECT COUNT(*)
629 FROM email_redirect_account
630 WHERE uid = {?} AND type = {?} AND flags = \'active\'',
631 $user->id(), $storage);
632 return !is_null($res) && $res > 0;
635 // class Redirect {{{1
636 // Redirect is a placeholder class for an user's active redirections (third-party
637 // redirection email, or Polytechnique.org mail storages).
640 private $flags = 'active';
645 public function __construct(User
$user)
647 $this->user
= &$user;
649 // Adds third-party email redirections.
650 $res = XDB
::iterator('SELECT redirect, rewrite, type, action, broken_date, broken_level, last, flags, hash, allow_rewrite
651 FROM email_redirect_account
652 WHERE uid = {?} AND type != \'homonym\'',
654 $this->emails
= array();
655 while ($row = $res->next()) {
656 $this->emails
[] = new Email($user, $row);
660 public function other_active($email)
662 foreach ($this->emails
as $mail) {
663 if ($mail->email
!= $email && $mail->active
&& $mail->sufficient
) {
670 public function delete_email($email)
672 if (!$this->other_active($email)) {
673 return ERROR_INACTIVE_REDIRECTION
;
675 XDB
::execute('DELETE FROM email_redirect_account
676 WHERE uid = {?} AND redirect = {?} AND type != \'homonym\'',
677 $this->user
->id(), $email);
678 S
::logger()->log('email_del', $email . ($this->user
->id() != S
::v('uid') ?
" (admin on {$this->user->login()})" : ""));
679 foreach ($this->emails
as $i => $mail) {
680 if ($email == $mail->email
) {
681 unset($this->emails
[$i]);
684 check_redirect($this);
685 $this->update_imap();
689 public function add_email($email)
691 $email_stripped = strtolower(trim($email));
692 if (!isvalid_email($email_stripped)) {
693 return ERROR_INVALID_EMAIL
;
695 if (!isvalid_email_redirection($email_stripped)) {
696 return ERROR_LOOP_EMAIL
;
698 // We first need to retrieve the value for the antispam filter: it is
699 // either the user's redirections common value, or if they differ, our
701 $bogo = new Bogo($this->user
);
702 $filter = ($bogo->single_state ? Bogo
::$states[$bogo->state
] : Bogo
::$states[0]);
703 // If the email was already present for this user, we reset it to the default values, we thus use REPLACE INTO.
704 XDB
::execute('REPLACE INTO email_redirect_account (uid, redirect, flags, action)
705 VALUES ({?}, {?}, \'active\', {?})',
706 $this->user
->id(), $email, $filter);
707 if ($logger = S
::v('log', null
)) { // may be absent --> step4.php
708 S
::logger()->log('email_add', $email . ($this->user
->id() != S
::v('uid') ?
" (admin on {$this->user->login()})" : ""));
710 foreach ($this->emails
as $mail) {
711 if ($mail->email
== $email_stripped) {
715 $this->emails
[] = new Email($this->user
, array(
716 'redirect' => $email,
720 'broken_date' => '0000-00-00',
722 'last' => '0000-00-00',
729 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->user
->login());
730 check_redirect($this);
731 $this->update_imap();
735 public function modify_email($emails_actifs, $emails_rewrite)
737 foreach ($this->emails
as &$email) {
738 if (in_array($email->email
, $emails_actifs)) {
741 $email->deactivate();
743 $email->set_rewrite($emails_rewrite[$email->email
]);
745 check_redirect($this);
746 $this->update_imap();
750 public function modify_one_email($email, $activate)
754 foreach ($this->emails
as $i=>$mail) {
755 if ($mail->email
== $email) {
758 $allinactive &= !$mail->active ||
!$mail->sufficient ||
$mail->email
== $email;
760 if ($thisone === false
) {
761 return ERROR_INVALID_EMAIL
;
763 if ($allinactive ||
$activate) {
764 $this->emails
[$thisone]->activate();
766 $this->emails
[$thisone]->deactivate();
768 check_redirect($this);
769 $this->update_imap();
770 if ($allinactive && !$activate) {
771 return ERROR_INACTIVE_REDIRECTION
;
776 public function modify_one_email_redirect($email, $redirect)
778 foreach ($this->emails
as &$mail) {
779 if ($mail->email
== $email) {
780 $mail->set_rewrite($redirect);
781 check_redirect($this);
782 $this->update_imap();
788 public function clean_errors($email)
790 foreach ($this->emails
as &$mail) {
791 if ($mail->email
== $email) {
792 check_redirect($this);
793 $this->update_imap();
794 return $mail->clean_errors();
800 public function disable()
802 XDB
::execute("UPDATE email_redirect_account
803 SET flags = 'disable'
804 WHERE flags = 'active' AND uid = {?}", $this->user
->id());
805 foreach ($this->emails
as &$mail) {
806 if ($mail->active
&& $mail->has_disable()) {
807 $mail->disabled
= true
;
808 $mail->active
= false
;
811 check_redirect($this);
812 $this->update_imap();
815 public function enable()
817 XDB
::execute("UPDATE email_redirect_account
819 WHERE flags = 'disable' AND uid = {?}", $this->user
->id());
820 foreach ($this->emails
as &$mail) {
821 if ($mail->disabled
) {
822 $mail->disabled
= false
;
823 $mail->active
= true
;
825 check_redirect($this);
827 $this->update_imap();
830 public function get_broken_mx()
832 $res = XDB
::query("SELECT host, text
834 WHERE state != 'ok'");
835 if (!$res->numRows()) {
838 $mxs = $res->fetchAllAssoc();
840 foreach ($this->emails
as &$mail) {
841 if ($mail->active
&& strstr($mail->email
, '@') !== false
) {
842 list(,$domain) = explode('@', $mail->email
);
843 getmxrr($domain, $lcl_mxs);
844 if (empty($lcl_mxs)) {
845 $lcl_mxs = array($domain);
848 foreach ($mxs as &$mx) {
849 foreach ($lcl_mxs as $lcl) {
850 if (fnmatch($mx['host'], $lcl)) {
851 $broken = $mx['text'];
856 $mails[] = array('mail' => $mail->email
, 'text' => $broken);
865 public function active_emails()
868 foreach ($this->emails
as $mail) {
876 public function get_uid()
878 return $this->user
->id();
881 private function update_imap()
883 // Imaps must bounce if and only if the user has no active redirection.
884 if (!$this->other_active('')) {
885 XDB
::execute('UPDATE email_redirect_account
886 SET action = \'imap_and_bounce\'
887 WHERE type = \'imap\' AND uid = {?}',
890 XDB
::execute('UPDATE email_redirect_account
891 SET action = \'let_spams\'
892 WHERE type = \'imap\' AND uid = {?}',
898 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: