Refactors the Email/Redirect classes, to prepare for MailStorage integration.
[platal.git] / include / emails.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 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
22require_once("xorg.misc.inc.php");
23
0337d704 24define("SUCCESS", 1);
25define("ERROR_INACTIVE_REDIRECTION", 2);
26define("ERROR_INVALID_EMAIL", 3);
27define("ERROR_LOOP_EMAIL", 4);
28
441c2451 29// function fix_bestalias() {{{1
11ed26e5
VZ
30// Checks for an existing 'bestalias' among the the current user's aliases, and
31// eventually selects a new bestalias when required.
0337d704 32function fix_bestalias($uid)
33{
11ed26e5
VZ
34 $res = XDB::query("SELECT COUNT(*)
35 FROM aliases
36 WHERE id = {?} AND FIND_IN_SET('bestalias', flags) AND type != 'homonyme'",
37 $uid);
38 if ($res->fetchOneCell()) {
0337d704 39 return;
40 }
11ed26e5 41
08cce2ff 42 XDB::execute("UPDATE aliases
612a2d8a 43 SET flags=CONCAT(flags,',','bestalias')
44 WHERE id={?} AND type!='homonyme'
45 ORDER BY !FIND_IN_SET('usage',flags),alias LIKE '%.%', LENGTH(alias)
46 LIMIT 1", $uid);
0337d704 47}
48
441c2451 49// function valide_email() {{{1
11ed26e5
VZ
50// Returns a cleaned-up version of the @p email string. It removes garbage
51// characters, and determines the canonical form (without _ and +) for
52// Polytechnique.org email addresses.
0337d704 53function valide_email($str)
54{
a3a049fc 55 global $globals;
56
57 $em = trim(rtrim($str));
58 $em = str_replace('<', '', $em);
59 $em = str_replace('>', '', $em);
60 list($ident, $dom) = explode('@', $em);
61 if ($dom == $globals->mail->domain or $dom == $globals->mail->domain2) {
62 list($ident1) = explode('_', $ident);
63 list($ident) = explode('+', $ident1);
64 }
65 return $ident . '@' . $dom;
0337d704 66}
67
441c2451 68// class Bogo {{{1
11ed26e5 69// The Bogo class represents a spam filtering level in plat/al architecture.
0337d704 70class Bogo
71{
441c2451 72 // properties {{{2
a3a049fc 73
11ed26e5 74 private $uid;
612a2d8a 75 private $state;
76 private $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams');
0337d704 77
441c2451 78 // constructor {{{2
a3a049fc 79
612a2d8a 80 public function __construct($uid)
0337d704 81 {
3c1e6a1e 82 if (!$uid) {
83 return;
84 }
11ed26e5
VZ
85
86 $this->uid = $uid;
3c1e6a1e 87 $res = XDB::query('SELECT email FROM emails WHERE uid={?} AND flags="filter"', $uid);
88 if ($res->numRows()) {
612a2d8a 89 $this->state = $res->fetchOneCell();
3c1e6a1e 90 } else {
91 $this->state = 'tag_and_drop_spams';
92 $res = XDB::query("INSERT INTO emails (uid,email,rewrite,panne,flags)
93 VALUES ({?},'tag_and_drop_spams','','0000-00-00','filter')", $uid);
94 }
0337d704 95 }
96
441c2451 97 // public function change() {{{2
0337d704 98
11ed26e5 99 public function change($state)
0337d704 100 {
612a2d8a 101 $this->state = is_int($state) ? $this->_states[$state] : $state;
102 XDB::execute('UPDATE emails SET email={?} WHERE uid={?} AND flags = "filter"',
11ed26e5 103 $this->state, $this->uid);
0337d704 104 }
105
441c2451 106 // pubic function level() {{{2
0337d704 107
612a2d8a 108 public function level()
109 {
110 return array_search($this->state, $this->_states);
111 }
0337d704 112}
113
441c2451 114// class Email {{{1
11ed26e5
VZ
115// Represents an "email address" used as final recipient for plat/al-managed
116// addresses; it can be subclasses a Redirection emails (third-party) or as
117// Storage emails (Polytechnique.org).
118abstract class Email
0337d704 119{
11ed26e5 120 protected $uid;
612a2d8a 121
11ed26e5
VZ
122 // Basic email properties; $sufficient indicates if the email can be used as
123 // an unique redirection; $email contains the delivery email address.
124 public $type;
125 public $sufficient;
612a2d8a 126 public $email;
11ed26e5
VZ
127 public $display_email;
128
129 // Redirection status properties.
612a2d8a 130 public $active;
131 public $broken;
441c2451 132 public $disabled;
612a2d8a 133 public $rewrite;
11ed26e5
VZ
134
135 // Redirection bounces stats.
612a2d8a 136 public $panne;
137 public $last;
138 public $panne_level;
0337d704 139
11ed26e5
VZ
140 // Activates the email address as a redirection.
141 public abstract function activate();
142
143 // Deactivates the email address as a redirection.
144 public abstract function deactivate();
145
146 // Sets the rewrite rule for the given address.
147 public abstract function set_rewrite($rewrite);
148
149 // Resets the error counts associated with the redirection.
150 public abstract function clean_errors();
151
152 // Email backend capabilities ('rewrite' refers to From: rewrite for mails
153 // forwarded by Polytechnique.org's MXs; 'removable' indicates if the email
154 // can be definitively removed; 'disable' indicates if the email has a third
155 // status 'disabled' in addition to 'active' and 'inactive').
156 public abstract function has_rewrite();
157 public abstract function is_removable();
158 public abstract function has_disable();
159}
160
161// class EmailRedirection {{{1
162// Implementation of Email for third-party redirection (redirection of emails to
163// external user-supplied addresses).
164class EmailRedirection extends Email
165{
441c2451 166 // constructor {{{2
0337d704 167
11ed26e5 168 public function __construct($uid, $row)
0337d704 169 {
11ed26e5
VZ
170 $this->uid = $uid;
171 $this->sufficient = true;
172
2069538b 173 list($this->email, $flags, $this->rewrite, $this->panne, $this->last, $this->panne_level) = $row;
11ed26e5 174 $this->display_email = $this->email;
441c2451 175 $this->active = ($flags == 'active');
176 $this->broken = ($flags == 'panne');
177 $this->disabled = ($flags == 'disable');
0337d704 178 }
179
441c2451 180 // public function activate() {{{2
0337d704 181
11ed26e5 182 public function activate()
0337d704 183 {
0337d704 184 if (!$this->active) {
dc557110 185 XDB::execute("UPDATE emails
186 SET panne_level = IF(flags = 'panne', panne_level - 1, panne_level),
187 flags = 'active'
11ed26e5
VZ
188 WHERE uid={?} AND email={?}", $this->uid, $this->email);
189 $_SESSION['log']->log("email_on", $this->email.($this->uid!=S::v('uid') ? "(admin on {$this->uid})" : ""));
0337d704 190 $this->active = true;
dc557110 191 $this->broken = false;
0337d704 192 }
193 }
194
441c2451 195 // public function deactivate() {{{2
0337d704 196
11ed26e5 197 public function deactivate()
0337d704 198 {
0337d704 199 if ($this->active) {
08cce2ff 200 XDB::execute("UPDATE emails SET flags =''
11ed26e5
VZ
201 WHERE uid={?} AND email={?}", $this->uid, $this->email);
202 $_SESSION['log']->log("email_off",$this->email.($this->uid != S::v('uid') ? "(admin on {$this->uid})" : "") );
0337d704 203 $this->active = false;
204 }
205 }
612a2d8a 206
11ed26e5 207 // public function set_rewrite() {{{2
0337d704 208
11ed26e5 209 public function set_rewrite($rewrite)
0337d704 210 {
11ed26e5 211 if ($this->rewrite == $rewrite) {
0337d704 212 return;
213 }
11ed26e5
VZ
214 if (!$rewrite || !isvalid_email($rewrite)) {
215 $rewrite = '';
12acff5d 216 }
11ed26e5
VZ
217 XDB::execute('UPDATE emails SET rewrite={?} WHERE uid={?} AND email={?}', $rewrite, $this->uid, $this->email);
218 $this->rewrite = $rewrite;
3c1e6a1e 219 return;
0337d704 220 }
221
11ed26e5 222 // public function clean_errors() {{{2
441c2451 223
11ed26e5 224 public function clean_errors()
441c2451 225 {
226 if (!S::has_perms()) {
227 return false;
228 }
229 $this->panne = 0;
230 $this->panne_level = 0;
231 $this->last = 0;
232 return XDB::execute("UPDATE emails
233 SET panne_level = 0, panne = 0, last = 0
234 WHERE uid = {?} AND email = {?}",
11ed26e5
VZ
235 $this->uid, $this->email);
236 }
237
238 // public function has_rewrite() {{{2
239
240 public function has_rewrite()
241 {
242 return true;
243 }
244
245 // public function is_removable() {{{2
246
247 public function is_removable()
248 {
249 return true;
250 }
251
252 // public function has_disable() {{{2
253
254 public function has_disable()
255 {
256 return true;
441c2451 257 }
0337d704 258}
259
441c2451 260// class Redirect {{{1
11ed26e5
VZ
261// Redirect is a placeholder class for an user's active redirections (third-party
262// redirection email, or Polytechnique.org mail storages).
0337d704 263class Redirect
264{
441c2451 265 // properties {{{2
612a2d8a 266
267 private $flag_active = 'active';
268 private $uid;
269
270 public $emails;
271 public $bogo;
0337d704 272
441c2451 273 // constructor {{{2
0337d704 274
612a2d8a 275 public function __construct($_uid)
0337d704 276 {
11ed26e5
VZ
277 $this->uid = $_uid;
278
612a2d8a 279 $res = XDB::iterRow("SELECT email, flags, rewrite, panne, last, panne_level
280 FROM emails
281 WHERE uid = {?} AND flags != 'filter'", $_uid);
11ed26e5 282 $this->emails = Array();
0337d704 283 while ($row = $res->next()) {
11ed26e5 284 $this->emails[] = new EmailRedirection($_uid, $row);
0337d704 285 }
3c1e6a1e 286 $this->bogo = new Bogo($_uid);
0337d704 287 }
288
441c2451 289 // public function other_active() {{{2
0337d704 290
612a2d8a 291 public function other_active($email)
0337d704 292 {
293 foreach ($this->emails as $mail) {
11ed26e5 294 if ($mail->email != $email && $mail->active && $mail->sufficient) {
0337d704 295 return true;
296 }
297 }
298 return false;
299 }
300
441c2451 301 // public function delete_email() {{{2
0337d704 302
612a2d8a 303 public function delete_email($email)
0337d704 304 {
0337d704 305 if (!$this->other_active($email)) {
306 return ERROR_INACTIVE_REDIRECTION;
307 }
08cce2ff 308 XDB::execute('DELETE FROM emails WHERE uid={?} AND email={?}', $this->uid, $email);
cab08090 309 $_SESSION['log']->log('email_del',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : ""));
11ed26e5
VZ
310 foreach ($this->emails as $i => $mail) {
311 if ($email == $mail->email) {
0337d704 312 unset($this->emails[$i]);
313 }
3c1e6a1e 314 }
ccdbc270 315 check_redirect($this);
0337d704 316 return SUCCESS;
317 }
318
441c2451 319 // public function add_email() {{{2
612a2d8a 320
321 public function add_email($email)
0337d704 322 {
0337d704 323 $email_stripped = strtolower(trim($email));
324 if (!isvalid_email($email_stripped)) {
325 return ERROR_INVALID_EMAIL;
326 }
327 if (!isvalid_email_redirection($email_stripped)) {
328 return ERROR_LOOP_EMAIL;
329 }
08cce2ff 330 XDB::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->uid, $email);
3c1e6a1e 331 if ($logger = S::v('log', null)) { // may be absent --> step4.php
332 $logger->log('email_add',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : ""));
0337d704 333 }
3c1e6a1e 334 foreach ($this->emails as $mail) {
335 if ($mail->email == $email_stripped) {
0337d704 336 return SUCCESS;
337 }
3c1e6a1e 338 }
11ed26e5 339 $this->emails[] = new EmailRedirection($this->uid, array($email, 'active', '', '0000-00-00', '0000-00-00', 0));
ca6d07f4 340
341 // security stuff
a7de4ef7 342 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->uid);
ccdbc270 343 check_redirect($this);
0337d704 344 return SUCCESS;
345 }
346
441c2451 347 // public function modify_email() {{{2
0337d704 348
612a2d8a 349 public function modify_email($emails_actifs, $emails_rewrite)
0337d704 350 {
441c2451 351 foreach ($this->emails as &$mail) {
352 if (in_array($mail->email, $emails_actifs)) {
11ed26e5 353 $mail->activate();
3c1e6a1e 354 } else {
11ed26e5 355 $mail->deactivate();
3c1e6a1e 356 }
11ed26e5 357 $mail->set_rewrite($emails_rewrite[$mail->email]);
0337d704 358 }
ccdbc270 359 check_redirect($this);
0337d704 360 }
361
441c2451 362 // public function modify_one_email() {{{2
363
eaf30d86 364 public function modify_one_email($email, $activate)
ccdbc270 365 {
b7582015 366 $allinactive = true;
367 $thisone = false;
8ffa657a 368 foreach ($this->emails as $i=>$mail) {
369 if ($mail->email == $email) {
b7582015 370 $thisone = $i;
8ffa657a 371 }
11ed26e5 372 $allinactive &= !$mail->active || !$mail->sufficient || $mail->email == $email;
8ffa657a 373 }
b7582015 374 if ($thisone === false) {
375 return ERROR_INVALID_EMAIL;
376 }
ccdbc270 377 if ($allinactive || $activate) {
11ed26e5 378 $this->emails[$thisone]->activate();
ccdbc270 379 } else {
11ed26e5 380 $this->emails[$thisone]->deactivate();
ccdbc270 381 }
382 check_redirect($this);
b7582015 383 if ($allinactive && !$activate) {
384 return ERROR_INACTIVE_REDIRECTION;
385 } else {
386 return SUCCESS;
eaf30d86 387 }
8ffa657a 388 }
389
441c2451 390 // public function modify_one_email_redirect() {{{2
391
612a2d8a 392 public function modify_one_email_redirect($email, $redirect)
393 {
441c2451 394 foreach ($this->emails as &$mail) {
612a2d8a 395 if ($mail->email == $email) {
11ed26e5 396 $mail->set_rewrite($redirect);
ccdbc270 397 check_redirect($this);
398 return;
612a2d8a 399 }
400 }
401 }
441c2451 402
11ed26e5 403 // function clean_errors() {{{2
441c2451 404
11ed26e5 405 public function clean_errors($email)
441c2451 406 {
407 foreach ($this->emails as &$mail) {
408 if ($mail->email == $email) {
e1547442 409 check_redirect($this);
11ed26e5 410 return $mail->clean_errors();
441c2451 411 }
412 }
413 return false;
414 }
415
416 // function disable() {{{2
417
418 public function disable()
419 {
420 XDB::execute("UPDATE emails
421 SET flags = 'disable'
422 WHERE flags = 'active' AND uid = {?}", $this->uid);
423 foreach ($this->emails as &$mail) {
11ed26e5 424 if ($mail->active && $mail->has_disable()) {
441c2451 425 $mail->disabled = true;
426 $mail->active = false;
427 }
428 }
e1547442 429 check_redirect($this);
441c2451 430 }
431
432 // function enable() {{{2
433
434 public function enable()
435 {
436 XDB::execute("UPDATE emails
437 SET flags = 'active'
438 WHERE flags = 'disable' AND uid = {?}", $this->uid);
439 foreach ($this->emails as &$mail) {
440 if ($mail->disabled) {
441 $mail->active = true;
442 $mail->disabled = false;
443 }
e1547442 444 check_redirect($this);
441c2451 445 }
446 }
447
448 // function get_broken_mx() {{{2
ccdbc270 449
612a2d8a 450 public function get_broken_mx()
ccdbc270 451 {
3083bd93 452 $res = XDB::query("SELECT host, text
8af1d78f
FB
453 FROM mx_watch
454 WHERE state != 'ok'");
120bd636 455 if (!$res->numRows()) {
ccdbc270 456 return array();
457 }
c754cf5b 458 $mxs = $res->fetchAllAssoc();
ccdbc270 459 $mails = array();
460 foreach ($this->emails as &$mail) {
11ed26e5 461 if ($mail->active && strstr($mail->email, '@') !== false) {
ccdbc270 462 list(,$domain) = explode('@', $mail->email);
463 getmxrr($domain, $lcl_mxs);
464 if (empty($lcl_mxs)) {
465 $lcl_mxs = array($domain);
466 }
467 $broken = false;
468 foreach ($mxs as &$mx) {
469 foreach ($lcl_mxs as $lcl) {
c754cf5b 470 if (fnmatch($mx['host'], $lcl)) {
ccdbc270 471 $broken = $mx['text'];
472 break;
473 }
474 }
475 if ($broken) {
3083bd93 476 $mails[] = array('mail' => $mail->email, 'text' => $broken);
f653ff3d 477 break;
ccdbc270 478 }
479 }
480 }
481 }
482 return $mails;
483 }
0337d704 484}
485
aab4661d
VZ
486// class MailStorage {{{1
487class MailStorage {
488 protected $uid;
489 protected $name;
f0882740 490 protected $storage;
aab4661d
VZ
491
492 public function __construct($_uid, $_name)
493 {
494 $this->uid = $_uid;
495 $this->name = $_name;
aab4661d 496
aab4661d
VZ
497 $res = XDB::query("SELECT mail_storage
498 FROM auth_user_md5
499 WHERE user_id = {?}", $this->uid);
f0882740
VZ
500 $this->storages = new FlagSet($res->fetchOneCell());
501 }
aab4661d 502
f0882740
VZ
503 public function disable()
504 {
505 $this->storages->rmFlag($this->name);
506 XDB::execute("UPDATE auth_user_md5
507 SET mail_storage = {?}
508 WHERE user_id = {?}", $this->storages->flags(), $this->uid);
bb0727ea 509 return true;
aab4661d
VZ
510 }
511
512 public function enable()
513 {
f0882740 514 $this->storages->addFlag($this->name);
aab4661d 515 XDB::execute("UPDATE auth_user_md5
f0882740
VZ
516 SET mail_storage = {?}
517 WHERE user_id = {?}", $this->storages->flags(), $this->uid);
bb0727ea
VZ
518 return true;
519 }
520
521 public function active()
522 {
523 return $this->storages->hasFlag($this->name);
aab4661d
VZ
524 }
525}
526
527class MailStorageIMAP extends MailStorage {
528 public function __construct($_uid)
529 {
530 parent::__construct($_uid, 'imap');
531 }
532}
533
bb0727ea
VZ
534class MailStorageGoogleApps extends MailStorage {
535 public function __construct($_uid)
536 {
537 parent::__construct($_uid, 'googleapps');
538 }
11ed26e5 539
bb0727ea
VZ
540 public function disable() {
541 $redirect = new Redirect(S::v('uid'));
542 if (!$redirect->other_active(NULL)) {
543 return false;
544 }
11ed26e5 545
bb0727ea
VZ
546 return parent::disable();
547 }
548}
549
a7de4ef7 550// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 551?>