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