fix rss du carnet
[platal.git] / modules / register.php
CommitLineData
f59bc2fb 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
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 {
cab08090 36 $sub_state = S::v('sub_state', Array());
f59bc2fb 37 if (!isset($sub_state['step'])) {
38 $sub_state['step'] = 0;
39 }
5e2307dc 40 if (Get::has('back') && Get::i('back') < $sub_state['step']) {
41 $sub_state['step'] = max(0,Get::i('back'));
f59bc2fb 42 }
43
2b6d403c 44 // Compatibility with old sources, keep it atm
45 if (!$hash && Env::has('hash')) {
5e2307dc 46 $hash = Env::v('hash');
2b6d403c 47 }
48
20d90835 49 if ($hash) {
08cce2ff 50 $res = XDB::query(
f59bc2fb 51 "SELECT m.uid, u.promo, u.nom, u.prenom, u.matricule
52 FROM register_marketing AS m
53 INNER JOIN auth_user_md5 AS u ON u.user_id = m.uid
20d90835 54 WHERE m.hash={?}", $hash);
f59bc2fb 55 if (list($uid, $promo, $nom, $prenom, $ourmat) = $res->fetchOneRow()) {
56 $sub_state['uid'] = $uid;
20d90835 57 $sub_state['hash'] = $hash;
f59bc2fb 58 $sub_state['promo'] = $promo;
59 $sub_state['nom'] = $nom;
60 $sub_state['prenom'] = $prenom;
61 $sub_state['ourmat'] = $ourmat;
62
08cce2ff 63 XDB::execute(
f59bc2fb 64 "REPLACE INTO register_mstats (uid,sender,success)
65 SELECT m.uid, m.sender, 0
66 FROM register_marketing AS m
67 WHERE m.hash", $sub_state['hash']);
68 }
69 }
70
71 switch ($sub_state['step']) {
72 case 0:
ab694f12 73 require_once('wiki.inc.php');
74 wiki_require_page('Docs.Charte');
f59bc2fb 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')) {
5e2307dc 87 $promo = Post::i('promo');
f59bc2fb 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');
5e2307dc 105 $sub_state['prenom'] = Post::v('prenom');
106 $sub_state['nom'] = Post::v('nom');
107 $sub_state['mat'] = Post::v('mat');
f59bc2fb 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:
bf273d6a 120 $alert = null;
f59bc2fb 121 if (count($_POST)) {
122 require_once('register.inc.php');
5e2307dc 123 if (!isvalid_email(Post::v('email'))) {
f59bc2fb 124 $err[] = "Le champ 'E-mail' n'est pas valide.";
5e2307dc 125 } elseif (!isvalid_email_redirection(Post::v('email'))) {
f59bc2fb 126 $err[] = $sub_state['forlife']." doit renvoyer vers un email existant ".
20d90835 127 "valide, en particulier, il ne peut pas être renvoyé vers lui-même.";
f59bc2fb 128 }
35cd1be1 129 $birth = Env::v('naissance');
130 if (!preg_match('/^[0-3][0-9][01][0-9][12][90][0-9][0-9]$/', $birth)) {
f59bc2fb 131 $err[] = "La 'Date de naissance' n'est pas correcte.";
35cd1be1 132 } else {
133 $year = (int)substr($birth, 4, 4);
134 $promo = (int)$sub_state['promo'];
135 if ($year > $promo - 15 || $year < $promo - 30) {
136 $err[] = "La 'Date de naissance' n'est pas correcte.";
bf273d6a 137 $alert = "Date de naissance proposée $birth\n\n";
35cd1be1 138 }
f59bc2fb 139 }
140
bf273d6a 141 // Check if the given email is known as dangerous
142 $res = Xdb::iterRow("SELECT w.state, w.description, a.alias
143 FROM emails AS e
144 INNER JOIN emails_watch AS w ON (e.email = w.email AND w.state != 'safe')
145 INNER JOIN aliases AS a ON (e.uid = a.id AND a.type = 'a_vie')
146 WHERE e.email = {?}
147 ORDER BY a.alias", Post::v('email'));
148 $aliases = array();
149 while(list($gstate, $gdescription, $alias) = $res->next()) {
150 $state = $gstate;
151 $description = $gdescription;
152 $aliases[] = $alias;
153 }
154 if (count($aliases) != 0) {
155 $alert .= "Email proposé : " . Post::v('email') . "\n"
156 . "Ce mails est connu avec l'état $state :\n"
157 . $description . "\n"
158 . "Pour les alias :\n* " . join("\n* ", $aliases) . "\n\n";
159 }
160
f59bc2fb 161 if (isset($err)) {
162 $err = join('<br />', $err);
163 } else {
f59bc2fb 164 $sub_state['naissance'] = sprintf("%s-%s-%s",
165 substr($birth,4,4),
166 substr($birth,2,2),
167 substr($birth,0,2));
5e2307dc 168 $sub_state['email'] = Post::v('email');
f59bc2fb 169 $sub_state['step'] = 4;
170 finish_ins($sub_state);
171 }
bf273d6a 172 if (!is_null($alert)) {
173 send_alert_mail($sub_state, $alert);
174 }
f59bc2fb 175 }
176 break;
177 }
178
179 $_SESSION['sub_state'] = $sub_state;
62a66dfc 180 $page->changeTpl('register/step'.intval($sub_state['step']).'.tpl', SIMPLE);
f59bc2fb 181 if (isset($err)) {
182 $page->trig($err);
183 }
f59bc2fb 184 }
185
2b6d403c 186 function handler_end_old(&$page)
187 {
5e2307dc 188 return $this->handler_end($page, Env::v('hash'));
2b6d403c 189 }
190
f59bc2fb 191 function handler_end(&$page, $hash = null)
192 {
193 global $globals;
194
195 $page->changeTpl('register/end.tpl');
196
197 require_once('user.func.inc.php');
198
199 if ($hash) {
08cce2ff 200 $res = XDB::query(
f59bc2fb 201 "SELECT r.uid, r.forlife, r.bestalias, r.mailorg2,
202 r.password, r.email, r.naissance, u.nom, u.prenom,
203 u.promo, u.flags
204 FROM register_pending AS r
205 INNER JOIN auth_user_md5 AS u ON r.uid = u.user_id
20d90835 206 WHERE hash={?} AND hash!='INSCRIT'", $hash);
f59bc2fb 207 }
208
209 if (!$hash || !list($uid, $forlife, $bestalias, $mailorg2, $password, $email,
210 $naissance, $nom, $prenom, $promo, $femme) = $res->fetchOneRow())
211 {
212 $page->kill("<p>Cette adresse n'existe pas, ou plus, sur le serveur.</p>
213 <p>Causes probables :</p>
214 <ol>
20d90835 215 <li>Vérifie que tu visites l'adresse du dernier
216 e-mail reçu s'il y en a eu plusieurs.</li>
217 <li>Tu as peut-être mal copié l'adresse reçue par
218 mail, vérifie-la à la main.</li>
219 <li>Tu as peut-être attendu trop longtemps pour
220 confirmer. Les pré-inscriptions sont annulées
f59bc2fb 221 tous les 30 jours.</li>
20d90835 222 <li>Tu es en fait déjà inscrit.</li>
f59bc2fb 223 </ol>");
224 }
225
226
227
228 /***********************************************************/
229 /****************** REALLY CREATE ACCOUNT ******************/
230 /***********************************************************/
231
08cce2ff 232 XDB::execute('UPDATE auth_user_md5
f59bc2fb 233 SET password={?}, perms="user",
234 date=NOW(), naissance={?}, date_ins = NOW()
235 WHERE user_id={?}', $password, $naissance, $uid);
08cce2ff 236 XDB::execute('REPLACE INTO auth_user_quick (user_id) VALUES ({?})', $uid);
237 XDB::execute('INSERT INTO aliases (id,alias,type)
f59bc2fb 238 VALUES ({?}, {?}, "a_vie")', $uid,
239 $forlife);
08cce2ff 240 XDB::execute('INSERT INTO aliases (id,alias,type,flags)
f59bc2fb 241 VALUES ({?}, {?}, "alias", "bestalias")',
242 $uid, $bestalias);
243 if ($mailorg2) {
08cce2ff 244 XDB::execute('INSERT INTO aliases (id,alias,type)
f59bc2fb 245 VALUES ({?}, {?}, "alias")', $uid,
246 $mailorg2);
247 }
248
249 require_once('emails.inc.php');
250 $redirect = new Redirect($uid);
251 $redirect->add_email($email);
252
253 // on cree un objet logger et on log l'inscription
c4271d38 254 $logger = new CoreLogger($uid);
f59bc2fb 255 $logger->log('inscription', $email);
256
08cce2ff 257 XDB::execute('UPDATE register_pending SET hash="INSCRIT" WHERE uid={?}', $uid);
f59bc2fb 258
8d8f7607 259 global $platal;
260 $platal->on_subscribe($forlife, $uid, $promo, $password);
f59bc2fb 261
262 require_once('xorg.mailer.inc.php');
263 $mymail = new XOrgMailer('register/inscription.reussie.tpl');
264 $mymail->assign('forlife', $forlife);
265 $mymail->assign('prenom', $prenom);
266 $mymail->send();
267
268 start_connexion($uid,false);
269 $_SESSION['auth'] = AUTH_MDP;
270
271 /***********************************************************/
20d90835 272 /************* envoi d'un mail au démarcheur ***************/
f59bc2fb 273 /***********************************************************/
08cce2ff 274 $res = XDB::iterRow(
f59bc2fb 275 "SELECT DISTINCT sa.alias, IF(s.nom_usage,s.nom_usage,s.nom) AS nom,
d5ca3059 276 s.prenom, FIND_IN_SET('femme', s.flags) AS femme
f59bc2fb 277 FROM register_marketing AS m
278 INNER JOIN auth_user_md5 AS s ON ( m.sender = s.user_id )
279 INNER JOIN aliases AS sa ON ( sa.id = m.sender
280 AND FIND_IN_SET('bestalias', sa.flags) )
281 WHERE m.uid = {?}", $uid);
08cce2ff 282 XDB::execute("UPDATE register_mstats SET success=NOW() WHERE uid={?}", $uid);
f59bc2fb 283
284 while (list($salias, $snom, $sprenom, $sfemme) = $res->next()) {
285 require_once('diogenes/diogenes.hermes.inc.php');
286 $mymail = new HermesMailer();
20d90835 287 $mymail->setSubject("$prenom $nom s'est inscrit à Polytechnique.org !");
f59bc2fb 288 $mymail->setFrom('"Marketing Polytechnique.org" <register@polytechnique.org>');
289 $mymail->addTo("\"$sprenom $snom\" <$salias@{$globals->mail->domain}>");
d5ca3059 290 $msg = ($sfemme?'Chère':'Cher')." $sprenom,\n\n"
20d90835 291 . "Nous t'écrivons pour t'informer que {$prenom} {$nom} (X{$promo}), "
292 . "que tu avais incité".($femme?'e':'')." à s'inscrire à Polytechnique.org, "
293 . "vient à l'instant de terminer son inscription.\n\n"
294 . "Merci de ta participation active à la reconnaissance de ce site !!!\n\n"
f59bc2fb 295 . "Bien cordialement,\n"
20d90835 296 . "L'équipe Polytechnique.org";
f59bc2fb 297 $mymail->setTxtBody(wordwrap($msg, 72));
298 $mymail->send();
299 }
300
08cce2ff 301 XDB::execute("DELETE FROM register_marketing WHERE uid = {?}", $uid);
f59bc2fb 302
8b00e0e0 303 pl_redirect('register/success');
f59bc2fb 304 $page->assign('uid', $uid);
f59bc2fb 305 }
306
307 function handler_success(&$page)
308 {
f59bc2fb 309 $page->changeTpl('register/success.tpl');
310
311 if (Env::has('response2')) {
5e2307dc 312 $_SESSION['password'] = $password = Post::v('response2');
f59bc2fb 313
08cce2ff 314 XDB::execute('UPDATE auth_user_md5 SET password={?}
f59bc2fb 315 WHERE user_id={?}', $password,
cab08090 316 S::v('uid'));
f59bc2fb 317
cab08090 318 $log =& S::v('log');
f59bc2fb 319 $log->log('passwd', '');
320
5e2307dc 321 if (Cookie::v('ORGaccess')) {
f59bc2fb 322 require_once('secure_hash.inc.php');
323 setcookie('ORGaccess', hash_encrypt($password), (time()+25920000), '/', '' ,0);
324 }
325
326 $page->assign('mdpok', true);
327 }
328
c99ef281 329 $page->addJsLink('motdepasse.js');
f59bc2fb 330 }
331}
332
333?>