Handle images in announces in admin page.
[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;
45
46 $page->changeTpl('emails/index.tpl');
47 $page->assign('xorg_title','Polytechnique.org - Mes emails');
48
cab08090 49 $uid = S::v('uid');
81b5b746 50
b7582015 51 if ($action == 'best' && $email) {
81b5b746 52 // bestalias is the first bit : 1
53 // there will be maximum 8 bits in flags : 255
08cce2ff 54 XDB::execute("UPDATE aliases SET flags=flags & (255 - 1) WHERE id={?}", $uid);
55 XDB::execute("UPDATE aliases SET flags=flags | 1 WHERE id={?} AND alias={?}",
b7582015 56 $uid, $email);
81b5b746 57 }
58
a7de4ef7 59 // on regarde si on a affaire à un homonyme
81b5b746 60 $sql = "SELECT alias, (type='a_vie') AS a_vie,
61 (alias REGEXP '\\\\.[0-9]{2}$') AS cent_ans,
62 FIND_IN_SET('bestalias',flags) AS best, expire
63 FROM aliases
64 WHERE id = {?} AND type!='homonyme'
65 ORDER BY LENGTH(alias)";
08cce2ff 66 $page->assign('aliases', XDB::iterator($sql, $uid));
81b5b746 67
e1547442
FB
68 $homonyme = XDB::query("SELECT alias FROM aliases INNER JOIN homonymes ON (id = homonyme_id) WHERE user_id = {?} AND type = 'homonyme'", $uid);
69 $page->assign('homonyme', $homonyme->fetchOneCell());
70
aab4661d 71 // Affichage des redirections de l'utilisateur.
81b5b746 72 $sql = "SELECT email
73 FROM emails
74 WHERE uid = {?} AND FIND_IN_SET('active', flags)";
08cce2ff 75 $page->assign('mails', XDB::iterator($sql, $uid));
81b5b746 76
aab4661d
VZ
77 // Affichage des backends actifs de stockage des emails.
78 $sql = "SELECT mail_storage
79 FROM auth_user_md5
80 WHERE user_id = {?}";
81 $storages = XDB::query($sql, $uid)->fetchOneCell();
82 $page->assign('storage', explode(',', $storages));
81b5b746 83
84 // on regarde si l'utilisateur a un alias et si oui on l'affiche !
cab08090 85 $forlife = S::v('forlife');
08cce2ff 86 $res = XDB::query(
81b5b746 87 "SELECT alias
88 FROM virtual AS v
89 INNER JOIN virtual_redirect AS vr USING(vid)
eaf30d86 90 WHERE (redirect={?} OR redirect={?})
81b5b746 91 AND alias LIKE '%@{$globals->mail->alias_dom}'",
92 $forlife.'@'.$globals->mail->domain, $forlife.'@'.$globals->mail->domain2);
93 $page->assign('melix', $res->fetchOneCell());
81b5b746 94 }
95
96 function handler_alias(&$page, $action = null, $value = null)
97 {
98 require_once 'validations.inc.php';
99
100 global $globals;
101
102 $page->changeTpl('emails/alias.tpl');
103 $page->assign('xorg_title','Polytechnique.org - Alias melix.net');
104
cab08090 105 $uid = S::v('uid');
106 $forlife = S::v('forlife');
81b5b746 107
108 $page->assign('demande', AliasReq::get_request($uid));
109
a5361008 110 if ($action == 'delete' && $value) {
81b5b746 111 //Suppression d'un alias
08cce2ff 112 XDB::execute(
81b5b746 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
a7de4ef7 120 //Récupération des alias éventuellement existants
08cce2ff 121 $res = XDB::query(
81b5b746 122 "SELECT alias, emails_alias_pub
123 FROM auth_user_quick, virtual
124 INNER JOIN virtual_redirect USING(vid)
125 WHERE ( redirect={?} OR redirect= {?} )
eaf30d86 126 AND alias LIKE '%@{$globals->mail->alias_dom}' AND user_id = {?}",
81b5b746 127 $forlife.'@'.$globals->mail->domain,
cab08090 128 $forlife.'@'.$globals->mail->domain2, S::v('uid'));
81b5b746 129 list($alias, $visibility) = $res->fetchOneRow();
130 $page->assign('actuel', $alias);
131
132 if ($action == 'ask' && Env::has('alias') and Env::has('raison')) {
133 //Si l'utilisateur vient de faire une damande
134
5e2307dc 135 $alias = Env::v('alias');
136 $raison = Env::v('raison');
137 $public = (Env::v('public', 'off') == 'on')?"public":"private";
81b5b746 138
139 $page->assign('r_alias', $alias);
140 $page->assign('r_raison', $raison);
141 if ($public == 'public') {
142 $page->assign('r_public', true);
143 }
144
a7de4ef7 145 //Quelques vérifications sur l'alias (caractères spéciaux)
81b5b746 146 if (!preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $alias)) {
a7de4ef7 147 $page->trig("L'adresse demandée n'est pas valide.
148 Vérifie qu'elle comporte entre 3 et 20 caractères
149 et qu'elle ne contient que des lettres non accentuées,
150 des chiffres ou les caractères - et .");
fd8f77de 151 return;
81b5b746 152 } else {
a7de4ef7 153 //vérifier que l'alias n'est pas déja pris
08cce2ff 154 $res = XDB::query('SELECT COUNT(*) FROM virtual WHERE alias={?}',
81b5b746 155 $alias.'@'.$globals->mail->alias_dom);
156 if ($res->fetchOneCell() > 0) {
a7de4ef7 157 $page->trig("L'alias $alias@{$globals->mail->alias_dom} a déja été attribué.
81b5b746 158 Tu ne peux donc pas l'obtenir.");
fd8f77de 159 return;
81b5b746 160 }
161
a7de4ef7 162 //vérifier que l'alias n'est pas déja en demande
81b5b746 163 $it = new ValidateIterator ();
164 while($req = $it->next()) {
1f07f6c2 165 if ($req->type == "alias" and $req->alias == $alias . '@' . $globals->mail->alias_dom) {
a7de4ef7 166 $page->trig("L'alias $alias@{$globals->mail->alias_dom} a déja été demandé.
81b5b746 167 Tu ne peux donc pas l'obtenir pour l'instant.");
fd8f77de 168 return ;
81b5b746 169 }
170 }
171
a7de4ef7 172 //Insertion de la demande dans la base, écrase les requêtes précédente
81b5b746 173 $myalias = new AliasReq($uid, $alias, $raison, $public);
174 $myalias->submit();
175 $page->assign('success',$alias);
fd8f77de 176 return;
81b5b746 177 }
178 }
179 elseif ($action == 'set'
180 && ($value == 'public' || $value == 'private'))
181 {
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
aab4661d
VZ
234 if ($action == 'storage' && $email == 'imap') {
235 $storage = new MailStorageIMAP(S::v('uid'));
236 $subaction = @func_get_arg(3);
237 if ($subaction == 'active') {
238 $storage->enable();
239 }
240 if ($subaction == 'inactive') {
241 $storage->disable();
242 }
243 }
244
81b5b746 245 if (Env::has('emailop')) {
5e2307dc 246 $actifs = Env::v('emails_actifs', Array());
b7582015 247 print_r(Env::v('emails_rewrite'));
5e2307dc 248 if (Env::v('emailop') == "ajouter" && Env::has('email')) {
249 $page->assign('retour', $redirect->add_email(Env::v('email')));
81b5b746 250 } elseif (empty($actifs)) {
251 $page->assign('retour', ERROR_INACTIVE_REDIRECTION);
252 } elseif (is_array($actifs)) {
253 $page->assign('retour', $redirect->modify_email($actifs,
5e2307dc 254 Env::v('emails_rewrite',Array())));
81b5b746 255 }
256 }
257
08cce2ff 258 $res = XDB::query(
81b5b746 259 "SELECT alias
260 FROM virtual
261 INNER JOIN virtual_redirect USING(vid)
262 WHERE (redirect={?} OR redirect={?})
eaf30d86 263 AND alias LIKE '%@{$globals->mail->alias_dom}'",
81b5b746 264 $forlife.'@'.$globals->mail->domain, $forlife.'@'.$globals->mail->domain2);
265 $melix = $res->fetchOneCell();
266 if ($melix) {
267 list($melix) = explode('@', $melix);
268 $page->assign('melix',$melix);
269 }
270
08cce2ff 271 $res = XDB::query(
81b5b746 272 "SELECT alias,expire
273 FROM aliases
274 WHERE id={?} AND (type='a_vie' OR type='alias')
275 ORDER BY !FIND_IN_SET('usage',flags), LENGTH(alias)", $uid);
e1547442 276
81b5b746 277 $page->assign('alias', $res->fetchAllAssoc());
278 $page->assign('emails',$redirect->emails);
aab4661d
VZ
279
280 $res = XDB::query(
281 "SELECT mail_storage
282 FROM auth_user_md5
283 WHERE user_id = {?}", $uid);
284 $page->assign('storage', explode(',', $res->fetchOneCell()));
81b5b746 285 }
286
b7582015 287 function handler_antispam(&$page, $statut_filtre = null)
81b5b746 288 {
289 require_once 'emails.inc.php';
841cfb97 290 require_once('wiki.inc.php');
291 wiki_require_page('Xorg.Antispam');
81b5b746 292
293 $page->changeTpl('emails/antispam.tpl');
294
cab08090 295 $bogo = new Bogo(S::v('uid'));
b7582015 296 if (isset($statut_filtre)) {
297 $bogo->change(S::v('uid'), $statut_filtre + 0);
81b5b746 298 }
299 $page->assign('filtre',$bogo->level());
81b5b746 300 }
301
62ce6356 302 function handler_submit(&$page)
303 {
4b9ba601 304 require_once('wiki.inc.php');
305 wiki_require_page('Xorg.Mails');
62ce6356 306 $page->changeTpl('emails/submit_spam.tpl');
307
308 if (Post::has('send_email')) {
15603084 309 $upload = PlUpload::get($_FILES['mail'], S::v('forlife'), 'spam.submit', true);
310 if (!$upload) {
a7de4ef7 311 $page->trig('Une erreur a été rencontrée lors du transfert du fichier');
62ce6356 312 return;
313 }
15603084 314 $mime = $upload->contentType();
62ce6356 315 if ($mime != 'text/x-mail' && $mime != 'message/rfc822') {
15603084 316 $upload->clear();
62ce6356 317 $page->trig('Le fichier ne contient pas un mail complet');
318 return;
319 }
320 global $globals;
321 $box = Post::v('type') . '@' . $globals->mail->domain;
322 $mailer = new PlMailer();
323 $mailer->addTo($box);
324 $mailer->setFrom('"' . S::v('prenom') . ' ' . S::v('nom') . '" <web@' . $globals->mail->domain . '>');
325 $mailer->setTxtBody(Post::v('type') . ' soumis par ' . S::v('forlife') . ' via le web');
15603084 326 $mailer->addUploadAttachment($upload, Post::v('type') . '.mail');
62ce6356 327 $mailer->send();
a7de4ef7 328 $page->trig('Le message a été transmis à ' . $box);
15603084 329 $upload->clear();
62ce6356 330 }
331 }
332
81b5b746 333 function handler_send(&$page)
334 {
335 global $globals;
81b5b746 336 $page->changeTpl('emails/send.tpl');
da419622 337 $page->addJsLink('ajax.js');
81b5b746 338
339 $page->assign('xorg_title','Polytechnique.org - Envoyer un email');
340
341 // action si on recoit un formulaire
3360c04f
FB
342 if (Post::has('save')) {
343 unset($_POST['save']);
344 if (trim(preg_replace('/-- .*/', '', Post::v('contenu'))) != "") {
345 $_POST['to_contacts'] = explode(';', @$_POST['to_contacts']);
346 $_POST['cc_contacts'] = explode(';', @$_POST['cc_contacts']);
347 $data = serialize($_POST);
348 XDB::execute("REPLACE INTO email_send_save
349 VALUES ({?}, {?})", S::i('uid'), $data);
350 }
351 exit;
352 } else if (Env::v('submit') == 'Envoyer') {
91a14f73 353 function getEmails($aliases)
354 {
355 if (!is_array($aliases)) {
356 return null;
357 }
358 $rel = Env::v('contacts');
359 $ret = array();
360 foreach ($aliases as $alias) {
361 $ret[$alias] = $rel[$alias];
362 }
363 return join(', ', $ret);
364 }
365
5e68682e 366 $error = false;
da419622 367 foreach ($_FILES as &$file) {
f3fb8eda 368 if ($file['name'] && !PlUpload::get($file, S::v('forlife'), 'emails.send', false)) {
5e68682e
FB
369 $page->trig(PlUpload::$lastError);
370 $error = true;
371 break;
da419622 372 }
373 }
374
5e68682e
FB
375 if (!$error) {
376 XDB::execute("DELETE FROM email_send_save
377 WHERE uid = {?}", S::i('uid'));
378
379 $to2 = getEmails(Env::v('to_contacts'));
380 $cc2 = getEmails(Env::v('cc_contacts'));
381 $txt = str_replace('^M', '', Env::v('contenu'));
382 $to = Env::v('to');
383 $subj = Env::v('sujet');
384 $from = Env::v('from');
385 $cc = trim(Env::v('cc'));
386 $bcc = trim(Env::v('bcc'));
387
388 if (empty($to) && empty($cc) && empty($to2) && empty($bcc) && empty($cc2)) {
389 $page->trig("Indique au moins un destinataire.");
da419622 390 $page->assign('uploaded_f', PlUpload::listFilenames(S::v('forlife'), 'emails.send'));
5e68682e
FB
391 } else {
392 $mymail = new PlMailer();
393 $mymail->setFrom($from);
394 $mymail->setSubject($subj);
395 if (!empty($to)) { $mymail->addTo($to); }
396 if (!empty($cc)) { $mymail->addCc($cc); }
397 if (!empty($bcc)) { $mymail->addBcc($bcc); }
398 if (!empty($to2)) { $mymail->addTo($to2); }
399 if (!empty($cc2)) { $mymail->addCc($cc2); }
400 $files =& PlUpload::listFiles(S::v('forlife'), 'emails.send');
401 foreach ($files as $name=>&$upload) {
402 $mymail->addUploadAttachment($upload, $name);
403 }
404 if (Env::v('nowiki')) {
405 $mymail->setTxtBody(wordwrap($txt, 78, "\n"));
406 } else {
407 $mymail->setWikiBody($txt);
408 }
409 if ($mymail->send()) {
410 $page->trig("Ton mail a bien été envoyé.");
411 $_REQUEST = array('bcc' => S::v('bestalias').'@'.$globals->mail->domain);
412 PlUpload::clear(S::v('forlife'), 'emails.send');
413 } else {
414 $page->trig("Erreur lors de l'envoi du courriel, réessaye.");
415 $page->assign('uploaded_f', PlUpload::listFilenames(S::v('forlife'), 'emails.send'));
416 }
81b5b746 417 }
418 }
419 } else {
3360c04f
FB
420 $res = XDB::query("SELECT data
421 FROM email_send_save
422 WHERE uid = {?}", S::i('uid'));
423 if ($res->numRows() == 0) {
424 PlUpload::clear(S::v('forlife'), 'emails.send');
425 $_REQUEST['bcc'] = S::v('bestalias').'@'.$globals->mail->domain;
426 } else {
427 $data = unserialize($res->fetchOneCell());
428 $_REQUEST = array_merge($_REQUEST, $data);
429 }
81b5b746 430 }
431
08cce2ff 432 $res = XDB::query(
81b5b746 433 "SELECT u.prenom, u.nom, u.promo, a.alias as forlife
434 FROM auth_user_md5 AS u
435 INNER JOIN contacts AS c ON (u.user_id = c.contact)
436 INNER JOIN aliases AS a ON (u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
437 WHERE c.uid = {?}
cab08090 438 ORDER BY u.nom, u.prenom", S::v('uid'));
81b5b746 439 $page->assign('contacts', $res->fetchAllAssoc());
5e68682e 440 $page->assign('maxsize', ini_get('upload_max_filesize') . 'o');
81b5b746 441 }
442
5ff3f06b
FB
443 function handler_test(&$page, $forlife = null)
444 {
445 global $globals;
446 if (!S::has_perms() || !$forlife) {
447 $forlife = S::v('bestalias');
448 }
b71f7275 449 $mailer = new PlMailer('emails/test.mail.tpl');
5ff3f06b
FB
450 $mailer->assign('email', $forlife . '@' . $globals->mail->domain);
451 $iterator = XDB::iterator("SELECT email
452 FROM emails AS e
453 INNER JOIN aliases AS a ON (e.uid = a.id)
454 WHERE FIND_IN_SET('active', e.flags) AND a.alias = {?}",
455 $forlife);
456 $mailer->assign('redirects', $iterator);
457 $res = XDB::query("SELECT FIND_IN_SET('femme', u.flags), prenom
458 FROM auth_user_md5 AS u
459 INNER JOIN aliases AS a ON (a.id = u.user_id)
460 WHERE a.alias = {?}", $forlife);
461 list($sexe, $prenom) = $res->fetchOneRow();
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
478 if ($warn == 'warn' && $email) {
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
484 INNER JOIN auth_user_md5 AS u ON e.uid = u.user_id
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
a7de4ef7 503Pour plus de rensignements sur le service de patte cassée, n'hésites pas à
2c388954 504consulter la page <{$globals->baseurl}/emails/broken>.
81b5b746 505
506
a7de4ef7 507A 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 }
518 } elseif (Post::has('email')) {
5e2307dc 519 $email = valide_email(Post::v('email'));
81b5b746 520
521 list(,$fqdn) = explode('@', $email);
522 $fqdn = strtolower($fqdn);
523 if ($fqdn == 'polytechnique.org' || $fqdn == 'melix.org'
524 || $fqdn == 'm4x.org' || $fqdn == 'melix.net')
525 {
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
569 switch (Post::v('action')) {
570 case 'create':
571 if (trim(Post::v('emailN')) != '') {
572 Xdb::execute('INSERT IGNORE INTO emails_watch (email, state, detection, last, uid, description)
573 VALUES ({?}, {?}, CURDATE(), NOW(), {?}, {?})',
574 trim(Post::v('emailN')), Post::v('stateN'), S::i('uid'), Post::v('descriptionN'));
575 };
576 break;
577
578 case 'edit':
579 Xdb::execute('UPDATE emails_watch
580 SET state = {?}, last = NOW(), uid = {?}, description = {?}
581 WHERE email = {?}', Post::v('stateN'), S::i('uid'), Post::v('descriptionN'), Post::v('emailN'));
582 break;
583
584 default:
585 if ($action == 'delete' && !is_null($email)) {
586 Xdb::execute('DELETE FROM emails_watch WHERE email = {?}', $email);
587 }
588 }
589 if ($action != 'create' && $action != 'edit') {
590 $action = 'list';
591 }
592 $page->assign('action', $action);
593
594 if ($action == 'list') {
595 $sql = "SELECT w.email, w.detection, w.state, a.alias AS forlife
596 FROM emails_watch AS w
ca6d07f4 597 LEFT JOIN emails AS e USING(email)
598 LEFT JOIN aliases AS a ON (a.id = e.uid AND a.type = 'a_vie')
9e570fe0 599 ORDER BY w.state, w.email, a.alias";
600 $it = Xdb::iterRow($sql);
601
602 $table = array();
603 $props = array();
604 while (list($email, $date, $state, $forlife) = $it->next()) {
605 if (count($props) == 0 || $props['mail'] != $email) {
606 if (count($props) > 0) {
607 $table[] = $props;
608 }
609 $props = array('mail' => $email,
610 'detection' => $date,
611 'state' => $state,
612 'users' => array($forlife));
613 } else {
614 $props['users'][] = $forlife;
615 }
616 }
617 if (count($props) > 0) {
618 $table[] = $props;
619 }
620 $page->assign('table', $table);
621 } elseif ($action == 'edit') {
622 $sql = "SELECT w.detection, w.state, w.last, w.description,
623 a1.alias AS edit, a2.alias AS forlife
624 FROM emails_watch AS w
eaf30d86 625 LEFT JOIN aliases AS a1 ON (a1.id = w.uid AND a1.type = 'a_vie')
9615c895 626 LEFT JOIN emails AS e ON (w.email = e.email)
ca6d07f4 627 LEFT JOIN aliases AS a2 ON (a2.id = e.uid AND a2.type = 'a_vie')
9e570fe0 628 WHERE w.email = {?}
629 ORDER BY a2.alias";
630 $it = Xdb::iterRow($sql, $email);
631
632 $props = array();
633 while (list($detection, $state, $last, $description, $edit, $forlife) = $it->next()) {
634 if (count($props) == 0) {
635 $props = array('mail' => $email,
636 'detection' => $detection,
637 'state' => $state,
638 'last' => $last,
639 'description' => $description,
640 'edit' => $edit,
641 'users' => array($forlife));
642 } else {
643 $props['users'][] = $forlife;
644 }
645 }
646 $page->assign('doublon', $props);
647 }
648 }
7727ffc7 649 function handler_lost(&$page, $action = 'list', $email = null)
650 {
651 $page->changeTpl('emails/lost.tpl');
eaf30d86 652
7727ffc7 653 $page->assign('lost_emails', XDB::iterator('
e1547442
FB
654 SELECT u.user_id, a.alias
655 FROM auth_user_md5 AS u
656 INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type = "a_vie")
657 LEFT JOIN emails AS e ON (u.user_id=e.uid AND FIND_IN_SET("active",e.flags))
658 WHERE e.uid IS NULL AND u.deces = 0
659 ORDER BY u.promo DESC, u.nom, u.prenom'));
7727ffc7 660 }
81b5b746 661}
662
a7de4ef7 663// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
81b5b746 664?>