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