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