admin/emails/broken: remove trailing spaces after email addresses
[platal.git] / modules / email.php
index 6c5d96f..78cef2d 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -41,7 +41,7 @@ class EmailModule extends PLModule
             'admin/emails/duplicated' => $this->make_hook('duplicated',  AUTH_PASSWD, 'admin'),
             'admin/emails/watch'      => $this->make_hook('duplicated',  AUTH_PASSWD, 'admin'),
             'admin/emails/lost'       => $this->make_hook('lost',        AUTH_PASSWD, 'admin'),
-            'admin/emails/broken'     => $this->make_hook('broken_addr', AUTH_PASSWD, 'admin'),
+            'admin/emails/broken'     => $this->make_hook('broken_addr', AUTH_PASSWD, 'admin,edit_directory'),
         );
     }
 
@@ -98,9 +98,23 @@ class EmailModule extends PLModule
                               INNER JOIN  email_virtual_domains AS m ON (s.domain = m.id)
                               INNER JOIN  email_virtual_domains AS d ON (d.aliasing = m.id)
                                    WHERE  s.uid = {?}
-                                ORDER BY  !alias, s.email",
+                                ORDER BY  !alias, s.email, d.name",
                                  $user->id());
-        $page->assign('aliases', $aliases);
+        $aliases_forlife = array();
+        $aliases_hundred = array();
+        $aliases_other = array();
+        while ($a = $aliases->next()) {
+            if ($a['forlife']) {
+                $aliases_forlife[] = $a;
+            } elseif ($a['hundred_year']) {
+                $aliases_hundred[] = $a;
+            } else {
+                $aliases_other[] = $a;
+            }
+        }
+        $page->assign('aliases_forlife', $aliases_forlife);
+        $page->assign('aliases_hundred', $aliases_hundred);
+        $page->assign('aliases_other', $aliases_other);
 
         $alias = XDB::fetchOneCell('SELECT  COUNT(email)
                                       FROM  email_source_account
@@ -152,7 +166,7 @@ class EmailModule extends PLModule
                                                  INNER JOIN  email_virtual_domains AS d ON (s.domain = d.id)
                                                       WHERE  s.uid = {?} AND s.type = \'alias_aux\'',
                                                     $user->id());
-        $visibility = $user->hasProfile() && ($user->profile(true)->alias_pub == 'public');
+        $visibility = $user->hasProfile() && $user->profile()->isVisible($user->profile()->alias_pub);
         $page->assign('current', $alias);
         $page->assign('user', $user);
         $page->assign('mail_public', $visibility);
@@ -163,20 +177,21 @@ class EmailModule extends PLModule
             // Retrieves user request.
             $new_alias  = Env::v('alias');
             $reason = Env::v('reason');
-            $public = (Env::v('public', 'off') == 'on') ? 'public' : 'private';
+            $public = (Env::v('public', 'off') == 'on') ? 'private' : 'hidden';
 
             $page->assign('r_alias', $new_alias);
             $page->assign('r_reason', $reason);
