2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 // First delete the bestalias flag from all this user's emails.
65 XDB
::execute("UPDATE email_source_account
66 SET flags = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', flags, ','), ',bestalias,', ','))
67 WHERE uid = {?}", $user->id());
68 // Then gives the bestalias flag to the given email.
69 list($email, $domain) = explode('@', $email);
70 XDB
::execute("UPDATE email_source_account
71 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'bestalias')
72 WHERE uid = {?} AND email = {?}", $user->id(), $email);
73 XDB
::execute('UPDATE accounts AS a
74 INNER JOIN email_virtual_domains AS d ON (d.name = {?})
75 INNER JOIN email_virtual_domains AS m ON (d.aliasing = m.id)
76 SET a.best_domain = d.id
77 WHERE a.uid = {?} AND m.name = {?}',
78 $domain, $user->id(), $user->mainEmailDomain());
80 // As having a non-null bestalias value is critical in
81 // plat/al's code, we do an a posteriori check on the
82 // validity of the bestalias.
84 // Then refetch the user to update its bestalias.
85 S
::set('user', User
::getWithUID(S
::user()->id()));
88 // Fetch and display aliases.
89 $aliases = XDB
::iterator("SELECT CONCAT(s.email, '@', d.name) AS email, (s.type = 'forlife') AS forlife,
90 (s.email REGEXP '\\\\.[0-9]{2}$') AS hundred_year, s.expire,
91 (FIND_IN_SET('bestalias', s.flags) AND a.best_domain = d.id) AS bestalias,
92 ((s.type = 'alias_aux') AND d.aliasing = d.id) AS alias
93 FROM email_source_account AS s
94 INNER JOIN accounts AS a ON (s.uid = a.uid)
95 INNER JOIN email_virtual_domains AS m ON (s.domain = m.id)
96 INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id)
98 ORDER BY !alias, s.email",
100 $page->assign('aliases', $aliases);
102 $alias = XDB
::fetchOneCell('SELECT COUNT(email)
103 FROM email_source_account
104 WHERE uid = {?} AND type = \'alias_aux\'',
106 $page->assign('alias', $alias);
109 // Check for homonyms.
110 $page->assign('homonyme', $user->homonyme
);
112 // Display active redirections.
113 $redirect = new Redirect($user);
114 $page->assign('mails', $redirect->active_emails());
116 // User's mail domains.
117 $mail_domains = array($user->alternateEmailDomain());
118 $mail_domains[] = User
::$sub_mail_domains['all'] . $globals->mail
->domain
;
119 $mail_domains[] = User
::$sub_mail_domains['all'] . $globals->mail
->domain2
;
120 $page->assign('main_email_domain', $user->mainEmailDomain());
121 $page->assign('mail_domains', $mail_domains);
124 function handler_alias($page, $action = null
, $value = null
)
128 $page->changeTpl('emails/alias.tpl');
129 $page->setTitle('Alias melix.net');
132 $page->assign('request', AliasReq
::get_request($user->id()));
134 // Remove the email alias.
135 if ($action == 'delete') {
136 S
::assert_xsrf_token();
138 XDB
::execute('DELETE FROM email_source_account
139 WHERE uid = {?} AND type = \'alias_aux\'',
142 require_once 'emails.inc.php';
143 fix_bestalias($user);
146 // Fetch existing auxiliary aliases.
147 list($alias, $old_alias) = XDB
::fetchOneRow('SELECT CONCAT(s.email, \'@\', d.name), s.email
148 FROM email_source_account AS s
149 INNER JOIN email_virtual_domains AS d ON (s.domain = d.id)
150 WHERE s.uid = {?} AND s.type = \'alias_aux\'',
152 $visibility = $user->hasProfile() && ($user->profile(true
)->alias_pub
== 'public');
153 $page->assign('current', $alias);
154 $page->assign('user', $user);
155 $page->assign('mail_public', $visibility);
157 if ($action == 'ask' && Env
::has('alias') && Env
::has('reason')) {
158 S
::assert_xsrf_token();
160 // Retrieves user request.
161 $new_alias = Env
::v('alias');
162 $reason = Env
::v('reason');
163 $public = (Env
::v('public', 'off') == 'on') ?
'public' : 'private';
165 $page->assign('r_alias', $new_alias);
166 $page->assign('r_reason', $reason);
167 if ($public == 'public') {
168 $page->assign('r_public', true
);
171 // Checks special charaters in alias.
172 if (!preg_match("/^[a-zA-Z0-9\-.]{3,20}$/", $new_alias)) {
173 $page->trigError("L'adresse demandée n'est pas valide."
174 . " Vérifie qu'elle comporte entre 3 et 20 caractères"
175 . " et qu'elle ne contient que des lettres non accentuées,"
176 . " des chiffres ou les caractères - et .");
179 // Checks if the alias has already been given.
180 $res = XDB
::query('SELECT COUNT(email)
181 FROM email_source_account
182 WHERE email = {?} AND type = \'alias_aux\'',
184 if ($res->fetchOneCell() > 0) {
185 $page->trigError("L'alias $new_alias a déja été attribué. Tu ne peux donc pas l'obtenir.");
189 // Checks if the alias has already been asked for.
190 $it = Validate
::iterate('alias');
191 while($req = $it->next()) {
192 if ($req->alias
== $new_alias) {
193 $page->trigError("L'alias $new_alias a déja été demandé. Tu ne peux donc pas l'obtenir pour l'instant.");
198 // Sends requests. This will erase any previous alias pending request.
199 $myalias = new AliasReq($user, $new_alias, $reason, $public, $old_alias);
201 $page->assign('success', $new_alias);
204 } elseif ($action == 'set' && ($value == 'public' ||
$value == 'private')) {
205 if (!S
::has_xsrf_token()) {
209 if ($user->hasProfile()) {
210 XDB
::execute('UPDATE profiles
213 $value, $user->profile()->id());
219 function handler_redirect($page, $action = null
, $email = null
, $rewrite = null
)
222 require_once 'emails.inc.php';
224 $page->changeTpl('emails/redirect.tpl');
227 $page->assign_by_ref('user', $user);
228 $page->assign('eleve', $user->promo() >= date("Y") - 5);
230 $redirect = new Redirect($user);
232 // FS#703 : $_GET is urldecoded twice, hence
233 // + (the data) => %2B (in the url) => + (first decoding) => ' ' (second decoding)
234 // Since there can be no spaces in emails, we can fix this with :
235 $email = str_replace(' ', '+', $email);
237 // Apply email redirection change requests.
239 if ($action == 'remove' && $email) {
240 $result = $redirect->delete_email($email);
243 if ($action == 'active' && $email) {
244 $redirect->modify_one_email($email, true
);
247 if ($action == 'inactive' && $email) {
248 $redirect->modify_one_email($email, false
);
251 if ($action == 'rewrite' && $email) {
252 $redirect->modify_one_email_redirect($email, $rewrite);
255 if (Env
::has('emailop')) {
256 S
::assert_xsrf_token();
258 $actifs = Env
::v('emails_actifs', array());
259 if (Env
::v('emailop') == "ajouter" && Env
::has('email')) {
260 $error_email = false
;
261 $new_email = Env
::v('email');
262 if ($new_email == "new@example.org") {
263 $new_email = Env
::v('email_new');
265 $result = $redirect->add_email($new_email);
266 if ($result == ERROR_INVALID_EMAIL
) {
268 $page->assign('email', $new_email);
270 $page->assign('retour', $result);
271 $page->assign('error_email', $error_email);
272 } elseif (empty($actifs)) {
273 $result = ERROR_INACTIVE_REDIRECTION
;
274 } elseif (is_array($actifs)) {
275 $result = $redirect->modify_email($actifs, Env
::v('emails_rewrite', array()));
280 case ERROR_INACTIVE_REDIRECTION
:
281 $page->trigError('Tu ne peux pas avoir aucune adresse de redirection active, sinon ton adresse '
282 . $user->forlifeEmail() . ' ne fonctionnerait plus.');
284 case ERROR_INVALID_EMAIL
:
285 $page->trigError('Erreur : l\'email n\'est pas valide.');
287 case ERROR_LOOP_EMAIL
:
288 $page->trigError('Erreur : ' . $user->forlifeEmail()
289 . ' ne doit pas être renvoyé vers lui-même, ni vers son équivalent en '
290 . $globals->mail
->domain2
. ' ni vers polytechnique.edu.');
293 // Fetch existing email aliases.
294 $alias = XDB
::query('SELECT CONCAT(s.email, \'@\', d.name) AS email, s.expire
295 FROM email_source_account AS s
296 INNER JOIN email_virtual_domains AS m ON (s.domain = m.id)
297 INNER JOIN email_virtual_domains AS d ON (m.id = d.aliasing)
299 ORDER BY NOT(s.type = \'alias_aux\'), s.email, d.name',
301 $page->assign('alias', $alias->fetchAllAssoc());
302 $page->assign('best_email', $user->bestEmail());
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, array('job', 'stripped_directory'));
314 function handler_antispam($page, $filter_status = null
, $redirection = 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 (!is_null($filter_status)) {
325 if (is_null($redirection)) {
326 $bogo->changeAll($filter_status);
328 $bogo->change($redirection, $filter_status);
331 $page->assign('filter', $bogo->state
);
332 $page->assign('single_state', $bogo->single_state
);
333 $page->assign('single_redirection', $bogo->single_redirection
);
334 $page->assign('redirections', $bogo->redirections
);
337 function handler_submit($page)
339 $wp = new PlWikiPage('Xorg.Mails');
341 $page->changeTpl('emails/submit_spam.tpl');
343 if (Post
::has('send_email')) {
344 S
::assert_xsrf_token();
346 $upload = PlUpload
::get($_FILES['mail'], S
::user()->login(), 'spam.submit', true
);
348 $page->trigError('Une erreur a été rencontrée lors du transfert du fichier');
351 $mime = $upload->contentType();
352 if ($mime != 'text/x-mail' && $mime != 'message/rfc822') {
354 $page->trigError('Le fichier ne contient pas un email complet');
357 $type = (Post
::v('type') == 'spam' ?
'spam' : 'nonspam');
360 $box = $type . '@' . $globals->mail
->domain
;
361 $mailer = new PlMailer();
362 $mailer->addTo($box);
363 $mailer->setFrom('"' . S
::user()->fullName() . '" <web@' . $globals->mail
->domain
. '>');
364 $mailer->setTxtBody($type . ' soumis par ' . S
::user()->login() . ' via le web');
365 $mailer->addUploadAttachment($upload, $type . '.mail');
367 $page->trigSuccess('Le message a été transmis à ' . $box);
372 function handler_send($page)
374 $page->changeTpl('emails/send.tpl');
376 $page->setTitle('Envoyer un email');
378 // action si on recoit un formulaire
379 if (Post
::has('save')) {
380 if (!S
::has_xsrf_token()) {
384 unset($_POST['save']);
385 if (trim(preg_replace('/-- .*/', '', Post
::v('contenu'))) != "") {
386 Post
::set('to_contacts', explode(';', Post
::s('to_contacts')));
387 Post
::set('cc_contacts', explode(';', Post
::s('cc_contacts')));
388 $data = serialize($_POST);
389 XDB
::execute('INSERT INTO email_send_save (uid, data)
391 ON DUPLICATE KEY UPDATE data = VALUES(data)',
392 S
::user()->id('uid'), $data);
395 } else if (Env
::v('submit') == 'Envoyer') {
396 S
::assert_xsrf_token();
398 function getEmails($aliases)
400 if (!is_array($aliases)) {
403 $uf = new UserFilter(new UFC_Hrpid($aliases));
404 $users = $uf->iterUsers();
406 while ($user = $users->next()) {
407 $ret[] = $user->forlife
;
409 return join(', ', $ret);
413 foreach ($_FILES as &$file) {
414 if ($file['name'] && !PlUpload
::get($file, S
::user()->login(), 'emails.send', false
)) {
415 $page->trigError(PlUpload
::$lastError);
422 XDB
::execute("DELETE FROM email_send_save
426 $to2 = getEmails(Env
::v('to_contacts'));
427 $cc2 = getEmails(Env
::v('cc_contacts'));
428 $txt = str_replace('^M', '', Env
::v('contenu'));
429 $to = str_replace(';', ',', Env
::t('to'));
430 $subj = Env
::t('sujet');
431 $from = Env
::t('from');
432 $cc = str_replace(';', ',', Env
::t('cc'));
433 $bcc = str_replace(';', ',', Env
::t('bcc'));
435 $email_regex = '/^[a-z0-9.\-+_\$]+@([\-.+_]?[a-z0-9])+$/i';
436 foreach (explode(',', $to . ',' . $cc . ',' . $bcc) as $email) {
437 $email = trim($email);
438 if ($email != '' && !preg_match($email_regex, $email)) {
439 $page->trigError("L'adresse email " . $email . ' est erronée.');
443 if (empty($to) && empty($cc) && empty($to2) && empty($bcc) && empty($cc2)) {
444 $page->trigError("Indique au moins un destinataire.");
449 $page->assign('uploaded_f', PlUpload
::listFilenames(S
::user()->login(), 'emails.send'));
451 $mymail = new PlMailer();
452 $mymail->setFrom($from);
453 $mymail->setSubject($subj);
454 if (!empty($to)) { $mymail->addTo($to); }
455 if (!empty($cc)) { $mymail->addCc($cc); }
456 if (!empty($bcc)) { $mymail->addBcc($bcc); }
457 if (!empty($to2)) { $mymail->addTo($to2); }
458 if (!empty($cc2)) { $mymail->addCc($cc2); }
459 $files =& PlUpload
::listFiles(S
::user()->login(), 'emails.send');
460 foreach ($files as $name=>&$upload) {
461 $mymail->addUploadAttachment($upload, $name);
463 if (Env
::v('nowiki')) {
464 $mymail->setTxtBody(wordwrap($txt, 78, "\n"));
466 $mymail->setWikiBody($txt);
468 if ($mymail->send()) {
469 $page->trigSuccess("Ton email a bien été envoyé.");
470 $_REQUEST = array('bcc' => S
::user()->bestEmail());
471 PlUpload
::clear(S
::user()->login(), 'emails.send');
473 $page->trigError("Erreur lors de l'envoi du courriel, réessaye.");
474 $page->assign('uploaded_f', PlUpload
::listFilenames(S
::user()->login(), 'emails.send'));
479 $res = XDB
::query("SELECT data
481 WHERE uid = {?}", S
::i('uid'));
482 if ($res->numRows() == 0) {
483 PlUpload
::clear(S
::user()->login(), 'emails.send');
484 $_REQUEST['bcc'] = S
::user()->bestEmail();
486 $data = unserialize($res->fetchOneCell());
487 $_REQUEST = array_merge($_REQUEST, $data);
491 $uf = new UserFilter(new PFC_And(new UFC_Contact(S
::user()),
492 new UFC_Registered()),
493 UserFilter
::sortByName());
494 $contacts = $uf->getProfiles();
495 $page->assign('contacts', $contacts);
496 $page->assign('maxsize', ini_get('upload_max_filesize') . 'o');
497 $page->assign('user', S
::user());
500 function handler_test($page, $hruid = null
)
502 require_once 'emails.inc.php';
504 if (!S
::has_xsrf_token()) {
508 // Retrieves the User object for the test email recipient.
509 if (S
::admin() && $hruid) {
510 $user = User
::getSilent($hruid);
518 // Sends the test email.
519 $redirect = new Redirect($user);
521 $mailer = new PlMailer('emails/test.mail.tpl');
522 $mailer->assign('email', $user->bestEmail());
523 $mailer->assign('redirects', $redirect->active_emails());
524 $mailer->assign('display_name', $user->displayName());
525 $mailer->assign('sexe', $user->isFemale());
526 $mailer->send($user->isEmailFormatHtml());
530 function handler_rewrite_in($page, $mail, $hash)
532 $page->changeTpl('emails/rewrite.tpl');
533 $page->assign('option', 'in');
534 if (empty($mail) ||
empty($hash)) {
537 $pos = strrpos($mail, '_');
538 if ($pos === false
) {
542 $res = XDB
::query('SELECT COUNT(*)
543 FROM email_redirect_account
544 WHERE redirect = {?} AND hash = {?} AND type = \'smtp\'',
546 $count = intval($res->fetchOneCell());
548 XDB
::query('UPDATE email_redirect_account
549 SET allow_rewrite = true, hash = NULL
550 WHERE redirect = {?} AND hash = {?} AND type = \'smtp\'',
552 $page->trigSuccess("Réécriture activée pour l'adresse " . $mail);
558 function handler_rewrite_out($page, $mail, $hash)
560 $page->changeTpl('emails/rewrite.tpl');
561 $page->assign('option', 'out');
562 if (empty($mail) ||
empty($hash)) {
565 $pos = strrpos($mail, '_');
566 if ($pos === false
) {
570 $res = XDB
::query('SELECT COUNT(*)
571 FROM email_redirect_account
572 WHERE redirect = {?} AND hash = {?} AND type = \'smtp\'',
574 $count = intval($res->fetchOneCell());
577 $res = XDB
::query('SELECT e.redirect, e.rewrite, a.hruid
578 FROM email_redirect_account AS e
579 INNER JOIN accounts AS a ON (e.uid = a.uid)
580 WHERE e.redirect = {?} AND e.hash = {?}',
582 XDB
::query('UPDATE email_redirect_account
583 SET allow_rewrite = false, hash = NULL
584 WHERE redirect = {?} AND hash = {?}',
586 list($mail, $rewrite, $hruid) = $res->fetchOneRow();
587 $mail = new PlMailer();
588 $mail->setFrom("webmaster@" . $globals->mail
->domain
);
589 $mail->addTo("support@" . $globals->mail
->domain
);
590 $mail->setSubject("Tentative de détournement de correspondance via le rewrite");
591 $mail->setTxtBody("$hruid a tenté un rewrite de $mail vers $rewrite. Cette demande a été rejetée via le web");
593 $page->trigWarning("Un mail d'alerte a été envoyé à l'équipe de " . $globals->core
->sitename
);
599 function handler_imap_in($page, $hash = null
, $login = null
)
601 $page->changeTpl('emails/imap_register.tpl');
603 if (!empty($hash) ||
!empty($login)) {
604 $user = User
::getSilent($login);
606 $req = XDB
::query('SELECT 1
608 WHERE uid = {?} AND hash = {?}',
610 if ($req->numRows() == 0) {
616 require_once 'emails.inc.php';
617 $page->assign('ok', false
);
618 if (S
::logged() && (is_null($user) ||
$user->id() == S
::i('uid'))) {
619 Email
::activate_storage(S
::user(), 'imap', Bogo
::IMAP_DEFAULT
);
620 $page->assign('ok', true
);
621 $page->assign('yourself', S
::user()->displayName());
622 $page->assign('sexe', S
::user()->isFemale());
623 } else if (!S
::logged() && $user) {
624 Email
::activate_storage($user, 'imap', Bogo
::IMAP_DEFAULT
);
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 // Usual verifications.
645 $email = valide_email($email);
646 $uid = XDB
::fetchOneCell('SELECT uid
647 FROM email_redirect_account
648 WHERE redirect = {?}', $email);
651 $dest = User
::getWithUID($uid);
653 $mail = new PlMailer('emails/broken-web.mail.tpl');
654 $mail->assign('email', $email);
655 $mail->assign('request', S
::user());
656 $mail->sendTo($dest);
657 $page->trigSuccess('Email envoyé !');
659 } elseif (Post
::has('email')) {
660 S
::assert_xsrf_token();
662 $email = Post
::t('email');
664 if (!User
::isForeignEmailAddress($email)) {
665 $page->assign('neuneu', true
);
667 $user = mark_broken_email($email);
668 $page->assign('user', $user);
669 $page->assign('email', $email);
674 function handler_duplicated($page, $action = 'list', $email = null
)
676 $page->changeTpl('emails/duplicated.tpl');
678 $states = array('pending' => 'En attente...',
679 'safe' => 'Pas d\'inquiétude',
680 'unsafe' => 'Recherches en cours',
681 'dangerous' => 'Usurpations par cette adresse');
682 $page->assign('states', $states);
684 if (Post
::has('action')) {
685 S
::assert_xsrf_token();
687 switch (Post
::v('action')) {
689 if (trim(Post
::v('emailN')) != '') {
690 Xdb
::execute('INSERT IGNORE INTO email_watch (email, state, detection, last, uid, description)
691 VALUES ({?}, {?}, CURDATE(), NOW(), {?}, {?})',
692 trim(Post
::v('emailN')), Post
::v('stateN'), S
::i('uid'), Post
::v('descriptionN'));
697 Xdb
::execute('UPDATE email_watch
698 SET state = {?}, last = NOW(), uid = {?}, description = {?}
699 WHERE email = {?}', Post
::v('stateN'), S
::i('uid'), Post
::v('descriptionN'), Post
::v('emailN'));
703 if ($action == 'delete' && !is_null($email)) {
704 Xdb
::execute('DELETE FROM email_watch WHERE email = {?}', $email);
707 if ($action != 'create' && $action != 'edit') {
710 $page->assign('action', $action);
712 if ($action == 'list') {
713 $it = XDB
::iterRow('SELECT w.email, w.detection, w.state, s.email AS forlife
714 FROM email_watch AS w
715 INNER JOIN email_redirect_account AS r ON (w.email = r.redirect)
716 INNER JOIN email_source_account AS s ON (s.uid = r.uid AND s.type = \'forlife\')
717 ORDER BY w.state, w.email, s.email');
721 while (list($email, $date, $state, $forlife) = $it->next()) {
722 if (count($props) == 0 ||
$props['mail'] != $email) {
723 if (count($props) > 0) {
726 $props = array('mail' => $email,
727 'detection' => $date,
729 'users' => array($forlife));
731 $props['users'][] = $forlife;
734 if (count($props) > 0) {
737 $page->assign('table', $table);
738 } elseif ($action == 'edit') {
739 $it = XDB
::iterRow('SELECT w.detection, w.state, w.last, w.description,
740 a.hruid AS edit, s.email AS forlife
741 FROM email_watch AS w
742 INNER JOIN email_redirect_account AS r ON (w.email = r.redirect)
743 INNER JOIN email_source_account AS s ON (s.uid = r.uid AND s.type = \'forlife\')
744 LEFT JOIN accounts AS a ON (w.uid = a.uid)
750 while (list($detection, $state, $last, $description, $edit, $forlife) = $it->next()) {
751 if (count($props) == 0) {
752 $props = array('mail' => $email,
753 'detection' => $detection,
756 'description' => $description,
758 'users' => array($forlife));
760 $props['users'][] = $forlife;
763 $page->assign('doublon', $props);
767 function handler_lost($page, $action = 'list', $email = null
)
769 $page->changeTpl('emails/lost.tpl');
771 $page->assign('lost_emails',
772 XDB
::iterator('SELECT a.uid, a.hruid, pd.promo
774 INNER JOIN account_types AS at ON (a.type = at.type)
775 LEFT JOIN email_redirect_account AS er ON (er.uid = a.uid AND er.flags = \'active\' AND er.broken_level < 3
776 AND er.type != \'imap\' AND er.type != \'homonym\')
777 LEFT JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
778 LEFT JOIN profile_display AS pd ON (ap.pid = pd.pid)
779 WHERE a.state = \'active\' AND er.redirect IS NULL AND FIND_IN_SET(\'mail\', at.perms)
781 ORDER BY pd.promo, a.hruid'));
784 function handler_broken_addr($page)
786 require_once 'emails.inc.php';
787 $page->changeTpl('emails/broken_addr.tpl');
789 if (Env
::has('sort_broken')) {
790 S
::assert_xsrf_token();
792 $list = trim(Env
::v('list'));
794 $page->trigError('La liste est vide.');
796 $valid_emails = array();
797 $invalid_emails = array();
798 $broken_list = explode("\n", $list);
800 foreach ($broken_list as $orig_email) {
801 $orig_email = trim($orig_email);
802 if ($orig_email != '') {
803 $email = valide_email($orig_email);
804 if (empty($email) ||
$email == '@') {
805 $invalid_emails[] = trim($orig_email) . ': invalid email';
806 } elseif (!in_array($email, $valid_emails)) {
807 $nb = XDB
::fetchOneCell('SELECT COUNT(*)
808 FROM email_redirect_account
809 WHERE redirect = {?}', $email);
811 $valid_emails[] = $email;
813 $invalid_emails[] = $orig_email . ': no such redirection';
819 $page->assign('valid_emails', $valid_emails);
820 $page->assign('invalid_emails', $invalid_emails);
824 if (Env
::has('process_broken')) {
825 S
::assert_xsrf_token();
827 $list = trim(Env
::v('list'));
829 $page->trigError('La liste est vide.');
831 require_once 'notifs.inc.php';
833 $broken_user_list = array();
834 $broken_user_email_count = array();
835 $broken_list = explode("\n", $list);
838 foreach ($broken_list as $email) {
839 if ($user = mark_broken_email($email, true
)) {
840 if ($user['nb_mails'] > 0 && $user['notify']) {
841 $mail = new PlMailer('emails/broken.mail.tpl');
842 $dest = User
::getSilentWithUID($user['uid']);
844 $mail->assign('user', $user);
845 $mail->assign('email', $email);
848 $profile = Profile
::get($user['alias']);
849 WatchProfileUpdate
::register($profile, 'broken');
852 if (!isset($broken_user_list[$user['uid']])) {
853 $broken_user_list[$user['uid']] = array($email);
855 $broken_user_list[$user['uid']][] = $email;
857 $broken_user_email_count[$user['uid']] = $user['nb_mails'];
861 XDB
::execute('UPDATE email_redirect_account
862 SET broken_level = broken_level - 1
863 WHERE flags = \'active\' AND broken_level > 1
864 AND DATE_ADD(last, INTERVAL 1 MONTH) < CURDATE()');
865 XDB
::execute('UPDATE email_redirect_account
867 WHERE flags = \'active\' AND broken_level = 1
868 AND DATE_ADD(last, INTERVAL 1 YEAR) < CURDATE()');
870 // Output the list of users with recently broken addresses,
871 // along with the count of valid redirections.
872 pl_cached_content_headers('text/x-csv', 1);
874 $csv = fopen('php://output', 'w');
875 fputcsv($csv, array('nom', 'promo', 'bounces', 'nbmails', 'url', 'corps', 'job', 'networking'), ';');
876 $corpsList = DirEnum
::getOptions(DirEnum
::CURRENTCORPS
);
877 foreach ($broken_user_list as $uid => $mails) {
878 $profile = Profile
::get($uid);
879 $corps = $profile->getCorps();
880 $current_corps = ($corps && $corps->current
) ?
$corpsList[$corps->current
] : '';
881 $jobs = $profile->getJobs();
882 $companies = array();
883 foreach ($jobs as $job) {
884 $companies[] = $job->company
->name
;
886 $networkings = $profile->getNetworking(Profile
::NETWORKING_ALL
);
887 $networking_list = array();
888 foreach ($networkings as $networking) {
889 $networking_list[] = $networking['address'];
891 fputcsv($csv, array($profile->fullName(), $profile->promo(),
892 join(',', $mails), $broken_user_email_count[$uid],
893 'https://www.polytechnique.org/marketing/broken/' . $profile->hrid(),
894 $current_corps, implode(',', $companies), implode(',', $networking_list)), ';');
903 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: