migrate events submission
[platal.git] / modules / register.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 class RegisterModule extends PLModule
23 {
24 function handlers()
25 {
26 return array(
27 'register' => $this->make_hook('register', AUTH_PUBLIC),
28 'register/end' => $this->make_hook('end', AUTH_PUBLIC),
29 'register/end.php' => $this->make_hook('end_old', AUTH_PUBLIC),
30 'register/success' => $this->make_hook('success', AUTH_MDP),
31 );
32 }
33
34 function handler_register(&$page, $hash = null)
35 {
36 global $globals;
37
38 $sub_state = Session::getMixed('sub_state', Array());
39 if (!isset($sub_state['step'])) {
40 $sub_state['step'] = 0;
41 }
42 if (Get::has('back') && Get::getInt('back') < $sub_state['step']) {
43 $sub_state['step'] = max(0,Get::getInt('back'));
44 }
45
46 // Compatibility with old sources, keep it atm
47 if (!$hash && Env::has('hash')) {
48 $hash = Env::get('hash');
49 }
50
51 if ($hash) {
52 $res = $globals->xdb->query(
53 "SELECT m.uid, u.promo, u.nom, u.prenom, u.matricule
54 FROM register_marketing AS m
55 INNER JOIN auth_user_md5 AS u ON u.user_id = m.uid
56 WHERE m.hash={?}", $hash);
57 if (list($uid, $promo, $nom, $prenom, $ourmat) = $res->fetchOneRow()) {
58 $sub_state['uid'] = $uid;
59 $sub_state['hash'] = $hash;
60 $sub_state['promo'] = $promo;
61 $sub_state['nom'] = $nom;
62 $sub_state['prenom'] = $prenom;
63 $sub_state['ourmat'] = $ourmat;
64
65 $globals->xdb->execute(
66 "REPLACE INTO register_mstats (uid,sender,success)
67 SELECT m.uid, m.sender, 0
68 FROM register_marketing AS m
69 WHERE m.hash", $sub_state['hash']);
70 }
71 }
72
73 switch ($sub_state['step']) {
74 case 0:
75 if (Post::has('step1')) {
76 $sub_state['step'] = 1;
77 if (isset($sub_state['hash'])) {
78 $sub_state['step'] = 3;
79 require_once('register.inc.php');
80 create_aliases($sub_state);
81 }
82 }
83 break;
84
85 case 1:
86 if (Post::has('promo')) {
87 $promo = Post::getInt('promo');
88 if ($promo < 1900 || $promo > date('Y')) {
89 $err = "La promotion saisie est incorrecte !";
90 } else {
91 $sub_state['step'] = 2;
92 $sub_state['promo'] = $promo;
93 if ($promo >= 1996 && $promo<2000) {
94 $sub_state['mat'] = ($promo % 100)*10 . '???';
95 } elseif($promo >= 2000) {
96 $sub_state['mat'] = 100 + ($promo % 100) . '???';
97 }
98 }
99 }
100 break;
101
102 case 2:
103 if (count($_POST)) {
104 require_once('register.inc.php');
105 $sub_state['prenom'] = Post::get('prenom');
106 $sub_state['nom'] = Post::get('nom');
107 $sub_state['mat'] = Post::get('mat');
108 $err = check_new_user($sub_state);
109
110 if ($err !== true) { break; }
111 $err = create_aliases($sub_state);
112 if ($err === true) {
113 unset($err);
114 $sub_state['step'] = 3;
115 }
116 }
117 break;
118
119 case 3:
120 if (count($_POST)) {
121 require_once('register.inc.php');
122 if (!isvalid_email(Post::get('email'))) {
123 $err[] = "Le champ 'E-mail' n'est pas valide.";
124 } elseif (!isvalid_email_redirection(Post::get('email'))) {
125 $err[] = $sub_state['forlife']." doit renvoyer vers un email existant ".
126 "valide, en particulier, il ne peut pas être renvoyé vers lui-même.";
127 }
128 if (!preg_match('/^[0-3][0-9][01][0-9][12][90][0-9][0-9]$/',
129 Post::get('naissance')))
130 {
131 $err[] = "La 'Date de naissance' n'est pas correcte.";
132 }
133
134 if (isset($err)) {
135 $err = join('<br />', $err);
136 } else {
137 $birth = Env::get('naissance');
138 $sub_state['naissance'] = sprintf("%s-%s-%s",
139 substr($birth,4,4),
140 substr($birth,2,2),
141 substr($birth,0,2));
142 $sub_state['email'] = Post::get('email');
143 $sub_state['step'] = 4;
144 finish_ins($sub_state);
145 }
146 }
147 break;
148 }
149
150 $_SESSION['sub_state'] = $sub_state;
151 $page->changeTpl('register/step'.intval($sub_state['step']).'.tpl');
152 $page->assign('simple', true);
153 if (isset($err)) {
154 $page->trig($err);
155 }
156
157 return PL_OK;
158 }
159
160 function handler_end_old(&$page)
161 {
162 return $this->handler_end($page, Env::get('hash'));
163 }
164
165 function handler_end(&$page, $hash = null)
166 {
167 global $globals;
168
169 $page->changeTpl('register/end.tpl');
170
171 require_once('user.func.inc.php');
172
173 if ($hash) {
174 $res = $globals->xdb->query(
175 "SELECT r.uid, r.forlife, r.bestalias, r.mailorg2,
176 r.password, r.email, r.naissance, u.nom, u.prenom,
177 u.promo, u.flags
178 FROM register_pending AS r
179 INNER JOIN auth_user_md5 AS u ON r.uid = u.user_id
180 WHERE hash={?} AND hash!='INSCRIT'", $hash);
181 }
182
183 if (!$hash || !list($uid, $forlife, $bestalias, $mailorg2, $password, $email,
184 $naissance, $nom, $prenom, $promo, $femme) = $res->fetchOneRow())
185 {
186 $page->kill("<p>Cette adresse n'existe pas, ou plus, sur le serveur.</p>
187 <p>Causes probables :</p>
188 <ol>
189 <li>Vérifie que tu visites l'adresse du dernier
190 e-mail reçu s'il y en a eu plusieurs.</li>
191 <li>Tu as peut-être mal copié l'adresse reçue par
192 mail, vérifie-la à la main.</li>
193 <li>Tu as peut-être attendu trop longtemps pour
194 confirmer. Les pré-inscriptions sont annulées
195 tous les 30 jours.</li>
196 <li>Tu es en fait déjà inscrit.</li>
197 </ol>");
198 }
199
200
201
202 /***********************************************************/
203 /****************** REALLY CREATE ACCOUNT ******************/
204 /***********************************************************/
205
206 $globals->xdb->execute('UPDATE auth_user_md5
207 SET password={?}, perms="user",
208 date=NOW(), naissance={?}, date_ins = NOW()
209 WHERE user_id={?}', $password, $naissance, $uid);
210 $globals->xdb->execute('REPLACE INTO auth_user_quick (user_id) VALUES ({?})', $uid);
211 $globals->xdb->execute('INSERT INTO aliases (id,alias,type)
212 VALUES ({?}, {?}, "a_vie")', $uid,
213 $forlife);
214 $globals->xdb->execute('INSERT INTO aliases (id,alias,type,flags)
215 VALUES ({?}, {?}, "alias", "bestalias")',
216 $uid, $bestalias);
217 if ($mailorg2) {
218 $globals->xdb->execute('INSERT INTO aliases (id,alias,type)
219 VALUES ({?}, {?}, "alias")', $uid,
220 $mailorg2);
221 }
222
223 require_once('emails.inc.php');
224 $redirect = new Redirect($uid);
225 $redirect->add_email($email);
226
227 // on cree un objet logger et on log l'inscription
228 $logger = new DiogenesCoreLogger($uid);
229 $logger->log('inscription', $email);
230
231 $globals->xdb->execute('UPDATE register_pending SET hash="INSCRIT" WHERE uid={?}', $uid);
232
233 $globals->hook->subscribe($forlife, $uid, $promo, $password);
234
235 require_once('xorg.mailer.inc.php');
236 $mymail = new XOrgMailer('register/inscription.reussie.tpl');
237 $mymail->assign('forlife', $forlife);
238 $mymail->assign('prenom', $prenom);
239 $mymail->send();
240
241 start_connexion($uid,false);
242 $_SESSION['auth'] = AUTH_MDP;
243
244 /***********************************************************/
245 /************* envoi d'un mail au démarcheur ***************/
246 /***********************************************************/
247 $res = $globals->xdb->iterRow(
248 "SELECT DISTINCT sa.alias, IF(s.nom_usage,s.nom_usage,s.nom) AS nom,
249 s.prenom, s.flags AS femme
250 FROM register_marketing AS m
251 INNER JOIN auth_user_md5 AS s ON ( m.sender = s.user_id )
252 INNER JOIN aliases AS sa ON ( sa.id = m.sender
253 AND FIND_IN_SET('bestalias', sa.flags) )
254 WHERE m.uid = {?}", $uid);
255 $globals->xdb->execute("UPDATE register_mstats SET success=NOW() WHERE uid={?}", $uid);
256
257 while (list($salias, $snom, $sprenom, $sfemme) = $res->next()) {
258 require_once('diogenes/diogenes.hermes.inc.php');
259 $mymail = new HermesMailer();
260 $mymail->setSubject("$prenom $nom s'est inscrit à Polytechnique.org !");
261 $mymail->setFrom('"Marketing Polytechnique.org" <register@polytechnique.org>');
262 $mymail->addTo("\"$sprenom $snom\" <$salias@{$globals->mail->domain}>");
263 $msg = ($sfemme?'Cher':'Chère')." $sprenom,\n\n"
264 . "Nous t'écrivons pour t'informer que {$prenom} {$nom} (X{$promo}), "
265 . "que tu avais incité".($femme?'e':'')." à s'inscrire à Polytechnique.org, "
266 . "vient à l'instant de terminer son inscription.\n\n"
267 . "Merci de ta participation active à la reconnaissance de ce site !!!\n\n"
268 . "Bien cordialement,\n"
269 . "L'équipe Polytechnique.org";
270 $mymail->setTxtBody(wordwrap($msg, 72));
271 $mymail->send();
272 }
273
274 $globals->xdb->execute("DELETE FROM register_marketing WHERE uid = {?}", $uid);
275
276 redirect($globals->baseurl.'/register/success');
277 $page->assign('uid', $uid);
278
279 return PL_OK;
280 }
281
282 function handler_success(&$page)
283 {
284 global $globals;
285
286 $page->changeTpl('register/success.tpl');
287
288 if (Env::has('response2')) {
289 $_SESSION['password'] = $password = Post::get('response2');
290
291 $globals->xdb->execute('UPDATE auth_user_md5 SET password={?}
292 WHERE user_id={?}', $password,
293 Session::getInt('uid'));
294
295 $log =& Session::getMixed('log');
296 $log->log('passwd', '');
297
298 if (Cookie::get('ORGaccess')) {
299 require_once('secure_hash.inc.php');
300 setcookie('ORGaccess', hash_encrypt($password), (time()+25920000), '/', '' ,0);
301 }
302
303 $page->assign('mdpok', true);
304 }
305
306 $page->addJsLink('javascript/motdepasse.js');
307
308 return PL_OK;
309 }
310 }
311
312 ?>