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