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