Specifies when birthdate is unknown.
[platal.git] / modules / register.php
CommitLineData
f59bc2fb 1<?php
2/***************************************************************************
5e1513f6 3 * Copyright (C) 2003-2011 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(
94590511
SJ
27 'register' => $this->make_hook('register', AUTH_PUBLIC),
28 'register/end' => $this->make_hook('end', AUTH_PUBLIC),
f59bc2fb 29 );
30 }
31
26ba053e 32 function handler_register($page, $hash = null)
f59bc2fb 33 {
63954a5c
RB
34 $page->forceSkin('register');
35
34a82c29
SJ
36 $alert = array();
37 $alert_details = '';
94590511
SJ
38 $subState = new PlDict(S::v('subState', array()));
39 if (!$subState->has('step')) {
40 $subState->set('step', 0);
f59bc2fb 41 }
94590511
SJ
42 if (!$subState->has('backs')) {
43 $subState->set('backs', new PlDict());
2efe5355 44 }
94590511
SJ
45 if (Get::has('back') && Get::i('back') < $subState->i('step')) {
46 $subState->set('step', max(0, Get::i('back')));
c9910f75
SJ
47 $subState->v('backs')->set($subState->v('backs')->count() + 1, $subState->dict());
48 $subState->v('backs')->kill('backs');
94590511 49 if ($subState->v('backs')->count() == 3) {
34a82c29
SJ
50 $alert[] = "Tentative d'inscription très hésitante";
51 $alert_details .= "\n * Retours en arrières : 3.";
eaf30d86 52 }
f59bc2fb 53 }
54
20d90835 55 if ($hash) {
0e1dfbad 56 $res = XDB::query("SELECT a.uid, a.hruid, ppn.lastname_initial AS lastname, ppn.firstname_initial AS firstname, p.xorg_id AS xorgid,
e4860774 57 pd.promo, pe.promo_year AS yearpromo, pde.degree AS edu_type,
34a82c29 58 p.birthdate_ref AS birthdateRef, FIND_IN_SET('watch', a.flags) AS watch, m.hash, a.type, a.comment
f59bc2fb 59 FROM register_marketing AS m
94660e07 60 INNER JOIN accounts AS a ON (m.uid = a.uid)
4b0cf4e4 61 INNER JOIN account_profiles AS ap ON (a.uid = ap.uid AND FIND_IN_SET('owner', ap.perms))
9f1cd432 62 INNER JOIN profiles AS p ON (p.pid = ap.pid)
7733ade1
SJ
63 INNER JOIN profile_display AS pd ON (p.pid = pd.pid)
64 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET('primary', pe.flags))
e4860774 65 INNER JOIN profile_education_degree_enum AS pde ON (pde.id = pe.degreeid)
0e1dfbad 66 INNER JOIN profile_public_names AS ppn ON (ppn.pid = p.pid)
4c5a5921 67 WHERE m.hash = {?} AND a.state = 'pending'",
0e1dfbad 68 $hash);
94660e07 69
94590511
SJ
70 if ($res->numRows() == 1) {
71 $subState->merge($res->fetchOneRow());
f036c896 72 $subState->set('main_mail_domain', User::$sub_mail_domains[$subState->v('type')]);
94660e07 73
00ba8a74
SJ
74 XDB::execute('INSERT INTO register_mstats (uid, sender, success)
75 SELECT m.uid, m.sender, 0
76 FROM register_marketing AS m
77 WHERE m.hash
a245a3e1 78 ON DUPLICATE KEY UPDATE sender = VALUES(sender), success = VALUES(success)',
94590511 79 $subState->s('hash'));
f59bc2fb 80 }
81 }
82
94590511 83 switch ($subState->i('step')) {
f59bc2fb 84 case 0:
8f201b69
FB
85 $wp = new PlWikiPage('Reference.Charte');
86 $wp->buildCache();
f59bc2fb 87 if (Post::has('step1')) {
94590511
SJ
88 $subState->set('step', 1);
89 if ($subState->has('hash')) {
90 $subState->set('step', 3);
460d8f55 91 $this->load('register.inc.php');
94590511 92 createAliases($subState);
f59bc2fb 93 }
94 }
95 break;
96
97 case 1:
94590511 98 if (Post::has('yearpromo')) {
f0a52f1b 99 $edu_type = Post::t('edu_type');
94590511 100 $yearpromo = Post::i('yearpromo');
e4860774 101 $promo = Profile::$cycle_prefixes[$edu_type] . $yearpromo;
94660e07 102 $res = XDB::query("SELECT COUNT(*)
94590511
SJ
103 FROM accounts AS a
104 INNER JOIN account_profiles AS ap ON (a.uid = ap.uid AND FIND_IN_SET('owner', ap.perms))
105 INNER JOIN profiles AS p ON (p.pid = ap.pid)
761f6f96 106 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET('primary', pe.flags))
7733ade1 107 WHERE a.state = 'pending' AND p.deathdate IS NULL AND pe.promo_year = {?}",
761f6f96 108 $yearpromo);
94660e07 109
a41bf2f8 110 if (!$res->fetchOneCell()) {
94660e07 111 $error = 'La promotion saisie est incorrecte ou tous les camarades de cette promotion sont inscrits !';
f59bc2fb 112 } else {
94590511
SJ
113 $subState->set('step', 2);
114 $subState->set('promo', $promo);
115 $subState->set('yearpromo', $yearpromo);
f0a52f1b 116 $subState->set('edu_type', $edu_type);
e4860774 117 if ($edu_type == Profile::DEGREE_X) {
f0a52f1b
SJ
118 if ($yearpromo >= 1996 && $yearpromo < 2000) {
119 $subState->set('schoolid', ($yearpromo % 100) * 10 . '???');
e4860774
SJ
120 $subState->set('schoolid_exemple', ($yearpromo % 100) * 10000 + 532);
121 $subState->set('schoolid_exemple_ev2', (($yearpromo + 1) % 100) * 10000 + 532);
f0a52f1b
SJ
122 } elseif($yearpromo >= 2000) {
123 $subState->set('schoolid', 100 + ($yearpromo % 100) . '???');
e4860774
SJ
124 $subState->set('schoolid_exemple', (100 + ($yearpromo % 100)) * 1000 + 532);
125 $subState->set('schoolid_exemple_ev2', (100 + (($yearpromo + 1) % 100)) * 1000 + 532);
f0a52f1b 126 }
f59bc2fb 127 }
128 }
129 }
130 break;
131
132 case 2:
133 if (count($_POST)) {
460d8f55 134 $this->load('register.inc.php');
94590511
SJ
135 $subState->set('firstname', Post::t('firstname'));
136 $subState->set('lastname', Post::t('lastname'));
e4860774
SJ
137 if (Post::has('schoolid')) {
138 $subState->set('schoolid', Post::i('schoolid'));
139 }
94590511 140 $error = checkNewUser($subState);
f59bc2fb 141
94660e07
SJ
142 if ($error !== true) {
143 break;
144 }
94590511 145 $error = createAliases($subState);
94660e07
SJ
146 if ($error === true) {
147 unset($error);
94590511 148 $subState->set('step', 3);
f59bc2fb 149 }
150 }
151 break;
152
153 case 3:
154 if (count($_POST)) {
460d8f55 155 $this->load('register.inc.php');
97a82cd2
VZ
156
157 // Validate the email address format and domain.
c6310567 158 require_once 'emails.inc.php';
94660e07 159
f4dda5fb 160 $user = User::get($subState->s('uid'));
5e2307dc 161 if (!isvalid_email(Post::v('email'))) {
94660e07 162 $error[] = "Le champ 'Email' n'est pas valide.";
f4dda5fb 163 } elseif (!isvalid_email_redirection(Post::v('email'), $user)) {
94590511 164 $error[] = $subState->s('forlife') . ' doit renvoyer vers un email existant '
94660e07 165 . 'valide, en particulier, il ne peut pas être renvoyé vers lui-même.';
f59bc2fb 166 }
97a82cd2
VZ
167
168 // Validate the birthday format and range.
94590511 169 $birth = Post::t('birthdate');
12e5d7a6 170 if (!preg_match('@^[0-3]?\d/[01]?\d/(19|20)?\d{2}$@', $birth)) {
94660e07 171 $error[] = "La 'Date de naissance' n'est pas correcte.";
35cd1be1 172 } else {
12e5d7a6 173 $birth = explode('/', $birth, 3);
94660e07 174 for ($i = 0; $i < 3; ++$i)
7caaaf6d 175 $birth[$i] = intval($birth[$i]);
94660e07
SJ
176 if ($birth[2] < 100) {
177 $birth[2] += 1900;
178 }
e4860774
SJ
179 $year = $birth[2];
180 $ref_year = substr($subState->v('birthdateRef'), 0, 4);
181 if (abs($ref_year - $year) > 2) {
94660e07 182 $error[] = "La 'Date de naissance' n'est pas correcte.";
34a82c29 183 $alert[] = "Date de naissance incorrecte à l'inscription";
ad31c657
SJ
184 $alert_details .= "\n * Date de naissance renseignée : " . Post::t('birthdate');
185 if ($subState->v('birthdateRef') == '0000-00-00') {
186 $alert_details .= ' (date inconnue)';
187 } else {
188 $alert_details .= ' (date connue : ' . $subState->v('birthdateRef') . ')';
189 }
94590511 190 $subState->set('wrong_birthdate', $birth);
35cd1be1 191 }
f59bc2fb 192 }
193
3546b253
VZ
194 // Register the optional services requested by the user.
195 $services = array();
4b1a8575 196 foreach (array('ax_letter', 'imap', 'ml_promo', 'nl') as $service) {
3546b253
VZ
197 if (Post::b($service)) {
198 $services[] = $service;
199 }
200 }
94590511 201 $subState->set('services', $services);
3546b253 202
97a82cd2 203 // Validate the password.
81b5a6c9 204 if (!Post::v('pwhash', false)) {
94660e07 205 $error[] = "Le mot de passe n'est pas valide.";
97a82cd2
VZ
206 }
207
208 // Check if the given email is known as dangerous.
94590511
SJ
209 $res = XDB::query("SELECT state, description
210 FROM email_watch
211 WHERE email = {?} AND state != 'safe'",
94660e07 212 Post::v('email'));
94590511 213 $bannedEmail = false;
15836cdd
FB
214 if ($res->numRows()) {
215 list($state, $description) = $res->fetchOneRow();
34a82c29
SJ
216 $alert[] = "Email surveillé proposé à l'inscription";
217 $alert_details .= "\n * Email surveillé : " . Post::v('email');
94590511 218 $subState->set('email_desc', $description);
706ed3ef 219 if ($state == 'dangerous') {
94590511 220 $bannedEmail = true;
706ed3ef 221 }
5480a216 222 }
4b0cf4e4 223 if ($subState->i('watch') != 0) {
34a82c29
SJ
224 $alert[] = "Inscription d'un utilisateur surveillé";
225 $alert_details .= "\n * Commentaire pour la surveillance : " . $subState->v('comment');
0be07aa6 226 }
5480a216 227
94590511 228 if (($bannedIp = check_ip('unsafe'))) {
94660e07 229 unset($error);
bf273d6a 230 }
231
94660e07
SJ
232 if (isset($error)) {
233 $error = join('<br />', $error);
f59bc2fb 234 } else {
94590511
SJ
235 $subState->set('birthdate', sprintf("%04d-%02d-%02d",
236 intval($birth[2]), intval($birth[1]), intval($birth[0])));
237 $subState->set('email', Post::t('email'));
81b5a6c9 238 $subState->set('password', Post::t('pwhash'));
97a82cd2
VZ
239
240 // Update the current alert if the birthdate is incorrect,
241 // or if the IP address of the user has been banned.
94590511
SJ
242 if ($subState->s('birthdateRef') != '0000-00-00'
243 && $subState->s('birthdateRef') != $subState->s('birthdate')) {
34a82c29 244 $alert[] = "Date de naissance incorrecte à l'inscription";
ad31c657
SJ
245 $alert_details .= "\n * Date de naissance renseignée : " . Post::t('birthdate');
246 if ($subState->v('birthdateRef') == '0000-00-00') {
247 $alert_details .= ' (date inconnue)';
248 } else {
249 $alert_details .= ' (date connue : ' . $subState->v('birthdateRef') . ')';
250 }
ecc734a5 251 }
94590511 252 if ($bannedIp) {
34a82c29
SJ
253 $alert[] = "Tentative d'inscription depuis une IP surveillée";
254 $alert_details .= "\n * IP surveillée : " . $_SESSION['check_ip'];
706ed3ef 255 }
97a82cd2
VZ
256
257 // Prevent banned user from actually registering; save the current state for others.
94590511 258 if ($bannedEmail || $bannedIp) {
115c90db 259 global $globals;
94660e07 260 $error = "Une erreur s'est produite lors de l'inscription."
1d55fe45 261 . " Merci de contacter <a href='mailto:register@{$globals->mail->domain}>"
262 . " register@{$globals->mail->domain}</a>"
94660e07 263 . " pour nous faire part de cette erreur.";
5480a216 264 } else {
94590511 265 $subState->set('step', 4);
4b0cf4e4 266 if ($subState->v('backs')->count() >= 3) {
34a82c29
SJ
267 $alert[] = "Fin d'une inscription hésitante";
268 $alert_details .= "\n * Nombre de retours en arrière : " . $subState->v('backs')->count();
2efe5355 269 }
94590511 270 finishRegistration($subState);
5480a216 271 }
bf273d6a 272 }
f59bc2fb 273 }
274 break;
275 }
276
94590511 277 $_SESSION['subState'] = $subState->dict();
34a82c29
SJ
278 if (count($alert)) {
279 $alert_details = "Détails des alertes :" . $alert_details . "\n\n\n";
280 send_warning_mail(implode(' - ', $alert), $alert_details);
5480a216 281 }
97a82cd2 282
94590511 283 $page->changeTpl('register/step' . $subState->i('step') . '.tpl');
94660e07
SJ
284 if (isset($error)) {
285 $page->trigError($error);
f59bc2fb 286 }
f59bc2fb 287 }
288
26ba053e 289 function handler_end($page, $hash = null)
f59bc2fb 290 {
291 global $globals;
94590511 292 $_SESSION['subState'] = array('step' => 5);
ecc734a5 293
97a82cd2
VZ
294 // Reject registration requests from unsafe IP addresses (and remove the
295 // registration information from the database, to prevent IP changes).
ecc734a5 296 if (check_ip('unsafe')) {
94660e07 297 send_warning_mail('Une IP surveillée a tenté de finaliser son inscription.');
97a82cd2
VZ
298 XDB::execute("DELETE FROM register_pending
299 WHERE hash = {?} AND hash != 'INSCRIT'", $hash);
ecc734a5 300 return PL_FORBIDDEN;
301 }
302
97a82cd2
VZ
303 // Retrieve the pre-registration information using the url-provided
304 // authentication token.
94590511
SJ
305 $res = XDB::query("SELECT r.uid, p.pid, r.forlife, r.bestalias, r.mailorg2,
306 r.password, r.email, r.services, r.naissance,
0e1dfbad 307 ppn.lastname_initial, ppn.firstname_initial, pe.promo_year,
74f8cda1 308 pd.promo, p.sex, p.birthdate_ref, a.type, a.email AS old_account_email
94590511
SJ
309 FROM register_pending AS r
310 INNER JOIN accounts AS a ON (r.uid = a.uid)
311 INNER JOIN account_profiles AS ap ON (a.uid = ap.uid AND FIND_IN_SET('owner', ap.perms))
9f1cd432 312 INNER JOIN profiles AS p ON (p.pid = ap.pid)
0e1dfbad 313 INNER JOIN profile_public_names AS ppn ON (ppn.pid = p.pid)
94590511 314 INNER JOIN profile_display AS pd ON (p.pid = pd.pid)
7733ade1 315 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET('primary', pe.flags))
4c5a5921 316 WHERE hash = {?} AND hash != 'INSCRIT' AND a.state = 'pending'",
0e1dfbad 317 $hash);
97a82cd2 318 if (!$hash || $res->numRows() == 0) {
f59bc2fb 319 $page->kill("<p>Cette adresse n'existe pas, ou plus, sur le serveur.</p>
97a82cd2 320 <p>Causes probables&nbsp;:</p>
f59bc2fb 321 <ol>
a7de4ef7 322 <li>Vérifie que tu visites l'adresse du dernier
97a82cd2 323 email reçu s'il y en a eu plusieurs.</li>
a7de4ef7 324 <li>Tu as peut-être mal copié l'adresse reçue par
97a82cd2 325 email, vérifie-la à la main.</li>
a7de4ef7 326 <li>Tu as peut-être attendu trop longtemps pour
94590511 327 confirmer. Les pré-inscriptions sont annulées
f59bc2fb 328 tous les 30 jours.</li>
a7de4ef7 329 <li>Tu es en fait déjà inscrit.</li>
f59bc2fb 330 </ol>");
331 }
332
94590511 333 list($uid, $pid, $forlife, $bestalias, $emailXorg2, $password, $email, $services,
74f8cda1 334 $birthdate, $lastname, $firstname, $yearpromo, $promo, $sex, $birthdate_ref, $type, $old_account_email) = $res->fetchOneRow();
2bf73be5
SJ
335 $isX = ($type == 'x');
336 $mail_domain = User::$sub_mail_domains[$type] . $globals->mail->domain;
f59bc2fb 337
97a82cd2
VZ
338 // Prepare the template for display.
339 $page->changeTpl('register/end.tpl');
97a82cd2 340 $page->assign('forlife', $forlife);
94590511 341 $page->assign('firstname', $firstname);
97a82cd2
VZ
342
343 // Check if the user did enter a valid password; if not (or if none is found),
344 // get her an information page.
94590511
SJ
345 if (Post::has('response')) {
346 $expected_response = sha1("$forlife:$password:" . S::v('challenge'));
347 if (Post::v('response') != $expected_response) {
97a82cd2
VZ
348 $page->trigError("Mot de passe invalide.");
349 S::logger($uid)->log('auth_fail', 'bad password (register/end)');
350 return;
351 }
352 } else {
353 return;
354 }
f59bc2fb 355
97a82cd2
VZ
356 //
357 // Create the user account.
358 //
dd9b3613 359 XDB::startTransaction();
94590511
SJ
360 XDB::execute("UPDATE accounts
361 SET password = {?}, state = 'active',
33a4f3f9 362 registration_date = NOW(), email = NULL
94590511
SJ
363 WHERE uid = {?}", $password, $uid);
364 XDB::execute("UPDATE profiles
365 SET birthdate = {?}, last_change = NOW()
366 WHERE pid = {?}", $birthdate, $pid);
c0436d0b
SJ
367 XDB::execute('INSERT INTO email_source_account (email, uid, type, flags, domain)
368 SELECT {?}, {?}, \'forlife\', \'\', id
369 FROM email_virtual_domains
370 WHERE name = {?}',
f036c896 371 $forlife, $uid, $mail_domain);
c0436d0b
SJ
372 XDB::execute('INSERT INTO email_source_account (email, uid, type, flags, domain)
373 SELECT {?}, {?}, \'alias\', \'bestalias\', id
374 FROM email_virtual_domains
375 WHERE name = {?}',
f036c896 376 $bestalias, $uid, $mail_domain);
94590511 377 if ($emailXorg2) {
c0436d0b
SJ
378 XDB::execute('INSERT INTO email_source_account (email, uid, type, flags, domain)
379 SELECT {?}, {?}, \'alias\', \'\', id
380 FROM email_virtual_domains
381 WHERE name = {?}',
f036c896 382 $emailXorg2, $uid, $mail_domain);
f59bc2fb 383 }
dd9b3613 384 XDB::commit();
f59bc2fb 385
97a82cd2 386 // Add the registration email address as first and only redirection.
726eaf7a 387 require_once 'emails.inc.php';
f0a52f1b 388 $user = User::getSilentWithUID($uid);
4b1a8575
SJ
389 $redirect = new Redirect($user);
390 $redirect->add_email($email);
f25e42eb 391 fix_bestalias($user);
f59bc2fb 392
74f8cda1
SJ
393 // If the user was registered to some aliases and MLs, we must change
394 // the subscription to her forlife email.
395 if ($old_account_email) {
396 $listClient = new MMList($user);
397 $listClient->change_user_email($old_account_email, $user->forlifeEmail());
398 update_alias_user($old_account_email, $user->forlifeEmail());
399 }
400
3546b253
VZ
401 // Try to start a session (so the user don't have to log in); we will use
402 // the password available in Post:: to authenticate the user.
bfe9f4c7 403 Platal::session()->start(AUTH_PASSWD);
3546b253
VZ
404
405 // Subscribe the user to the services she did request at registration time.
2bf73be5 406 require_once 'newsletter.inc.php';
3546b253
VZ
407 foreach (explode(',', $services) as $service) {
408 switch ($service) {
409 case 'ax_letter':
6ae6840d
RB
410 NewsLetter::forGroup(NewsLetter::GROUP_AX)->subscribe($user);
411 break;
412 case 'nl':
413 NewsLetter::forGroup(NewsLetter::GROUP_XORG)->subscribe($user);
3546b253
VZ
414 break;
415 case 'imap':
5ad556d3 416 Email::activate_storage($user, 'imap', Bogo::IMAP_DEFAULT);
3546b253
VZ
417 break;
418 case 'ml_promo':
d1d7fe34
SJ
419 if ($isX) {
420 $r = XDB::query('SELECT id FROM groups WHERE diminutif = {?}', $yearpromo);
421 if ($r->numRows()) {
422 $asso_id = $r->fetchOneCell();
423 XDB::execute('INSERT IGNORE INTO group_members (uid, asso_id)
424 VALUES ({?}, {?})',
425 $uid, $asso_id);
426 try {
427 $mmlist = new MMList($user);
428 $mmlist->subscribe("promo" . $yearpromo);
429 } catch (Exception $e) {
430 PlErrorReport::report($e);
431 $page->trigError("L'inscription à la liste promo" . $yearpromo . " a échouée.");
432 }
e78be37f 433 }
3546b253
VZ
434 }
435 break;
3546b253
VZ
436 }
437 }
438
97a82cd2 439 // Log the registration in the user session.
03c0a3a7 440 S::logger($uid)->log('inscription', $email);
97a82cd2
VZ
441 XDB::execute("UPDATE register_pending
442 SET hash = 'INSCRIT'
443 WHERE uid = {?}", $uid);
f59bc2fb 444
97a82cd2 445 // Congratulate our newly registered user by email.
94590511 446 $mymail = new PlMailer('register/success.mail.tpl');
4b1a8575 447 $mymail->addTo("\"{$user->fullName()}\" <{$user->forlifeEmail()}>");
96700179 448 if ($isX) {
96700179
SJ
449 $mymail->setSubject('Bienvenue parmi les X sur le web !');
450 } else {
96700179
SJ
451 $mymail->setSubject('Bienvenue sur Polytechnique.org !');
452 }
f59bc2fb 453 $mymail->assign('forlife', $forlife);
94590511 454 $mymail->assign('firstname', $firstname);
f59bc2fb 455 $mymail->send();
456
97a82cd2 457 // Index the user, to allow her to appear in searches.
bbdfd693 458 Profile::rebuildSearchTokens($pid);
2a54eb4d 459
97a82cd2 460 // Notify other users which were watching for her arrival.
00ba8a74
SJ
461 XDB::execute('INSERT INTO contacts (uid, contact)
462 SELECT uid, ni_id
463 FROM watch_nonins
464 WHERE ni_id = {?}', $uid);
94590511
SJ
465 XDB::execute('DELETE FROM watch_nonins
466 WHERE ni_id = {?}', $uid);
467 Platal::session()->updateNbNotifs();
03c0a3a7 468
97a82cd2 469 // Forcibly register the new user on default forums.
2bf73be5 470 $registeredForums = array('xorg.general', 'xorg.pa.divers', 'xorg.pa.logements');
97a82cd2 471
2bf73be5
SJ
472 if ($isX) {
473 $promoForum = 'xorg.promo.' . strtolower($promo);
474 $exists = XDB::fetchOneCell('SELECT COUNT(*)
475 FROM forums
476 WHERE name = {?}',
477 $promoForum);
478
479 if ($exists == 0) {
480 // Notify the newsgroup admin of the promotion forum needs be created.
94590511
SJ
481 $promoFull = new UserFilter(new UFC_Promo('=', UserFilter::DISPLAY, $promo));
482 $promoRegistered = new UserFilter(new PFC_And(
483 new UFC_Promo('=', UserFilter::DISPLAY, $promo),
484 new UFC_Registered(true),
485 new PFC_Not(new UFC_Dead())
486 ));
487 if ($promoRegistered->getTotalCount() > 0.2 * $promoFull->getTotalCount()) {
03c0a3a7
FB
488 $mymail = new PlMailer('admin/forums-promo.mail.tpl');
489 $mymail->assign('promo', $promo);
490 $mymail->send();
491 }
2bf73be5
SJ
492 } else {
493 $registeredForums[] = $promoForum;
03c0a3a7
FB
494 }
495 }
496
2bf73be5
SJ
497 foreach ($registeredForums as $forum) {
498 XDB::execute("INSERT INTO forum_subs (fid, uid)
499 SELECT fid, {?}
500 FROM forums
501 WHERE name = {?}",
502 $uid, $val);
503 }
504
97a82cd2 505 // Update the global registration count stats.
ebfdf077 506 $globals->updateNbIns();
b5dd6f2f 507
97a82cd2
VZ
508 //
509 // Update collateral data sources, and inform watchers by email.
510 //
f59bc2fb 511
97a82cd2 512 // Email the referrer(s) of this new user.
94590511
SJ
513 $res = XDB::iterRow("SELECT sender, GROUP_CONCAT(email SEPARATOR ', ') AS mails, MAX(last) AS lastDate
514 FROM register_marketing
515 WHERE uid = {?}
516 GROUP BY sender
517 ORDER BY lastDate DESC", $uid);
97a82cd2
VZ
518 XDB::execute("UPDATE register_mstats
519 SET success = NOW()
520 WHERE uid = {?}", $uid);
f59bc2fb 521
d3447a09 522 $market = array();
94590511
SJ
523 while (list($senderid, $maketingEmails, $lastDate) = $res->next()) {
524 $sender = User::getWithUID($senderid);
07f1f729 525 $market[] = " - par {$sender->fullName()} sur $maketingEmails (le plus récemment le $lastDate)";
94590511
SJ
526 $mymail = new PlMailer('register/marketer.mail.tpl');
527 $mymail->setSubject("$firstname $lastname s'est inscrit à Polytechnique.org !");
76345d3b 528 $mymail->setTo($sender);
94590511
SJ
529 $mymail->assign('sender', $sender);
530 $mymail->assign('firstname', $firstname);
531 $mymail->assign('lastname', $lastname);
532 $mymail->assign('promo', $promo);
533 $mymail->assign('sex', $sex);
f59bc2fb 534 $mymail->setTxtBody(wordwrap($msg, 72));
535 $mymail->send();
536 }
5f5f0eb5 537
97a82cd2 538 // Email the plat/al administrators about the registration.
9812efa0 539 if ($globals->register->notif) {
94590511
SJ
540 $mymail = new PlMailer('register/registration.mail.tpl');
541 $mymail->setSubject("Inscription de $firstname $lastname ($promo)");
542 $mymail->assign('firstname', $firstname);
543 $mymail->assign('lastname', $lastname);
544 $mymail->assign('promo', $promo);
545 $mymail->assign('sex', $sex);
546 $mymail->assign('birthdate', $birthdate);
547 $mymail->assign('birthdate_ref', $birthdate_ref);
548 $mymail->assign('forlife', $forlife);
549 $mymail->assign('email', $email);
4b0cf4e4 550 $mymail->assign('logger', S::logger());
defff1aa 551 if (count($market) > 0) {
94590511 552 $mymail->assign('market', implode("\n", $market));
defff1aa 553 }
9812efa0 554 $mymail->setTxtBody($msg);
eaf30d86 555 $mymail->send();
9812efa0 556 }
f59bc2fb 557
97a82cd2 558 // Remove old pending marketing requests for the new user.
e654517d 559 Marketing::clear($uid);
f59bc2fb 560
97a0a459
FB
561 pl_redirect('profile/edit');
562 }
f59bc2fb 563}
564
a7de4ef7 565// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
f59bc2fb 566?>