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