+ $page->assign('lost_emails',
+ XDB::iterator('SELECT a.uid, a.hruid, pd.promo
+ FROM accounts AS a
+ INNER JOIN account_types AS at ON (a.type = at.type)
+ LEFT JOIN email_redirect_account AS er ON (er.uid = a.uid AND er.flags = \'active\' AND er.broken_level < 3
+ AND er.type != \'imap\' AND er.type != \'homonym\')
+ LEFT JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
+ LEFT JOIN profile_display AS pd ON (ap.pid = pd.pid)
+ WHERE a.state = \'active\' AND er.redirect IS NULL AND FIND_IN_SET(\'mail\', at.perms)
+ GROUP BY a.uid
+ ORDER BY pd.promo, a.hruid'));
+ }
+
+ function handler_broken_addr($page)
+ {
+ require_once 'emails.inc.php';
+ $page->changeTpl('emails/broken_addr.tpl');
+
+ if (Env::has('sort_broken')) {
+ S::assert_xsrf_token();
+
+ $list = trim(Env::v('list'));
+ if ($list == '') {
+ $page->trigError('La liste est vide.');
+ } else {
+ $valid_emails = array();
+ $invalid_emails = array();
+ $broken_list = explode("\n", $list);
+ sort($broken_list);
+ foreach ($broken_list as $orig_email) {
+ $orig_email = trim($orig_email);
+ if ($orig_email != '') {
+ $email = valide_email($orig_email);
+ if (empty($email) || $email == '@') {
+ $invalid_emails[] = trim($orig_email) . ': invalid email';
+ } elseif (!in_array($email, $valid_emails)) {
+ $nb = XDB::fetchOneCell('SELECT COUNT(*)
+ FROM email_redirect_account
+ WHERE redirect = {?}', $email);
+ if ($nb > 0) {
+ $valid_emails[] = $email;
+ } else {
+ $invalid_emails[] = $orig_email . ': no such redirection';
+ }
+ }
+ }
+ }
+
+ $page->assign('valid_emails', $valid_emails);
+ $page->assign('invalid_emails', $invalid_emails);
+ }
+ }
+
+ if (Env::has('process_broken')) {
+ S::assert_xsrf_token();
+
+ $list = trim(Env::v('list'));
+ if ($list == '') {
+ $page->trigError('La liste est vide.');
+ } else {
+ require_once 'notifs.inc.php';
+
+ $broken_user_list = array();
+ $broken_user_email_count = array();
+ $broken_list = explode("\n", $list);
+ sort($broken_list);
+
+ foreach ($broken_list as $email) {
+ if ($user = mark_broken_email($email, true)) {
+ if ($user['nb_mails'] > 0 && $user['notify']) {
+ $mail = new PlMailer('emails/broken.mail.tpl');
+ $dest = User::getSilentWithUID($user['uid']);
+ $mail->setTo($dest);
+ $mail->assign('user', $user);
+ $mail->assign('email', $email);
+ $mail->send();
+ } else {
+ $profile = Profile::get($user['alias']);
+ WatchProfileUpdate::register($profile, 'broken');
+ }
+
+ if (!isset($broken_user_list[$user['uid']])) {
+ $broken_user_list[$user['uid']] = array($email);
+ } else {
+ $broken_user_list[$user['uid']][] = $email;
+ }
+ $broken_user_email_count[$user['uid']] = $user['nb_mails'];
+ }
+ }
+
+ XDB::execute('UPDATE email_redirect_account
+ SET broken_level = broken_level - 1
+ WHERE flags = \'active\' AND broken_level > 1
+ AND DATE_ADD(last, INTERVAL 1 MONTH) < CURDATE()');
+ XDB::execute('UPDATE email_redirect_account
+ SET broken_level = 0
+ WHERE flags = \'active\' AND broken_level = 1
+ AND DATE_ADD(last, INTERVAL 1 YEAR) < CURDATE()');
+
+ // Output the list of users with recently broken addresses,
+ // along with the count of valid redirections.
+ pl_cached_content_headers('text/x-csv', 1);
+
+ $csv = fopen('php://output', 'w');
+ fputcsv($csv, array('nom', 'promo', 'bounces', 'nbmails', 'url', 'corps', 'job', 'networking'), ';');
+ $corpsList = DirEnum::getOptions(DirEnum::CURRENTCORPS);
+ foreach ($broken_user_list as $uid => $mails) {
+ $profile = Profile::get($uid);
+ $corps = $profile->getCorps();
+ $current_corps = ($corps && $corps->current) ? $corpsList[$corps->current] : '';
+ $jobs = $profile->getJobs();
+ $companies = array();
+ foreach ($jobs as $job) {
+ $companies[] = $job->company->name;
+ }
+ $networkings = $profile->getNetworking(Profile::NETWORKING_ALL);
+ $networking_list = array();
+ foreach ($networkings as $networking) {
+ $networking_list[] = $networking['address'];
+ }
+ fputcsv($csv, array($profile->fullName(), $profile->promo(),
+ join(',', $mails), $broken_user_email_count[$uid],
+ 'https://www.polytechnique.org/marketing/broken/' . $profile->hrid(),
+ $current_corps, implode(',', $companies), implode(',', $networking_list)), ';');
+ }
+ fclose($csv);
+ exit;
+ }
+ }