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 $rel = Env
::v('contacts');
398 foreach ($aliases as $alias) {
399 $ret[$alias] = $rel[$alias];
401 return join(', ', $ret);
405 foreach ($_FILES as &$file) {
406 if ($file['name'] && !PlUpload
::get($file, S
::user()->login(), 'emails.send', false
)) {
407 $page->trigError(PlUpload
::$lastError);
414 XDB
::execute("DELETE FROM email_send_save
418 $to2 = getEmails(Env
::v('to_contacts'));
419 $cc2 = getEmails(Env
::v('cc_contacts'));
420 $txt = str_replace('^M', '', Env
::v('contenu'));
421 $to = str_replace(';', ',', Env
::t('to'));
422 $subj = Env
::t('sujet');
423 $from = Env
::t('from');
424 $cc = str_replace(';', ',', Env
::t('cc'));
425 $bcc = str_replace(';', ',', Env
::t('bcc'));
427 $email_regex = '/^[a-z0-9.\-+_\$]+@([\-.+_]?[a-z0-9])+$/i';
428 foreach (explode(',', $to . ',' . $cc . ',' . $bcc) as $email) {
429 $email = trim($email);
430 if ($email != '' && !preg_match($email_regex, $email)) {
431 $page->trigError("L'adresse email " . $email . ' est erronée.');
435 if (empty($to) && empty($cc) && empty($to2) && empty($bcc) && empty($cc2)) {
436 $page->trigError("Indique au moins un destinataire.");
441 $page->assign('uploaded_f', PlUpload
::listFilenames(S
::user()->login(), 'emails.send'));
443 $mymail = new PlMailer();
444 $mymail->setFrom($from);
445 $mymail->setSubject($subj);
446 if (!empty($to)) { $mymail->addTo($to); }
447 if (!empty($cc)) { $mymail->addCc($cc); }
448 if (!empty($bcc)) { $mymail->addBcc($bcc); }
449 if (!empty($to2)) { $mymail->addTo($to2); }
450 if (!empty($cc2)) { $mymail->addCc($cc2); }
451 $files =& PlUpload
::listFiles(S
::user()->login(), 'emails.send');
452 foreach ($files as $name=>&$upload) {
453 $mymail->addUploadAttachment($upload, $name);
455 if (Env
::v('nowiki')) {
456 $mymail->setTxtBody(wordwrap($txt, 78, "\n"));
458 $mymail->setWikiBody($txt);
460 if ($mymail->send()) {
461 $page->trigSuccess("Ton email a bien été envoyé.");
462 $_REQUEST = array('bcc' => S
::user()->bestEmail());
463 PlUpload
::clear(S
::user()->login(), 'emails.send');
465 $page->trigError("Erreur lors de l'envoi du courriel, réessaye.");
466 $page->assign('uploaded_f', PlUpload
::listFilenames(S
::user()->login(), 'emails.send'));
471 $res = XDB
::query("SELECT data
473 WHERE uid = {?}", S
::i('uid'));
474 if ($res->numRows() == 0) {
475 PlUpload
::clear(S
::user()->login(), 'emails.send');
476 $_REQUEST['bcc'] = S
::user()->bestEmail();
478 $data = unserialize($res->fetchOneCell());
479 $_REQUEST = array_merge($_REQUEST, $data);
483 $contacts = S
::user()->getContacts();
484 $page->assign('contacts', $contacts);
485 $page->assign('maxsize', ini_get('upload_max_filesize') . 'o');
486 $page->assign('user', S
::user());
489 function handler_test(&$page, $hruid = null
)
491 require_once 'emails.inc.php';
493 if (!S
::has_xsrf_token()) {
497 // Retrieves the User object for the test email recipient.
498 if (S
::admin() && $hruid) {
499 $user = User
::getSilent($hruid);
507 // Sends the test email.
508 $redirect = new Redirect($user);
510 $mailer = new PlMailer('emails/test.mail.tpl');
511 $mailer->assign('email', $user->bestEmail());
512 $mailer->assign('redirects', $redirect->active_emails());
513 $mailer->assign('display_name', $user->displayName());
514 $mailer->assign('sexe', $user->isFemale());
515 $mailer->send($user->isEmailFormatHtml());
519 function handler_rewrite_in(&$page, $mail, $hash)
521 $page->changeTpl('emails/rewrite.tpl');
522 $page->assign('option', 'in');
523 if (empty($mail) ||
empty($hash)) {
526 $pos = strrpos($mail, '_');
527 if ($pos === false
) {
531 $res = XDB
::query("SELECT COUNT(*)
533 WHERE email = {?} AND hash = {?}",
535 $count = intval($res->fetchOneCell());
537 XDB
::query("UPDATE emails
538 SET allow_rewrite = true, hash = NULL
539 WHERE email = {?} AND hash = {?}",
541 $page->trigSuccess("Réécriture activée pour l'adresse " . $mail);
547 function handler_rewrite_out(&$page, $mail, $hash)
549 $page->changeTpl('emails/rewrite.tpl');
550 $page->assign('option', 'out');
551 if (empty($mail) ||
empty($hash)) {
554 $pos = strrpos($mail, '_');
555 if ($pos === false
) {
559 $res = XDB
::query("SELECT COUNT(*)
561 WHERE email = {?} AND hash = {?}",
563 $count = intval($res->fetchOneCell());
566 $res = XDB
::query("SELECT e.email, e.rewrite, a.alias
568 INNER JOIN aliases AS a ON (a.uid = e.uid AND a.type = 'a_vie')
569 WHERE e.email = {?} AND e.hash = {?}",
571 XDB
::query("UPDATE emails
572 SET allow_rewrite = false, hash = NULL
573 WHERE email = {?} AND hash = {?}",
575 list($mail, $rewrite, $forlife) = $res->fetchOneRow();
576 $mail = new PlMailer();
577 $mail->setFrom("webmaster@" . $globals->mail
->domain
);
578 $mail->addTo("support@" . $globals->mail
->domain
);
579 $mail->setSubject("Tentative de détournement de correspondance via le rewrite");
580 $mail->setTxtBody("$forlife a tenté un rewrite de $mail vers $rewrite. Cette demande a été rejetée via le web");
582 $page->trigWarning("Un mail d'alerte a été envoyé à l'équipe de " . $globals->core
->sitename
);
588 function handler_imap_in(&$page, $hash = null
, $login = null
)
590 $page->changeTpl('emails/imap_register.tpl');
592 if (!empty($hash) ||
!empty($login)) {
593 $user = User
::getSilent($login);
595 $req = XDB
::query("SELECT 1 FROM newsletter_ins WHERE user_id = {?} AND hash = {?}", $user->id(), $hash);
596 if ($req->numRows() == 0) {
602 require_once('emails.inc.php');
603 $page->assign('ok', false
);
604 if (S
::logged() && (is_null($user) ||
$user->id() == S
::i('uid'))) {
605 $storage = new EmailStorage(S
::user(), 'imap');
606 $storage->activate();
607 $page->assign('ok', true
);
608 $page->assign('prenom', S
::v('prenom'));
609 $page->assign('sexe', S
::v('femme'));
610 } else if (!S
::logged() && $user) {
611 $storage = new EmailStorage($user, 'imap');
612 $storage->activate();
613 $page->assign('ok', true
);
614 $page->assign('prenom', $user->displayName());
615 $page->assign('sexe', $user->isFemale());
619 function handler_broken(&$page, $warn = null
, $email = null
)
621 require_once 'emails.inc.php';
622 $wp = new PlWikiPage('Xorg.PatteCassée');
627 $page->changeTpl('emails/broken.tpl');
629 if ($warn == 'warn' && $email) {
630 S
::assert_xsrf_token();
632 $email = valide_email($email);
633 // vérifications d'usage
634 $uid = XDB
::fetchOneCell("SELECT uid
636 WHERE email = {?}", $email);
638 $dest = User
::getWithUID($uid);
640 $mail = new PlMailer('emails/broken-web.mail.tpl');
641 $mail->assign('email', $email);
642 $mail->assign('request', S
::user());
643 $mail->sendTo($dest);
644 $page->trigSuccess('Email envoyé !');
646 } elseif (Post
::has('email')) {
647 S
::assert_xsrf_token();
649 $email = valide_email(Post
::v('email'));
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
);
656 $page->assign('email',$email);
657 $x = XDB
::fetchOneAssoc("SELECT e1.uid, e1.panne != 0 AS panne,
658 (count(e2.uid) + IF(FIND_IN_SET('googleapps', eo.storage), 1, 0)) AS nb_mails
660 INNER JOIN email_options AS eo ON (eo.uid = e1.uid)
661 LEFT JOIN emails as e2 ON(e1.uid = e2.uid
662 AND FIND_IN_SET('active', e2.flags)
663 AND e1.email != e2.email)
665 GROUP BY e1.uid", $email);
667 // on écrit dans la base que l'adresse est cassée
669 XDB
::execute("UPDATE emails
670 SET panne=NOW(), last=NOW(), panne_level = 1
671 WHERE email = {?}", $email);
673 XDB
::execute("UPDATE emails
675 WHERE email = {?} AND panne_level = 0", $email);
677 $x['user'] = User
::getWithUID($x['uid']);
678 $page->assign_by_ref('x', $x);
684 function handler_duplicated(&$page, $action = 'list', $email = null
)
686 $page->changeTpl('emails/duplicated.tpl');
688 $states = array('pending' => 'En attente...',
689 'safe' => 'Pas d\'inquiétude',
690 'unsafe' => 'Recherches en cours',
691 'dangerous' => 'Usurpations par cette adresse');
692 $page->assign('states', $states);
694 if (Post
::has('action')) {
695 S
::assert_xsrf_token();
697 switch (Post
::v('action')) {
699 if (trim(Post
::v('emailN')) != '') {
700 Xdb
::execute('INSERT IGNORE INTO email_watch (email, state, detection, last, uid, description)
701 VALUES ({?}, {?}, CURDATE(), NOW(), {?}, {?})',
702 trim(Post
::v('emailN')), Post
::v('stateN'), S
::i('uid'), Post
::v('descriptionN'));
707 Xdb
::execute('UPDATE email_watch
708 SET state = {?}, last = NOW(), uid = {?}, description = {?}
709 WHERE email = {?}', Post
::v('stateN'), S
::i('uid'), Post
::v('descriptionN'), Post
::v('emailN'));
713 if ($action == 'delete' && !is_null($email)) {
714 Xdb
::execute('DELETE FROM email_watch WHERE email = {?}', $email);
717 if ($action != 'create' && $action != 'edit') {
720 $page->assign('action', $action);
722 if ($action == 'list') {
723 $sql = "SELECT w.email, w.detection, w.state, a.alias AS forlife
724 FROM email_watch AS w
725 LEFT JOIN emails AS e USING(email)
726 LEFT JOIN aliases AS a ON (a.uid = e.uid AND a.type = 'a_vie')
727 ORDER BY w.state, w.email, a.alias";
728 $it = Xdb
::iterRow($sql);
732 while (list($email, $date, $state, $forlife) = $it->next()) {
733 if (count($props) == 0 ||
$props['mail'] != $email) {
734 if (count($props) > 0) {
737 $props = array('mail' => $email,
738 'detection' => $date,
740 'users' => array($forlife));
742 $props['users'][] = $forlife;
745 if (count($props) > 0) {
748 $page->assign('table', $table);
749 } elseif ($action == 'edit') {
750 $sql = "SELECT w.detection, w.state, w.last, w.description,
751 a1.alias AS edit, a2.alias AS forlife
752 FROM email_watch AS w
753 LEFT JOIN aliases AS a1 ON (a1.uid = w.uid AND a1.type = 'a_vie')
754 LEFT JOIN emails AS e ON (w.email = e.email)
755 LEFT JOIN aliases AS a2 ON (a2.uid = e.uid AND a2.type = 'a_vie')
758 $it = Xdb
::iterRow($sql, $email);
761 while (list($detection, $state, $last, $description, $edit, $forlife) = $it->next()) {
762 if (count($props) == 0) {
763 $props = array('mail' => $email,
764 'detection' => $detection,
767 'description' => $description,
769 'users' => array($forlife));
771 $props['users'][] = $forlife;
774 $page->assign('doublon', $props);
778 function handler_lost(&$page, $action = 'list', $email = null
)
780 $page->changeTpl('emails/lost.tpl');
782 // TODO: Order by promo.
783 $page->assign('lost_emails',
784 XDB
::iterator("SELECT a.uid, a.hruid
786 INNER JOIN email_options AS eo ON (eo.uid = a.uid)
787 LEFT JOIN emails AS e ON (a.uid = e.uid AND FIND_IN_SET('active', e.flags))
788 WHERE e.uid IS NULL AND FIND_IN_SET('googleapps', eo.storage) = 0 AND
793 function handler_broken_addr(&$page)
795 require_once 'emails.inc.php';
796 $page->changeTpl('emails/broken_addr.tpl');
798 if (Env
::has('sort_broken')) {
799 S
::assert_xsrf_token();
801 $list = trim(Env
::v('list'));
803 $page->trigError('La liste est vide.');
805 $valid_emails = array();
806 $invalid_emails = array();
807 $broken_list = explode("\n", $list);
809 foreach ($broken_list as $orig_email) {
810 $orig_email = trim($orig_email);
811 if ($orig_email != '') {
812 $email = valide_email($orig_email);
813 if (empty($email) ||
$email == '@') {
814 $invalid_emails[] = trim($orig_email) . ': invalid email';
815 } elseif (!in_array($email, $valid_emails)) {
816 $res = XDB
::query('SELECT COUNT(*)
818 WHERE email = {?}', $email);
819 if ($res->fetchOneCell() > 0) {
820 $valid_emails[] = $email;
822 $invalid_emails[] = "$orig_email: no such redirection";
828 $page->assign('valid_emails', $valid_emails);
829 $page->assign('invalid_emails', $invalid_emails);
833 if (Env
::has('process_broken')) {
834 S
::assert_xsrf_token();
836 $list = trim(Env
::v('list'));
838 $page->trigError('La liste est vide.');
842 $broken_user_list = array();
843 $broken_list = explode("\n", $list);
845 foreach ($broken_list as $orig_email) {
846 $email = valide_email(trim($orig_email));
847 if (empty($email) ||
$email == '@') {
852 "SELECT e1.uid, e1.panne != 0 AS panne, count(e2.uid) AS nb_mails,
853 acc.full_name, a.alias
855 LEFT JOIN emails AS e2 ON (e1.uid = e2.uid AND FIND_IN_SET('active', e2.flags)
856 AND e1.email != e2.email)
857 INNER JOIN accounts AS acc ON (e1.uid = acc.uid)
858 INNER JOIN aliases AS a ON (acc.uid = a.uid AND FIND_IN_SET('bestalias', a.flags))
860 GROUP BY e1.uid", $email);
862 if ($x = $sel->fetchOneAssoc()) {
864 XDB
::execute('UPDATE emails
865 SET panne=NOW(), last=NOW(), panne_level = 1
869 XDB
::execute('UPDATE emails
870 SET last = CURDATE(), panne_level = panne_level + 1
872 AND DATE_ADD(last, INTERVAL 14 DAY) < CURDATE()',
876 if (!empty($x['nb_mails'])) {
877 $mail = new PlMailer('emails/broken.mail.tpl');
878 $mail->addTo("\"{$x['full_name']}\" <{$x['alias']}@"
879 . $globals->mail
->domain
. '>');
880 $mail->assign('x', $x);
881 $mail->assign('email', $email);
885 if (!isset($broken_user_list[$x['alias']])) {
886 $broken_user_list[$x['alias']] = array($email);
888 $broken_user_list[$x['alias']][] = $email;
893 XDB
::execute("UPDATE emails
894 SET panne_level = panne_level - 1
895 WHERE flags = 'active' AND panne_level > 1
896 AND DATE_ADD(last, INTERVAL 1 MONTH) < CURDATE()");
897 XDB
::execute("UPDATE emails
899 WHERE flags = 'active' AND panne_level = 1
900 AND DATE_ADD(last, INTERVAL 1 YEAR) < CURDATE()");
902 // Output the list of users with recently broken addresses,
903 // along with the count of valid redirections.
904 require_once 'notifs.inc.php';
905 pl_content_headers("text/x-csv");
907 $csv = fopen('php://output', 'w');
908 fputcsv($csv, array('nom', 'promo', 'alias', 'bounce', 'nbmails', 'url'), ';');
909 foreach ($broken_user_list as $alias => $mails) {
911 "SELECT acc.uid, count(e.email) AS nb_mails,
912 IFNULL(pd.public_name, acc.full_name) AS fullname,
913 IFNULL(pd.promo, 0) AS promo,
915 INNER JOIN accounts AS acc ON a.id = acc.uid
916 LEFT JOIN emails AS e ON (e.uid = acc.uid
917 AND FIND_IN_SET('active', e.flags) AND e.panne = 0)
918 LEFT JOIN account_profiles AS ap ON (acc.uid = ap.uid AND FIND_IN_SET('owner', ap.perms))
919 LEFT JOIN profile_display AS pd ON (pd.pid = ap.pid)
921 GROUP BY acc.uid", $alias);
923 if ($x = $sel->fetchOneAssoc()) {
924 if ($x['nb_mails'] == 0) {
925 register_profile_update($x['user_id'], 'broken');
927 fputcsv($csv, array($x['fullname'], $x['promo'], $alias,
928 join(',', $mails), $x['nb_mails'],
929 'https://www.polytechnique.org/marketing/broken/' . $alias), ';');
939 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: