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