Updates the GoogleApps password, if required, when an above-10-chars password was...
[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 $page->assign('retour', $retour);
224 }
225
226 if ($action == 'active' && $email) {
227 $redirect->modify_one_email($email, true);
228 }
229
230 if ($action == 'inactive' && $email) {
231 $redirect->modify_one_email($email, false);
232 }
233
234 if ($action == 'rewrite' && $email) {
235 $rewrite = @func_get_arg(3);
236 $redirect->modify_one_email_redirect($email, $rewrite);
237 }
238
239 if (Env::has('emailop')) {
240 S::assert_xsrf_token();
241
242 $actifs = Env::v('emails_actifs', Array());
243 print_r(Env::v('emails_rewrite'));
244 if (Env::v('emailop') == "ajouter" && Env::has('email')) {
245 $page->assign('retour', $redirect->add_email(Env::v('email')));
246 } elseif (empty($actifs)) {
247 $page->assign('retour', ERROR_INACTIVE_REDIRECTION);
248 } elseif (is_array($actifs)) {
249 $page->assign('retour', $redirect->modify_email($actifs,
250 Env::v('emails_rewrite',Array())));
251 }
252 }
253
254 $res = XDB::query(
255 "SELECT alias
256 FROM virtual
257 INNER JOIN virtual_redirect USING(vid)
258 WHERE (redirect={?} OR redirect={?})
259 AND alias LIKE '%@{$globals->mail->alias_dom}'",
260 $forlife.'@'.$globals->mail->domain, $forlife.'@'.$globals->mail->domain2);
261 $melix = $res->fetchOneCell();
262 if ($melix) {
263 list($melix) = explode('@', $melix);
264 $page->assign('melix',$melix);
265 }
266
267 $res = XDB::query(
268 "SELECT alias,expire
269 FROM aliases
270 WHERE id={?} AND (type='a_vie' OR type='alias')
271 ORDER BY !FIND_IN_SET('usage',flags), LENGTH(alias)", $uid);
272
273 $page->assign('alias', $res->fetchAllAssoc());
274 $page->assign('emails',$redirect->emails);
275
276 require_once 'googleapps.inc.php';
277 $page->assign('googleapps', GoogleAppsAccount::account_status($uid));
278 }
279
280 function handler_antispam(&$page, $statut_filtre = null)
281 {
282 require_once 'emails.inc.php';
283 $wp = new PlWikiPage('Xorg.Antispam');
284 $wp->buildCache();
285
286 $page->changeTpl('emails/antispam.tpl');
287
288 $bogo = new Bogo(S::v('uid'));
289 if (isset($statut_filtre)) {
290 $bogo->change($statut_filtre + 0);
291 }
292 $page->assign('filtre',$bogo->level());
293 }
294
295 function handler_submit(&$page)
296 {
297 $wp = new PlWikiPage('Xorg.Mails');
298 $wp->buildCache();
299 $page->changeTpl('emails/submit_spam.tpl');
300
301 if (Post::has('send_email')) {
302 S::assert_xsrf_token();
303
304 $upload = PlUpload::get($_FILES['mail'], S::v('forlife'), 'spam.submit', true);
305 if (!$upload) {
306 $page->trigError('Une erreur a été rencontrée lors du transfert du fichier');
307 return;
308 }
309 $mime = $upload->contentType();
310 if ($mime != 'text/x-mail' && $mime != 'message/rfc822') {
311 $upload->clear();
312 $page->trigError('Le fichier ne contient pas un email complet');
313 return;
314 }
315 global $globals;
316 $box = Post::v('type') . '@' . $globals->mail->domain;
317 $mailer = new PlMailer();
318 $mailer->addTo($box);
319 $mailer->setFrom('"' . S::v('prenom') . ' ' . S::v('nom') . '" <web@' . $globals->mail->domain . '>');
320 $mailer->setTxtBody(Post::v('type') . ' soumis par ' . S::v('forlife') . ' via le web');
321 $mailer->addUploadAttachment($upload, Post::v('type') . '.mail');
322 $mailer->send();
323 $page->trigSuccess('Le message a été transmis à ' . $box);
324 $upload->clear();
325 }
326 }
327
328 function handler_send(&$page)
329 {
330 global $globals;
331 $page->changeTpl('emails/send.tpl');
332 $page->addJsLink('ajax.js');
333
334 $page->setTitle('Envoyer un email');
335
336 // action si on recoit un formulaire
337 if (Post::has('save')) {
338 if (!S::has_xsrf_token()) {
339 return PL_FORBIDDEN;
340 }
341
342 unset($_POST['save']);
343 if (trim(preg_replace('/-- .*/', '', Post::v('contenu'))) != "") {
344 $_POST['to_contacts'] = explode(';', @$_POST['to_contacts']);
345 $_POST['cc_contacts'] = explode(';', @$_POST['cc_contacts']);
346 $data = serialize($_POST);
347 XDB::execute("REPLACE INTO email_send_save
348 VALUES ({?}, {?})", S::i('uid'), $data);
349 }
350 exit;
351 } else if (Env::v('submit') == 'Envoyer') {
352 S::assert_xsrf_token();
353
354 function getEmails($aliases)
355 {
356 if (!is_array($aliases)) {
357 return null;
358 }
359 $rel = Env::v('contacts');
360 $ret = array();
361 foreach ($aliases as $alias) {
362 $ret[$alias] = $rel[$alias];
363 }
364 return join(', ', $ret);
365 }
366
367 $error = false;
368 foreach ($_FILES as &$file) {
369 if ($file['name'] && !PlUpload::get($file, S::v('forlife'), 'emails.send', false)) {
370 $page->trigError(PlUpload::$lastError);
371 $error = true;
372 break;
373 }
374 }
375
376 if (!$error) {
377 XDB::execute("DELETE FROM email_send_save
378 WHERE uid = {?}", S::i('uid'));
379
380 $to2 = getEmails(Env::v('to_contacts'));
381 $cc2 = getEmails(Env::v('cc_contacts'));
382 $txt = str_replace('^M', '', Env::v('contenu'));
383 $to = Env::v('to');
384 $subj = Env::v('sujet');
385 $from = Env::v('from');
386 $cc = trim(Env::v('cc'));
387 $bcc = trim(Env::v('bcc'));
388
389 if (empty($to) && empty($cc) && empty($to2) && empty($bcc) && empty($cc2)) {
390 $page->trigError("Indique au moins un destinataire.");
391 $page->assign('uploaded_f', PlUpload::listFilenames(S::v('forlife'), 'emails.send'));
392 } else {
393 $mymail = new PlMailer();
394 $mymail->setFrom($from);
395 $mymail->setSubject($subj);
396 if (!empty($to)) { $mymail->addTo($to); }
397 if (!empty($cc)) { $mymail->addCc($cc); }
398 if (!empty($bcc)) { $mymail->addBcc($bcc); }
399 if (!empty($to2)) { $mymail->addTo($to2); }
400 if (!empty($cc2)) { $mymail->addCc($cc2); }
401 $files =& PlUpload::listFiles(S::v('forlife'), 'emails.send');
402 foreach ($files as $name=>&$upload) {
403 $mymail->addUploadAttachment($upload, $name);
404 }
405 if (Env::v('nowiki')) {
406 $mymail->setTxtBody(wordwrap($txt, 78, "\n"));
407 } else {
408 $mymail->setWikiBody($txt);
409 }
410 if ($mymail->send()) {
411 $page->trigSuccess("Ton email a bien été envoyé.");
412 $_REQUEST = array('bcc' => S::v('bestalias').'@'.$globals->mail->domain);
413 PlUpload::clear(S::v('forlife'), 'emails.send');
414 } else {
415 $page->trigError("Erreur lors de l'envoi du courriel, réessaye.");
416 $page->assign('uploaded_f', PlUpload::listFilenames(S::v('forlife'), 'emails.send'));
417 }
418 }
419 }
420 } else {
421 $res = XDB::query("SELECT data
422 FROM email_send_save
423 WHERE uid = {?}", S::i('uid'));
424 if ($res->numRows() == 0) {
425 PlUpload::clear(S::v('forlife'), 'emails.send');
426 $_REQUEST['bcc'] = S::v('bestalias').'@'.$globals->mail->domain;
427 } else {
428 $data = unserialize($res->fetchOneCell());
429 $_REQUEST = array_merge($_REQUEST, $data);
430 }
431 }
432
433 $res = XDB::query(
434 "SELECT u.prenom, u.nom, u.promo, a.alias as forlife
435 FROM auth_user_md5 AS u
436 INNER JOIN contacts AS c ON (u.user_id = c.contact)
437 INNER JOIN aliases AS a ON (u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
438 WHERE c.uid = {?}
439 ORDER BY u.nom, u.prenom", S::v('uid'));
440 $page->assign('contacts', $res->fetchAllAssoc());
441 $page->assign('maxsize', ini_get('upload_max_filesize') . 'o');
442 }
443
444 function handler_test(&$page, $forlife = null)
445 {
446 global $globals;
447 require_once 'emails.inc.php';
448
449 if (!S::has_xsrf_token()) {
450 return PL_FORBIDDEN;
451 }
452 if (!S::has_perms() || !$forlife) {
453 $forlife = S::v('bestalias');
454 }
455
456 $res = XDB::query("SELECT FIND_IN_SET('femme', u.flags), prenom, user_id
457 FROM auth_user_md5 AS u
458 INNER JOIN aliases AS a ON (a.id = u.user_id)
459 WHERE a.alias = {?}", $forlife);
460 list($sexe, $prenom, $uid) = $res->fetchOneRow();
461 $redirect = new Redirect($uid);
462
463 $mailer = new PlMailer('emails/test.mail.tpl');
464 $mailer->assign('email', $forlife . '@' . $globals->mail->domain);
465 $mailer->assign('redirects', $redirect->active_emails());
466 $mailer->assign('sexe', $sexe);
467 $mailer->assign('prenom', $prenom);
468 $mailer->send();
469 exit;
470 }
471
472 function handler_rewrite_in(&$page, $mail, $hash)
473 {
474 $page->changeTpl('emails/rewrite.tpl');
475 $page->assign('option', 'in');
476 if (empty($mail) || empty($hash)) {
477 return PL_NOT_FOUND;
478 }
479 $pos = strrpos($mail, '_');
480 if ($pos === false) {
481 return PL_NOT_FOUND;
482 }
483 $mail{$pos} = '@';
484 $res = XDB::query("SELECT COUNT(*)
485 FROM emails
486 WHERE email = {?} AND hash = {?}",
487 $mail, $hash);
488 $count = intval($res->fetchOneCell());
489 if ($count > 0) {
490 XDB::query("UPDATE emails
491 SET allow_rewrite = true, hash = NULL
492 WHERE email = {?} AND hash = {?}",
493 $mail, $hash);
494 $page->trigSuccess("Réécriture activée pour l'adresse " . $mail);
495 return;
496 }
497 return PL_NOT_FOUND;
498 }
499
500 function handler_rewrite_out(&$page, $mail, $hash)
501 {
502 $page->changeTpl('emails/rewrite.tpl');
503 $page->assign('option', 'out');
504 if (empty($mail) || empty($hash)) {
505 return PL_NOT_FOUND;
506 }
507 $pos = strrpos($mail, '_');
508 if ($pos === false) {
509 return PL_NOT_FOUND;
510 }
511 $mail{$pos} = '@';
512 $res = XDB::query("SELECT COUNT(*)
513 FROM emails
514 WHERE email = {?} AND hash = {?}",
515 $mail, $hash);
516 $count = intval($res->fetchOneCell());
517 if ($count > 0) {
518 global $globals;
519 $res = XDB::query("SELECT e.email, e.rewrite, a.alias
520 FROM emails AS e
521 INNER JOIN aliases AS a ON (a.id = e.uid AND a.type = 'a_vie')
522 WHERE e.email = {?} AND e.hash = {?}",
523 $mail, $hash);
524 XDB::query("UPDATE emails
525 SET allow_rewrite = false, hash = NULL
526 WHERE email = {?} AND hash = {?}",
527 $mail, $hash);
528 list($mail, $rewrite, $forlife) = $res->fetchOneRow();
529 $mail = new PlMailer();
530 $mail->setFrom("webmaster@" . $globals->mail->domain);
531 $mail->addTo("support@" . $globals->mail->domain);
532 $mail->setSubject("Tentative de détournement de correspondance via le rewrite");
533 $mail->setTxtBody("$forlife a tenté un rewrite de $mail vers $rewrite. Cette demande a été rejetée via le web");
534 $mail->send();
535 $page->trigWarning("Un mail d'alerte a été envoyé à l'équipe de " . $globals->core->sitename);
536 return;
537 }
538 return PL_NOT_FOUND;
539 }
540
541 function handler_imap_in(&$page, $hash = null, $login = null)
542 {
543 $page->changeTpl('emails/imap_register.tpl');
544 $id = null;
545 if (!empty($hash) || !empty($login)) {
546 $req = XDB::query("SELECT u.prenom, FIND_IN_SET('femme', u.flags) AS sexe, a.id
547 FROM aliases AS a
548 INNER JOIN newsletter_ins AS ni ON (a.id = ni.user_id)
549 INNER JOIN auth_user_md5 AS u ON (u.user_id = a.id)
550 WHERE a.alias = {?} AND ni.hash = {?}", $login, $hash);
551 list($prenom, $sexe, $id) = $req->fetchOneRow();
552 }
553
554 require_once('emails.inc.php');
555 $page->assign('ok', false);
556 if (S::logged() && (is_null($id) || $id == S::i('uid'))) {
557 $storage = new EmailStorage(S::i('uid'), 'imap');
558 $storage->activate();
559 $page->assign('ok', true);
560 $page->assign('prenom', S::v('prenom'));
561 $page->assign('sexe', S::v('femme'));
562 } else if (!S::logged() && $id) {
563 $storage = new EmailStorage($id, 'imap');
564 $storage->activate();
565 $page->assign('ok', true);
566 $page->assign('prenom', $prenom);
567 $page->assign('sexe', $sexe);
568 }
569 }
570
571 function handler_broken(&$page, $warn = null, $email = null)
572 {
573 require_once 'emails.inc.php';
574 $wp = new PlWikiPage('Xorg.PatteCassée');
575 $wp->buildCache();
576
577 global $globals;
578
579 $page->changeTpl('emails/broken.tpl');
580
581 if ($warn == 'warn' && $email) {
582 S::assert_xsrf_token();
583
584 $email = valide_email($email);
585 // vérifications d'usage
586 $sel = XDB::query(
587 "SELECT e.uid, a.alias
588 FROM emails AS e
589 INNER JOIN aliases AS a ON (e.uid = a.id AND type!='homonyme'
590 AND FIND_IN_SET('bestalias',a.flags))
591 WHERE e.email={?}", $email);
592
593 if (list($uid, $dest) = $sel->fetchOneRow()) {
594 // envoi du mail
595 $message = "Bonjour !
596
597Cet email a été généré automatiquement par le service de patte cassée de
598Polytechnique.org car un autre utilisateur, ".S::v('prenom').' '.S::v('nom').",
599nous a signalé qu'en t'envoyant un email, il avait reçu un message d'erreur
600indiquant que ton adresse de redirection $email
601ne fonctionnait plus !
602
603Nous te suggérons de vérifier cette adresse, et le cas échéant de mettre
604à jour sur le site <{$globals->baseurl}/emails> tes adresses
605de redirection...
606
607Pour plus de renseignements sur le service de patte cassée, n'hésite pas à
608consulter la page <{$globals->baseurl}/emails/broken>.
609
610
611À bientôt sur Polytechnique.org !
612L'équipe d'administration <support@" . $globals->mail->domain . '>';
613
614 $mail = new PlMailer();
615 $mail->setFrom('"Polytechnique.org" <support@' . $globals->mail->domain . '>');
616 $mail->addTo("$dest@" . $globals->mail->domain);
617 $mail->setSubject("Une de tes adresse de redirection Polytechnique.org ne marche plus !!");
618 $mail->setTxtBody($message);
619 $mail->send();
620 $page->trigSuccess("Email envoyé !");
621 }
622 } elseif (Post::has('email')) {
623 S::assert_xsrf_token();
624
625 $email = valide_email(Post::v('email'));
626
627 list(,$fqdn) = explode('@', $email);
628 $fqdn = strtolower($fqdn);
629 if ($fqdn == 'polytechnique.org' || $fqdn == 'melix.org' || $fqdn == 'm4x.org' || $fqdn == 'melix.net') {
630 $page->assign('neuneu', true);
631 } else {
632 $page->assign('email',$email);
633 $sel = XDB::query(
634 "SELECT e1.uid, e1.panne != 0 AS panne,
635 (count(e2.uid) + IF(FIND_IN_SET('googleapps', u.mail_storage), 1, 0)) AS nb_mails,
636 u.nom, u.prenom, u.promo, a.alias AS forlife
637 FROM emails as e1
638 LEFT JOIN emails as e2 ON(e1.uid = e2.uid
639 AND FIND_IN_SET('active', e2.flags)
640 AND e1.email != e2.email)
641 INNER JOIN auth_user_md5 as u ON(e1.uid = u.user_id)
642 INNER JOIN aliases AS a ON (a.id = e1.uid AND a.type = 'a_vie')
643 WHERE e1.email = {?}
644 GROUP BY e1.uid", $email);
645 if ($x = $sel->fetchOneAssoc()) {
646 // on écrit dans la base que l'adresse est cassée
647 if (!$x['panne']) {
648 XDB::execute("UPDATE emails
649 SET panne=NOW(),
650 last=NOW(),
651 panne_level = 1
652 WHERE email = {?}", $email);
653 } else {
654 XDB::execute("UPDATE emails
655 SET panne_level = 1
656 WHERE email = {?} AND panne_level = 0", $email);
657 }
658 $page->assign_by_ref('x', $x);
659 }
660 }
661 }
662 }
663
664 function handler_duplicated(&$page, $action = 'list', $email = null)
665 {
666 $page->changeTpl('emails/duplicated.tpl');
667
668 $states = array('pending' => 'En attente...',
669 'safe' => 'Pas d\'inquiétude',
670 'unsafe' => 'Recherches en cours',
671 'dangerous' => 'Usurpations par cette adresse');
672 $page->assign('states', $states);
673
674 if (Post::has('action')) {
675 S::assert_xsrf_token();
676 }
677 switch (Post::v('action')) {
678 case 'create':
679 if (trim(Post::v('emailN')) != '') {
680 Xdb::execute('INSERT IGNORE INTO emails_watch (email, state, detection, last, uid, description)
681 VALUES ({?}, {?}, CURDATE(), NOW(), {?}, {?})',
682 trim(Post::v('emailN')), Post::v('stateN'), S::i('uid'), Post::v('descriptionN'));
683 };
684 break;
685
686 case 'edit':
687 Xdb::execute('UPDATE emails_watch
688 SET state = {?}, last = NOW(), uid = {?}, description = {?}
689 WHERE email = {?}', Post::v('stateN'), S::i('uid'), Post::v('descriptionN'), Post::v('emailN'));
690 break;
691
692 default:
693 if ($action == 'delete' && !is_null($email)) {
694 Xdb::execute('DELETE FROM emails_watch WHERE email = {?}', $email);
695 }
696 }
697 if ($action != 'create' && $action != 'edit') {
698 $action = 'list';
699 }
700 $page->assign('action', $action);
701
702 if ($action == 'list') {
703 $sql = "SELECT w.email, w.detection, w.state, a.alias AS forlife
704 FROM emails_watch AS w
705 LEFT JOIN emails AS e USING(email)
706 LEFT JOIN aliases AS a ON (a.id = e.uid AND a.type = 'a_vie')
707 ORDER BY w.state, w.email, a.alias";
708 $it = Xdb::iterRow($sql);
709
710 $table = array();
711 $props = array();
712 while (list($email, $date, $state, $forlife) = $it->next()) {
713 if (count($props) == 0 || $props['mail'] != $email) {
714 if (count($props) > 0) {
715 $table[] = $props;
716 }
717 $props = array('mail' => $email,
718 'detection' => $date,
719 'state' => $state,
720 'users' => array($forlife));
721 } else {
722 $props['users'][] = $forlife;
723 }
724 }
725 if (count($props) > 0) {
726 $table[] = $props;
727 }
728 $page->assign('table', $table);
729 } elseif ($action == 'edit') {
730 $sql = "SELECT w.detection, w.state, w.last, w.description,
731 a1.alias AS edit, a2.alias AS forlife
732 FROM emails_watch AS w
733 LEFT JOIN aliases AS a1 ON (a1.id = w.uid AND a1.type = 'a_vie')
734 LEFT JOIN emails AS e ON (w.email = e.email)
735 LEFT JOIN aliases AS a2 ON (a2.id = e.uid AND a2.type = 'a_vie')
736 WHERE w.email = {?}
737 ORDER BY a2.alias";
738 $it = Xdb::iterRow($sql, $email);
739
740 $props = array();
741 while (list($detection, $state, $last, $description, $edit, $forlife) = $it->next()) {
742 if (count($props) == 0) {
743 $props = array('mail' => $email,
744 'detection' => $detection,
745 'state' => $state,
746 'last' => $last,
747 'description' => $description,
748 'edit' => $edit,
749 'users' => array($forlife));
750 } else {
751 $props['users'][] = $forlife;
752 }
753 }
754 $page->assign('doublon', $props);
755 }
756 }
757 function handler_lost(&$page, $action = 'list', $email = null)
758 {
759 $page->changeTpl('emails/lost.tpl');
760
761 $page->assign('lost_emails', XDB::iterator('
762 SELECT u.user_id, a.alias
763 FROM auth_user_md5 AS u
764 INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type = "a_vie")
765 LEFT JOIN emails AS e ON (u.user_id=e.uid AND FIND_IN_SET("active",e.flags))
766 WHERE e.uid IS NULL AND
767 FIND_IN_SET("googleapps", u.mail_storage) = 0 AND
768 u.deces = 0
769 ORDER BY u.promo DESC, u.nom, u.prenom'));
770 }
771}
772
773// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
774?>