Adds XSRF protection to the Carnet module.
[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
afde0a3a
VZ
260// class EmailStorage {{{1
261// Implementation of Email for email storage backends from Polytechnique.org.
262class EmailStorage extends Email
263{
264 // Shortname to realname mapping for known mail storage backends.
265 private $display_names = array(
266 'imap' => 'Accès de secours aux emails (IMAP)',
690c8519 267 'googleapps' => 'Compte Google Apps',
afde0a3a
VZ
268 );
269
270 // Retrieves the current list of actives storages.
271 private function get_storages()
272 {
273 $res = XDB::query("SELECT mail_storage
274 FROM auth_user_md5
275 WHERE user_id = {?}", $this->uid);
276 return new FlagSet($res->fetchOneCell());
277 }
278
279 // Updates the list of active storages.
280 private function set_storages($storages)
281 {
282 XDB::execute("UPDATE auth_user_md5
283 SET mail_storage = {?}
284 WHERE user_id = {?}", $storages->flags(), $this->uid);
285 }
286
287 // Returns the list of allowed storages for the @p user.
288 static public function get_allowed_storages($uid)
289 {
290 global $globals;
291 $storages = array();
292
293 // Google Apps storage is available for users with valid Google Apps account.
294 require_once 'googleapps.inc.php';
295 if ($globals->mailstorage->googleapps_domain &&
296 GoogleAppsAccount::account_status($uid) == 'active') {
297 $storages[] = 'googleapps';
298 }
299
300 // IMAP storage is always visible to administrators, and is allowed for
301 // everyone when the service is marked as 'active'.
302 if ($globals->mailstorage->imap_active || S::has_perms()) {
303 $storages[] = 'imap';
304 }
305
306 return $storages;
307 }
308
309
310 public function __construct($uid, $name)
311 {
312 $this->uid = $uid;
313 $this->email = $name;
314 $this->display_email = (isset($this->display_names[$name]) ? $this->display_names[$name] : $name);
315
316 $storages = $this->get_storages();
317 $this->sufficient = ($name == 'googleapps');
318 $this->active = $storages->hasFlag($name);
319 $this->broken = false;
320 $this->disabled = false;
321 $this->rewrite = '';
322 $this->panne = $this->last = $this->panne_level = 0;
323 }
324
325 public function activate()
326 {
327 if (!$this->active) {
328 $storages = $this->get_storages();
329 $storages->addFlag($this->email);
330 $this->set_storages($storages);
331 $this->active = true;
332 }
333 }
334
335 public function deactivate()
336 {
337 if ($this->active) {
338 $storages = $this->get_storages();
339 $storages->rmFlag($this->email);
340 $this->set_storages($storages);
341 $this->active = false;
342 }
343
344 }
345
346 // Source rewrite can't be enabled for email storage addresses.
347 public function set_rewrite($rewrite) {}
348
349 // Email storage are not supposed to be broken, hence not supposed to be
350 // cleaned-up.
351 public function clean_errors() {}
352
353 // Capabilities.
354 public function has_rewrite() { return false; }
355 public function is_removable() { return false; }
356 public function has_disable() { return false; }
357}
358
441c2451 359// class Redirect {{{1
11ed26e5
VZ
360// Redirect is a placeholder class for an user's active redirections (third-party
361// redirection email, or Polytechnique.org mail storages).
0337d704 362class Redirect
363{
441c2451 364 // properties {{{2
612a2d8a 365
366 private $flag_active = 'active';
367 private $uid;
368
369 public $emails;
370 public $bogo;
0337d704 371
441c2451 372 // constructor {{{2
0337d704 373
612a2d8a 374 public function __construct($_uid)
0337d704 375 {
11ed26e5 376 $this->uid = $_uid;
afde0a3a 377 $this->bogo = new Bogo($_uid);
11ed26e5 378
afde0a3a 379 // Adds third-party email redirections.
612a2d8a 380 $res = XDB::iterRow("SELECT email, flags, rewrite, panne, last, panne_level
381 FROM emails
382 WHERE uid = {?} AND flags != 'filter'", $_uid);
11ed26e5 383 $this->emails = Array();
0337d704 384 while ($row = $res->next()) {
11ed26e5 385 $this->emails[] = new EmailRedirection($_uid, $row);
0337d704 386 }
afde0a3a
VZ
387
388 // Adds local email storage backends.
389 foreach (EmailStorage::get_allowed_storages($_uid) as $storage) {
390 $this->emails[] = new EmailStorage($_uid, $storage);
391 }
0337d704 392 }
393
441c2451 394 // public function other_active() {{{2
0337d704 395
612a2d8a 396 public function other_active($email)
0337d704 397 {
398 foreach ($this->emails as $mail) {
11ed26e5 399 if ($mail->email != $email && $mail->active && $mail->sufficient) {
0337d704 400 return true;
401 }
402 }
403 return false;
404 }
405
441c2451 406 // public function delete_email() {{{2
0337d704 407
612a2d8a 408 public function delete_email($email)
0337d704 409 {
0337d704 410 if (!$this->other_active($email)) {
411 return ERROR_INACTIVE_REDIRECTION;
412 }
08cce2ff 413 XDB::execute('DELETE FROM emails WHERE uid={?} AND email={?}', $this->uid, $email);
cab08090 414 $_SESSION['log']->log('email_del',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : ""));
11ed26e5
VZ
415 foreach ($this->emails as $i => $mail) {
416 if ($email == $mail->email) {
0337d704 417 unset($this->emails[$i]);
418 }
3c1e6a1e 419 }
ccdbc270 420 check_redirect($this);
0337d704 421 return SUCCESS;
422 }
423
441c2451 424 // public function add_email() {{{2
612a2d8a 425
426 public function add_email($email)
0337d704 427 {
0337d704 428 $email_stripped = strtolower(trim($email));
429 if (!isvalid_email($email_stripped)) {
430 return ERROR_INVALID_EMAIL;
431 }
432 if (!isvalid_email_redirection($email_stripped)) {
433 return ERROR_LOOP_EMAIL;
434 }
08cce2ff 435 XDB::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->uid, $email);
3c1e6a1e 436 if ($logger = S::v('log', null)) { // may be absent --> step4.php
437 $logger->log('email_add',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : ""));
0337d704 438 }
3c1e6a1e 439 foreach ($this->emails as $mail) {
440 if ($mail->email == $email_stripped) {
0337d704 441 return SUCCESS;
442 }
3c1e6a1e 443 }
11ed26e5 444 $this->emails[] = new EmailRedirection($this->uid, array($email, 'active', '', '0000-00-00', '0000-00-00', 0));
ca6d07f4 445
446 // security stuff
a7de4ef7 447 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->uid);
ccdbc270 448 check_redirect($this);
0337d704 449 return SUCCESS;
450 }
451
441c2451 452 // public function modify_email() {{{2
0337d704 453
612a2d8a 454 public function modify_email($emails_actifs, $emails_rewrite)
0337d704 455 {
441c2451 456 foreach ($this->emails as &$mail) {
457 if (in_array($mail->email, $emails_actifs)) {
11ed26e5 458 $mail->activate();
3c1e6a1e 459 } else {
11ed26e5 460 $mail->deactivate();
3c1e6a1e 461 }
11ed26e5 462 $mail->set_rewrite($emails_rewrite[$mail->email]);
0337d704 463 }
ccdbc270 464 check_redirect($this);
0337d704 465 }
466
441c2451 467 // public function modify_one_email() {{{2
468
eaf30d86 469 public function modify_one_email($email, $activate)
ccdbc270 470 {
b7582015 471 $allinactive = true;
472 $thisone = false;
8ffa657a 473 foreach ($this->emails as $i=>$mail) {
474 if ($mail->email == $email) {
b7582015 475 $thisone = $i;
8ffa657a 476 }
11ed26e5 477 $allinactive &= !$mail->active || !$mail->sufficient || $mail->email == $email;
8ffa657a 478 }
b7582015 479 if ($thisone === false) {
480 return ERROR_INVALID_EMAIL;
481 }
ccdbc270 482 if ($allinactive || $activate) {
11ed26e5 483 $this->emails[$thisone]->activate();
ccdbc270 484 } else {
11ed26e5 485 $this->emails[$thisone]->deactivate();
ccdbc270 486 }
487 check_redirect($this);
b7582015 488 if ($allinactive && !$activate) {
489 return ERROR_INACTIVE_REDIRECTION;
490 } else {
491 return SUCCESS;
eaf30d86 492 }
8ffa657a 493 }
494
441c2451 495 // public function modify_one_email_redirect() {{{2
496
612a2d8a 497 public function modify_one_email_redirect($email, $redirect)
498 {
441c2451 499 foreach ($this->emails as &$mail) {
612a2d8a 500 if ($mail->email == $email) {
11ed26e5 501 $mail->set_rewrite($redirect);
ccdbc270 502 check_redirect($this);
503 return;
612a2d8a 504 }
505 }
506 }
441c2451 507
11ed26e5 508 // function clean_errors() {{{2
441c2451 509
11ed26e5 510 public function clean_errors($email)
441c2451 511 {
512 foreach ($this->emails as &$mail) {
513 if ($mail->email == $email) {
e1547442 514 check_redirect($this);
11ed26e5 515 return $mail->clean_errors();
441c2451 516 }
517 }
518 return false;
519 }
520
521 // function disable() {{{2
522
523 public function disable()
524 {
525 XDB::execute("UPDATE emails
526 SET flags = 'disable'
527 WHERE flags = 'active' AND uid = {?}", $this->uid);
528 foreach ($this->emails as &$mail) {
11ed26e5 529 if ($mail->active && $mail->has_disable()) {
441c2451 530 $mail->disabled = true;
531 $mail->active = false;
532 }
533 }
e1547442 534 check_redirect($this);
441c2451 535 }
536
537 // function enable() {{{2
538
539 public function enable()
540 {
541 XDB::execute("UPDATE emails
542 SET flags = 'active'
543 WHERE flags = 'disable' AND uid = {?}", $this->uid);
544 foreach ($this->emails as &$mail) {
545 if ($mail->disabled) {
546 $mail->active = true;
547 $mail->disabled = false;
548 }
e1547442 549 check_redirect($this);
441c2451 550 }
551 }
552
553 // function get_broken_mx() {{{2
ccdbc270 554
612a2d8a 555 public function get_broken_mx()
ccdbc270 556 {
3083bd93 557 $res = XDB::query("SELECT host, text
8af1d78f
FB
558 FROM mx_watch
559 WHERE state != 'ok'");
120bd636 560 if (!$res->numRows()) {
ccdbc270 561 return array();
562 }
c754cf5b 563 $mxs = $res->fetchAllAssoc();
ccdbc270 564 $mails = array();
565 foreach ($this->emails as &$mail) {
11ed26e5 566 if ($mail->active && strstr($mail->email, '@') !== false) {
ccdbc270 567 list(,$domain) = explode('@', $mail->email);
568 getmxrr($domain, $lcl_mxs);
569 if (empty($lcl_mxs)) {
570 $lcl_mxs = array($domain);
571 }
572 $broken = false;
573 foreach ($mxs as &$mx) {
574 foreach ($lcl_mxs as $lcl) {
c754cf5b 575 if (fnmatch($mx['host'], $lcl)) {
ccdbc270 576 $broken = $mx['text'];
577 break;
578 }
579 }
580 if ($broken) {
3083bd93 581 $mails[] = array('mail' => $mail->email, 'text' => $broken);
f653ff3d 582 break;
ccdbc270 583 }
584 }
585 }
586 }
587 return $mails;
588 }
e97e9b8f
VZ
589
590 // function active_emails() {{{2
591
592 public function active_emails()
593 {
594 $emails = array();
595 foreach ($this->emails as $mail) {
596 if ($mail->active) {
597 $emails[] = $mail;
598 }
599 }
600 return $emails;
601 }
45282934
VZ
602
603 // function get_uid() {{{2
604
605 public function get_uid()
606 {
607 return $this->uid;
608 }
0337d704 609}
610
a7de4ef7 611// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 612?>