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