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