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