Happy New Year!
[platal.git] / modules / register.php
index 2ce6266..936f8c7 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -49,30 +49,29 @@ class RegisterModule extends PLModule
         }
 
         if ($hash) {
-            require_once 'directory.enums.inc.php';
-
             $nameTypes = DirEnum::getOptions(DirEnum::NAMETYPES);
             $nameTypes = array_flip($nameTypes);
             $res = XDB::query("SELECT  a.uid, pd.promo, pnl.name AS lastname, pnf.name AS firstname, p.xorg_id AS xorgid,
-                                       p.birthdate_ref AS birthdateRef, FIND_IN_SET('watch', a.flags) AS watch, m.hash
+                                       p.birthdate_ref AS birthdateRef, FIND_IN_SET('watch', a.flags) AS watch, m.hash, a.type as eduType
                                  FROM  register_marketing AS m
                            INNER JOIN  accounts           AS a   ON (m.uid = a.uid)
-                           INNER JOIN  account_profiles   AS ap  ON (a.uid = ap.id AND FIND_IN_SET('owner', ap.perms))
-                           INNER JOIN  profiles           AS p   ON (p.pid = ap.id)
+                           INNER JOIN  account_profiles   AS ap  ON (a.uid = ap.uid AND FIND_IN_SET('owner', ap.perms))
+                           INNER JOIN  profiles           AS p   ON (p.pid = ap.pid)
                            INNER JOIN  profile_display    AS pd  ON (p.pid = pd.pid)
                            INNER JOIN  profile_name       AS pnl ON (p.pid = pnl.pid AND pnl.typeid = {?})
                            INNER JOIN  profile_name       AS pnf ON (p.pid = pnf.pid AND pnf.typeid = {?})
-                                WHERE  m.hash = {?}",
+                                WHERE  m.hash = {?} AND a.state = 'pending'",
                               $nameTypes['name_ini'], $nameTypes['firstname_ini'], $hash);
 
             if ($res->numRows() == 1) {
                 $subState->merge($res->fetchOneRow());
                 $subState->set('yearpromo', substr($subState->s('promo'), 1, 4));
 
-                XDB::execute('REPLACE INTO  register_mstats (uid,sender,success)
-                                    SELECT  m.uid, m.sender, 0
-                                      FROM  register_marketing AS m
-                                     WHERE  m.hash',
+                XDB::execute('INSERT INTO  register_mstats (uid, sender, success)
+                                   SELECT  m.uid, m.sender, 0
+                                     FROM  register_marketing AS m
+                                    WHERE  m.hash
+                  ON DUPLICATE KEY UPDATE  sender = VALUES(sender), success = VALUES(success)',
                              $subState->s('hash'));
             }
         }
@@ -172,8 +171,13 @@ class RegisterModule extends PLModule
                     }
 
                     // Register the optional services requested by the user.
+                    if ($subState->v('eduType') == 'x') {
+                        $proposedServices = array('ax_letter', 'imap', 'ml_promo', 'nl');
+                    } else {
+                        $proposedServices = array('ax_letter', 'nl');
+                    }
                     $services = array();
-                    foreach (array('ax_letter', 'imap', 'ml_promo', 'nl') as $service) {
+                    foreach ($proposedServices as $service) {
                         if (Post::b($service)) {
                             $services[] = $service;
                         }
@@ -181,7 +185,7 @@ class RegisterModule extends PLModule
                     $subState->set('services', $services);
 
                     // Validate the password.
-                    if (!Post::v('response2', false)) {
+                    if (!Post::v('pwhash', false)) {
                         $error[] = "Le mot de passe n'est pas valide.";
                     }
 
@@ -199,7 +203,7 @@ class RegisterModule extends PLModule
                             $bannedEmail = true;
                         }
                     }
-                    if ($subState->has('watch')) {
+                    if ($subState->i('watch') != 0) {
                         $alert .= "Inscription d'un utilisateur surveillé - ";
                     }
 
@@ -213,7 +217,7 @@ class RegisterModule extends PLModule
                         $subState->set('birthdate', sprintf("%04d-%02d-%02d",
                                                             intval($birth[2]), intval($birth[1]), intval($birth[0])));
                         $subState->set('email', Post::t('email'));
-                        $subState->set('password', Post::t('response2'));
+                        $subState->set('password', Post::t('pwhash'));
 
                         // Update the current alert if the birthdate is incorrect,
                         // or if the IP address of the user has been banned.
@@ -234,7 +238,7 @@ class RegisterModule extends PLModule
                                  . " pour nous faire part de cette erreur.";
                         } else {
                             $subState->set('step', 4);
-                            if ($subState->count('backs') >= 3) {
+                            if ($subState->v('backs')->count() >= 3) {
                                 $alert .= "Fin d'une inscription hésitante.";
                             }
                             finishRegistration($subState);
@@ -250,7 +254,7 @@ class RegisterModule extends PLModule
         }
 
         $page->changeTpl('register/step' . $subState->i('step') . '.tpl');
-        $page->addJsLink('motdepasse.js');
+        $page->addJsLink('password.js');
         if (isset($error)) {
             $page->trigError($error);
         }
@@ -270,7 +274,6 @@ class RegisterModule extends PLModule
             return PL_FORBIDDEN;
         }
 
-        require_once 'directory.enums.inc.php';
         $nameTypes = DirEnum::getOptions(DirEnum::NAMETYPES);
         $nameTypes = array_flip($nameTypes);
 
@@ -279,15 +282,15 @@ class RegisterModule extends PLModule
         $res = XDB::query("SELECT  r.uid, p.pid, r.forlife, r.bestalias, r.mailorg2,
                                    r.password, r.email, r.services, r.naissance,
                                    pnl.name AS lastname, pnf.name AS firstname,
-                                   pd.promo, p.sex, p.birthdate_ref
+                                   pd.promo, p.sex, p.birthdate_ref, a.type AS eduType
                              FROM  register_pending AS r
                        INNER JOIN  accounts         AS a   ON (r.uid = a.uid)
                        INNER JOIN  account_profiles AS ap  ON (a.uid = ap.uid AND FIND_IN_SET('owner', ap.perms))
-                       INNER JOIN  profiles         AS p   ON (p.pid = ap.uid)
+                       INNER JOIN  profiles         AS p   ON (p.pid = ap.pid)
                        INNER JOIN  profile_name     AS pnl ON (p.pid = pnl.pid AND pnl.typeid = {?})
                        INNER JOIN  profile_name     AS pnf ON (p.pid = pnf.pid AND pnf.typeid = {?})
                        INNER JOIN  profile_display  AS pd  ON (p.pid = pd.pid)
-                            WHERE  hash = {?} AND hash != 'INSCRIT'",
+                            WHERE  hash = {?} AND hash != 'INSCRIT' AND a.state = 'pending'",
                           $nameTypes['name_ini'], $nameTypes['firstname_ini'], $hash);
         if (!$hash || $res->numRows() == 0) {
             $page->kill("<p>Cette adresse n'existe pas, ou plus, sur le serveur.</p>
@@ -305,7 +308,8 @@ class RegisterModule extends PLModule
         }
 
         list($uid, $pid, $forlife, $bestalias, $emailXorg2, $password, $email, $services,
-             $birthdate, $lastname, $firstname, $promo, $sex, $birthdate_ref) = $res->fetchOneRow();
+             $birthdate, $lastname, $firstname, $promo, $sex, $birthdate_ref, $eduType) = $res->fetchOneRow();
+        $isX = ($eduType == 'x');
         $yearpromo = substr($promo, 1, 4);
 
         // Prepare the template for display.
@@ -330,9 +334,10 @@ class RegisterModule extends PLModule
         //
         // Create the user account.
         //
+        XDB::startTransaction();
         XDB::execute("UPDATE  accounts
                          SET  password = {?}, state = 'active',
-                              registration_date = NOW()
+                              registration_date = NOW(), email = NULL
                        WHERE  uid = {?}", $password, $uid);
         XDB::execute("UPDATE  profiles
                          SET  birthdate = {?}, last_change = NOW()
@@ -345,12 +350,19 @@ class RegisterModule extends PLModule
             XDB::execute("INSERT INTO  aliases (uid, alias, type)
                                VALUES  ({?}, {?}, 'alias')", $uid, $emailXorg2);
         }
+        XDB::commit();
 
         // Add the registration email address as first and only redirection.
         require_once 'emails.inc.php';
         $user = User::getSilentWithUID($uid);
-        $redirect = new Redirect($user);
-        $redirect->add_email($email);
+        if ($isX) {
+            $redirect = new Redirect($user);
+            $redirect->add_email($email);
+        } else {
+            XDB::execute('UPDATE  accounts
+                             SET  email = {?}
+                           WHERE  uid = {?}', $email, $uid);
+        }
 
         // Try to start a session (so the user don't have to log in); we will use
         // the password available in Post:: to authenticate the user.
@@ -371,11 +383,16 @@ class RegisterModule extends PLModule
                     $r = XDB::query('SELECT id FROM groups WHERE diminutif = {?}', $yearpromo);
                     if ($r->numRows()) {
                         $asso_id = $r->fetchOneCell();
-                        XDB::execute('REPLACE INTO  group_members (uid, asso_id)
-                                            VALUES  ({?}, {?})',
+                        XDB::execute('INSERT IGNORE INTO  group_members (uid, asso_id)
+                                                  VALUES  ({?}, {?})',
                                      $uid, $asso_id);
-                        $mmlist = new MMList($uid, S::v('password'));
-                        $mmlist->subscribe("promo" . S::v('promo'));
+                        try {
+                            $mmlist = new MMList($user);
+                            $mmlist->subscribe("promo" . $yearpromo);
+                        } catch (Exception $e) {
+                            PlErrorReport::report($e);
+                            $page->trigError("L'inscription à la liste promo" . $yearpromo . " a échouée.");
+                        }
                     }
                     break;
                 case 'nl':
@@ -393,18 +410,25 @@ class RegisterModule extends PLModule
 
         // Congratulate our newly registered user by email.
         $mymail = new PlMailer('register/success.mail.tpl');
+        if ($isX) {
+            $mymail->addTo("\"{$user->fullName()}\" <{$user->forlifeEmail()}>");
+            $mymail->setSubject('Bienvenue parmi les X sur le web !');
+        } else {
+            $mymail->addTo($email);
+            $mymail->setSubject('Bienvenue sur Polytechnique.org !');
+        }
         $mymail->assign('forlife', $forlife);
         $mymail->assign('firstname', $firstname);
         $mymail->send();
 
         // Index the user, to allow her to appear in searches.
-        Profile::rebuildSearchTokens($uid);
+        Profile::rebuildSearchTokens($pid);
 
         // Notify other users which were watching for her arrival.
-        XDB::execute('REPLACE INTO  contacts (uid, contact)
-                            SELECT  uid, ni_id
-                              FROM  watch_nonins
-                             WHERE  ni_id = {?}', $uid);
+        XDB::execute('INSERT INTO  contacts (uid, contact)
+                           SELECT  uid, ni_id
+                             FROM  watch_nonins
+                            WHERE  ni_id = {?}', $uid);
         XDB::execute('DELETE FROM  watch_nonins
                             WHERE  ni_id = {?}', $uid);
         Platal::session()->updateNbNotifs();
@@ -452,12 +476,13 @@ class RegisterModule extends PLModule
                          SET  success = NOW()
                        WHERE  uid = {?}", $uid);
 
+        $market = array();
         while (list($senderid, $maketingEmails, $lastDate) = $res->next()) {
             $sender = User::getWithUID($senderid);
-            $market[] = " - par $sender->fullName() sur $maketingEmails (le plus récemment le $lastDate)";
+            $market[] = " - par {$sender->fullName()} sur $maketingEmails (le plus récemment le $lastDate)";
             $mymail = new PlMailer('register/marketer.mail.tpl');
             $mymail->setSubject("$firstname $lastname s'est inscrit à Polytechnique.org !");
-            $mymail->addTo("\"$sender->fullName()\" <$sender->bestEmail()@{$globals->mail->domain}>");
+            $mymail->addTo($sender);
             $mymail->assign('sender', $sender);
             $mymail->assign('firstname', $firstname);
             $mymail->assign('lastname', $lastname);
@@ -479,6 +504,7 @@ class RegisterModule extends PLModule
             $mymail->assign('birthdate_ref', $birthdate_ref);
             $mymail->assign('forlife', $forlife);
             $mymail->assign('email', $email);
+            $mymail->assign('logger', S::logger());
             if (count($market) > 0) {
                 $mymail->assign('market', implode("\n", $market));
             }