2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class EmailModule
extends PLModule
27 'emails' => $this->make_hook('emails', AUTH_COOKIE
, 'mail'),
28 'emails/alias' => $this->make_hook('alias', AUTH_MDP
, 'mail'),
29 'emails/antispam' => $this->make_hook('antispam', AUTH_MDP
, 'mail'),
30 'emails/broken' => $this->make_hook('broken', AUTH_COOKIE
),
31 'emails/redirect' => $this->make_hook('redirect', AUTH_MDP
, 'mail'),
32 'emails/send' => $this->make_hook('send', AUTH_MDP
, 'mail'),
33 'emails/antispam/submit' => $this->make_hook('submit', AUTH_COOKIE
),
34 'emails/test' => $this->make_hook('test', AUTH_COOKIE
, 'mail', NO_AUTH
),
36 'emails/rewrite/in' => $this->make_hook('rewrite_in', AUTH_PUBLIC
),
37 'emails/rewrite/out' => $this->make_hook('rewrite_out', AUTH_PUBLIC
),
39 'emails/imap/in' => $this->make_hook('imap_in', AUTH_PUBLIC
),
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 'admin/emails/broken' => $this->make_hook('broken_addr', AUTH_MDP
, 'admin'),
48 function handler_emails(&$page, $action = null
, $email = null
)
51 require_once 'emails.inc.php';
53 $page->changeTpl('emails/index.tpl');
54 $page->setTitle('Mes emails');
58 // Apply the bestalias change request.
59 if ($action == 'best' && $email) {
60 if (!S
::has_xsrf_token()) {
64 XDB
::execute("UPDATE aliases
65 SET flags = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', flags, ','), ',bestalias,', ','))
66 WHERE uid = {?}", $user->id());
67 XDB
::execute("UPDATE aliases
68 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'bestalias')
69 WHERE uid = {?} AND alias = {?}", $user->id(), $email);
71 // As having a non-null bestalias value is critical in
72 // plat/al's code, we do an a posteriori check on the
73 // validity of the bestalias.
77 // Fetch and display aliases.
78 $sql = "SELECT alias, (type='a_vie') AS a_vie,
79 (alias REGEXP '\\\\.[0-9]{2}$') AS cent_ans,
80 FIND_IN_SET('bestalias',flags) AS best, expire
82 WHERE uid = {?} AND type!='homonyme'
83 ORDER BY LENGTH(alias)";
84 $page->assign('aliases', XDB
::iterator($sql, $user->id()));
86 // Check for homonyms.
87 $page->assign('homonyme', $user->homonyme
);
89 // Display active redirections.
90 $redirect = new Redirect($user);
91 $page->assign('mails', $redirect->active_emails());
93 // Display, when available, the @alias_dom email alias.
97 INNER JOIN virtual_redirect AS vr USING(vid)
98 WHERE (redirect={?} OR redirect={?})
99 AND alias LIKE '%@{$globals->mail->alias_dom}'",
100 $user->forlifeEmail(),
101 // TODO: remove this über-ugly hack. The issue is that you need
102 // to remove all @m4x.org addresses in virtual_redirect first.
103 $user->login() . '@' . $globals->mail
->domain2
);
104 $page->assign('melix', $res->fetchOneCell());
107 function handler_alias(&$page, $action = null
, $value = null
)
111 $page->changeTpl('emails/alias.tpl');
112 $page->setTitle('Alias melix.net');
115 $page->assign('demande', AliasReq
::get_request($user->id()));
117 // Remove the email alias.
118 if ($action == 'delete' && $value) {
119 S
::assert_xsrf_token();
122 "DELETE virtual, virtual_redirect
124 INNER JOIN virtual_redirect USING (vid)
125 WHERE alias = {?} AND (redirect = {?} OR redirect = {?})",
126 $value, $user->forlifeEmail(), $user->m4xForlifeEmail());
129 // Fetch existing @alias_dom aliases.
130 $alias = $user->emailAlias();
131 $visibility = $user->hasProfile() && $user->profile()->alias_pub
;
132 $page->assign('actuel', $alias);
133 $page->assign('user', $user);
134 $page->assign('mail_public', $visibility);
136 if ($action == 'ask' && Env
::has('alias') && Env
::has('raison')) {
137 S
::assert_xsrf_token();
139 //Si l'utilisateur vient de faire une damande
140 $alias = Env
::v('alias');
141 $raison = Env
::v('raison');
142 $public = (Env
::v('public', 'off') == 'on') ?
'public' : 'private';
144 $page->assign('r_alias', $alias);
145 $page->assign('r_raison', $raison);
146 if ($public == 'public') {
147 $page->assign('r_public', true
);
150 //Quelques vérifications sur l'alias (caractères spéciaux)
151 if (!preg_match("/^[a-zA-Z0-9\-.]{3,20}$/", $alias)) {
152 $page->trigError("L'adresse demandée n'est pas valide."
153 . " Vérifie qu'elle comporte entre 3 et 20 caractères"
154 . " et qu'elle ne contient que des lettres non accentuées,"
155 . " des chiffres ou les caractères - et .");
158 $alias_mail = $alias.'@'.$globals->mail
->alias_dom
;
160 //vérifier que l'alias n'est pas déja pris
161 $res = XDB
::query('SELECT COUNT(*)
165 if ($res->fetchOneCell() > 0) {
166 $page->trigError("L'alias $alias_mail a déja été attribué.
167 Tu ne peux donc pas l'obtenir.");
171 //vérifier que l'alias n'est pas déja en demande
172 $it = Validate
::iterate('alias');
173 while($req = $it->next()) {
174 if ($req->alias
== $alias_mail) {
175 $page->trigError("L'alias $alias_mail a déja été demandé.
176 Tu ne peux donc pas l'obtenir pour l'instant.");
181 //Insertion de la demande dans la base, écrase les requêtes précédente
182 $myalias = new AliasReq($user, $alias, $raison, $public);
184 $page->assign('success',$alias);
187 } elseif ($action == 'set' && ($value == 'public' ||
$value == 'private')) {
188 if (!S
::has_xsrf_token()) {
192 if ($user->hasProfile()) {
193 XDB
::execute("UPDATE profiles
196 $value, $user->profile()->id());
198 $visibility = ($value == 'public');
202 $page->assign('actuel', $alias);
203 $page->assign('user', $user);
204 $page->assign('mail_public', $visibility);
207 function handler_redirect(&$page, $action = null
, $email = null
)
211 require_once 'emails.inc.php';
213 $page->changeTpl('emails/redirect.tpl');
216 $page->assign_by_ref('user', $user);
217 $page->assign('eleve', $user->promo() >= date("Y") - 5);
219 $redirect = new Redirect($user);
221 // FS#703 : $_GET is urldecoded twice, hence
222 // + (the data) => %2B (in the url) => + (first decoding) => ' ' (second decoding)
223 // Since there can be no spaces in emails, we can fix this with :
224 $email = str_replace(' ', '+', $email);
226 // Apply email redirection change requests.
228 if ($action == 'remove' && $email) {
229 $result = $redirect->delete_email($email);
232 if ($action == 'active' && $email) {
233 $redirect->modify_one_email($email, true
);
236 if ($action == 'inactive' && $email) {
237 $redirect->modify_one_email($email, false
);
240 if ($action == 'rewrite' && $email) {
241 $rewrite = @func_get_arg
(3);
242 $redirect->modify_one_email_redirect($email, $rewrite);
245 if (Env
::has('emailop')) {
246 S
::assert_xsrf_token();
248 $actifs = Env
::v('emails_actifs', Array());
249 print_r(Env
::v('emails_rewrite'));
250 if (Env
::v('emailop') == "ajouter" && Env
::has('email')) {
251 $error_email = false
;
252 $new_email = Env
::v('email');
253 if ($new_email == "new@example.org") {
254 $new_email = Env
::v('email_new');
256 $result = $redirect->add_email($new_email);
257 if ($result == ERROR_INVALID_EMAIL
) {
259 $page->assign('email', $new_email);
261 $page->assign('retour', $result);
262 $page->assign('error_email', $error_email);
263 } elseif (empty($actifs)) {
264 $result = ERROR_INACTIVE_REDIRECTION
;
265 } elseif (is_array($actifs)) {
266 $result = $redirect->modify_email($actifs, Env
::v('emails_rewrite', Array()));
271 case ERROR_INACTIVE_REDIRECTION
:
272 $page->trigError('Tu ne peux pas avoir aucune adresse de redirection active, sinon ton adresse '
273 . $user->forlifeEmail() . ' ne fonctionnerait plus.');
275 case ERROR_INVALID_EMAIL
:
276 $page->trigError('Erreur : l\'email n\'est pas valide.');
278 case ERROR_LOOP_EMAIL
:
279 $page->trigError('Erreur : ' . $user->forlifeEmail()
280 . ' ne doit pas être renvoyé vers lui-même, ni vers son équivalent en '
281 . $globals->mail
->domain2
. ' ni vers polytechnique.edu.');
285 // Fetch the @alias_dom email alias, if any.
289 INNER JOIN virtual_redirect USING(vid)
290 WHERE (redirect={?} OR redirect={?})
291 AND alias LIKE '%@{$globals->mail->alias_dom}'",
292 $user->forlifeEmail(),
293 // TODO: remove this über-ugly hack. The issue is that you need
294 // to remove all @m4x.org addresses in virtual_redirect first.
295 $user->login() . '@' . $globals->mail
->domain2
);
296 $melix = $res->fetchOneCell();
298 list($melix) = explode('@', $melix);
299 $page->assign('melix',$melix);
302 // Fetch existing email aliases.
306 WHERE uid={?} AND (type='a_vie' OR type='alias')
307 ORDER BY !FIND_IN_SET('usage',flags), LENGTH(alias)", $user->id());
308 $page->assign('alias', $res->fetchAllAssoc());
309 $page->assign('emails', $redirect->emails
);
311 // Display GoogleApps acount information.
312 require_once 'googleapps.inc.php';
313 $page->assign('googleapps', GoogleAppsAccount
::account_status($user->id()));
315 require_once 'emails.combobox.inc.php';
316 fill_email_combobox($page);
319 function handler_antispam(&$page, $statut_filtre = null
)
321 require_once 'emails.inc.php';
322 $wp = new PlWikiPage('Xorg.Antispam');
325 $page->changeTpl('emails/antispam.tpl');
328 $bogo = new Bogo($user);
329 if (isset($statut_filtre)) {
330 $bogo->change($statut_filtre +
0);
332 $page->assign('filtre', $bogo->level());
335 function handler_submit(&$page)
337 $wp = new PlWikiPage('Xorg.Mails');
339 $page->changeTpl('emails/submit_spam.tpl');
341 if (Post
::has('send_email')) {
342 S
::assert_xsrf_token();
344 $upload = PlUpload
::get($_FILES['mail'], S
::user()->login(), 'spam.submit', true
);
346 $page->trigError('Une erreur a été rencontrée lors du transfert du fichier');
349 $mime = $upload->contentType();
350 if ($mime != 'text/x-mail' && $mime != 'message/rfc822') {
352 $page->trigError('Le fichier ne contient pas un email complet');
355 $type = (Post
::v('type') == 'spam' ?
'spam' : 'nonspam');
358 $box = $type . '@' . $globals->mail
->domain
;
359 $mailer = new PlMailer();
360 $mailer->addTo($box);
361 $mailer->setFrom('"' . S
::user()->fullName() . '" <web@' . $globals->mail
->domain
. '>');
362 $mailer->setTxtBody($type . ' soumis par ' . S
::user()->login() . ' via le web');
363 $mailer->addUploadAttachment($upload, $type . '.mail');
365 $page->trigSuccess('Le message a été transmis à ' . $box);
370 function handler_send(&$page)
372 $page->changeTpl('emails/send.tpl');
373 $page->addJsLink('ajax.js');
375 $page->setTitle('Envoyer un email');
377 // action si on recoit un formulaire
378 if (Post
::has('save')) {
379 if (!S
::has_xsrf_token()) {
383 unset($_POST['save']);
384 if (trim(preg_replace('/-- .*/', '', Post
::v('contenu'))) != "") {
385 $_POST['to_contacts'] = explode(';', @$_POST['to_contacts']);
386 $_POST['cc_contacts'] = explode(';', @$_POST['cc_contacts']);
387 $data = serialize($_POST);
388 XDB
::execute("REPLACE INTO email_send_save
390 S
::user()->id('uid'), $data);
393 } else if (Env
::v('submit') == 'Envoyer') {
394 S
::assert_xsrf_token();
396 function getEmails($aliases)
398 if (!is_array($aliases)) {
401 $uf = new UserFilter(new UFC_Hrpid($aliases));
402 $users = $uf->iterUsers();
404 while ($user = $users->next()) {
405 $ret[] = $user->forlife
;
407 return join(', ', $ret);
411 foreach ($_FILES as &$file) {
412 if ($file['name'] && !PlUpload
::get($file, S
::user()->login(), 'emails.send', false
)) {
413 $page->trigError(PlUpload
::$lastError);
420 XDB
::execute("DELETE FROM email_send_save
424 $to2 = getEmails(Env
::v('to_contacts'));
425 $cc2 = getEmails(Env
::v('cc_contacts'));
426 $txt = str_replace('^M', '', Env
::v('contenu'));
427 $to = str_replace(';', ',', Env
::t('to'));
428 $subj = Env
::t('sujet');
429 $from = Env
::t('from');
430 $cc = str_replace(';', ',', Env
::t('cc'));
431 $bcc = str_replace(';', ',', Env
::t('bcc'));
433 $email_regex = '/^[a-z0-9.\-+_\$]+@([\-.+_]?[a-z0-9])+$/i';
434 foreach (explode(',', $to . ',' . $cc . ',' . $bcc) as $email) {
435 $email = trim($email);
436 if ($email != '' && !preg_match($email_regex, $email)) {
437 $page->trigError("L'adresse email " . $email . ' est erronée.');
441 if (empty($to) && empty($cc) && empty($to2) && empty($bcc) && empty($cc2)) {
442 $page->trigError("Indique au moins un destinataire.");
447 $page->assign('uploaded_f', PlUpload
::listFilenames(S
::user()->login(), 'emails.send'));
449 $mymail = new PlMailer();
450 $mymail->setFrom($from);
451 $mymail->setSubject($subj);
452 if (!empty($to)) { $mymail->addTo($to); }
453 if (!empty($cc)) { $mymail->addCc($cc); }
454 if (!empty($bcc)) { $mymail->addBcc($bcc); }
455 if (!empty($to2)) { $mymail->addTo($to2); }
456 if (!empty($cc2)) { $mymail->addCc($cc2); }
457 $files =& PlUpload
::listFiles(S
::user()->login(), 'emails.send');
458 foreach ($files as $name=>&$upload) {
459 $mymail->addUploadAttachment($upload, $name);
461 if (Env
::v('nowiki')) {
462 $mymail->setTxtBody(wordwrap($txt, 78, "\n"));
464 $mymail->setWikiBody($txt);
466 if ($mymail->send()) {
467 $page->trigSuccess("Ton email a bien été envoyé.");
468 $_REQUEST = array('bcc' => S
::user()->bestEmail());
469 PlUpload
::clear(S
::user()->login(), 'emails.send');
471 $page->trigError("Erreur lors de l'envoi du courriel, réessaye.");
472 $page->assign('uploaded_f', PlUpload
::listFilenames(S
::user()->login(), 'emails.send'));
477 $res = XDB
::query("SELECT data
479 WHERE uid = {?}", S
::i('uid'));
480 if ($res->numRows() == 0) {
481 PlUpload
::clear(S
::user()->login(), 'emails.send');
482 $_REQUEST['bcc'] = S
::user()->bestEmail();
484 $data = unserialize($res->fetchOneCell());
485 $_REQUEST = array_merge($_REQUEST, $data);
489 $uf = new UserFilter(new PFC_And(new UFC_Contact(S
::user()),
490 new UFC_Registered()),
491 UserFilter
::sortByName());
492 $contacts = $uf->getProfiles();
493 $page->assign('contacts', $contacts);
494 $page->assign('maxsize', ini_get('upload_max_filesize') . 'o');
495 $page->assign('user', S
::user());
498 function handler_test(&$page, $hruid = null
)
500 require_once 'emails.inc.php';
502 if (!S
::has_xsrf_token()) {
506 // Retrieves the User object for the test email recipient.
507 if (S
::admin() && $hruid) {
508 $user = User
::getSilent($hruid);
516 // Sends the test email.
517 $redirect = new Redirect($user);
519 $mailer = new PlMailer('emails/test.mail.tpl');
520 $mailer->assign('email', $user->bestEmail());
521 $mailer->assign('redirects', $redirect->active_emails());
522 $mailer->assign('display_name', $user->displayName());
523 $mailer->assign('sexe', $user->isFemale());
524 $mailer->send($user->isEmailFormatHtml());
528 function handler_rewrite_in(&$page, $mail, $hash)
530 $page->changeTpl('emails/rewrite.tpl');
531 $page->assign('option', 'in');
532 if (empty($mail) ||
empty($hash)) {
535 $pos = strrpos($mail, '_');
536 if ($pos === false
) {
540 $res = XDB
::query("SELECT COUNT(*)
542 WHERE email = {?} AND hash = {?}",
544 $count = intval($res->fetchOneCell());
546 XDB
::query("UPDATE emails
547 SET allow_rewrite = true, hash = NULL
548 WHERE email = {?} AND hash = {?}",
550 $page->trigSuccess("Réécriture activée pour l'adresse " . $mail);
556 function handler_rewrite_out(&$page, $mail, $hash)
558 $page->changeTpl('emails/rewrite.tpl');
559 $page->assign('option', 'out');
560 if (empty($mail) ||
empty($hash)) {
563 $pos = strrpos($mail, '_');
564 if ($pos === false
) {
568 $res = XDB
::query("SELECT COUNT(*)
570 WHERE email = {?} AND hash = {?}",
572 $count = intval($res->fetchOneCell());
575 $res = XDB
::query("SELECT e.email, e.rewrite, a.alias
577 INNER JOIN aliases AS a ON (a.uid = e.uid AND a.type = 'a_vie')
578 WHERE e.email = {?} AND e.hash = {?}",
580 XDB
::query("UPDATE emails
581 SET allow_rewrite = false, hash = NULL
582 WHERE email = {?} AND hash = {?}",
584 list($mail, $rewrite, $forlife) = $res->fetchOneRow();
585 $mail = new PlMailer();
586 $mail->setFrom("webmaster@" . $globals->mail
->domain
);
587 $mail->addTo("support@" . $globals->mail
->domain
);
588 $mail->setSubject("Tentative de détournement de correspondance via le rewrite");
589 $mail->setTxtBody("$forlife a tenté un rewrite de $mail vers $rewrite. Cette demande a été rejetée via le web");
591 $page->trigWarning("Un mail d'alerte a été envoyé à l'équipe de " . $globals->core
->sitename
);
597 function handler_imap_in(&$page, $hash = null
, $login = null
)
599 $page->changeTpl('emails/imap_register.tpl');
601 if (!empty($hash) ||
!empty($login)) {
602 $user = User
::getSilent($login);
604 $req = XDB
::query('SELECT 1
606 WHERE uid = {?} AND hash = {?}',
608 if ($req->numRows() == 0) {
614 require_once('emails.inc.php');
615 $page->assign('ok', false
);
616 if (S
::logged() && (is_null($user) ||
$user->id() == S
::i('uid'))) {
617 $storage = new EmailStorage(S
::user(), 'imap');
618 $storage->activate();
619 $page->assign('ok', true
);
620 $page->assign('yourself', S
::user()->displayName());
621 $page->assign('sexe', S
::user()->isFemale());
622 } else if (!S
::logged() && $user) {
623 $storage = new EmailStorage($user, 'imap');
624 $storage->activate();
625 $page->assign('ok', true
);
626 $page->assign('yourself', $user->displayName());
627 $page->assign('sexe', $user->isFemale());
631 function handler_broken(&$page, $warn = null
, $email = null
)
633 require_once 'emails.inc.php';
634 $wp = new PlWikiPage('Xorg.PatteCassée');
639 $page->changeTpl('emails/broken.tpl');
641 if ($warn == 'warn' && $email) {
642 S
::assert_xsrf_token();
644 $email = valide_email($email);
645 // vérifications d'usage
646 $uid = XDB
::fetchOneCell("SELECT uid
648 WHERE email = {?}", $email);
650 $dest = User
::getWithUID($uid);
652 $mail = new PlMailer('emails/broken-web.mail.tpl');
653 $mail->assign('email', $email);
654 $mail->assign('request', S
::user());
655 $mail->sendTo($dest);
656 $page->trigSuccess('Email envoyé !');
658 } elseif (Post
::has('email')) {
659 S
::assert_xsrf_token();
661 $email = valide_email(Post
::v('email'));
663 list(,$fqdn) = explode('@', $email);
664 $fqdn = strtolower($fqdn);
665 if ($fqdn == 'polytechnique.org' ||
$fqdn == 'melix.org' ||
$fqdn == 'm4x.org' ||
$fqdn == 'melix.net') {
666 $page->assign('neuneu', true
);
668 $page->assign('email',$email);
669 $x = XDB
::fetchOneAssoc("SELECT e1.uid, e1.panne != 0 AS panne, a.hruid,
670 (COUNT(e2.uid) + IF(FIND_IN_SET('googleapps', eo.storage), 1, 0)) AS nb_mails
672 INNER JOIN email_options AS eo ON (eo.uid = e1.uid)
673 INNER JOIN accounts AS a ON (e1.uid = a.uid)
674 LEFT JOIN emails AS e2 ON (e1.uid = e2.uid
675 AND FIND_IN_SET('active', e2.flags)
676 AND e1.email != e2.email)
678 GROUP BY e1.uid", $email);
680 // on écrit dans la base que l'adresse est cassée
682 XDB
::execute("UPDATE emails
683 SET panne=NOW(), last=NOW(), panne_level = 1
684 WHERE email = {?}", $email);
686 XDB
::execute("UPDATE emails
688 WHERE email = {?} AND panne_level = 0", $email);
690 $x['user'] = User
::getWithUID($x['uid']);
691 $page->assign_by_ref('x', $x);
697 function handler_duplicated(&$page, $action = 'list', $email = null
)
699 $page->changeTpl('emails/duplicated.tpl');
701 $states = array('pending' => 'En attente...',
702 'safe' => 'Pas d\'inquiétude',
703 'unsafe' => 'Recherches en cours',
704 'dangerous' => 'Usurpations par cette adresse');
705 $page->assign('states', $states);
707 if (Post
::has('action')) {
708 S
::assert_xsrf_token();
710 switch (Post
::v('action')) {
712 if (trim(Post
::v('emailN')) != '') {
713 Xdb
::execute('INSERT IGNORE INTO email_watch (email, state, detection, last, uid, description)
714 VALUES ({?}, {?}, CURDATE(), NOW(), {?}, {?})',
715 trim(Post
::v('emailN')), Post
::v('stateN'), S
::i('uid'), Post
::v('descriptionN'));
720 Xdb
::execute('UPDATE email_watch
721 SET state = {?}, last = NOW(), uid = {?}, description = {?}
722 WHERE email = {?}', Post
::v('stateN'), S
::i('uid'), Post
::v('descriptionN'), Post
::v('emailN'));
726 if ($action == 'delete' && !is_null($email)) {
727 Xdb
::execute('DELETE FROM email_watch WHERE email = {?}', $email);
730 if ($action != 'create' && $action != 'edit') {
733 $page->assign('action', $action);
735 if ($action == 'list') {
736 $sql = "SELECT w.email, w.detection, w.state, a.alias AS forlife
737 FROM email_watch AS w
738 LEFT JOIN emails AS e USING(email)
739 LEFT JOIN aliases AS a ON (a.uid = e.uid AND a.type = 'a_vie')
740 ORDER BY w.state, w.email, a.alias";
741 $it = Xdb
::iterRow($sql);
745 while (list($email, $date, $state, $forlife) = $it->next()) {
746 if (count($props) == 0 ||
$props['mail'] != $email) {
747 if (count($props) > 0) {
750 $props = array('mail' => $email,
751 'detection' => $date,
753 'users' => array($forlife));
755 $props['users'][] = $forlife;
758 if (count($props) > 0) {
761 $page->assign('table', $table);
762 } elseif ($action == 'edit') {
763 $sql = "SELECT w.detection, w.state, w.last, w.description,
764 a1.alias AS edit, a2.alias AS forlife
765 FROM email_watch AS w
766 LEFT JOIN aliases AS a1 ON (a1.uid = w.uid AND a1.type = 'a_vie')
767 LEFT JOIN emails AS e ON (w.email = e.email)
768 LEFT JOIN aliases AS a2 ON (a2.uid = e.uid AND a2.type = 'a_vie')
771 $it = Xdb
::iterRow($sql, $email);
774 while (list($detection, $state, $last, $description, $edit, $forlife) = $it->next()) {
775 if (count($props) == 0) {
776 $props = array('mail' => $email,
777 'detection' => $detection,
780 'description' => $description,
782 'users' => array($forlife));
784 $props['users'][] = $forlife;
787 $page->assign('doublon', $props);
791 function handler_lost(&$page, $action = 'list', $email = null
)
793 $page->changeTpl('emails/lost.tpl');
795 $page->assign('lost_emails',
796 XDB
::iterator('SELECT a.uid, a.hruid, pd.promo
798 INNER JOIN email_options AS eo ON (eo.uid = a.uid)
799 LEFT JOIN emails AS e ON (a.uid = e.uid AND FIND_IN_SET(\'active\', e.flags))
800 LEFT JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', perms))
801 LEFT JOIN profile_display AS pd ON (ap.pid = pd.pid)
802 WHERE e.uid IS NULL AND FIND_IN_SET(\'googleapps\', eo.storage) = 0
803 AND a.state = \'active\'
804 ORDER BY pd.promo, a.hruid'));
807 function handler_broken_addr(&$page)
809 require_once 'emails.inc.php';
810 $page->changeTpl('emails/broken_addr.tpl');
812 if (Env
::has('sort_broken')) {
813 S
::assert_xsrf_token();
815 $list = trim(Env
::v('list'));
817 $page->trigError('La liste est vide.');
819 $valid_emails = array();
820 $invalid_emails = array();
821 $broken_list = explode("\n", $list);
823 foreach ($broken_list as $orig_email) {
824 $orig_email = trim($orig_email);
825 if ($orig_email != '') {
826 $email = valide_email($orig_email);
827 if (empty($email) ||
$email == '@') {
828 $invalid_emails[] = trim($orig_email) . ': invalid email';
829 } elseif (!in_array($email, $valid_emails)) {
830 $res = XDB
::query('SELECT COUNT(*)
832 WHERE email = {?}', $email);
833 if ($res->fetchOneCell() > 0) {
834 $valid_emails[] = $email;
836 $invalid_emails[] = "$orig_email: no such redirection";
842 $page->assign('valid_emails', $valid_emails);
843 $page->assign('invalid_emails', $invalid_emails);
847 if (Env
::has('process_broken')) {
848 S
::assert_xsrf_token();
850 $list = trim(Env
::v('list'));
852 $page->trigError('La liste est vide.');
856 $broken_user_list = array();
857 $broken_list = explode("\n", $list);
859 foreach ($broken_list as $orig_email) {
860 $email = valide_email(trim($orig_email));
861 if (empty($email) ||
$email == '@') {
866 "SELECT e1.uid, e1.panne != 0 AS panne, count(e2.uid) AS nb_mails,
867 acc.full_name, a.alias
869 LEFT JOIN emails AS e2 ON (e1.uid = e2.uid AND FIND_IN_SET('active', e2.flags)
870 AND e1.email != e2.email)
871 INNER JOIN accounts AS acc ON (e1.uid = acc.uid)
872 INNER JOIN aliases AS a ON (acc.uid = a.uid AND FIND_IN_SET('bestalias', a.flags))
874 GROUP BY e1.uid", $email);
876 if ($x = $sel->fetchOneAssoc()) {
878 XDB
::execute('UPDATE emails
879 SET panne=NOW(), last=NOW(), panne_level = 1
883 XDB
::execute('UPDATE emails
884 SET last = CURDATE(), panne_level = panne_level + 1
886 AND DATE_ADD(last, INTERVAL 14 DAY) < CURDATE()',
890 if (!empty($x['nb_mails'])) {
891 $mail = new PlMailer('emails/broken.mail.tpl');
892 $mail->addTo("\"{$x['full_name']}\" <{$x['alias']}@"
893 . $globals->mail
->domain
. '>');
894 $mail->assign('x', $x);
895 $mail->assign('email', $email);
899 if (!isset($broken_user_list[$x['alias']])) {
900 $broken_user_list[$x['alias']] = array($email);
902 $broken_user_list[$x['alias']][] = $email;
907 XDB
::execute("UPDATE emails
908 SET panne_level = panne_level - 1
909 WHERE flags = 'active' AND panne_level > 1
910 AND DATE_ADD(last, INTERVAL 1 MONTH) < CURDATE()");
911 XDB
::execute("UPDATE emails
913 WHERE flags = 'active' AND panne_level = 1
914 AND DATE_ADD(last, INTERVAL 1 YEAR) < CURDATE()");
916 // Output the list of users with recently broken addresses,
917 // along with the count of valid redirections.
918 require_once 'notifs.inc.php';
919 pl_content_headers("text/x-csv");
921 $csv = fopen('php://output', 'w');
922 fputcsv($csv, array('nom', 'promo', 'alias', 'bounce', 'nbmails', 'url'), ';');
923 foreach ($broken_user_list as $alias => $mails) {
925 "SELECT acc.uid, count(e.email) AS nb_mails,
926 IFNULL(pd.public_name, acc.full_name) AS fullname,
927 IFNULL(pd.promo, 0) AS promo
929 INNER JOIN accounts AS acc ON (a.uid = acc.uid)
930 LEFT JOIN emails AS e ON (e.uid = acc.uid
931 AND FIND_IN_SET('active', e.flags) AND e.panne = 0)
932 LEFT JOIN account_profiles AS ap ON (acc.uid = ap.uid AND FIND_IN_SET('owner', ap.perms))
933 LEFT JOIN profile_display AS pd ON (pd.pid = ap.pid)
935 GROUP BY acc.uid", $alias);
937 if ($x = $sel->fetchOneAssoc()) {
938 if ($x['nb_mails'] == 0) {
939 $user = User
::getSilentWithUID($x['uid']);
940 $profile = $user->profile();
941 WatchProfileUpdate
::register($profile, 'broken');
943 fputcsv($csv, array($x['fullname'], $x['promo'], $alias,
944 join(',', $mails), $x['nb_mails'],
945 'https://www.polytechnique.org/marketing/broken/' . $alias), ';');
955 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: