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