Merge commit 'origin/master' into hruid
authorVincent Zanotti <vincent.zanotti@polytechnique.org>
Sun, 21 Sep 2008 20:09:09 +0000 (22:09 +0200)
committerVincent Zanotti <vincent.zanotti@polytechnique.org>
Sun, 21 Sep 2008 20:09:09 +0000 (22:09 +0200)
Conflicts:
classes/xnetsession.php
include/validations/medals.inc.php
include/validations/nomusage.inc.php
include/validations/photos.inc.php
include/vcard.inc.php

Signed-off-by: Vincent Zanotti <vincent.zanotti@polytechnique.org>
1  2 
classes/xnetsession.php
include/user.func.inc.php
include/validations/listes.inc.php
include/validations/medals.inc.php
include/validations/nomusage.inc.php
include/validations/photos.inc.php
include/vcard.inc.php
modules/carnet.php
modules/profile.php
modules/xnetgrp.php

diff --combined classes/xnetsession.php
@@@ -19,7 -19,7 +19,7 @@@
   *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
   ***************************************************************************/
  
- class XnetSession extends PlSession
+ class XnetSession extends XorgSession
  {
      public function __construct()
      {
          if ($level == -1) {
              S::set('auth', AUTH_MDP);
          }
 -        $res  = XDB::query('SELECT  u.user_id AS uid, prenom, nom, perms, promo, password, FIND_IN_SET(\'femme\', u.flags) AS femme,
 -                                    a.alias AS forlife, a2.alias AS bestalias, q.core_mail_fmt AS mail_fmt, q.core_rss_hash
 +        $res  = XDB::query("SELECT  u.user_id AS uid, u.hruid, prenom, nom, perms, promo, password, FIND_IN_SET('femme', u.flags) AS femme,
 +                                    CONCAT(a.alias, '@{$globals->mail->domain}') AS forlife,
 +                                    CONCAT(a2.alias, '@{$globals->mail->domain}') AS bestalias,
 +                                    q.core_mail_fmt AS mail_fmt, q.core_rss_hash
                                FROM  auth_user_md5   AS u
                          INNER JOIN  auth_user_quick AS q  USING(user_id)
 -                        INNER JOIN  aliases         AS a  ON (u.user_id = a.id AND a.type = \'a_vie\')
 -                        INNER JOIN  aliases         AS a2 ON (u.user_id = a2.id AND FIND_IN_SET(\'bestalias\', a2.flags))
 -                             WHERE  u.user_id = {?} AND u.perms IN(\'admin\', \'user\')
 -                             LIMIT  1', $user);
 +                        INNER JOIN  aliases         AS a  ON (u.user_id = a.id AND a.type = 'a_vie')
 +                        INNER JOIN  aliases         AS a2 ON (u.user_id = a2.id AND FIND_IN_SET('bestalias', a2.flags))
 +                             WHERE  u.user_id = {?} AND u.perms IN('admin', 'user')
 +                             LIMIT  1", $user);
          $sess = $res->fetchOneAssoc();
          $perms = $sess['perms'];
          unset($sess['perms']);
          $_SESSION = array_merge($_SESSION, $sess);
 -        $this->makePerms($perms);
 +        S::set('perms', User::makePerms($perms));
          S::kill('challenge');
          S::kill('loginX');
          S::kill('may_update');
          return true;
      }
  
-     public function tokenAuth($login, $token)
-     {
-         $res = XDB::query('SELECT  u.hruid
-                              FROM  aliases         AS a
-                        INNER JOIN  auth_user_md5   AS u ON (a.id = u.user_id AND u.perms IN ("admin", "user"))
-                        INNER JOIN  auth_user_quick AS q ON (a.id = q.user_id AND q.core_rss_hash = {?})
-                             WHERE  a.alias = {?} AND a.type != "homonyme"', $token, $login);
-         if ($res->numRows() == 1) {
-             $data = $res->fetchOneAssoc();
-             return new User($res->fetchOneCell());
-         }
-         return null;
-     }
      public function doSelfSuid()
      {
          if (!$this->startSUID(S::i('uid'))) {
              return false;
          }
 -        $this->makePerms('user');
 +        S::set('perms', User::makePerms('user'));
          return true;
      }
  
          S::set('perms', $suid['perms']);
          return true;
      }
-     public function makePerms($perm)
-     {
-         $flags = new PlFlagSet();
-         if ($perm == 'disabled' || $perm == 'ext') {
-             S::set('perms', $flags);
-             return;
-         }
-         $flags->addFlag(PERMS_USER);
-         if ($perm == 'admin') {
-             $flags->addFlag(PERMS_ADMIN);
-         }
-         S::set('perms', $flags);
-     }
-     public function loggedLevel()
-     {
-         return AUTH_COOKIE;
-     }
-     public function sureLevel()
-     {
-         return AUTH_MDP;
-     }
  }
  
  // {{{ function may_update
@@@ -32,9 -32,9 +32,9 @@@ function user_clear_all_subs($user_id, 
      // + delete maillists
  
      global $globals;
 -    $uid   = intval($user_id);
 -    $res   = XDB::query("SELECT alias FROM aliases WHERE type='a_vie' AND id={?}", $uid);
 -    $alias = $res->fetchOneCell();
 +    $uid = intval($user_id);
 +    $user = User::getSilent($uid);
 +    list($alias) = explode('@', $user->forlifeEmail());
  
      $tables_to_clear = array('uid' => array('competences_ins', 'entreprises', 'langues_ins', 'mentor_pays',
                                              'mentor_secteurs', 'mentor', 'perte_pass', 'watch_sub'),
      if ($globals->mailstorage->googleapps_domain) {
          require_once 'googleapps.inc.php';
          if (GoogleAppsAccount::account_status($uid)) {
 -            $account = new GoogleAppsAccount($uid, $alias);
 +            $account = new GoogleAppsAccount($user);
              $account->suspend();
          }
      }
  }
  
  // }}}
 -// {{{ function get_user_login()
 -
 -// Defaut callback to call when a login is not found
 -function _default_user_callback($login)
 -{
 -    Platal::page()->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login");
 -    return;
 -}
 -
 -function _silent_user_callback($login)
 -{
 -    return;
 -}
 -
 -function get_user_login($data, $get_forlife = false, $callback = '_default_user_callback')
 -{
 -    global $globals;
 -
 -    if (is_numeric($data)) {
 -        $res = XDB::query("SELECT alias FROM aliases WHERE type='a_vie' AND id={?}", $data);
 -        if ($res->numRows()) {
 -            return $res->fetchOneCell();
 -        } else {
 -            call_user_func($callback, $data);
 -            return false;
 -        }
 -    }
 -
 -    $data = trim(strtolower($data));
 -
 -    if (strstr($data, '@')===false) {
 -        $data = $data.'@'.$globals->mail->domain;
 -    }
 -
 -    list($mbox, $fqdn) = explode('@', $data);
 -    if ($fqdn == $globals->mail->domain || $fqdn == $globals->mail->domain2) {
 -
 -        $res = XDB::query("SELECT  a.alias
 -                             FROM  aliases AS a
 -                       INNER JOIN  aliases AS b ON (a.id = b.id AND b.type IN ('alias', 'a_vie') AND b.alias={?})
 -                            WHERE  a.type = 'a_vie'", $mbox);
 -        if ($res->numRows()) {
 -            return $get_forlife ? $res->fetchOneCell() : $mbox;
 -        }
 -
 -        if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) {
 -            $res = XDB::query("SELECT  a.alias
 -                                 FROM  aliases AS a
 -                           INNER JOIN  aliases AS b ON (a.id = b.id AND b.type IN ('alias', 'a_vie') AND b.alias={?})
 -                           INNER JOIN  auth_user_md5 AS u ON (a.id = u.user_id AND promo = {?})
 -                                WHERE  a.type = 'a_vie'", $matches[1], $matches[2]);
 -            if ($res->numRows() == 1) {
 -                return $res->fetchOneCell();
 -            }
 -        }
 -        call_user_func($callback, $data);
 -        return false;
 -
 -    } elseif ($fqdn == $globals->mail->alias_dom || $fqdn == $globals->mail->alias_dom2) {
 -
 -        $res = XDB::query("SELECT  redirect
 -                             FROM  virtual_redirect
 -                       INNER JOIN  virtual USING(vid)
 -                            WHERE  alias={?}", $mbox.'@'.$globals->mail->alias_dom);
 -        if ($redir = $res->fetchOneCell()) {
 -            list($alias) = explode('@', $redir);
 -        } else {
 -            call_user_func($callback, $data);
 -            $alias = false;
 -        }
 -        return $alias;
 -    } else {
 -
 -        $res = XDB::query("SELECT  alias
 -                                       FROM  aliases AS a
 -                                 INNER JOIN  emails  AS e ON e.uid=a.id
 -                                      WHERE  e.email={?} AND a.type='a_vie'", $data);
 -        switch ($i = $res->numRows()) {
 -            case 0:
 -                call_user_func($callback, $data);
 -                return false;
 -
 -            case 1:
 -                return $res->fetchOneCell();
 -
 -            default:
 -                if (S::has_perms()) {
 -                    $aliases = $res->fetchColumn();
 -                    Platal::page()->trigError("Il y a $i utilisateurs avec cette adresse mail : ".join(', ', $aliases));
 -                } else {
 -                    $res->free();
 -                }
 -        }
 -    }
 -
 -    return false;
 -}
 -
 -// }}}
 -// {{{ function get_user_forlife()
 -
 -function get_user_forlife($data, $callback = '_default_user_callback')
 -{
 -    return get_user_login($data, true, $callback);
 -}
 -
 -// }}}
 -// {{{ function get_users_forlife_list()
 -
 -function get_users_forlife_list($members, $strict = false, $callback = '_default_user_callback')
 -{
 -    if (!is_array($members)) {
 -        if (strlen(trim($members)) == 0) {
 -            return null;
 -        }
 -        $members = split("[; ,\r\n\|]+", $members);
 -    }
 -    if ($members) {
 -        $list = array();
 -        foreach ($members as $i => $alias) {
 -            $alias = trim($alias);
 -            if (empty($alias)) {
 -                continue;
 -            }
 -            if (($login = get_user_forlife($alias, $callback)) !== false) {
 -                $list[$i] = $login;
 -            } else if (!$strict) {
 -                $list[$i] = $alias;
 -            } else {
 -                global $globals;
 -                if (strpos($alias, '@') !== false) {
 -                    list($user, $dom) = explode('@', $alias);
 -                    if ($dom != $globals->mail->domain && $dom != $globals->mail->domain2) {
 -                        $list[$i] = $alias;
 -                    }
 -                }
 -            }
 -        }
 -        return $list;
 -    }
 -    return null;
 -}
 -
 -// }}}
  // {{{ function has_user_right()
  function has_user_right($pub, $view = 'private') {
      if ($pub == $view) return true;
@@@ -214,6 -358,7 +214,7 @@@ function get_user_details_adr($uid, $vi
                       gp.pays AS countrytxt,a.region, a.regiontxt,
                       FIND_IN_SET('active', a.statut) AS active, a.adrid,
                       FIND_IN_SET('res-secondaire', a.statut) AS secondaire,
+                      FIND_IN_SET('courrier', a.statut) AS courier,
                       a.pub, gp.display
                 FROM  adresses AS a
            LEFT JOIN  geoloc_pays AS gp ON (gp.a2=a.country)
@@@ -42,10 -42,10 +42,10 @@@ class ListeReq extends Validat
      // }}}
      // {{{ constructor
  
 -    public function __construct($_uid, $_asso, $_liste, $_domain, $_desc, $_advertise,
 +    public function __construct(User &$_user, $_asso, $_liste, $_domain, $_desc, $_advertise,
                                  $_modlevel, $_inslevel, $_owners, $_members, $_stamp=0)
      {
 -        parent::__construct($_uid, false, 'liste', $_stamp);
 +        parent::__construct($_user, false, 'liste', $_stamp);
  
          $this->asso      = $_asso;
          $this->liste     = $_liste;
  
      protected function _mail_subj()
      {
-         return "[Polytechnique.org/LISTES] Demande de la liste {$this->liste}";
+         return "[Polytechnique.org/LISTES] Demande de la liste {$this->liste}@{$this->domain}";
      }
  
      // }}}
      protected function _mail_body($isok)
      {
          if ($isok) {
-             return "  La liste de diffusion {$this->liste} que tu avais demandée vient d'être créée.";
+             return "  La liste de diffusion {$this->liste}@{$this->domain} que tu avais demandée vient d'être créée.";
          } else {
-             return "  La demande que tu avais faite pour la liste de diffusion {$this->liste} a été refusée.";
+             return "  La demande que tu avais faite pour la liste de diffusion {$this->liste}@{$this->domain} a été refusée.";
          }
      }
  
              return 1;
          }
  
 -        $list = new MMList(S::v('uid'), S::v('password'), $this->domain);
 +        $list = new MMList(S::user()->id(), S::v('password'), $this->domain);
          $ret = $list->create_list($this->liste, utf8_decode($this->desc), $this->advertise,
                                    $this->modlevel, $this->inslevel,
                                    $this->owners, $this->members);
@@@ -31,10 -31,10 +31,10 @@@ class MedalReq extends Validat
      // }}}
      // {{{ constructor
  
 -    public function __construct($_uid, $_idmedal, $_subidmedal, $_stamp=0)
 +    public function __construct(User &$_user, $_idmedal, $_subidmedal, $_stamp=0)
      {
 -        parent::__construct($_uid, false, 'medal', $_stamp);
 -        $this->mid  = $_idmedal;
 +        parent::__construct($_user, false, 'medal', $_stamp);
 +        $this->mid = $_idmedal;
          $this->gid = $_subidmedal;
      }
  
      public function commit ()
      {
          require_once 'notifs.inc.php';
-         register_watch_op($this->user->id(), WATCH_FICHE, 'medals');
 -        register_watch_op($this->uid, WATCH_FICHE, '', 'medals');
++        register_watch_op($this->user->id(), WATCH_FICHE, '', 'medals');
          return XDB::execute('REPLACE INTO  profile_medals_sub
                                     VALUES  ({?}, {?}, {?})',
 -                            $this->uid, $this->mid, $this->gid);
 +                            $this->user->id(), $this->mid, $this->gid);
      }
  
      // }}}
@@@ -44,24 -44,20 +44,24 @@@ class UsageReq extends Validat
      // }}}
      // {{{ constructor
  
 -    public function __construct($_uid, $_usage, $_reason)
 +    public function __construct(User &$_user, $_usage, $_reason)
      {
 -        parent::__construct($_uid, true, 'usage');
 -        $this->nom_usage  = $_usage;
 +        parent::__construct($_user, true, 'usage');
 +        $this->nom_usage = $_usage;
          $this->reason = $_reason;
 -        $this->alias   = make_username($this->prenom, $this->nom_usage);
 -        if (!$this->nom_usage) $this->alias = "";
 +
 +        $res = XDB::query("SELECT prenom FROM auth_user_md5 WHERE user_id = {?}", $this->user->id());
 +        $this->alias = make_username($res->fetchOneCell(), $this->nom_usage);
 +        if (!$this->nom_usage) {
 +            $this->alias = "";
 +        }
  
          $res = XDB::query("
                  SELECT  e.alias, u.nom_usage, a.id
                    FROM  auth_user_md5 as u
               LEFT JOIN  aliases       as e ON(e.type='alias' AND FIND_IN_SET('usage',e.flags) AND e.id = u.user_id)
               LEFT JOIN  aliases       as a ON(a.alias = {?} AND a.id != u.user_id)
 -                 WHERE  u.user_id = {?}", $this->alias, $this->uid);
 +                 WHERE  u.user_id = {?}", $this->alias, $this->user->id());
          list($this->oldalias, $this->oldusage, $this->homonyme) = $res->fetchOneRow();
      }
  
@@@ -97,7 -93,7 +97,7 @@@
              }
              if ($globals->mailstorage->googleapps_domain) {
                  require_once 'googleapps.inc.php';
 -                $account = new GoogleAppsAccount($this->uid, $this->forlife);
 +                $account = new GoogleAppsAccount($this->user);
                  if ($account->active()) {
                      $res .= "\n\n  Si tu utilises Google Apps, tu peux changer ton nom d'usage sur https://mail.google.com/a/polytechnique.org/#settings/accounts";
                  }
      public function commit()
      {
          require_once 'notifs.inc.php';
-         register_watch_op($this->user->id(), WATCH_FICHE, 'nom');
 -        register_watch_op($this->uid, WATCH_FICHE, '', 'nom');
++        register_watch_op($this->user->id(), WATCH_FICHE, '', 'nom');
          require_once('user.func.inc.php');
 -        $this->bestalias = set_new_usage($this->uid, $this->nom_usage, $this->alias);
 +        set_new_usage($this->user->id(), $this->nom_usage, $this->alias);
          return true;
      }
  
@@@ -43,9 -43,9 +43,9 @@@ class PhotoReq extends Validat
      // }}}
      // {{{ constructor
  
 -    public function __construct($_uid, PlUpload &$upload, $_stamp=0)
 +    public function __construct(User &$_user, PlUpload &$upload, $_stamp=0)
      {
 -        parent::__construct($_uid, true, 'photo', $_stamp);
 +        parent::__construct($_user, true, 'photo', $_stamp);
          $this->read($upload);
      }
  
@@@ -76,7 -76,7 +76,7 @@@
  
      static public function get_request($uid)
      {
 -        return parent::get_typed_request($uid,'photo');
 +        return parent::get_typed_request($uid, 'photo');
      }
  
      // }}}
      protected function handle_editor()
      {
          if (isset($_FILES['userfile'])) {
 -            $upload =& PlUpload::get($_FILES['userfile'], S::v('forlife'), 'photo');
 +            $upload =& PlUpload::get($_FILES['userfile'], S::user()->login(), 'photo');
              if (!$upload) {
                  $this->trigError('Une erreur est survenue lors du téléchargement du fichier');
                  return false;
          require_once 'notifs.inc.php';
          XDB::execute('REPLACE INTO  photo (uid, attachmime, attach, x, y)
                              VALUES  ({?},{?},{?},{?},{?})',
 -                     $this->uid, $this->mimetype, $this->data, $this->x, $this->y);
 -        register_watch_op($this->uid, WATCH_FICHE, '', 'photo');
 +                     $this->user->id(), $this->mimetype, $this->data, $this->x, $this->y);
-         register_watch_op($this->user->id(), WATCH_FICHE, 'photo');
++        register_watch_op($this->user->id(), WATCH_FICHE, '', 'photo');
          return true;
      }
  
diff --combined include/vcard.inc.php
  
  require_once('user.func.inc.php');
  
- class VCardIterator implements PlIterator
+ class VCard extends PlVCard
  {
      private $user_list = array();
      private $count     = 0;
      private $freetext  = null;
      private $photos    = true;
  
-     public function __construct($photos, $freetext)
+     public function __construct($photos = true, $freetext = null)
      {
+         PlVCard::$folding = false;
          $this->freetext = $freetext;
          $this->photos   = $photos;
      }
  
-     public function add_user($user)
+     public function addUser($user)
      {
 -        $forlife = get_user_forlife($user, '_silent_user_callback');
 -        if ($forlife) {
 -            $this->user_list[] = get_user_forlife($user);
 +        $user = User::getSilent($user);
 +        if ($user) {
 +            $this->user_list[] = $user;
              $this->count++;
          }
      }
  
-     public function first()
-     {
-         return count($this->user_list) == $this->count - 1;
-     }
-     public function last()
-     {
-         return count($this->user_list) == 0;
+     public function addUsers(array $users) {
+         foreach ($users as $user) {
+             $this->addUser($user);
+         }
      }
  
-     public function total()
+     protected function fetch()
      {
-         return $this->count;
+         return PlIteratorUtils::fromArray($this->user_list);
      }
  
-     public function next()
+     protected function buildEntry($entry)
      {
-         if (!$this->user_list) {
-             return null;
-         }
          global $globals;
-         $login = array_shift($this->user_list);
+         $login = $entry['value'];
 -        $user  = get_user_details($login);
 +        $user  = get_user_details($login->login());
  
-         if (strlen(trim($user['freetext']))) {
-             $user['freetext'] = pl_entity_decode($user['freetext']);
+         if (empty($user['nom_usage'])) {
+             $entry = new PlVCardEntry($user['prenom'], $user['nom'], null, null, @$user['nickname']);
+         } else {
+             $entry = new PlVCardEntry($user['prenom'], array($user['nom'], $user['nom_usage']), null, null, @$user['nickname']);
          }
+         // Free text
+         $freetext = '(' . $user['promo'] . ')';
          if ($this->freetext) {
-             if (strlen(trim($user['freetext']))) {
-                 $user['freetext'] = $this->freetext . "\n" . $user['freetext'];
-             } else {
-                 $user['freetext'] = $this->freetext;
+             $freetext .= "\n" . $this->freetext;
+         }
+         if (strlen(trim($user['freetext']))) {
+             $freetext .= "\n" . MiniWiki::WikiToText($user['freetext']);
+         }
+         $entry->set('NOTE', $freetext);
+         // Mobile
+         if (!empty($user['mobile'])) {
+             $entry->addTel(null, $user['mobile'], false, true, true, false, true, true);
+         }
+         // Emails
+         $entry->addMail(null, $user['bestalias'] . '@' . $globals->mail->domain, true);
+         $entry->addMail(null, $user['bestalias'] . '@' . $globals->mail->domain2);
+         if ($user['bestalias'] != $user['forlife']) {
+             $entry->addMail(null, $user['forlife'] . '@' . $globals->mail->domain);
+             $entry->addMail(null, $user['forlife'] . '@' . $globals->mail->domain2);
+         }
+         // Homes
+         foreach ($user['adr'] as $adr) {
+             $street = array($adr['adr1']);
+             if (!empty($adr['adr2'])) {
+                 $street[] = $adr['adr2'];
+             }
+             if (!empty($adr['adr3'])) {
+                 $street[] = $adr['adr3'];
+             }
+             $group = $entry->addHome($street, null, null, $adr['postcode'], $adr['city'], $adr['region'], @$adr['country'],
+                                      $adr['active'], $adr['courier'], $adr['courier']);
+             if (!empty($adr['tels'])) {
+                 foreach ($adr['tels'] as $tel) {
+                     $fax = $tel['tel_type'] == 'Fax';
+                     $entry->addTel($group, $tel['tel'], $fax, !$fax, !$fax, false, false, !$fax && $adr['active'] && empty($user['mobile']));
+                 }
+             }
+         }
+         // Pro
+         foreach ($user['adr_pro'] as $pro) {
+             $street = array($adr['adr1']);
+             if (!empty($pro['adr2'])) {
+                 $street[] = $pro['adr2'];
+             }
+             if (!empty($pro['adr3'])) {
+                 $street[] = $pro['adr3'];
+             }
+             $group = $entry->addWork($pro['entreprise'], null, $pro['poste'], $pro['fonction'],
+                                      $street, null, null, $pro['postcode'], $pro['city'], $pro['region'], @$pro['country']);
+             if (!empty($pro['tel'])) {
+                 $entry->addTel($group, $pro['tel']);
+             }
+             if (!empty($pro['fax'])) {
+                 $entry->addTel($group, $pro['fax'], true);
+             }
+             if (!empty($pro['email'])) {
+                 $entry->addMail($group, $pro['email']);
              }
          }
  
-         // alias virtual
+         // Melix
          $res = XDB::query(
                  "SELECT alias
                     FROM virtual
                  $user['user_id'],
                  $user['forlife'].'@'.$globals->mail->domain,
                  $user['forlife'].'@'.$globals->mail->domain2);
+         if ($res->numRows()) {
+             $entry->addMail(null, $res->fetchOneCell());
+         }
+         // Custom fields
+         if (count($user['gpxs_name'])) {
+             $entry->set('X-GROUPS', join(', ', $user['gpxs_name']));
+         }
+         if (count($user['binets'])) {
+             $entry->set('X-BINETS', join(', ', $user['binets']));
+         }
+         if (!empty($user['section'])) {
+             $entry->set('X-SECTION', $user['section']);
+         }
  
-         $user['virtualalias'] = $res->fetchOneCell();
-         $user['gpxs_vcardjoin'] = join(', ', array_map(array('VCard', 'text_encode'), $user['gpxs_name']));
-         $user['binets_vcardjoin'] = join(', ', array_map(array('VCard', 'text_encode'), $user['binets']));
-         // get photo
+         // Photo
          if ($this->photos) {
              $res = XDB::query(
 -                    "SELECT attach, attachmime
 -                       FROM photo   AS p
 -                 INNER JOIN aliases AS a ON (a.id = p.uid AND a.type = 'a_vie')
 -                      WHERE a.alias = {?}", $login);
 +                    "SELECT  attach, attachmime
 +                       FROM  photo AS p
 +                      WHERE  u.user_id = {?}", $login->id());
              if ($res->numRows()) {
-                 $user['photo'] = $res->fetchOneAssoc();
-             }
-         }
-         return $user;
-     }
- }
- class VCard
- {
-     static private $windows = false;
-     private $iterator = null;
-     public function __construct($users, $photos = true, $freetext = null)
-     {
-         $this->iterator = new VCardIterator($photos, $freetext);
-         VCard::$windows  = (strpos($_SERVER['HTTP_USER_AGENT'], 'Windows') !== false);
-         if (is_array($users)) {
-             foreach ($users as $user) {
-                 $this->iterator->add_user($user);
+                 list($data, $type) = $res->fetchOneRow();
+                 $entry->setPhoto($data, strtoupper($type));
              }
-         } else {
-             $this->iterator->add_user($users);
-         }
-     }
-     public static function escape($text)
-     {
-         if (VCard::$windows) {
-             return str_replace(';', '\\\\;', $text);
-         } else {
-             return str_replace(array(';', ','), array('\\\\;', '\\\\,'), $text);
-         }
-     }
-     public static function format_adr($params, &$smarty)
-     {
-         // $adr1, $adr2, $adr3, $postcode, $city, $region, $country
-         extract($params['adr']);
-         $adr = trim($adr1);
-         $adr = trim("$adr\n$adr2");
-         $adr = trim("$adr\n$adr3");
-         return VCard::text_encode(';;'
-                 . (VCard::$windows ? VCard::escape($adr) : $adr) . ';'
-                 . (VCard::$windows ? VCard::escape($city) : $city) . ';'
-                 . (VCard::$windows ? VCard::escape($region) : $region) . ';'
-                 . (VCard::$windows ? VCard::escape($postcode) : $postcode) . ';'
-                 . (VCard::$windows ? VCard::escape($country) : $country), false);
-     }
-     public static function text_encode($text, $escape = true)
-     {
-         if (is_array($text)) {
-             return implode(',', array_map(array('VCard', 'text_encode'), $text));
-         }
-         if ($escape) {
-             $text = VCard::escape($text);
          }
-         if (VCard::$windows) {
-             $text = utf8_decode($text);
-         }
-         return str_replace(array("\r\n", "\n", "\r"), '\n', $text);
+         return $entry;
      }
-     public function do_page(&$page)
-     {
-         $page->changeTpl('core/vcard.tpl', NO_SKIN);
-         $page->register_modifier('vcard_enc',  array($this, 'text_encode'));
-         $page->register_function('format_adr', array($this, 'format_adr'));
-         $page->assign_by_ref('users', $this->iterator);
-         header("Pragma: ");
-         header("Cache-Control: ");
-         header("Content-type: text/x-vcard; charset=UTF-8");
-   }
  }
  
  // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
diff --combined modules/carnet.php
@@@ -208,24 -208,39 +208,24 @@@ class CarnetModule extends PLModul
          }
          switch (Env::v('action')) {
              case 'retirer':
 -                if (is_numeric($user)) {
 -                    if (XDB::execute('DELETE FROM contacts
 -                                       WHERE uid = {?} AND contact = {?}',
 -                                     $uid, $user))
 -                    {
 -                        $page->trigSuccess("Contact retiré !");
 -                    }
 -                } else {
 -                    if (XDB::execute(
 -                                'DELETE FROM  c
 -                                       USING  contacts AS c
 -                                  INNER JOIN  aliases  AS a ON (c.contact=a.id and a.type!="homonyme")
 -                                       WHERE  c.uid = {?} AND a.alias={?}', $uid, $user))
 -                    {
 +                if (($user = User::get(Env::v('user')))) {
 +                    if (XDB::execute("DELETE FROM  contacts
 +                                            WHERE  uid = {?} AND contact = {?}", $uid, $user->id())) {
                          $page->trigSuccess("Contact retiré !");
                      }
                  }
                  break;
  
              case 'ajouter':
 -                require_once('user.func.inc.php');
 -                if (($login = get_user_login($user)) !== false) {
 -                    if (XDB::execute(
 -                                'REPLACE INTO  contacts (uid, contact)
 -                                       SELECT  {?}, id
 -                                         FROM  aliases
 -                                        WHERE  alias = {?}', $uid, $login))
 -                    {
 +                if (($user = User::get(Env::v('user')))) {
 +                    if (XDB::execute("REPLACE INTO  contacts (uid, contact)
 +                                            VALUES  ({?}, {?})", $uid, $user->id())) {
                          $page->trigSuccess('Contact ajouté !');
                      } else {
                          $page->trigWarning('Contact déjà dans la liste !');
                      }
                  }
 +                break;
          }
  
          $search = false;
          $res = XDB::query('SELECT contact
                               FROM contacts
                              WHERE uid = {?}', S::v('uid'));
-         $vcard = new VCard($res->fetchColumn(), $photos == 'photos');
-         $vcard->do_page(&$page);
+         $vcard = new VCard($photos == 'photos');
+         $vcard->addUsers($res->fetchColumn());
+         $vcard->show();
      }
  }
  
diff --combined modules/profile.php
@@@ -73,40 -73,35 +73,40 @@@ class ProfileModule extends PLModul
  
      function handler_photo(&$page, $x = null, $req = null)
      {
 -        if (is_null($x)) {
 +        if (!$x || !($user = User::getSilent($x))) {
              return PL_NOT_FOUND;
          }
  
 -        $res = XDB::query("SELECT id, pub FROM aliases
 -                                  LEFT JOIN photo ON(id = uid)
 -                                      WHERE alias = {?}", $x);
 -        list($uid, $photo_pub) = $res->fetchOneRow();
 +        // Retrieve the photo and its mime type.
 +        $photo_data = null;
 +        $photo_type = null;
  
          if ($req && S::logged()) {
              include 'validations.inc.php';
 -            $myphoto = PhotoReq::get_request($uid);
 -            Header('Content-type: image/'.$myphoto->mimetype);
 -            echo $myphoto->data;
 +            $myphoto = PhotoReq::get_request($user->id());
 +            if ($myphoto) {
 +                $photo_data = $myphoto->data;
 +                $photo_type = $myphoto->mimetype;
 +            }
          } else {
              $res = XDB::query(
 -                    "SELECT  attachmime, attach
 +                    "SELECT  attachmime, attach, pub
                         FROM  photo
 -                      WHERE  uid={?}", $uid);
 -
 -            if ((list($type, $data) = $res->fetchOneRow())
 -            &&  ($photo_pub == 'public' || S::logged())) {
 -                Header("Content-type: image/$type");
 -                echo $data;
 -            } else {
 -                Header('Content-type: image/png');
 -                echo file_get_contents(dirname(__FILE__).'/../htdocs/images/none.png');
 +                      WHERE  uid = {?}", $user->id());
 +            list($photo_type, $photo_data, $photo_pub) = $res->fetchOneRow();
 +            if ($photo_pub != 'public' && !S::logged()) {
 +                $photo_type = $photo_data = null;
              }
          }
 +
 +        // Display the photo, or a default one when not available.
 +        if ($photo_type && $photo_data != null) {
 +            header('Content-type: image/' . $photo_type);
 +            echo $photo_data;
 +        } else {
 +            header('Content-type: image/png');
 +            echo file_get_contents(dirname(__FILE__).'/../htdocs/images/none.png');
 +        }
          exit;
      }
  
  
          require_once('validations.inc.php');
  
 -        $trombi_x = '/home/web/trombino/photos'.S::v('promo')
 -                    .'/'.S::v('forlife').'.jpg';
 -
 +        $trombi_x = '/home/web/trombino/photos' . S::v('promo') . '/' . S::user()->login() . '.jpg';
          if (Env::has('upload')) {
              S::assert_xsrf_token();
  
 -            $upload = new PlUpload(S::v('forlife'), 'photo');
 +            $upload = new PlUpload(S::user()->login(), 'photo');
              if (!$upload->upload($_FILES['userfile']) && !$upload->download(Env::v('photo'))) {
                  $page->trigError('Une erreur est survenue lors du téléchargement du fichier');
              } else {
 -                $myphoto = new PhotoReq(S::v('uid'), $upload);
 +                $myphoto = new PhotoReq(S::user(), $upload);
                  if ($myphoto->isValid()) {
                      $myphoto->submit();
                  }
          } elseif (Env::has('trombi')) {
              S::assert_xsrf_token();
  
 -            $upload = new PlUpload(S::v('forlife'), 'photo');
 +            $upload = new PlUpload(S::user()->login(), 'photo');
              if ($upload->copyFrom($trombi_x)) {
 -                $myphoto = new PhotoReq(S::v('uid'), $upload);
 +                $myphoto = new PhotoReq(S::user(), $upload);
                  if ($myphoto->isValid()) {
                      $myphoto->commit();
                      $myphoto->clean();
  
      function handler_profile(&$page, $x = null)
      {
 +        // TODO/note for upcoming developers:
 +        // We currently maintain both $user and $login; $user is the old way of
 +        // obtaining information, and eventually everything will be loaded
 +        // through $login. That is the reason why in the template $user is named
 +        // $x, and $login $user (sorry for the confusion).
 +
 +        // Determines which user to display the profile of, and retrieves basic
 +        // information on this user.
          if (is_null($x)) {
              return PL_NOT_FOUND;
          }
  
 -        global $globals;
 -        require_once 'user.func.inc.php';
 +        $login = S::logged() ? User::get($x) : User::getSilent($x);
 +        if (!$login) {
 +            return PL_NOT_FOUND;
 +        }
  
 +        // Now that we know this is the profile of an existing user, we can
 +        // switch to the appropriate template.
          $page->changeTpl('profile/profile.tpl', SIMPLE);
 +        require_once 'user.func.inc.php';
  
 -        $view = 'private';
 -        if (!S::logged() || Env::v('view') == 'public') $view = 'public';
 -        if (S::logged() && Env::v('view') == 'ax')      $view = 'ax';
 -
 -        if (is_numeric($x)) {
 -            $res = XDB::query(
 -                    "SELECT  alias
 -                       FROM  aliases       AS a
 -                 INNER JOIN  auth_user_md5 AS u ON (a.id=u.user_id AND a.type='a_vie')
 -                      WHERE  matricule={?}", $x);
 -            $login = $res->fetchOneCell();
 +        // Determines the access level at which the profile will be displayed.
 +        if (!S::logged() || Env::v('view') == 'public') {
 +            $view = 'public';
 +        } else if (S::logged() && Env::v('view') == 'ax') {
 +            $view = 'ax';
          } else {
 -            $login = get_user_forlife($x, S::logged() ? '_default_user_callback'
 -                                                      : '_silent_user_callback');
 +            $view = 'private';
          }
  
 -        if (empty($login)) {
 -            $user = get_not_registered_user($x, true);
 -            if ($user->total() != 1) {
 -                return PL_NOT_FOUND;
 -            }
 -            $user = $user->next();
 +        // Determines is the user is registered, and fetches the user infos in
 +        // the appropriate way.
 +        $res = XDB::query("SELECT  perms IN ('admin','user','disabled')
 +                             FROM  auth_user_md5
 +                            WHERE  user_id = {?}", $login->id());
 +        if ($res->fetchOneCell()) {
 +            $new  = Env::v('modif') == 'new';
 +            $user = get_user_details($login->login(), S::v('uid'), $view);
 +        } else {
 +            $new  = false;
 +            $user = array();
              if (S::logged()) {
 -                pl_redirect('marketing/public/' . $user['user_id']);
 +                pl_redirect('marketing/public/' . $login->login());
              }
 -            $user['forlife'] = $x;
 -        } else {
 -            $new   = Env::v('modif') == 'new';
 -            $user  = get_user_details($login, S::v('uid'), $view);
          }
  
 +        // Profile view are logged.
          if (S::logged()) {
 -            S::logger()->log('view_profile', $login);
 +            S::logger()->log('view_profile', $login->login());
          }
  
 -        $title = $user['prenom'] . ' ' . ( empty($user['nom_usage']) ? $user['nom'] : $user['nom_usage'] );
 -        $page->setTitle($title);
 +        // Sets the title of the html page.
 +        $page->setTitle($login->fullName());
  
 -        // photo
 -
 -        $photo = 'photo/'.$user['forlife'].($new ? '/req' : '');
 +        // Prepares the display of the user's mugshot.
 +        $photo = 'photo/' . $login->login() . ($new ? '/req' : '');
 +        if (!isset($user['photo_pub']) || !has_user_right($user['photo_pub'], $view)) {
 +            $photo = "";
 +        }
 +        $page->assign('photo_url', $photo);
  
          if (!isset($user['y']) and !isset($user['x'])) {
              list($user['x'], $user['y']) = getimagesize("images/none.png");
              $user['x'] = 160;
          }
  
 -        $page->assign('logged', has_user_right('private', $view));
 -        if (!has_user_right($user['photo_pub'], $view)) {
 -            $photo = "";
 -        }
 -
 -        $page->assign_by_ref('x', $user);
 -        $page->assign('photo_url', $photo);
 -        // alias virtual
 +        // Determines and displays the virtual alias.
 +        global $globals;
          $res = XDB::query(
 -                "SELECT alias
 -                   FROM virtual
 -             INNER JOIN virtual_redirect USING(vid)
 -             INNER JOIN auth_user_quick  ON ( user_id = {?} AND emails_alias_pub = 'public' )
 -                  WHERE ( redirect={?} OR redirect={?} )
 -                        AND alias LIKE '%@{$globals->mail->alias_dom}'",
 -                $user['user_id'],
 -                $user['forlife'].'@'.$globals->mail->domain,
 -                $user['forlife'].'@'.$globals->mail->domain2);
 +                "SELECT  alias
 +                   FROM  virtual
 +             INNER JOIN  virtual_redirect USING (vid)
 +             INNER JOIN  auth_user_quick ON (user_id = {?} AND emails_alias_pub = 'public')
 +                  WHERE  (redirect={?} OR redirect={?})
 +                         AND alias LIKE '%@{$globals->mail->alias_dom}'",
 +                $login->id(),
 +                $login->forlifeEmail(),
 +                // TODO(vzanotti): get ride of all @m4x.org addresses in the
 +                // virtual redirect base, and remove this über-ugly hack.
 +                $login->login() . '@' . $globals->mail->domain2);
          $page->assign('virtualalias', $res->fetchOneCell());
 +
 +        // Adds miscellaneous properties to the display.
 +        // Adds the global user property array to the display.
 +        $page->assign_by_ref('x', $user);
 +        $page->assign_by_ref('user', $login);
 +        $page->assign('logged', has_user_right('private', $view));
          $page->assign('view', $view);
  
          $page->addJsLink('close_on_esc.js');
 -        header('Last-Modified: ' . date('r', strtotime($user['date'])));
 +        if (isset($user['date'])) {
 +            header('Last-Modified: ' . date('r', strtotime($user['date'])));
 +        }
      }
  
      function handler_ax(&$page, $user = null)
      {
 -        require_once 'user.func.inc.php';
 -        $user = get_user_forlife($user);
 +        $user = User::get($user);
          if (!$user) {
              return PL_NOT_FOUND;
          }
 -        $res = XDB::query('SELECT matricule_ax
 -                             FROM auth_user_md5 AS u
 -                       INNER JOIN aliases       AS a ON (a.type = "a_vie" AND a.id = u.user_id)
 -                            WHERE a.alias = {?}', $user);
 +
 +        $res = XDB::query("SELECT  matricule_ax
 +                             FROM  auth_user_md5
 +                            WHERE  user_id = {?}", $user->id());
          $mat = $res->fetchOneCell();
          if (!intval($mat)) {
 -            $page->kill("Le matricule AX de $user est inconnu");
 +            $page->kill("Le matricule AX de {$user->login()} est inconnu");
          }
          http_redirect("http://www.polytechniciens.com/?page=AX_FICHE_ANCIEN&anc_id=$mat");
      }
              $page->assign('promo_sortie', $promo_sortie);
  
              if (Env::has('submit')) {
 -                $myorange = new OrangeReq(S::v('uid'),
 -                                          $promo_sortie);
 +                $myorange = new OrangeReq(S::user(), $promo_sortie);
                  $myorange->submit();
                  $page->assign('myorange', $myorange);
              }
                  if ($reason == 'other') {
                      $reason = Env::v('other_reason');
                  }
 -                $myusage = new UsageReq(S::v('uid'), $nom_usage, $reason);
 +                $myusage = new UsageReq(S::user(), $nom_usage, $reason);
                  $myusage->submit();
                  $page->assign('myusage', $myusage);
              }
              $x = substr($x, 0, strlen($x) - 4);
          }
  
-         $vcard = new VCard($x);
-         $vcard->do_page($page);
+         $vcard = new VCard();
+         $vcard->addUser($x);
+         $vcard->show();
      }
  
 -    function handler_admin_trombino(&$page, $uid = null, $action = null) {
 +    function handler_admin_trombino(&$page, $login = null, $action = null) {
          $page->changeTpl('profile/admin_trombino.tpl');
          $page->setTitle('Administration - Trombino');
 -        $page->assign('uid', $uid);
  
 -        $q   = XDB::query(
 -                "SELECT  a.alias,promo
 -                  FROM  auth_user_md5 AS u
 -            INNER JOIN  aliases       AS a ON ( u.user_id = a.id AND type='a_vie' )
 -                 WHERE  user_id = {?}", $uid);
 -        list($forlife, $promo) = $q->fetchOneRow();
 +        if (!$login || !($user = User::get($login))) {
 +            return PL_NOT_FOUND;
 +        } else {
 +            $page->assign_by_ref('user', $user);
 +        }
  
          switch ($action) {
              case "original":
                  header("Content-type: image/jpeg");
 -              readfile("/home/web/trombino/photos".$promo."/".$forlife.".jpg");
 +              readfile("/home/web/trombino/photos" . $user->promo() . "/" . $user->login() . ".jpg");
                  exit;
                break;
  
                unlink($_FILES['userfile']['tmp_name']);
                  XDB::execute(
                          "REPLACE INTO photo SET uid={?}, attachmime = {?}, attach={?}, x={?}, y={?}",
 -                        $uid, $mimetype, $data, $x, $y);
 +                        $user->id(), $mimetype, $data, $x, $y);
                break;
  
              case "delete":
                  S::assert_xsrf_token();
  
 -                XDB::execute('DELETE FROM photo WHERE uid = {?}', $uid);
 +                XDB::execute('DELETE FROM photo WHERE uid = {?}', $user->id());
                  break;
          }
 -
 -        $page->assign('forlife', $forlife);
      }
      function handler_admin_binets(&$page, $action = 'list', $id = null) {
          $page->setTitle('Administration - Binets');
diff --combined modules/xnetgrp.php
@@@ -137,10 -137,11 +137,10 @@@ class XnetGrpModule extends PLModul
                              Env::i('unread'), S::i('uid'));
                  pl_redirect("#art" . Env::i('unread'));
              }
 -            $arts = XDB::iterator("SELECT a.*, u.nom, u.prenom, u.promo, l.alias AS forlife,
 +            $arts = XDB::iterator("SELECT a.*, u.nom, u.prenom, u.promo, u.hruid,
                                            FIND_IN_SET('photo', a.flags) AS photo
                                       FROM groupex.announces AS a
                                 INNER JOIN auth_user_md5 AS u USING(user_id)
 -                               INNER JOIN aliases AS l ON (u.user_id = l.id AND l.type = 'a_vie')
                                  LEFT JOIN groupex.announces_read AS r ON (r.user_id = {?} AND r.announce_id = a.id)
                                      WHERE asso_id = {?} AND peremption >= CURRENT_DATE()
                                            AND (promo_min = 0 OR promo_min <= {?})
                                $platal->ns . "rss/rss.xml");
          } else {
              $page->setRssLink("Polytechnique.net :: {$globals->asso("nom")} :: News",
 -                              $platal->ns . 'rss/'.S::v('forlife') .'/'.S::v('core_rss_hash').'/rss.xml');
 +                              $platal->ns . 'rss/'.S::v('hruid') .'/'.S::v('core_rss_hash').'/rss.xml');
          }
  
          $page->assign('articles', $arts);
              $this->load('mail.inc.php');
              set_time_limit(120);
              $tos = get_all_redirects($mbr,  $mls, $mmlist);
 -            $upload = PlUpload::get($_FILES['uploaded'], S::v('forlife'), 'xnet.emails', true);
 +            $upload = PlUpload::get($_FILES['uploaded'], S::user()->login(), 'xnet.emails', true);
              send_xnet_mails($from, $sujet, $body, Env::v('wiki'), $tos, Post::v('replyto'), $upload, @$_FILES['uploaded']['name']);
              if ($upload) {
                  $upload->rm();
          $res = XDB::query('SELECT  uid
                               FROM  groupex.membres
                              WHERE  asso_id = {?}', $globals->asso('id'));
-         $vcard = new VCard($res->fetchColumn(), $photos == 'photos', 'Membre du groupe ' . $globals->asso('nom'));
-         $vcard->do_page($page);
+         $vcard = new VCard($photos == 'photos', 'Membre du groupe ' . $globals->asso('nom'));
+         $vcard->addUsers($res->fetchColumn());
+         $vcard->show();
      }
  
      function handler_csv(&$page, $filename = null)
                       $globals->asso('id'), $uid);
      }
  
 -    private function validSubscription($nom, $prenom, $sexe, $uid, $forlife)
 +    private function validSubscription(User &$user)
      {
          global $globals;
 -        $this->removeSubscriptionRequest($uid);
 +        $this->removeSubscriptionRequest($user->id());
          XDB::execute("INSERT INTO  groupex.membres (asso_id, uid)
                             VALUES  ({?}, {?})",
 -                     $globals->asso('id'), $uid);
 +                     $globals->asso('id'), $user->id());
          $mailer = new PlMailer();
 -        $mailer->addTo("$forlife@polytechnique.org");
 -        $mailer->setFrom('"' . S::v('prenom') . ' ' . S::v('nom')
 -                         . '" <' . S::v('forlife') . '@polytechnique.org>');
 +        $mailer->addTo($user->forlifeEmail());
 +        $mailer->setFrom('"' . S::user()->fullName() . '" <' . S::user()->forlifeEmail() . '>');
          $mailer->setSubject('[' . $globals->asso('nom') . '] Demande d\'inscription');
 -        $message = ($sexe ? 'Chère' : 'Cher') . " Camarade,\n"
 +        $message = ($user->isFemale() ? 'Chère' : 'Cher') . " Camarade,\n"
                   . "\n"
                   . "  Suite à ta demande d'adhésion à " . $globals->asso('nom') . ",\n"
                   . "j'ai le plaisir de t'annoncer que ton inscription a été validée !\n"
                   . "\n"
                   . "Bien cordialement,\n"
                   . "-- \n"
 -                 . S::s('prenom') . ' ' . S::s('nom') . '.';
 +                 . S::user()->fullName() . '.';
          $mailer->setTxtBody($message);
          $mailer->send();
      }
                              ."sur la page de présentation.");
  
          if (!is_null($u) && may_update()) {
 -            $page->assign('u', $u);
 -            $res = XDB::query("SELECT  u.nom, u.prenom, u.promo, u.user_id, FIND_IN_SET('femme', u.flags), s.reason
 -                                 FROM  auth_user_md5 AS u
 -                           INNER JOIN  aliases AS al ON (al.id = u.user_id AND al.type != 'liste')
 -                            LEFT JOIN  groupex.membres_sub_requests AS s ON (u.user_id = s.uid AND s.asso_id = {?})
 -                                WHERE  al.alias = {?}", $globals->asso('id'), $u);
 -
 -            if (list($nom, $prenom, $promo, $uid, $sexe, $reason) = $res->fetchOneRow()) {
 -                $res = XDB::query("SELECT  COUNT(*)
 -                                     FROM  groupex.membres AS m
 -                               INNER JOIN  aliases  AS a ON (m.uid = a.id AND a.type != 'homonyme')
 -                                    WHERE  a.alias = {?} AND m.asso_id = {?}",
 -                                  $u, $globals->asso('id'));
 -                $n   = $res->fetchOneCell();
 -                if ($n) {
 -                    $this->removeSubscriptionRequest($uid);
 -                    $page->kill("$prenom $nom est déjà membre du groupe !");
 -                    return;
 -                } elseif (Env::has('accept')) {
 -                    S::assert_xsrf_token();
 -
 -                    $this->validSubscription($nom, $prenom, $sexe, $uid, $u);
 -                    pl_redirect("member/$u");
 -                } elseif (Env::has('refuse')) {
 -                    S::assert_xsrf_token();
 -
 -                    $this->removeSubscriptionRequest($uid);
 -                    $mailer = new PlMailer();
 -                    $mailer->addTo("$u@polytechnique.org");
 -                    $mailer->setFrom('"'.S::v('prenom').' '.S::v('nom')
 -                                     .'" <'.S::v('forlife').'@polytechnique.org>');
 -                    $mailer->setSubject('['.$globals->asso('nom').'] Demande d\'inscription annulée');
 -                    $mailer->setTxtBody(Env::v('motif'));
 -                    $mailer->send();
 -                    $page->kill("La demande de $prenom $nom a bien été refusée.");
 -                } else {
 -                    $page->assign('show_form', true);
 -                    $page->assign('prenom', $prenom);
 -                    $page->assign('nom', $nom);
 -                    $page->assign('promo', $promo);
 -                    $page->assign('uid', $uid);
 -                    $page->assign('reason', $reason);
 -                }
 -                return;
 +            $user = User::get($u);
 +            if (!$user) {
 +                return PL_NOT_FOUND;
 +            } else {
 +                $page->assign('user', $user);
              }
 -            return PL_NOT_FOUND;
 +
 +            // Retrieves the subscription status, and the reason.
 +            $res = XDB::query("SELECT  reason
 +                                 FROM  groupex.membres_sub_requests
 +                                WHERE  asso_id = {?} AND uid = {?}",
 +                              $globals->asso('id'), $user->id());
 +            $reason = ($res->numRows() ? $res->fetchOneCell() : null);
 +
 +            $res = XDB::query("SELECT  COUNT(*)
 +                                 FROM  groupex.membres
 +                                WHERE  asso_id = {?} AND uid = {?}",
 +                              $globals->asso('id'), $user->id());
 +            $already_member = ($res->fetchOneCell() > 0);
 +
 +            // Handles the membership request.
 +            if ($already_member) {
 +                $this->removeSubscriptionRequest($user->id());
 +                $page->kill($user->fullName() . " est déjà membre du groupe !");
 +            } elseif (Env::has('accept')) {
 +                S::assert_xsrf_token();
 +
 +                $this->validSubscription($user);
 +                pl_redirect("member/" . $user->login());
 +            } elseif (Env::has('refuse')) {
 +                S::assert_xsrf_token();
 +
 +                $this->removeSubscriptionRequest($user->id());
 +                $mailer = new PlMailer();
 +                $mailer->addTo($user->forlifeEmail());
 +                $mailer->setFrom('"' . S::user()->fullName() . '" <' . S::user()->forlifeEmail() . '>');
 +                $mailer->setSubject('['.$globals->asso('nom').'] Demande d\'inscription annulée');
 +                $mailer->setTxtBody(Env::v('motif'));
 +                $mailer->send();
 +                $page->kill("La demande de {$user->fullName()} a bien été refusée.");
 +            } else {
 +                $page->assign('show_form', true);
 +                $page->assign('reason', $reason);
 +            }
 +            return;
          }
  
          if (is_member()) {
              $append = "\n"
                      . "-- \n"
                      . "Ce message a été envoyé suite à la demande d'inscription de\n"
 -                    . S::v('prenom').' '.S::v('nom').' (X'.S::v('promo').")\n"
 +                    . S::user()->fullName() . ' (X' . S::v('promo') . ")\n"
                      . "Via le site www.polytechnique.net. Tu peux choisir de valider ou\n"
                      . "de refuser sa demande d'inscription depuis la page :\n"
 -                    .
 -                    "http://www.polytechnique.net/".$globals->asso("diminutif")."/subscribe/"
 -                        .S::v('forlife')."\n"
 +                    . "http://www.polytechnique.net/" . $globals->asso("diminutif") . "/subscribe/" . S::user()->login() . "\n"
                      . "\n"
                      . "En cas de problème, contacter l'équipe de Polytechnique.org\n"
                      . "à l'adresse : support@polytechnique.org\n";
  
              $mailer = new PlMailer();
              $mailer->addTo($to);
 -            $mailer->setFrom('"'.S::v('prenom').' '.S::v('nom')
 -                             .'" <'.S::v('forlife').'@polytechnique.org>');
 +            $mailer->setFrom('"' . S::user()->fullName() . '" <' . S::user()->forlifeEmail() . '>');
              $mailer->setSubject('['.$globals->asso('nom').'] Demande d\'inscription');
              $mailer->setTxtBody(Post::v('message').$append);
              $mailer->send();
              $subs = Post::v('subs');
              if (is_array($subs)) {
                  $users = array();
 -                foreach ($subs as $forlife => $val) {
 +                foreach ($subs as $hruid => $val) {
                      if ($val == '1') {
 -                        $res = XDB::query("SELECT  IF(u.nom_usage != '', u.nom_usage, u.nom) AS u,
 -                                                   u.prenom, FIND_IN_SET('femme', u.flags) AS sexe,
 -                                                   u.user_id
 -                                             FROM  auth_user_md5 AS u
 -                                       INNER JOIN  aliases AS a ON (a.id = u.user_id)
 -                                            WHERE  a.alias = {?}", $forlife);
 -                        if ($res->numRows() == 1) {
 -                            list($nom, $prenom, $sexe, $uid) = $res->fetchOneRow();
 -                            $this->validSubscription($nom, $prenom, $sexe, $uid, $forlife);
 +                        $user = User::get($hruid);
 +                        if ($user) {
 +                            $this->validSubscription($user);
                          }
                      }
                  }
          }
  
          $it = XDB::iterator("SELECT  IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom,
 -                                     u.prenom, u.promo, a.alias AS forlife, s.ts AS date
 +                                     u.prenom, u.promo, u.hruid, s.ts AS date
                                 FROM  groupex.membres_sub_requests AS s
                           INNER JOIN  auth_user_md5 AS u ON (s.uid = u.user_id)
 -                         INNER JOIN  aliases AS a ON (a.id = s.uid AND a.type = 'a_vie')
                                WHERE  asso_id = {?}
                             ORDER BY  nom, prenom",
                             $globals->asso('id'));
              S::assert_xsrf_token();
          }
  
 -        if (strpos($email, '@') === false) {
 -            $x = true;
 -        } else {
 -            list(,$fqdn) = explode('@', $email, 2);
 -            $fqdn = strtolower($fqdn);
 -            $x = ($fqdn == 'polytechnique.org' || $fqdn == 'melix.org' ||
 -                  $fqdn == 'm4x.org' || $fqdn == 'melix.net');
 -        }
 -        if ($x) {
 -            require_once 'user.func.inc.php';
 -            if ($forlife = get_user_forlife($email)) {
 -                XDB::execute(
 -                    'INSERT INTO  groupex.membres (uid,asso_id,origine)
 -                          SELECT  user_id,{?},"X"
 -                            FROM  auth_user_md5 AS u
 -                      INNER JOIN  aliases       AS a ON (u.user_id = a.id)
 -                           WHERE  a.alias={?}', $globals->asso('id'), $forlife);
 -                pl_redirect("member/$forlife");
 -            } else {
 -                $page->trigError($email." n'est pas un alias polytechnique.org valide.");
 +        if (!User::isForeignEmailAddress($email)) {
 +            $user = User::get($email);
 +            if ($user) {
 +                XDB::execute("REPLACE INTO  groupex.membres (uid, asso_id, origine)
 +                                    VALUES  ({?}, {?}, 'X')",
 +                             $user->id(), $globals->asso('id'));
 +                pl_redirect("member/" . $user->login());
              }
          } else {
              if (isvalid_email($email)) {
      function handler_unsubscribe(&$page)
      {
          $page->changeTpl('xnetgrp/membres-del.tpl');
 -        $user = get_infos(S::v('forlife'));
 +        $user = get_infos(S::user()->id());
          if (empty($user)) {
              return PL_NOT_FOUND;
          }
                  exit;
              }
          } else {
 -            $upload = new PlUpload(S::v('forlife'), 'xnetannounce');
 +            $upload = new PlUpload(S::user()->login(), 'xnetannounce');
              if ($upload->exists() && $upload->isType('image')) {
                  header('Content-Type: ' . $upload->contentType());
                  echo $upload->getContents();
              $art['nom']        = S::v('nom');
              $art['prenom']     = S::v('prenom');
              $art['promo']      = S::v('promo');
 -            $art['forlife']    = S::v('forlife');
 +            $art['hruid']      = S::user()->login();
              $art['peremption'] = Post::v('peremption');
              $art['public']     = Post::has('public');
              $art['xorg']       = Post::has('xorg');
              $art['nl']         = Post::has('nl');
              $art['event']      = Post::v('event');
 -            $upload     = new PlUpload(S::v('forlife'), 'xnetannounce');
 +            $upload     = new PlUpload(S::user()->login(), 'xnetannounce');
              $this->upload_image($page, $upload);
  
              $art['contact_html'] = $art['contacts'];
                  $post = null;/*
                  if ($globals->asso('forum')) {
                      require_once 'banana/forum.inc.php';
 -                    $banana = new ForumsBanana(S::v('forlife'));
 +                    $banana = new ForumsBanana(S::user());
                      $post = $banana->post($globals->asso('forum'), null,
                                            $art['titre'], MiniWiki::wikiToText($fulltext, false, 0, 80));
                  }*/
                  if ($art['xorg']) {
                      require_once('validations.inc.php');
                      $article = new EvtReq("[{$globals->asso('nom')}] " . $art['titre'], $fulltext,
 -                                    $art['promo_min'], $art['promo_max'], $art['peremption'], "", S::v('uid'),
 +                                    $art['promo_min'], $art['promo_max'], $art['peremption'], "", S::user(),
                                      $upload);
                      $article->submit();
                      $page->trigWarning("L'affichage sur la page d'accueil de Polytechnique.org est en attente de validation.");
                  }
                  if ($art['nl']) {
                      require_once('validations.inc.php');
 -                    $article = new NLReq(S::v('uid'), $globals->asso('nom') . " : " .$art['titre'],
 +                    $article = new NLReq(S::user(), $globals->asso('nom') . " : " .$art['titre'],
                                           $art['texte'], $art['contact_html']);
                      $article->submit();
                      $page->trigWarning("La parution dans la Lettre Mensuelle est en attente de validation.");
          }
  
          if (empty($art) && !is_null($aid)) {
 -            $res = XDB::query("SELECT a.*, u.nom, u.prenom, u.promo, l.alias AS forlife,
 +            $res = XDB::query("SELECT a.*, u.nom, u.prenom, u.promo, u.hruid,
                                        FIND_IN_SET('public', a.flags) AS public,
                                        FIND_IN_SET('photo', a.flags) AS photo
                                   FROM groupex.announces AS a
                             INNER JOIN auth_user_md5 AS u USING(user_id)
 -                           INNER JOIN aliases AS l ON (l.id = u.user_id AND l.type = 'a_vie')
                                  WHERE asso_id = {?} AND a.id = {?}",
                                $globals->asso('id'), $aid);
              if ($res->numRows()) {