Moving to GitHub.
[platal.git] / include / emails.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2014 Polytechnique.org *
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
22 define('SUCCESS', 1);
23 define('ERROR_INACTIVE_REDIRECTION', 2);
24 define('ERROR_INVALID_EMAIL', 3);
25 define('ERROR_LOOP_EMAIL', 4);
26
27 // Checks if an email update is required in MLs and aliases.
28 // This occurs when the user don't have email permissions and her email has changed.
29 function require_email_update(User $user, $new_email)
30 {
31 Platal::assert(!is_null($user), 'User cannot be null.');
32
33 $is_new = !$user->checkPerms(User::PERM_MAIL) && $new_email != strtolower($user->email);
34 if ($new_email && $is_new) {
35 $already = XDB::fetchOneCell('SELECT hruid
36 FROM accounts
37 WHERE email = {?} AND uid != {?}',
38 $new_email, $user->id());
39 if ($already) {
40 Platal::page()->trigError("L'email ne peut pas être utilisé pour ce compte car il correspond déjà au compte : "
41 . $already . ". Si l'utilisateur courant et cette personne ne sont en fait qu'une "
42 . "seul et même personne, ou en cas de problème, contacter : contact@polytechnique.org");
43 return false;
44 }
45 }
46 return $is_new;
47 }
48
49 function format_email_alias($email)
50 {
51 if ($user = User::getSilent($email)) {
52 return $user->forlifeEmail();
53 }
54 if (isvalid_email($email)) {
55 return $email;
56 }
57 return null;
58 }
59
60 function add_to_list_alias($email, $local_part, $domain, $type = 'alias')
61 {
62 $email = format_email_alias($email);
63 if (is_null($email)) {
64 return false;
65 }
66
67 XDB::execute('INSERT IGNORE INTO email_virtual (email, domain, redirect, type)
68 SELECT {?}, id, {?}, {?}
69 FROM email_virtual_domains
70 WHERE name = {?}',
71 $local_part, $email, $type, $domain);
72 return true;
73 }
74
75 function delete_from_list_alias($email, $local_part, $domain, $type = 'alias')
76 {
77 $email = format_email_alias($email);
78 if (is_null($email)) {
79 return false;
80 }
81
82 XDB::execute('DELETE v
83 FROM email_virtual AS v
84 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
85 INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id)
86 WHERE v.email = {?} AND d.name = {?} AND v.redirect = {?} AND type = {?}',
87 $local_part, $domain, $email, $type);
88 return true;
89 }
90
91 function update_list_alias($email, $former_email, $local_part, $domain, $type = 'alias')
92 {
93 $email = format_email_alias($email);
94 if (is_null($email)) {
95 return false;
96 }
97
98 XDB::execute('UPDATE email_virtual AS v
99 INNER JOIN email_virtual_domains AS d ON (v.domain = d.id)
100 SET v.redirect = {?}
101 WHERE v.redirect = {?} AND d.name = {?} AND v.email = {?} AND v.type = {?}',
102 $email, $former_email, $domain, $local_part, $type);
103 return true;
104 }
105
106 // Updates an email in all aliases (groups and events).
107 function update_alias_user($former_email, $new_email)
108 {
109 XDB::execute('UPDATE email_virtual
110 SET redirect = {?}
111 WHERE redirect = {?} AND (type = \'alias\' OR type = \'event\')',
112 $new_email, $former_email);
113 }
114
115 function list_alias_members($local_part, $domain)
116 {
117 $emails = XDB::fetchColumn('SELECT DISTINCT(redirect)
118 FROM email_virtual AS v
119 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
120 INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id)
121 WHERE v.email = {?} AND d.name = {?} AND type = \'alias\'',
122 $local_part, $domain);
123
124 $users = array();
125 $nonusers = array();
126 foreach ($emails as $email) {
127 if ($user = User::getSilent($email)) {
128 $users[] = $user;
129 } else {
130 $nonusers[] = $email;
131 }
132 }
133
134 return array(
135 'users' => $users,
136 'nonusers' => $nonusers
137 );
138 }
139
140 function delete_list_alias($local_part, $domain)
141 {
142 XDB::execute('DELETE v
143 FROM email_virtual AS v
144 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
145 INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id)
146 WHERE v.email = {?} AND d.name = {?} AND type = \'alias\'',
147 $local_part, $domain);
148 }
149
150 function iterate_list_alias($domain)
151 {
152 return XDB::fetchColumn('SELECT CONCAT(v.email, \'@\', m.name)
153 FROM email_virtual AS v
154 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
155 WHERE m.name = {?} AND v.type = \'alias\'
156 GROUP BY v.email',
157 $domain);
158 }
159
160 function create_list($local_part, $domain)
161 {
162 global $globals;
163
164 $redirect = $domain . '_' . $local_part . '+';
165 foreach(array('post', 'owner', 'admin', 'bounces', 'unsubscribe') as $suffix) {
166 XDB::execute('INSERT IGNORE INTO email_virtual (email, domain, redirect, type)
167 SELECT {?}, id, {?}, \'list\'
168 FROM email_virtual_domains
169 WHERE name = {?}',
170 ($suffix == 'post') ? $local_part : $local_part . '-' . $suffix,
171 $redirect . $suffix . '@' . $globals->lists->redirect_domain, $domain);
172 }
173 }
174
175 function delete_list($local_part, $domain)
176 {
177 global $globals;
178
179 $redirect = $domain . '_' . $local_part . '+';
180 foreach(array('post', 'owner', 'admin', 'bounces', 'unsubscribe') as $suffix) {
181 XDB::execute('DELETE FROM email_virtual
182 WHERE redirect = {?} AND type = \'list\'',
183 $redirect . $suffix . '@' . $globals->lists->redirect_domain);
184 }
185 }
186
187 function list_exist($local_part, $domain)
188 {
189 return XDB::fetchOneCell('SELECT COUNT(*)
190 FROM email_virtual AS v
191 INNER JOIN email_virtual_domains AS m ON (v.domain = m.id)
192 INNER JOIN email_virtual_domains AS d ON (m.id = d.aliasing)
193 WHERE v.email = {?} AND d.name = {?}',
194 $local_part, $domain);
195 }
196
197 // function mark_broken_email() {{{1
198 function mark_broken_email($email, $admin = false)
199 {
200 $email = valide_email($email);
201 if (empty($email) || $email == '@') {
202 return;
203 }
204
205 $user = XDB::fetchOneAssoc('SELECT r1.uid, a.hruid, a.full_name, r1.broken_level != 0 AS broken, COUNT(r2.uid) AS nb_mails,
206 s.email AS alias, DATE_ADD(r1.last, INTERVAL 14 DAY) < CURDATE() as notify
207 FROM email_redirect_account AS r1
208 INNER JOIN accounts AS a ON (a.uid = r1.uid)
209 INNER JOIN email_source_account AS s ON (a.uid = s.uid AND s.flags = \'bestalias\')
210 LEFT JOIN email_redirect_account AS r2 ON (a.uid = r2.uid AND r1.redirect != r2.redirect AND
211 r2.broken_level = 0 AND r2.flags = \'active\' AND
212 (r2.type = \'smtp\' OR r2.type = \'googleapps\'))
213 WHERE r1.redirect = {?}
214 GROUP BY r1.uid', $email);
215
216 if ($user) {
217 // Mark address as broken.
218 if (!$user['broken']) {
219 XDB::execute('UPDATE email_redirect_account
220 SET broken_date = NOW(), last = NOW(), broken_level = 1
221 WHERE redirect = {?}', $email);
222 } elseif ($admin) {
223 XDB::execute('UPDATE email_redirect_account
224 SET last = CURDATE(), broken_level = broken_level + 1
225 WHERE redirect = {?} AND DATE_ADD(last, INTERVAL 14 DAY) < CURDATE()',
226 $email);
227 } else {
228 XDB::execute('UPDATE email_redirect_account
229 SET broken_level = 1
230 WHERE redirect = {?} AND broken_level = 0', $email);
231 }
232 }
233
234 return $user;
235 }
236
237 // function fix_bestalias() {{{1
238 // Checks for an existing 'bestalias' among the the current user's aliases, and
239 // eventually selects a new bestalias when required.
240 function fix_bestalias(User $user)
241 {
242 // First check if the bestalias is properly set.
243 $alias_count = XDB::fetchOneCell('SELECT COUNT(*)
244 FROM email_source_account
245 WHERE uid = {?} AND FIND_IN_SET(\'bestalias\', flags) AND expire IS NULL',
246 $user->id());
247
248 if ($alias_count > 1) {
249 // If too many bestaliases, delete the bestalias flag from all this
250 // user's emails (this should never happen).
251 XDB::execute("UPDATE email_source_account
252 SET flags = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', flags, ','), ',bestalias,', ','))
253 WHERE uid = {?}",
254 $user->id());
255 }
256 if ($alias_count != 1) {
257 // If no bestalias is selected, we choose the shortest email which is not
258 // related to a usage name and contains a '.'.
259 XDB::execute("UPDATE email_source_account
260 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'bestalias')
261 WHERE uid = {?} AND expire IS NULL
262 ORDER BY NOT FIND_IN_SET('usage', flags), email LIKE '%.%', LENGTH(email)
263 LIMIT 1",
264 $user->id());
265 }
266
267 // First check if best_domain is properly set.
268 $domain_count = XDB::fetchOneCell('SELECT COUNT(*)
269 FROM accounts AS a
270 INNER JOIN email_source_account AS s ON (s.uid = a.uid AND FIND_IN_SET(\'bestalias\', s.flags))
271 INNER JOIN email_virtual_domains AS d ON (d.id = a.best_domain)
272 INNER JOIN email_virtual_domains AS m ON (d.aliasing = m.id)
273 INNER JOIN email_virtual_domains AS v ON (v.aliasing = m.id AND v.id = s.domain)
274 WHERE a.uid = {?} AND (m.name = {?} OR m.name = {?})',
275 $user->id(), $user->mainEmailDomain(), Platal::globals()->mail->alias_dom);
276
277 if ($domain_count == 0) {
278 XDB::execute('UPDATE accounts AS a
279 INNER JOIN email_source_account AS s ON (s.uid = a.uid AND FIND_IN_SET(\'bestalias\', s.flags))
280 INNER JOIN email_virtual_domains AS d ON (d.aliasing = s.domain AND (d.name = {?} OR d.name = {?}))
281 SET a.best_domain = d.id
282 WHERE a.uid = {?}',
283 $user->mainEmailDomain(), Platal::globals()->mail->alias_dom, $user->id());
284 }
285
286
287 }
288
289 // function valide_email() {{{1
290 // Returns a cleaned-up version of the @p email string. It removes garbage
291 // characters, and determines the canonical form (without _ and +) for
292 // Polytechnique.org email addresses.
293 function valide_email($str)
294 {
295 global $globals;
296
297 $em = trim(rtrim($str));
298 $em = str_replace('<', '', $em);
299 $em = str_replace('>', '', $em);
300 if (strpos($em, '@') === false) {
301 return;
302 }
303 list($ident, $dom) = explode('@', $em);
304 if (User::isMainMailDomain($dom)) {
305 list($ident1) = explode('_', $ident);
306 list($ident) = explode('+', $ident1);
307 }
308 return $ident . '@' . $dom;
309 }
310
311 // function isvalid_email_redirection() {{{1
312 /** Checks if an email is a suitable redirection.
313 * @param $email the email to check
314 * @param $user the user asking for the redirection
315 * @return BOOL
316 */
317 function isvalid_email_redirection($email, User $user)
318 {
319 $valid = isvalid_email($email) && User::isForeignEmailAddress($email);
320 if (!$user->hasProfile() || ($user->profile()->grad_year > date('Y') - 3)) {
321 return $valid && !preg_match("/@polytechnique\.edu$/", $email);
322 } else {
323 return $valid;
324 }
325 }
326
327 // function ids_from_mails() {{{1
328 // Converts an array of emails to an array of email => uid, where email is the
329 // given email when we found a matching user.
330 function ids_from_mails(array $emails)
331 {
332 // Removes duplicates, if any.
333 $emails = array_unique($emails);
334
335 // Formats and splits by domain type (locally managed or external) emails.
336 $main_domain_emails = array();
337 $aux_domain_emails = array();
338 $other_emails = array();
339 foreach ($emails as $email) {
340 if (strpos($email, '@') === false) {
341 $main_domain_emails[] = $email;
342 } else {
343 if (User::isForeignEmailAddress($email)) {
344 $other_emails[$email] = strtolower($user . '@' . $domain);
345 } else {
346 list($local_part, $domain) = explode('@', $email);
347 list($local_part) = explode('+', $local_part);
348 list($local_part) = explode('_', $local_part);
349 if (User::isMainMailDomain($domain)) {
350 $main_domain_emails[$email] = strtolower($local_part);
351 } elseif (User::isAliasMailDomain($domain)) {
352 $aux_domain_emails[$email] = strtolower($local_part);
353 }
354 }
355 }
356 }
357
358 // Retrieves emails from our domains.
359 $main_domain_uids = XDB::fetchAllAssoc('email',
360 'SELECT email, uid
361 FROM email_source_account
362 WHERE email IN {?} AND type != \'alias_aux\'',
363 array_unique($main_domain_emails));
364 $aux_domain_uids = XDB::fetchAllAssoc('email',
365 'SELECT email, uid
366 FROM email_source_account
367 WHERE email IN {?} AND type = \'alias_aux\'',
368 array_unique($aux_domain_emails));
369
370 // Retrieves emails from redirections.
371 $other_uids = XDB::fetchAllAssoc('redirect',
372 'SELECT redirect, uid
373 FROM email_redirect_account
374 WHERE redirect IN {?}',
375 array_unique($other_emails));
376
377 // Associates given emails with the corresponding uid.
378 $uids = array();
379 foreach ($main_domain_emails as $email => $key) {
380 $uids[$email] = $main_domain_uids[$key];
381 }
382 foreach ($aux_domain_emails as $email => $key) {
383 $uids[$email] = $aux_domain_uids[$key];
384 }
385 foreach ($other_emails as $email => $key) {
386 $uids[$email] = $other_uids[$key];
387 }
388
389 return array_unique($uids);
390 }
391
392 // class Bogo {{{1
393 // The Bogo class represents a spam filtering level in plat/al architecture.
394 class Bogo
395 {
396 const MAIN_DEFAULT = 'default';
397 const IMAP_DEFAULT = 'let_spams';
398
399 public static $states = array(
400 0 => 'default',
401 1 => 'let_spams',
402 2 => 'tag_spams',
403 3 => 'tag_and_drop_spams',
404 4 => 'drop_spams'
405 );
406
407 private $user;
408 public $state;
409 public $single_state;
410 public $redirections;
411 public $single_redirection;
412
413 public function __construct(User $user)
414 {
415 if (!$user) {
416 return;
417 }
418
419 $this->user = &$user;
420 $res = XDB::fetchOneAssoc('SELECT COUNT(DISTINCT(action)) AS action_count, COUNT(redirect) AS redirect_count, action
421 FROM email_redirect_account
422 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\') AND flags = \'active\'
423 GROUP BY uid',
424 $user->id());
425 if ($res['redirect_count'] == 0) {
426 return;
427 }
428
429 $this->single_redirection = ($res['redirect_count'] == 1);
430 $this->redirections = XDB::fetchAllAssoc('SELECT IF(type = \'googleapps\', type, redirect) AS redirect, type, action
431 FROM email_redirect_account
432 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')
433 ORDER BY type, redirect',
434 $user->id());
435
436 foreach ($this->redirections AS &$redirection) {
437 $redirection['filter'] = array_search($redirection['action'], self::$states);
438 }
439 if ($res['action_count'] == 1) {
440 $this->state = array_search($res['action'], self::$states);
441 $this->single_state = true;
442 } else {
443 $this->single_state = $this->state = false;
444 }
445 }
446
447 public function changeAll($state)
448 {
449 Platal::assert($state >= 0 && $state < count(self::$states), 'Unknown antispam level.');
450
451 $this->state = $state;
452 XDB::execute('UPDATE email_redirect_account
453 SET action = {?}
454 WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')',
455 self::$states[$this->state], $this->user->id());
456 }
457
458 public function change($redirection, $state)
459 {
460 Platal::assert($state >= 0 && $state < count(self::$states), 'Unknown antispam level.');
461
462 XDB::execute('UPDATE email_redirect_account
463 SET action = {?}
464 WHERE uid = {?} AND (type = {?} OR redirect = {?})',
465 self::$states[$state], $this->user->id(), $redirection, $redirection);
466 }
467 }
468
469 // class Email {{{1
470 // Represents an "email address" used as final recipient for plat/al-managed
471 // addresses.
472 class Email
473 {
474 // Lists fields to load automatically.
475 static private $field_names = array('rewrite', 'type', 'action', 'broken_date', 'broken_level', 'last', 'hash', 'allow_rewrite');
476
477 // Shortname to realname mapping for known mail storage backends.
478 static private $display_names = array(
479 'imap' => 'Accès de secours aux emails (IMAP)',
480 'googleapps' => 'Compte Google Apps',
481 );
482 static private $storage_domains = array(
483 'imap' => 'imap',
484 'googleapps' => 'g'
485 );
486
487 private $user;
488
489 // Basic email properties; $sufficient indicates if the email can be used as
490 // an unique redirection; $redirect contains the delivery email address.
491 public $id;
492 public $type;
493 public $sufficient;
494 public $email;
495 public $display_email;
496 public $domain;
497 public $action;
498 public $filter_level;
499
500 // Redirection status properties.
501 public $active;
502 public $inactive;
503 public $broken;
504 public $disabled;
505 public $rewrite;
506 public $allow_rewrite;
507 public $hash;
508
509 // Redirection bounces stats.
510 public $last;
511 public $broken_level;
512 public $broken_date;
513
514 public function __construct(User $user, array $row)
515 {
516 foreach (self::$field_names as $field) {
517 if (array_key_exists($field, $row)) {
518 $this->$field = $row[$field];
519 }
520 }
521 $this->email = $row['redirect'];
522
523 if (array_key_exists($this->type, Email::$display_names)) {
524 $this->display_email = self::$display_names[$this->type];
525 } else {
526 $this->display_email = $this->email;
527 }
528 foreach (array('active', 'inactive', 'broken', 'disabled') as $status) {
529 $this->$status = ($status == $row['flags']);
530 }
531 $this->sufficient = ($this->type == 'smtp' || $this->type == 'googleapps');
532 $this->filter_level = ($this->type == 'imap') ? null : array_search($this->action, Bogo::$states);
533 if (array_key_exists($this->type , self::$storage_domains)) {
534 $this->id = $this->type;
535 } else {
536 $this->id = str_replace(array('@', '.'), array('_at_', '_dot_'), $this->email);
537 }
538 $this->user = &$user;
539 }
540
541 // Activates the email address as a redirection.
542 public function activate()
543 {
544 if (!$this->active) {
545 if (in_array($this->type, self::get_allowed_storages($this->user))) {
546 self::activate_storage($this->user, $this->type, $this->action);
547 } else {
548 XDB::execute('UPDATE email_redirect_account
549 SET broken_level = IF(flags = \'broken\', broken_level - 1, broken_level), flags = \'active\'
550 WHERE uid = {?} AND redirect = {?}',
551 $this->user->id(), $this->email);
552 }
553 S::logger()->log('email_on', $this->email . ($this->user->id() != S::v('uid') ? "(admin on {$this->user->login()})" : ''));
554 $this->disabled = false;
555 $this->broken = false;
556 $this->inactive = false;
557 $this->active = true;
558 }
559 }
560
561 // Deactivates the email address as a redirection.
562 public function deactivate()
563 {
564 if ($this->active) {
565 if (in_array($this->type, self::get_allowed_storages($this->user))) {
566 self::deactivate_storage($this->user, $this->type);
567 } else {
568 XDB::execute('UPDATE email_redirect_account
569 SET flags = \'inactive\'
570 WHERE uid = {?} AND redirect = {?}',
571 $this->user->id(), $this->email);
572 }
573 S::logger()->log('email_off', $this->email . ($this->user->id() != S::v('uid') ? "(admin on {$this->user->login()})" : "") );
574 $this->inactive = true;
575 $this->active = false;
576 }
577 }
578
579
580 // Sets the rewrite rule for the given address.
581 public function set_rewrite($rewrite)
582 {
583 if ($this->type != 'smtp' || $this->rewrite == $rewrite) {
584 return;
585 }
586 if (!$rewrite || !isvalid_email($rewrite)) {
587 $rewrite = '';
588 }
589 XDB::execute('UPDATE email_redirect_account
590 SET rewrite = {?}
591 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
592 $rewrite, $this->user->id(), $this->email);
593 $this->rewrite = $rewrite;
594 if (!$this->allow_rewrite) {
595 global $globals;
596 if (empty($this->hash)) {
597 $this->hash = rand_url_id();
598 XDB::execute('UPDATE email_redirect_account
599 SET hash = {?}
600 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
601 $this->hash, $this->user->id(), $this->email);
602 }
603 $mail = new PlMailer('emails/rewrite-in.mail.tpl');
604 $mail->assign('mail', $this);
605 $mail->assign('user', $this->user);
606 $mail->assign('baseurl', $globals->baseurl);
607 $mail->assign('sitename', $globals->core->sitename);
608 $mail->assign('to', $this->email);
609 $mail->send($this->user->isEmailFormatHtml());
610 }
611 }
612
613
614 // Resets the error counts associated with the redirection.
615 public function clean_errors()
616 {
617 if ($this->type != 'smtp') {
618 return;
619 }
620 if (!S::admin()) {
621 return false;
622 }
623 $this->broken = 0;
624 $this->broken_level = 0;
625 $this->last = 0;
626 return XDB::execute('UPDATE email_redirect_account
627 SET broken_level = 0, broken_date = 0, last = 0
628 WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'',
629 $this->user->id(), $this->email);
630 }
631
632
633 // Email backend capabilities ('rewrite' refers to From: rewrite for mails
634 // forwarded by Polytechnique.org's MXs; 'removable' indicates if the email
635 // can be definitively removed; 'disable' indicates if the email has a third
636 // status 'disabled' in addition to 'active' and 'inactive').
637 public function has_rewrite()
638 {
639 return ($this->type == 'smtp');
640 }
641
642 public function is_removable()
643 {
644 return ($this->type == 'smtp');
645 }
646
647 public function has_disable()
648 {
649 return true;
650 }
651
652 public function is_redirection()
653 {
654 return ($this->type == 'smtp');
655 }
656
657 // Returns the list of allowed storages for the @p user.
658 static public function get_allowed_storages(User $user)
659 {
660 global $globals;
661 $storages = array();
662
663 // Google Apps storage is available for users with valid Google Apps account.
664 require_once 'googleapps.inc.php';
665 if ($user->checkPerms('gapps') &&
666 $globals->mailstorage->googleapps_domain &&
667 GoogleAppsAccount::account_status($user->id()) == 'active') {
668 $storages[] = 'googleapps';
669 }
670
671 // IMAP storage is always visible to administrators, and is allowed for
672 // everyone when the service is marked as 'active'.
673 if ($globals->mailstorage->imap_active || S::admin()) {
674 $storages[] = 'imap';
675 }
676
677 return $storages;
678 }
679
680 static public function make_storage_redirection(User $user, $storage)
681 {
682 return $user->hruid . '@' . self::$storage_domains[$storage] . '.' . Platal::globals()->mail->domain;
683 }
684
685 static public function activate_storage(User $user, $storage, $action = null)
686 {
687 Platal::assert(in_array($storage, self::get_allowed_storages($user)), 'Unknown storage.');
688
689 // We first need to retrieve the value for the antispam filter if not
690 // provided: it is either the user's redirections common value, or if
691 // they differ, our default value.
692 if (is_null($action)) {
693 $bogo = new Bogo($user);
694 $action = ($bogo->single_state ? Bogo::$states[$bogo->state] : Bogo::MAIN_DEFAULT);
695 }
696
697 if (!self::is_active_storage($user, $storage)) {
698 XDB::execute('INSERT INTO email_redirect_account (uid, type, action, redirect, flags)
699 VALUES ({?}, {?}, {?}, {?}, \'active\')',
700 $user->id(), $storage, $action, self::make_storage_redirection($user, $storage));
701 }
702 }
703
704 static public function deactivate_storage(User $user, $storage)
705 {
706 if (in_array($storage, self::get_allowed_storages($user))) {
707 XDB::execute('DELETE FROM email_redirect_account
708 WHERE uid = {?} AND type = {?}',
709 $user->id(), $storage);
710 }
711 }
712
713 static public function is_active_storage(User $user, $storage)
714 {
715 if (!in_array($storage, self::get_allowed_storages($user))) {
716 return false;
717 }
718 $res = XDB::fetchOneCell('SELECT COUNT(*)
719 FROM email_redirect_account
720 WHERE uid = {?} AND type = {?} AND flags = \'active\'',
721 $user->id(), $storage);
722 return !is_null($res) && $res > 0;
723 }
724 }
725 // class Redirect {{{1
726 // Redirect is a placeholder class for an user's active redirections (third-party
727 // redirection email, or Polytechnique.org mail storages).
728 class Redirect
729 {
730 private $flags = 'active';
731 private $user;
732
733 public $emails;
734
735 public function __construct(User $user)
736 {
737 $this->user = &$user;
738
739 // Adds third-party email redirections.
740 $res = XDB::iterator('SELECT redirect, rewrite, type, action, broken_date, broken_level, last, flags, hash, allow_rewrite
741 FROM email_redirect_account
742 WHERE uid = {?} AND type != \'homonym\'',
743 $user->id());
744 $this->emails = array();
745 while ($row = $res->next()) {
746 $this->emails[] = new Email($user, $row);
747 }
748
749 if ($storages = Email::get_allowed_storages($user)) {
750 // We first need to retrieve the value for the antispam filter: it is
751 // either the user's redirections common value, or if they differ, our
752 // default value.
753 $bogo = new Bogo($user);
754 $filter = ($bogo->single_state ? Bogo::$states[$bogo->state] : Bogo::MAIN_DEFAULT);
755
756 foreach ($storages as $storage) {
757 if (!Email::is_active_storage($user, $storage)) {
758 $this->emails[] = new Email($user, array(
759 'redirect' => Email::make_storage_redirection($user, $storage),
760 'rewrite' => '',
761 'type' => $storage,
762 'action' => $filter,
763 'broken_date' => 0,
764 'broken_level' => '0000-00-00',
765 'last' => '0000-00-00',
766 'flags' => 'inactive',
767 'hash' => '',
768 'allow_rewrite' => 0));
769 }
770 }
771 }
772 }
773
774 public function other_active($email)
775 {
776 foreach ($this->emails as $mail) {
777 if ($mail->email != $email && $mail->active && $mail->sufficient) {
778 return true;
779 }
780 }
781 return false;
782 }
783
784 public function delete_email($email)
785 {
786 if (!$this->other_active($email)) {
787 return ERROR_INACTIVE_REDIRECTION;
788 }
789 XDB::execute('DELETE FROM email_redirect_account
790 WHERE uid = {?} AND redirect = {?} AND type != \'homonym\'',
791 $this->user->id(), $email);
792 S::logger()->log('email_del', $email . ($this->user->id() != S::v('uid') ? " (admin on {$this->user->login()})" : ""));
793 foreach ($this->emails as $i => $mail) {
794 if ($email == $mail->email) {
795 unset($this->emails[$i]);
796 }
797 }
798 check_redirect($this);
799 $this->update_imap();
800 return SUCCESS;
801 }
802
803 public function add_email($email)
804 {
805 $email_stripped = strtolower(trim($email));
806 if (!isvalid_email($email_stripped)) {
807 return ERROR_INVALID_EMAIL;
808 }
809 if (!isvalid_email_redirection($email_stripped, $this->user)) {
810 return ERROR_LOOP_EMAIL;
811 }
812 // We first need to retrieve the value for the antispam filter: it is
813 // either the user's redirections common value, or if they differ, our
814 // default value.
815 $bogo = new Bogo($this->user);
816 $filter = ($bogo->single_state ? Bogo::$states[$bogo->state] : Bogo::MAIN_DEFAULT);
817 // If the email was already present for this user, we reset it to the default values, we thus use REPLACE INTO.
818 XDB::execute('REPLACE INTO email_redirect_account (uid, redirect, flags, action)
819 VALUES ({?}, {?}, \'active\', {?})',
820 $this->user->id(), $email, $filter);
821 // Replace this email by forlife email, if present in aliases and MLs.
822 $listClient = new MMList(S::user());
823 $listClient->change_user_email($email, $this->user->forlifeEmail());
824 update_alias_user($email, $this->user->forlifeEmail());
825 if ($logger = S::v('log', null)) { // may be absent --> step4.php
826 S::logger()->log('email_add', $email . ($this->user->id() != S::v('uid') ? " (admin on {$this->user->login()})" : ""));
827 }
828 foreach ($this->emails as $mail) {
829 if ($mail->email == $email_stripped) {
830 return SUCCESS;
831 }
832 }
833 $this->emails[] = new Email($this->user, array(
834 'redirect' => $email,
835 'rewrite' => '',
836 'type' => 'smtp',
837 'action' => $filter,
838 'broken_date' => '0000-00-00',
839 'broken_level' => 0,
840 'last' => '0000-00-00',
841 'flags' => 'active',
842 'hash' => null,
843 'allow_rewrite' => 0
844 ));
845
846 // security stuff
847 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->user->login());
848 check_redirect($this);
849 $this->update_imap();
850 return SUCCESS;
851 }
852
853 public function modify_email($emails_actifs, $emails_rewrite)
854 {
855 foreach ($this->emails as &$email) {
856 if (in_array($email->email, $emails_actifs)) {
857 $email->activate();
858 } else {
859 $email->deactivate();
860 }
861 $email->set_rewrite($emails_rewrite[$email->email]);
862 }
863 check_redirect($this);
864 $this->update_imap();
865 return SUCCESS;
866 }
867
868 public function modify_one_email($email, $activate)
869 {
870 $allinactive = true;
871 $thisone = false;
872 foreach ($this->emails as $i=>$mail) {
873 if ($mail->email == $email) {
874 $thisone = $i;
875 }
876 $allinactive &= !$mail->active || !$mail->sufficient || $mail->email == $email;
877 }
878 if ($thisone === false) {
879 return ERROR_INVALID_EMAIL;
880 }
881 if ($allinactive || $activate) {
882 $this->emails[$thisone]->activate();
883 } else {
884 $this->emails[$thisone]->deactivate();
885 }
886 check_redirect($this);
887 $this->update_imap();
888 if ($allinactive && !$activate) {
889 return ERROR_INACTIVE_REDIRECTION;
890 }
891 return SUCCESS;
892 }
893
894 public function modify_one_email_redirect($email, $redirect)
895 {
896 foreach ($this->emails as &$mail) {
897 if ($mail->email == $email) {
898 $mail->set_rewrite($redirect);
899 check_redirect($this);
900 $this->update_imap();
901 return;
902 }
903 }
904 }
905
906 public function clean_errors($email)
907 {
908 foreach ($this->emails as &$mail) {
909 if ($mail->email == $email) {
910 check_redirect($this);
911 $this->update_imap();
912 return $mail->clean_errors();
913 }
914 }
915 return false;
916 }
917
918 public function disable()
919 {
920 XDB::execute("UPDATE email_redirect_account
921 SET flags = 'disable'
922 WHERE flags = 'active' AND uid = {?}", $this->user->id());
923 foreach ($this->emails as &$mail) {
924 if ($mail->active && $mail->has_disable()) {
925 $mail->disabled = true;
926 $mail->active = false;
927 }
928 }
929 check_redirect($this);
930 $this->update_imap();
931 }
932
933 public function enable()
934 {
935 XDB::execute("UPDATE email_redirect_account
936 SET flags = 'active'
937 WHERE flags = 'disable' AND uid = {?}", $this->user->id());
938 foreach ($this->emails as &$mail) {
939 if ($mail->disabled) {
940 $mail->disabled = false;
941 $mail->active = true;
942 }
943 check_redirect($this);
944 }
945 $this->update_imap();
946 }
947
948 public function get_broken_mx()
949 {
950 $res = XDB::query("SELECT host, text
951 FROM mx_watch
952 WHERE state != 'ok'");
953 if (!$res->numRows()) {
954 return array();
955 }
956 $mxs = $res->fetchAllAssoc();
957 $mails = array();
958 foreach ($this->emails as &$mail) {
959 if ($mail->active && strstr($mail->email, '@') !== false) {
960 list(,$domain) = explode('@', $mail->email);
961 getmxrr($domain, $lcl_mxs);
962 if (empty($lcl_mxs)) {
963 $lcl_mxs = array($domain);
964 }
965 $broken = false;
966 foreach ($mxs as &$mx) {
967 foreach ($lcl_mxs as $lcl) {
968 if (fnmatch($mx['host'], $lcl)) {
969 $broken = $mx['text'];
970 break;
971 }
972 }
973 if ($broken) {
974 $mails[] = array('mail' => $mail->email, 'text' => $broken);
975 break;
976 }
977 }
978 }
979 }
980 return $mails;
981 }
982
983 public function active_emails()
984 {
985 $emails = array();
986 foreach ($this->emails as $mail) {
987 if ($mail->active) {
988 $emails[] = $mail;
989 }
990 }
991 return $emails;
992 }
993
994 public function get_uid()
995 {
996 return $this->user->id();
997 }
998
999 private function update_imap()
1000 {
1001 // Imaps must bounce if and only if the user has no active redirection.
1002 if (!$this->other_active('')) {
1003 XDB::execute('UPDATE email_redirect_account
1004 SET action = \'imap_and_bounce\'
1005 WHERE type = \'imap\' AND uid = {?}',
1006 $this->user->id());
1007 } else {
1008 XDB::execute('UPDATE email_redirect_account
1009 SET action = \'let_spams\'
1010 WHERE type = \'imap\' AND uid = {?}',
1011 $this->user->id());
1012 }
1013 }
1014 }
1015
1016 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
1017 ?>