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 AS s
71 INNER JOIN email_virtual_domains AS d ON (s.domain = d.id)
72 SET s.flags = CONCAT_WS(',', IF(s.flags = '', NULL, s.flags), 'bestalias')
73 WHERE s.uid = {?} AND s.email = {?} AND d.name = {?}", $user->id(), $email, $domain);
75 // As having a non-null bestalias value is critical in
76 // plat/al's code, we do an a posteriori check on the
77 // validity of the bestalias.
81 // Fetch and display aliases.
82 $aliases = XDB
::iterator("SELECT CONCAT(s.email, '@', d.name) AS email, (s.type = 'forlife') AS forlife,
83 (s.email REGEXP '\\\\.[0-9]{2}$') AS hundred_year,
84 FIND_IN_SET('bestalias', s.flags) AS bestalias, s.expire,
85 (s.type = 'alias_aux') AS alias
86 FROM email_source_account AS s
87 INNER JOIN email_virtual_domains AS d ON (s.domain = d.id)
89 ORDER BY !alias, s.email",
91 $page->assign('aliases', $aliases);
93 $alias = XDB
::fetchOneCell('SELECT COUNT(email)
94 FROM email_source_account
95 WHERE uid = {?} AND type = \'alias_aux\'',
97 $page->assign('alias', $alias);
100 // Check for homonyms.
101 $page->assign('homonyme', $user->homonyme
);
103 // Display active redirections.
104 $redirect = new Redirect($user);
105 $page->assign('mails', $redirect->active_emails());
107 // User's mail domains.
108 $mail_domains = array($user->alternateEmailDomain());
109 $mail_domains[] = User
::$sub_mail_domains['all'] . $globals->mail
->domain
;
110 $mail_domains[] = User
::$sub_mail_domains['all'] . $globals->mail
->domain2
;
111 $page->assign('main_email_domain', $user->mainEmailDomain());
112 $page->assign('mail_domains', $mail_domains);
115 function handler_alias($page, $action = null
, $value = null
)
119 $page->changeTpl('emails/alias.tpl');
120 $page->setTitle('Alias melix.net');
123 $page->assign('request', AliasReq
::get_request($user->id()));
125 // Remove the email alias.
126 if ($action == 'delete') {
127 S
::assert_xsrf_token();
129 XDB
::execute('DELETE FROM email_source_account
130 WHERE uid = {?} AND type = \'alias_aux\'',
133 require_once 'emails.inc.php';
134 fix_bestalias($user);
137 // Fetch existing auxiliary aliases.
138 list($alias, $old_alias) = XDB
::fetchOneRow('SELECT CONCAT(s.email, \'@\', d.name), s.email
139 FROM email_source_account AS s
140 INNER JOIN email_virtual_domains AS d ON (s.domain = d.id)
141 WHERE s.uid = {?} AND s.type = \'alias_aux\'',
143 $visibility = $user->hasProfile() && ($user->profile(true
)->alias_pub
== 'public');
144 $page->assign('current', $alias);
145 $page->assign('user', $user);
146 $page->assign('mail_public', $visibility);
148 if ($action == 'ask' && Env
::has('alias') && Env
::has('reason')) {
149 S
::assert_xsrf_token();
151 // Retrieves user request.
152 $new_alias = Env
::v('alias');
153 $reason = Env
::v('reason');
154 $public = (Env
::v('public', 'off') == 'on') ?
'public' : 'private';
156 $page->assign('r_alias', $new_alias);
157 $page->assign('r_reason', $reason);
158 if ($public == 'public') {
159 $page->assign('r_public', true
);
162 // Checks special charaters in alias.
163 if (!preg_match("/^[a-zA-Z0-9\-.]{3,20}$/", $new_alias)) {
164 $page->trigError("L'adresse demandée n'est pas valide."
165 . " Vérifie qu'elle comporte entre 3 et 20 caractères"
166 . " et qu'elle ne contient que des lettres non accentuées,"
167 . " des chiffres ou les caractères - et .");
170 // Checks if the alias has already been given.
171 $res = XDB
::query('SELECT COUNT(email)
172 FROM email_source_account
173 WHERE email = {?} AND type = \'alias_aux\'',
175 if ($res->fetchOneCell() > 0) {
176 $page->trigError("L'alias $new_alias a déja été attribué. Tu ne peux donc pas l'obtenir.");
180 // Checks if the alias has already been asked for.
181 $it = Validate
::iterate('alias');
182 while($req = $it->next()) {
183 if ($req->alias
== $new_alias) {
184 $page->trigError("L'alias $new_alias a déja été demandé. Tu ne peux donc pas l'obtenir pour l'instant.");
189 // Sends requests. This will erase any previous alias pending request.
190 $myalias = new AliasReq($user, $new_alias, $reason, $public, $old_alias);
192 $page->assign('success', $new_alias);
195 } elseif ($action == 'set' && ($value == 'public' ||
$value == 'private')) {
196 if (!S
::has_xsrf_token()) {
200 if ($user->hasProfile()) {
201 XDB
::execute('UPDATE profiles
204 $value, $user->profile()->id());
210 function handler_redirect($page, $action = null
, $email = null
, $rewrite = null
)
213 require_once 'emails.inc.php';
215 $page->changeTpl('emails/redirect.tpl');
218 $page->assign_by_ref('user', $user);
219 $page->assign('eleve', $user->promo() >= date("Y") - 5);
221 $redirect = new Redirect($user);
223 // FS#703 : $_GET is urldecoded twice, hence
224 // + (the data) => %2B (in the url) => + (first decoding) => ' ' (second decoding)
225 // Since there can be no spaces in emails, we can fix this with :
226 $email = str_replace(' ', '+', $email);
228 // Apply email redirection change requests.
230 if ($action == 'remove' && $email) {
231 $result = $redirect->delete_email($email);
234 if ($action == 'active' && $email) {
235 $redirect->modify_one_email($email, true
);
238 if ($action == 'inactive' && $email) {
239 $redirect->modify_one_email($email, false
);
242 if ($action == 'rewrite' && $email) {
243 $redirect->modify_one_email_redirect($email, $rewrite);
246 if (Env
::has('emailop')) {
247 S
::assert_xsrf_token();
249 $actifs = Env
::v('emails_actifs', array());
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.');
284 // Fetch existing email aliases.
285 $alias = XDB
::query('SELECT CONCAT(s.email, \'@\', d.name) AS email, s.expire
286 FROM email_source_account AS s
287 INNER JOIN email_virtual_domains AS m ON (s.domain = m.id)
288 INNER JOIN email_virtual_domains AS d ON (m.id = d.aliasing)
290 ORDER BY NOT(s.type = \'alias_aux\'), s.email, d.name',
292 $page->assign('alias', $alias->fetchAllAssoc());
294 $page->assign('emails', $redirect->emails
);
296 // Display GoogleApps acount information.
297 require_once 'googleapps.inc.php';
298 $page->assign('googleapps', GoogleAppsAccount
::account_status($user->id()));
300 require_once 'emails.combobox.inc.php';
301 fill_email_combobox($page);
304 function handler_antispam($page, $filter_status = null
, $redirection = null
)
306 require_once 'emails.inc.php';
307 $wp = new PlWikiPage('Xorg.Antispam');
310 $page->changeTpl('emails/antispam.tpl');
313 $bogo = new Bogo($user);
314 if (!is_null($filter_status)) {
315 if (is_null($redirection)) {
316 $bogo->changeAll($filter_status);
318 $bogo->change($redirection, $filter_status);
321 $page->assign('filter', $bogo->state
);
322 $page->assign('single_state', $bogo->single_state
);
323 $page->assign('single_redirection', $bogo->single_redirection
);
324 $page->assign('redirections', $bogo->redirections
);
327 function handler_submit($page)
329 $wp = new PlWikiPage('Xorg.Mails');
331 $page->changeTpl('emails/submit_spam.tpl');
333 if (Post
::has('send_email')) {
334 S
::assert_xsrf_token();
336 $upload = PlUpload
::get($_FILES['mail'], S
::user()->login(), 'spam.submit', true
);
338 $page->trigError('Une erreur a été rencontrée lors du transfert du fichier');
341 $mime = $upload->contentType();
342 if ($mime != 'text/x-mail' && $mime != 'message/rfc822') {
344 $page->trigError('Le fichier ne contient pas un email complet');
347 $type = (Post
::v('type') == 'spam' ?
'spam' : 'nonspam');
350 $box = $type . '@' . $globals->mail
->domain
;
351 $mailer = new PlMailer();
352 $mailer->addTo($box);
353 $mailer->setFrom('"' . S
::user()->fullName() . '" <web@' . $globals->mail
->domain
. '>');
354 $mailer->setTxtBody($type . ' soumis par ' . S
::user()->login() . ' via le web');
355 $mailer->addUploadAttachment($upload, $type . '.mail');
357 $page->trigSuccess('Le message a été transmis à ' . $box);
362 function handler_send($page)
364 $page->changeTpl('emails/send.tpl');
366 $page->setTitle('Envoyer un email');
368 // action si on recoit un formulaire
369 if (Post
::has('save')) {
370 if (!S
::has_xsrf_token()) {
374 unset($_POST['save']);
375 if (trim(preg_replace('/-- .*/', '', Post
::v('contenu'))) != "") {
376 Post
::set('to_contacts', explode(';', Post
::s('to_contacts')));
377 Post
::set('cc_contacts', explode(';', Post
::s('cc_contacts')));
378 $data = serialize($_POST);
379 XDB
::execute('INSERT INTO email_send_save (uid, data)
381 ON DUPLICATE KEY UPDATE data = VALUES(data)',
382 S
::user()->id('uid'), $data);
385 } else if (Env
::v('submit') == 'Envoyer') {
386 S
::assert_xsrf_token();
388 function getEmails($aliases)
390 if (!is_array($aliases)) {
393 $uf = new UserFilter(new UFC_Hrpid($aliases));
394 $users = $uf->iterUsers();
396 while ($user = $users->next()) {
397 $ret[] = $user->forlife
;
399 return join(', ', $ret);
403 foreach ($_FILES as &$file) {
404 if ($file['name'] && !PlUpload
::get($file, S
::user()->login(), 'emails.send', false
)) {
405 $page->trigError(PlUpload
::$lastError);
412 XDB
::execute("DELETE FROM email_send_save
416 $to2 = getEmails(Env
::v('to_contacts'));
417 $cc2 = getEmails(Env
::v('cc_contacts'));
418 $txt = str_replace('^M', '', Env
::v('contenu'));
419 $to = str_replace(';', ',', Env
::t('to'));
420 $subj = Env
::t('sujet');
421 $from = Env
::t('from');
422 $cc = str_replace(';', ',', Env
::t('cc'));
423 $bcc = str_replace(';', ',', Env
::t('bcc'));
425 $email_regex = '/^[a-z0-9.\-+_\$]+@([\-.+_]?[a-z0-9])+$/i';
426 foreach (explode(',', $to . ',' . $cc . ',' . $bcc) as $email) {
427 $email = trim($email);
428 if ($email != '' && !preg_match($email_regex, $email)) {
429 $page->trigError("L'adresse email " . $email . ' est erronée.');
433 if (empty($to) && empty($cc) && empty($to2) && empty($bcc) && empty($cc2)) {
434 $page->trigError("Indique au moins un destinataire.");
439 $page->assign('uploaded_f', PlUpload
::listFilenames(S
::user()->login(), 'emails.send'));
441 $mymail = new PlMailer();
442 $mymail->setFrom($from);
443 $mymail->setSubject($subj);
444 if (!empty($to)) { $mymail->addTo($to); }
445 if (!empty($cc)) { $mymail->addCc($cc); }
446 if (!empty($bcc)) { $mymail->addBcc($bcc); }
447 if (!empty($to2)) { $mymail->addTo($to2); }
448 if (!empty($cc2)) { $mymail->addCc($cc2); }
449 $files =& PlUpload
::listFiles(S
::user()->login(), 'emails.send');
450 foreach ($files as $name=>&$upload) {
451 $mymail->addUploadAttachment($upload, $name);
453 if (Env
::v('nowiki')) {
454 $mymail->setTxtBody(wordwrap($txt, 78, "\n"));
456 $mymail->setWikiBody($txt);
458 if ($mymail->send()) {
459 $page->trigSuccess("Ton email a bien été envoyé.");
460 $_REQUEST = array('bcc' => S
::user()->bestEmail());
461 PlUpload
::clear(S
::user()->login(), 'emails.send');
463 $page->trigError("Erreur lors de l'envoi du courriel, réessaye.");
464 $page->assign('uploaded_f', PlUpload
::listFilenames(S
::user()->login(), 'emails.send'));
469 $res = XDB
::query("SELECT data
471 WHERE uid = {?}", S
::i('uid'));
472 if ($res->numRows() == 0) {
473 PlUpload
::clear(S
::user()->login(), 'emails.send');
474 $_REQUEST['bcc'] = S
::user()->bestEmail();
476 $data = unserialize($res->fetchOneCell());
477 $_REQUEST = array_merge($_REQUEST, $data);
481 $uf = new UserFilter(new PFC_And(new UFC_Contact(S
::user()),
482 new UFC_Registered()),
483 UserFilter
::sortByName());
484 $contacts = $uf->getProfiles();
485 $page->assign('contacts', $contacts);
486 $page->assign('maxsize', ini_get('upload_max_filesize') . 'o');
487 $page->assign('user', S
::user());
490 function handler_test($page, $hruid = null
)
492 require_once 'emails.inc.php';
494 if (!S
::has_xsrf_token()) {
498 // Retrieves the User object for the test email recipient.
499 if (S
::admin() && $hruid) {
500 $user = User
::getSilent($hruid);
508 // Sends the test email.
509 $redirect = new Redirect($user);
511 $mailer = new PlMailer('emails/test.mail.tpl');
512 $mailer->assign('email', $user->bestEmail());
513 $mailer->assign('redirects', $redirect->active_emails());
514 $mailer->assign('display_name', $user->displayName());
515 $mailer->assign('sexe', $user->isFemale());
516 $mailer->send($user->isEmailFormatHtml());
520 function handler_rewrite_in($page, $mail, $hash)
522 $page->changeTpl('emails/rewrite.tpl');
523 $page->assign('option', 'in');
524 if (empty($mail) ||
empty($hash)) {
527 $pos = strrpos($mail, '_');
528 if ($pos === false
) {
532 $res = XDB
::query('SELECT COUNT(*)
533 FROM email_redirect_account
534 WHERE redirect = {?} AND hash = {?} AND type = \'smtp\'',
536 $count = intval($res->fetchOneCell());
538 XDB
::query('UPDATE email_redirect_account
539 SET allow_rewrite = true, hash = NULL
540 WHERE redirect = {?} AND hash = {?} AND type = \'smtp\'',
542 $page->trigSuccess("Réécriture activée pour l'adresse " . $mail);
548 function handler_rewrite_out($page, $mail, $hash)
550 $page->changeTpl('emails/rewrite.tpl');
551 $page->assign('option', 'out');
552 if (empty($mail) ||
empty($hash)) {
555 $pos = strrpos($mail, '_');
556 if ($pos === false
) {
560 $res = XDB
::query('SELECT COUNT(*)
561 FROM email_redirect_account
562 WHERE redirect = {?} AND hash = {?} AND type = \'smtp\'',
564 $count = intval($res->fetchOneCell());
567 $res = XDB
::query('SELECT e.redirect, e.rewrite, a.hruid
568 FROM email_redirect_account AS e
569 INNER JOIN accounts AS a ON (e.uid = a.uid)
570 WHERE e.redirect = {?} AND e.hash = {?}',
572 XDB
::query('UPDATE email_redirect_account
573 SET allow_rewrite = false, hash = NULL
574 WHERE redirect = {?} AND hash = {?}',
576 list($mail, $rewrite, $hruid) = $res->fetchOneRow();
577 $mail = new PlMailer();
578 $mail->setFrom("webmaster@" . $globals->mail
->domain
);
579 $mail->addTo("support@" . $globals->mail
->domain
);
580 $mail->setSubject("Tentative de détournement de correspondance via le rewrite");
581 $mail->setTxtBody("$hruid a tenté un rewrite de $mail vers $rewrite. Cette demande a été rejetée via le web");
583 $page->trigWarning("Un mail d'alerte a été envoyé à l'équipe de " . $globals->core
->sitename
);
589 function handler_imap_in($page, $hash = null
, $login = null
)
591 $page->changeTpl('emails/imap_register.tpl');
593 if (!empty($hash) ||
!empty($login)) {
594 $user = User
::getSilent($login);
596 $req = XDB
::query('SELECT 1
598 WHERE uid = {?} AND hash = {?}',
600 if ($req->numRows() == 0) {
606 require_once 'emails.inc.php';
607 $page->assign('ok', false
);
608 if (S
::logged() && (is_null($user) ||
$user->id() == S
::i('uid'))) {
609 Email
::activate_storage(S
::user(), 'imap');
610 $page->assign('ok', true
);
611 $page->assign('yourself', S
::user()->displayName());
612 $page->assign('sexe', S
::user()->isFemale());
613 } else if (!S
::logged() && $user) {
614 Email
::activate_storage($user, 'imap');
615 $page->assign('ok', true
);
616 $page->assign('yourself', $user->displayName());
617 $page->assign('sexe', $user->isFemale());
621 function handler_broken($page, $warn = null
, $email = null
)
623 require_once 'emails.inc.php';
624 $wp = new PlWikiPage('Xorg.PatteCassée');
629 $page->changeTpl('emails/broken.tpl');
631 if ($warn == 'warn' && $email) {
632 S
::assert_xsrf_token();
634 // Usual verifications.
635 $email = valide_email($email);
636 $uid = XDB
::fetchOneCell('SELECT uid
637 FROM email_redirect_account
638 WHERE redirect = {?}', $email);
641 $dest = User
::getWithUID($uid);
643 $mail = new PlMailer('emails/broken-web.mail.tpl');
644 $mail->assign('email', $email);
645 $mail->assign('request', S
::user());
646 $mail->sendTo($dest);
647 $page->trigSuccess('Email envoyé !');
649 } elseif (Post
::has('email')) {
650 S
::assert_xsrf_token();
652 $email = Post
::t('email');
654 if (!User
::isForeignEmailAddress($email)) {
655 $page->assign('neuneu', true
);
657 $user = mark_broken_email($email);
658 $page->assign('user', $user);
659 $page->assign('email', $email);
664 function handler_duplicated($page, $action = 'list', $email = null
)
666 $page->changeTpl('emails/duplicated.tpl');
668 $states = array('pending' => 'En attente...',
669 'safe' => 'Pas d\'inquiétude',
670 'unsafe' => 'Recherches en cours',
671 'dangerous' => 'Usurpations par cette adresse');
672 $page->assign('states', $states);
674 if (Post
::has('action')) {
675 S
::assert_xsrf_token();
677 switch (Post
::v('action')) {
679 if (trim(Post
::v('emailN')) != '') {
680 Xdb
::execute('INSERT IGNORE INTO email_watch (email, state, detection, last, uid, description)
681 VALUES ({?}, {?}, CURDATE(), NOW(), {?}, {?})',
682 trim(Post
::v('emailN')), Post
::v('stateN'), S
::i('uid'), Post
::v('descriptionN'));
687 Xdb
::execute('UPDATE email_watch
688 SET state = {?}, last = NOW(), uid = {?}, description = {?}
689 WHERE email = {?}', Post
::v('stateN'), S
::i('uid'), Post
::v('descriptionN'), Post
::v('emailN'));
693 if ($action == 'delete' && !is_null($email)) {
694 Xdb
::execute('DELETE FROM email_watch WHERE email = {?}', $email);
697 if ($action != 'create' && $action != 'edit') {
700 $page->assign('action', $action);
702 if ($action == 'list') {
703 $it = XDB
::iterRow('SELECT w.email, w.detection, w.state, s.email AS forlife
704 FROM email_watch AS w
705 INNER JOIN email_redirect_account AS r ON (w.email = r.redirect)
706 INNER JOIN email_source_account AS s ON (s.uid = r.uid AND s.type = \'forlife\')
707 ORDER BY w.state, w.email, s.email');
711 while (list($email, $date, $state, $forlife) = $it->next()) {
712 if (count($props) == 0 ||
$props['mail'] != $email) {
713 if (count($props) > 0) {
716 $props = array('mail' => $email,
717 'detection' => $date,
719 'users' => array($forlife));
721 $props['users'][] = $forlife;
724 if (count($props) > 0) {
727 $page->assign('table', $table);
728 } elseif ($action == 'edit') {
729 $it = XDB
::iterRow('SELECT w.detection, w.state, w.last, w.description,
730 a.hruid AS edit, s.email AS forlife
731 FROM email_watch AS w
732 INNER JOIN email_redirect_account AS r ON (w.email = r.redirect)
733 INNER JOIN email_source_account AS s ON (s.uid = r.uid AND s.type = \'forlife\')
734 INNER JOIN accounts AS a ON (w.uid = a.uid)
740 while (list($detection, $state, $last, $description, $edit, $forlife) = $it->next()) {
741 if (count($props) == 0) {
742 $props = array('mail' => $email,
743 'detection' => $detection,
746 'description' => $description,
748 'users' => array($forlife));
750 $props['users'][] = $forlife;
753 $page->assign('doublon', $props);
757 function handler_lost($page, $action = 'list', $email = null
)
759 $page->changeTpl('emails/lost.tpl');
761 $page->assign('lost_emails',
762 XDB
::iterator('SELECT a.uid, a.hruid, pd.promo
764 INNER JOIN account_types AS at ON (a.type = at.type)
765 LEFT JOIN email_redirect_account AS er ON (er.uid = a.uid AND er.flags = \'active\' AND er.broken_level < 3
766 AND er.type != \'imap\' AND er.type != \'homonym\')
767 LEFT JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
768 LEFT JOIN profile_display AS pd ON (ap.pid = pd.pid)
769 WHERE a.state = \'active\' AND er.redirect IS NULL AND FIND_IN_SET(\'mail\', at.perms)
771 ORDER BY pd.promo, a.hruid'));
774 function handler_broken_addr($page)
776 require_once 'emails.inc.php';
777 $page->changeTpl('emails/broken_addr.tpl');
779 if (Env
::has('sort_broken')) {
780 S
::assert_xsrf_token();
782 $list = trim(Env
::v('list'));
784 $page->trigError('La liste est vide.');
786 $valid_emails = array();
787 $invalid_emails = array();
788 $broken_list = explode("\n", $list);
790 foreach ($broken_list as $orig_email) {
791 $orig_email = trim($orig_email);
792 if ($orig_email != '') {
793 $email = valide_email($orig_email);
794 if (empty($email) ||
$email == '@') {
795 $invalid_emails[] = trim($orig_email) . ': invalid email';
796 } elseif (!in_array($email, $valid_emails)) {
797 $nb = XDB
::fetchOneCell('SELECT COUNT(*)
798 FROM email_redirect_account
799 WHERE redirect = {?}', $email);
801 $valid_emails[] = $email;
803 $invalid_emails[] = $orig_email . ': no such redirection';
809 $page->assign('valid_emails', $valid_emails);
810 $page->assign('invalid_emails', $invalid_emails);
814 if (Env
::has('process_broken')) {
815 S
::assert_xsrf_token();
817 $list = trim(Env
::v('list'));
819 $page->trigError('La liste est vide.');
821 $broken_user_list = array();
822 $broken_list = explode("\n", $list);
825 foreach ($broken_list as $email) {
826 if ($user = mark_broken_email($email, true
)) {
827 if ($user['nb_mails'] > 0) {
828 $mail = new PlMailer('emails/broken.mail.tpl');
830 $mail->assign('user', $user);
831 $mail->assign('email', $email);
835 if (!isset($broken_user_list[$user['alias']])) {
836 $broken_user_list[$user['alias']] = array($email);
838 $broken_user_list[$user['alias']][] = $email;
843 XDB
::execute('UPDATE email_redirect_account
844 SET broken_level = broken_level - 1
845 WHERE flags = \'active\' AND broken_level > 1
846 AND DATE_ADD(last, INTERVAL 1 MONTH) < CURDATE()');
847 XDB
::execute('UPDATE email_redirect_account
849 WHERE flags = \'active\' AND broken_level = 1
850 AND DATE_ADD(last, INTERVAL 1 YEAR) < CURDATE()');
852 // Output the list of users with recently broken addresses,
853 // along with the count of valid redirections.
854 require_once 'notifs.inc.php';
855 pl_cached_content_headers('text/x-csv', 1);
857 $csv = fopen('php://output', 'w');
858 fputcsv($csv, array('nom', 'promo', 'alias', 'bounce', 'nbmails', 'url', 'corps', 'job', 'networking'), ';');
859 foreach ($broken_user_list as $alias => $mails) {
861 'SELECT a.uid, count(DISTINCT(r.redirect)) AS nb_mails,
862 IFNULL(pd.public_name, a.full_name) AS fullname,
863 IFNULL(pd.promo, 0) AS promo, IFNULL(pce.name, \'Aucun\') AS corps,
864 IFNULL(pje.name, \'Aucun\') AS job, GROUP_CONCAT(pn.address SEPARATOR \', \') AS networking
865 FROM email_source_account AS s
866 INNER JOIN accounts AS a ON (s.uid = a.uid)
867 LEFT JOIN email_redirect_account AS r ON (a.uid = r.uid AND r.broken_level = 0 AND r.flags = \'active\' AND
868 (r.type = \'smtp\' OR r.type = \'googleapps\'))
869 LEFT JOIN account_profiles AS ap ON (a.uid = ap.uid AND FIND_IN_SET(\'owner\', ap.perms))
870 LEFT JOIN profile_display AS pd ON (pd.pid = ap.pid)
871 LEFT JOIN profile_corps AS pc ON (pc.pid = ap.pid)
872 LEFT JOIN profile_corps_enum AS pce ON (pc.current_corpsid = pce.id)
873 LEFT JOIN profile_job AS pj ON (pj.pid = ap.pid)
874 LEFT JOIN profile_job_enum AS pje ON (pj.jobid = pje.id)
875 LEFT JOIN profile_networking AS pn ON (pn.pid = ap.pid)
877 GROUP BY a.uid', $alias);
879 if ($x = $sel->fetchOneAssoc()) {
880 if ($x['nb_mails'] == 0) {
881 $user = User
::getSilentWithUID($x['uid']);
882 $profile = $user->profile();
883 WatchProfileUpdate
::register($profile, 'broken');
885 fputcsv($csv, array($x['fullname'], $x['promo'], $alias,
886 join(',', $mails), $x['nb_mails'],
887 'https://www.polytechnique.org/marketing/broken/' . $alias,
888 $x['corps'], $x['job'], $x['networking']), ';');
898 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: