Convert source code to UTF-8
[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 );
32 }
33
34 function handler_register(&$page, $hash = null)
35 {
36 $alert = null;
37 $sub_state = S::v('sub_state', Array());
38 if (!isset($sub_state['step'])) {
39 $sub_state['step'] = 0;
40 }
41 if (!isset($sub_state['backs'])) {
42 $sub_state['backs'] = array();
43 }
44 if (Get::has('back') && Get::i('back') < $sub_state['step']) {
45 $sub_state['step'] = max(0,Get::i('back'));
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 }
52 }
53
54 // Compatibility with old sources, keep it atm
55 if (!$hash && Env::has('hash')) {
56 $hash = Env::v('hash');
57 }
58
59 if ($hash) {
60 $res = XDB::query(
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
64 WHERE m.hash={?}", $hash);
65 if (list($uid, $promo, $nom, $prenom, $ourmat) = $res->fetchOneRow()) {
66 $sub_state['uid'] = $uid;
67 $sub_state['hash'] = $hash;
68 $sub_state['promo'] = $promo;
69 $sub_state['nom'] = $nom;
70 $sub_state['prenom'] = $prenom;
71 $sub_state['ourmat'] = $ourmat;
72
73 XDB::execute(
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:
83 require_once('wiki.inc.php');
84 wiki_require_page('Reference.Charte');
85 if (Post::has('step1')) {
86 $sub_state['step'] = 1;
87 if (isset($sub_state['hash'])) {
88 $sub_state['step'] = 3;
89 require_once(dirname(__FILE__) . '/register/register.inc.php');
90 create_aliases($sub_state);
91 }
92 }
93 break;
94
95 case 1:
96 if (Post::has('promo')) {
97 $promo = Post::i('promo');
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 !";
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)) {
119 require_once(dirname(__FILE__) . '/register/register.inc.php');
120 $sub_state['prenom'] = Post::v('prenom');
121 $sub_state['nom'] = Post::v('nom');
122 $sub_state['mat'] = Post::v('mat');
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)) {
136 require_once(dirname(__FILE__) . '/register/register.inc.php');
137 if (!isvalid_email(Post::v('email'))) {
138 $err[] = "Le champ 'E-mail' n'est pas valide.";
139 } elseif (!isvalid_email_redirection(Post::v('email'))) {
140 $err[] = $sub_state['forlife']." doit renvoyer vers un email existant ".
141 "valide, en particulier, il ne peut pas être renvoyé vers lui-même.";
142 }
143 $birth = trim(Env::v('naissance'));
144 if (!preg_match('/^[0-3][0-9][01][0-9][12][90][0-9][0-9]$/', $birth)) {
145 $err[] = "La 'Date de naissance' n'est pas correcte.";
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.";
151 $alert = "Date de naissance incorrecte a l'inscription - ";
152 }
153 }
154
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) {
169 $alert .= "Email surveille propose a l'inscription - ";
170 }
171
172 if (check_ip('unsafe')) {
173 unset($err);
174 }
175
176 if (isset($err)) {
177 $err = join('<br />', $err);
178 } else {
179 $sub_state['naissance'] = sprintf("%s-%s-%s",
180 substr($birth,4,4),
181 substr($birth,2,2),
182 substr($birth,0,2));
183 $sub_state['email'] = Post::v('email');
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;
191 if (count($sub_state['backs']) >= 3) {
192 $alert .= "Fin d'une inscription hésitante";
193 }
194 finish_ins($sub_state);
195 }
196 }
197 }
198 break;
199 }
200
201 $_SESSION['sub_state'] = $sub_state;
202 if ($alert) {
203 send_warning_mail($alert);
204 }
205 $page->changeTpl('register/step'.intval($sub_state['step']).'.tpl');
206 if (isset($err)) {
207 $page->trig($err);
208 }
209 }
210
211 function handler_end_old(&$page)
212 {
213 return $this->handler_end($page, Env::v('hash'));
214 }
215
216 function handler_end(&$page, $hash = null)
217 {
218 global $globals;
219
220 $page->changeTpl('register/end.tpl');
221 $_SESSION['sub_state'] = array('step' => 5);
222 require_once('user.func.inc.php');
223
224 if ($hash) {
225 $res = XDB::query(
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
231 WHERE hash={?} AND hash!='INSCRIT'", $hash);
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>
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
246 tous les 30 jours.</li>
247 <li>Tu es en fait déjà inscrit.</li>
248 </ol>");
249 }
250
251
252
253 /***********************************************************/
254 /****************** REALLY CREATE ACCOUNT ******************/
255 /***********************************************************/
256
257 XDB::execute('UPDATE auth_user_md5
258 SET password={?}, perms="user",
259 date=NOW(), naissance={?}, date_ins = NOW()
260 WHERE user_id={?}', $password, $naissance, $uid);
261 XDB::execute('REPLACE INTO auth_user_quick (user_id) VALUES ({?})', $uid);
262 XDB::execute('INSERT INTO aliases (id,alias,type)
263 VALUES ({?}, {?}, "a_vie")', $uid,
264 $forlife);
265 XDB::execute('INSERT INTO aliases (id,alias,type,flags)
266 VALUES ({?}, {?}, "alias", "bestalias")',
267 $uid, $bestalias);
268 if ($mailorg2) {
269 XDB::execute('INSERT INTO aliases (id,alias,type)
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
279 $logger = new CoreLogger($uid);
280 $logger->log('inscription', $email);
281
282 XDB::execute('UPDATE register_pending SET hash="INSCRIT" WHERE uid={?}', $uid);
283
284 global $platal;
285 $platal->on_subscribe($forlife, $uid, $promo, $password);
286
287 $mymail = new PlMailer('register/inscription.reussie.tpl');
288 $mymail->assign('forlife', $forlife);
289 $mymail->assign('prenom', $prenom);
290 $mymail->send();
291
292 if (!start_connexion($uid,false)) {
293 return PL_FORBIDDEN;
294 }
295 $_SESSION['auth'] = AUTH_MDP;
296
297 /***********************************************************/
298 /************* envoi d'un mail au démarcheur ***************/
299 /***********************************************************/
300 $res = XDB::iterRow(
301 "SELECT DISTINCT sa.alias, IF(s.nom_usage,s.nom_usage,s.nom) AS nom,
302 s.prenom, FIND_IN_SET('femme', s.flags) AS femme
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);
308 XDB::execute("UPDATE register_mstats SET success=NOW() WHERE uid={?}", $uid);
309
310 while (list($salias, $snom, $sprenom, $sfemme) = $res->next()) {
311 $mymail = new PlMailer();
312 $mymail->setSubject("$prenom $nom s'est inscrit à Polytechnique.org !");
313 $mymail->setFrom('"Marketing Polytechnique.org" <register@polytechnique.org>');
314 $mymail->addTo("\"$sprenom $snom\" <$salias@{$globals->mail->domain}>");
315 $msg = ($sfemme?'Chère':'Cher')." $sprenom,\n\n"
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"
320 . "Bien cordialement,\n"
321 . "L'équipe Polytechnique.org";
322 $mymail->setTxtBody(wordwrap($msg, 72));
323 $mymail->send();
324 }
325 if ($globals->register->notif) {
326 $mymail = new PlMailer();
327 $mymail->setSubject("Inscription de $prenom $nom (X$promo)");
328 $mymail->setFrom('"Webmaster Polytechnique.org" <web@polytechnique.org>');
329 $mymail->addTo($globals->register->notif);
330 $msg = "$prenom $nom (X$promo) a terminé son inscription avec les données suivantes :\n"
331 . " - nom : $nom\n"
332 . " - prenom : $prenom\n"
333 . " - promo : $promo\n"
334 . " - naissance : $naissance\n"
335 . " - forlife : $forlife\n"
336 . " - email : $email\n"
337 . " - sexe : $femme\n";
338 $mymail->setTxtBody($msg);
339 $mymail->send();
340 }
341
342 XDB::execute("DELETE FROM register_marketing WHERE uid = {?}", $uid);
343
344 pl_redirect('register/success');
345 $page->assign('uid', $uid);
346 }
347
348 function handler_success(&$page)
349 {
350 $page->changeTpl('register/success.tpl');
351
352 $_SESSION['sub_state'] = array('step' => 5);
353 if (Env::has('response2')) {
354 $_SESSION['password'] = $password = Post::v('response2');
355
356 XDB::execute('UPDATE auth_user_md5 SET password={?}
357 WHERE user_id={?}', $password,
358 S::v('uid'));
359
360 $log =& S::v('log');
361 $log->log('passwd', '');
362
363 if (Cookie::v('ORGaccess')) {
364 require_once('secure_hash.inc.php');
365 setcookie('ORGaccess', hash_encrypt($password), (time()+25920000), '/', '' ,0);
366 }
367
368 $page->assign('mdpok', true);
369 }
370
371 $page->addJsLink('motdepasse.js');
372 }
373 }
374
375 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
376 ?>