Use Get::set() and Post::set() instead of direct assignations of
[platal.git] / modules / email.php
index 89b89dd..8b4bcf3 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2009 Polytechnique.org                              *
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -24,14 +24,14 @@ class EmailModule extends PLModule
     function handlers()
     {
         return array(
-            'emails'                  => $this->make_hook('emails',      AUTH_COOKIE),
-            'emails/alias'            => $this->make_hook('alias',       AUTH_MDP),
-            'emails/antispam'         => $this->make_hook('antispam',    AUTH_MDP),
+            'emails'                  => $this->make_hook('emails',      AUTH_COOKIE, 'mail'),
+            'emails/alias'            => $this->make_hook('alias',       AUTH_MDP,    'mail'),
+            'emails/antispam'         => $this->make_hook('antispam',    AUTH_MDP,    'mail'),
             'emails/broken'           => $this->make_hook('broken',      AUTH_COOKIE),
-            'emails/redirect'         => $this->make_hook('redirect',    AUTH_MDP),
-            'emails/send'             => $this->make_hook('send',        AUTH_MDP),
+            'emails/redirect'         => $this->make_hook('redirect',    AUTH_MDP,    'mail'),
+            'emails/send'             => $this->make_hook('send',        AUTH_MDP,    'mail'),
             'emails/antispam/submit'  => $this->make_hook('submit',      AUTH_COOKIE),
-            'emails/test'             => $this->make_hook('test',        AUTH_COOKIE, 'user', NO_AUTH),
+            'emails/test'             => $this->make_hook('test',        AUTH_COOKIE, 'mail', NO_AUTH),
 
             'emails/rewrite/in'       => $this->make_hook('rewrite_in',  AUTH_PUBLIC),
             'emails/rewrite/out'      => $this->make_hook('rewrite_out', AUTH_PUBLIC),
@@ -45,7 +45,7 @@ class EmailModule extends PLModule
         );
     }
 
