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