Extends the user massadder to automatically set the hruid field, when a value can...
[platal.git] / modules / register / register.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 Polytechnique.org *
0337d704 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
0337d704 22// {{{ function user_cmp
23
24function user_cmp($prenom, $nom, $_prenom, $_nom)
25{
26 $_nom = strtoupper(replace_accent($_nom));
27 $_prenom = strtoupper(replace_accent($_prenom));
28 $nom = strtoupper(replace_accent($nom));
29 $prenom = strtoupper(replace_accent($prenom));
30
31 $is_ok = strtoupper($_prenom) == strtoupper($prenom);
eaf30d86 32
0337d704 33 $tokens = preg_split("/[ \-']/", $nom, -1, PREG_SPLIT_NO_EMPTY);
34 $maxlen = 0;
35
36 foreach ($tokens as $str) {
37 $is_ok &= strpos($_nom, $str)!==false;
38 $maxlen = max($maxlen, strlen($str));
39 }
40
41 return $is_ok && ($maxlen > 2 || $maxlen == strlen($_nom));
42}
43
44// }}}
0337d704 45// {{{ function check_mat
46
ecc734a5 47function check_mat($promo, $mat, $nom, $prenom, &$ourmat, &$ourid, &$watch, &$naiss)
0337d704 48{
0337d704 49 if (!preg_match('/^[0-9][0-9][0-9][0-9][0-9][0-9]$/', $mat)) {
50 return "Le matricule doit comporter 6 chiffres.";
51 }
52
53 $year = intval(substr($mat, 0, 3));
54 $rang = intval(substr($mat, 3, 3));
55 if ($year > 200) { $year /= 10; };
56 if ($year < 96) {
57 return "ton matricule est incorrect";
58 } else {
59 $ourmat = sprintf('%04u%04u', 1900+$year, $rang);
60 }
61
08cce2ff 62 $res = XDB::query(
fbc210fa 63 'SELECT user_id, promo, perms IN ("admin","user"), nom, prenom, FIND_IN_SET(\'watch\', flags), naissance_ini
0337d704 64 FROM auth_user_md5
65 WHERE matricule={?} and deces = 0', $ourmat);
ecc734a5 66 list ($uid, $_promo, $_already, $_nom, $_prenom, $watch, $naiss) = $res->fetchOneRow();
a7de4ef7 67 if ($_already) { return "tu es déjà inscrit ou ton matricule est incorrect !"; }
0337d704 68 if ($_promo != $promo) { return "erreur de matricule"; }
69
70 if (!user_cmp($prenom, $nom, $_prenom, $_nom)) {
a7de4ef7 71 return "erreur dans l'identification. Réessaie, il y a une erreur quelque part !";
0337d704 72 }
73
74 $ourid = $uid;
75 return true;
76}
77
78// }}}
79// {{{ function check_old_mat
80
ecc734a5 81function check_old_mat($promo, $mat, $nom, $prenom, &$ourmat, &$ourid, &$watch, &$naiss)
0337d704 82{
08cce2ff 83 $res = XDB::iterRow(
ecc734a5 84 'SELECT user_id, nom, prenom, matricule, FIND_IN_SET(\'watch\', flags), naissance_ini
0337d704 85 FROM auth_user_md5
86 WHERE promo={?} AND deces=0 AND perms="pending"', $promo);
ecc734a5 87 while (list($_uid, $_nom, $_prenom, $_mat, $watch, $naiss) = $res->next()) {
0337d704 88 if (user_cmp($prenom, $nom, $_prenom, $_nom)) {
89 $ourid = $_uid;
90 $ourmat = $_mat;
91 return true;
92 }
93 }
94
08cce2ff 95 $res = XDB::iterRow(
ecc734a5 96 'SELECT user_id, nom, prenom, matricule, alias, FIND_IN_SET(\'watch\', u.flags), naissance_ini
0337d704 97 FROM auth_user_md5 AS u
98 INNER JOIN aliases AS a ON (u.user_id = a.id and FIND_IN_SET("bestalias", a.flags))
99 WHERE promo={?} AND deces=0 AND perms IN ("user","admin")', $promo);
ecc734a5 100 while (list($_uid, $_nom, $_prenom, $_mat, $alias, $watch, $naiss) = $res->next()) {
0337d704 101 if (user_cmp($prenom, $nom, $_prenom, $_nom)) {
102 $ourid = $_uid;
103 $ourmat = $_mat;
a7de4ef7 104 return "Tu es vraisemblablement déjà inscrit !";
0337d704 105 }
106 }
a7de4ef7 107 return "erreur: vérifie que tu as bien orthographié ton nom !";
0337d704 108}
109
110// }}}
111// {{{ function check_new_user
112
113function check_new_user(&$sub)
114{
0337d704 115 extract($sub);
116
117 $prenom = preg_replace("/[ \t]+/", ' ', trim($prenom));
eb8e3f3c 118 $prenom = preg_replace("/--+/", '-', $prenom);
119 $prenom = preg_replace("/''+/", '\'', $prenom);
0337d704 120 $prenom = make_firstname_case($prenom);
121
122 $nom = preg_replace("/[ \t]+/", ' ', trim($nom));
eb8e3f3c 123 $nom = preg_replace("/--+/", '-', $nom);
124 $nom = preg_replace("/''+/", '\'', $nom);
0337d704 125 $nom = strtoupper(replace_accent($nom));
126
127 if ($promo >= 1996) {
ecc734a5 128 $res = check_mat($promo, $mat, $nom, $prenom, $ourmat, $ourid, $watch, $naiss);
0337d704 129 } else {
ecc734a5 130 $res = check_old_mat($promo, $mat, $nom, $prenom, $ourmat, $ourid, $watch, $naiss);
0337d704 131 }
132 if ($res !== true) { return $res; }
133
134 $sub['nom'] = $nom;
135 $sub['prenom'] = $prenom;
136 $sub['ourmat'] = $ourmat;
137 $sub['uid'] = $ourid;
0be07aa6 138 $sub['watch'] = $watch;
ecc734a5 139 $sub['naissance_ini'] = $naiss;
0337d704 140
141 return true;
142}
143
144// }}}
145// {{{ function create_aliases
146
147function create_aliases (&$sub)
148{
115c90db 149 global $globals;
0337d704 150 extract ($sub);
151
152 $mailorg = make_username($prenom, $nom);
153 $mailorg2 = $mailorg.sprintf(".%02u", ($promo%100));
154 $forlife = make_forlife($prenom, $nom, $promo);
155
08cce2ff 156 $res = XDB::query('SELECT COUNT(*) FROM aliases WHERE alias={?}', $forlife);
0337d704 157 if ($res->fetchOneCell() > 0) {
158 return "Tu as un homonyme dans ta promo, il faut traiter ce cas manuellement.<br />".
d7dd70be 159 "envoie un mail à <a href=\"mailto:support@{$globals->mail->domain}</a>\">" .
160 "support@{$globals->mail->domain}</a> en expliquant ta situation.";
0337d704 161 }
eaf30d86 162
08cce2ff 163 $res = XDB::query('SELECT id, type, expire FROM aliases WHERE alias={?}', $mailorg);
0337d704 164
165 if ( $res->numRows() ) {
166
167 list($h_id, $h_type, $expire) = $res->fetchOneRow();
0337d704 168
169 if ( $h_type != 'homonyme' and empty($expire) ) {
08cce2ff 170 XDB::execute('UPDATE aliases SET expire=ADDDATE(NOW(),INTERVAL 1 MONTH) WHERE alias={?}', $mailorg);
171 XDB::execute('REPLACE INTO homonymes (homonyme_id,user_id) VALUES ({?},{?})', $h_id, $h_id);
172 XDB::execute('REPLACE INTO homonymes (homonyme_id,user_id) VALUES ({?},{?})', $h_id, $uid);
173 $res = XDB::query("SELECT alias FROM aliases WHERE id={?} AND expire IS NULL", $h_id);
0337d704 174 $als = $res->fetchColumn();
175
1e33266a 176 $mailer = new PlMailer();
1d55fe45 177 $mailer->setFrom('"Support Polytechnique.org" <support@' . $globals->mail->domain . '>');
178 $mailer->addTo("$mailorg@" . $globals->mail->domain);
0337d704 179 $mailer->setSubject("perte de ton alias $mailorg dans un mois !");
1d55fe45 180 $mailer->addCc('"Support Polytechnique.org" <support@' . $globals->mail->domain . '>');
0337d704 181 $msg =
e945001b 182 "Bonjour,\n\n".
eaf30d86 183
e945001b 184 "Un homonyme vient de s'inscrire. La politique de Polytechnique.org est de fournir des\n".
faefdbb7 185 "adresses email devinables, nous ne pouvons donc pas conserver ton alias '$mailorg' qui\n".
a7de4ef7 186 "correspond maintenant à deux personnes.\n\n".
eaf30d86 187
a7de4ef7 188 "Tu gardes tout de même l'usage de cet alias pour un mois encore à compter de ce jour.\n\n".
eaf30d86 189
a7de4ef7 190 "Lorsque cet alias sera désactivé, l'adresse $mailorg@polytechnique.org renverra vers un \n".
191 "robot qui indiquera qu'il y a plusieurs personnes portant le même nom ;\n".
192 "cela évite que l'un des homonymes reçoive des courriels destinés à l'autre.\n\n".
eaf30d86 193
e945001b 194 "Pour te connecter au site, tu pourras utiliser comme identifiant n'importe lequel de tes\n".
195 "autres alias :\n".
0337d704 196 " ".join(', ', $als)."\n";
a7de4ef7 197 "Commence dès aujourd'hui à communiquer à tes correspondants la nouvelle adresse que tu comptes utiliser !\n\n".
eaf30d86 198
a7de4ef7 199 "En nous excusant pour le désagrément occasionné,\n".
e945001b 200 "Cordialement,\n\n".
eaf30d86 201
0337d704 202 "-- \n".
a7de4ef7 203 "L'équipe de Polytechnique.org\n".
204 "\"Le portail des élèves & anciens élèves de l'X\"";
0337d704 205 $mailer->SetTxtBody(wordwrap($msg,72));
206 $mailer->send();
207 }
208
209 $sub['forlife'] = $forlife;
210 $sub['bestalias'] = $mailorg2;
211 $sub['mailorg2'] = null;
212 } else {
213 $sub['forlife'] = $forlife;
214 $sub['bestalias'] = $mailorg;
215 $sub['mailorg2'] = $mailorg2;
216 }
217
218 return true;
219}
220
221// }}}
222// {{{ function finish_ins
223
224function finish_ins($sub_state)
225{
226 global $globals;
227 extract($sub_state);
46bde4d1 228 require_once('secure_hash.inc.php');
0337d704 229
230 $pass = rand_pass();
f4ba548f 231 $pass_encrypted = hash_encrypt($pass);
0337d704 232 $hash = rand_url_id(12);
eaf30d86 233
08cce2ff 234 XDB::execute('UPDATE auth_user_md5 SET last_known_email={?} WHERE matricule = {?}', $email, $mat);
0337d704 235
08cce2ff 236 XDB::execute(
0337d704 237 "REPLACE INTO register_pending (uid, forlife, bestalias, mailorg2, password, email, date, relance, naissance, hash)
238 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, NOW(), 0, {?}, {?})",
46bde4d1 239 $uid, $forlife, $bestalias, $mailorg2, $pass_encrypted, $email, $naissance, $hash);
0337d704 240
1e33266a 241 $mymail = new PlMailer('register/inscrire.mail.tpl');
0337d704 242 $mymail->assign('mailorg', $bestalias);
243 $mymail->assign('lemail', $email);
244 $mymail->assign('pass', $pass);
245 $mymail->assign('baseurl', $globals->baseurl);
246 $mymail->assign('hash', $hash);
1d55fe45 247 $mymail->assign('subj', $bestalias."@" . $globals->mail->domain);
0337d704 248 $mymail->send();
249}
250
251// }}}
a7de4ef7 252// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 253?>