Bye xorg.misc.inc.php
[platal.git] / include / emails.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 // function fix_bestalias() {{{1
28 // Checks for an existing 'bestalias' among the the current user's aliases, and
29 // eventually selects a new bestalias when required.
30 function fix_bestalias($uid)
31 {
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()) {
37 return;
38 }
39
40 XDB::execute("UPDATE aliases
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);
45 }
46
47 // function valide_email() {{{1
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.
51 function valide_email($str)
52 {
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;
64 }
65
66 // class Bogo {{{1
67 // The Bogo class represents a spam filtering level in plat/al architecture.
68 class Bogo
69 {
70 // properties {{{2
71
72 private $uid;
73 private $state;
74 private $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams');
75
76 // constructor {{{2
77
78 public function __construct($uid)
79 {
80 if (!$uid) {
81 return;
82 }
83
84 $this->uid = $uid;
85 $res = XDB::query('SELECT email FROM emails WHERE uid={?} AND flags="filter"', $uid);
86 if ($res->numRows()) {
87 $this->state = $res->fetchOneCell();
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 }
93 }
94
95 // public function change() {{{2
96
97 public function change($state)
98 {
99 $this->state = is_int($state) ? $this->_states[$state] : $state;
100 XDB::execute('UPDATE emails SET email={?} WHERE uid={?} AND flags = "filter"',
101 $this->state, $this->uid);
102 }
103
104 // pubic function level() {{{2
105
106 public function level()
107 {
108 return array_search($this->state, $this->_states);
109 }
110 }
111
112 // class Email {{{1
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).
116 abstract class Email
117 {
118 protected $uid;
119
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;
124 public $email;
125 public $display_email;
126
127 // Redirection status properties.
128 public $active;
129 public $broken;
130 public $disabled;
131 public $rewrite;
132
133 // Redirection bounces stats.
134 public $panne;
135 public $last;
136 public $panne_level;
137
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).
162 class EmailRedirection extends Email
163 {
164 // constructor {{{2
165
166 public function __construct($uid, $row)
167 {
168 $this->uid = $uid;
169 $this->sufficient = true;
170
171 list($this->email, $flags, $this->rewrite, $this->panne, $this->last, $this->panne_level) = $row;
172 $this->display_email = $this->email;
173 $this->active = ($flags == 'active');
174 $this->broken = ($flags == 'panne');
175 $this->disabled = ($flags == 'disable');
176 }
177
178 // public function activate() {{{2
179
180 public function activate()
181 {
182 if (!$this->active) {
183 XDB::execute("UPDATE emails
184 SET panne_level = IF(flags = 'panne', panne_level - 1, panne_level),
185 flags = 'active'
186 WHERE uid={?} AND email={?}", $this->uid, $this->email);
187 S::logger()->log("email_on", $this->email.($this->uid!=S::v('uid') ? "(admin on {$this->uid})" : ""));
188 $this->active = true;
189 $this->broken = false;
190 }
191 }
192
193 // public function deactivate() {{{2
194
195 public function deactivate()
196 {
197 if ($this->active) {
198 XDB::execute("UPDATE emails SET flags =''
199 WHERE uid={?} AND email={?}", $this->uid, $this->email);
200 S::logger()->log("email_off",$this->email.($this->uid != S::v('uid') ? "(admin on {$this->uid})" : "") );
201 $this->active = false;
202 }
203 }
204
205 // public function set_rewrite() {{{2
206
207 public function set_rewrite($rewrite)
208 {
209 if ($this->rewrite == $rewrite) {
210 return;
211 }
212 if (!$rewrite || !isvalid_email($rewrite)) {
213 $rewrite = '';
214 }
215 XDB::execute('UPDATE emails SET rewrite={?} WHERE uid={?} AND email={?}', $rewrite, $this->uid, $this->email);
216 $this->rewrite = $rewrite;
217 return;
218 }
219
220 // public function clean_errors() {{{2
221
222 public function clean_errors()
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 = {?}",
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;
255 }
256 }
257
258 // class EmailStorage {{{1
259 // Implementation of Email for email storage backends from Polytechnique.org.
260 class 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)',
265 'googleapps' => 'Compte Google Apps',
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);
274 return new PlFlagSet($res->fetchOneCell());
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 = {?}
282 WHERE user_id = {?}", $storages, $this->uid);
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
357 // class Redirect {{{1
358 // Redirect is a placeholder class for an user's active redirections (third-party
359 // redirection email, or Polytechnique.org mail storages).
360 class Redirect
361 {
362 // properties {{{2
363
364 private $flag_active = 'active';
365 private $uid;
366
367 public $emails;
368 public $bogo;
369
370 // constructor {{{2
371
372 public function __construct($_uid)
373 {
374 $this->uid = $_uid;
375 $this->bogo = new Bogo($_uid);
376
377 // Adds third-party email redirections.
378 $res = XDB::iterRow("SELECT email, flags, rewrite, panne, last, panne_level
379 FROM emails
380 WHERE uid = {?} AND flags != 'filter'", $_uid);
381 $this->emails = Array();
382 while ($row = $res->next()) {
383 $this->emails[] = new EmailRedirection($_uid, $row);
384 }
385
386 // Adds local email storage backends.
387 foreach (EmailStorage::get_allowed_storages($_uid) as $storage) {
388 $this->emails[] = new EmailStorage($_uid, $storage);
389 }
390 }
391
392 // public function other_active() {{{2
393
394 public function other_active($email)
395 {
396 foreach ($this->emails as $mail) {
397 if ($mail->email != $email && $mail->active && $mail->sufficient) {
398 return true;
399 }
400 }
401 return false;
402 }
403
404 // public function delete_email() {{{2
405
406 public function delete_email($email)
407 {
408 if (!$this->other_active($email)) {
409 return ERROR_INACTIVE_REDIRECTION;
410 }
411 XDB::execute('DELETE FROM emails WHERE uid={?} AND email={?}', $this->uid, $email);
412 S::logger()->log('email_del',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : ""));
413 foreach ($this->emails as $i => $mail) {
414 if ($email == $mail->email) {
415 unset($this->emails[$i]);
416 }
417 }
418 check_redirect($this);
419 return SUCCESS;
420 }
421
422 // public function add_email() {{{2
423
424 public function add_email($email)
425 {
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 }
433 XDB::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->uid, $email);
434 if ($logger = S::v('log', null)) { // may be absent --> step4.php
435 S::logger()->log('email_add',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : ""));
436 }
437 foreach ($this->emails as $mail) {
438 if ($mail->email == $email_stripped) {
439 return SUCCESS;
440 }
441 }
442 $this->emails[] = new EmailRedirection($this->uid, array($email, 'active', '', '0000-00-00', '0000-00-00', 0));
443
444 // security stuff
445 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->uid);
446 check_redirect($this);
447 return SUCCESS;
448 }
449
450 // public function modify_email() {{{2
451
452 public function modify_email($emails_actifs, $emails_rewrite)
453 {
454 foreach ($this->emails as &$mail) {
455 if (in_array($mail->email, $emails_actifs)) {
456 $mail->activate();
457 } else {
458 $mail->deactivate();
459 }
460 $mail->set_rewrite($emails_rewrite[$mail->email]);
461 }
462 check_redirect($this);
463 }
464
465 // public function modify_one_email() {{{2
466
467 public function modify_one_email($email, $activate)
468 {
469 $allinactive = true;
470 $thisone = false;
471 foreach ($this->emails as $i=>$mail) {
472 if ($mail->email == $email) {
473 $thisone = $i;
474 }
475 $allinactive &= !$mail->active || !$mail->sufficient || $mail->email == $email;
476 }
477 if ($thisone === false) {
478 return ERROR_INVALID_EMAIL;
479 }
480 if ($allinactive || $activate) {
481 $this->emails[$thisone]->activate();
482 } else {
483 $this->emails[$thisone]->deactivate();
484 }
485 check_redirect($this);
486 if ($allinactive && !$activate) {
487 return ERROR_INACTIVE_REDIRECTION;
488 } else {
489 return SUCCESS;
490 }
491 }
492
493 // public function modify_one_email_redirect() {{{2
494
495 public function modify_one_email_redirect($email, $redirect)
496 {
497 foreach ($this->emails as &$mail) {
498 if ($mail->email == $email) {
499 $mail->set_rewrite($redirect);
500 check_redirect($this);
501 return;
502 }
503 }
504 }
505
506 // function clean_errors() {{{2
507
508 public function clean_errors($email)
509 {
510 foreach ($this->emails as &$mail) {
511 if ($mail->email == $email) {
512 check_redirect($this);
513 return $mail->clean_errors();
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) {
527 if ($mail->active && $mail->has_disable()) {
528 $mail->disabled = true;
529 $mail->active = false;
530 }
531 }
532 check_redirect($this);
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 }
547 check_redirect($this);
548 }
549 }
550
551 // function get_broken_mx() {{{2
552
553 public function get_broken_mx()
554 {
555 $res = XDB::query("SELECT host, text
556 FROM mx_watch
557 WHERE state != 'ok'");
558 if (!$res->numRows()) {
559 return array();
560 }
561 $mxs = $res->fetchAllAssoc();
562 $mails = array();
563 foreach ($this->emails as &$mail) {
564 if ($mail->active && strstr($mail->email, '@') !== false) {
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) {
573 if (fnmatch($mx['host'], $lcl)) {
574 $broken = $mx['text'];
575 break;
576 }
577 }
578 if ($broken) {
579 $mails[] = array('mail' => $mail->email, 'text' => $broken);
580 break;
581 }
582 }
583 }
584 }
585 return $mails;
586 }
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 }
600
601 // function get_uid() {{{2
602
603 public function get_uid()
604 {
605 return $this->uid;
606 }
607 }
608
609 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
610 ?>