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
),
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
),
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
)
109 require_once 'validations.inc.php';
113 $page->changeTpl('emails/alias.tpl');
114 $page->setTitle('Alias melix.net');
117 $page->assign('demande', AliasReq
::get_request($user->id()));
119 // Remove the email alias.
120 if ($action == 'delete' && $value) {
121 S
::assert_xsrf_token();
124 "DELETE virtual, virtual_redirect
126 INNER JOIN virtual_redirect USING (vid)
127 WHERE alias = {?} AND (redirect = {?} OR redirect = {?})",
128 $value, $user->forlifeEmail(), $user->m4xForlifeEmail());
131 // Fetch existing @alias_dom aliases.
132 $alias = $user->emailAlias();
133 $visibility = $user->hasProfile() && $user->profile()->alias_pub
;
135 if ($action == 'ask' && Env
::has('alias') && Env
::has('raison')) {
136 S
::assert_xsrf_token();
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';
143 $page->assign('r_alias', $alias);
144 $page->assign('r_raison', $raison);
145 if ($public == 'public') {
146 $page->assign('r_public', true
);
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 .");
157 $alias_mail = $alias.'@'.$globals->mail
->alias_dom
;
159 //vérifier que l'alias n'est pas déja pris
160 $res = XDB
::query('SELECT COUNT(*)
164 if ($res->fetchOneCell() > 0) {
165 $page->trigError("L'alias $alias_mail a déja été attribué.
166 Tu ne peux donc pas l'obtenir.");
170 //vérifier que l'alias n'est pas déja en demande
171 $it = new ValidateIterator();
172 while($req = $it->next()) {
173 if ($req->type
== 'alias' and $req->alias
== $alias_mail) {
174 $page->trigError("L'alias $alias_mail a déja été demandé.
175 Tu ne peux donc pas l'obtenir pour l'instant.");
180 //Insertion de la demande dans la base, écrase les requêtes précédente
181 $myalias = new AliasReq($user, $alias, $raison, $public);
183 $page->assign('success',$alias);
186 } elseif ($action == 'set' && ($value == 'public' ||
$value == 'private')) {
187 if (!S
::has_xsrf_token()) {
191 if ($user->hasProfile()) {
192 XDB
::execute("UPDATE profiles
195 $value, $user->profile()->id());
197 $visibility = ($value == 'public');
200 $page->assign('actuel', $alias);
201 $page->assign('user', $user);
202 $page->assign('mail_public', $visibility);
205 function handler_redirect(&$page, $action = null
, $email = null
)
209 require_once 'emails.inc.php';
211 $page->changeTpl('emails/redirect.tpl');
214 $page->assign_by_ref('user', $user);
215 $page->assign('eleve', $user->promo() >= date("Y") - 5);
217 $redirect = new Redirect($user);
219 // FS#703 : $_GET is urldecoded twice, hence
220 // + (the data) => %2B (in the url) => + (first decoding) => ' ' (second decoding)
221 // Since there can be no spaces in emails, we can fix this with :
222 $email = str_replace(' ', '+', $email);
224 // Apply email redirection change requests.
226 if ($action == 'remove' && $email) {
227 $result = $redirect->delete_email($email);
230 if ($action == 'active' && $email) {
231 $redirect->modify_one_email($email, true
);
234 if ($action == 'inactive' && $email) {
235 $redirect->modify_one_email($email, false
);
238 if ($action == 'rewrite' && $email) {
239 $rewrite = @func_get_arg
(3);
240 $redirect->modify_one_email_redirect($email, $rewrite);
243 if (Env
::has('emailop')) {
244 S
::assert_xsrf_token();
246 $actifs = Env
::v('emails_actifs', Array());
247 print_r(Env
::v('emails_rewrite'));
248 if (Env
::v('emailop') == "ajouter" && Env
::has('email')) {
249 $new_email = Env
::v('email');
250 if ($new_email == "new@example.org") {
251 $new_email = Env
::v('email_new');
253 $result = $redirect->add_email($new_email);
254 if ($result == ERROR_INVALID_EMAIL
) {
255 $page->assign('email', $new_email);
257 $page->assign('retour', $result);
258 } elseif (empty($actifs)) {
259 $result = ERROR_INACTIVE_REDIRECTION
;
260 } elseif (is_array($actifs)) {
261 $result = $redirect->modify_email($actifs, Env
::v('emails_rewrite', Array()));
266 case ERROR_INACTIVE_REDIRECTION
:
267 $page->trigError('Tu ne peux pas avoir aucune adresse de redirection active, sinon ton adresse '
268 . $user->forlifeEmail() . ' ne fonctionnerait plus.');
270 case ERROR_INVALID_EMAIL
:
271 $page->trigError('Erreur: l\'email n\'est pas valide.');
273 case ERROR_LOOP_EMAIL
:
274 $page->trigError('Erreur : ' . $user->forlifeEmail()
275 . ' ne doit pas être renvoyé vers lui-même, ni vers son équivalent en '
276 . $globals->mail
->domain2
. ' ni vers polytechnique.edu.');
280 // Fetch the @alias_dom email alias, if any.
284 INNER JOIN virtual_redirect USING(vid)
285 WHERE (redirect={?} OR redirect={?})
286 AND alias LIKE '%@{$globals->mail->alias_dom}'",
287 $user->forlifeEmail(),
288 // TODO: remove this über-ugly hack. The issue is that you need
289 // to remove all @m4x.org addresses in virtual_redirect first.
290 $user->login() . '@' . $globals->mail
->domain2
);
291 $melix = $res->fetchOneCell();
293 list($melix) = explode('@', $melix);
294 $page->assign('melix',$melix);
297 // Fetch existing email aliases.
301 WHERE uid={?} AND (type='a_vie' OR type='alias')
302 ORDER BY !FIND_IN_SET('usage',flags), LENGTH(alias)", $user->id());
303 $page->assign('alias', $res->fetchAllAssoc());
304 $page->assign('emails', $redirect->emails
);
306 // Display GoogleApps acount information.
307 require_once 'googleapps.inc.php';
308 $page->assign('googleapps', GoogleAppsAccount
::account_status($user->id()));
310 require_once 'emails.combobox.inc.php';
311 fill_email_combobox($page);
314 function handler_antispam(&$page, $statut_filtre = null
)
316 require_once 'emails.inc.php';
317 $wp = new PlWikiPage('Xorg.Antispam');
320 $page->changeTpl('emails/antispam.tpl');
323 $bogo = new Bogo($user);
324 if (isset($statut_filtre)) {
325 $bogo->change($statut_filtre +
0);
327 $page->assign('filtre', $bogo->level());
330 function handler_submit(&$page)
332 $wp = new PlWikiPage('Xorg.Mails');
334 $page->changeTpl('emails/submit_spam.tpl');
336 if (Post
::has('send_email')) {
337 S
::assert_xsrf_token();
339 $upload = PlUpload
::get($_FILES['mail'], S
::user()->login(), 'spam.submit', true
);
341 $page->trigError('Une erreur a été rencontrée lors du transfert du fichier');
344 $mime = $upload->contentType();
345 if ($mime != 'text/x-mail' && $mime != 'message/rfc822') {
347 $page->trigError('Le fichier ne contient pas un email complet');
350 $type = (Post
::v('type') == 'spam' ?
'spam' : 'nonspam');
353 $box = $type . '@' . $globals->mail
->domain
;
354 $mailer = new PlMailer();
355 $mailer->addTo($box);
356 $mailer->setFrom('"' . S
::user()->fullName() . '" <web@' . $globals->mail
->domain
. '>');
357 $mailer->setTxtBody($type . ' soumis par ' . S
::user()->login() . ' via le web');
358 $mailer->addUploadAttachment($upload, $type . '.mail');
360 $page->trigSuccess('Le message a été transmis à ' . $box);
365 function handler_send(&$page)
367 $page->changeTpl('emails/send.tpl');
368 $page->addJsLink('ajax.js');
370 $page->setTitle('Envoyer un email');
372 // action si on recoit un formulaire
373 if (Post
::has('save')) {
374 if (!S
::has_xsrf_token()) {
378 unset($_POST['save']);
379 if (trim(preg_replace('/-- .*/', '', Post
::v('contenu'))) != "") {
380 $_POST['to_contacts'] = explode(';', @$_POST['to_contacts']);
381 $_POST['cc_contacts'] = explode(';', @$_POST['cc_contacts']);
382 $data = serialize($_POST);
383 XDB
::execute("REPLACE INTO email_send_save
385 S
::user()->id('uid'), $data);
388 } else if (Env
::v('submit') == 'Envoyer') {
389 S
::assert_xsrf_token();
391 function getEmails($aliases)
393 if (!is_array($aliases)) {
396 $uf = new UserFilter(new UFC_Hrpid($aliases));
397 $users = $uf->iterUsers();
399 while ($user = $users->next()) {
400 $ret[] = $user->forlife
;
402 return join(', ', $ret);
406 foreach ($_FILES as &$file) {
407 if ($file['name'] && !PlUpload
::get($file, S
::user()->login(), 'emails.send', false
)) {
408 $page->trigError(PlUpload
::$lastError);
415 XDB
::execute("DELETE FROM email_send_save
419 $to2 = getEmails(Env
::v('to_contacts'));
420 $cc2 = getEmails(Env
::v('cc_contacts'));
421 $txt = str_replace('^M', '', Env
::v('contenu'));
422 $to = str_replace(';', ',', Env
::t('to'));
423 $subj = Env
::t('sujet');
424 $from = Env
::t('from');
425 $cc = str_replace(';', ',', Env
::t('cc'));
426 $bcc = str_replace(';', ',', Env
::t('bcc'));
428 $email_regex = '/^[a-z0-9.\-+_\$]+@([\-.+_]?[a-z0-9])+$/i';
429 foreach (explode(',', $to . ',' . $cc . ',' . $bcc) as $email) {
430 $email = trim($email);
431 if ($email != '' && !preg_match($email_regex, $email)) {
432 $page->trigError("L'adresse email " . $email . ' est erronée.');
436 if (empty($to) && empty($cc) && empty($to2) && empty($bcc) && empty($cc2)) {
437 $page->trigError("Indique au moins un destinataire.");
442 $page->assign('uploaded_f', PlUpload
::listFilenames(S
::user()->login(), 'emails.send'));
444 $mymail = new PlMailer();
445 $mymail->setFrom($from);
446 $mymail->setSubject($subj);
447 if (!empty($to)) { $mymail->addTo($to); }
448 if (!empty($cc)) { $mymail->addCc($cc); }
449 if (!empty($bcc)) { $mymail->addBcc($bcc); }
450 if (!empty($to2)) { $mymail->addTo($to2); }
451 if (!empty($cc2)) { $mymail->addCc($cc2); }
452 $files =& PlUpload
::listFiles(S
::user()->login(), 'emails.send');
453 foreach ($files as $name=>&$upload) {
454 $mymail->addUploadAttachment($upload, $name);
456 if (Env
::v('nowiki')) {
457 $mymail->setTxtBody(wordwrap($txt, 78, "\n"));
459 $mymail->setWikiBody($txt);
461 if ($mymail->send()) {
462 $page->trigSuccess("Ton email a bien été envoyé.");
463 $_REQUEST = array('bcc' => S
::user()->bestEmail());
464 PlUpload
::clear(S
::user()->login(), 'emails.send');
466 $page->trigError("Erreur lors de l'envoi du courriel, réessaye.");
467 $page->assign('uploaded_f', PlUpload
::listFilenames(S
::user()->login(), 'emails.send'));
472 $res = XDB
::query("SELECT data
474 WHERE uid = {?}", S
::i('uid'));
475 if ($res->numRows() == 0) {
476 PlUpload
::clear(S
::user()->login(), 'emails.send');
477 $_REQUEST['bcc'] = S
::user()->bestEmail();
479 $data = unserialize($res->fetchOneCell());
480 $_REQUEST = array_merge($_REQUEST, $data);
484 $uf = new UserFilter(new PFC_And(new UFC_Contact(S
::user()),
485 new UFC_Registered()),
486 UserFilter
::sortByName());
487 $contacts = $uf->getProfiles();
488 $page->assign('contacts', $contacts);
489 $page->assign('maxsize', ini_get('upload_max_filesize') . 'o');
490 $page->assign('user', S
::user());
493 function handler_test(&$page, $hruid = null
)
495 require_once 'emails.inc.php';
497 if (!S
::has_xsrf_token()) {
501 // Retrieves the User object for the test email recipient.
502 if (S
::admin() && $hruid) {
503 $user = User
::getSilent($hruid);
511 // Sends the test email.
512 $redirect = new Redirect($user);
514 $mailer = new PlMailer('emails/test.mail.tpl');
515 $mailer->assign('email', $user->bestEmail());
516 $mailer->assign('redirects', $redirect->active_emails());
517 $mailer->assign('display_name', $user->displayName());
518 $mailer->assign('sexe', $user->isFemale());
519 $mailer->send($user->isEmailFormatHtml());
523 function handler_rewrite_in(&$page, $mail, $hash)
525 $page->changeTpl('emails/rewrite.tpl');
526 $page->assign('option', 'in');
527 if (empty($mail) ||
empty($hash)) {
530 $pos = strrpos($mail, '_');
531 if ($pos === false
) {
535 $res = XDB
::query("SELECT COUNT(*)
537 WHERE email = {?} AND hash = {?}",
539 $count = intval($res->fetchOneCell());
541 XDB
::query("UPDATE emails
542 SET allow_rewrite = true, hash = NULL
543 WHERE email = {?} AND hash = {?}",
545 $page->trigSuccess("Réécriture activée pour l'adresse " . $mail);
551 function handler_rewrite_out(&$page, $mail, $hash)
553 $page->changeTpl('emails/rewrite.tpl');
554 $page->assign('option', 'out');
555 if (empty($mail) ||
empty($hash)) {
558 $pos = strrpos($mail, '_');
559 if ($pos === false
) {
563 $res = XDB
::query("SELECT COUNT(*)
565 WHERE email = {?} AND hash = {?}",
567 $count = intval($res->fetchOneCell());
570 $res = XDB
::query("SELECT e.email, e.rewrite, a.alias
572 INNER JOIN aliases AS a ON (a.uid = e.uid AND a.type = 'a_vie')
573 WHERE e.email = {?} AND e.hash = {?}",
575 XDB
::query("UPDATE emails
576 SET allow_rewrite = false, hash = NULL
577 WHERE email = {?} AND hash = {?}",
579 list($mail, $rewrite, $forlife) = $res->fetchOneRow();
580 $mail = new PlMailer();
581 $mail->setFrom("webmaster@" . $globals->mail
->domain
);
582 $mail->addTo("support@" . $globals->mail
->domain
);
583 $mail->setSubject("Tentative de détournement de correspondance via le rewrite");
584 $mail->setTxtBody("$forlife a tenté un rewrite de $mail vers $rewrite. Cette demande a été rejetée via le web");
586 $page->trigWarning("Un mail d'alerte a été envoyé à l'équipe de " . $globals->core
->sitename
);
592 function handler_imap_in(&$page, $hash = null
, $login = null
)
594 $page->changeTpl('emails/imap_register.tpl');
596 if (!empty($hash) ||
!empty($login)) {
597 $user = User
::getSilent($login);
599 $req = XDB
::query('SELECT 1
601 WHERE uid = {?} AND hash = {?}',
603 if ($req->numRows() == 0) {
609 require_once('emails.inc.php');
610 $page->assign('ok', false
);
611 if (S
::logged() && (is_null($user) ||
$user->id() == S
::i('uid'))) {
612 $storage = new EmailStorage(S
::user(), 'imap');
613 $storage->activate();
614 $page->assign('ok', true
);
615 $page->assign('prenom', S
::v('prenom'));
616 $page->assign('sexe', S
::v('femme'));
617 } else if (!S
::logged() && $user) {
618 $storage = new EmailStorage($user, 'imap');
619 $storage->activate();
620 $page->assign('ok', true
);
621 $page->assign('prenom', $user->displayName());
622 $page->assign('sexe', $user->isFemale());
626 function handler_broken(&$page, $warn = null
, $email = null
)
628 require_once 'emails.inc.php';
629 $wp = new PlWikiPage('Xorg.PatteCassée');
634 $page->changeTpl('emails/broken.tpl');
636 if ($warn == 'warn' && $email) {
637 S
::assert_xsrf_token();
639 $email = valide_email($email);
640 // vérifications d'usage
641 $uid = XDB
::fetchOneCell("SELECT uid
643 WHERE email = {?}", $email);
645 $dest = User
::getWithUID($uid);
647 $mail = new PlMailer('emails/broken-web.mail.tpl');
648 $mail->assign('email', $email);
649 $mail->assign('request', S
::user());
650 $mail->sendTo($dest);
651 $page->trigSuccess('Email envoyé !');
653 } elseif (Post
::has('email')) {
654 S
::assert_xsrf_token();
656 $email = valide_email(Post
::v('email'));
658 list(,$fqdn) = explode('@', $email);
659 $fqdn = strtolower($fqdn);
660 if ($fqdn == 'polytechnique.org' ||
$fqdn == 'melix.org' ||
$fqdn == 'm4x.org' ||
$fqdn == 'melix.net') {
661 $page->assign('neuneu', true
);
663 $page->assign('email',$email);
664 $x = XDB
::fetchOneAssoc("SELECT e1.uid, e1.panne != 0 AS panne,
665 (count(e2.uid) + IF(FIND_IN_SET('googleapps', eo.storage), 1, 0)) AS nb_mails
667 INNER JOIN email_options AS eo ON (eo.uid = e1.uid)
668 LEFT JOIN emails as e2 ON(e1.uid = e2.uid
669 AND FIND_IN_SET('active', e2.flags)
670 AND e1.email != e2.email)
672 GROUP BY e1.uid", $email);
674 // on écrit dans la base que l'adresse est cassée
676 XDB
::execute("UPDATE emails
677 SET panne=NOW(), last=NOW(), panne_level = 1
678 WHERE email = {?}", $email);
680 XDB
::execute("UPDATE emails
682 WHERE email = {?} AND panne_level = 0", $email);
684 $x['user'] = User
::getWithUID($x['uid']);
685 $page->assign_by_ref('x', $x);
691 function handler_duplicated(&$page, $action = 'list', $email = null
)
693 $page->changeTpl('emails/duplicated.tpl');
695 $states = array('pending' => 'En attente...',
696 'safe' => 'Pas d\'inquiétude',
697 'unsafe' => 'Recherches en cours',
698 'dangerous' => 'Usurpations par cette adresse');
699 $page->assign('states', $states);
701 if (Post
::has('action')) {
702 S
::assert_xsrf_token();
704 switch (Post
::v('action')) {
706 if (trim(Post
::v('emailN')) != '') {
707 Xdb
::execute('INSERT IGNORE INTO email_watch (email, state, detection, last, uid, description)
708 VALUES ({?}, {?}, CURDATE(), NOW(), {?}, {?})',
709 trim(Post
::v('emailN')), Post
::v('stateN'), S
::i('uid'), Post
::v('descriptionN'));
714 Xdb
::execute('UPDATE email_watch
715 SET state = {?}, last = NOW(), uid = {?}, description = {?}
716 WHERE email = {?}', Post
::v('stateN'), S
::i('uid'), Post
::v('descriptionN'), Post
::v('emailN'));
720 if ($action == 'delete' && !is_null($email)) {
721 Xdb
::execute('DELETE FROM email_watch WHERE email = {?}', $email);
724 if ($action != 'create' && $action != 'edit') {
727 $page->assign('action', $action);
729 if ($action == 'list') {
730 $sql = "SELECT w.email, w.detection, w.state, a.alias AS forlife
731 FROM email_watch AS w
732 LEFT JOIN emails AS e USING(email)
733 LEFT JOIN aliases AS a ON (a.uid = e.uid AND a.type = 'a_vie')
734 ORDER BY w.state, w.email, a.alias";
735 $it = Xdb
::iterRow($sql);
739 while (list($email, $date, $state, $forlife) = $it->next()) {
740 if (count($props) == 0 ||
$props['mail'] != $email) {
741 if (count($props) > 0) {
744 $props = array('mail' => $email,
745 'detection' => $date,
747 'users' => array($forlife));
749 $props['users'][] = $forlife;
752 if (count($props) > 0) {
755 $page->assign('table', $table);
756 } elseif ($action == 'edit') {
757 $sql = "SELECT w.detection, w.state, w.last, w.description,
758 a1.alias AS edit, a2.alias AS forlife
759 FROM email_watch AS w
760 LEFT JOIN aliases AS a1 ON (a1.uid = w.uid AND a1.type = 'a_vie')
761 LEFT JOIN emails AS e ON (w.email = e.email)
762 LEFT JOIN aliases AS a2 ON (a2.uid = e.uid AND a2.type = 'a_vie')
765 $it = Xdb
::iterRow($sql, $email);
768 while (list($detection, $state, $last, $description, $edit, $forlife) = $it->next()) {
769 if (count($props) == 0) {
770 $props = array('mail' => $email,
771 'detection' => $detection,
774 'description' => $description,
776 'users' => array($forlife));
778 $props['users'][] = $forlife;
781 $page->assign('doublon', $props);
785 function handler_lost(&$page, $action = 'list', $email = null
)
787 $page->changeTpl('emails/lost.tpl');
789 // TODO: Order by promo.
790 $page->assign('lost_emails',
791 XDB
::iterator("SELECT a.uid, a.hruid
793 INNER JOIN email_options AS eo ON (eo.uid = a.uid)
794 LEFT JOIN emails AS e ON (a.uid = e.uid AND FIND_IN_SET('active', e.flags))
795 WHERE e.uid IS NULL AND FIND_IN_SET('googleapps', eo.storage) = 0 AND
800 function handler_broken_addr(&$page)
802 require_once 'emails.inc.php';
803 $page->changeTpl('emails/broken_addr.tpl');
805 if (Env
::has('sort_broken')) {
806 S
::assert_xsrf_token();
808 $list = trim(Env
::v('list'));
810 $page->trigError('La liste est vide.');
812 $valid_emails = array();
813 $invalid_emails = array();
814 $broken_list = explode("\n", $list);
816 foreach ($broken_list as $orig_email) {
817 $orig_email = trim($orig_email);
818 if ($orig_email != '') {
819 $email = valide_email($orig_email);
820 if (empty($email) ||
$email == '@') {
821 $invalid_emails[] = trim($orig_email) . ': invalid email';
822 } elseif (!in_array($email, $valid_emails)) {
823 $res = XDB
::query('SELECT COUNT(*)
825 WHERE email = {?}', $email);
826 if ($res->fetchOneCell() > 0) {
827 $valid_emails[] = $email;
829 $invalid_emails[] = "$orig_email: no such redirection";
835 $page->assign('valid_emails', $valid_emails);
836 $page->assign('invalid_emails', $invalid_emails);
840 if (Env
::has('process_broken')) {
841 S
::assert_xsrf_token();
843 $list = trim(Env
::v('list'));
845 $page->trigError('La liste est vide.');
849 $broken_user_list = array();
850 $broken_list = explode("\n", $list);
852 foreach ($broken_list as $orig_email) {
853 $email = valide_email(trim($orig_email));
854 if (empty($email) ||
$email == '@') {
859 "SELECT e1.uid, e1.panne != 0 AS panne, count(e2.uid) AS nb_mails,
860 acc.full_name, a.alias
862 LEFT JOIN emails AS e2 ON (e1.uid = e2.uid AND FIND_IN_SET('active', e2.flags)
863 AND e1.email != e2.email)
864 INNER JOIN accounts AS acc ON (e1.uid = acc.uid)
865 INNER JOIN aliases AS a ON (acc.uid = a.uid AND FIND_IN_SET('bestalias', a.flags))
867 GROUP BY e1.uid", $email);
869 if ($x = $sel->fetchOneAssoc()) {
871 XDB
::execute('UPDATE emails
872 SET panne=NOW(), last=NOW(), panne_level = 1
876 XDB
::execute('UPDATE emails
877 SET last = CURDATE(), panne_level = panne_level + 1
879 AND DATE_ADD(last, INTERVAL 14 DAY) < CURDATE()',
883 if (!empty($x['nb_mails'])) {
884 $mail = new PlMailer('emails/broken.mail.tpl');
885 $mail->addTo("\"{$x['full_name']}\" <{$x['alias']}@"
886 . $globals->mail
->domain
. '>');
887 $mail->assign('x', $x);
888 $mail->assign('email', $email);
892 if (!isset($broken_user_list[$x['alias']])) {
893 $broken_user_list[$x['alias']] = array($email);
895 $broken_user_list[$x['alias']][] = $email;
900 XDB
::execute("UPDATE emails
901 SET panne_level = panne_level - 1
902 WHERE flags = 'active' AND panne_level > 1
903 AND DATE_ADD(last, INTERVAL 1 MONTH) < CURDATE()");
904 XDB
::execute("UPDATE emails
906 WHERE flags = 'active' AND panne_level = 1
907 AND DATE_ADD(last, INTERVAL 1 YEAR) < CURDATE()");
909 // Output the list of users with recently broken addresses,
910 // along with the count of valid redirections.
911 require_once 'notifs.inc.php';
912 pl_content_headers("text/x-csv");
914 $csv = fopen('php://output', 'w');
915 fputcsv($csv, array('nom', 'promo', 'alias', 'bounce', 'nbmails', 'url'), ';');
916 foreach ($broken_user_list as $alias => $mails) {
918 "SELECT acc.uid, count(e.email) AS nb_mails,
919 IFNULL(pd.public_name, acc.full_name) AS fullname,
920 IFNULL(pd.promo, 0) AS promo,
922 INNER JOIN accounts AS acc ON a.id = acc.uid
923 LEFT JOIN emails AS e ON (e.uid = acc.uid
924 AND FIND_IN_SET('active', e.flags) AND e.panne = 0)
925 LEFT JOIN account_profiles AS ap ON (acc.uid = ap.uid AND FIND_IN_SET('owner', ap.perms))
926 LEFT JOIN profile_display AS pd ON (pd.pid = ap.pid)
928 GROUP BY acc.uid", $alias);
930 if ($x = $sel->fetchOneAssoc()) {
931 if ($x['nb_mails'] == 0) {
932 register_profile_update($x['uid'], 'broken');
934 fputcsv($csv, array($x['fullname'], $x['promo'], $alias,
935 join(',', $mails), $x['nb_mails'],
936 'https://www.polytechnique.org/marketing/broken/' . $alias), ';');
946 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: