| 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 = 'user') |
| 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 = 'user') |
| 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 = 'user') |
| 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 = \'user\'', |
| 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 = \'user\'', |
| 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 = \'user\' |
| 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 = IF(broken_level > 1, 3, 2) |
| 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 | $count = XDB::fetchOneCell('SELECT COUNT(*) |
| 180 | FROM email_source_account |
| 181 | WHERE uid = {?} AND FIND_IN_SET(\'bestalias\', flags) AND expire IS NULL', |
| 182 | $user->id()); |
| 183 | |
| 184 | if ($count == 1) { |
| 185 | return; |
| 186 | } elseif ($count > 1) { |
| 187 | // If too many bestaliases, delete the bestalias flag from all this |
| 188 | // user's emails (this should never happen). |
| 189 | XDB::execute("UPDATE email_source_account |
| 190 | SET flags = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', flags, ','), ',bestalias,', ',')) |
| 191 | WHERE uid = {?}", |
| 192 | $user->id()); |
| 193 | } |
| 194 | |
| 195 | // If no bestalias is selected, we choose the shortest email which is not |
| 196 | // related to a usage name and contains a '.'. |
| 197 | XDB::execute("UPDATE email_source_account |
| 198 | SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'bestalias') |
| 199 | WHERE uid = {?} AND expire IS NULL |
| 200 | ORDER BY NOT FIND_IN_SET('usage', flags), email LIKE '%.%', LENGTH(email) |
| 201 | LIMIT 1", |
| 202 | $user->id()); |
| 203 | } |
| 204 | |
| 205 | // function valide_email() {{{1 |
| 206 | // Returns a cleaned-up version of the @p email string. It removes garbage |
| 207 | // characters, and determines the canonical form (without _ and +) for |
| 208 | // Polytechnique.org email addresses. |
| 209 | function valide_email($str) |
| 210 | { |
| 211 | global $globals; |
| 212 | |
| 213 | $em = trim(rtrim($str)); |
| 214 | $em = str_replace('<', '', $em); |
| 215 | $em = str_replace('>', '', $em); |
| 216 | if (strpos($em, '@') === false) { |
| 217 | return; |
| 218 | } |
| 219 | list($ident, $dom) = explode('@', $em); |
| 220 | if ($dom == $globals->mail->domain || $dom == $globals->mail->domain2) { |
| 221 | list($ident1) = explode('_', $ident); |
| 222 | list($ident) = explode('+', $ident1); |
| 223 | } |
| 224 | return $ident . '@' . $dom; |
| 225 | } |
| 226 | |
| 227 | // function isvalid_email_redirection() {{{1 |
| 228 | /** Checks if an email is a suitable redirection. |
| 229 | * @param $email the email to check |
| 230 | * @return BOOL |
| 231 | */ |
| 232 | function isvalid_email_redirection($email) |
| 233 | { |
| 234 | return isvalid_email($email) && |
| 235 | !preg_match("/@(polytechnique\.(org|edu)|melix\.(org|net)|m4x\.org)$/", $email); |
| 236 | } |
| 237 | |
| 238 | // function ids_from_mails() {{{1 |
| 239 | // Converts an array of emails to an array of email => uid, where email is the |
| 240 | // given email when we found a matching user. |
| 241 | function ids_from_mails(array $emails) |
| 242 | { |
| 243 | global $globals; |
| 244 | |
| 245 | // Removes duplicates, if any. |
| 246 | $emails = array_unique($emails); |
| 247 | |
| 248 | // Formats and splits by domain type (locally managed or external) emails. |
| 249 | $domain_emails = array(); |
| 250 | $other_emails = array(); |
| 251 | foreach ($emails as $email) { |
| 252 | if (strpos($email, '@') === false) { |
| 253 | $user = $email; |
| 254 | $domain = $globals->mail->domain2; |
| 255 | } else { |
| 256 | list($user, $domain) = explode('@', $email); |
| 257 | } |
| 258 | if ($domain == $globals->mail->alias_dom || $domain == $globals->mail->alias_dom2 |
| 259 | || $domain == $globals->mail->domain || $domain == $globals->mail->domain2) { |
| 260 | list($user) = explode('+', $user); |
| 261 | list($user) = explode('_', $user); |
| 262 | $domain_emails[$email] = strtolower($user . '@' . $domain); |
| 263 | } else { |
| 264 | $other_emails[$email] = strtolower($user . '@' . $domain); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // Retrieves emails from our domains. |
| 269 | $domain_uids = XDB::fetchAllAssoc('email', |
| 270 | 'SELECT email, uid |
| 271 | FROM email_source_account |
| 272 | WHERE email IN {?}', |
| 273 | array_unique($domain_emails)); |
| 274 | |
| 275 | // Retrieves emails from redirections. |
| 276 | $other_uids = XDB::fetchAllAssoc('redirect', |
| 277 | 'SELECT redirect, uid |
| 278 | FROM email_redirect_account |
| 279 | WHERE redirect IN {?}', |
| 280 | array_unique($other_emails)); |
| 281 | |
| 282 | // Associates given emails with the corresponding uid. |
| 283 | $uids = array(); |
| 284 | foreach (array_merge($domain_emails, $other_emails) as $email => $canonical_email) { |
| 285 | if (array_key_exists($canonical_email, $domain_uids)) { |
| 286 | $uids[$email] = $domain_uids[$canonical_email]; |
| 287 | } elseif (array_key_exists($canonical_email, $other_uids)) { |
| 288 | $uids[$email] = $other_uids[$canonical_email]; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | return $uids; |
| 293 | } |
| 294 | |
| 295 | // class Bogo {{{1 |
| 296 | // The Bogo class represents a spam filtering level in plat/al architecture. |
| 297 | class Bogo |
| 298 | { |
| 299 | private static $states = array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams'); |
| 300 | |
| 301 | private $user; |
| 302 | private $state; |
| 303 | |
| 304 | public function __construct(User $user) |
| 305 | { |
| 306 | if (!$user) { |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | $this->user = &$user; |
| 311 | $res = XDB::query('SELECT action |
| 312 | FROM email_redirect_account |
| 313 | WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')', |
| 314 | $user->id()); |
| 315 | if ($res->numRows() == 0) { |
| 316 | return; |
| 317 | } |
| 318 | $this->state = $res->fetchOneCell(); |
| 319 | } |
| 320 | |
| 321 | public function change($state) |
| 322 | { |
| 323 | $this->state = is_int($state) ? self::$states[$state] : $state; |
| 324 | XDB::execute('UPDATE email_redirect_account |
| 325 | SET action = {?} |
| 326 | WHERE uid = {?} AND (type = \'smtp\' OR type = \'googleapps\')', |
| 327 | $this->state, $this->user->id()); |
| 328 | } |
| 329 | |
| 330 | public function level() |
| 331 | { |
| 332 | return array_search($this->state, self::$states); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | // class Email {{{1 |
| 337 | // Represents an "email address" used as final recipient for plat/al-managed |
| 338 | // addresses. |
| 339 | class Email |
| 340 | { |
| 341 | // Lists fields to load automatically. |
| 342 | static private $field_names = array('rewrite', 'type', 'action', 'broken_date', 'broken_level', 'last', 'hash', 'allow_rewrite'); |
| 343 | |
| 344 | // Shortname to realname mapping for known mail storage backends. |
| 345 | static private $display_names = array( |
| 346 | 'imap' => 'Accès de secours aux emails (IMAP)', |
| 347 | 'googleapps' => 'Compte Google Apps', |
| 348 | ); |
| 349 | static private $storage_domains = array( |
| 350 | 'imap' => 'imap', |
| 351 | 'googleapps' => 'g' |
| 352 | ); |
| 353 | |
| 354 | private $user; |
| 355 | |
| 356 | // Basic email properties; $sufficient indicates if the email can be used as |
| 357 | // an unique redirection; $redirect contains the delivery email address. |
| 358 | public $type; |
| 359 | public $sufficient; |
| 360 | public $email; |
| 361 | public $display_email; |
| 362 | public $domain; |
| 363 | public $action; |
| 364 | |
| 365 | // Redirection status properties. |
| 366 | public $active; |
| 367 | public $inactive; |
| 368 | public $broken; |
| 369 | public $disabled; |
| 370 | public $rewrite; |
| 371 | public $allow_rewrite; |
| 372 | public $hash; |
| 373 | |
| 374 | // Redirection bounces stats. |
| 375 | public $last; |
| 376 | public $broken_level; |
| 377 | public $broken_date; |
| 378 | |
| 379 | public function __construct(User $user, array $row) |
| 380 | { |
| 381 | foreach (self::$field_names as $field) { |
| 382 | if (array_key_exists($field, $row)) { |
| 383 | $this->$field = $row[$field]; |
| 384 | } |
| 385 | } |
| 386 | $this->email = $row['redirect']; |
| 387 | |
| 388 | if (array_key_exists($this->type, Email::$display_names)) { |
| 389 | $this->display_email = self::$display_names[$this->type]; |
| 390 | } else { |
| 391 | $this->display_email = $this->email; |
| 392 | } |
| 393 | foreach (array('active', 'inactive', 'broken', 'disabled') as $status) { |
| 394 | $this->$status = ($status == $row['flags']); |
| 395 | } |
| 396 | $this->sufficient = ($this->type == 'smtp' || $this->type == 'googleapps'); |
| 397 | $this->user = &$user; |
| 398 | } |
| 399 | |
| 400 | // Activates the email address as a redirection. |
| 401 | public function activate() |
| 402 | { |
| 403 | if ($this->inactive) { |
| 404 | XDB::execute('UPDATE email_redirect_account |
| 405 | SET broken_level = IF(flags = \'broken\', broken_level - 1, broken_level), flags = \'active\' |
| 406 | WHERE uid = {?} AND redirect = {?}', |
| 407 | $this->user->id(), $this->email); |
| 408 | S::logger()->log('email_on', $this->email . ($this->user->id() != S::v('uid') ? "(admin on {$this->user->login()})" : '')); |
| 409 | $this->inactive = false; |
| 410 | $this->active = true; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | // Deactivates the email address as a redirection. |
| 415 | public function deactivate() |
| 416 | { |
| 417 | if ($this->active) { |
| 418 | XDB::execute('UPDATE email_redirect_account |
| 419 | SET flags = \'inactive\' |
| 420 | WHERE uid = {?} AND redirect = {?}', |
| 421 | $this->user->id(), $this->email); |
| 422 | S::logger()->log('email_off', $this->email . ($this->user->id() != S::v('uid') ? "(admin on {$this->user->login()})" : "") ); |
| 423 | $this->inactive = true; |
| 424 | $this->active = false; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | |
| 429 | // Sets the rewrite rule for the given address. |
| 430 | public function set_rewrite($rewrite) |
| 431 | { |
| 432 | if ($this->type != 'smtp' || $this->rewrite == $rewrite) { |
| 433 | return; |
| 434 | } |
| 435 | if (!$rewrite || !isvalid_email($rewrite)) { |
| 436 | $rewrite = ''; |
| 437 | } |
| 438 | XDB::execute('UPDATE email_redirect_account |
| 439 | SET rewrite = {?} |
| 440 | WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'', |
| 441 | $rewrite, $this->user->id(), $this->email); |
| 442 | $this->rewrite = $rewrite; |
| 443 | if (!$this->allow_rewrite) { |
| 444 | global $globals; |
| 445 | if (empty($this->hash)) { |
| 446 | $this->hash = rand_url_id(); |
| 447 | XDB::execute('UPDATE email_redirect_account |
| 448 | SET hash = {?} |
| 449 | WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'', |
| 450 | $this->hash, $this->user->id(), $this->email); |
| 451 | } |
| 452 | $mail = new PlMailer('emails/rewrite-in.mail.tpl'); |
| 453 | $mail->assign('mail', $this); |
| 454 | $mail->assign('user', $this->user); |
| 455 | $mail->assign('baseurl', $globals->baseurl); |
| 456 | $mail->assign('sitename', $globals->core->sitename); |
| 457 | $mail->assign('to', $this->email); |
| 458 | $mail->send($this->user->isEmailFormatHtml()); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | |
| 463 | // Resets the error counts associated with the redirection. |
| 464 | public function clean_errors() |
| 465 | { |
| 466 | if ($this->type != 'smtp') { |
| 467 | return; |
| 468 | } |
| 469 | if (!S::admin()) { |
| 470 | return false; |
| 471 | } |
| 472 | $this->broken = 0; |
| 473 | $this->broken_level = 0; |
| 474 | $this->last = 0; |
| 475 | return XDB::execute('UPDATE email_redirect_account |
| 476 | SET broken_level = 0, broken_date = 0, last = 0 |
| 477 | WHERE uid = {?} AND redirect = {?} AND type = \'smtp\'', |
| 478 | $this->user->id(), $this->email); |
| 479 | } |
| 480 | |
| 481 | |
| 482 | // Email backend capabilities ('rewrite' refers to From: rewrite for mails |
| 483 | // forwarded by Polytechnique.org's MXs; 'removable' indicates if the email |
| 484 | // can be definitively removed; 'disable' indicates if the email has a third |
| 485 | // status 'disabled' in addition to 'active' and 'inactive'). |
| 486 | public function has_rewrite() |
| 487 | { |
| 488 | return ($this->type == 'smtp'); |
| 489 | } |
| 490 | |
| 491 | public function is_removable() |
| 492 | { |
| 493 | return ($this->type == 'smtp'); |
| 494 | } |
| 495 | |
| 496 | public function has_disable() |
| 497 | { |
| 498 | return true; |
| 499 | } |
| 500 | |
| 501 | public function is_redirection() |
| 502 | { |
| 503 | return ($this->type == 'smtp'); |
| 504 | } |
| 505 | |
| 506 | // Returns the list of allowed storages for the @p user. |
| 507 | static private function get_allowed_storages(User $user) |
| 508 | { |
| 509 | global $globals; |
| 510 | $storages = array(); |
| 511 | |
| 512 | // Google Apps storage is available for users with valid Google Apps account. |
| 513 | require_once 'googleapps.inc.php'; |
| 514 | if ($globals->mailstorage->googleapps_domain && |
| 515 | GoogleAppsAccount::account_status($user->id()) == 'active') { |
| 516 | $storages[] = 'googleapps'; |
| 517 | } |
| 518 | |
| 519 | // IMAP storage is always visible to administrators, and is allowed for |
| 520 | // everyone when the service is marked as 'active'. |
| 521 | if ($globals->mailstorage->imap_active || S::admin()) { |
| 522 | $storages[] = 'imap'; |
| 523 | } |
| 524 | |
| 525 | return $storages; |
| 526 | } |
| 527 | |
| 528 | static public function activate_storage(User $user, $storage) |
| 529 | { |
| 530 | Platal::assert(in_array($storage, self::get_allowed_storages($user))); |
| 531 | |
| 532 | if (!self::is_active_storage($user, $storage)) { |
| 533 | global $globals; |
| 534 | |
| 535 | XDB::execute('INSERT INTO email_redirect_account (uid, type, redirect, flags) |
| 536 | VALUES ({?}, {?}, {?}, \'active\')', |
| 537 | $user->id(), $storage, |
| 538 | $user->hruid . '@' . self::$storage_domains[$storage] . '.' . $globals->mail->domain); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | static public function deactivate_storage(User $user, $storage) |
| 543 | { |
| 544 | if (in_array($storage, self::$storage_domains)) { |
| 545 | XDB::execute('DELETE FROM email_redirect_account |
| 546 | WHERE uid = {?} AND type = {?}', |
| 547 | $user->id(), $storage); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | static public function is_active_storage(User $user, $storage) |
| 552 | { |
| 553 | if (!in_array($storage, self::$storage_domains)) { |
| 554 | return false; |
| 555 | } |
| 556 | $res = XDB::fetchOneCell('SELECT COUNT(*) |
| 557 | FROM email_redirect_account |
| 558 | WHERE uid = {?} AND type = {?} AND flags = \'active\')', |
| 559 | $user->id(), $storage); |
| 560 | return !is_null($res) && $res > 0; |
| 561 | } |
| 562 | } |
| 563 | // class Redirect {{{1 |
| 564 | // Redirect is a placeholder class for an user's active redirections (third-party |
| 565 | // redirection email, or Polytechnique.org mail storages). |
| 566 | class Redirect |
| 567 | { |
| 568 | private $flags = 'active'; |
| 569 | private $user; |
| 570 | |
| 571 | public $emails; |
| 572 | public $bogo; |
| 573 | |
| 574 | public function __construct(User $user) |
| 575 | { |
| 576 | $this->user = &$user; |
| 577 | $this->bogo = new Bogo($user); |
| 578 | |
| 579 | // Adds third-party email redirections. |
| 580 | $res = XDB::iterator('SELECT redirect, rewrite, type, action, broken_date, broken_level, last, flags, hash, allow_rewrite |
| 581 | FROM email_redirect_account |
| 582 | WHERE uid = {?} AND type != \'homonym\'', |
| 583 | $user->id()); |
| 584 | $this->emails = array(); |
| 585 | while ($row = $res->next()) { |
| 586 | $this->emails[] = new Email($user, $row); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | public function other_active($email) |
| 591 | { |
| 592 | foreach ($this->emails as $mail) { |
| 593 | if ($mail->email != $email && $mail->active && $mail->sufficient) { |
| 594 | return true; |
| 595 | } |
| 596 | } |
| 597 | return false; |
| 598 | } |
| 599 | |
| 600 | public function delete_email($email) |
| 601 | { |
| 602 | if (!$this->other_active($email)) { |
| 603 | return ERROR_INACTIVE_REDIRECTION; |
| 604 | } |
| 605 | XDB::execute('DELETE FROM email_redirect_account |
| 606 | WHERE uid = {?} AND redirect = {?} AND type != \'homonym\'', |
| 607 | $this->user->id(), $email); |
| 608 | S::logger()->log('email_del', $email . ($this->user->id() != S::v('uid') ? " (admin on {$this->user->login()})" : "")); |
| 609 | foreach ($this->emails as $i => $mail) { |
| 610 | if ($email == $mail->email) { |
| 611 | unset($this->emails[$i]); |
| 612 | } |
| 613 | } |
| 614 | check_redirect($this); |
| 615 | return SUCCESS; |
| 616 | } |
| 617 | |
| 618 | public function add_email($email) |
| 619 | { |
| 620 | $email_stripped = strtolower(trim($email)); |
| 621 | if (!isvalid_email($email_stripped)) { |
| 622 | return ERROR_INVALID_EMAIL; |
| 623 | } |
| 624 | if (!isvalid_email_redirection($email_stripped)) { |
| 625 | return ERROR_LOOP_EMAIL; |
| 626 | } |
| 627 | // If the email was already present for this user, we reset it to the default values, we thus use REPLACE INTO. |
| 628 | XDB::execute('REPLACE INTO email_redirect_account (uid, redirect, flags) |
| 629 | VALUES ({?}, {?}, \'active\')', |
| 630 | $this->user->id(), $email); |
| 631 | if ($logger = S::v('log', null)) { // may be absent --> step4.php |
| 632 | S::logger()->log('email_add', $email . ($this->user->id() != S::v('uid') ? " (admin on {$this->user->login()})" : "")); |
| 633 | } |
| 634 | foreach ($this->emails as $mail) { |
| 635 | if ($mail->email == $email_stripped) { |
| 636 | return SUCCESS; |
| 637 | } |
| 638 | } |
| 639 | $this->emails[] = new Email($this->user, array( |
| 640 | 'redirect' => $email, |
| 641 | 'rewrite' => '', |
| 642 | 'type' => 'smtp', |
| 643 | 'action' => 'default', |
| 644 | 'broken_date' => '0000-00-00', |
| 645 | 'broken_level' => 0, |
| 646 | 'last' => '0000-00-00', |
| 647 | 'flags' => 'active', |
| 648 | 'hash' => null, |
| 649 | 'allow_rewrite' => 0 |
| 650 | )); |
| 651 | |
| 652 | // security stuff |
| 653 | check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->user->login()); |
| 654 | check_redirect($this); |
| 655 | return SUCCESS; |
| 656 | } |
| 657 | |
| 658 | public function modify_email($emails_actifs, $emails_rewrite) |
| 659 | { |
| 660 | foreach ($this->emails as &$email) { |
| 661 | if (in_array($email->email, $emails_actifs)) { |
| 662 | $email->activate(); |
| 663 | } else { |
| 664 | $email->deactivate(); |
| 665 | } |
| 666 | $email->set_rewrite($emails_rewrite[$email->email]); |
| 667 | } |
| 668 | check_redirect($this); |
| 669 | return SUCCESS; |
| 670 | } |
| 671 | |
| 672 | public function modify_one_email($email, $activate) |
| 673 | { |
| 674 | $allinactive = true; |
| 675 | $thisone = false; |
| 676 | foreach ($this->emails as $i=>$mail) { |
| 677 | if ($mail->email == $email) { |
| 678 | $thisone = $i; |
| 679 | } |
| 680 | $allinactive &= !$mail->active || !$mail->sufficient || $mail->email == $email; |
| 681 | } |
| 682 | if ($thisone === false) { |
| 683 | return ERROR_INVALID_EMAIL; |
| 684 | } |
| 685 | if ($allinactive || $activate) { |
| 686 | $this->emails[$thisone]->activate(); |
| 687 | } else { |
| 688 | $this->emails[$thisone]->deactivate(); |
| 689 | } |
| 690 | check_redirect($this); |
| 691 | if ($allinactive && !$activate) { |
| 692 | return ERROR_INACTIVE_REDIRECTION; |
| 693 | } |
| 694 | return SUCCESS; |
| 695 | } |
| 696 | |
| 697 | public function modify_one_email_redirect($email, $redirect) |
| 698 | { |
| 699 | foreach ($this->emails as &$mail) { |
| 700 | if ($mail->email == $email) { |
| 701 | $mail->set_rewrite($redirect); |
| 702 | check_redirect($this); |
| 703 | return; |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | public function clean_errors($email) |
| 709 | { |
| 710 | foreach ($this->emails as &$mail) { |
| 711 | if ($mail->email == $email) { |
| 712 | check_redirect($this); |
| 713 | return $mail->clean_errors(); |
| 714 | } |
| 715 | } |
| 716 | return false; |
| 717 | } |
| 718 | |
| 719 | public function disable() |
| 720 | { |
| 721 | XDB::execute("UPDATE email_redirect_account |
| 722 | SET flags = 'disable' |
| 723 | WHERE flags = 'active' AND uid = {?}", $this->user->id()); |
| 724 | foreach ($this->emails as &$mail) { |
| 725 | if ($mail->active && $mail->has_disable()) { |
| 726 | $mail->disabled = true; |
| 727 | $mail->active = false; |
| 728 | } |
| 729 | } |
| 730 | check_redirect($this); |
| 731 | } |
| 732 | |
| 733 | public function enable() |
| 734 | { |
| 735 | XDB::execute("UPDATE email_redirect_account |
| 736 | SET flags = 'active' |
| 737 | WHERE flags = 'disable' AND uid = {?}", $this->user->id()); |
| 738 | foreach ($this->emails as &$mail) { |
| 739 | if ($mail->disabled) { |
| 740 | $mail->disabled = false; |
| 741 | $mail->active = true; |
| 742 | } |
| 743 | check_redirect($this); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | public function get_broken_mx() |
| 748 | { |
| 749 | $res = XDB::query("SELECT host, text |
| 750 | FROM mx_watch |
| 751 | WHERE state != 'ok'"); |
| 752 | if (!$res->numRows()) { |
| 753 | return array(); |
| 754 | } |
| 755 | $mxs = $res->fetchAllAssoc(); |
| 756 | $mails = array(); |
| 757 | foreach ($this->emails as &$mail) { |
| 758 | if ($mail->active && strstr($mail->email, '@') !== false) { |
| 759 | list(,$domain) = explode('@', $mail->email); |
| 760 | getmxrr($domain, $lcl_mxs); |
| 761 | if (empty($lcl_mxs)) { |
| 762 | $lcl_mxs = array($domain); |
| 763 | } |
| 764 | $broken = false; |
| 765 | foreach ($mxs as &$mx) { |
| 766 | foreach ($lcl_mxs as $lcl) { |
| 767 | if (fnmatch($mx['host'], $lcl)) { |
| 768 | $broken = $mx['text']; |
| 769 | break; |
| 770 | } |
| 771 | } |
| 772 | if ($broken) { |
| 773 | $mails[] = array('mail' => $mail->email, 'text' => $broken); |
| 774 | break; |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | return $mails; |
| 780 | } |
| 781 | |
| 782 | public function active_emails() |
| 783 | { |
| 784 | $emails = array(); |
| 785 | foreach ($this->emails as $mail) { |
| 786 | if ($mail->active) { |
| 787 | $emails[] = $mail; |
| 788 | } |
| 789 | } |
| 790 | return $emails; |
| 791 | } |
| 792 | |
| 793 | public function get_uid() |
| 794 | { |
| 795 | return $this->user->id(); |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 800 | ?> |