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