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