-            if ($public == 'public') {
+            if ($public == 'private') {
                 $page->assign('r_public', true);
             }
 
             // Checks special charaters in alias.
-            if (!preg_match("/^[a-zA-Z0-9\-.]{3,20}$/", $new_alias)) {
+            if (!preg_match("/^[a-zA-Z0-9\-.]{2,19}[a-zA-Z0-9\-]$/", $new_alias)) {
                 $page->trigError("L'adresse demandée n'est pas valide."
                             . " Vérifie qu'elle comporte entre 3 et 20 caractères"
                             . " et qu'elle ne contient que des lettres non accentuées,"
-                            . " des chiffres ou les caractères - et .");
+                            . " des chiffres ou les caractères '-' et '.'. De plus, elle ne"
+                            . " peut pas se terminer par un point.");
                 return;
             } else {
                 // Checks if the alias has already been given.
@@ -660,6 +675,9 @@ class EmailModule extends PLModule
 
             if ($uid) {
                 $dest = User::getWithUID($uid);
+                $active = XDB::fetchOneCell('SELECT flags
+                                               FROM email_redirect_account
+                                              WHERE redirect = {?} AND uid = {?}', $email, $uid) == 'active';
 
                 $mail = new PlMailer('emails/broken-web.mail.tpl');
                 $mail->assign('email', $email);
@@ -787,7 +805,8 @@ class EmailModule extends PLModule
                                                                               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)
+                                  LEFT JOIN  profiles               AS p ON (ap.pid = p.pid)
+                                      WHERE  a.state = \'active\' AND er.redirect IS NULL AND FIND_IN_SET(\'mail\', at.perms) AND p.deathdate IS NULL
                                    GROUP BY  a.uid
                                    ORDER BY  pd.promo, a.hruid'));
     }
@@ -843,30 +862,41 @@ class EmailModule extends PLModule
 
                 $broken_user_list = array();
                 $broken_user_email_count = array();
+                $broken_user_profiles = array();
                 $broken_list = explode("\n", $list);
                 sort($broken_list);
 
                 foreach ($broken_list as $email) {
+                    $email = trim($email);
+                    $userobj = null;
                     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');
-                        }
+                        $userobj = User::getSilentWithUID($user['uid']);
+                    }
+                    if (is_null($userobj)) {
+                        continue;
+                    }
+                    $profile = $userobj->profile();
+                    if (is_null($profile)) {
+                        continue;
+                    }
+                    if ($user['nb_mails'] > 0 && $user['notify']) {
+                        $mail = new PlMailer('emails/broken.mail.tpl');
+                        $mail->setTo($userobj);
+                        $mail->assign('user', $user);
+                        $mail->assign('email', $email);
+                        $mail->send();
+                    } else {
+                        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'];
+                    $pid = $profile->id();
+                    if (!isset($broken_user_list[$pid])) {
+                        $broken_user_list[$pid] = array($email);
+                    } else {
+                        $broken_user_list[$pid][] = $email;
                     }
+                    $broken_user_email_count[$pid] = $user['nb_mails'];
+                    $broken_user_profiles[$pid] = $profile;
                 }
 
                 XDB::execute('UPDATE  email_redirect_account
@@ -878,6 +908,14 @@ class EmailModule extends PLModule
                                WHERE  flags = \'active\' AND broken_level = 1
                                       AND DATE_ADD(last, INTERVAL 1 YEAR) < CURDATE()');
 
+                // Sort $broken_user_list with (promo, sortname, pid)
+                $sortable_array = array();
+                foreach ($broken_user_list as $pid => $mails) {
+                    $profile = $broken_user_profiles[$pid];
+                    $sortable_array[$pid] = array($profile->promo(), $profile->sortName(), $pid);
+                }
+                asort($sortable_array);
+
                 // Output the list of users with recently broken addresses,
                 // along with the count of valid redirections.
                 pl_cached_content_headers('text/x-csv', 1);
@@ -885,10 +923,10 @@ class EmailModule extends PLModule
                 $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] : '';
+                foreach (array_keys($sortable_array) as $pid) {
+                    $mails = $broken_user_list[$pid];
+                    $profile = $broken_user_profiles[$pid];
+                    $current_corps = $profile->getCorpsName();
                     $jobs = $profile->getJobs();
                     $companies = array();
                     foreach ($jobs as $job) {
@@ -900,7 +938,7 @@ class EmailModule extends PLModule
                         $networking_list[] = $networking['address'];
                     }
                     fputcsv($csv, array($profile->fullName(), $profile->promo(),
-                                        join(',', $mails), $broken_user_email_count[$uid],
+                                        join(',', $mails), $broken_user_email_count[$pid],
                                         'https://www.polytechnique.org/marketing/broken/' . $profile->hrid(),
                                         $current_corps, implode(',', $companies), implode(',', $networking_list)), ';');
                 }
@@ -911,5 +949,5 @@ class EmailModule extends PLModule
     }
 }
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>