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