Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
12262f13 | 3 | * Copyright (C) 2003-2011 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 | ||
b4503762 SJ |
22 | define('SUCCESS', 1); |
23 | define('ERROR_INACTIVE_REDIRECTION', 2); | |
24 | define('ERROR_INVALID_EMAIL', 3); | |
25 | define('ERROR_LOOP_EMAIL', 4); | |
0337d704 | 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. | |
26ba053e | 30 | function fix_bestalias(User $user) |
0337d704 | 31 | { |
b4503762 SJ |
32 | $count = XDB::fetchOneCell('SELECT COUNT(*) |
33 | FROM email_source_account | |
34 | WHERE uid = {?} AND FIND_IN_SET(\'bestalias\', flags)', | |
35 | $user->id()); | |
11ed26e5 | 36 | |
b4503762 SJ |
37 | if ($count == 1) { |
38 | return; | |
39 | } elseif ($count > 1) { | |
40 | // If too many bestaliases, delete the bestalias flag from all this | |
41 | // user's emails (this should never happen). | |
42 | XDB::execute("UPDATE email_source_account | |
43 | SET flags = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', flags, ','), ',bestalias,', ',')) | |
44 | WHERE uid = {?}", | |
45 | $user->id()); | |
46 | } | |
47 | ||
48 | // If no bestalias is selected, we choose the shortest email which is not | |
49 | // related to a usage name and contains a '.'. | |
50 | XDB::execute("UPDATE email_source_account | |
51 | SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'bestalias') | |
52 | WHERE uid = {?} | |
53 | ORDER BY NOT FIND_IN_SET('usage', flags), email LIKE '%.%', LENGTH(email) | |
54 | LIMIT 1", | |
55 | $user->id()); | |
0337d704 | 56 | } |
57 | ||
441c2451 | 58 | // function valide_email() {{{1 |
11ed26e5 VZ |
59 | // Returns a cleaned-up version of the @p email string. It removes garbage |
60 | // characters, and determines the canonical form (without _ and +) for | |
61 | // Polytechnique.org email addresses. | |
0337d704 | 62 | function valide_email($str) |
63 | { | |
a3a049fc | 64 | global $globals; |
65 | ||
66 | $em = trim(rtrim($str)); | |
67 | $em = str_replace('<', '', $em); | |
68 | $em = str_replace('>', '', $em); | |
97664536 SJ |
69 | if (strpos($em, '@') === false) { |
70 | return; | |
71 | } | |
a3a049fc | 72 | list($ident, $dom) = explode('@', $em); |
97664536 | 73 | if ($dom == $globals->mail->domain || $dom == $globals->mail->domain2) { |
a3a049fc | 74 | list($ident1) = explode('_', $ident); |
75 | list($ident) = explode('+', $ident1); | |
76 | } | |
77 | return $ident . '@' . $dom; | |
0337d704 | 78 | } |
79 | ||
c6310567 | 80 | // function isvalid_email_redirection() {{{1 |
b4503762 SJ |
81 | /** Checks if an email is a suitable redirection. |
82 | * @param $email the email to check | |
c6310567 FB |
83 | * @return BOOL |
84 | */ | |
85 | function isvalid_email_redirection($email) | |
86 | { | |
87 | return isvalid_email($email) && | |
88 | !preg_match("/@(polytechnique\.(org|edu)|melix\.(org|net)|m4x\.org)$/", $email); | |
89 | } | |
90 | ||
180627f3 | 91 | // function ids_from_mails() {{{1 |
b4503762 SJ |
92 | // Converts an array of emails to an array of email => uid, where email is the |
93 | // given email when we found a matching user. | |
180627f3 | 94 | function ids_from_mails(array $emails) |
1605f171 RB |
95 | { |
96 | global $globals; | |
180627f3 | 97 | |
b4503762 SJ |
98 | // Removes duplicates, if any. |
99 | $emails = array_unique($emails); | |
100 | ||
101 | // Formats and splits by domain type (locally managed or external) emails. | |
102 | $domain_emails = array(); | |
103 | $other_emails = array(); | |
1605f171 RB |
104 | foreach ($emails as $email) { |
105 | if (strpos($email, '@') === false) { | |
106 | $user = $email; | |
107 | $domain = $globals->mail->domain2; | |
108 | } else { | |
109 | list($user, $domain) = explode('@', $email); | |
110 | } | |
b4503762 SJ |
111 | if ($domain == $globals->mail->alias_dom || $domain == $globals->mail->alias_dom2 |
112 | || $domain == $globals->mail->domain || $domain == $globals->mail->domain2) { | |
1605f171 RB |
113 | list($user) = explode('+', $user); |
114 | list($user) = explode('_', $user); | |
b4503762 | 115 | $domain_emails[$email] = strtolower($user . '@' . $domain); |
1605f171 | 116 | } else { |
b4503762 | 117 | $other_emails[$email] = strtolower($user . '@' . $domain); |
1605f171 RB |
118 | } |
119 | } | |
180627f3 | 120 | |
b4503762 SJ |
121 | // Retrieves emails from our domains. |
122 | $domain_uids = XDB::fetchAllAssoc('email', | |
123 | 'SELECT email, uid | |
124 | FROM email_source_account | |
125 | WHERE email IN {?}', | |
126 | array_unique($domain_emails)); | |
1605f171 | 127 | |
b4503762 SJ |
128 | // Retrieves emails from redirections. |
129 | $other_uids = XDB::fetchAllAssoc('redirect', | |
130 | 'SELECT redirect, uid | |
131 | FROM email_redirect_account | |
132 | WHERE redirect IN {?}', | |
133 | array_unique($other_emails)); | |
1605f171 | 134 | |
b4503762 SJ |
135 | // Associates given emails with the corresponding uid. |
136 | $uids = array(); | |
137 | foreach (array_merge($domain_emails, $other_emails) as $email => $canonical_email) { | |
138 | if (array_key_exists($canonical_email, $domain_uids)) { | |
139 | $uids[$email] = $domain_uids[$canonical_email]; | |
140 | } elseif (array_key_exists($canonical_email, $other_uids)) { | |
141 | $uids[$email] = $other_uids[$canonical_email]; | |
1605f171 RB |
142 | } |
143 | } | |
144 | ||
145 | return $uids; | |
146 | } | |
147 | ||
441c2451 | 148 | // class Bogo {{{1 |
11ed26e5 | 149 | // The Bogo class represents a spam filtering level in plat/al architecture. |
0337d704 | 150 | class Bogo |
151 | { | |
b4503762 | 152 | private static $states = array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams'); |
a3a049fc | 153 | |
12a587df | 154 | private $user; |
612a2d8a | 155 | private $state; |
a3a049fc | 156 | |
26ba053e | 157 | public function __construct(User $user) |
0337d704 | 158 | { |
12a587df | 159 | if (!$user) { |
3c1e6a1e | 160 | return; |
161 | } | |
11ed26e5 | 162 | |
12a587df | 163 | $this->user = &$user; |
b4503762 SJ |
164 | $res = XDB::query('SELECT action |
165 | FROM email_redirect_account | |
166 | WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')', | |
167 | $user->id()); | |
168 | if ($res->numRows() == 0) { | |
169 | return; | |
3c1e6a1e | 170 | } |
b4503762 | 171 | $this->state = $res->fetchOneCell(); |
0337d704 | 172 | } |
173 | ||
11ed26e5 | 174 | public function change($state) |
0337d704 | 175 | { |
b4503762 SJ |
176 | $this->state = is_int($state) ? self::$states[$state] : $state; |
177 | XDB::execute('UPDATE email_redirect_account | |
178 | SET action = {?} | |
179 | WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')', | |
12a587df | 180 | $this->state, $this->user->id()); |
0337d704 | 181 | } |
182 | ||
612a2d8a | 183 | public function level() |
184 | { | |
b4503762 | 185 | return array_search($this->state, self::$states); |
612a2d8a | 186 | } |
0337d704 | 187 | } |
188 | ||
441c2451 | 189 | // class Email {{{1 |
11ed26e5 | 190 | // Represents an "email address" used as final recipient for plat/al-managed |
b4503762 SJ |
191 | // addresses. |
192 | class Email | |
0337d704 | 193 | { |
b4503762 SJ |
194 | // Lists fields to load automatically. |
195 | static private $field_names = array('rewrite', 'type', 'action', 'broken_date', 'broken_level', 'last', 'hash', 'allow_rewrite'); | |
196 | ||
197 | // Shortname to realname mapping for known mail storage backends. | |
198 | static private $display_names = array( | |
199 | 'imap' => 'Accès de secours aux emails (IMAP)', | |
200 | 'googleapps' => 'Compte Google Apps', | |
201 | ); | |
202 | static private $storage_domains = array( | |
203 | 'imap' => 'imap', | |
204 | 'googleapps' => 'g' | |
205 | ); | |
206 | ||
207 | private $user; | |
612a2d8a | 208 | |
11ed26e5 | 209 | // Basic email properties; $sufficient indicates if the email can be used as |
b4503762 | 210 | // an unique redirection; $redirect contains the delivery email address. |
11ed26e5 VZ |
211 | public $type; |
212 | public $sufficient; | |
612a2d8a | 213 | public $email; |
11ed26e5 | 214 | public $display_email; |
b4503762 SJ |
215 | public $domain; |
216 | public $action; | |
11ed26e5 VZ |
217 | |
218 | // Redirection status properties. | |
612a2d8a | 219 | public $active; |
b4503762 | 220 | public $inactive; |
612a2d8a | 221 | public $broken; |
441c2451 | 222 | public $disabled; |
612a2d8a | 223 | public $rewrite; |
3d17e48f FB |
224 | public $allow_rewrite; |
225 | public $hash; | |
11ed26e5 VZ |
226 | |
227 | // Redirection bounces stats. | |
612a2d8a | 228 | public $last; |
b4503762 SJ |
229 | public $broken_level; |
230 | public $broken_date; | |
0337d704 | 231 | |
b4503762 | 232 | public function __construct(User $user, array $row) |
0337d704 | 233 | { |
b4503762 SJ |
234 | foreach (self::$field_names as $field) { |
235 | if (array_key_exists($field, $row)) { | |
236 | $this->$field = $row[$field]; | |
237 | } | |
238 | } | |
239 | $this->email = $row['redirect']; | |
11ed26e5 | 240 | |
b4503762 SJ |
241 | if (array_key_exists($this->type, Email::$display_names)) { |
242 | $this->display_email = self::$display_names[$this->type]; | |
243 | } else { | |
244 | $this->display_email = $this->email; | |
245 | } | |
246 | foreach (array('active', 'inactive', 'broken', 'disabled') as $status) { | |
247 | $this->$status = ($status == $row['flags']); | |
248 | } | |
249 | $this->sufficient = ($this->type == 'smtp' || $this->type == 'googleapps'); | |
250 | $this->user = &$user; | |
0337d704 | 251 | } |
252 | ||
b4503762 | 253 | // Activates the email address as a redirection. |
11ed26e5 | 254 | public function activate() |
0337d704 | 255 | { |
b4503762 SJ |
256 | if ($this->inactive) { |
257 | XDB::execute('UPDATE email_redirect_account | |
258 | SET broken_level = IF(flags = \'broken\', broken_level - 1, broken_level), flags = \'active\' | |
259 | WHERE uid = {?} AND redirect = {?}', | |
260 | $this->user->id(), $this->email); | |
261 | S::logger()->log('email_on', $this->email . ($this->user->id() != S::v('uid') ? "(admin on {$this->user->login()})" : '')); | |
262 | $this->inactive = false; | |
263 | $this->active = true; | |
0337d704 | 264 | } |
265 | } | |
266 | ||
b4503762 | 267 | // Deactivates the email address as a redirection. |
11ed26e5 | 268 | public function deactivate() |
0337d704 | 269 | { |
0337d704 | 270 | if ($this->active) { |
b4503762 SJ |
271 | XDB::execute('UPDATE email_redirect_account |
272 | SET flags = \'inactive\' | |
273 | WHERE uid = {?} AND redirect = {?}', | |
274 | $this->user->id(), $this->email); | |
275 | S::logger()->log('email_off', $this->email . ($this->user->id() != S::v('uid') ? "(admin on {$this->user->login()})" : "") ); | |
276 | $this->inactive = true; | |
277 | $this->active = false; | |
0337d704 | 278 | } |
279 | } | |
612a2d8a | 280 | |
0337d704 | 281 | |
b4503762 | 282 | // Sets the rewrite rule for the given address. |
11ed26e5 | 283 | public function set_rewrite($rewrite) |
0337d704 | 284 | { |
b4503762 | 285 | if ($this->type != 'smtp' || $this->rewrite == $rewrite) { |
0337d704 | 286 | return; |
287 | } | |
11ed26e5 VZ |
288 | if (!$rewrite || !isvalid_email($rewrite)) { |
289 | $rewrite = ''; | |
12acff5d | 290 | } |
b4503762 SJ |
291 | XDB::execute('UPDATE email_redirect_account |
292 | SET rewrite = {?} | |
293 | WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'', | |
294 | $rewrite, $this->user->id(), $this->email); | |
11ed26e5 | 295 | $this->rewrite = $rewrite; |
3d17e48f FB |
296 | if (!$this->allow_rewrite) { |
297 | global $globals; | |
298 | if (empty($this->hash)) { | |
299 | $this->hash = rand_url_id(); | |
b4503762 SJ |
300 | XDB::execute('UPDATE email_redirect_account |
301 | SET hash = {?} | |
302 | WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'', | |
303 | $this->hash, $this->user->id(), $this->email); | |
3d17e48f | 304 | } |
3d17e48f FB |
305 | $mail = new PlMailer('emails/rewrite-in.mail.tpl'); |
306 | $mail->assign('mail', $this); | |
21c7c593 | 307 | $mail->assign('user', $this->user); |
3d17e48f | 308 | $mail->assign('baseurl', $globals->baseurl); |
c66de9a0 FB |
309 | $mail->assign('sitename', $globals->core->sitename); |
310 | $mail->assign('to', $this->email); | |
21c7c593 | 311 | $mail->send($this->user->isEmailFormatHtml()); |
3d17e48f | 312 | } |
0337d704 | 313 | } |
314 | ||
441c2451 | 315 | |
b4503762 | 316 | // Resets the error counts associated with the redirection. |
11ed26e5 | 317 | public function clean_errors() |
441c2451 | 318 | { |
b4503762 SJ |
319 | if ($this->type != 'smtp') { |
320 | return; | |
321 | } | |
dd70cd28 | 322 | if (!S::admin()) { |
441c2451 | 323 | return false; |
324 | } | |
b4503762 SJ |
325 | $this->broken = 0; |
326 | $this->broken_level = 0; | |
327 | $this->last = 0; | |
328 | return XDB::execute('UPDATE email_redirect_account | |
329 | SET broken_level = 0, broken_date = 0, last = 0 | |
330 | WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'', | |
12a587df | 331 | $this->user->id(), $this->email); |
11ed26e5 VZ |
332 | } |
333 | ||
11ed26e5 | 334 | |
b4503762 SJ |
335 | // Email backend capabilities ('rewrite' refers to From: rewrite for mails |
336 | // forwarded by Polytechnique.org's MXs; 'removable' indicates if the email | |
337 | // can be definitively removed; 'disable' indicates if the email has a third | |
338 | // status 'disabled' in addition to 'active' and 'inactive'). | |
11ed26e5 VZ |
339 | public function has_rewrite() |
340 | { | |
b4503762 | 341 | return ($this->type == 'smtp'); |
11ed26e5 VZ |
342 | } |
343 | ||
11ed26e5 VZ |
344 | public function is_removable() |
345 | { | |
b4503762 | 346 | return ($this->type == 'smtp'); |
11ed26e5 VZ |
347 | } |
348 | ||
11ed26e5 VZ |
349 | public function has_disable() |
350 | { | |
351 | return true; | |
441c2451 | 352 | } |
afde0a3a | 353 | |
b4503762 | 354 | public function is_redirection() |
afde0a3a | 355 | { |
b4503762 | 356 | return ($this->type == 'smtp'); |
afde0a3a VZ |
357 | } |
358 | ||
359 | // Returns the list of allowed storages for the @p user. | |
b4503762 | 360 | static private function get_allowed_storages(User $user) |
afde0a3a VZ |
361 | { |
362 | global $globals; | |
363 | $storages = array(); | |
364 | ||
365 | // Google Apps storage is available for users with valid Google Apps account. | |
366 | require_once 'googleapps.inc.php'; | |
367 | if ($globals->mailstorage->googleapps_domain && | |
12a587df | 368 | GoogleAppsAccount::account_status($user->id()) == 'active') { |
afde0a3a VZ |
369 | $storages[] = 'googleapps'; |
370 | } | |
371 | ||
372 | // IMAP storage is always visible to administrators, and is allowed for | |
373 | // everyone when the service is marked as 'active'. | |
dd70cd28 | 374 | if ($globals->mailstorage->imap_active || S::admin()) { |
afde0a3a VZ |
375 | $storages[] = 'imap'; |
376 | } | |
377 | ||
378 | return $storages; | |
379 | } | |
380 | ||
b4503762 | 381 | static public function activate_storage(User $user, $storage) |
afde0a3a | 382 | { |
b4503762 | 383 | Platal::assert(in_array($storage, self::get_allowed_storages($user))); |
afde0a3a | 384 | |
b4503762 SJ |
385 | if (!self::is_active_storage($user, $storage)) { |
386 | global $globals; | |
387 | ||
388 | XDB::execute('INSERT INTO email_redirect_account (uid, type, redirect, flags) | |
389 | VALUES ({?}, {?}, {?}, \'active\')', | |
390 | $user->id(), $storage, | |
391 | $user->hruid . '@' . self::$storage_domains[$storage] . '.' . $globals->mail->domain); | |
392 | } | |
afde0a3a VZ |
393 | } |
394 | ||
b4503762 | 395 | static public function deactivate_storage(User $user, $storage) |
afde0a3a | 396 | { |
b4503762 SJ |
397 | if (in_array($storage, self::$storage_domains)) { |
398 | XDB::execute('DELETE FROM email_redirect_account | |
399 | WHERE uid = {?} AND type = {?}', | |
400 | $user->id(), $storage); | |
401 | } | |
afde0a3a VZ |
402 | } |
403 | ||
b4503762 | 404 | static public function is_active_storage(User $user, $storage) |
afde0a3a | 405 | { |
b4503762 SJ |
406 | if (!in_array($storage, self::$storage_domains)) { |
407 | return false; | |
afde0a3a | 408 | } |
b4503762 SJ |
409 | $res = XDB::fetchOneCell('SELECT COUNT(*) |
410 | FROM email_redirect_account | |
411 | WHERE uid = {?} AND type = {?} AND flags = \'active\')', | |
412 | $user->id(), $storage); | |
413 | return !is_null($res) && $res > 0; | |
afde0a3a | 414 | } |
afde0a3a | 415 | } |
441c2451 | 416 | // class Redirect {{{1 |
11ed26e5 VZ |
417 | // Redirect is a placeholder class for an user's active redirections (third-party |
418 | // redirection email, or Polytechnique.org mail storages). | |
0337d704 | 419 | class Redirect |
420 | { | |
b4503762 | 421 | private $flags = 'active'; |
12a587df | 422 | private $user; |
612a2d8a | 423 | |
424 | public $emails; | |
425 | public $bogo; | |
0337d704 | 426 | |
26ba053e | 427 | public function __construct(User $user) |
0337d704 | 428 | { |
12a587df VZ |
429 | $this->user = &$user; |
430 | $this->bogo = new Bogo($user); | |
11ed26e5 | 431 | |
afde0a3a | 432 | // Adds third-party email redirections. |
b4503762 SJ |
433 | $res = XDB::iterator('SELECT redirect, rewrite, type, action, broken_date, broken_level, last, flags, hash, allow_rewrite |
434 | FROM email_redirect_account | |
435 | WHERE uid = {?} AND type != \'homonym\'', | |
436 | $user->id()); | |
437 | $this->emails = array(); | |
0337d704 | 438 | while ($row = $res->next()) { |
b4503762 | 439 | $this->emails[] = new Email($user, $row); |
afde0a3a | 440 | } |
0337d704 | 441 | } |
442 | ||
612a2d8a | 443 | public function other_active($email) |
0337d704 | 444 | { |
445 | foreach ($this->emails as $mail) { | |
11ed26e5 | 446 | if ($mail->email != $email && $mail->active && $mail->sufficient) { |
0337d704 | 447 | return true; |
448 | } | |
449 | } | |
450 | return false; | |
451 | } | |
452 | ||
612a2d8a | 453 | public function delete_email($email) |
0337d704 | 454 | { |
0337d704 | 455 | if (!$this->other_active($email)) { |
456 | return ERROR_INACTIVE_REDIRECTION; | |
457 | } | |
b4503762 SJ |
458 | XDB::execute('DELETE FROM email_redirect_account |
459 | WHERE uid = {?} AND redirect = {?} AND type != \'homonym\'', | |
460 | $this->user->id(), $email); | |
12a587df | 461 | S::logger()->log('email_del', $email . ($this->user->id() != S::v('uid') ? " (admin on {$this->user->login()})" : "")); |
11ed26e5 VZ |
462 | foreach ($this->emails as $i => $mail) { |
463 | if ($email == $mail->email) { | |
0337d704 | 464 | unset($this->emails[$i]); |
465 | } | |
3c1e6a1e | 466 | } |
ccdbc270 | 467 | check_redirect($this); |
0337d704 | 468 | return SUCCESS; |
469 | } | |
470 | ||
612a2d8a | 471 | public function add_email($email) |
0337d704 | 472 | { |
0337d704 | 473 | $email_stripped = strtolower(trim($email)); |
474 | if (!isvalid_email($email_stripped)) { | |
475 | return ERROR_INVALID_EMAIL; | |
476 | } | |
477 | if (!isvalid_email_redirection($email_stripped)) { | |
478 | return ERROR_LOOP_EMAIL; | |
479 | } | |
00ba8a74 | 480 | // If the email was already present for this user, we reset it to the default values, we thus use REPLACE INTO. |
b4503762 | 481 | XDB::execute('REPLACE INTO email_redirect_account (uid, redirect, flags) |
00ba8a74 SJ |
482 | VALUES ({?}, {?}, \'active\')', |
483 | $this->user->id(), $email); | |
3c1e6a1e | 484 | if ($logger = S::v('log', null)) { // may be absent --> step4.php |
12a587df | 485 | S::logger()->log('email_add', $email . ($this->user->id() != S::v('uid') ? " (admin on {$this->user->login()})" : "")); |
0337d704 | 486 | } |
3c1e6a1e | 487 | foreach ($this->emails as $mail) { |
488 | if ($mail->email == $email_stripped) { | |
0337d704 | 489 | return SUCCESS; |
490 | } | |
3c1e6a1e | 491 | } |
b4503762 SJ |
492 | $this->emails[] = new Email($this->user, array( |
493 | 'redirect' => $email, | |
494 | 'rewrite' => '', | |
495 | 'type' => 'smtp', | |
496 | 'action' => 'default', | |
497 | 'broken_date' => '0000-00-00', | |
498 | 'broken_level' => 0, | |
499 | 'last' => '0000-00-00', | |
500 | 'flags' => 'active', | |
501 | 'hash' => null, | |
502 | 'allow_rewrite' => 0 | |
503 | )); | |
ca6d07f4 | 504 | |
505 | // security stuff | |
12a587df | 506 | check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->user->login()); |
ccdbc270 | 507 | check_redirect($this); |
0337d704 | 508 | return SUCCESS; |
509 | } | |
510 | ||
612a2d8a | 511 | public function modify_email($emails_actifs, $emails_rewrite) |
0337d704 | 512 | { |
b4503762 SJ |
513 | foreach ($this->emails as &$email) { |
514 | if (in_array($email->email, $emails_actifs)) { | |
515 | $email->activate(); | |
3c1e6a1e | 516 | } else { |
b4503762 | 517 | $email->deactivate(); |
3c1e6a1e | 518 | } |
b4503762 | 519 | $email->set_rewrite($emails_rewrite[$email->email]); |
0337d704 | 520 | } |
ccdbc270 | 521 | check_redirect($this); |
b4503762 | 522 | return SUCCESS; |
0337d704 | 523 | } |
524 | ||
eaf30d86 | 525 | public function modify_one_email($email, $activate) |
ccdbc270 | 526 | { |
b7582015 | 527 | $allinactive = true; |
528 | $thisone = false; | |
8ffa657a | 529 | foreach ($this->emails as $i=>$mail) { |
530 | if ($mail->email == $email) { | |
b7582015 | 531 | $thisone = $i; |
8ffa657a | 532 | } |
11ed26e5 | 533 | $allinactive &= !$mail->active || !$mail->sufficient || $mail->email == $email; |
8ffa657a | 534 | } |
b7582015 | 535 | if ($thisone === false) { |
536 | return ERROR_INVALID_EMAIL; | |
537 | } | |
ccdbc270 | 538 | if ($allinactive || $activate) { |
11ed26e5 | 539 | $this->emails[$thisone]->activate(); |
ccdbc270 | 540 | } else { |
11ed26e5 | 541 | $this->emails[$thisone]->deactivate(); |
ccdbc270 | 542 | } |
543 | check_redirect($this); | |
b7582015 | 544 | if ($allinactive && !$activate) { |
545 | return ERROR_INACTIVE_REDIRECTION; | |
eaf30d86 | 546 | } |
b4503762 | 547 | return SUCCESS; |
8ffa657a | 548 | } |
549 | ||
612a2d8a | 550 | public function modify_one_email_redirect($email, $redirect) |
551 | { | |
441c2451 | 552 | foreach ($this->emails as &$mail) { |
612a2d8a | 553 | if ($mail->email == $email) { |
11ed26e5 | 554 | $mail->set_rewrite($redirect); |
ccdbc270 | 555 | check_redirect($this); |
556 | return; | |
612a2d8a | 557 | } |
558 | } | |
559 | } | |
441c2451 | 560 | |
11ed26e5 | 561 | public function clean_errors($email) |
441c2451 | 562 | { |
563 | foreach ($this->emails as &$mail) { | |
564 | if ($mail->email == $email) { | |
e1547442 | 565 | check_redirect($this); |
11ed26e5 | 566 | return $mail->clean_errors(); |
441c2451 | 567 | } |
568 | } | |
569 | return false; | |
570 | } | |
571 | ||
441c2451 | 572 | public function disable() |
573 | { | |
b4503762 | 574 | XDB::execute("UPDATE email_redirect_account |
441c2451 | 575 | SET flags = 'disable' |
20398e42 | 576 | WHERE flags = 'active' AND uid = {?}", $this->user->id()); |
441c2451 | 577 | foreach ($this->emails as &$mail) { |
11ed26e5 | 578 | if ($mail->active && $mail->has_disable()) { |
441c2451 | 579 | $mail->disabled = true; |
580 | $mail->active = false; | |
581 | } | |
582 | } | |
e1547442 | 583 | check_redirect($this); |
441c2451 | 584 | } |
585 | ||
441c2451 | 586 | public function enable() |
587 | { | |
b4503762 | 588 | XDB::execute("UPDATE email_redirect_account |
441c2451 | 589 | SET flags = 'active' |
20398e42 | 590 | WHERE flags = 'disable' AND uid = {?}", $this->user->id()); |
441c2451 | 591 | foreach ($this->emails as &$mail) { |
592 | if ($mail->disabled) { | |
441c2451 | 593 | $mail->disabled = false; |
b4503762 | 594 | $mail->active = true; |
441c2451 | 595 | } |
e1547442 | 596 | check_redirect($this); |
441c2451 | 597 | } |
598 | } | |
599 | ||
612a2d8a | 600 | public function get_broken_mx() |
ccdbc270 | 601 | { |
3083bd93 | 602 | $res = XDB::query("SELECT host, text |
8af1d78f FB |
603 | FROM mx_watch |
604 | WHERE state != 'ok'"); | |
120bd636 | 605 | if (!$res->numRows()) { |
ccdbc270 | 606 | return array(); |
607 | } | |
c754cf5b | 608 | $mxs = $res->fetchAllAssoc(); |
ccdbc270 | 609 | $mails = array(); |
610 | foreach ($this->emails as &$mail) { | |
11ed26e5 | 611 | if ($mail->active && strstr($mail->email, '@') !== false) { |
ccdbc270 | 612 | list(,$domain) = explode('@', $mail->email); |
613 | getmxrr($domain, $lcl_mxs); | |
614 | if (empty($lcl_mxs)) { | |
615 | $lcl_mxs = array($domain); | |
616 | } | |
617 | $broken = false; | |
618 | foreach ($mxs as &$mx) { | |
619 | foreach ($lcl_mxs as $lcl) { | |
c754cf5b | 620 | if (fnmatch($mx['host'], $lcl)) { |
ccdbc270 | 621 | $broken = $mx['text']; |
622 | break; | |
623 | } | |
624 | } | |
625 | if ($broken) { | |
3083bd93 | 626 | $mails[] = array('mail' => $mail->email, 'text' => $broken); |
f653ff3d | 627 | break; |
ccdbc270 | 628 | } |
629 | } | |
630 | } | |
631 | } | |
632 | return $mails; | |
633 | } | |
e97e9b8f | 634 | |
e97e9b8f VZ |
635 | public function active_emails() |
636 | { | |
637 | $emails = array(); | |
638 | foreach ($this->emails as $mail) { | |
639 | if ($mail->active) { | |
640 | $emails[] = $mail; | |
641 | } | |
642 | } | |
643 | return $emails; | |
644 | } | |
45282934 | 645 | |
45282934 VZ |
646 | public function get_uid() |
647 | { | |
12a587df | 648 | return $this->user->id(); |
45282934 | 649 | } |
0337d704 | 650 | } |
651 | ||
a7de4ef7 | 652 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 | 653 | ?> |