| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2008 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 | require_once("xorg.misc.inc.php"); |
| 23 | |
| 24 | define("SUCCESS", 1); |
| 25 | define("ERROR_INACTIVE_REDIRECTION", 2); |
| 26 | define("ERROR_INVALID_EMAIL", 3); |
| 27 | define("ERROR_LOOP_EMAIL", 4); |
| 28 | |
| 29 | // function fix_bestalias() {{{1 |
| 30 | // Checks for an existing 'bestalias' among the the current user's aliases, and |
| 31 | // eventually selects a new bestalias when required. |
| 32 | function fix_bestalias($uid) |
| 33 | { |
| 34 | $res = XDB::query("SELECT COUNT(*) |
| 35 | FROM aliases |
| 36 | WHERE id = {?} AND FIND_IN_SET('bestalias', flags) AND type != 'homonyme'", |
| 37 | $uid); |
| 38 | if ($res->fetchOneCell()) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | XDB::execute("UPDATE aliases |
| 43 | SET flags=CONCAT(flags,',','bestalias') |
| 44 | WHERE id={?} AND type!='homonyme' |
| 45 | ORDER BY !FIND_IN_SET('usage',flags),alias LIKE '%.%', LENGTH(alias) |
| 46 | LIMIT 1", $uid); |
| 47 | } |
| 48 | |
| 49 | // function valide_email() {{{1 |
| 50 | // Returns a cleaned-up version of the @p email string. It removes garbage |
| 51 | // characters, and determines the canonical form (without _ and +) for |
| 52 | // Polytechnique.org email addresses. |
| 53 | function valide_email($str) |
| 54 | { |
| 55 | global $globals; |
| 56 | |
| 57 | $em = trim(rtrim($str)); |
| 58 | $em = str_replace('<', '', $em); |
| 59 | $em = str_replace('>', '', $em); |
| 60 | list($ident, $dom) = explode('@', $em); |
| 61 | if ($dom == $globals->mail->domain or $dom == $globals->mail->domain2) { |
| 62 | list($ident1) = explode('_', $ident); |
| 63 | list($ident) = explode('+', $ident1); |
| 64 | } |
| 65 | return $ident . '@' . $dom; |
| 66 | } |
| 67 | |
| 68 | // class Bogo {{{1 |
| 69 | // The Bogo class represents a spam filtering level in plat/al architecture. |
| 70 | class Bogo |
| 71 | { |
| 72 | // properties {{{2 |
| 73 | |
| 74 | private $uid; |
| 75 | private $state; |
| 76 | private $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams'); |
| 77 | |
| 78 | // constructor {{{2 |
| 79 | |
| 80 | public function __construct($uid) |
| 81 | { |
| 82 | if (!$uid) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | $this->uid = $uid; |
| 87 | $res = XDB::query('SELECT email FROM emails WHERE uid={?} AND flags="filter"', $uid); |
| 88 | if ($res->numRows()) { |
| 89 | $this->state = $res->fetchOneCell(); |
| 90 | } else { |
| 91 | $this->state = 'tag_and_drop_spams'; |
| 92 | $res = XDB::query("INSERT INTO emails (uid,email,rewrite,panne,flags) |
| 93 | VALUES ({?},'tag_and_drop_spams','','0000-00-00','filter')", $uid); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // public function change() {{{2 |
| 98 | |
| 99 | public function change($state) |
| 100 | { |
| 101 | $this->state = is_int($state) ? $this->_states[$state] : $state; |
| 102 | XDB::execute('UPDATE emails SET email={?} WHERE uid={?} AND flags = "filter"', |
| 103 | $this->state, $this->uid); |
| 104 | } |
| 105 | |
| 106 | // pubic function level() {{{2 |
| 107 | |
| 108 | public function level() |
| 109 | { |
| 110 | return array_search($this->state, $this->_states); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // class Email {{{1 |
| 115 | // Represents an "email address" used as final recipient for plat/al-managed |
| 116 | // addresses; it can be subclasses a Redirection emails (third-party) or as |
| 117 | // Storage emails (Polytechnique.org). |
| 118 | abstract class Email |
| 119 | { |
| 120 | protected $uid; |
| 121 | |
| 122 | // Basic email properties; $sufficient indicates if the email can be used as |
| 123 | // an unique redirection; $email contains the delivery email address. |
| 124 | public $type; |
| 125 | public $sufficient; |
| 126 | public $email; |
| 127 | public $display_email; |
| 128 | |
| 129 | // Redirection status properties. |
| 130 | public $active; |
| 131 | public $broken; |
| 132 | public $disabled; |
| 133 | public $rewrite; |
| 134 | |
| 135 | // Redirection bounces stats. |
| 136 | public $panne; |
| 137 | public $last; |
| 138 | public $panne_level; |
| 139 | |
| 140 | // Activates the email address as a redirection. |
| 141 | public abstract function activate(); |
| 142 | |
| 143 | // Deactivates the email address as a redirection. |
| 144 | public abstract function deactivate(); |
| 145 | |
| 146 | // Sets the rewrite rule for the given address. |
| 147 | public abstract function set_rewrite($rewrite); |
| 148 | |
| 149 | // Resets the error counts associated with the redirection. |
| 150 | public abstract function clean_errors(); |
| 151 | |
| 152 | // Email backend capabilities ('rewrite' refers to From: rewrite for mails |
| 153 | // forwarded by Polytechnique.org's MXs; 'removable' indicates if the email |
| 154 | // can be definitively removed; 'disable' indicates if the email has a third |
| 155 | // status 'disabled' in addition to 'active' and 'inactive'). |
| 156 | public abstract function has_rewrite(); |
| 157 | public abstract function is_removable(); |
| 158 | public abstract function has_disable(); |
| 159 | } |
| 160 | |
| 161 | // class EmailRedirection {{{1 |
| 162 | // Implementation of Email for third-party redirection (redirection of emails to |
| 163 | // external user-supplied addresses). |
| 164 | class EmailRedirection extends Email |
| 165 | { |
| 166 | // constructor {{{2 |
| 167 | |
| 168 | public function __construct($uid, $row) |
| 169 | { |
| 170 | $this->uid = $uid; |
| 171 | $this->sufficient = true; |
| 172 | |
| 173 | list($this->email, $flags, $this->rewrite, $this->panne, $this->last, $this->panne_level) = $row; |
| 174 | $this->display_email = $this->email; |
| 175 | $this->active = ($flags == 'active'); |
| 176 | $this->broken = ($flags == 'panne'); |
| 177 | $this->disabled = ($flags == 'disable'); |
| 178 | } |
| 179 | |
| 180 | // public function activate() {{{2 |
| 181 | |
| 182 | public function activate() |
| 183 | { |
| 184 | if (!$this->active) { |
| 185 | XDB::execute("UPDATE emails |
| 186 | SET panne_level = IF(flags = 'panne', panne_level - 1, panne_level), |
| 187 | flags = 'active' |
| 188 | WHERE uid={?} AND email={?}", $this->uid, $this->email); |
| 189 | $_SESSION['log']->log("email_on", $this->email.($this->uid!=S::v('uid') ? "(admin on {$this->uid})" : "")); |
| 190 | $this->active = true; |
| 191 | $this->broken = false; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // public function deactivate() {{{2 |
| 196 | |
| 197 | public function deactivate() |
| 198 | { |
| 199 | if ($this->active) { |
| 200 | XDB::execute("UPDATE emails SET flags ='' |
| 201 | WHERE uid={?} AND email={?}", $this->uid, $this->email); |
| 202 | $_SESSION['log']->log("email_off",$this->email.($this->uid != S::v('uid') ? "(admin on {$this->uid})" : "") ); |
| 203 | $this->active = false; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // public function set_rewrite() {{{2 |
| 208 | |
| 209 | public function set_rewrite($rewrite) |
| 210 | { |
| 211 | if ($this->rewrite == $rewrite) { |
| 212 | return; |
| 213 | } |
| 214 | if (!$rewrite || !isvalid_email($rewrite)) { |
| 215 | $rewrite = ''; |
| 216 | } |
| 217 | XDB::execute('UPDATE emails SET rewrite={?} WHERE uid={?} AND email={?}', $rewrite, $this->uid, $this->email); |
| 218 | $this->rewrite = $rewrite; |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | // public function clean_errors() {{{2 |
| 223 | |
| 224 | public function clean_errors() |
| 225 | { |
| 226 | if (!S::has_perms()) { |
| 227 | return false; |
| 228 | } |
| 229 | $this->panne = 0; |
| 230 | $this->panne_level = 0; |
| 231 | $this->last = 0; |
| 232 | return XDB::execute("UPDATE emails |
| 233 | SET panne_level = 0, panne = 0, last = 0 |
| 234 | WHERE uid = {?} AND email = {?}", |
| 235 | $this->uid, $this->email); |
| 236 | } |
| 237 | |
| 238 | // public function has_rewrite() {{{2 |
| 239 | |
| 240 | public function has_rewrite() |
| 241 | { |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | // public function is_removable() {{{2 |
| 246 | |
| 247 | public function is_removable() |
| 248 | { |
| 249 | return true; |
| 250 | } |
| 251 | |
| 252 | // public function has_disable() {{{2 |
| 253 | |
| 254 | public function has_disable() |
| 255 | { |
| 256 | return true; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // class EmailStorage {{{1 |
| 261 | // Implementation of Email for email storage backends from Polytechnique.org. |
| 262 | class EmailStorage extends Email |
| 263 | { |
| 264 | // Shortname to realname mapping for known mail storage backends. |
| 265 | private $display_names = array( |
| 266 | 'imap' => 'Accès de secours aux emails (IMAP)', |
| 267 | 'googleapps' => 'Compte Google Apps', |
| 268 | ); |
| 269 | |
| 270 | // Retrieves the current list of actives storages. |
| 271 | private function get_storages() |
| 272 | { |
| 273 | $res = XDB::query("SELECT mail_storage |
| 274 | FROM auth_user_md5 |
| 275 | WHERE user_id = {?}", $this->uid); |
| 276 | return new FlagSet($res->fetchOneCell()); |
| 277 | } |
| 278 | |
| 279 | // Updates the list of active storages. |
| 280 | private function set_storages($storages) |
| 281 | { |
| 282 | XDB::execute("UPDATE auth_user_md5 |
| 283 | SET mail_storage = {?} |
| 284 | WHERE user_id = {?}", $storages, $this->uid); |
| 285 | } |
| 286 | |
| 287 | // Returns the list of allowed storages for the @p user. |
| 288 | static public function get_allowed_storages($uid) |
| 289 | { |
| 290 | global $globals; |
| 291 | $storages = array(); |
| 292 | |
| 293 | // Google Apps storage is available for users with valid Google Apps account. |
| 294 | require_once 'googleapps.inc.php'; |
| 295 | if ($globals->mailstorage->googleapps_domain && |
| 296 | GoogleAppsAccount::account_status($uid) == 'active') { |
| 297 | $storages[] = 'googleapps'; |
| 298 | } |
| 299 | |
| 300 | // IMAP storage is always visible to administrators, and is allowed for |
| 301 | // everyone when the service is marked as 'active'. |
| 302 | if ($globals->mailstorage->imap_active || S::has_perms()) { |
| 303 | $storages[] = 'imap'; |
| 304 | } |
| 305 | |
| 306 | return $storages; |
| 307 | } |
| 308 | |
| 309 | |
| 310 | public function __construct($uid, $name) |
| 311 | { |
| 312 | $this->uid = $uid; |
| 313 | $this->email = $name; |
| 314 | $this->display_email = (isset($this->display_names[$name]) ? $this->display_names[$name] : $name); |
| 315 | |
| 316 | $storages = $this->get_storages(); |
| 317 | $this->sufficient = ($name == 'googleapps'); |
| 318 | $this->active = $storages->hasFlag($name); |
| 319 | $this->broken = false; |
| 320 | $this->disabled = false; |
| 321 | $this->rewrite = ''; |
| 322 | $this->panne = $this->last = $this->panne_level = 0; |
| 323 | } |
| 324 | |
| 325 | public function activate() |
| 326 | { |
| 327 | if (!$this->active) { |
| 328 | $storages = $this->get_storages(); |
| 329 | $storages->addFlag($this->email); |
| 330 | $this->set_storages($storages); |
| 331 | $this->active = true; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | public function deactivate() |
| 336 | { |
| 337 | if ($this->active) { |
| 338 | $storages = $this->get_storages(); |
| 339 | $storages->rmFlag($this->email); |
| 340 | $this->set_storages($storages); |
| 341 | $this->active = false; |
| 342 | } |
| 343 | |
| 344 | } |
| 345 | |
| 346 | // Source rewrite can't be enabled for email storage addresses. |
| 347 | public function set_rewrite($rewrite) {} |
| 348 | |
| 349 | // Email storage are not supposed to be broken, hence not supposed to be |
| 350 | // cleaned-up. |
| 351 | public function clean_errors() {} |
| 352 | |
| 353 | // Capabilities. |
| 354 | public function has_rewrite() { return false; } |
| 355 | public function is_removable() { return false; } |
| 356 | public function has_disable() { return false; } |
| 357 | } |
| 358 | |
| 359 | // class Redirect {{{1 |
| 360 | // Redirect is a placeholder class for an user's active redirections (third-party |
| 361 | // redirection email, or Polytechnique.org mail storages). |
| 362 | class Redirect |
| 363 | { |
| 364 | // properties {{{2 |
| 365 | |
| 366 | private $flag_active = 'active'; |
| 367 | private $uid; |
| 368 | |
| 369 | public $emails; |
| 370 | public $bogo; |
| 371 | |
| 372 | // constructor {{{2 |
| 373 | |
| 374 | public function __construct($_uid) |
| 375 | { |
| 376 | $this->uid = $_uid; |
| 377 | $this->bogo = new Bogo($_uid); |
| 378 | |
| 379 | // Adds third-party email redirections. |
| 380 | $res = XDB::iterRow("SELECT email, flags, rewrite, panne, last, panne_level |
| 381 | FROM emails |
| 382 | WHERE uid = {?} AND flags != 'filter'", $_uid); |
| 383 | $this->emails = Array(); |
| 384 | while ($row = $res->next()) { |
| 385 | $this->emails[] = new EmailRedirection($_uid, $row); |
| 386 | } |
| 387 | |
| 388 | // Adds local email storage backends. |
| 389 | foreach (EmailStorage::get_allowed_storages($_uid) as $storage) { |
| 390 | $this->emails[] = new EmailStorage($_uid, $storage); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // public function other_active() {{{2 |
| 395 | |
| 396 | public function other_active($email) |
| 397 | { |
| 398 | foreach ($this->emails as $mail) { |
| 399 | if ($mail->email != $email && $mail->active && $mail->sufficient) { |
| 400 | return true; |
| 401 | } |
| 402 | } |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | // public function delete_email() {{{2 |
| 407 | |
| 408 | public function delete_email($email) |
| 409 | { |
| 410 | if (!$this->other_active($email)) { |
| 411 | return ERROR_INACTIVE_REDIRECTION; |
| 412 | } |
| 413 | XDB::execute('DELETE FROM emails WHERE uid={?} AND email={?}', $this->uid, $email); |
| 414 | $_SESSION['log']->log('email_del',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : "")); |
| 415 | foreach ($this->emails as $i => $mail) { |
| 416 | if ($email == $mail->email) { |
| 417 | unset($this->emails[$i]); |
| 418 | } |
| 419 | } |
| 420 | check_redirect($this); |
| 421 | return SUCCESS; |
| 422 | } |
| 423 | |
| 424 | // public function add_email() {{{2 |
| 425 | |
| 426 | public function add_email($email) |
| 427 | { |
| 428 | $email_stripped = strtolower(trim($email)); |
| 429 | if (!isvalid_email($email_stripped)) { |
| 430 | return ERROR_INVALID_EMAIL; |
| 431 | } |
| 432 | if (!isvalid_email_redirection($email_stripped)) { |
| 433 | return ERROR_LOOP_EMAIL; |
| 434 | } |
| 435 | XDB::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->uid, $email); |
| 436 | if ($logger = S::v('log', null)) { // may be absent --> step4.php |
| 437 | $logger->log('email_add',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : "")); |
| 438 | } |
| 439 | foreach ($this->emails as $mail) { |
| 440 | if ($mail->email == $email_stripped) { |
| 441 | return SUCCESS; |
| 442 | } |
| 443 | } |
| 444 | $this->emails[] = new EmailRedirection($this->uid, array($email, 'active', '', '0000-00-00', '0000-00-00', 0)); |
| 445 | |
| 446 | // security stuff |
| 447 | check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->uid); |
| 448 | check_redirect($this); |
| 449 | return SUCCESS; |
| 450 | } |
| 451 | |
| 452 | // public function modify_email() {{{2 |
| 453 | |
| 454 | public function modify_email($emails_actifs, $emails_rewrite) |
| 455 | { |
| 456 | foreach ($this->emails as &$mail) { |
| 457 | if (in_array($mail->email, $emails_actifs)) { |
| 458 | $mail->activate(); |
| 459 | } else { |
| 460 | $mail->deactivate(); |
| 461 | } |
| 462 | $mail->set_rewrite($emails_rewrite[$mail->email]); |
| 463 | } |
| 464 | check_redirect($this); |
| 465 | } |
| 466 | |
| 467 | // public function modify_one_email() {{{2 |
| 468 | |
| 469 | public function modify_one_email($email, $activate) |
| 470 | { |
| 471 | $allinactive = true; |
| 472 | $thisone = false; |
| 473 | foreach ($this->emails as $i=>$mail) { |
| 474 | if ($mail->email == $email) { |
| 475 | $thisone = $i; |
| 476 | } |
| 477 | $allinactive &= !$mail->active || !$mail->sufficient || $mail->email == $email; |
| 478 | } |
| 479 | if ($thisone === false) { |
| 480 | return ERROR_INVALID_EMAIL; |
| 481 | } |
| 482 | if ($allinactive || $activate) { |
| 483 | $this->emails[$thisone]->activate(); |
| 484 | } else { |
| 485 | $this->emails[$thisone]->deactivate(); |
| 486 | } |
| 487 | check_redirect($this); |
| 488 | if ($allinactive && !$activate) { |
| 489 | return ERROR_INACTIVE_REDIRECTION; |
| 490 | } else { |
| 491 | return SUCCESS; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | // public function modify_one_email_redirect() {{{2 |
| 496 | |
| 497 | public function modify_one_email_redirect($email, $redirect) |
| 498 | { |
| 499 | foreach ($this->emails as &$mail) { |
| 500 | if ($mail->email == $email) { |
| 501 | $mail->set_rewrite($redirect); |
| 502 | check_redirect($this); |
| 503 | return; |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | // function clean_errors() {{{2 |
| 509 | |
| 510 | public function clean_errors($email) |
| 511 | { |
| 512 | foreach ($this->emails as &$mail) { |
| 513 | if ($mail->email == $email) { |
| 514 | check_redirect($this); |
| 515 | return $mail->clean_errors(); |
| 516 | } |
| 517 | } |
| 518 | return false; |
| 519 | } |
| 520 | |
| 521 | // function disable() {{{2 |
| 522 | |
| 523 | public function disable() |
| 524 | { |
| 525 | XDB::execute("UPDATE emails |
| 526 | SET flags = 'disable' |
| 527 | WHERE flags = 'active' AND uid = {?}", $this->uid); |
| 528 | foreach ($this->emails as &$mail) { |
| 529 | if ($mail->active && $mail->has_disable()) { |
| 530 | $mail->disabled = true; |
| 531 | $mail->active = false; |
| 532 | } |
| 533 | } |
| 534 | check_redirect($this); |
| 535 | } |
| 536 | |
| 537 | // function enable() {{{2 |
| 538 | |
| 539 | public function enable() |
| 540 | { |
| 541 | XDB::execute("UPDATE emails |
| 542 | SET flags = 'active' |
| 543 | WHERE flags = 'disable' AND uid = {?}", $this->uid); |
| 544 | foreach ($this->emails as &$mail) { |
| 545 | if ($mail->disabled) { |
| 546 | $mail->active = true; |
| 547 | $mail->disabled = false; |
| 548 | } |
| 549 | check_redirect($this); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | // function get_broken_mx() {{{2 |
| 554 | |
| 555 | public function get_broken_mx() |
| 556 | { |
| 557 | $res = XDB::query("SELECT host, text |
| 558 | FROM mx_watch |
| 559 | WHERE state != 'ok'"); |
| 560 | if (!$res->numRows()) { |
| 561 | return array(); |
| 562 | } |
| 563 | $mxs = $res->fetchAllAssoc(); |
| 564 | $mails = array(); |
| 565 | foreach ($this->emails as &$mail) { |
| 566 | if ($mail->active && strstr($mail->email, '@') !== false) { |
| 567 | list(,$domain) = explode('@', $mail->email); |
| 568 | getmxrr($domain, $lcl_mxs); |
| 569 | if (empty($lcl_mxs)) { |
| 570 | $lcl_mxs = array($domain); |
| 571 | } |
| 572 | $broken = false; |
| 573 | foreach ($mxs as &$mx) { |
| 574 | foreach ($lcl_mxs as $lcl) { |
| 575 | if (fnmatch($mx['host'], $lcl)) { |
| 576 | $broken = $mx['text']; |
| 577 | break; |
| 578 | } |
| 579 | } |
| 580 | if ($broken) { |
| 581 | $mails[] = array('mail' => $mail->email, 'text' => $broken); |
| 582 | break; |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | return $mails; |
| 588 | } |
| 589 | |
| 590 | // function active_emails() {{{2 |
| 591 | |
| 592 | public function active_emails() |
| 593 | { |
| 594 | $emails = array(); |
| 595 | foreach ($this->emails as $mail) { |
| 596 | if ($mail->active) { |
| 597 | $emails[] = $mail; |
| 598 | } |
| 599 | } |
| 600 | return $emails; |
| 601 | } |
| 602 | |
| 603 | // function get_uid() {{{2 |
| 604 | |
| 605 | public function get_uid() |
| 606 | { |
| 607 | return $this->uid; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 612 | ?> |