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