-    function handler_emails(&$page, $action = null, $email = null)
+    function handler_emails($page, $action = null, $email = null)
     {
         global $globals;
         require_once 'emails.inc.php';
@@ -63,10 +63,10 @@ class EmailModule extends PLModule
 
             XDB::execute("UPDATE  aliases
                              SET  flags = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', flags, ','), ',bestalias,', ','))
-                           WHERE  id = {?}", $user->id());
+                           WHERE  uid = {?}", $user->id());
             XDB::execute("UPDATE  aliases
                              SET  flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'bestalias')
-                           WHERE  id = {?} AND alias = {?}", $user->id(), $email);
+                           WHERE  uid = {?} AND alias = {?}", $user->id(), $email);
 
             // As having a non-null bestalias value is critical in
             // plat/al's code, we do an a posteriori check on the
@@ -79,17 +79,12 @@ class EmailModule extends PLModule
                         (alias REGEXP '\\\\.[0-9]{2}$') AS cent_ans,
                         FIND_IN_SET('bestalias',flags) AS best, expire
                   FROM  aliases
-                 WHERE  id = {?} AND type!='homonyme'
+                 WHERE  uid = {?} AND type!='homonyme'
               ORDER BY  LENGTH(alias)";
         $page->assign('aliases', XDB::iterator($sql, $user->id()));
 
         // Check for homonyms.
-        $homonyme = XDB::query(
-                "SELECT  alias
-                   FROM  aliases
-             INNER JOIN  homonymes ON (id = homonyme_id)
-                  WHERE  user_id = {?} AND type = 'homonyme'", $user->id());
-        $page->assign('homonyme', $homonyme->fetchOneCell());
+        $page->assign('homonyme', $user->homonyme);
 
         // Display active redirections.
         $redirect = new Redirect($user);
@@ -109,10 +104,8 @@ class EmailModule extends PLModule
         $page->assign('melix', $res->fetchOneCell());
     }
 
-    function handler_alias(&$page, $action = null, $value = null)
+    function handler_alias($page, $action = null, $value = null)
     {
-        require_once 'validations.inc.php';
-
         global $globals;
 
         $page->changeTpl('emails/alias.tpl');
@@ -136,6 +129,9 @@ class EmailModule extends PLModule
         // Fetch existing @alias_dom aliases.
         $alias = $user->emailAlias();
         $visibility = $user->hasProfile() && $user->profile()->alias_pub;
+        $page->assign('actuel', $alias);
+        $page->assign('user', $user);
+        $page->assign('mail_public', $visibility);
 
         if ($action == 'ask' && Env::has('alias') && Env::has('raison')) {
             S::assert_xsrf_token();
@@ -173,9 +169,9 @@ class EmailModule extends PLModule
                 }
 
                 //vérifier que l'alias n'est pas déja en demande
-                $it = new ValidateIterator();
+                $it = Validate::iterate('alias');
                 while($req = $it->next()) {
-                    if ($req->type == 'alias' and $req->alias == $alias_mail) {
+                    if ($req->alias == $alias_mail) {
                         $page->trigError("L'alias $alias_mail a déja été demandé.
                                     Tu ne peux donc pas l'obtenir pour l'instant.");
                         return ;
@@ -196,10 +192,11 @@ class EmailModule extends PLModule
             if ($user->hasProfile()) {
                 XDB::execute("UPDATE  profiles
                                  SET  alias_pub = {?}
-                               WHERE  pid = {?}", 
+                               WHERE  pid = {?}",
                             $value, $user->profile()->id());
             }
             $visibility = ($value == 'public');
+            exit;
         }
 
         $page->assign('actuel', $alias);
@@ -207,7 +204,7 @@ class EmailModule extends PLModule
         $page->assign('mail_public', $visibility);
     }
 
-    function handler_redirect(&$page, $action = null, $email = null)
+    function handler_redirect($page, $action = null, $email = null)
     {
         global $globals;
 
@@ -251,15 +248,18 @@ class EmailModule extends PLModule
             $actifs = Env::v('emails_actifs', Array());
             print_r(Env::v('emails_rewrite'));
             if (Env::v('emailop') == "ajouter" && Env::has('email')) {
+                $error_email = false;
                 $new_email = Env::v('email');
                 if ($new_email == "new@example.org") {
                     $new_email = Env::v('email_new');
                 }
                 $result = $redirect->add_email($new_email);
                 if ($result == ERROR_INVALID_EMAIL) {
+                    $error_email = true;
                     $page->assign('email', $new_email);
                 }
                 $page->assign('retour', $result);
+                $page->assign('error_email', $error_email);
             } elseif (empty($actifs)) {
                 $result = ERROR_INACTIVE_REDIRECTION;
             } elseif (is_array($actifs)) {
@@ -273,7 +273,7 @@ class EmailModule extends PLModule
                              . $user->forlifeEmail() . ' ne fonctionnerait plus.');
             break;
           case ERROR_INVALID_EMAIL:
-            $page->trigError('Erreur: l\'email n\'est pas valide.');
+            $page->trigError('Erreur : l\'email n\'est pas valide.');
             break;
           case ERROR_LOOP_EMAIL:
             $page->trigError('Erreur : ' . $user->forlifeEmail()
@@ -303,7 +303,7 @@ class EmailModule extends PLModule
         $res = XDB::query(
                 "SELECT  alias,expire
                    FROM  aliases
-                  WHERE  id={?} AND (type='a_vie' OR type='alias')
+                  WHERE  uid={?} AND (type='a_vie' OR type='alias')
                ORDER BY  !FIND_IN_SET('usage',flags), LENGTH(alias)", $user->id());
         $page->assign('alias', $res->fetchAllAssoc());
         $page->assign('emails', $redirect->emails);
@@ -316,7 +316,7 @@ class EmailModule extends PLModule
         fill_email_combobox($page);
     }
 
-    function handler_antispam(&$page, $statut_filtre = null)
+    function handler_antispam($page, $statut_filtre = null)
     {
         require_once 'emails.inc.php';
         $wp = new PlWikiPage('Xorg.Antispam');
@@ -332,7 +332,7 @@ class EmailModule extends PLModule
         $page->assign('filtre', $bogo->level());
     }
 
-    function handler_submit(&$page)
+    function handler_submit($page)
     {
         $wp = new PlWikiPage('Xorg.Mails');
         $wp->buildCache();
@@ -367,10 +367,9 @@ class EmailModule extends PLModule
         }
     }
 
-    function handler_send(&$page)
+    function handler_send($page)
     {
         $page->changeTpl('emails/send.tpl');
-        $page->addJsLink('ajax.js');
 
         $page->setTitle('Envoyer un email');
 
@@ -382,11 +381,13 @@ class EmailModule extends PLModule
 
             unset($_POST['save']);
             if (trim(preg_replace('/-- .*/', '', Post::v('contenu'))) != "") {
-                $_POST['to_contacts'] = explode(';', @$_POST['to_contacts']);
-                $_POST['cc_contacts'] = explode(';', @$_POST['cc_contacts']);
+                Post::set('to_contacts', explode(';', Post::s('to_contacts')));
+                Post::set('cc_contacts', explode(';', Post::s('cc_contacts')));
                 $data = serialize($_POST);
-                XDB::execute("REPLACE INTO  email_send_save
-                                    VALUES  ({?}, {?})", S::i('uid'), $data);
+                XDB::execute('INSERT INTO  email_send_save (uid, data)
+                                   VALUES  ({?}, {?})
+                  ON DUPLICATE KEY UPDATE  data = VALUES(data)',
+                             S::user()->id('uid'), $data);
             }
             exit;
         } else if (Env::v('submit') == 'Envoyer') {
@@ -397,10 +398,11 @@ class EmailModule extends PLModule
                 if (!is_array($aliases)) {
                     return null;
                 }
-                $rel = Env::v('contacts');
+                $uf = new UserFilter(new UFC_Hrpid($aliases));
+                $users = $uf->iterUsers();
                 $ret = array();
-                foreach ($aliases as $alias) {
-                    $ret[$alias] = $rel[$alias];
+                while ($user = $users->next()) {
+                    $ret[] = $user->forlife;
                 }
                 return join(', ', $ret);
             }
@@ -416,7 +418,8 @@ class EmailModule extends PLModule
 
             if (!$error) {
                 XDB::execute("DELETE FROM  email_send_save
-                                    WHERE  uid = {?}", S::i('uid'));
+                                    WHERE  uid = {?}",
+                             S::user()->id());
 
                 $to2  = getEmails(Env::v('to_contacts'));
                 $cc2  = getEmails(Env::v('cc_contacts'));
@@ -483,19 +486,16 @@ class EmailModule extends PLModule
             }
         }
 
-        $res = XDB::query(
-                "SELECT  ac.full_name, a.alias as forlife
-                   FROM  accounts      AS ac
-             INNER JOIN  contacts      AS c ON (ac.uid = c.contact)
-             INNER JOIN  aliases       AS a ON (ac.uid = a.id AND FIND_IN_SET('bestalias', a.flags))
-                  WHERE  c.uid = {?}
-                 ORDER BY ac.full_name", S::i('uid'));
-        $page->assign('contacts', $res->fetchAllAssoc());
+        $uf = new UserFilter(new PFC_And(new UFC_Contact(S::user()),
+                                         new UFC_Registered()),
+                             UserFilter::sortByName());
+        $contacts = $uf->getProfiles();
+        $page->assign('contacts', $contacts);
         $page->assign('maxsize', ini_get('upload_max_filesize') . 'o');
         $page->assign('user', S::user());
     }
 
-    function handler_test(&$page, $hruid = null)
+    function handler_test($page, $hruid = null)
     {
         require_once 'emails.inc.php';
 
@@ -525,7 +525,7 @@ class EmailModule extends PLModule
         exit;
     }
 
-    function handler_rewrite_in(&$page, $mail, $hash)
+    function handler_rewrite_in($page, $mail, $hash)
     {
         $page->changeTpl('emails/rewrite.tpl');
         $page->assign('option', 'in');
@@ -553,7 +553,7 @@ class EmailModule extends PLModule
         return PL_NOT_FOUND;
     }
 
-    function handler_rewrite_out(&$page, $mail, $hash)
+    function handler_rewrite_out($page, $mail, $hash)
     {
         $page->changeTpl('emails/rewrite.tpl');
         $page->assign('option', 'out');
@@ -574,7 +574,7 @@ class EmailModule extends PLModule
             global $globals;
             $res = XDB::query("SELECT  e.email, e.rewrite, a.alias
                                  FROM  emails AS e
-                           INNER JOIN  aliases AS a ON (a.id = e.uid AND a.type = 'a_vie')
+                           INNER JOIN  aliases AS a ON (a.uid = e.uid AND a.type = 'a_vie')
                                 WHERE  e.email = {?} AND e.hash = {?}",
                               $mail, $hash);
             XDB::query("UPDATE  emails
@@ -594,14 +594,17 @@ class EmailModule extends PLModule
         return PL_NOT_FOUND;
     }
 
-    function handler_imap_in(&$page, $hash = null, $login = null)
+    function handler_imap_in($page, $hash = null, $login = null)
     {
         $page->changeTpl('emails/imap_register.tpl');
         $user = null;
         if (!empty($hash) || !empty($login)) {
             $user = User::getSilent($login);
             if ($user) {
-                $req = XDB::query("SELECT 1 FROM newsletter_ins WHERE user_id = {?} AND hash = {?}", $user->id(), $hash);
+                $req = XDB::query('SELECT  1
+                                     FROM  newsletter_ins
+                                    WHERE  uid = {?} AND hash = {?}',
+                                  $user->id(), $hash);
                 if ($req->numRows() == 0) {
                     $user = null;
                 }
@@ -614,18 +617,18 @@ class EmailModule extends PLModule
             $storage = new EmailStorage(S::user(), 'imap');
             $storage->activate();
             $page->assign('ok', true);
-            $page->assign('prenom', S::v('prenom'));
-            $page->assign('sexe', S::v('femme'));
+            $page->assign('yourself', S::user()->displayName());
+            $page->assign('sexe', S::user()->isFemale());
         } else if (!S::logged() && $user) {
             $storage = new EmailStorage($user, 'imap');
             $storage->activate();
             $page->assign('ok', true);
-            $page->assign('prenom', $user->displayName());
+            $page->assign('yourself', $user->displayName());
             $page->assign('sexe', $user->isFemale());
         }
     }
 
-    function handler_broken(&$page, $warn = null, $email = null)
+    function handler_broken($page, $warn = null, $email = null)
     {
         require_once 'emails.inc.php';
         $wp = new PlWikiPage('Xorg.PatteCassée');
@@ -663,13 +666,14 @@ class EmailModule extends PLModule
                 $page->assign('neuneu', true);
             } else {
                 $page->assign('email',$email);
-                $x = XDB::fetchOneAssoc("SELECT  e1.uid, e1.panne != 0 AS panne,
-                                                 (count(e2.uid) + IF(FIND_IN_SET('googleapps', eo.storage), 1, 0)) AS nb_mails
-                                           FROM  emails as e1
+                $x = XDB::fetchOneAssoc("SELECT  e1.uid, e1.panne != 0 AS panne, a.hruid,
+                                                 (COUNT(e2.uid) + IF(FIND_IN_SET('googleapps', eo.storage), 1, 0)) AS nb_mails
+                                           FROM  emails        AS e1
                                      INNER JOIN  email_options AS eo ON (eo.uid = e1.uid)
-                                      LEFT JOIN  emails as e2 ON(e1.uid = e2.uid
-                                                 AND FIND_IN_SET('active', e2.flags)
-                                                 AND e1.email != e2.email)
+                                     INNER JOIN  accounts      AS a  ON (e1.uid = a.uid)
+                                      LEFT JOIN  emails        AS e2 ON (e1.uid = e2.uid
+                                                                         AND FIND_IN_SET('active', e2.flags)
+                                                                         AND e1.email != e2.email)
                                           WHERE  e1.email = {?}
                                        GROUP BY  e1.uid", $email);
                 if ($x) {
@@ -690,7 +694,7 @@ class EmailModule extends PLModule
         }
     }
 
-    function handler_duplicated(&$page, $action = 'list', $email = null)
+    function handler_duplicated($page, $action = 'list', $email = null)
     {
         $page->changeTpl('emails/duplicated.tpl');
 
@@ -706,21 +710,21 @@ class EmailModule extends PLModule
         switch (Post::v('action')) {
           case 'create':
             if (trim(Post::v('emailN')) != '') {
-                Xdb::execute('INSERT IGNORE INTO emails_watch (email, state, detection, last, uid, description)
+                Xdb::execute('INSERT IGNORE INTO email_watch (email, state, detection, last, uid, description)
                                           VALUES ({?}, {?}, CURDATE(), NOW(), {?}, {?})',
                              trim(Post::v('emailN')), Post::v('stateN'), S::i('uid'), Post::v('descriptionN'));
             };
             break;
 
           case 'edit':
-            Xdb::execute('UPDATE emails_watch
+            Xdb::execute('UPDATE email_watch
                              SET state = {?}, last = NOW(), uid = {?}, description = {?}
                            WHERE email = {?}', Post::v('stateN'), S::i('uid'), Post::v('descriptionN'), Post::v('emailN'));
             break;
 
           default:
             if ($action == 'delete' && !is_null($email)) {
-                Xdb::execute('DELETE FROM emails_watch WHERE email = {?}', $email);
+                Xdb::execute('DELETE FROM email_watch WHERE email = {?}', $email);
             }
         }
         if ($action != 'create' && $action != 'edit') {
@@ -730,9 +734,9 @@ class EmailModule extends PLModule
 
         if ($action == 'list') {
             $sql = "SELECT  w.email, w.detection, w.state, a.alias AS forlife
-                      FROM  emails_watch  AS w
+                      FROM  email_watch  AS w
                  LEFT JOIN  emails        AS e USING(email)
-                 LEFT JOIN  aliases       AS a ON (a.id = e.uid AND a.type = 'a_vie')
+                 LEFT JOIN  aliases       AS a ON (a.uid = e.uid AND a.type = 'a_vie')
                   ORDER BY  w.state, w.email, a.alias";
             $it = Xdb::iterRow($sql);
 
@@ -758,10 +762,10 @@ class EmailModule extends PLModule
         } elseif ($action == 'edit') {
             $sql = "SELECT  w.detection, w.state, w.last, w.description,
                             a1.alias AS edit, a2.alias AS forlife
-                      FROM  emails_watch AS w
-                 LEFT JOIN  aliases      AS a1 ON (a1.id = w.uid AND a1.type = 'a_vie')
+                      FROM  email_watch AS w
+                 LEFT JOIN  aliases      AS a1 ON (a1.uid = w.uid AND a1.type = 'a_vie')
                  LEFT JOIN  emails       AS e  ON (w.email = e.email)
-                 LEFT JOIN  aliases      AS a2 ON (a2.id = e.uid AND a2.type = 'a_vie')
+                 LEFT JOIN  aliases      AS a2 ON (a2.uid = e.uid AND a2.type = 'a_vie')
                      WHERE  w.email = {?}
                   ORDER BY  a2.alias";
             $it = Xdb::iterRow($sql, $email);
@@ -784,22 +788,23 @@ class EmailModule extends PLModule
         }
     }
 
-    function handler_lost(&$page, $action = 'list', $email = null)
+    function handler_lost($page, $action = 'list', $email = null)
     {
         $page->changeTpl('emails/lost.tpl');
 
-        // TODO: Order by promo.
         $page->assign('lost_emails',
-                      XDB::iterator("SELECT  a.uid, a.hruid
-                                       FROM  accounts AS a
-                                 INNER JOIN  email_options AS eo ON (eo.uid = a.uid)
-                                  LEFT JOIN  emails   AS e ON (a.uid = e.uid AND FIND_IN_SET('active', e.flags))
-                                      WHERE  e.uid IS NULL AND FIND_IN_SET('googleapps', eo.storage) = 0 AND
-                                             a.state = 'active'
-                                   ORDER BY  a.hruid"));
+                      XDB::iterator('SELECT  a.uid, a.hruid, pd.promo
+                                       FROM  accounts         AS a
+                                 INNER JOIN  email_options    AS eo ON (eo.uid = a.uid)
+                                  LEFT JOIN  emails           AS e  ON (a.uid = e.uid AND FIND_IN_SET(\'active\', e.flags))
+                                  LEFT JOIN  account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', perms))
+                                  LEFT JOIN  profile_display  AS pd ON (ap.pid = pd.pid)
+                                      WHERE  e.uid IS NULL AND FIND_IN_SET(\'googleapps\', eo.storage) = 0
+                                             AND a.state = \'active\'
+                                   ORDER BY  pd.promo, a.hruid'));
     }
 
-    function handler_broken_addr(&$page)
+    function handler_broken_addr($page)
     {
         require_once 'emails.inc.php';
         $page->changeTpl('emails/broken_addr.tpl');
@@ -816,17 +821,20 @@ class EmailModule extends PLModule
                 $broken_list = explode("\n", $list);
                 sort($broken_list);
                 foreach ($broken_list as $orig_email) {
-                    $email = valide_email(trim($orig_email));
-                    if (empty($email) || $email == '@') {
-                        $invalid_emails[] = trim($orig_email) . ': invalid email';
-                    } elseif (!in_array($email, $valid_emails)) {
-                        $res = XDB::query('SELECT  COUNT(*)
-                                             FROM  emails
-                                            WHERE  email = {?}', $email);
-                        if ($res->fetchOneCell() > 0) {
-                            $valid_emails[] = $email;
-                        } else {
-                            $invalid_emails[] = "$orig_email: no such redirection";
+                    $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)) {
+                            $res = XDB::query('SELECT  COUNT(*)
+                                                 FROM  emails
+                                                WHERE  email = {?}', $email);
+                            if ($res->fetchOneCell() > 0) {
+                                $valid_emails[] = $email;
+                            } else {
+                                $invalid_emails[] = "$orig_email: no such redirection";
+                            }
                         }
                     }
                 }
@@ -843,8 +851,6 @@ class EmailModule extends PLModule
             if ($list == '') {
                 $page->trigError('La liste est vide.');
             } else {
-                global $platal;
-
                 $broken_user_list = array();
                 $broken_list = explode("\n", $list);
                 sort($broken_list);
@@ -856,12 +862,12 @@ class EmailModule extends PLModule
 
                     $sel = XDB::query(
                         "SELECT  e1.uid, e1.panne != 0 AS panne, count(e2.uid) AS nb_mails,
-                                 u.nom, u.prenom, u.promo, a.alias
+                                 acc.full_name, a.alias
                            FROM  emails        AS e1
                       LEFT JOIN  emails        AS e2 ON (e1.uid = e2.uid AND FIND_IN_SET('active', e2.flags)
                                                          AND e1.email != e2.email)
-                     INNER JOIN  auth_user_md5 AS u  ON (e1.uid = u.user_id)
-                     INNER JOIN  aliases       AS a  ON (u.user_id = a.id AND FIND_IN_SET('bestalias', a.flags))
+                     INNER JOIN  accounts      AS acc  ON (e1.uid = acc.uid)
+                     INNER JOIN  aliases       AS a  ON (acc.uid = a.uid AND FIND_IN_SET('bestalias', a.flags))
                           WHERE  e1.email = {?}
                        GROUP BY  e1.uid", $email);
 
@@ -881,8 +887,8 @@ class EmailModule extends PLModule
 
                         if (!empty($x['nb_mails'])) {
                             $mail = new PlMailer('emails/broken.mail.tpl');
-                            $mail->addTo("\"{$x['prenom']} {$x['nom']}\" <{$x['alias']}@"
-                                         . $globals->mail->domain . '>');
+                            $mail->addTo("\"{$x['full_name']}\" <{$x['alias']}@"
+                                         . Platal::globals()->mail->domain . '>');
                             $mail->assign('x', $x);
                             $mail->assign('email', $email);
                             $mail->send();
@@ -907,28 +913,41 @@ class EmailModule extends PLModule
 
                 // Output the list of users with recently broken addresses,
                 // along with the count of valid redirections.
-                require_once 'include/notifs.inc.php';
-                pl_content_headers("text/x-csv");
+                require_once 'notifs.inc.php';
+                pl_cached_content_headers('text/x-csv', 1);
 
                 $csv = fopen('php://output', 'w');
-                fputcsv($csv, array('nom', 'prenom', 'promo', 'alias', 'bounce', 'nbmails', 'url'), ';');
+                fputcsv($csv, array('nom', 'promo', 'alias', 'bounce', 'nbmails', 'url', 'corps', 'job', 'networking'), ';');
                 foreach ($broken_user_list as $alias => $mails) {
                     $sel = Xdb::query(
-                        "SELECT  u.user_id, count(e.email) AS nb_mails, u.nom, u.prenom, u.promo
-                           FROM  aliases       AS a
-                     INNER JOIN  auth_user_md5 AS u ON a.id = u.user_id
-                      LEFT JOIN  emails        AS e ON (e.uid = u.user_id
-                                                        AND FIND_IN_SET('active', e.flags) AND e.panne = 0)
+                        "SELECT  acc.uid, count(DISTINCT(e.email)) AS nb_mails,
+                                 IFNULL(pd.public_name, acc.full_name) AS fullname,
+                                 IFNULL(pd.promo, 0) AS promo, IFNULL(pce.name, 'Aucun') AS corps,
+                                 IFNULL(pje.name, 'Aucun') AS job, GROUP_CONCAT(pn.address SEPARATOR ', ') AS networking
+                           FROM  aliases            AS a
+                     INNER JOIN  accounts           AS acc ON (a.uid = acc.uid)
+                      LEFT JOIN  emails             AS e   ON (e.uid = acc.uid
+                                                               AND FIND_IN_SET('active', e.flags) AND e.panne = 0)
+                      LEFT JOIN  account_profiles   AS ap  ON (acc.uid = ap.uid AND FIND_IN_SET('owner', ap.perms))
+                      LEFT JOIN  profile_display    AS pd  ON (pd.pid = ap.pid)
+                      LEFT JOIN  profile_corps      AS pc  ON (pc.pid = ap.pid)
+                      LEFT JOIN  profile_corps_enum AS pce ON (pc.current_corpsid = pce.id)
+                      LEFT JOIN  profile_job        AS pj  ON (pj.pid = ap.pid)
+                      LEFT JOIN  profile_job_enum   AS pje ON (pj.jobid = pje.id)
+                      LEFT JOIN  profile_networking AS pn  ON (pn.pid = ap.pid)
                           WHERE  a.alias = {?}
-                       GROUP BY  u.user_id", $alias);
+                       GROUP BY  acc.uid", $alias);
 
                     if ($x = $sel->fetchOneAssoc()) {
                         if ($x['nb_mails'] == 0) {
-                            register_profile_update($x['user_id'], 'broken');
+                            $user = User::getSilentWithUID($x['uid']);
+                            $profile = $user->profile();
+                            WatchProfileUpdate::register($profile, 'broken');
                         }
-                        fputcsv($csv, array($x['nom'], $x['prenom'], $x['promo'], $alias,
+                        fputcsv($csv, array($x['fullname'], $x['promo'], $alias,
                                             join(',', $mails), $x['nb_mails'],
-                                            'https://www.polytechnique.org/marketing/broken/' . $alias), ';');
+                                            'https://www.polytechnique.org/marketing/broken/' . $alias,
+                                            $x['corps'], $x['job'], $x['networking']), ';');
                     }
                 }
                 fclose($csv);