| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2006 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 | // {{{ defines |
| 25 | |
| 26 | define("SUCCESS", 1); |
| 27 | define("ERROR_INACTIVE_REDIRECTION", 2); |
| 28 | define("ERROR_INVALID_EMAIL", 3); |
| 29 | define("ERROR_LOOP_EMAIL", 4); |
| 30 | |
| 31 | // }}} |
| 32 | // {{{ function fix_bestalias() |
| 33 | |
| 34 | function fix_bestalias($uid) |
| 35 | { |
| 36 | $res = XDB::query("SELECT COUNT(*) FROM aliases WHERE id={?} AND FIND_IN_SET('bestalias',flags) AND type!='homonyme'", $uid); |
| 37 | if ($n = $res->fetchOneCell()) { |
| 38 | return; |
| 39 | } |
| 40 | XDB::execute("UPDATE aliases |
| 41 | SET flags=CONCAT(flags,',','bestalias') |
| 42 | WHERE id={?} AND type!='homonyme' |
| 43 | ORDER BY !FIND_IN_SET('usage',flags),alias LIKE '%.%', LENGTH(alias) |
| 44 | LIMIT 1", $uid); |
| 45 | } |
| 46 | |
| 47 | // }}} |
| 48 | // {{{ function valide_email() |
| 49 | |
| 50 | function valide_email($str) |
| 51 | { |
| 52 | global $globals; |
| 53 | |
| 54 | $em = trim(rtrim($str)); |
| 55 | $em = str_replace('<', '', $em); |
| 56 | $em = str_replace('>', '', $em); |
| 57 | list($ident, $dom) = explode('@', $em); |
| 58 | if ($dom == $globals->mail->domain or $dom == $globals->mail->domain2) { |
| 59 | list($ident1) = explode('_', $ident); |
| 60 | list($ident) = explode('+', $ident1); |
| 61 | } |
| 62 | return $ident . '@' . $dom; |
| 63 | } |
| 64 | |
| 65 | // }}} |
| 66 | // {{{ class Bogo |
| 67 | |
| 68 | class Bogo |
| 69 | { |
| 70 | // {{{ properties |
| 71 | |
| 72 | var $state; |
| 73 | var $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams'); |
| 74 | |
| 75 | // }}} |
| 76 | // {{{ constructor |
| 77 | |
| 78 | function Bogo($uid) |
| 79 | { |
| 80 | if (!$uid) { |
| 81 | return; |
| 82 | } |
| 83 | $res = XDB::query('SELECT email FROM emails WHERE uid={?} AND flags="filter"', $uid); |
| 84 | if ($res->numRows()) { |
| 85 | $this->state = $res->fetchOneCell(); |
| 86 | } else { |
| 87 | $this->state = 'tag_and_drop_spams'; |
| 88 | $res = XDB::query("INSERT INTO emails (uid,email,rewrite,panne,flags) |
| 89 | VALUES ({?},'tag_and_drop_spams','','0000-00-00','filter')", $uid); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // }}} |
| 94 | // {{{ function change() |
| 95 | |
| 96 | function change($uid, $state) |
| 97 | { |
| 98 | $this->state = is_int($state) ? $this->_states[$state] : $state; |
| 99 | XDB::execute('UPDATE emails SET email={?} WHERE uid={?} AND flags = "filter"', |
| 100 | $this->state, $uid); |
| 101 | } |
| 102 | |
| 103 | // }}} |
| 104 | // {{{ function level() |
| 105 | |
| 106 | function level() |
| 107 | { return array_search($this->state, $this->_states); } |
| 108 | |
| 109 | // }}} |
| 110 | } |
| 111 | |
| 112 | // }}} |
| 113 | // {{{ class Email |
| 114 | |
| 115 | class Email |
| 116 | { |
| 117 | // {{{ properties |
| 118 | |
| 119 | var $email; |
| 120 | var $active; |
| 121 | var $broken; |
| 122 | var $rewrite; |
| 123 | var $panne; |
| 124 | var $last; |
| 125 | var $panne_level; |
| 126 | |
| 127 | // }}} |
| 128 | // {{{ constructor |
| 129 | |
| 130 | function Email($row) |
| 131 | { |
| 132 | list($this->email, $flags, $this->rewrite, $this->panne, $this->last, $this->panne_level) = $row; |
| 133 | $this->active = ($flags == 'active'); |
| 134 | $this->broken = ($flags == 'panne'); |
| 135 | } |
| 136 | |
| 137 | // }}} |
| 138 | // {{{ function activate() |
| 139 | |
| 140 | function activate($uid) |
| 141 | { |
| 142 | if (!$this->active) { |
| 143 | XDB::execute("UPDATE emails |
| 144 | SET panne_level = IF(flags = 'panne', panne_level - 1, panne_level), |
| 145 | flags = 'active' |
| 146 | WHERE uid={?} AND email={?}", $uid, $this->email); |
| 147 | $_SESSION['log']->log("email_on", $this->email.($uid!=S::v('uid') ? "(admin on $uid)" : "")); |
| 148 | $this->active = true; |
| 149 | $this->broken = false; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // }}} |
| 154 | // {{{ function deactivate() |
| 155 | |
| 156 | function deactivate($uid) |
| 157 | { |
| 158 | if ($this->active) { |
| 159 | XDB::execute("UPDATE emails SET flags ='' |
| 160 | WHERE uid={?} AND email={?}", $uid, $this->email); |
| 161 | $_SESSION['log']->log("email_off",$this->email.($uid!=S::v('uid') ? "(admin on $uid)" : "") ); |
| 162 | $this->active = false; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // }}} |
| 167 | // {{{ function rewrite() |
| 168 | |
| 169 | function rewrite($rew, $uid) |
| 170 | { |
| 171 | if ($this->rewrite == $rew) { |
| 172 | return; |
| 173 | } |
| 174 | XDB::execute('UPDATE emails SET rewrite={?} WHERE uid={?} AND email={?}', $rew, $uid, $this->email); |
| 175 | $this->rewrite = $rew; |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | // }}} |
| 180 | } |
| 181 | |
| 182 | // }}} |
| 183 | // {{{ class Redirect |
| 184 | |
| 185 | class Redirect |
| 186 | { |
| 187 | // {{{ properties |
| 188 | |
| 189 | var $flag_active = 'active'; |
| 190 | var $emails; |
| 191 | var $bogo; |
| 192 | var $uid; |
| 193 | |
| 194 | // }}} |
| 195 | // {{{ function Redirect() |
| 196 | |
| 197 | function Redirect($_uid) |
| 198 | { |
| 199 | $this->uid=$_uid; |
| 200 | $res = XDB::iterRow(" |
| 201 | SELECT email, flags, rewrite, panne, last, panne_level |
| 202 | FROM emails WHERE uid = {?} AND flags != 'filter'", $_uid); |
| 203 | $this->emails=Array(); |
| 204 | while ($row = $res->next()) { |
| 205 | $this->emails[] = new Email($row); |
| 206 | } |
| 207 | $this->bogo = new Bogo($_uid); |
| 208 | } |
| 209 | |
| 210 | // }}} |
| 211 | // {{{ function other_active() |
| 212 | |
| 213 | function other_active($email) |
| 214 | { |
| 215 | foreach ($this->emails as $mail) { |
| 216 | if ($mail->email!=$email && $mail->active) { |
| 217 | return true; |
| 218 | } |
| 219 | } |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | // }}} |
| 224 | // {{{ function delete_email() |
| 225 | |
| 226 | function delete_email($email) |
| 227 | { |
| 228 | if (!$this->other_active($email)) { |
| 229 | return ERROR_INACTIVE_REDIRECTION; |
| 230 | } |
| 231 | XDB::execute('DELETE FROM emails WHERE uid={?} AND email={?}', $this->uid, $email); |
| 232 | $_SESSION['log']->log('email_del',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : "")); |
| 233 | foreach ($this->emails as $i=>$mail) { |
| 234 | if ($email==$mail->email) { |
| 235 | unset($this->emails[$i]); |
| 236 | } |
| 237 | } |
| 238 | return SUCCESS; |
| 239 | } |
| 240 | |
| 241 | // }}} |
| 242 | // {{{ function add_email() |
| 243 | |
| 244 | function add_email($email) |
| 245 | { |
| 246 | $email_stripped = strtolower(trim($email)); |
| 247 | if (!isvalid_email($email_stripped)) { |
| 248 | return ERROR_INVALID_EMAIL; |
| 249 | } |
| 250 | if (!isvalid_email_redirection($email_stripped)) { |
| 251 | return ERROR_LOOP_EMAIL; |
| 252 | } |
| 253 | XDB::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->uid, $email); |
| 254 | if ($logger = S::v('log', null)) { // may be absent --> step4.php |
| 255 | $logger->log('email_add',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : "")); |
| 256 | } |
| 257 | foreach ($this->emails as $mail) { |
| 258 | if ($mail->email == $email_stripped) { |
| 259 | return SUCCESS; |
| 260 | } |
| 261 | } |
| 262 | $this->emails[] = new Email(array($email,1,'','0000-00-00')); |
| 263 | |
| 264 | // security stuff |
| 265 | $res = XDB::query("SELECT state, description |
| 266 | FROM emails_watch |
| 267 | WHERE state != 'safe' AND email = {?}", $email); |
| 268 | if ($res->numRows()) { |
| 269 | $row = $res->fetchOneAssoc(); |
| 270 | $message = "L'email $email vient d'être ajouté aux redirections de ". S::v('forlife') |
| 271 | . ". Cette adresse est surveillée avec l'état *" . $row['state'] |
| 272 | . "* et la description :\n" . $row['description']; |
| 273 | $message = wordwrap($message); |
| 274 | require_once("diogenes/diogenes.hermes.inc.php"); |
| 275 | $mailer = new HermesMailer(); |
| 276 | $mailer->setFrom("webmaster@polytechnique.org"); |
| 277 | $mailer->addTo("hotliners@staff.polytechnique.org"); |
| 278 | $mailer->setSubject("ALERTE LORS DE L'AJOUT DE REDIRECTION de " |
| 279 | . S::v('prenom') . ' ' . S::v('nom') . '(' . S::v('promo') . ')'); |
| 280 | $mailer->setTxtBody($message |
| 281 | . "\n\nInformations de connexion :\n" . var_export($_SERVER, true)); |
| 282 | $mailer->send(); |
| 283 | } |
| 284 | return SUCCESS; |
| 285 | } |
| 286 | |
| 287 | // }}} |
| 288 | // {{{ function modify_email() |
| 289 | |
| 290 | function modify_email($emails_actifs,$emails_rewrite) |
| 291 | { |
| 292 | foreach ($this->emails as $i=>$mail) { |
| 293 | if (in_array($mail->email,$emails_actifs)) { |
| 294 | $this->emails[$i]->activate($this->uid); |
| 295 | } else { |
| 296 | $this->emails[$i]->deactivate($this->uid); |
| 297 | } |
| 298 | $this->emails[$i]->rewrite($emails_rewrite[$mail->email], $this->uid); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | function modify_one_email($email, $activate) { |
| 303 | $allinactive = true; |
| 304 | $thisone = false; |
| 305 | foreach ($this->emails as $i=>$mail) { |
| 306 | if ($mail->email == $email) { |
| 307 | $thisone = $i; |
| 308 | } |
| 309 | $allinactive &= !$mail->active || $mail->email == $email; |
| 310 | } |
| 311 | if ($thisone === false) { |
| 312 | return ERROR_INVALID_EMAIL; |
| 313 | } |
| 314 | if ($allinactive || $activate) |
| 315 | $this->emails[$thisone]->activate($this->uid); |
| 316 | else |
| 317 | $this->emails[$thisone]->deactivate($this->uid); |
| 318 | if ($allinactive && !$activate) { |
| 319 | return ERROR_INACTIVE_REDIRECTION; |
| 320 | } else { |
| 321 | return SUCCESS; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | function modify_one_email_redirect($email, $redirect) { |
| 326 | foreach ($this->emails as $i=>$mail) { |
| 327 | if ($mail->email == $email) { |
| 328 | $this->emails[$i]->rewrite($redirect, $this->uid); |
| 329 | return; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | // }}} |
| 334 | } |
| 335 | |
| 336 | // }}} |
| 337 | |
| 338 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: |
| 339 | ?> |