| 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 | class EmailModule extends PLModule |
| 23 | { |
| 24 | function handlers() |
| 25 | { |
| 26 | return array( |
| 27 | 'emails' => $this->make_hook('emails', AUTH_COOKIE), |
| 28 | 'emails/alias' => $this->make_hook('alias', AUTH_MDP), |
| 29 | 'emails/antispam' => $this->make_hook('antispam', AUTH_MDP), |
| 30 | 'emails/broken' => $this->make_hook('broken', AUTH_COOKIE), |
| 31 | 'emails/redirect' => $this->make_hook('redirect', AUTH_MDP), |
| 32 | 'emails/send' => $this->make_hook('send', AUTH_MDP), |
| 33 | 'emails/antispam/submit' => $this->make_hook('submit', AUTH_COOKIE), |
| 34 | 'emails/test' => $this->make_hook('test', AUTH_COOKIE, 'user', NO_AUTH), |
| 35 | |
| 36 | 'emails/rewrite/in' => $this->make_hook('rewrite_in', AUTH_PUBLIC), |
| 37 | 'emails/rewrite/out' => $this->make_hook('rewrite_out', AUTH_PUBLIC), |
| 38 | |
| 39 | 'emails/imap/in' => $this->make_hook('imap_in', AUTH_PUBLIC), |
| 40 | |
| 41 | 'admin/emails/duplicated' => $this->make_hook('duplicated', AUTH_MDP, 'admin'), |
| 42 | 'admin/emails/watch' => $this->make_hook('duplicated', AUTH_MDP, 'admin'), |
| 43 | 'admin/emails/lost' => $this->make_hook('lost', AUTH_MDP, 'admin'), |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | function handler_emails(&$page, $action = null, $email = null) |
| 48 | { |
| 49 | global $globals; |
| 50 | require_once 'emails.inc.php'; |
| 51 | |
| 52 | $page->changeTpl('emails/index.tpl'); |
| 53 | $page->setTitle('Mes emails'); |
| 54 | |
| 55 | $user = S::user(); |
| 56 | |
| 57 | // Apply the bestalias change request. |
| 58 | if ($action == 'best' && $email) { |
| 59 | if (!S::has_xsrf_token()) { |
| 60 | return PL_FORBIDDEN; |
| 61 | } |
| 62 | |
| 63 | XDB::execute("UPDATE aliases |
| 64 | SET flags = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', flags, ','), ',bestalias,', ',')) |
| 65 | WHERE id = {?}", $user->id()); |
| 66 | XDB::execute("UPDATE aliases |
| 67 | SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'bestalias') |
| 68 | WHERE id = {?} AND alias = {?}", $user->id(), $email); |
| 69 | |
| 70 | // As having a non-null bestalias value is critical in |
| 71 | // plat/al's code, we do an a posteriori check on the |
| 72 | // validity of the bestalias. |
| 73 | fix_bestalias($user); |
| 74 | } |
| 75 | |
| 76 | // Fetch and display aliases. |
| 77 | $sql = "SELECT alias, (type='a_vie') AS a_vie, |
| 78 | (alias REGEXP '\\\\.[0-9]{2}$') AS cent_ans, |
| 79 | FIND_IN_SET('bestalias',flags) AS best, expire |
| 80 | FROM aliases |
| 81 | WHERE id = {?} AND type!='homonyme' |
| 82 | ORDER BY LENGTH(alias)"; |
| 83 | $page->assign('aliases', XDB::iterator($sql, $user->id())); |
| 84 | |
| 85 | // Check for homonyms. |
| 86 | $homonyme = XDB::query( |
| 87 | "SELECT alias |
| 88 | FROM aliases |
| 89 | INNER JOIN homonymes ON (id = homonyme_id) |
| 90 | WHERE user_id = {?} AND type = 'homonyme'", $user->id()); |
| 91 | $page->assign('homonyme', $homonyme->fetchOneCell()); |
| 92 | |
| 93 | // Display active redirections. |
| 94 | $redirect = new Redirect($user); |
| 95 | $page->assign('mails', $redirect->active_emails()); |
| 96 | |
| 97 | // Display, when available, the @alias_dom email alias. |
| 98 | $res = XDB::query( |
| 99 | "SELECT alias |
| 100 | FROM virtual AS v |
| 101 | INNER JOIN virtual_redirect AS vr USING(vid) |
| 102 | WHERE (redirect={?} OR redirect={?}) |
| 103 | AND alias LIKE '%@{$globals->mail->alias_dom}'", |
| 104 | $user->forlifeEmail(), |
| 105 | // TODO: remove this über-ugly hack. The issue is that you need |
| 106 | // to remove all @m4x.org addresses in virtual_redirect first. |
| 107 | $user->login() . '@' . $globals->mail->domain2); |
| 108 | $page->assign('melix', $res->fetchOneCell()); |
| 109 | } |
| 110 | |
| 111 | function handler_alias(&$page, $action = null, $value = null) |
| 112 | { |
| 113 | require_once 'validations.inc.php'; |
| 114 | |
| 115 | global $globals; |
| 116 | |
| 117 | $page->changeTpl('emails/alias.tpl'); |
| 118 | $page->setTitle('Alias melix.net'); |
| 119 | |
| 120 | $user = S::user(); |
| 121 | $page->assign('demande', AliasReq::get_request($user->id())); |
| 122 | |
| 123 | // Remove the email alias. |
| 124 | if ($action == 'delete' && $value) { |
| 125 | S::assert_xsrf_token(); |
| 126 | |
| 127 | XDB::execute( |
| 128 | "DELETE virtual, virtual_redirect |
| 129 | FROM virtual |
| 130 | INNER JOIN virtual_redirect USING (vid) |
| 131 | WHERE alias = {?} AND (redirect = {?} OR redirect = {?})", |
| 132 | $value, $user->forlifeEmail(), |
| 133 | // TODO: remove this über-ugly hack. The issue is that you need |
| 134 | // to remove all @m4x.org addresses in virtual_redirect first. |
| 135 | $user->login() . '@' . $globals->mail->domain2); |
| 136 | } |
| 137 | |
| 138 | // Fetch existing @alias_dom aliases. |
| 139 | $res = XDB::query( |
| 140 | "SELECT alias, emails_alias_pub |
| 141 | FROM auth_user_quick, virtual |
| 142 | INNER JOIN virtual_redirect USING(vid) |
| 143 | WHERE (redirect = {?} OR redirect = {?}) |
| 144 | AND alias LIKE '%@{$globals->mail->alias_dom}' AND user_id = {?}", |
| 145 | $user->forlifeEmail(), |
| 146 | // TODO: remove this über-ugly hack. The issue is that you need |
| 147 | // to remove all @m4x.org addresses in virtual_redirect first. |
| 148 | $user->login() . '@' . $globals->mail->domain2, $user->id()); |
| 149 | list($alias, $visibility) = $res->fetchOneRow(); |
| 150 | $page->assign('actuel', $alias); |
| 151 | |
| 152 | if ($action == 'ask' && Env::has('alias') && Env::has('raison')) { |
| 153 | S::assert_xsrf_token(); |
| 154 | |
| 155 | //Si l'utilisateur vient de faire une damande |
| 156 | $alias = Env::v('alias'); |
| 157 | $raison = Env::v('raison'); |
| 158 | $public = (Env::v('public', 'off') == 'on')?"public":"private"; |
| 159 | |
| 160 | $page->assign('r_alias', $alias); |
| 161 | $page->assign('r_raison', $raison); |
| 162 | if ($public == 'public') { |
| 163 | $page->assign('r_public', true); |
| 164 | } |
| 165 | |
| 166 | //Quelques vérifications sur l'alias (caractères spéciaux) |
| 167 | if (!preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $alias)) { |
| 168 | $page->trigError("L'adresse demandée n'est pas valide." |
| 169 | . " Vérifie qu'elle comporte entre 3 et 20 caractères" |
| 170 | . " et qu'elle ne contient que des lettres non accentuées," |
| 171 | . " des chiffres ou les caractères - et ."); |
| 172 | return; |
| 173 | } else { |
| 174 | //vérifier que l'alias n'est pas déja pris |
| 175 | $res = XDB::query('SELECT COUNT(*) FROM virtual WHERE alias={?}', |
| 176 | $alias.'@'.$globals->mail->alias_dom); |
| 177 | if ($res->fetchOneCell() > 0) { |
| 178 | $page->trigError("L'alias $alias@{$globals->mail->alias_dom} a déja été attribué. |
| 179 | Tu ne peux donc pas l'obtenir."); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | //vérifier que l'alias n'est pas déja en demande |
| 184 | $it = new ValidateIterator (); |
| 185 | while($req = $it->next()) { |
| 186 | if ($req->type == "alias" and $req->alias == $alias . '@' . $globals->mail->alias_dom) { |
| 187 | $page->trigError("L'alias $alias@{$globals->mail->alias_dom} a déja été demandé. |
| 188 | Tu ne peux donc pas l'obtenir pour l'instant."); |
| 189 | return ; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | //Insertion de la demande dans la base, écrase les requêtes précédente |
| 194 | $myalias = new AliasReq($user, $alias, $raison, $public); |
| 195 | $myalias->submit(); |
| 196 | $page->assign('success',$alias); |
| 197 | return; |
| 198 | } |
| 199 | } elseif ($action == 'set' && ($value == 'public' || $value == 'private')) { |
| 200 | if (!S::has_xsrf_token()) { |
| 201 | return PL_FORBIDDEN; |
| 202 | } |
| 203 | |
| 204 | if ($value == 'public') { |
| 205 | XDB::execute("UPDATE auth_user_quick SET emails_alias_pub = 'public' |
| 206 | WHERE user_id = {?}", $user->id()); |
| 207 | } else { |
| 208 | XDB::execute("UPDATE auth_user_quick SET emails_alias_pub = 'private' |
| 209 | WHERE user_id = {?}", $user->id()); |
| 210 | } |
| 211 | |
| 212 | $visibility = $value; |
| 213 | } |
| 214 | |
| 215 | $page->assign('mail_public', ($visibility == 'public')); |
| 216 | } |
| 217 | |
| 218 | function handler_redirect(&$page, $action = null, $email = null) |
| 219 | { |
| 220 | global $globals; |
| 221 | |
| 222 | require_once 'emails.inc.php'; |
| 223 | |
| 224 | $page->changeTpl('emails/redirect.tpl'); |
| 225 | |
| 226 | $user = S::user(); |
| 227 | $page->assign_by_ref('user', $user); |
| 228 | $page->assign('eleve', $user->promo() >= date("Y") - 5); |
| 229 | |
| 230 | $redirect = new Redirect($user); |
| 231 | |
| 232 | // FS#703 : $_GET is urldecoded twice, hence |
| 233 | // + (the data) => %2B (in the url) => + (first decoding) => ' ' (second decoding) |
| 234 | // Since there can be no spaces in emails, we can fix this with : |
| 235 | $email = str_replace(' ', '+', $email); |
| 236 | |
| 237 | // Apply email redirection change requests. |
| 238 | if ($action == 'remove' && $email) { |
| 239 | $retour = $redirect->delete_email($email); |
| 240 | } |
| 241 | |
| 242 | if ($action == 'active' && $email) { |
| 243 | $redirect->modify_one_email($email, true); |
| 244 | } |
| 245 | |
| 246 | if ($action == 'inactive' && $email) { |
| 247 | $redirect->modify_one_email($email, false); |
| 248 | } |
| 249 | |
| 250 | if ($action == 'rewrite' && $email) { |
| 251 | $rewrite = @func_get_arg(3); |
| 252 | $redirect->modify_one_email_redirect($email, $rewrite); |
| 253 | } |
| 254 | |
| 255 | if (Env::has('emailop')) { |
| 256 | S::assert_xsrf_token(); |
| 257 | |
| 258 | $actifs = Env::v('emails_actifs', Array()); |
| 259 | print_r(Env::v('emails_rewrite')); |
| 260 | if (Env::v('emailop') == "ajouter" && Env::has('email')) { |
| 261 | $new_email = Env::v('email'); |
| 262 | if ($new_email == "new@example.org") { |
| 263 | $new_email = Env::v('email_new'); |
| 264 | } |
| 265 | $retour = $redirect->add_email($new_email); |
| 266 | if ($retour == ERROR_INVALID_EMAIL) { |
| 267 | $page->assign('email', $new_email); |
| 268 | } |
| 269 | $page->assign('retour', $retour); |
| 270 | } elseif (empty($actifs)) { |
| 271 | $retour = ERROR_INACTIVE_REDIRECTION; |
| 272 | } elseif (is_array($actifs)) { |
| 273 | $retour = $redirect->modify_email($actifs, Env::v('emails_rewrite', Array())); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | switch ($retour) { |
| 278 | case ERROR_INACTIVE_REDIRECTION: |
| 279 | $page->trigError('Tu ne peux pas avoir aucune adresse de redirection active, sinon ton adresse ' |
| 280 | . $user->forlifeEmail() . ' ne fonctionnerait plus.'); |
| 281 | break; |
| 282 | case ERROR_INVALID_EMAIL: |
| 283 | $page->trigError('Erreur: l\'email n\'est pas valide.'); |
| 284 | break; |
| 285 | case ERROR_LOOP_EMAIL: |
| 286 | $page->trigError('Erreur : ' . $user->forlifeEmail() |
| 287 | . ' ne doit pas être renvoyé vers lui-même, ni vers son équivalent en ' |
| 288 | . $globals->mail->domain2 . ' ni vers polytechnique.edu.'); |
| 289 | break; |
| 290 | } |
| 291 | |
| 292 | // Fetch the @alias_dom email alias, if any. |
| 293 | $res = XDB::query( |
| 294 | "SELECT alias |
| 295 | FROM virtual |
| 296 | INNER JOIN virtual_redirect USING(vid) |
| 297 | WHERE (redirect={?} OR redirect={?}) |
| 298 | AND alias LIKE '%@{$globals->mail->alias_dom}'", |
| 299 | $user->forlifeEmail(), |
| 300 | // TODO: remove this über-ugly hack. The issue is that you need |
| 301 | // to remove all @m4x.org addresses in virtual_redirect first. |
| 302 | $user->login() . '@' . $globals->mail->domain2); |
| 303 | $melix = $res->fetchOneCell(); |
| 304 | if ($melix) { |
| 305 | list($melix) = explode('@', $melix); |
| 306 | $page->assign('melix',$melix); |
| 307 | } |
| 308 | |
| 309 | // Fetch existing email aliases. |
| 310 | $res = XDB::query( |
| 311 | "SELECT alias,expire |
| 312 | FROM aliases |
| 313 | WHERE id={?} AND (type='a_vie' OR type='alias') |
| 314 | ORDER BY !FIND_IN_SET('usage',flags), LENGTH(alias)", $user->id()); |
| 315 | $page->assign('alias', $res->fetchAllAssoc()); |
| 316 | $page->assign('emails', $redirect->emails); |
| 317 | |
| 318 | // Display GoogleApps acount information. |
| 319 | require_once 'googleapps.inc.php'; |
| 320 | $page->assign('googleapps', GoogleAppsAccount::account_status($user->id())); |
| 321 | |
| 322 | require_once 'emails.combobox.inc.php'; |
| 323 | fill_email_combobox($page); |
| 324 | } |
| 325 | |
| 326 | function handler_antispam(&$page, $statut_filtre = null) |
| 327 | { |
| 328 | require_once 'emails.inc.php'; |
| 329 | $wp = new PlWikiPage('Xorg.Antispam'); |
| 330 | $wp->buildCache(); |
| 331 | |
| 332 | $page->changeTpl('emails/antispam.tpl'); |
| 333 | |
| 334 | $bogo = new Bogo(S::user()); |
| 335 | if (isset($statut_filtre)) { |
| 336 | $bogo->change($statut_filtre + 0); |
| 337 | } |
| 338 | $page->assign('filtre', $bogo->level()); |
| 339 | } |
| 340 | |
| 341 | function handler_submit(&$page) |
| 342 | { |
| 343 | $wp = new PlWikiPage('Xorg.Mails'); |
| 344 | $wp->buildCache(); |
| 345 | $page->changeTpl('emails/submit_spam.tpl'); |
| 346 | |
| 347 | if (Post::has('send_email')) { |
| 348 | S::assert_xsrf_token(); |
| 349 | |
| 350 | $upload = PlUpload::get($_FILES['mail'], S::user()->login(), 'spam.submit', true); |
| 351 | if (!$upload) { |
| 352 | $page->trigError('Une erreur a été rencontrée lors du transfert du fichier'); |
| 353 | return; |
| 354 | } |
| 355 | $mime = $upload->contentType(); |
| 356 | if ($mime != 'text/x-mail' && $mime != 'message/rfc822') { |
| 357 | $upload->clear(); |
| 358 | $page->trigError('Le fichier ne contient pas un email complet'); |
| 359 | return; |
| 360 | } |
| 361 | $type = (Post::v('type') == 'spam' ? 'spam' : 'nonspam'); |
| 362 | |
| 363 | global $globals; |
| 364 | $box = $type . '@' . $globals->mail->domain; |
| 365 | $mailer = new PlMailer(); |
| 366 | $mailer->addTo($box); |
| 367 | $mailer->setFrom('"' . S::user()->fullName() . '" <web@' . $globals->mail->domain . '>'); |
| 368 | $mailer->setTxtBody($type . ' soumis par ' . S::user()->login() . ' via le web'); |
| 369 | $mailer->addUploadAttachment($upload, $type . '.mail'); |
| 370 | $mailer->send(); |
| 371 | $page->trigSuccess('Le message a été transmis à ' . $box); |
| 372 | $upload->clear(); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | function handler_send(&$page) |
| 377 | { |
| 378 | $page->changeTpl('emails/send.tpl'); |
| 379 | $page->addJsLink('ajax.js'); |
| 380 | |
| 381 | $page->setTitle('Envoyer un email'); |
| 382 | |
| 383 | // action si on recoit un formulaire |
| 384 | if (Post::has('save')) { |
| 385 | if (!S::has_xsrf_token()) { |
| 386 | return PL_FORBIDDEN; |
| 387 | } |
| 388 | |
| 389 | unset($_POST['save']); |
| 390 | if (trim(preg_replace('/-- .*/', '', Post::v('contenu'))) != "") { |
| 391 | $_POST['to_contacts'] = explode(';', @$_POST['to_contacts']); |
| 392 | $_POST['cc_contacts'] = explode(';', @$_POST['cc_contacts']); |
| 393 | $data = serialize($_POST); |
| 394 | XDB::execute("REPLACE INTO email_send_save |
| 395 | VALUES ({?}, {?})", S::i('uid'), $data); |
| 396 | } |
| 397 | exit; |
| 398 | } else if (Env::v('submit') == 'Envoyer') { |
| 399 | S::assert_xsrf_token(); |
| 400 | |
| 401 | function getEmails($aliases) |
| 402 | { |
| 403 | if (!is_array($aliases)) { |
| 404 | return null; |
| 405 | } |
| 406 | $rel = Env::v('contacts'); |
| 407 | $ret = array(); |
| 408 | foreach ($aliases as $alias) { |
| 409 | $ret[$alias] = $rel[$alias]; |
| 410 | } |
| 411 | return join(', ', $ret); |
| 412 | } |
| 413 | |
| 414 | $error = false; |
| 415 | foreach ($_FILES as &$file) { |
| 416 | if ($file['name'] && !PlUpload::get($file, S::user()->login(), 'emails.send', false)) { |
| 417 | $page->trigError(PlUpload::$lastError); |
| 418 | $error = true; |
| 419 | break; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | if (!$error) { |
| 424 | XDB::execute("DELETE FROM email_send_save |
| 425 | WHERE uid = {?}", S::i('uid')); |
| 426 | |
| 427 | $to2 = getEmails(Env::v('to_contacts')); |
| 428 | $cc2 = getEmails(Env::v('cc_contacts')); |
| 429 | $txt = str_replace('^M', '', Env::v('contenu')); |
| 430 | $to = Env::v('to'); |
| 431 | $subj = Env::v('sujet'); |
| 432 | $from = Env::v('from'); |
| 433 | $cc = trim(Env::v('cc')); |
| 434 | $bcc = trim(Env::v('bcc')); |
| 435 | |
| 436 | if (empty($to) && empty($cc) && empty($to2) && empty($bcc) && empty($cc2)) { |
| 437 | $page->trigError("Indique au moins un destinataire."); |
| 438 | $page->assign('uploaded_f', PlUpload::listFilenames(S::user()->login(), 'emails.send')); |
| 439 | } else { |
| 440 | $mymail = new PlMailer(); |
| 441 | $mymail->setFrom($from); |
| 442 | $mymail->setSubject($subj); |
| 443 | if (!empty($to)) { $mymail->addTo($to); } |
| 444 | if (!empty($cc)) { $mymail->addCc($cc); } |
| 445 | if (!empty($bcc)) { $mymail->addBcc($bcc); } |
| 446 | if (!empty($to2)) { $mymail->addTo($to2); } |
| 447 | if (!empty($cc2)) { $mymail->addCc($cc2); } |
| 448 | $files =& PlUpload::listFiles(S::user()->login(), 'emails.send'); |
| 449 | foreach ($files as $name=>&$upload) { |
| 450 | $mymail->addUploadAttachment($upload, $name); |
| 451 | } |
| 452 | if (Env::v('nowiki')) { |
| 453 | $mymail->setTxtBody(wordwrap($txt, 78, "\n")); |
| 454 | } else { |
| 455 | $mymail->setWikiBody($txt); |
| 456 | } |
| 457 | if ($mymail->send()) { |
| 458 | $page->trigSuccess("Ton email a bien été envoyé."); |
| 459 | $_REQUEST = array('bcc' => S::user()->bestEmail()); |
| 460 | PlUpload::clear(S::user()->login(), 'emails.send'); |
| 461 | } else { |
| 462 | $page->trigError("Erreur lors de l'envoi du courriel, réessaye."); |
| 463 | $page->assign('uploaded_f', PlUpload::listFilenames(S::user()->login(), 'emails.send')); |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | } else { |
| 468 | $res = XDB::query("SELECT data |
| 469 | FROM email_send_save |
| 470 | WHERE uid = {?}", S::i('uid')); |
| 471 | if ($res->numRows() == 0) { |
| 472 | PlUpload::clear(S::user()->login(), 'emails.send'); |
| 473 | $_REQUEST['bcc'] = S::user()->bestEmail(); |
| 474 | } else { |
| 475 | $data = unserialize($res->fetchOneCell()); |
| 476 | $_REQUEST = array_merge($_REQUEST, $data); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | $res = XDB::query( |
| 481 | "SELECT u.prenom, u.nom, u.promo, a.alias as forlife |
| 482 | FROM auth_user_md5 AS u |
| 483 | INNER JOIN contacts AS c ON (u.user_id = c.contact) |
| 484 | INNER JOIN aliases AS a ON (u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags)) |
| 485 | WHERE c.uid = {?} |
| 486 | ORDER BY u.nom, u.prenom", S::v('uid')); |
| 487 | $page->assign('contacts', $res->fetchAllAssoc()); |
| 488 | $page->assign('maxsize', ini_get('upload_max_filesize') . 'o'); |
| 489 | $page->assign('user', S::user()); |
| 490 | } |
| 491 | |
| 492 | function handler_test(&$page, $hruid = null) |
| 493 | { |
| 494 | require_once 'emails.inc.php'; |
| 495 | |
| 496 | if (!S::has_xsrf_token()) { |
| 497 | return PL_FORBIDDEN; |
| 498 | } |
| 499 | |
| 500 | // Retrieves the User object for the test email recipient. |
| 501 | if (S::has_perms() && $hruid) { |
| 502 | $user = User::getSilent($hruid); |
| 503 | } else { |
| 504 | $user = S::user(); |
| 505 | } |
| 506 | if (!$user) { |
| 507 | return PL_NOT_FOUND; |
| 508 | } |
| 509 | |
| 510 | // Sends the test email. |
| 511 | $redirect = new Redirect($user); |
| 512 | |
| 513 | $mailer = new PlMailer('emails/test.mail.tpl'); |
| 514 | $mailer->assign('email', $user->bestEmail()); |
| 515 | $mailer->assign('redirects', $redirect->active_emails()); |
| 516 | $mailer->assign('display_name', $user->displayName()); |
| 517 | $mailer->assign('sexe', $user->isFemale()); |
| 518 | $mailer->send($user->isEmailFormatHtml()); |
| 519 | exit; |
| 520 | } |
| 521 | |
| 522 | function handler_rewrite_in(&$page, $mail, $hash) |
| 523 | { |
| 524 | $page->changeTpl('emails/rewrite.tpl'); |
| 525 | $page->assign('option', 'in'); |
| 526 | if (empty($mail) || empty($hash)) { |
| 527 | return PL_NOT_FOUND; |
| 528 | } |
| 529 | $pos = strrpos($mail, '_'); |
| 530 | if ($pos === false) { |
| 531 | return PL_NOT_FOUND; |
| 532 | } |
| 533 | $mail{$pos} = '@'; |
| 534 | $res = XDB::query("SELECT COUNT(*) |
| 535 | FROM emails |
| 536 | WHERE email = {?} AND hash = {?}", |
| 537 | $mail, $hash); |
| 538 | $count = intval($res->fetchOneCell()); |
| 539 | if ($count > 0) { |
| 540 | XDB::query("UPDATE emails |
| 541 | SET allow_rewrite = true, hash = NULL |
| 542 | WHERE email = {?} AND hash = {?}", |
| 543 | $mail, $hash); |
| 544 | $page->trigSuccess("Réécriture activée pour l'adresse " . $mail); |
| 545 | return; |
| 546 | } |
| 547 | return PL_NOT_FOUND; |
| 548 | } |
| 549 | |
| 550 | function handler_rewrite_out(&$page, $mail, $hash) |
| 551 | { |
| 552 | $page->changeTpl('emails/rewrite.tpl'); |
| 553 | $page->assign('option', 'out'); |
| 554 | if (empty($mail) || empty($hash)) { |
| 555 | return PL_NOT_FOUND; |
| 556 | } |
| 557 | $pos = strrpos($mail, '_'); |
| 558 | if ($pos === false) { |
| 559 | return PL_NOT_FOUND; |
| 560 | } |
| 561 | $mail{$pos} = '@'; |
| 562 | $res = XDB::query("SELECT COUNT(*) |
| 563 | FROM emails |
| 564 | WHERE email = {?} AND hash = {?}", |
| 565 | $mail, $hash); |
| 566 | $count = intval($res->fetchOneCell()); |
| 567 | if ($count > 0) { |
| 568 | global $globals; |
| 569 | $res = XDB::query("SELECT e.email, e.rewrite, a.alias |
| 570 | FROM emails AS e |
| 571 | INNER JOIN aliases AS a ON (a.id = e.uid AND a.type = 'a_vie') |
| 572 | WHERE e.email = {?} AND e.hash = {?}", |
| 573 | $mail, $hash); |
| 574 | XDB::query("UPDATE emails |
| 575 | SET allow_rewrite = false, hash = NULL |
| 576 | WHERE email = {?} AND hash = {?}", |
| 577 | $mail, $hash); |
| 578 | list($mail, $rewrite, $forlife) = $res->fetchOneRow(); |
| 579 | $mail = new PlMailer(); |
| 580 | $mail->setFrom("webmaster@" . $globals->mail->domain); |
| 581 | $mail->addTo("support@" . $globals->mail->domain); |
| 582 | $mail->setSubject("Tentative de détournement de correspondance via le rewrite"); |
| 583 | $mail->setTxtBody("$forlife a tenté un rewrite de $mail vers $rewrite. Cette demande a été rejetée via le web"); |
| 584 | $mail->send(); |
| 585 | $page->trigWarning("Un mail d'alerte a été envoyé à l'équipe de " . $globals->core->sitename); |
| 586 | return; |
| 587 | } |
| 588 | return PL_NOT_FOUND; |
| 589 | } |
| 590 | |
| 591 | function handler_imap_in(&$page, $hash = null, $login = null) |
| 592 | { |
| 593 | $page->changeTpl('emails/imap_register.tpl'); |
| 594 | $user = null; |
| 595 | if (!empty($hash) || !empty($login)) { |
| 596 | $user = User::getSilent($login); |
| 597 | if ($user) { |
| 598 | $req = XDB::query("SELECT 1 FROM newsletter_ins WHERE user_id = {?} AND hash = {?}", $user->id(), $hash); |
| 599 | if ($req->numRows() == 0) { |
| 600 | $user = null; |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | require_once('emails.inc.php'); |
| 606 | $page->assign('ok', false); |
| 607 | if (S::logged() && (is_null($user) || $user->id() == S::i('uid'))) { |
| 608 | $storage = new EmailStorage(S::user(), 'imap'); |
| 609 | $storage->activate(); |
| 610 | $page->assign('ok', true); |
| 611 | $page->assign('prenom', S::v('prenom')); |
| 612 | $page->assign('sexe', S::v('femme')); |
| 613 | } else if (!S::logged() && $user) { |
| 614 | $storage = new EmailStorage($user, 'imap'); |
| 615 | $storage->activate(); |
| 616 | $page->assign('ok', true); |
| 617 | $page->assign('prenom', $user->displayName()); |
| 618 | $page->assign('sexe', $user->isFemale()); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | function handler_broken(&$page, $warn = null, $email = null) |
| 623 | { |
| 624 | require_once 'emails.inc.php'; |
| 625 | $wp = new PlWikiPage('Xorg.PatteCassée'); |
| 626 | $wp->buildCache(); |
| 627 | |
| 628 | global $globals; |
| 629 | |
| 630 | $page->changeTpl('emails/broken.tpl'); |
| 631 | |
| 632 | if ($warn == 'warn' && $email) { |
| 633 | S::assert_xsrf_token(); |
| 634 | |
| 635 | $email = valide_email($email); |
| 636 | // vérifications d'usage |
| 637 | $sel = XDB::query("SELECT uid FROM emails WHERE email = {?}", $email); |
| 638 | if (($uid = $sel->fetchOneCell())) { |
| 639 | $dest = User::getSilent($uid); |
| 640 | |
| 641 | // envoi du mail |
| 642 | $message = "Bonjour ! |
| 643 | |
| 644 | Cet email a été généré automatiquement par le service de patte cassée de |
| 645 | Polytechnique.org car un autre utilisateur, " . S::user()->fullName() . ", |
| 646 | nous a signalé qu'en t'envoyant un email, il avait reçu un message d'erreur |
| 647 | indiquant que ton adresse de redirection $email |
| 648 | ne fonctionnait plus ! |
| 649 | |
| 650 | Nous te suggérons de vérifier cette adresse, et le cas échéant de mettre |
| 651 | à jour sur le site <{$globals->baseurl}/emails> tes adresses |
| 652 | de redirection... |
| 653 | |
| 654 | Pour plus de renseignements sur le service de patte cassée, n'hésite pas à |
| 655 | consulter la page <{$globals->baseurl}/emails/broken>. |
| 656 | |
| 657 | |
| 658 | À bientôt sur Polytechnique.org ! |
| 659 | L'équipe d'administration <support@" . $globals->mail->domain . '>'; |
| 660 | |
| 661 | $mail = new PlMailer(); |
| 662 | $mail->setFrom('"Polytechnique.org" <support@' . $globals->mail->domain . '>'); |
| 663 | $mail->addTo($dest->bestEmail()); |
| 664 | $mail->setSubject("Une de tes adresse de redirection Polytechnique.org ne marche plus !!"); |
| 665 | $mail->setTxtBody($message); |
| 666 | $mail->send(); |
| 667 | $page->trigSuccess("Email envoyé !"); |
| 668 | } |
| 669 | } elseif (Post::has('email')) { |
| 670 | S::assert_xsrf_token(); |
| 671 | |
| 672 | $email = valide_email(Post::v('email')); |
| 673 | |
| 674 | list(,$fqdn) = explode('@', $email); |
| 675 | $fqdn = strtolower($fqdn); |
| 676 | if ($fqdn == 'polytechnique.org' || $fqdn == 'melix.org' || $fqdn == 'm4x.org' || $fqdn == 'melix.net') { |
| 677 | $page->assign('neuneu', true); |
| 678 | } else { |
| 679 | $page->assign('email',$email); |
| 680 | $sel = XDB::query( |
| 681 | "SELECT e1.uid, e1.panne != 0 AS panne, |
| 682 | (count(e2.uid) + IF(FIND_IN_SET('googleapps', u.mail_storage), 1, 0)) AS nb_mails, |
| 683 | u.nom, u.prenom, u.promo, u.hruid |
| 684 | FROM emails as e1 |
| 685 | LEFT JOIN emails as e2 ON(e1.uid = e2.uid |
| 686 | AND FIND_IN_SET('active', e2.flags) |
| 687 | AND e1.email != e2.email) |
| 688 | INNER JOIN auth_user_md5 as u ON(e1.uid = u.user_id) |
| 689 | WHERE e1.email = {?} |
| 690 | GROUP BY e1.uid", $email); |
| 691 | if ($x = $sel->fetchOneAssoc()) { |
| 692 | // on écrit dans la base que l'adresse est cassée |
| 693 | if (!$x['panne']) { |
| 694 | XDB::execute("UPDATE emails |
| 695 | SET panne=NOW(), |
| 696 | last=NOW(), |
| 697 | panne_level = 1 |
| 698 | WHERE email = {?}", $email); |
| 699 | } else { |
| 700 | XDB::execute("UPDATE emails |
| 701 | SET panne_level = 1 |
| 702 | WHERE email = {?} AND panne_level = 0", $email); |
| 703 | } |
| 704 | $page->assign_by_ref('x', $x); |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | function handler_duplicated(&$page, $action = 'list', $email = null) |
| 711 | { |
| 712 | $page->changeTpl('emails/duplicated.tpl'); |
| 713 | |
| 714 | $states = array('pending' => 'En attente...', |
| 715 | 'safe' => 'Pas d\'inquiétude', |
| 716 | 'unsafe' => 'Recherches en cours', |
| 717 | 'dangerous' => 'Usurpations par cette adresse'); |
| 718 | $page->assign('states', $states); |
| 719 | |
| 720 | if (Post::has('action')) { |
| 721 | S::assert_xsrf_token(); |
| 722 | } |
| 723 | switch (Post::v('action')) { |
| 724 | case 'create': |
| 725 | if (trim(Post::v('emailN')) != '') { |
| 726 | Xdb::execute('INSERT IGNORE INTO emails_watch (email, state, detection, last, uid, description) |
| 727 | VALUES ({?}, {?}, CURDATE(), NOW(), {?}, {?})', |
| 728 | trim(Post::v('emailN')), Post::v('stateN'), S::i('uid'), Post::v('descriptionN')); |
| 729 | }; |
| 730 | break; |
| 731 | |
| 732 | case 'edit': |
| 733 | Xdb::execute('UPDATE emails_watch |
| 734 | SET state = {?}, last = NOW(), uid = {?}, description = {?} |
| 735 | WHERE email = {?}', Post::v('stateN'), S::i('uid'), Post::v('descriptionN'), Post::v('emailN')); |
| 736 | break; |
| 737 | |
| 738 | default: |
| 739 | if ($action == 'delete' && !is_null($email)) { |
| 740 | Xdb::execute('DELETE FROM emails_watch WHERE email = {?}', $email); |
| 741 | } |
| 742 | } |
| 743 | if ($action != 'create' && $action != 'edit') { |
| 744 | $action = 'list'; |
| 745 | } |
| 746 | $page->assign('action', $action); |
| 747 | |
| 748 | if ($action == 'list') { |
| 749 | $sql = "SELECT w.email, w.detection, w.state, a.alias AS forlife |
| 750 | FROM emails_watch AS w |
| 751 | LEFT JOIN emails AS e USING(email) |
| 752 | LEFT JOIN aliases AS a ON (a.id = e.uid AND a.type = 'a_vie') |
| 753 | ORDER BY w.state, w.email, a.alias"; |
| 754 | $it = Xdb::iterRow($sql); |
| 755 | |
| 756 | $table = array(); |
| 757 | $props = array(); |
| 758 | while (list($email, $date, $state, $forlife) = $it->next()) { |
| 759 | if (count($props) == 0 || $props['mail'] != $email) { |
| 760 | if (count($props) > 0) { |
| 761 | $table[] = $props; |
| 762 | } |
| 763 | $props = array('mail' => $email, |
| 764 | 'detection' => $date, |
| 765 | 'state' => $state, |
| 766 | 'users' => array($forlife)); |
| 767 | } else { |
| 768 | $props['users'][] = $forlife; |
| 769 | } |
| 770 | } |
| 771 | if (count($props) > 0) { |
| 772 | $table[] = $props; |
| 773 | } |
| 774 | $page->assign('table', $table); |
| 775 | } elseif ($action == 'edit') { |
| 776 | $sql = "SELECT w.detection, w.state, w.last, w.description, |
| 777 | a1.alias AS edit, a2.alias AS forlife |
| 778 | FROM emails_watch AS w |
| 779 | LEFT JOIN aliases AS a1 ON (a1.id = w.uid AND a1.type = 'a_vie') |
| 780 | LEFT JOIN emails AS e ON (w.email = e.email) |
| 781 | LEFT JOIN aliases AS a2 ON (a2.id = e.uid AND a2.type = 'a_vie') |
| 782 | WHERE w.email = {?} |
| 783 | ORDER BY a2.alias"; |
| 784 | $it = Xdb::iterRow($sql, $email); |
| 785 | |
| 786 | $props = array(); |
| 787 | while (list($detection, $state, $last, $description, $edit, $forlife) = $it->next()) { |
| 788 | if (count($props) == 0) { |
| 789 | $props = array('mail' => $email, |
| 790 | 'detection' => $detection, |
| 791 | 'state' => $state, |
| 792 | 'last' => $last, |
| 793 | 'description' => $description, |
| 794 | 'edit' => $edit, |
| 795 | 'users' => array($forlife)); |
| 796 | } else { |
| 797 | $props['users'][] = $forlife; |
| 798 | } |
| 799 | } |
| 800 | $page->assign('doublon', $props); |
| 801 | } |
| 802 | } |
| 803 | function handler_lost(&$page, $action = 'list', $email = null) |
| 804 | { |
| 805 | $page->changeTpl('emails/lost.tpl'); |
| 806 | |
| 807 | $page->assign('lost_emails', XDB::iterator(" |
| 808 | SELECT u.user_id, u.hruid |
| 809 | FROM auth_user_md5 AS u |
| 810 | LEFT JOIN emails AS e ON (u.user_id = e.uid AND FIND_IN_SET('active', e.flags)) |
| 811 | WHERE e.uid IS NULL AND FIND_IN_SET('googleapps', u.mail_storage) = 0 AND |
| 812 | u.deces = 0 AND u.perms IN ('user', 'admin', 'disabled') |
| 813 | ORDER BY u.promo DESC, u.nom, u.prenom")); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 818 | ?> |