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