New evolutive marketing engine
[platal.git] / modules / register.php
CommitLineData
f59bc2fb 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 Polytechnique.org *
f59bc2fb 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
22class RegisterModule extends PLModule
23{
24 function handlers()
25 {
26 return array(
20d90835 27 'register' => $this->make_hook('register', AUTH_PUBLIC),
28 'register/end' => $this->make_hook('end', AUTH_PUBLIC),
2b6d403c 29 'register/end.php' => $this->make_hook('end_old', AUTH_PUBLIC),
f59bc2fb 30 'register/success' => $this->make_hook('success', AUTH_MDP),
31 );
32 }
33
20d90835 34 function handler_register(&$page, $hash = null)
f59bc2fb 35 {
5480a216 36 $alert = null;
cab08090 37 $sub_state = S::v('sub_state', Array());
f59bc2fb 38 if (!isset($sub_state['step'])) {
39 $sub_state['step'] = 0;
40 }
2efe5355 41 if (!isset($sub_state['backs'])) {
42 $sub_state['backs'] = array();
43 }
5e2307dc 44 if (Get::has('back') && Get::i('back') < $sub_state['step']) {
45 $sub_state['step'] = max(0,Get::i('back'));
2efe5355 46 $state = $sub_state;
47 unset($state['backs']);
48 $sub_state['backs'][] = $state;
49 if (count($sub_state['backs']) == 3) {
50 $alert .= "Tentative d'inscription tres hesitante - ";
51 }
f59bc2fb 52 }
53
2b6d403c 54 // Compatibility with old sources, keep it atm
55 if (!$hash && Env::has('hash')) {
5e2307dc 56 $hash = Env::v('hash');
2b6d403c 57 }
58
20d90835 59 if ($hash) {
08cce2ff 60 $res = XDB::query(
0be07aa6 61 "SELECT m.uid, u.promo, u.nom, u.prenom, u.matricule, FIND_IN_SET('watch', u.flags)
f59bc2fb 62 FROM register_marketing AS m
63 INNER JOIN auth_user_md5 AS u ON u.user_id = m.uid
20d90835 64 WHERE m.hash={?}", $hash);
0be07aa6 65 if (list($uid, $promo, $nom, $prenom, $ourmat, $watch) = $res->fetchOneRow()) {
f59bc2fb 66 $sub_state['uid'] = $uid;
20d90835 67 $sub_state['hash'] = $hash;
f59bc2fb 68 $sub_state['promo'] = $promo;
69 $sub_state['nom'] = $nom;
70 $sub_state['prenom'] = $prenom;
71 $sub_state['ourmat'] = $ourmat;
0be07aa6 72 $sub_state['watch'] = $watch;
f59bc2fb 73
08cce2ff 74 XDB::execute(
f59bc2fb 75 "REPLACE INTO register_mstats (uid,sender,success)
76 SELECT m.uid, m.sender, 0
77 FROM register_marketing AS m
78 WHERE m.hash", $sub_state['hash']);
79 }
80 }
81
82 switch ($sub_state['step']) {
83 case 0:
ab694f12 84 require_once('wiki.inc.php');
af3f07c5 85 wiki_require_page('Reference.Charte');
f59bc2fb 86 if (Post::has('step1')) {
87 $sub_state['step'] = 1;
88 if (isset($sub_state['hash'])) {
89 $sub_state['step'] = 3;
433336f3 90 require_once(dirname(__FILE__) . '/register/register.inc.php');
f59bc2fb 91 create_aliases($sub_state);
92 }
93 }
94 break;
95
96 case 1:
97 if (Post::has('promo')) {
5e2307dc 98 $promo = Post::i('promo');
a41bf2f8 99 $res = XDB::query("SELECT COUNT(*)
100 FROM auth_user_md5
101 WHERE perms='pending' AND deces = '0000-00-00'
102 AND promo = {?}",
103 $promo);
104 if (!$res->fetchOneCell()) {
105 $err = "La promotion saisie est incorrecte ou tous les camardes de cette promo sont inscrits !";
f59bc2fb 106 } else {
107 $sub_state['step'] = 2;
108 $sub_state['promo'] = $promo;
109 if ($promo >= 1996 && $promo<2000) {
110 $sub_state['mat'] = ($promo % 100)*10 . '???';
111 } elseif($promo >= 2000) {
112 $sub_state['mat'] = 100 + ($promo % 100) . '???';
113 }
114 }
115 }
116 break;
117
118 case 2:
119 if (count($_POST)) {
433336f3 120 require_once(dirname(__FILE__) . '/register/register.inc.php');
5e2307dc 121 $sub_state['prenom'] = Post::v('prenom');
122 $sub_state['nom'] = Post::v('nom');
123 $sub_state['mat'] = Post::v('mat');
f59bc2fb 124 $err = check_new_user($sub_state);
125
126 if ($err !== true) { break; }
127 $err = create_aliases($sub_state);
128 if ($err === true) {
129 unset($err);
130 $sub_state['step'] = 3;
131 }
132 }
133 break;
134
135 case 3:
136 if (count($_POST)) {
433336f3 137 require_once(dirname(__FILE__) . '/register/register.inc.php');
5e2307dc 138 if (!isvalid_email(Post::v('email'))) {
f59bc2fb 139 $err[] = "Le champ 'E-mail' n'est pas valide.";
5e2307dc 140 } elseif (!isvalid_email_redirection(Post::v('email'))) {
f59bc2fb 141 $err[] = $sub_state['forlife']." doit renvoyer vers un email existant ".
a7de4ef7 142 "valide, en particulier, il ne peut pas être renvoyé vers lui-même.";
f59bc2fb 143 }
1970c12b 144 $birth = trim(Env::v('naissance'));
35cd1be1 145 if (!preg_match('/^[0-3][0-9][01][0-9][12][90][0-9][0-9]$/', $birth)) {
f59bc2fb 146 $err[] = "La 'Date de naissance' n'est pas correcte.";
35cd1be1 147 } else {
148 $year = (int)substr($birth, 4, 4);
149 $promo = (int)$sub_state['promo'];
150 if ($year > $promo - 15 || $year < $promo - 30) {
151 $err[] = "La 'Date de naissance' n'est pas correcte.";
5480a216 152 $alert = "Date de naissance incorrecte a l'inscription - ";
35cd1be1 153 }
f59bc2fb 154 }
155
bf273d6a 156 // Check if the given email is known as dangerous
157 $res = Xdb::iterRow("SELECT w.state, w.description, a.alias
158 FROM emails AS e
159 INNER JOIN emails_watch AS w ON (e.email = w.email AND w.state != 'safe')
160 INNER JOIN aliases AS a ON (e.uid = a.id AND a.type = 'a_vie')
161 WHERE e.email = {?}
162 ORDER BY a.alias", Post::v('email'));
163 $aliases = array();
164 while(list($gstate, $gdescription, $alias) = $res->next()) {
165 $state = $gstate;
166 $description = $gdescription;
167 $aliases[] = $alias;
168 }
169 if (count($aliases) != 0) {
5480a216 170 $alert .= "Email surveille propose a l'inscription - ";
171 }
0be07aa6 172 if ($sub_state['watch']) {
173 $alter .= "Inscription d'un utilisateur surveillé - ";
174 }
5480a216 175
176 if (check_ip('unsafe')) {
177 unset($err);
bf273d6a 178 }
179
f59bc2fb 180 if (isset($err)) {
181 $err = join('<br />', $err);
182 } else {
f59bc2fb 183 $sub_state['naissance'] = sprintf("%s-%s-%s",
184 substr($birth,4,4),
185 substr($birth,2,2),
186 substr($birth,0,2));
5e2307dc 187 $sub_state['email'] = Post::v('email');
5480a216 188 if (check_ip('unsafe')) {
189 $err = "Une erreur s'est produite lors de l'inscription."
190 . " Merci de contacter <a href='register@polytechnique.org'>register@polytechnique.org</a>"
191 . " pour nous faire part de cette erreur";
192 $alert .= "Tentative d'inscription depuis une IP surveillee";
193 } else {
194 $sub_state['step'] = 4;
9fc2d0d2 195 if (count($sub_state['backs']) >= 3) {
a7de4ef7 196 $alert .= "Fin d'une inscription hésitante";
2efe5355 197 }
5480a216 198 finish_ins($sub_state);
199 }
bf273d6a 200 }
f59bc2fb 201 }
202 break;
203 }
204
205 $_SESSION['sub_state'] = $sub_state;
5480a216 206 if ($alert) {
207 send_warning_mail($alert);
208 }
a41bf2f8 209 $page->changeTpl('register/step'.intval($sub_state['step']).'.tpl');
f59bc2fb 210 if (isset($err)) {
211 $page->trig($err);
212 }
f59bc2fb 213 }
214
2b6d403c 215 function handler_end_old(&$page)
216 {
5e2307dc 217 return $this->handler_end($page, Env::v('hash'));
2b6d403c 218 }
219
f59bc2fb 220 function handler_end(&$page, $hash = null)
221 {
222 global $globals;
223
224 $page->changeTpl('register/end.tpl');
a41bf2f8 225 $_SESSION['sub_state'] = array('step' => 5);
f59bc2fb 226 require_once('user.func.inc.php');
227
228 if ($hash) {
08cce2ff 229 $res = XDB::query(
f59bc2fb 230 "SELECT r.uid, r.forlife, r.bestalias, r.mailorg2,
231 r.password, r.email, r.naissance, u.nom, u.prenom,
232 u.promo, u.flags
233 FROM register_pending AS r
234 INNER JOIN auth_user_md5 AS u ON r.uid = u.user_id
20d90835 235 WHERE hash={?} AND hash!='INSCRIT'", $hash);
f59bc2fb 236 }
237
238 if (!$hash || !list($uid, $forlife, $bestalias, $mailorg2, $password, $email,
239 $naissance, $nom, $prenom, $promo, $femme) = $res->fetchOneRow())
240 {
241 $page->kill("<p>Cette adresse n'existe pas, ou plus, sur le serveur.</p>
242 <p>Causes probables :</p>
243 <ol>
a7de4ef7 244 <li>Vérifie que tu visites l'adresse du dernier
245 e-mail reçu s'il y en a eu plusieurs.</li>
246 <li>Tu as peut-être mal copié l'adresse reçue par
247 mail, vérifie-la à la main.</li>
248 <li>Tu as peut-être attendu trop longtemps pour
249 confirmer. Les pré-inscriptions sont annulées
f59bc2fb 250 tous les 30 jours.</li>
a7de4ef7 251 <li>Tu es en fait déjà inscrit.</li>
f59bc2fb 252 </ol>");
253 }
254
255
256
257 /***********************************************************/
258 /****************** REALLY CREATE ACCOUNT ******************/
259 /***********************************************************/
260
08cce2ff 261 XDB::execute('UPDATE auth_user_md5
f59bc2fb 262 SET password={?}, perms="user",
263 date=NOW(), naissance={?}, date_ins = NOW()
264 WHERE user_id={?}', $password, $naissance, $uid);
08cce2ff 265 XDB::execute('REPLACE INTO auth_user_quick (user_id) VALUES ({?})', $uid);
266 XDB::execute('INSERT INTO aliases (id,alias,type)
f59bc2fb 267 VALUES ({?}, {?}, "a_vie")', $uid,
268 $forlife);
08cce2ff 269 XDB::execute('INSERT INTO aliases (id,alias,type,flags)
f59bc2fb 270 VALUES ({?}, {?}, "alias", "bestalias")',
271 $uid, $bestalias);
272 if ($mailorg2) {
08cce2ff 273 XDB::execute('INSERT INTO aliases (id,alias,type)
f59bc2fb 274 VALUES ({?}, {?}, "alias")', $uid,
275 $mailorg2);
276 }
277
278 require_once('emails.inc.php');
279 $redirect = new Redirect($uid);
280 $redirect->add_email($email);
281
282 // on cree un objet logger et on log l'inscription
c4271d38 283 $logger = new CoreLogger($uid);
f59bc2fb 284 $logger->log('inscription', $email);
285
08cce2ff 286 XDB::execute('UPDATE register_pending SET hash="INSCRIT" WHERE uid={?}', $uid);
f59bc2fb 287
8d8f7607 288 global $platal;
289 $platal->on_subscribe($forlife, $uid, $promo, $password);
f59bc2fb 290
1e33266a 291 $mymail = new PlMailer('register/inscription.reussie.tpl');
f59bc2fb 292 $mymail->assign('forlife', $forlife);
293 $mymail->assign('prenom', $prenom);
294 $mymail->send();
295
5480a216 296 if (!start_connexion($uid,false)) {
297 return PL_FORBIDDEN;
298 }
f59bc2fb 299 $_SESSION['auth'] = AUTH_MDP;
300
301 /***********************************************************/
a7de4ef7 302 /************* envoi d'un mail au démarcheur ***************/
f59bc2fb 303 /***********************************************************/
08cce2ff 304 $res = XDB::iterRow(
f59bc2fb 305 "SELECT DISTINCT sa.alias, IF(s.nom_usage,s.nom_usage,s.nom) AS nom,
d5ca3059 306 s.prenom, FIND_IN_SET('femme', s.flags) AS femme
f59bc2fb 307 FROM register_marketing AS m
308 INNER JOIN auth_user_md5 AS s ON ( m.sender = s.user_id )
309 INNER JOIN aliases AS sa ON ( sa.id = m.sender
310 AND FIND_IN_SET('bestalias', sa.flags) )
311 WHERE m.uid = {?}", $uid);
08cce2ff 312 XDB::execute("UPDATE register_mstats SET success=NOW() WHERE uid={?}", $uid);
f59bc2fb 313
314 while (list($salias, $snom, $sprenom, $sfemme) = $res->next()) {
1e33266a 315 $mymail = new PlMailer();
a7de4ef7 316 $mymail->setSubject("$prenom $nom s'est inscrit à Polytechnique.org !");
f59bc2fb 317 $mymail->setFrom('"Marketing Polytechnique.org" <register@polytechnique.org>');
318 $mymail->addTo("\"$sprenom $snom\" <$salias@{$globals->mail->domain}>");
a7de4ef7 319 $msg = ($sfemme?'Chère':'Cher')." $sprenom,\n\n"
320 . "Nous t'écrivons pour t'informer que {$prenom} {$nom} (X{$promo}), "
321 . "que tu avais incité".($femme?'e':'')." à s'inscrire à Polytechnique.org, "
322 . "vient à l'instant de terminer son inscription.\n\n"
323 . "Merci de ta participation active à la reconnaissance de ce site !!!\n\n"
f59bc2fb 324 . "Bien cordialement,\n"
a7de4ef7 325 . "L'équipe Polytechnique.org";
f59bc2fb 326 $mymail->setTxtBody(wordwrap($msg, 72));
327 $mymail->send();
328 }
5f5f0eb5 329
330 /**** send a mail to X.org administrators ****/
9812efa0 331 if ($globals->register->notif) {
332 $mymail = new PlMailer();
333 $mymail->setSubject("Inscription de $prenom $nom (X$promo)");
334 $mymail->setFrom('"Webmaster Polytechnique.org" <web@polytechnique.org>');
335 $mymail->addTo($globals->register->notif);
a7de4ef7 336 $msg = "$prenom $nom (X$promo) a terminé son inscription avec les données suivantes :\n"
9812efa0 337 . " - nom : $nom\n"
338 . " - prenom : $prenom\n"
339 . " - promo : $promo\n"
340 . " - naissance : $naissance\n"
341 . " - forlife : $forlife\n"
342 . " - email : $email\n"
5f5f0eb5 343 . " - sexe : $femme\n"
344 . " - ip : " . (@$_SERVER['HTTP_X_FORWARDED_FOR'] ? $_SERVER['HTTP_X_FORWARDED_FOR']
345 : $_SERVER['REMOTE_ADDR']) . "\n"
346 . " - proxy : " . (@$_SERVER['HTTP_X_FORWARDED_FOR'] ? $_SERVER['REMOTE_ADDR'] : "") . "\n";
9812efa0 347 $mymail->setTxtBody($msg);
348 $mymail->send();
349 }
f59bc2fb 350
e654517d 351 Marketing::clear($uid);
f59bc2fb 352
8b00e0e0 353 pl_redirect('register/success');
f59bc2fb 354 $page->assign('uid', $uid);
f59bc2fb 355 }
356
357 function handler_success(&$page)
358 {
f59bc2fb 359 $page->changeTpl('register/success.tpl');
360
a41bf2f8 361 $_SESSION['sub_state'] = array('step' => 5);
f59bc2fb 362 if (Env::has('response2')) {
5e2307dc 363 $_SESSION['password'] = $password = Post::v('response2');
f59bc2fb 364
08cce2ff 365 XDB::execute('UPDATE auth_user_md5 SET password={?}
f59bc2fb 366 WHERE user_id={?}', $password,
cab08090 367 S::v('uid'));
f59bc2fb 368
714bc242 369 $log = S::v('log');
f59bc2fb 370 $log->log('passwd', '');
371
5e2307dc 372 if (Cookie::v('ORGaccess')) {
f59bc2fb 373 require_once('secure_hash.inc.php');
374 setcookie('ORGaccess', hash_encrypt($password), (time()+25920000), '/', '' ,0);
375 }
376
377 $page->assign('mdpok', true);
378 }
379
c99ef281 380 $page->addJsLink('motdepasse.js');
f59bc2fb 381 }
382}
383
a7de4ef7 384// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
f59bc2fb 385?>