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