Merge branch 'platal-0.9.17'
[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
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.
0337d704 30function fix_bestalias($uid)
31{
11ed26e5
VZ
32 $res = XDB::query("SELECT COUNT(*)
33 FROM aliases
34 WHERE id = {?} AND FIND_IN_SET('bestalias', flags) AND type != 'homonyme'",
35 $uid);
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)
44 LIMIT 1", $uid);
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);
58 list($ident, $dom) = explode('@', $em);
59 if ($dom == $globals->mail->domain or $dom == $globals->mail->domain2) {
60 list($ident1) = explode('_', $ident);
61 list($ident) = explode('+', $ident1);
62 }
63 return $ident . '@' . $dom;
0337d704 64}
65
c6310567
FB
66// function isvalid_email_redirection() {{{1
67/** vérifie si une adresse email convient comme adresse de redirection
68 * @param $email l'adresse email a verifier
69 * @return BOOL
70 */
71function isvalid_email_redirection($email)
72{
73 return isvalid_email($email) &&
74 !preg_match("/@(polytechnique\.(org|edu)|melix\.(org|net)|m4x\.org)$/", $email);
75}
76
441c2451 77// class Bogo {{{1
11ed26e5 78// The Bogo class represents a spam filtering level in plat/al architecture.
0337d704 79class Bogo
80{
441c2451 81 // properties {{{2
a3a049fc 82
11ed26e5 83 private $uid;
612a2d8a 84 private $state;
85 private $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams');
0337d704 86
441c2451 87 // constructor {{{2
a3a049fc 88
612a2d8a 89 public function __construct($uid)
0337d704 90 {
3c1e6a1e 91 if (!$uid) {
92 return;
93 }
11ed26e5
VZ
94
95 $this->uid = $uid;
3c1e6a1e 96 $res = XDB::query('SELECT email FROM emails WHERE uid={?} AND flags="filter"', $uid);
97 if ($res->numRows()) {
612a2d8a 98 $this->state = $res->fetchOneCell();
3c1e6a1e 99 } else {
100 $this->state = 'tag_and_drop_spams';
101 $res = XDB::query("INSERT INTO emails (uid,email,rewrite,panne,flags)
102 VALUES ({?},'tag_and_drop_spams','','0000-00-00','filter')", $uid);
103 }
0337d704 104 }
105
441c2451 106 // public function change() {{{2
0337d704 107
11ed26e5 108 public function change($state)
0337d704 109 {
612a2d8a 110 $this->state = is_int($state) ? $this->_states[$state] : $state;
111 XDB::execute('UPDATE emails SET email={?} WHERE uid={?} AND flags = "filter"',
11ed26e5 112 $this->state, $this->uid);
0337d704 113 }
114
441c2451 115 // pubic function level() {{{2
0337d704 116
612a2d8a 117 public function level()
118 {
119 return array_search($this->state, $this->_states);
120 }
0337d704 121}
122
441c2451 123// class Email {{{1
11ed26e5
VZ
124// Represents an "email address" used as final recipient for plat/al-managed
125// addresses; it can be subclasses a Redirection emails (third-party) or as
126// Storage emails (Polytechnique.org).
127abstract class Email
0337d704 128{
11ed26e5 129 protected $uid;
612a2d8a 130
11ed26e5
VZ
131 // Basic email properties; $sufficient indicates if the email can be used as
132 // an unique redirection; $email contains the delivery email address.
133 public $type;
134 public $sufficient;
612a2d8a 135 public $email;
11ed26e5
VZ
136 public $display_email;
137
138 // Redirection status properties.
612a2d8a 139 public $active;
140 public $broken;
441c2451 141 public $disabled;
612a2d8a 142 public $rewrite;
11ed26e5
VZ
143
144 // Redirection bounces stats.
612a2d8a 145 public $panne;
146 public $last;
147 public $panne_level;
0337d704 148
11ed26e5
VZ
149 // Activates the email address as a redirection.
150 public abstract function activate();
151
152 // Deactivates the email address as a redirection.
153 public abstract function deactivate();
154
155 // Sets the rewrite rule for the given address.
156 public abstract function set_rewrite($rewrite);
157
158 // Resets the error counts associated with the redirection.
159 public abstract function clean_errors();
160
161 // Email backend capabilities ('rewrite' refers to From: rewrite for mails
162 // forwarded by Polytechnique.org's MXs; 'removable' indicates if the email
163 // can be definitively removed; 'disable' indicates if the email has a third
164 // status 'disabled' in addition to 'active' and 'inactive').
165 public abstract function has_rewrite();
166 public abstract function is_removable();
167 public abstract function has_disable();
168}
169
170// class EmailRedirection {{{1
171// Implementation of Email for third-party redirection (redirection of emails to
172// external user-supplied addresses).
173class EmailRedirection extends Email
174{
441c2451 175 // constructor {{{2
0337d704 176
11ed26e5 177 public function __construct($uid, $row)
0337d704 178 {
11ed26e5
VZ
179 $this->uid = $uid;
180 $this->sufficient = true;
181
2069538b 182 list($this->email, $flags, $this->rewrite, $this->panne, $this->last, $this->panne_level) = $row;
11ed26e5 183 $this->display_email = $this->email;
441c2451 184 $this->active = ($flags == 'active');
185 $this->broken = ($flags == 'panne');
186 $this->disabled = ($flags == 'disable');
0337d704 187 }
188
441c2451 189 // public function activate() {{{2
0337d704 190
11ed26e5 191 public function activate()
0337d704 192 {
0337d704 193 if (!$this->active) {
dc557110 194 XDB::execute("UPDATE emails
195 SET panne_level = IF(flags = 'panne', panne_level - 1, panne_level),
196 flags = 'active'
11ed26e5 197 WHERE uid={?} AND email={?}", $this->uid, $this->email);
732e5855 198 S::logger()->log("email_on", $this->email.($this->uid!=S::v('uid') ? "(admin on {$this->uid})" : ""));
0337d704 199 $this->active = true;
dc557110 200 $this->broken = false;
0337d704 201 }
202 }
203
441c2451 204 // public function deactivate() {{{2
0337d704 205
11ed26e5 206 public function deactivate()
0337d704 207 {
0337d704 208 if ($this->active) {
08cce2ff 209 XDB::execute("UPDATE emails SET flags =''
11ed26e5 210 WHERE uid={?} AND email={?}", $this->uid, $this->email);
732e5855 211 S::logger()->log("email_off",$this->email.($this->uid != S::v('uid') ? "(admin on {$this->uid})" : "") );
0337d704 212 $this->active = false;
213 }
214 }
612a2d8a 215
11ed26e5 216 // public function set_rewrite() {{{2
0337d704 217
11ed26e5 218 public function set_rewrite($rewrite)
0337d704 219 {
11ed26e5 220 if ($this->rewrite == $rewrite) {
0337d704 221 return;
222 }
11ed26e5
VZ
223 if (!$rewrite || !isvalid_email($rewrite)) {
224 $rewrite = '';
12acff5d 225 }
11ed26e5
VZ
226 XDB::execute('UPDATE emails SET rewrite={?} WHERE uid={?} AND email={?}', $rewrite, $this->uid, $this->email);
227 $this->rewrite = $rewrite;
3c1e6a1e 228 return;
0337d704 229 }
230
11ed26e5 231 // public function clean_errors() {{{2
441c2451 232
11ed26e5 233 public function clean_errors()
441c2451 234 {
235 if (!S::has_perms()) {
236 return false;
237 }
238 $this->panne = 0;
239 $this->panne_level = 0;
240 $this->last = 0;
241 return XDB::execute("UPDATE emails
242 SET panne_level = 0, panne = 0, last = 0
243 WHERE uid = {?} AND email = {?}",
11ed26e5
VZ
244 $this->uid, $this->email);
245 }
246
247 // public function has_rewrite() {{{2
248
249 public function has_rewrite()
250 {
251 return true;
252 }
253
254 // public function is_removable() {{{2
255
256 public function is_removable()
257 {
258 return true;
259 }
260
261 // public function has_disable() {{{2
262
263 public function has_disable()
264 {
265 return true;
441c2451 266 }
0337d704 267}
268
afde0a3a
VZ
269// class EmailStorage {{{1
270// Implementation of Email for email storage backends from Polytechnique.org.
271class EmailStorage extends Email
272{
273 // Shortname to realname mapping for known mail storage backends.
274 private $display_names = array(
275 'imap' => 'Accès de secours aux emails (IMAP)',
690c8519 276 'googleapps' => 'Compte Google Apps',
afde0a3a
VZ
277 );
278
279 // Retrieves the current list of actives storages.
280 private function get_storages()
281 {
282 $res = XDB::query("SELECT mail_storage
283 FROM auth_user_md5
284 WHERE user_id = {?}", $this->uid);
113f6de8 285 return new PlFlagSet($res->fetchOneCell());
afde0a3a
VZ
286 }
287
288 // Updates the list of active storages.
289 private function set_storages($storages)
290 {
291 XDB::execute("UPDATE auth_user_md5
292 SET mail_storage = {?}
77e786e1 293 WHERE user_id = {?}", $storages, $this->uid);
afde0a3a
VZ
294 }
295
296 // Returns the list of allowed storages for the @p user.
297 static public function get_allowed_storages($uid)
298 {
299 global $globals;
300 $storages = array();
301
302 // Google Apps storage is available for users with valid Google Apps account.
303 require_once 'googleapps.inc.php';
304 if ($globals->mailstorage->googleapps_domain &&
305 GoogleAppsAccount::account_status($uid) == 'active') {
306 $storages[] = 'googleapps';
307 }
308
309 // IMAP storage is always visible to administrators, and is allowed for
310 // everyone when the service is marked as 'active'.
311 if ($globals->mailstorage->imap_active || S::has_perms()) {
312 $storages[] = 'imap';
313 }
314
315 return $storages;
316 }
317
318
319 public function __construct($uid, $name)
320 {
321 $this->uid = $uid;
322 $this->email = $name;
323 $this->display_email = (isset($this->display_names[$name]) ? $this->display_names[$name] : $name);
324
325 $storages = $this->get_storages();
326 $this->sufficient = ($name == 'googleapps');
327 $this->active = $storages->hasFlag($name);
328 $this->broken = false;
329 $this->disabled = false;
330 $this->rewrite = '';
331 $this->panne = $this->last = $this->panne_level = 0;
332 }
333
334 public function activate()
335 {
336 if (!$this->active) {
337 $storages = $this->get_storages();
338 $storages->addFlag($this->email);
339 $this->set_storages($storages);
340 $this->active = true;
341 }
342 }
343
344 public function deactivate()
345 {
346 if ($this->active) {
347 $storages = $this->get_storages();
348 $storages->rmFlag($this->email);
349 $this->set_storages($storages);
350 $this->active = false;
351 }
352
353 }
354
355 // Source rewrite can't be enabled for email storage addresses.
356 public function set_rewrite($rewrite) {}
357
358 // Email storage are not supposed to be broken, hence not supposed to be
359 // cleaned-up.
360 public function clean_errors() {}
361
362 // Capabilities.
363 public function has_rewrite() { return false; }
364 public function is_removable() { return false; }
365 public function has_disable() { return false; }
366}
367
441c2451 368// class Redirect {{{1
11ed26e5
VZ
369// Redirect is a placeholder class for an user's active redirections (third-party
370// redirection email, or Polytechnique.org mail storages).
0337d704 371class Redirect
372{
441c2451 373 // properties {{{2
612a2d8a 374
375 private $flag_active = 'active';
376 private $uid;
377
378 public $emails;
379 public $bogo;
0337d704 380
441c2451 381 // constructor {{{2
0337d704 382
612a2d8a 383 public function __construct($_uid)
0337d704 384 {
11ed26e5 385 $this->uid = $_uid;
afde0a3a 386 $this->bogo = new Bogo($_uid);
11ed26e5 387
afde0a3a 388 // Adds third-party email redirections.
612a2d8a 389 $res = XDB::iterRow("SELECT email, flags, rewrite, panne, last, panne_level
390 FROM emails
391 WHERE uid = {?} AND flags != 'filter'", $_uid);
11ed26e5 392 $this->emails = Array();
0337d704 393 while ($row = $res->next()) {
11ed26e5 394 $this->emails[] = new EmailRedirection($_uid, $row);
0337d704 395 }
afde0a3a
VZ
396
397 // Adds local email storage backends.
398 foreach (EmailStorage::get_allowed_storages($_uid) as $storage) {
399 $this->emails[] = new EmailStorage($_uid, $storage);
400 }
0337d704 401 }
402
441c2451 403 // public function other_active() {{{2
0337d704 404
612a2d8a 405 public function other_active($email)
0337d704 406 {
407 foreach ($this->emails as $mail) {
11ed26e5 408 if ($mail->email != $email && $mail->active && $mail->sufficient) {
0337d704 409 return true;
410 }
411 }
412 return false;
413 }
414
441c2451 415 // public function delete_email() {{{2
0337d704 416
612a2d8a 417 public function delete_email($email)
0337d704 418 {
0337d704 419 if (!$this->other_active($email)) {
420 return ERROR_INACTIVE_REDIRECTION;
421 }
08cce2ff 422 XDB::execute('DELETE FROM emails WHERE uid={?} AND email={?}', $this->uid, $email);
732e5855 423 S::logger()->log('email_del',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : ""));
11ed26e5
VZ
424 foreach ($this->emails as $i => $mail) {
425 if ($email == $mail->email) {
0337d704 426 unset($this->emails[$i]);
427 }
3c1e6a1e 428 }
ccdbc270 429 check_redirect($this);
0337d704 430 return SUCCESS;
431 }
432
441c2451 433 // public function add_email() {{{2
612a2d8a 434
435 public function add_email($email)
0337d704 436 {
0337d704 437 $email_stripped = strtolower(trim($email));
438 if (!isvalid_email($email_stripped)) {
439 return ERROR_INVALID_EMAIL;
440 }
441 if (!isvalid_email_redirection($email_stripped)) {
442 return ERROR_LOOP_EMAIL;
443 }
08cce2ff 444 XDB::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->uid, $email);
3c1e6a1e 445 if ($logger = S::v('log', null)) { // may be absent --> step4.php
732e5855 446 S::logger()->log('email_add',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : ""));
0337d704 447 }
3c1e6a1e 448 foreach ($this->emails as $mail) {
449 if ($mail->email == $email_stripped) {
0337d704 450 return SUCCESS;
451 }
3c1e6a1e 452 }
11ed26e5 453 $this->emails[] = new EmailRedirection($this->uid, array($email, 'active', '', '0000-00-00', '0000-00-00', 0));
ca6d07f4 454
455 // security stuff
a7de4ef7 456 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->uid);
ccdbc270 457 check_redirect($this);
0337d704 458 return SUCCESS;
459 }
460
441c2451 461 // public function modify_email() {{{2
0337d704 462
612a2d8a 463 public function modify_email($emails_actifs, $emails_rewrite)
0337d704 464 {
441c2451 465 foreach ($this->emails as &$mail) {
466 if (in_array($mail->email, $emails_actifs)) {
11ed26e5 467 $mail->activate();
3c1e6a1e 468 } else {
11ed26e5 469 $mail->deactivate();
3c1e6a1e 470 }
11ed26e5 471 $mail->set_rewrite($emails_rewrite[$mail->email]);
0337d704 472 }
ccdbc270 473 check_redirect($this);
0337d704 474 }
475
441c2451 476 // public function modify_one_email() {{{2
477
eaf30d86 478 public function modify_one_email($email, $activate)
ccdbc270 479 {
b7582015 480 $allinactive = true;
481 $thisone = false;
8ffa657a 482 foreach ($this->emails as $i=>$mail) {
483 if ($mail->email == $email) {
b7582015 484 $thisone = $i;
8ffa657a 485 }
11ed26e5 486 $allinactive &= !$mail->active || !$mail->sufficient || $mail->email == $email;
8ffa657a 487 }
b7582015 488 if ($thisone === false) {
489 return ERROR_INVALID_EMAIL;
490 }
ccdbc270 491 if ($allinactive || $activate) {
11ed26e5 492 $this->emails[$thisone]->activate();
ccdbc270 493 } else {
11ed26e5 494 $this->emails[$thisone]->deactivate();
ccdbc270 495 }
496 check_redirect($this);
b7582015 497 if ($allinactive && !$activate) {
498 return ERROR_INACTIVE_REDIRECTION;
499 } else {
500 return SUCCESS;
eaf30d86 501 }
8ffa657a 502 }
503
441c2451 504 // public function modify_one_email_redirect() {{{2
505
612a2d8a 506 public function modify_one_email_redirect($email, $redirect)
507 {
441c2451 508 foreach ($this->emails as &$mail) {
612a2d8a 509 if ($mail->email == $email) {
11ed26e5 510 $mail->set_rewrite($redirect);
ccdbc270 511 check_redirect($this);
512 return;
612a2d8a 513 }
514 }
515 }
441c2451 516
11ed26e5 517 // function clean_errors() {{{2
441c2451 518
11ed26e5 519 public function clean_errors($email)
441c2451 520 {
521 foreach ($this->emails as &$mail) {
522 if ($mail->email == $email) {
e1547442 523 check_redirect($this);
11ed26e5 524 return $mail->clean_errors();
441c2451 525 }
526 }
527 return false;
528 }
529
530 // function disable() {{{2
531
532 public function disable()
533 {
534 XDB::execute("UPDATE emails
535 SET flags = 'disable'
536 WHERE flags = 'active' AND uid = {?}", $this->uid);
537 foreach ($this->emails as &$mail) {
11ed26e5 538 if ($mail->active && $mail->has_disable()) {
441c2451 539 $mail->disabled = true;
540 $mail->active = false;
541 }
542 }
e1547442 543 check_redirect($this);
441c2451 544 }
545
546 // function enable() {{{2
547
548 public function enable()
549 {
550 XDB::execute("UPDATE emails
551 SET flags = 'active'
552 WHERE flags = 'disable' AND uid = {?}", $this->uid);
553 foreach ($this->emails as &$mail) {
554 if ($mail->disabled) {
555 $mail->active = true;
556 $mail->disabled = false;
557 }
e1547442 558 check_redirect($this);
441c2451 559 }
560 }
561
562 // function get_broken_mx() {{{2
ccdbc270 563
612a2d8a 564 public function get_broken_mx()
ccdbc270 565 {
3083bd93 566 $res = XDB::query("SELECT host, text
8af1d78f
FB
567 FROM mx_watch
568 WHERE state != 'ok'");
120bd636 569 if (!$res->numRows()) {
ccdbc270 570 return array();
571 }
c754cf5b 572 $mxs = $res->fetchAllAssoc();
ccdbc270 573 $mails = array();
574 foreach ($this->emails as &$mail) {
11ed26e5 575 if ($mail->active && strstr($mail->email, '@') !== false) {
ccdbc270 576 list(,$domain) = explode('@', $mail->email);
577 getmxrr($domain, $lcl_mxs);
578 if (empty($lcl_mxs)) {
579 $lcl_mxs = array($domain);
580 }
581 $broken = false;
582 foreach ($mxs as &$mx) {
583 foreach ($lcl_mxs as $lcl) {
c754cf5b 584 if (fnmatch($mx['host'], $lcl)) {
ccdbc270 585 $broken = $mx['text'];
586 break;
587 }
588 }
589 if ($broken) {
3083bd93 590 $mails[] = array('mail' => $mail->email, 'text' => $broken);
f653ff3d 591 break;
ccdbc270 592 }
593 }
594 }
595 }
596 return $mails;
597 }
e97e9b8f
VZ
598
599 // function active_emails() {{{2
600
601 public function active_emails()
602 {
603 $emails = array();
604 foreach ($this->emails as $mail) {
605 if ($mail->active) {
606 $emails[] = $mail;
607 }
608 }
609 return $emails;
610 }
45282934
VZ
611
612 // function get_uid() {{{2
613
614 public function get_uid()
615 {
616 return $this->uid;
617 }
0337d704 618}
619
a7de4ef7 620// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 621?>