Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
9f5bd98e | 3 | * Copyright (C) 2003-2010 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 | 22 | define("SUCCESS", 1); |
23 | define("ERROR_INACTIVE_REDIRECTION", 2); | |
24 | define("ERROR_INVALID_EMAIL", 3); | |
25 | define("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 | 30 | function fix_bestalias(User &$user) |
0337d704 | 31 | { |
11ed26e5 VZ |
32 | $res = XDB::query("SELECT COUNT(*) |
33 | FROM aliases | |
fe13bc1d | 34 | WHERE uid = {?} 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') |
fe13bc1d | 42 | WHERE uid={?} AND type!='homonyme' |
612a2d8a | 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 | 51 | function valide_email($str) |
52 | { | |
a3a049fc | 53 | global $globals; |
54 | ||
55 | $em = trim(rtrim($str)); | |
56 | $em = str_replace('<', '', $em); | |
57 | $em = str_replace('>', '', $em); | |
97664536 SJ |
58 | if (strpos($em, '@') === false) { |
59 | return; | |
60 | } | |
a3a049fc | 61 | list($ident, $dom) = explode('@', $em); |
97664536 | 62 | if ($dom == $globals->mail->domain || $dom == $globals->mail->domain2) { |
a3a049fc | 63 | list($ident1) = explode('_', $ident); |
64 | list($ident) = explode('+', $ident1); | |
65 | } | |
66 | return $ident . '@' . $dom; | |
0337d704 | 67 | } |
68 | ||
c6310567 FB |
69 | // function isvalid_email_redirection() {{{1 |
70 | /** vérifie si une adresse email convient comme adresse de redirection | |
71 | * @param $email l'adresse email a verifier | |
72 | * @return BOOL | |
73 | */ | |
74 | function isvalid_email_redirection($email) | |
75 | { | |
76 | return isvalid_email($email) && | |
77 | !preg_match("/@(polytechnique\.(org|edu)|melix\.(org|net)|m4x\.org)$/", $email); | |
78 | } | |
79 | ||
180627f3 RB |
80 | // function ids_from_mails() {{{1 |
81 | // Converts an array of emails to an array of email => uid | |
82 | function ids_from_mails(array $emails) | |
1605f171 RB |
83 | { |
84 | global $globals; | |
85 | $domain_mails = array(); | |
86 | $alias_mails = array(); | |
87 | $other_mails = array(); | |
180627f3 RB |
88 | |
89 | // Determine the type of the email adresses. It can eiher be a domain | |
90 | // email (@polytechnique.org), an alias email (@melix.net) or any other | |
91 | // email (potentially used as a redirection by one user) | |
1605f171 RB |
92 | foreach ($emails as $email) { |
93 | if (strpos($email, '@') === false) { | |
94 | $user = $email; | |
95 | $domain = $globals->mail->domain2; | |
96 | } else { | |
97 | list($user, $domain) = explode('@', $email); | |
98 | } | |
99 | if ($domain == $globals->mail->alias_dom || $domain == $globals->mail->alias_dom2) { | |
100 | list($user) = explode('+', $user); | |
101 | list($user) = explode('_', $user); | |
4f731783 | 102 | $alias_mails[$email] = $user . "@" . $globals->mail->alias_dom; |
1605f171 RB |
103 | } elseif ($domain == $globals->mail->domain || $domain == $globals->mail->domain2) { |
104 | list($user) = explode('+', $user); | |
105 | list($user) = explode('_', $user); | |
4f731783 | 106 | $domain_mails[$email] = $user; |
1605f171 RB |
107 | } else { |
108 | $other_mails[] = $email; | |
109 | } | |
110 | } | |
111 | $uids = array(); | |
180627f3 RB |
112 | |
113 | // Look up user ids for addresses in domain | |
4f731783 | 114 | $alias_uids = array(); |
1605f171 | 115 | if (count($domain_mails)) { |
fe13bc1d | 116 | $res = XDB::query("SELECT alias, uid |
180627f3 | 117 | FROM aliases |
4f731783 | 118 | WHERE alias IN {?}", array_unique($domain_mails)); |
1605f171 RB |
119 | foreach ($res->fetchAllRow() as $row) { |
120 | list ($alias, $id) = $row; | |
4f731783 | 121 | $domain_uids[$alias] = $id; |
1605f171 RB |
122 | } |
123 | } | |
4f731783 RB |
124 | // Connect emails with uids |
125 | foreach ($domain_mails as $email => $user) { | |
126 | $uids[$email] = $domain_uids[$user]; | |
127 | } | |
1605f171 | 128 | |
180627f3 | 129 | // Look up user ids for addresses in our alias domain |
4f731783 | 130 | $alias_uids = array(); |
1605f171 | 131 | if (count($alias_mails)) { |
fe13bc1d | 132 | $res = XDB::query("SELECT v.alias, a.uid |
180627f3 RB |
133 | FROM virtual AS v |
134 | INNER JOIN virtual_redirect AS r USING(vid) | |
135 | INNER JOIN aliases AS a ON (a.type = 'a_vie' | |
136 | AND r.redirect = CONCAT(a.alias, '@{$globals->mail->domain2}')) | |
4f731783 | 137 | WHERE v.alias IN {?}", array_unique($alias_mails)); |
1605f171 RB |
138 | foreach ($res->fetchAllRow() as $row) { |
139 | list ($alias, $id) = $row; | |
4f731783 | 140 | $alias_uids[$alias] = $id; |
1605f171 RB |
141 | } |
142 | } | |
4f731783 RB |
143 | // Connect emails with uids |
144 | foreach ($alias_mails as $email => $user) { | |
145 | $uids[$email] = $alias_uids[$user]; | |
146 | } | |
1605f171 | 147 | |
180627f3 | 148 | // Look up user ids for other addresses in the email redirection list |
1605f171 | 149 | if (count($other_mails)) { |
1605f171 | 150 | $res = XDB::query("SELECT email, uid |
180627f3 | 151 | FROM emails |
4f731783 | 152 | WHERE email IN {?}", $other_mails); |
1605f171 RB |
153 | foreach ($res->fetchAllRow() as $row) { |
154 | list ($email, $uid) = $row; | |
2ebe2e68 | 155 | $uids[$email] = $uid; |
1605f171 RB |
156 | } |
157 | } | |
158 | ||
159 | return $uids; | |
160 | } | |
161 | ||
441c2451 | 162 | // class Bogo {{{1 |
11ed26e5 | 163 | // The Bogo class represents a spam filtering level in plat/al architecture. |
0337d704 | 164 | class Bogo |
165 | { | |
441c2451 | 166 | // properties {{{2 |
a3a049fc | 167 | |
12a587df | 168 | private $user; |
612a2d8a | 169 | private $state; |
170 | private $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams'); | |
0337d704 | 171 | |
441c2451 | 172 | // constructor {{{2 |
a3a049fc | 173 | |
12a587df | 174 | public function __construct(User &$user) |
0337d704 | 175 | { |
12a587df | 176 | if (!$user) { |
3c1e6a1e | 177 | return; |
178 | } | |
11ed26e5 | 179 | |
12a587df VZ |
180 | $this->user = &$user; |
181 | $res = XDB::query('SELECT email FROM emails WHERE uid = {?} AND flags = "filter"', $user->id()); | |
3c1e6a1e | 182 | if ($res->numRows()) { |
612a2d8a | 183 | $this->state = $res->fetchOneCell(); |
3c1e6a1e | 184 | } else { |
185 | $this->state = 'tag_and_drop_spams'; | |
12a587df VZ |
186 | $res = XDB::query( |
187 | "INSERT INTO emails (uid, email, rewrite, panne, flags) | |
188 | VALUES ({?}, 'tag_and_drop_spams', '', '0000-00-00', 'filter')", | |
189 | $user->id()); | |
3c1e6a1e | 190 | } |
0337d704 | 191 | } |
192 | ||
441c2451 | 193 | // public function change() {{{2 |
0337d704 | 194 | |
11ed26e5 | 195 | public function change($state) |
0337d704 | 196 | { |
612a2d8a | 197 | $this->state = is_int($state) ? $this->_states[$state] : $state; |
12a587df VZ |
198 | XDB::execute('UPDATE emails SET email = {?} WHERE uid = {?} AND flags = "filter"', |
199 | $this->state, $this->user->id()); | |
0337d704 | 200 | } |
201 | ||
441c2451 | 202 | // pubic function level() {{{2 |
0337d704 | 203 | |
612a2d8a | 204 | public function level() |
205 | { | |
206 | return array_search($this->state, $this->_states); | |
207 | } | |
0337d704 | 208 | } |
209 | ||
441c2451 | 210 | // class Email {{{1 |
11ed26e5 VZ |
211 | // Represents an "email address" used as final recipient for plat/al-managed |
212 | // addresses; it can be subclasses a Redirection emails (third-party) or as | |
213 | // Storage emails (Polytechnique.org). | |
214 | abstract class Email | |
0337d704 | 215 | { |
12a587df | 216 | protected $user; |
612a2d8a | 217 | |
11ed26e5 VZ |
218 | // Basic email properties; $sufficient indicates if the email can be used as |
219 | // an unique redirection; $email contains the delivery email address. | |
220 | public $type; | |
221 | public $sufficient; | |
612a2d8a | 222 | public $email; |
11ed26e5 VZ |
223 | public $display_email; |
224 | ||
225 | // Redirection status properties. | |
612a2d8a | 226 | public $active; |
227 | public $broken; | |
441c2451 | 228 | public $disabled; |
612a2d8a | 229 | public $rewrite; |
3d17e48f FB |
230 | public $allow_rewrite; |
231 | public $hash; | |
11ed26e5 VZ |
232 | |
233 | // Redirection bounces stats. | |
612a2d8a | 234 | public $panne; |
235 | public $last; | |
236 | public $panne_level; | |
0337d704 | 237 | |
11ed26e5 VZ |
238 | // Activates the email address as a redirection. |
239 | public abstract function activate(); | |
240 | ||
241 | // Deactivates the email address as a redirection. | |
242 | public abstract function deactivate(); | |
243 | ||
244 | // Sets the rewrite rule for the given address. | |
245 | public abstract function set_rewrite($rewrite); | |
246 | ||
247 | // Resets the error counts associated with the redirection. | |
248 | public abstract function clean_errors(); | |
249 | ||
250 | // Email backend capabilities ('rewrite' refers to From: rewrite for mails | |
251 | // forwarded by Polytechnique.org's MXs; 'removable' indicates if the email | |
252 | // can be definitively removed; 'disable' indicates if the email has a third | |
253 | // status 'disabled' in addition to 'active' and 'inactive'). | |
254 | public abstract function has_rewrite(); | |
255 | public abstract function is_removable(); | |
256 | public abstract function has_disable(); | |
257 | } | |
258 | ||
259 | // class EmailRedirection {{{1 | |
260 | // Implementation of Email for third-party redirection (redirection of emails to | |
261 | // external user-supplied addresses). | |
262 | class EmailRedirection extends Email | |
263 | { | |
441c2451 | 264 | // constructor {{{2 |
0337d704 | 265 | |
12a587df | 266 | public function __construct(User &$user, $row) |
0337d704 | 267 | { |
12a587df | 268 | $this->user = &$user; |
11ed26e5 VZ |
269 | $this->sufficient = true; |
270 | ||
3d17e48f | 271 | list($this->email, $flags, $this->rewrite, $this->allow_rewrite, $this->hash, $this->panne, $this->last, $this->panne_level) = $row; |
11ed26e5 | 272 | $this->display_email = $this->email; |
441c2451 | 273 | $this->active = ($flags == 'active'); |
274 | $this->broken = ($flags == 'panne'); | |
275 | $this->disabled = ($flags == 'disable'); | |
0337d704 | 276 | } |
277 | ||
441c2451 | 278 | // public function activate() {{{2 |
0337d704 | 279 | |
11ed26e5 | 280 | public function activate() |
0337d704 | 281 | { |
0337d704 | 282 | if (!$this->active) { |
dc557110 | 283 | XDB::execute("UPDATE emails |
284 | SET panne_level = IF(flags = 'panne', panne_level - 1, panne_level), | |
285 | flags = 'active' | |
12a587df VZ |
286 | WHERE uid = {?} AND email = {?}", $this->user->id(), $this->email); |
287 | S::logger()->log("email_on", $this->email . ($this->user->id() != S::v('uid') ? "(admin on {$this->user->login()})" : "")); | |
0337d704 | 288 | $this->active = true; |
dc557110 | 289 | $this->broken = false; |
0337d704 | 290 | } |
291 | } | |
292 | ||
441c2451 | 293 | // public function deactivate() {{{2 |
0337d704 | 294 | |
11ed26e5 | 295 | public function deactivate() |
0337d704 | 296 | { |
0337d704 | 297 | if ($this->active) { |
08cce2ff | 298 | XDB::execute("UPDATE emails SET flags ='' |
12a587df VZ |
299 | WHERE uid = {?} AND email = {?}", $this->user->id(), $this->email); |
300 | S::logger()->log("email_off", $this->email . ($this->user->id() != S::v('uid') ? "(admin on {$this->user->login()})" : "") ); | |
0337d704 | 301 | $this->active = false; |
302 | } | |
303 | } | |
612a2d8a | 304 | |
11ed26e5 | 305 | // public function set_rewrite() {{{2 |
0337d704 | 306 | |
11ed26e5 | 307 | public function set_rewrite($rewrite) |
0337d704 | 308 | { |
11ed26e5 | 309 | if ($this->rewrite == $rewrite) { |
0337d704 | 310 | return; |
311 | } | |
11ed26e5 VZ |
312 | if (!$rewrite || !isvalid_email($rewrite)) { |
313 | $rewrite = ''; | |
12acff5d | 314 | } |
12a587df | 315 | XDB::execute('UPDATE emails SET rewrite = {?} WHERE uid = {?} AND email = {?}', $rewrite, $this->user->id(), $this->email); |
11ed26e5 | 316 | $this->rewrite = $rewrite; |
3d17e48f FB |
317 | if (!$this->allow_rewrite) { |
318 | global $globals; | |
319 | if (empty($this->hash)) { | |
320 | $this->hash = rand_url_id(); | |
321 | XDB::execute("UPDATE emails | |
322 | SET hash = {?} | |
21c7c593 | 323 | WHERE uid = {?} AND email = {?}", $this->hash, $this->user->id(), $this->email); |
3d17e48f | 324 | } |
3d17e48f FB |
325 | $mail = new PlMailer('emails/rewrite-in.mail.tpl'); |
326 | $mail->assign('mail', $this); | |
21c7c593 | 327 | $mail->assign('user', $this->user); |
3d17e48f | 328 | $mail->assign('baseurl', $globals->baseurl); |
c66de9a0 FB |
329 | $mail->assign('sitename', $globals->core->sitename); |
330 | $mail->assign('to', $this->email); | |
21c7c593 | 331 | $mail->send($this->user->isEmailFormatHtml()); |
3d17e48f | 332 | } |
3c1e6a1e | 333 | return; |
0337d704 | 334 | } |
335 | ||
11ed26e5 | 336 | // public function clean_errors() {{{2 |
441c2451 | 337 | |
11ed26e5 | 338 | public function clean_errors() |
441c2451 | 339 | { |
dd70cd28 | 340 | if (!S::admin()) { |
441c2451 | 341 | return false; |
342 | } | |
343 | $this->panne = 0; | |
344 | $this->panne_level = 0; | |
345 | $this->last = 0; | |
346 | return XDB::execute("UPDATE emails | |
347 | SET panne_level = 0, panne = 0, last = 0 | |
348 | WHERE uid = {?} AND email = {?}", | |
12a587df | 349 | $this->user->id(), $this->email); |
11ed26e5 VZ |
350 | } |
351 | ||
352 | // public function has_rewrite() {{{2 | |
353 | ||
354 | public function has_rewrite() | |
355 | { | |
356 | return true; | |
357 | } | |
358 | ||
359 | // public function is_removable() {{{2 | |
360 | ||
361 | public function is_removable() | |
362 | { | |
363 | return true; | |
364 | } | |
365 | ||
366 | // public function has_disable() {{{2 | |
367 | ||
368 | public function has_disable() | |
369 | { | |
370 | return true; | |
441c2451 | 371 | } |
0337d704 | 372 | } |
373 | ||
afde0a3a VZ |
374 | // class EmailStorage {{{1 |
375 | // Implementation of Email for email storage backends from Polytechnique.org. | |
376 | class EmailStorage extends Email | |
377 | { | |
378 | // Shortname to realname mapping for known mail storage backends. | |
379 | private $display_names = array( | |
380 | 'imap' => 'Accès de secours aux emails (IMAP)', | |
690c8519 | 381 | 'googleapps' => 'Compte Google Apps', |
afde0a3a VZ |
382 | ); |
383 | ||
384 | // Retrieves the current list of actives storages. | |
385 | private function get_storages() | |
386 | { | |
58a4e6bb FB |
387 | return new PlFlagSet(XDB::fetchOneCell('SELECT storage |
388 | FROM email_options | |
389 | WHERE uid = {?}', | |
390 | $this->user->id())); | |
afde0a3a VZ |
391 | } |
392 | ||
393 | // Updates the list of active storages. | |
394 | private function set_storages($storages) | |
395 | { | |
58a4e6bb FB |
396 | XDB::execute("UPDATE email_options |
397 | SET storage = {?} | |
398 | WHERE uid = {?}", $storages, $this->user->id()); | |
afde0a3a VZ |
399 | } |
400 | ||
401 | // Returns the list of allowed storages for the @p user. | |
12a587df | 402 | static public function get_allowed_storages(User &$user) |
afde0a3a VZ |
403 | { |
404 | global $globals; | |
405 | $storages = array(); | |
406 | ||
407 | // Google Apps storage is available for users with valid Google Apps account. | |
408 | require_once 'googleapps.inc.php'; | |
409 | if ($globals->mailstorage->googleapps_domain && | |
12a587df | 410 | GoogleAppsAccount::account_status($user->id()) == 'active') { |
afde0a3a VZ |
411 | $storages[] = 'googleapps'; |
412 | } | |
413 | ||
414 | // IMAP storage is always visible to administrators, and is allowed for | |
415 | // everyone when the service is marked as 'active'. | |
dd70cd28 | 416 | if ($globals->mailstorage->imap_active || S::admin()) { |
afde0a3a VZ |
417 | $storages[] = 'imap'; |
418 | } | |
419 | ||
420 | return $storages; | |
421 | } | |
422 | ||
423 | ||
12a587df | 424 | public function __construct(User &$user, $name) |
afde0a3a | 425 | { |
12a587df | 426 | $this->user = &$user; |
afde0a3a VZ |
427 | $this->email = $name; |
428 | $this->display_email = (isset($this->display_names[$name]) ? $this->display_names[$name] : $name); | |
429 | ||
430 | $storages = $this->get_storages(); | |
431 | $this->sufficient = ($name == 'googleapps'); | |
432 | $this->active = $storages->hasFlag($name); | |
433 | $this->broken = false; | |
434 | $this->disabled = false; | |
435 | $this->rewrite = ''; | |
436 | $this->panne = $this->last = $this->panne_level = 0; | |
437 | } | |
438 | ||
439 | public function activate() | |
440 | { | |
441 | if (!$this->active) { | |
442 | $storages = $this->get_storages(); | |
443 | $storages->addFlag($this->email); | |
444 | $this->set_storages($storages); | |
445 | $this->active = true; | |
446 | } | |
447 | } | |
448 | ||
449 | public function deactivate() | |
450 | { | |
451 | if ($this->active) { | |
452 | $storages = $this->get_storages(); | |
453 | $storages->rmFlag($this->email); | |
454 | $this->set_storages($storages); | |
455 | $this->active = false; | |
456 | } | |
457 | ||
458 | } | |
459 | ||
460 | // Source rewrite can't be enabled for email storage addresses. | |
461 | public function set_rewrite($rewrite) {} | |
462 | ||
463 | // Email storage are not supposed to be broken, hence not supposed to be | |
464 | // cleaned-up. | |
465 | public function clean_errors() {} | |
466 | ||
467 | // Capabilities. | |
468 | public function has_rewrite() { return false; } | |
469 | public function is_removable() { return false; } | |
470 | public function has_disable() { return false; } | |
471 | } | |
472 | ||
441c2451 | 473 | // class Redirect {{{1 |
11ed26e5 VZ |
474 | // Redirect is a placeholder class for an user's active redirections (third-party |
475 | // redirection email, or Polytechnique.org mail storages). | |
0337d704 | 476 | class Redirect |
477 | { | |
441c2451 | 478 | // properties {{{2 |
612a2d8a | 479 | |
480 | private $flag_active = 'active'; | |
12a587df | 481 | private $user; |
612a2d8a | 482 | |
483 | public $emails; | |
484 | public $bogo; | |
0337d704 | 485 | |
441c2451 | 486 | // constructor {{{2 |
0337d704 | 487 | |
12a587df | 488 | public function __construct(User &$user) |
0337d704 | 489 | { |
12a587df VZ |
490 | $this->user = &$user; |
491 | $this->bogo = new Bogo($user); | |
11ed26e5 | 492 | |
afde0a3a | 493 | // Adds third-party email redirections. |
3d17e48f | 494 | $res = XDB::iterRow("SELECT email, flags, rewrite, allow_rewrite, hash, panne, last, panne_level |
612a2d8a | 495 | FROM emails |
12a587df | 496 | WHERE uid = {?} AND flags != 'filter'", $user->id()); |
11ed26e5 | 497 | $this->emails = Array(); |
0337d704 | 498 | while ($row = $res->next()) { |
12a587df | 499 | $this->emails[] = new EmailRedirection($user, $row); |
0337d704 | 500 | } |
afde0a3a VZ |
501 | |
502 | // Adds local email storage backends. | |
12a587df VZ |
503 | foreach (EmailStorage::get_allowed_storages($user) as $storage) { |
504 | $this->emails[] = new EmailStorage($user, $storage); | |
afde0a3a | 505 | } |
0337d704 | 506 | } |
507 | ||
441c2451 | 508 | // public function other_active() {{{2 |
0337d704 | 509 | |
612a2d8a | 510 | public function other_active($email) |
0337d704 | 511 | { |
512 | foreach ($this->emails as $mail) { | |
11ed26e5 | 513 | if ($mail->email != $email && $mail->active && $mail->sufficient) { |
0337d704 | 514 | return true; |
515 | } | |
516 | } | |
517 | return false; | |
518 | } | |
519 | ||
441c2451 | 520 | // public function delete_email() {{{2 |
0337d704 | 521 | |
612a2d8a | 522 | public function delete_email($email) |
0337d704 | 523 | { |
0337d704 | 524 | if (!$this->other_active($email)) { |
525 | return ERROR_INACTIVE_REDIRECTION; | |
526 | } | |
12a587df VZ |
527 | XDB::execute('DELETE FROM emails WHERE uid = {?} AND email = {?}', $this->user->id(), $email); |
528 | S::logger()->log('email_del', $email . ($this->user->id() != S::v('uid') ? " (admin on {$this->user->login()})" : "")); | |
11ed26e5 VZ |
529 | foreach ($this->emails as $i => $mail) { |
530 | if ($email == $mail->email) { | |
0337d704 | 531 | unset($this->emails[$i]); |
532 | } | |
3c1e6a1e | 533 | } |
ccdbc270 | 534 | check_redirect($this); |
0337d704 | 535 | return SUCCESS; |
536 | } | |
537 | ||
441c2451 | 538 | // public function add_email() {{{2 |
612a2d8a | 539 | |
540 | public function add_email($email) | |
0337d704 | 541 | { |
0337d704 | 542 | $email_stripped = strtolower(trim($email)); |
543 | if (!isvalid_email($email_stripped)) { | |
544 | return ERROR_INVALID_EMAIL; | |
545 | } | |
546 | if (!isvalid_email_redirection($email_stripped)) { | |
547 | return ERROR_LOOP_EMAIL; | |
548 | } | |
12a587df | 549 | XDB::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->user->id(), $email); |
3c1e6a1e | 550 | if ($logger = S::v('log', null)) { // may be absent --> step4.php |
12a587df | 551 | S::logger()->log('email_add', $email . ($this->user->id() != S::v('uid') ? " (admin on {$this->user->login()})" : "")); |
0337d704 | 552 | } |
3c1e6a1e | 553 | foreach ($this->emails as $mail) { |
554 | if ($mail->email == $email_stripped) { | |
0337d704 | 555 | return SUCCESS; |
556 | } | |
3c1e6a1e | 557 | } |
21c7c593 | 558 | $this->emails[] = new EmailRedirection($this->user, array($email, 'active', '', 0, null, '0000-00-00', '0000-00-00', 0)); |
ca6d07f4 | 559 | |
560 | // security stuff | |
12a587df | 561 | check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->user->login()); |
ccdbc270 | 562 | check_redirect($this); |
0337d704 | 563 | return SUCCESS; |
564 | } | |
565 | ||
441c2451 | 566 | // public function modify_email() {{{2 |
0337d704 | 567 | |
612a2d8a | 568 | public function modify_email($emails_actifs, $emails_rewrite) |
0337d704 | 569 | { |
441c2451 | 570 | foreach ($this->emails as &$mail) { |
571 | if (in_array($mail->email, $emails_actifs)) { | |
11ed26e5 | 572 | $mail->activate(); |
3c1e6a1e | 573 | } else { |
11ed26e5 | 574 | $mail->deactivate(); |
3c1e6a1e | 575 | } |
11ed26e5 | 576 | $mail->set_rewrite($emails_rewrite[$mail->email]); |
0337d704 | 577 | } |
ccdbc270 | 578 | check_redirect($this); |
0337d704 | 579 | } |
580 | ||
441c2451 | 581 | // public function modify_one_email() {{{2 |
582 | ||
eaf30d86 | 583 | public function modify_one_email($email, $activate) |
ccdbc270 | 584 | { |
b7582015 | 585 | $allinactive = true; |
586 | $thisone = false; | |
8ffa657a | 587 | foreach ($this->emails as $i=>$mail) { |
588 | if ($mail->email == $email) { | |
b7582015 | 589 | $thisone = $i; |
8ffa657a | 590 | } |
11ed26e5 | 591 | $allinactive &= !$mail->active || !$mail->sufficient || $mail->email == $email; |
8ffa657a | 592 | } |
b7582015 | 593 | if ($thisone === false) { |
594 | return ERROR_INVALID_EMAIL; | |
595 | } | |
ccdbc270 | 596 | if ($allinactive || $activate) { |
11ed26e5 | 597 | $this->emails[$thisone]->activate(); |
ccdbc270 | 598 | } else { |
11ed26e5 | 599 | $this->emails[$thisone]->deactivate(); |
ccdbc270 | 600 | } |
601 | check_redirect($this); | |
b7582015 | 602 | if ($allinactive && !$activate) { |
603 | return ERROR_INACTIVE_REDIRECTION; | |
604 | } else { | |
605 | return SUCCESS; | |
eaf30d86 | 606 | } |
8ffa657a | 607 | } |
608 | ||
441c2451 | 609 | // public function modify_one_email_redirect() {{{2 |
610 | ||
612a2d8a | 611 | public function modify_one_email_redirect($email, $redirect) |
612 | { | |
441c2451 | 613 | foreach ($this->emails as &$mail) { |
612a2d8a | 614 | if ($mail->email == $email) { |
11ed26e5 | 615 | $mail->set_rewrite($redirect); |
ccdbc270 | 616 | check_redirect($this); |
617 | return; | |
612a2d8a | 618 | } |
619 | } | |
620 | } | |
441c2451 | 621 | |
11ed26e5 | 622 | // function clean_errors() {{{2 |
441c2451 | 623 | |
11ed26e5 | 624 | public function clean_errors($email) |
441c2451 | 625 | { |
626 | foreach ($this->emails as &$mail) { | |
627 | if ($mail->email == $email) { | |
e1547442 | 628 | check_redirect($this); |
11ed26e5 | 629 | return $mail->clean_errors(); |
441c2451 | 630 | } |
631 | } | |
632 | return false; | |
633 | } | |
634 | ||
635 | // function disable() {{{2 | |
636 | ||
637 | public function disable() | |
638 | { | |
639 | XDB::execute("UPDATE emails | |
640 | SET flags = 'disable' | |
20398e42 | 641 | WHERE flags = 'active' AND uid = {?}", $this->user->id()); |
441c2451 | 642 | foreach ($this->emails as &$mail) { |
11ed26e5 | 643 | if ($mail->active && $mail->has_disable()) { |
441c2451 | 644 | $mail->disabled = true; |
645 | $mail->active = false; | |
646 | } | |
647 | } | |
e1547442 | 648 | check_redirect($this); |
441c2451 | 649 | } |
650 | ||
651 | // function enable() {{{2 | |
652 | ||
653 | public function enable() | |
654 | { | |
655 | XDB::execute("UPDATE emails | |
656 | SET flags = 'active' | |
20398e42 | 657 | WHERE flags = 'disable' AND uid = {?}", $this->user->id()); |
441c2451 | 658 | foreach ($this->emails as &$mail) { |
659 | if ($mail->disabled) { | |
660 | $mail->active = true; | |
661 | $mail->disabled = false; | |
662 | } | |
e1547442 | 663 | check_redirect($this); |
441c2451 | 664 | } |
665 | } | |
666 | ||
667 | // function get_broken_mx() {{{2 | |
ccdbc270 | 668 | |
612a2d8a | 669 | public function get_broken_mx() |
ccdbc270 | 670 | { |
3083bd93 | 671 | $res = XDB::query("SELECT host, text |
8af1d78f FB |
672 | FROM mx_watch |
673 | WHERE state != 'ok'"); | |
120bd636 | 674 | if (!$res->numRows()) { |
ccdbc270 | 675 | return array(); |
676 | } | |
c754cf5b | 677 | $mxs = $res->fetchAllAssoc(); |
ccdbc270 | 678 | $mails = array(); |
679 | foreach ($this->emails as &$mail) { | |
11ed26e5 | 680 | if ($mail->active && strstr($mail->email, '@') !== false) { |
ccdbc270 | 681 | list(,$domain) = explode('@', $mail->email); |
682 | getmxrr($domain, $lcl_mxs); | |
683 | if (empty($lcl_mxs)) { | |
684 | $lcl_mxs = array($domain); | |
685 | } | |
686 | $broken = false; | |
687 | foreach ($mxs as &$mx) { | |
688 | foreach ($lcl_mxs as $lcl) { | |
c754cf5b | 689 | if (fnmatch($mx['host'], $lcl)) { |
ccdbc270 | 690 | $broken = $mx['text']; |
691 | break; | |
692 | } | |
693 | } | |
694 | if ($broken) { | |
3083bd93 | 695 | $mails[] = array('mail' => $mail->email, 'text' => $broken); |
f653ff3d | 696 | break; |
ccdbc270 | 697 | } |
698 | } | |
699 | } | |
700 | } | |
701 | return $mails; | |
702 | } | |
e97e9b8f VZ |
703 | |
704 | // function active_emails() {{{2 | |
705 | ||
706 | public function active_emails() | |
707 | { | |
708 | $emails = array(); | |
709 | foreach ($this->emails as $mail) { | |
710 | if ($mail->active) { | |
711 | $emails[] = $mail; | |
712 | } | |
713 | } | |
714 | return $emails; | |
715 | } | |
45282934 VZ |
716 | |
717 | // function get_uid() {{{2 | |
718 | ||
719 | public function get_uid() | |
720 | { | |
12a587df | 721 | return $this->user->id(); |
45282934 | 722 | } |
0337d704 | 723 | } |
724 | ||
a7de4ef7 | 725 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 | 726 | ?> |