Minor improvement of keynote skin
[platal.git] / modules / register.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 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 'register/save' => $this->make_hook('save', AUTH_MDP),
32 );
33 }
34
35 function handler_register(&$page, $hash = null)
36 {
37 $alert = null;
38 $sub_state = S::v('sub_state', Array());
39 if (!isset($sub_state['step'])) {
40 $sub_state['step'] = 0;
41 }
42 if (!isset($sub_state['backs'])) {
43 $sub_state['backs'] = array();
44 }
45 if (Get::has('back') && Get::i('back') < $sub_state['step']) {
46 $sub_state['step'] = max(0,Get::i('back'));
47 $state = $sub_state;
48 unset($state['backs']);
49 $sub_state['backs'][] = $state;
50 if (count($sub_state['backs']) == 3) {
51 $alert .= "Tentative d'inscription tres hesitante - ";
52 }
53 }
54
55 // Compatibility with old sources, keep it atm
56 if (!$hash && Env::has('hash')) {
57 $hash = Env::v('hash');
58 }
59
60 if ($hash) {
61 $res = XDB::query(
62 "SELECT m.uid, u.promo, u.nom, u.prenom, u.matricule, u.naissance_ini, FIND_IN_SET('watch', u.flags)
63 FROM register_marketing AS m
64 INNER JOIN auth_user_md5 AS u ON u.user_id = m.uid
65 WHERE m.hash={?}", $hash);
66 if (list($uid, $promo, $nom, $prenom, $ourmat, $naiss, $watch) = $res->fetchOneRow()) {
67 $sub_state['uid'] = $uid;
68 $sub_state['hash'] = $hash;
69 $sub_state['promo'] = $promo;
70 $sub_state['nom'] = $nom;
71 $sub_state['prenom'] = $prenom;
72 $sub_state['ourmat'] = $ourmat;
73 $sub_state['watch'] = $watch;
74 $sub_state['naissance_ini'] = $naiss;
75
76 XDB::execute(
77 "REPLACE INTO register_mstats (uid,sender,success)
78 SELECT m.uid, m.sender, 0
79 FROM register_marketing AS m
80 WHERE m.hash", $sub_state['hash']);
81 }
82 }
83
84 switch ($sub_state['step']) {
85 case 0:
86 require_once('wiki.inc.php');
87 wiki_require_page('Reference.Charte');
88 if (Post::has('step1')) {
89 $sub_state['step'] = 1;
90 if (isset($sub_state['hash'])) {
91 $sub_state['step'] = 3;
92 require_once(dirname(__FILE__) . '/register/register.inc.php');
93 create_aliases($sub_state);
94 }
95 }
96 break;
97
98 case 1:
99 if (Post::has('promo')) {
100 $promo = Post::i('promo');
101 $res = XDB::query("SELECT COUNT(*)
102 FROM auth_user_md5
103 WHERE perms='pending' AND deces = '0000-00-00'
104 AND promo = {?}",
105 $promo);
106 if (!$res->fetchOneCell()) {
107 $err = "La promotion saisie est incorrecte ou tous les camardes de cette promo sont inscrits !";
108 } else {
109 $sub_state['step'] = 2;
110 $sub_state['promo'] = $promo;
111 if ($promo >= 1996 && $promo<2000) {
112 $sub_state['mat'] = ($promo % 100)*10 . '???';
113 } elseif($promo >= 2000) {
114 $sub_state['mat'] = 100 + ($promo % 100) . '???';
115 }
116 }
117 }
118 break;
119
120 case 2:
121 if (count($_POST)) {
122 require_once(dirname(__FILE__) . '/register/register.inc.php');
123 $sub_state['prenom'] = Post::v('prenom');
124 $sub_state['nom'] = Post::v('nom');
125 $sub_state['mat'] = Post::v('mat');
126 $err = check_new_user($sub_state);
127
128 if ($err !== true) { break; }
129 $err = create_aliases($sub_state);
130 if ($err === true) {
131 unset($err);
132 $sub_state['step'] = 3;
133 }
134 }
135 break;
136
137 case 3:
138 if (count($_POST)) {
139 require_once(dirname(__FILE__) . '/register/register.inc.php');
140 if (!isvalid_email(Post::v('email'))) {
141 $err[] = "Le champ 'E-mail' n'est pas valide.";
142 } elseif (!isvalid_email_redirection(Post::v('email'))) {
143 $err[] = $sub_state['forlife']." doit renvoyer vers un email existant ".
144 "valide, en particulier, il ne peut pas être renvoyé vers lui-même.";
145 }
146 $birth = trim(Env::v('naissance'));
147 if (!preg_match('/^[0-3][0-9][01][0-9][12][90][0-9][0-9]$/', $birth)) {
148 $err[] = "La 'Date de naissance' n'est pas correcte.";
149 } else {
150 $year = (int)substr($birth, 4, 4);
151 $promo = (int)$sub_state['promo'];
152 if ($year > $promo - 15 || $year < $promo - 30) {
153 $err[] = "La 'Date de naissance' n'est pas correcte.";
154 $alert = "Date de naissance incorrecte a l'inscription - ";
155 $sub_state['wrong_naissance'] = $birth;
156 }
157 }
158
159 // Check if the given email is known as dangerous
160 $res = Xdb::iterRow("SELECT w.state, w.description, a.alias
161 FROM emails AS e
162 INNER JOIN emails_watch AS w ON (e.email = w.email AND w.state != 'safe')
163 INNER JOIN aliases AS a ON (e.uid = a.id AND a.type = 'a_vie')
164 WHERE e.email = {?}
165 ORDER BY a.alias", Post::v('email'));
166 $aliases = array();
167 while(list($gstate, $gdescription, $alias) = $res->next()) {
168 $state = $gstate;
169 $description = $gdescription;
170 $aliases[] = $alias;
171 }
172 if (count($aliases) != 0) {
173 $alert .= "Email surveille propose a l'inscription - ";
174 }
175 if ($sub_state['watch']) {
176 $alter .= "Inscription d'un utilisateur surveillé - ";
177 }
178
179 if (check_ip('unsafe')) {
180 unset($err);
181 }
182
183 if (isset($err)) {
184 $err = join('<br />', $err);
185 } else {
186 $sub_state['naissance'] = sprintf("%s-%s-%s",
187 substr($birth,4,4),
188 substr($birth,2,2),
189 substr($birth,0,2));
190 if ($sub_state['naissance_ini'] != '0000-00-00' && $sub_state['naissance'] != $sub_state['naissance_ini']) {
191 $alert .= "Date de naissance incorrecte à l'inscription - ";
192 }
193 $sub_state['email'] = Post::v('email');
194 if (check_ip('unsafe')) {
195 $err = "Une erreur s'est produite lors de l'inscription."
196 . " Merci de contacter <a href='mailto:register@{$globals->mail->domain}>"
197 . " register@{$globals->mail->domain}</a>"
198 . " pour nous faire part de cette erreur";
199 $alert .= "Tentative d'inscription depuis une IP surveillee";
200 } else {
201 $sub_state['step'] = 4;
202 if (count($sub_state['backs']) >= 3) {
203 $alert .= "Fin d'une inscription hésitante";
204 }
205 finish_ins($sub_state);
206 }
207 }
208 }
209 break;
210 }
211
212 $_SESSION['sub_state'] = $sub_state;
213 if ($alert) {
214 send_warning_mail($alert);
215 }
216 $page->changeTpl('register/step'.intval($sub_state['step']).'.tpl');
217 if (isset($err)) {
218 $page->trig($err);
219 }
220 }
221
222 function handler_end_old(&$page)
223 {
224 return $this->handler_end($page, Env::v('hash'));
225 }
226
227 function handler_end(&$page, $hash = null)
228 {
229 global $globals;
230
231
232 $page->changeTpl('register/end.tpl');
233 $_SESSION['sub_state'] = array('step' => 5);
234
235 if (check_ip('unsafe')) {
236 send_warning_mail('Une IP surveillée a tenté de finaliser son inscription');
237 XDB::execute('DELETE FROM register_pending
238 WHERE hash = {?} AND hash != \'INSCRIT\'', $hash);
239 return PL_FORBIDDEN;
240 }
241
242 require_once('user.func.inc.php');
243
244 if ($hash) {
245 $res = XDB::query(
246 "SELECT r.uid, r.forlife, r.bestalias, r.mailorg2,
247 r.password, r.email, r.naissance, u.nom, u.prenom,
248 u.promo, FIND_IN_SET('femme', u.flags), u.naissance_ini
249 FROM register_pending AS r
250 INNER JOIN auth_user_md5 AS u ON r.uid = u.user_id
251 WHERE hash={?} AND hash!='INSCRIT'", $hash);
252 }
253
254 if (!$hash || !list($uid, $forlife, $bestalias, $mailorg2, $password, $email,
255 $naissance, $nom, $prenom, $promo, $femme, $naiss_ini) = $res->fetchOneRow())
256 {
257 $page->kill("<p>Cette adresse n'existe pas, ou plus, sur le serveur.</p>
258 <p>Causes probables :</p>
259 <ol>
260 <li>Vérifie que tu visites l'adresse du dernier
261 e-mail reçu s'il y en a eu plusieurs.</li>
262 <li>Tu as peut-être mal copié l'adresse reçue par
263 mail, vérifie-la à la main.</li>
264 <li>Tu as peut-être attendu trop longtemps pour
265 confirmer. Les pré-inscriptions sont annulées
266 tous les 30 jours.</li>
267 <li>Tu es en fait déjà inscrit.</li>
268 </ol>");
269 }
270
271
272
273 /***********************************************************/
274 /****************** REALLY CREATE ACCOUNT ******************/
275 /***********************************************************/
276
277 XDB::execute('UPDATE auth_user_md5
278 SET password={?}, perms="user",
279 date=NOW(), naissance={?}, date_ins = NOW()
280 WHERE user_id={?}', $password, $naissance, $uid);
281 XDB::execute('REPLACE INTO auth_user_quick (user_id) VALUES ({?})', $uid);
282 XDB::execute('INSERT INTO aliases (id,alias,type)
283 VALUES ({?}, {?}, "a_vie")', $uid,
284 $forlife);
285 XDB::execute('INSERT INTO aliases (id,alias,type,flags)
286 VALUES ({?}, {?}, "alias", "bestalias")',
287 $uid, $bestalias);
288 if ($mailorg2) {
289 XDB::execute('INSERT INTO aliases (id,alias,type)
290 VALUES ({?}, {?}, "alias")', $uid,
291 $mailorg2);
292 }
293
294 require_once('emails.inc.php');
295 $redirect = new Redirect($uid);
296 $redirect->add_email($email);
297
298 // on cree un objet logger et on log l'inscription
299 $logger = new CoreLogger($uid);
300 $logger->log('inscription', $email);
301
302 XDB::execute('UPDATE register_pending SET hash="INSCRIT" WHERE uid={?}', $uid);
303
304 global $platal;
305 $platal->on_subscribe($forlife, $uid, $promo, $password);
306
307 $mymail = new PlMailer('register/inscription.reussie.tpl');
308 $mymail->assign('forlife', $forlife);
309 $mymail->assign('prenom', $prenom);
310 $mymail->send();
311
312 require_once('user.func.inc.php');
313 user_reindex($uid);
314
315 // update number of subscribers (perms has changed)
316 update_NbIns();
317
318 if (!start_connexion($uid, false)) {
319 return PL_FORBIDDEN;
320 }
321 $_SESSION['auth'] = AUTH_MDP;
322
323 /***********************************************************/
324 /************* envoi d'un mail au démarcheur ***************/
325 /***********************************************************/
326 $res = XDB::iterRow(
327 "SELECT DISTINCT sa.alias, IF(s.nom_usage,s.nom_usage,s.nom) AS nom,
328 s.prenom, FIND_IN_SET('femme', s.flags) AS femme
329 FROM register_marketing AS m
330 INNER JOIN auth_user_md5 AS s ON ( m.sender = s.user_id )
331 INNER JOIN aliases AS sa ON ( sa.id = m.sender
332 AND FIND_IN_SET('bestalias', sa.flags) )
333 WHERE m.uid = {?}", $uid);
334 XDB::execute("UPDATE register_mstats SET success=NOW() WHERE uid={?}", $uid);
335
336 while (list($salias, $snom, $sprenom, $sfemme) = $res->next()) {
337 $mymail = new PlMailer();
338 $mymail->setSubject("$prenom $nom s'est inscrit à Polytechnique.org !");
339 $mymail->setFrom('"Marketing Polytechnique.org" <register@' . $globals->mail->domain . '>');
340 $mymail->addTo("\"$sprenom $snom\" <$salias@{$globals->mail->domain}>");
341 $msg = ($sfemme?'Chère':'Cher')." $sprenom,\n\n"
342 . "Nous t'écrivons pour t'informer que $prenom $nom (X$promo), "
343 . "que tu avais incité".($femme?'e':'')." à s'inscrire à Polytechnique.org, "
344 . "vient à l'instant de terminer son inscription.\n\n"
345 . "Merci de ta participation active à la reconnaissance de ce site !!!\n\n"
346 . "Bien cordialement,\n"
347 . "L'équipe Polytechnique.org";
348 $mymail->setTxtBody(wordwrap($msg, 72));
349 $mymail->send();
350 }
351
352 /**** send a mail to X.org administrators ****/
353 if ($globals->register->notif) {
354 $mymail = new PlMailer();
355 $mymail->setSubject("Inscription de $prenom $nom (X$promo)");
356 $mymail->setFrom('"Webmaster Polytechnique.org" <web@' . $globals->mail->domain . '>');
357 $mymail->addTo($globals->register->notif);
358 $msg = "$prenom $nom (X$promo) a terminé son inscription avec les données suivantes :\n"
359 . " - nom : $nom\n"
360 . " - prenom : $prenom\n"
361 . " - promo : $promo\n"
362 . " - naissance : $naissance (date connue : $naiss_ini)\n"
363 . " - forlife : $forlife\n"
364 . " - email : $email\n"
365 . " - sexe : $femme\n"
366 . " - ip : {$logger->ip} ({$logger->host})\n"
367 . ($logger->proxy_ip ? " - proxy : {$logger->proxy_ip} ({$logger->proxy_host})\n" : "");
368 $mymail->setTxtBody($msg);
369 $mymail->send();
370 }
371
372 Marketing::clear($uid);
373
374 pl_redirect('register/success');
375 $page->assign('uid', $uid);
376 }
377
378 function handler_success(&$page)
379 {
380 $page->changeTpl('register/success.tpl');
381
382 $_SESSION['sub_state'] = array('step' => 5);
383 if (Env::has('response2')) {
384 $_SESSION['password'] = $password = Post::v('response2');
385
386 XDB::execute('UPDATE auth_user_md5 SET password={?}
387 WHERE user_id={?}', $password,
388 S::v('uid'));
389
390 $log = S::v('log');
391 $log->log('passwd', '');
392
393 if (Cookie::v('ORGaccess')) {
394 require_once('secure_hash.inc.php');
395 setcookie('ORGaccess', hash_encrypt($password), (time()+25920000), '/', '' ,0);
396 }
397
398 $page->assign('mdpok', true);
399 }
400
401 $res = XDB::iterRow("SELECT sub, domain
402 FROM register_subs
403 WHERE uid = {?} AND type = 'list'
404 ORDER BY domain",
405 S::i('uid'));
406 $current_domain = null;
407 $lists = array();
408 while (list($sub, $domain) = $res->next()) {
409 if ($current_domain != $domain) {
410 $current_domain = $domain;
411 $client = new MMList(S::v('uid'), S::v('password'), $domain);
412 }
413 list($details, ) = $client->get_members($sub);
414 $lists["$sub@$domain"] = $details;
415 }
416 $page->assign_by_ref('lists', $lists);
417
418 $page->addJsLink('motdepasse.js');
419 }
420
421 function handler_save(&$page)
422 {
423 global $globals;
424
425 // Finish registration procedure
426 if (Post::v('register_from_ax_question')) {
427 XDB::execute('UPDATE auth_user_quick
428 SET profile_from_ax = 1
429 WHERE user_id = {?}',
430 S::v('uid'));
431 }
432 if (Post::v('add_to_nl')) {
433 require_once 'newsletter.inc.php';
434 NewsLetter::subscribe();
435 }
436 if (Post::v('add_to_ax')) {
437 require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
438 AXLetter::subscribe();
439 }
440 if (Post::v('add_to_promo')) {
441 $r = XDB::query('SELECT id FROM groupex.asso WHERE diminutif = {?}',
442 S::v('promo'));
443 $asso_id = $r->fetchOneCell();
444 XDB::execute('REPLACE INTO groupex.membres (uid,asso_id)
445 VALUES ({?}, {?})',
446 S::v('uid'), $asso_id);
447 $mmlist = new MMList(S::v('uid'), S::v('password'));
448 $mmlist->subscribe("promo".S::v('promo'));
449 }
450 if (Post::v('sub_ml')) {
451 $subs = array_keys(Post::v('sub_ml'));
452 $current_domain = null;
453 foreach ($subs as $list) {
454 list($sub, $domain) = explode('@', $list);
455 if ($domain != $current_domain) {
456 $current_domain = $domain;
457 $client = new MMList(S::v('uid'), S::v('password'), $domain);
458 }
459 $client->subscribe($sub);
460 }
461 }
462
463 pl_redirect('profile/edit');
464 }
465 }
466
467 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
468 ?>