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