Adapts marketting processing to new mail chain.
[platal.git] / include / emails.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
12262f13 3 * Copyright (C) 2003-2011 Polytechnique.org *
0337d704 4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
b4503762
SJ
22define('SUCCESS', 1);
23define('ERROR_INACTIVE_REDIRECTION', 2);
24define('ERROR_INVALID_EMAIL', 3);
25define('ERROR_LOOP_EMAIL', 4);
0337d704 26
a9fd3272
SJ
27// function mark_broken_email() {{{1
28function mark_broken_email($email, $admin = false)
29{
30 $email = valide_email($email);
31 if (empty($email) || $email == '@') {
32 return;
33 }
34
35 $user = XDB::fetchOneAssoc('SELECT r1.uid, r1.broken_level != 0 AS broken, a.hruid, COUNT(r2.uid) AS nb_mails, a.full_name, s.email AS alias
36 FROM email_redirect_account AS r1
37 INNER JOIN accounts AS a ON (a.uid = r1.uid)
38 INNER JOIN email_source_account AS s ON (a.uid = s.uid AND s.flags = \'bestalias\')
39 LEFT JOIN email_redirect_account AS r2 ON (a.uid = r2.uid AND r1.redirect != r2.redirect AND
40 r2.broken_level = 0 AND r2.flags = \'active\' AND
41 (r2.type = \'smtp\' OR r2.type = \'googleapps\'))
42 WHERE r1.redirect = {?}
43 GROUP BY r1.uid', $email);
44
45 if ($user) {
46 // Mark address as broken.
47 if (!$user['broken']) {
48 XDB::execute('UPDATE email_redirect_account
49 SET broken_date = NOW(), last = NOW(), broken_level = 1
50 WHERE redirect = {?}', $email);
51 } elseif ($admin) {
52 XDB::execute('UPDATE email_redirect_account
53 SET last = CURDATE(), broken_level = IF(broken_level > 1, 3, 2)
54 WHERE redirect = {?} AND DATE_ADD(last, INTERVAL 14 DAY) < CURDATE()',
55 $email);
56 } else {
57 XDB::execute('UPDATE email_redirect_account
58 SET broken_level = 1
59 WHERE redirect = {?} AND broken_level = 0', $email);
60 }
61 }
62
63 return $user;
64}
65
441c2451 66// function fix_bestalias() {{{1
11ed26e5
VZ
67// Checks for an existing 'bestalias' among the the current user's aliases, and
68// eventually selects a new bestalias when required.
26ba053e 69function fix_bestalias(User $user)
0337d704 70{
b4503762
SJ
71 $count = XDB::fetchOneCell('SELECT COUNT(*)
72 FROM email_source_account
73 WHERE uid = {?} AND FIND_IN_SET(\'bestalias\', flags)',
74 $user->id());
11ed26e5 75
b4503762
SJ
76 if ($count == 1) {
77 return;
78 } elseif ($count > 1) {
79 // If too many bestaliases, delete the bestalias flag from all this
80 // user's emails (this should never happen).
81 XDB::execute("UPDATE email_source_account
82 SET flags = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', flags, ','), ',bestalias,', ','))
83 WHERE uid = {?}",
84 $user->id());
85 }
86
87 // If no bestalias is selected, we choose the shortest email which is not
88 // related to a usage name and contains a '.'.
89 XDB::execute("UPDATE email_source_account
90 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'bestalias')
91 WHERE uid = {?}
92 ORDER BY NOT FIND_IN_SET('usage', flags), email LIKE '%.%', LENGTH(email)
93 LIMIT 1",
94 $user->id());
0337d704 95}
96
441c2451 97// function valide_email() {{{1
11ed26e5
VZ
98// Returns a cleaned-up version of the @p email string. It removes garbage
99// characters, and determines the canonical form (without _ and +) for
100// Polytechnique.org email addresses.
0337d704 101function valide_email($str)
102{
a3a049fc 103 global $globals;
104
105 $em = trim(rtrim($str));
106 $em = str_replace('<', '', $em);
107 $em = str_replace('>', '', $em);
97664536
SJ
108 if (strpos($em, '@') === false) {
109 return;
110 }
a3a049fc 111 list($ident, $dom) = explode('@', $em);
97664536 112 if ($dom == $globals->mail->domain || $dom == $globals->mail->domain2) {
a3a049fc 113 list($ident1) = explode('_', $ident);
114 list($ident) = explode('+', $ident1);
115 }
116 return $ident . '@' . $dom;
0337d704 117}
118
c6310567 119// function isvalid_email_redirection() {{{1
b4503762
SJ
120/** Checks if an email is a suitable redirection.
121 * @param $email the email to check
c6310567
FB
122 * @return BOOL
123 */
124function isvalid_email_redirection($email)
125{
126 return isvalid_email($email) &&
127 !preg_match("/@(polytechnique\.(org|edu)|melix\.(org|net)|m4x\.org)$/", $email);
128}
129
180627f3 130// function ids_from_mails() {{{1
b4503762
SJ
131// Converts an array of emails to an array of email => uid, where email is the
132// given email when we found a matching user.
180627f3 133function ids_from_mails(array $emails)
1605f171
RB
134{
135 global $globals;
180627f3 136
b4503762
SJ
137 // Removes duplicates, if any.
138 $emails = array_unique($emails);
139
140 // Formats and splits by domain type (locally managed or external) emails.
141 $domain_emails = array();
142 $other_emails = array();
1605f171
RB
143 foreach ($emails as $email) {
144 if (strpos($email, '@') === false) {
145 $user = $email;
146 $domain = $globals->mail->domain2;
147 } else {
148 list($user, $domain) = explode('@', $email);
149 }
b4503762
SJ
150 if ($domain == $globals->mail->alias_dom || $domain == $globals->mail->alias_dom2
151 || $domain == $globals->mail->domain || $domain == $globals->mail->domain2) {
1605f171
RB
152 list($user) = explode('+', $user);
153 list($user) = explode('_', $user);
b4503762 154 $domain_emails[$email] = strtolower($user . '@' . $domain);
1605f171 155 } else {
b4503762 156 $other_emails[$email] = strtolower($user . '@' . $domain);
1605f171
RB
157 }
158 }
180627f3 159
b4503762
SJ
160 // Retrieves emails from our domains.
161 $domain_uids = XDB::fetchAllAssoc('email',
162 'SELECT email, uid
163 FROM email_source_account
164 WHERE email IN {?}',
165 array_unique($domain_emails));
1605f171 166
b4503762
SJ
167 // Retrieves emails from redirections.
168 $other_uids = XDB::fetchAllAssoc('redirect',
169 'SELECT redirect, uid
170 FROM email_redirect_account
171 WHERE redirect IN {?}',
172 array_unique($other_emails));
1605f171 173
b4503762
SJ
174 // Associates given emails with the corresponding uid.
175 $uids = array();
176 foreach (array_merge($domain_emails, $other_emails) as $email => $canonical_email) {
177 if (array_key_exists($canonical_email, $domain_uids)) {
178 $uids[$email] = $domain_uids[$canonical_email];
179 } elseif (array_key_exists($canonical_email, $other_uids)) {
180 $uids[$email] = $other_uids[$canonical_email];
1605f171
RB
181 }
182 }
183
184 return $uids;
185}
186
441c2451 187// class Bogo {{{1
11ed26e5 188// The Bogo class represents a spam filtering level in plat/al architecture.
0337d704 189class Bogo
190{
b4503762 191 private static $states = array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams');
a3a049fc 192
12a587df 193 private $user;
612a2d8a 194 private $state;
a3a049fc 195
26ba053e 196 public function __construct(User $user)
0337d704 197 {
12a587df 198 if (!$user) {
3c1e6a1e 199 return;
200 }
11ed26e5 201
12a587df 202 $this->user = &$user;
b4503762
SJ
203 $res = XDB::query('SELECT action
204 FROM email_redirect_account
205 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')',
206 $user->id());
207 if ($res->numRows() == 0) {
208 return;
3c1e6a1e 209 }
b4503762 210 $this->state = $res->fetchOneCell();
0337d704 211 }
212
11ed26e5 213 public function change($state)
0337d704 214 {
b4503762
SJ
215 $this->state = is_int($state) ? self::$states[$state] : $state;
216 XDB::execute('UPDATE email_redirect_account
217 SET action = {?}
218 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')',
12a587df 219 $this->state, $this->user->id());
0337d704 220 }
221
612a2d8a 222 public function level()
223 {
b4503762 224 return array_search($this->state, self::$states);
612a2d8a 225 }
0337d704 226}
227
441c2451 228// class Email {{{1
11ed26e5 229// Represents an "email address" used as final recipient for plat/al-managed
b4503762
SJ
230// addresses.
231class Email
0337d704 232{
b4503762
SJ
233 // Lists fields to load automatically.
234 static private $field_names = array('rewrite', 'type', 'action', 'broken_date', 'broken_level', 'last', 'hash', 'allow_rewrite');
235
236 // Shortname to realname mapping for known mail storage backends.
237 static private $display_names = array(
238 'imap' => 'Accès de secours aux emails (IMAP)',
239 'googleapps' => 'Compte Google Apps',
240 );
241 static private $storage_domains = array(
242 'imap' => 'imap',
243 'googleapps' => 'g'
244 );
245
246 private $user;
612a2d8a 247
11ed26e5 248 // Basic email properties; $sufficient indicates if the email can be used as
b4503762 249 // an unique redirection; $redirect contains the delivery email address.
11ed26e5
VZ
250 public $type;
251 public $sufficient;
612a2d8a 252 public $email;
11ed26e5 253 public $display_email;
b4503762
SJ
254 public $domain;
255 public $action;
11ed26e5
VZ
256
257 // Redirection status properties.
612a2d8a 258 public $active;
b4503762 259 public $inactive;
612a2d8a 260 public $broken;
441c2451 261 public $disabled;
612a2d8a 262 public $rewrite;
3d17e48f
FB
263 public $allow_rewrite;
264 public $hash;
11ed26e5
VZ
265
266 // Redirection bounces stats.
612a2d8a 267 public $last;
b4503762
SJ
268 public $broken_level;
269 public $broken_date;
0337d704 270
b4503762 271 public function __construct(User $user, array $row)
0337d704 272 {
b4503762
SJ
273 foreach (self::$field_names as $field) {
274 if (array_key_exists($field, $row)) {
275 $this->$field = $row[$field];
276 }
277 }
278 $this->email = $row['redirect'];
11ed26e5 279
b4503762
SJ
280 if (array_key_exists($this->type, Email::$display_names)) {
281 $this->display_email = self::$display_names[$this->type];
282 } else {
283 $this->display_email = $this->email;
284 }
285 foreach (array('active', 'inactive', 'broken', 'disabled') as $status) {
286 $this->$status = ($status == $row['flags']);
287 }
288 $this->sufficient = ($this->type == 'smtp' || $this->type == 'googleapps');
289 $this->user = &$user;
0337d704 290 }
291
b4503762 292 // Activates the email address as a redirection.
11ed26e5 293 public function activate()
0337d704 294 {
b4503762
SJ
295 if ($this->inactive) {
296 XDB::execute('UPDATE email_redirect_account
297 SET broken_level = IF(flags = \'broken\', broken_level - 1, broken_level), flags = \'active\'
298 WHERE uid = {?} AND redirect = {?}',
299 $this->user->id(), $this->email);
300 S::logger()->log('email_on', $this->email . ($this->user->id() != S::v('uid') ? "(admin on {$this->user->login()})" : ''));
301 $this->inactive = false;
302 $this->active = true;
0337d704 303 }
304 }
305
b4503762 306 // Deactivates the email address as a redirection.
11ed26e5 307 public function deactivate()
0337d704 308 {
0337d704 309 if ($this->active) {
b4503762
SJ
310 XDB::execute('UPDATE email_redirect_account
311 SET flags = \'inactive\'
312 WHERE uid = {?} AND redirect = {?}',
313 $this->user->id(), $this->email);
314 S::logger()->log('email_off', $this->email . ($this->user->id() != S::v('uid') ? "(admin on {$this->user->login()})" : "") );
315 $this->inactive = true;
316 $this->active = false;
0337d704 317 }
318 }
612a2d8a 319
0337d704 320
b4503762 321 // Sets the rewrite rule for the given address.
11ed26e5 322 public function set_rewrite($rewrite)
0337d704 323 {
b4503762 324 if ($this->type != 'smtp' || $this->rewrite == $rewrite) {
0337d704 325 return;
326 }
11ed26e5
VZ
327 if (!$rewrite || !isvalid_email($rewrite)) {
328 $rewrite = '';
12acff5d 329 }
b4503762
SJ
330 XDB::execute('UPDATE email_redirect_account
331 SET rewrite = {?}
332 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
333 $rewrite, $this->user->id(), $this->email);
11ed26e5 334 $this->rewrite = $rewrite;
3d17e48f
FB
335 if (!$this->allow_rewrite) {
336 global $globals;
337 if (empty($this->hash)) {
338 $this->hash = rand_url_id();
b4503762
SJ
339 XDB::execute('UPDATE email_redirect_account
340 SET hash = {?}
341 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
342 $this->hash, $this->user->id(), $this->email);
3d17e48f 343 }
3d17e48f
FB
344 $mail = new PlMailer('emails/rewrite-in.mail.tpl');
345 $mail->assign('mail', $this);
21c7c593 346 $mail->assign('user', $this->user);
3d17e48f 347 $mail->assign('baseurl', $globals->baseurl);
c66de9a0
FB
348 $mail->assign('sitename', $globals->core->sitename);
349 $mail->assign('to', $this->email);
21c7c593 350 $mail->send($this->user->isEmailFormatHtml());
3d17e48f 351 }
0337d704 352 }
353
441c2451 354
b4503762 355 // Resets the error counts associated with the redirection.
11ed26e5 356 public function clean_errors()
441c2451 357 {
b4503762
SJ
358 if ($this->type != 'smtp') {
359 return;
360 }
dd70cd28 361 if (!S::admin()) {
441c2451 362 return false;
363 }
b4503762
SJ
364 $this->broken = 0;
365 $this->broken_level = 0;
366 $this->last = 0;
367 return XDB::execute('UPDATE email_redirect_account
368 SET broken_level = 0, broken_date = 0, last = 0
369 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
12a587df 370 $this->user->id(), $this->email);
11ed26e5
VZ
371 }
372
11ed26e5 373
b4503762
SJ
374 // Email backend capabilities ('rewrite' refers to From: rewrite for mails
375 // forwarded by Polytechnique.org's MXs; 'removable' indicates if the email
376 // can be definitively removed; 'disable' indicates if the email has a third
377 // status 'disabled' in addition to 'active' and 'inactive').
11ed26e5
VZ
378 public function has_rewrite()
379 {
b4503762 380 return ($this->type == 'smtp');
11ed26e5
VZ
381 }
382
11ed26e5
VZ
383 public function is_removable()
384 {
b4503762 385 return ($this->type == 'smtp');
11ed26e5
VZ
386 }
387
11ed26e5
VZ
388 public function has_disable()
389 {
390 return true;
441c2451 391 }
afde0a3a 392
b4503762 393 public function is_redirection()
afde0a3a 394 {
b4503762 395 return ($this->type == 'smtp');
afde0a3a
VZ
396 }
397
398 // Returns the list of allowed storages for the @p user.
b4503762 399 static private function get_allowed_storages(User $user)
afde0a3a
VZ
400 {
401 global $globals;
402 $storages = array();
403
404 // Google Apps storage is available for users with valid Google Apps account.
405 require_once 'googleapps.inc.php';
406 if ($globals->mailstorage->googleapps_domain &&
12a587df 407 GoogleAppsAccount::account_status($user->id()) == 'active') {
afde0a3a
VZ
408 $storages[] = 'googleapps';
409 }
410
411 // IMAP storage is always visible to administrators, and is allowed for
412 // everyone when the service is marked as 'active'.
dd70cd28 413 if ($globals->mailstorage->imap_active || S::admin()) {
afde0a3a
VZ
414 $storages[] = 'imap';
415 }
416
417 return $storages;
418 }
419
b4503762 420 static public function activate_storage(User $user, $storage)
afde0a3a 421 {
b4503762 422 Platal::assert(in_array($storage, self::get_allowed_storages($user)));
afde0a3a 423
b4503762
SJ
424 if (!self::is_active_storage($user, $storage)) {
425 global $globals;
426
427 XDB::execute('INSERT INTO email_redirect_account (uid, type, redirect, flags)
428 VALUES ({?}, {?}, {?}, \'active\')',
429 $user->id(), $storage,
430 $user->hruid . '@' . self::$storage_domains[$storage] . '.' . $globals->mail->domain);
431 }
afde0a3a
VZ
432 }
433
b4503762 434 static public function deactivate_storage(User $user, $storage)
afde0a3a 435 {
b4503762
SJ
436 if (in_array($storage, self::$storage_domains)) {
437 XDB::execute('DELETE FROM email_redirect_account
438 WHERE uid = {?} AND type = {?}',
439 $user->id(), $storage);
440 }
afde0a3a
VZ
441 }
442
b4503762 443 static public function is_active_storage(User $user, $storage)
afde0a3a 444 {
b4503762
SJ
445 if (!in_array($storage, self::$storage_domains)) {
446 return false;
afde0a3a 447 }
b4503762
SJ
448 $res = XDB::fetchOneCell('SELECT COUNT(*)
449 FROM email_redirect_account
450 WHERE uid = {?} AND type = {?} AND flags = \'active\')',
451 $user->id(), $storage);
452 return !is_null($res) && $res > 0;
afde0a3a 453 }
afde0a3a 454}
441c2451 455// class Redirect {{{1
11ed26e5
VZ
456// Redirect is a placeholder class for an user's active redirections (third-party
457// redirection email, or Polytechnique.org mail storages).
0337d704 458class Redirect
459{
b4503762 460 private $flags = 'active';
12a587df 461 private $user;
612a2d8a 462
463 public $emails;
464 public $bogo;
0337d704 465
26ba053e 466 public function __construct(User $user)
0337d704 467 {
12a587df
VZ
468 $this->user = &$user;
469 $this->bogo = new Bogo($user);
11ed26e5 470
afde0a3a 471 // Adds third-party email redirections.
b4503762
SJ
472 $res = XDB::iterator('SELECT redirect, rewrite, type, action, broken_date, broken_level, last, flags, hash, allow_rewrite
473 FROM email_redirect_account
474 WHERE uid = {?} AND type != \'homonym\'',
475 $user->id());
476 $this->emails = array();
0337d704 477 while ($row = $res->next()) {
b4503762 478 $this->emails[] = new Email($user, $row);
afde0a3a 479 }
0337d704 480 }
481
612a2d8a 482 public function other_active($email)
0337d704 483 {
484 foreach ($this->emails as $mail) {
11ed26e5 485 if ($mail->email != $email && $mail->active && $mail->sufficient) {
0337d704 486 return true;
487 }
488 }
489 return false;
490 }
491
612a2d8a 492 public function delete_email($email)
0337d704 493 {
0337d704 494 if (!$this->other_active($email)) {
495 return ERROR_INACTIVE_REDIRECTION;
496 }
b4503762
SJ
497 XDB::execute('DELETE FROM email_redirect_account
498 WHERE uid = {?} AND redirect = {?} AND type != \'homonym\'',
499 $this->user->id(), $email);
12a587df 500 S::logger()->log('email_del', $email . ($this->user->id() != S::v('uid') ? " (admin on {$this->user->login()})" : ""));
11ed26e5
VZ
501 foreach ($this->emails as $i => $mail) {
502 if ($email == $mail->email) {
0337d704 503 unset($this->emails[$i]);
504 }
3c1e6a1e 505 }
ccdbc270 506 check_redirect($this);
0337d704 507 return SUCCESS;
508 }
509
612a2d8a 510 public function add_email($email)
0337d704 511 {
0337d704 512 $email_stripped = strtolower(trim($email));
513 if (!isvalid_email($email_stripped)) {
514 return ERROR_INVALID_EMAIL;
515 }
516 if (!isvalid_email_redirection($email_stripped)) {
517 return ERROR_LOOP_EMAIL;
518 }
00ba8a74 519 // If the email was already present for this user, we reset it to the default values, we thus use REPLACE INTO.
b4503762 520 XDB::execute('REPLACE INTO email_redirect_account (uid, redirect, flags)
00ba8a74
SJ
521 VALUES ({?}, {?}, \'active\')',
522 $this->user->id(), $email);
3c1e6a1e 523 if ($logger = S::v('log', null)) { // may be absent --> step4.php
12a587df 524 S::logger()->log('email_add', $email . ($this->user->id() != S::v('uid') ? " (admin on {$this->user->login()})" : ""));
0337d704 525 }
3c1e6a1e 526 foreach ($this->emails as $mail) {
527 if ($mail->email == $email_stripped) {
0337d704 528 return SUCCESS;
529 }
3c1e6a1e 530 }
b4503762
SJ
531 $this->emails[] = new Email($this->user, array(
532 'redirect' => $email,
533 'rewrite' => '',
534 'type' => 'smtp',
535 'action' => 'default',
536 'broken_date' => '0000-00-00',
537 'broken_level' => 0,
538 'last' => '0000-00-00',
539 'flags' => 'active',
540 'hash' => null,
541 'allow_rewrite' => 0
542 ));
ca6d07f4 543
544 // security stuff
12a587df 545 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->user->login());
ccdbc270 546 check_redirect($this);
0337d704 547 return SUCCESS;
548 }
549
612a2d8a 550 public function modify_email($emails_actifs, $emails_rewrite)
0337d704 551 {
b4503762
SJ
552 foreach ($this->emails as &$email) {
553 if (in_array($email->email, $emails_actifs)) {
554 $email->activate();
3c1e6a1e 555 } else {
b4503762 556 $email->deactivate();
3c1e6a1e 557 }
b4503762 558 $email->set_rewrite($emails_rewrite[$email->email]);
0337d704 559 }
ccdbc270 560 check_redirect($this);
b4503762 561 return SUCCESS;
0337d704 562 }
563
eaf30d86 564 public function modify_one_email($email, $activate)
ccdbc270 565 {
b7582015 566 $allinactive = true;
567 $thisone = false;
8ffa657a 568 foreach ($this->emails as $i=>$mail) {
569 if ($mail->email == $email) {
b7582015 570 $thisone = $i;
8ffa657a 571 }
11ed26e5 572 $allinactive &= !$mail->active || !$mail->sufficient || $mail->email == $email;
8ffa657a 573 }
b7582015 574 if ($thisone === false) {
575 return ERROR_INVALID_EMAIL;
576 }
ccdbc270 577 if ($allinactive || $activate) {
11ed26e5 578 $this->emails[$thisone]->activate();
ccdbc270 579 } else {
11ed26e5 580 $this->emails[$thisone]->deactivate();
ccdbc270 581 }
582 check_redirect($this);
b7582015 583 if ($allinactive && !$activate) {
584 return ERROR_INACTIVE_REDIRECTION;
eaf30d86 585 }
b4503762 586 return SUCCESS;
8ffa657a 587 }
588
612a2d8a 589 public function modify_one_email_redirect($email, $redirect)
590 {
441c2451 591 foreach ($this->emails as &$mail) {
612a2d8a 592 if ($mail->email == $email) {
11ed26e5 593 $mail->set_rewrite($redirect);
ccdbc270 594 check_redirect($this);
595 return;
612a2d8a 596 }
597 }
598 }
441c2451 599
11ed26e5 600 public function clean_errors($email)
441c2451 601 {
602 foreach ($this->emails as &$mail) {
603 if ($mail->email == $email) {
e1547442 604 check_redirect($this);
11ed26e5 605 return $mail->clean_errors();
441c2451 606 }
607 }
608 return false;
609 }
610
441c2451 611 public function disable()
612 {
b4503762 613 XDB::execute("UPDATE email_redirect_account
441c2451 614 SET flags = 'disable'
20398e42 615 WHERE flags = 'active' AND uid = {?}", $this->user->id());
441c2451 616 foreach ($this->emails as &$mail) {
11ed26e5 617 if ($mail->active && $mail->has_disable()) {
441c2451 618 $mail->disabled = true;
619 $mail->active = false;
620 }
621 }
e1547442 622 check_redirect($this);
441c2451 623 }
624
441c2451 625 public function enable()
626 {
b4503762 627 XDB::execute("UPDATE email_redirect_account
441c2451 628 SET flags = 'active'
20398e42 629 WHERE flags = 'disable' AND uid = {?}", $this->user->id());
441c2451 630 foreach ($this->emails as &$mail) {
631 if ($mail->disabled) {
441c2451 632 $mail->disabled = false;
b4503762 633 $mail->active = true;
441c2451 634 }
e1547442 635 check_redirect($this);
441c2451 636 }
637 }
638
612a2d8a 639 public function get_broken_mx()
ccdbc270 640 {
3083bd93 641 $res = XDB::query("SELECT host, text
8af1d78f
FB
642 FROM mx_watch
643 WHERE state != 'ok'");
120bd636 644 if (!$res->numRows()) {
ccdbc270 645 return array();
646 }
c754cf5b 647 $mxs = $res->fetchAllAssoc();
ccdbc270 648 $mails = array();
649 foreach ($this->emails as &$mail) {
11ed26e5 650 if ($mail->active && strstr($mail->email, '@') !== false) {
ccdbc270 651 list(,$domain) = explode('@', $mail->email);
652 getmxrr($domain, $lcl_mxs);
653 if (empty($lcl_mxs)) {
654 $lcl_mxs = array($domain);
655 }
656 $broken = false;
657 foreach ($mxs as &$mx) {
658 foreach ($lcl_mxs as $lcl) {
c754cf5b 659 if (fnmatch($mx['host'], $lcl)) {
ccdbc270 660 $broken = $mx['text'];
661 break;
662 }
663 }
664 if ($broken) {
3083bd93 665 $mails[] = array('mail' => $mail->email, 'text' => $broken);
f653ff3d 666 break;
ccdbc270 667 }
668 }
669 }
670 }
671 return $mails;
672 }
e97e9b8f 673
e97e9b8f
VZ
674 public function active_emails()
675 {
676 $emails = array();
677 foreach ($this->emails as $mail) {
678 if ($mail->active) {
679 $emails[] = $mail;
680 }
681 }
682 return $emails;
683 }
45282934 684
45282934
VZ
685 public function get_uid()
686 {
12a587df 687 return $this->user->id();
45282934 688 }
0337d704 689}
690
a7de4ef7 691// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 692?>