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