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