Change École polytechnique favicon.
[platal.git] / modules / register / register.inc.php
index 4506edd..993ead3 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -19,9 +19,9 @@
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-// {{{ function checkId
+// {{{ function strongCheckId
 
-function checkId(&$subState)
+function strongCheckId($subState)
 {
     $subState->set('xorgid', Profile::getXorgId($subState->i('schoolid')));
     if (!$subState->v('xorgid')) {
@@ -29,66 +29,72 @@ function checkId(&$subState)
     }
 
     $uf = new UserFilter(new PFC_And(
-            new UFC_UID($subState->i('xorg_id')),
-            new PFC_Not(new UFC_Dead())
+            new UFC_SchoolId('xorg', $subState->i('xorgid')),
+            new PFC_Not(new UFC_Dead()),
+            new PFC_Not(new UFC_Registered(true))
     ));
-    $user = $uf->getProfiles();
-
-    if ($user->__get('state') == 'active') {
+    $profile = $uf->getProfile();
+    if (is_null($profile)) {
         return "Tu es déjà inscrit ou ton matricule est incorrect !";
     }
-    if ($user->promo() != $subState->s('promo')) {
+
+    if ($profile->promo() != $subState->s('promo')) {
         return 'Le matricule est incorrect.';
     }
 
-    if (!$user->compareNames($subState->s('firstname'), $subState->s('lastname'))) {
+    if (!$profile->compareNames($subState->s('firstname'), $subState->s('lastname'))) {
         return "Erreur dans l'identification. Réessaie, il y a une erreur quelque part !";
     }
 
-    $subState->set('lastname', $user->lastName());
-    $subState->set('firstname', $user->firstName());
-    $subState->set('uid', $user->id());
-    $subState->set('watch', $user->watch());
-    $subState->set('birthdateRef', $user->profile->__get('birthdate_ref'));
+    $subState->set('lastname', $profile->lastName());
+    $subState->set('firstname', $profile->firstName());
+    $subState->set('uid', $profile->owner()->id());
+    $subState->set('watch', $profile->owner()->watch);
+    $subState->set('comment', $profile->owner()->comment);
+    $subState->set('birthdateRef', $profile->__get('birthdate_ref'));
     return true;
 }
 
 // }}}
-// {{{ function checkOldId
+// {{{ function weakCheckId
 
-function checkOldId(&$subState)
+function weakCheckId($subState)
 {
     $uf = new UserFilter(new PFC_And(
             new PFC_Not(new UFC_Dead()),
-            new UFC_Promo('=', UserFilter::DISPLAY, $subState->s('promo')),
+            new UFC_Promo('=', $subState->s('edu_type'), $subState->s('yearpromo')),
             new PFC_Not(new UFC_Registered(true))
     ));
-    $it = $uf->iterProfiles();
-    while ($profile = $it->next()) {
-        if ($profile->compareNames($subState->s('firstname'), $subState->s('lastname'))) {
-            $subState->set('lastname', $profile->lastName());
-            $subState->set('firstname', $profile->firstName());
-            $subState->set('uid', $profile->owner()->id());
-            $subState->set('watch', $profile->owner()->watch());
-            $subState->set('birthdateRef', $profile->__get('birthdate_ref'));
-            $subState->set('xorgid', $profile->__get('xorg_id'));
-            return true;
+    if ($it = $uf->iterProfiles()) {
+        while ($profile = $it->next()) {
+            if ($profile->compareNames($subState->s('firstname'), $subState->s('lastname'))) {
+                $subState->set('lastname', $profile->lastName());
+                $subState->set('firstname', $profile->firstName());
+                $subState->set('uid', $profile->owner()->id());
+                $subState->set('watch', $profile->owner()->watch);
+                $subState->set('comment', $profile->owner()->comment);
+                $subState->set('birthdateRef', $profile->__get('birthdate_ref'));
+                $subState->set('xorgid', $profile->__get('xorg_id'));
+                return true;
+            }
         }
     }
 
     $uf = new UserFilter(new PFC_And(
             new PFC_Not(new UFC_Dead()),
-            new UFC_Promo('=', UserFilter::DISPLAY, $subState->s('promo')),
+            new UFC_Promo('=', $subState->s('edu_type'), $subState->s('yearpromo')),
             new UFC_Registered(true)
     ));
-    $it = $uf->iterProfiles();
-    while ($profile = $it->next()) {
-        if ($profile->compareNames($subState->s('firstname'), $subState->s('lastname'))) {
-            $subState->set('uid', $profile->owner()->id());
-            $subState->set('watch', $profile->owner()->watch());
-            $subState->set('birthdateRef', $profile->__get('birthdate_ref'));
-            $subState->set('xorgid', $profile->__get('xorg_id'));
-            return 'Tu es vraisemblablement déjà inscrit !';
+    if ($it = $uf->iterProfiles()) {
+        while ($profile = $it->next()) {
+            if ($profile->compareNames($subState->s('firstname'), $subState->s('lastname'))) {
+                $subState->set('uid', $profile->owner()->id());
+                $subState->set('watch', $profile->owner()->watch);
+                $subState->set('comment', $profile->owner()->comment);
+                $subState->set('birthdateRef', $profile->__get('birthdate_ref'));
+                $subState->set('xorgid', $profile->__get('xorg_id'));
+                return 'Tu es vraisemblablement déjà inscrit !';
+            }
         }
     }
     return 'Erreur : vérifie que tu as bien orthographié ton nom !';
@@ -97,7 +103,7 @@ function checkOldId(&$subState)
 // }}}
 // {{{ function checkNewUser
 
-function checkNewUser(&$subState)
+function checkNewUser($subState)
 {
     $firstname = preg_replace("/[ \t]+/", ' ', $subState->t('firstname'));
     $firstname = preg_replace("/--+/", '-', $firstname);
@@ -109,10 +115,10 @@ function checkNewUser(&$subState)
     $lastname = preg_replace("/''+/", '\'', $lastname);
     $subState->set('lastname', mb_strtoupper($lastname));
 
-    if ($subState->i('yearpromo') >= 1996) {
-        $res = checkId($subState);
+    if ($subState->v('edu_type') == Profile::DEGREE_X && $subState->i('yearpromo') >= 1996) {
+        $res = strongCheckId($subState);
     } else {
-        $res = checkOldId($subState);
+        $res = weakCheckId($subState);
     }
     if ($res !== true) {
         return $res;
@@ -124,49 +130,77 @@ function checkNewUser(&$subState)
 // }}}
 // {{{ function createAliases
 
-function createAliases(&$subState)
+function createAliases($subState)
 {
     global $globals;
 
-    $emailXorg  = PlUser::makeUserName($subState->t('firstname'), $subState->t('lastname'));
-    $emailXorg2 = $emailXorg . sprintf(".%02u", ($subState->i('yearpromo') % 100));
-
-    $res = XDB::query("SELECT  hruid
+    $res = XDB::query("SELECT  hruid, state, type
                          FROM  accounts
-                        WHERE  uid = {?} AND hruid != ''", $subState->i('uid'));
+                        WHERE  uid = {?} AND hruid != ''",
+                      $subState->i('uid'));
     if ($res->numRows() == 0) {
         return "Tu n'as pas d'adresse à vie pré-attribuée.<br />"
-            . "Envoie un mail à <a href=\"mailto:support@{$globals->mail->domain}</a>\">"
+            . "Envoie un mail à <a href=\"mailto:support@{$globals->mail->domain}\">"
             . "support@{$globals->mail->domain}</a> en expliquant ta situation.";
     } else {
-        $forlife = $res->fetchOneCell();
+        list($forlife, $state, $type) = $res->fetchOneRow();
+    }
+    if ($state == 'active') {
+        return "Tu es déjà inscrit, si tu ne te souviens plus de ton mot de passe d'accès au site, "
+             . "tu peux suivre <a href=\"recovery\">la procédure de récupération de mot de passe</a>.";
+    } else if ($state == 'disabled') {
+        return "Ton compte a été désactivé par les administrateurs du site suite à des abus. "
+             . "Pour plus d'information ou pour demander la réactivation du compte, tu peux t'adresser à "
+             . "<a href=\"mailto:support@{$globals->mail->domain}\">support@{$globals->mail->domain}</a>.";
     }
 
-    $res = XDB::query('SELECT  uid, type, expire
-                         FROM  aliases
-                        WHERE  alias = {?}', $emailXorg);
-    if ($res->numRows()) {
-        list($h_id, $h_type, $expire) = $res->fetchOneRow();
-        if ($h_type != 'homonyme' and empty($expire)) {
-            XDB::execute('UPDATE  aliases
-                             SET  expire = ADDDATE(NOW(), INTERVAL 1 MONTH)
-                           WHERE  alias = {?}', $emailXorg);
-            XDB::execute('REPLACE INTO  homonyms (homonyme_id, uid)
-                                VALUES  ({?}, {?})', $h_id, $h_id);
-            XDB::execute('REPLACE INTO  homonyms (homonyme_id, uid)
-                                VALUES  ({?}, {?})', $h_id, $uid);
-            $res = XDB::query('SELECT  alias
-                                 FROM  aliases
-                                WHERE  uid = {?} AND expire IS NULL', $h_id);
-            $als = $res->fetchColumn();
-
-            $mailer = new PlMailer('register/lostalias.mail.tpl');
-            $mailer->addTo($emailXorg . '@' . $globals->mail->domain);
-            $mailer->setSubject("Perte de ton alias $emailXorg dans un mois !");
-            $mailer->assign('emailXorg', $emailXorg);
-            $mailer->assign('als', join(', ', $als));
-            $mailer->SetTxtBody(wordwrap($msg,72));
-            $mailer->send();
+
+    $emailXorg  = PlUser::makeUserName($subState->t('firstname'), $subState->t('lastname'));
+    $suffix = (User::$sub_mail_domains[$type] ? substr(User::$sub_mail_domains[$type], 0, 1) : '') . substr($subState->v('yearpromo'), -2);
+    $emailXorg2 = $emailXorg . '.' . $suffix;
+    // Search for homonyms:
+    //  * first case: only one homonym already registered.
+    $res = XDB::query('SELECT  uid, expire
+                         FROM  email_source_account
+                        WHERE  email = {?} AND type != \'alias_aux\'',
+                      $emailXorg);
+    //  * second case: at least two homonyms registerd.
+    $result = XDB::query("SELECT  hrmid
+                            FROM  email_source_other
+                           WHERE  type = 'homonym' AND email = {?}",
+                         $emailXorg);
+    if ($res->numRows() || $result->numRows()) {
+        if ($res->numRows()) {
+            list($h_id, $expire) = $res->fetchOneRow();
+            if (empty($expire)) {
+                XDB::execute('UPDATE  email_source_account
+                                 SET  expire = ADDDATE(NOW(), INTERVAL 1 MONTH)
+                               WHERE  email = {?} AND type != \'alias_aux\'',
+                             $emailXorg);
+                $hrmid = User::makeHomonymHrmid($emailXorg);
+                XDB::execute('INSERT IGNORE INTO  homonyms_list (hrmid, uid)
+                                          VALUES  ({?}, {?}), ({?}, {?})',
+                             $hrmid, $h_id, $hrmid, $subState->i('uid'));
+                $als = XDB::fetchColumn('SELECT  email
+                                           FROM  email_source_account
+                                          WHERE  uid = {?} AND type != \'alias_aux\' AND expire IS NULL',
+                                        $h_id);
+
+                $homonym = User::getSilentWithUID($h_id);
+                $mailer = new PlMailer('register/lostalias.mail.tpl');
+                $mailer->addTo($homonym);
+                $mailer->setSubject("Perte de ton alias $emailXorg dans un mois !");
+                $mailer->assign('emailXorg', $emailXorg);
+                $mailer->assign('als', join(', ', $als));
+                $mailer->SetTxtBody(wordwrap($msg,72));
+                $mailer->send();
+            }
+        } else {
+            $hrmid = $result->fetchOneCell();
+            XDB::execute('INSERT IGNORE INTO  homonyms_list (hrmid, uid)
+                                      VALUES  ({?}, {?})',
+                         $hrmid, $subState->i('uid'));
+            // TODO: insert into source_other if new domain
         }
 
         $subState->set('forlife', $forlife);
@@ -177,6 +211,7 @@ function createAliases(&$subState)
         $subState->set('bestalias', $emailXorg);
         $subState->set('emailXorg2', $emailXorg2);
     }
+    $subState->set('main_mail_domain', User::$sub_mail_domains[$type] . Platal::globals()->mail->domain);
 
     return true;
 }
@@ -189,22 +224,25 @@ function finishRegistration($subState)
     global $globals;
 
     $hash = rand_url_id(12);
-    XDB::execute('REPLACE INTO  register_pending (uid, forlife, bestalias, mailorg2, password,
-                                                  email, date, relance, naissance, hash, services)
-                        VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, NOW(), 0, {?}, {?}, {?})',
+    XDB::execute('INSERT INTO  register_pending (uid, forlife, bestalias, mailorg2, password,
+                                                 email, date, relance, naissance, hash, services)
+                              VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, NOW(), 0, {?}, {?}, {?})
+                              ON DUPLICATE KEY UPDATE password=VALUES(password), email=VALUES(email),
+                                                      date=VALUES(date), naissance=VALUES(naissance),
+                                                      hash=VALUES(hash), services=VALUES(services)',
                  $subState->i('uid'), $subState->s('forlife'), $subState->s('bestalias'),
                  $subState->s('emailXorg2'), $subState->s('password'), $subState->s('email'),
                  $subState->s('birthdate'), $hash, implode(',', $subState->v('services')));
 
     $mymail = new PlMailer('register/end.mail.tpl');
     $mymail->assign('emailXorg', $subState->s('bestalias'));
-    $mymail->assign('lemail', $subState->s('email'));
+    $mymail->assign('to', $subState->s('email'));
     $mymail->assign('baseurl', $globals->baseurl);
     $mymail->assign('hash', $hash);
-    $mymail->assign('subj', $subState->s('bestalias') . '@' . $globals->mail->domain);
+    $mymail->assign('subject', ucfirst($globals->mail->domain) . ' : ' . $subState->s('bestalias'));
     $mymail->send();
 }
 
 // }}}
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>