Merge commit 'origin/master' into account
[platal.git] / modules / register / register.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 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{
a953f7e7
SJ
26 $_nom = mb_strtoupper($_nom);
27 $_prenom = mb_strtoupper($_prenom);
28 $nom = mb_strtoupper($nom);
29 $prenom = mb_strtoupper($prenom);
0337d704 30
a953f7e7 31 $is_ok = mb_strtoupper($_prenom) == mb_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
d3df41c3 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
d3df41c3
VZ
74 $nom = $_nom;
75 $prenom = $_prenom;
0337d704 76 $ourid = $uid;
77 return true;
78}
79
80// }}}
81// {{{ function check_old_mat
82
d3df41c3 83function check_old_mat($promo, $mat, &$nom, &$prenom, &$ourmat, &$ourid, &$watch, &$naiss)
0337d704 84{
08cce2ff 85 $res = XDB::iterRow(
ecc734a5 86 'SELECT user_id, nom, prenom, matricule, FIND_IN_SET(\'watch\', flags), naissance_ini
0337d704 87 FROM auth_user_md5
88 WHERE promo={?} AND deces=0 AND perms="pending"', $promo);
ecc734a5 89 while (list($_uid, $_nom, $_prenom, $_mat, $watch, $naiss) = $res->next()) {
0337d704 90 if (user_cmp($prenom, $nom, $_prenom, $_nom)) {
d3df41c3
VZ
91 $nom = $_nom;
92 $prenom = $_prenom;
0337d704 93 $ourid = $_uid;
94 $ourmat = $_mat;
95 return true;
96 }
97 }
98
08cce2ff 99 $res = XDB::iterRow(
ecc734a5 100 'SELECT user_id, nom, prenom, matricule, alias, FIND_IN_SET(\'watch\', u.flags), naissance_ini
0337d704 101 FROM auth_user_md5 AS u
102 INNER JOIN aliases AS a ON (u.user_id = a.id and FIND_IN_SET("bestalias", a.flags))
103 WHERE promo={?} AND deces=0 AND perms IN ("user","admin")', $promo);
ecc734a5 104 while (list($_uid, $_nom, $_prenom, $_mat, $alias, $watch, $naiss) = $res->next()) {
0337d704 105 if (user_cmp($prenom, $nom, $_prenom, $_nom)) {
106 $ourid = $_uid;
107 $ourmat = $_mat;
a7de4ef7 108 return "Tu es vraisemblablement déjà inscrit !";
0337d704 109 }
110 }
a7de4ef7 111 return "erreur: vérifie que tu as bien orthographié ton nom !";
0337d704 112}
113
114// }}}
115// {{{ function check_new_user
116
117function check_new_user(&$sub)
118{
0337d704 119 extract($sub);
120
121 $prenom = preg_replace("/[ \t]+/", ' ', trim($prenom));
eb8e3f3c 122 $prenom = preg_replace("/--+/", '-', $prenom);
123 $prenom = preg_replace("/''+/", '\'', $prenom);
0337d704 124 $prenom = make_firstname_case($prenom);
125
126 $nom = preg_replace("/[ \t]+/", ' ', trim($nom));
eb8e3f3c 127 $nom = preg_replace("/--+/", '-', $nom);
128 $nom = preg_replace("/''+/", '\'', $nom);
a953f7e7 129 $nom = mb_strtoupper($nom);
0337d704 130
131 if ($promo >= 1996) {
ecc734a5 132 $res = check_mat($promo, $mat, $nom, $prenom, $ourmat, $ourid, $watch, $naiss);
0337d704 133 } else {
ecc734a5 134 $res = check_old_mat($promo, $mat, $nom, $prenom, $ourmat, $ourid, $watch, $naiss);
0337d704 135 }
136 if ($res !== true) { return $res; }
137
138 $sub['nom'] = $nom;
139 $sub['prenom'] = $prenom;
140 $sub['ourmat'] = $ourmat;
141 $sub['uid'] = $ourid;
0be07aa6 142 $sub['watch'] = $watch;
ecc734a5 143 $sub['naissance_ini'] = $naiss;
0337d704 144
145 return true;
146}
147
148// }}}
149// {{{ function create_aliases
150
151function create_aliases (&$sub)
152{
115c90db 153 global $globals;
0337d704 154 extract ($sub);
155
56081a9c 156 $mailorg = make_username($prenom, $nom);
0337d704 157 $mailorg2 = $mailorg.sprintf(".%02u", ($promo%100));
0337d704 158
d3df41c3 159 $res = XDB::query("SELECT hruid FROM auth_user_md5 WHERE user_id = {?} AND hruid != ''", $uid);
56081a9c
VZ
160 if ($res->numRows() == 0) {
161 return "Tu n'as pas d'adresse à vie pré-attribuée.<br />"
4ce2a30a 162 . "Envoie un mail à <a href=\"mailto:support@{$globals->mail->domain}</a>\">"
56081a9c
VZ
163 . "support@{$globals->mail->domain}</a> en expliquant ta situation.";
164 } else {
165 // TODO: at the moment forlife == hruid, however we'll have to change
166 // that behaviour when masters will be on plat/al.
167 $forlife = $res->fetchOneCell();
0337d704 168 }
eaf30d86 169
56081a9c
VZ
170 $res = XDB::query('SELECT id, type, expire FROM aliases WHERE alias={?}', $mailorg);
171 if ($res->numRows()) {
0337d704 172 list($h_id, $h_type, $expire) = $res->fetchOneRow();
56081a9c 173 if ($h_type != 'homonyme' and empty($expire)) {
08cce2ff 174 XDB::execute('UPDATE aliases SET expire=ADDDATE(NOW(),INTERVAL 1 MONTH) WHERE alias={?}', $mailorg);
175 XDB::execute('REPLACE INTO homonymes (homonyme_id,user_id) VALUES ({?},{?})', $h_id, $h_id);
176 XDB::execute('REPLACE INTO homonymes (homonyme_id,user_id) VALUES ({?},{?})', $h_id, $uid);
177 $res = XDB::query("SELECT alias FROM aliases WHERE id={?} AND expire IS NULL", $h_id);
0337d704 178 $als = $res->fetchColumn();
179
1e33266a 180 $mailer = new PlMailer();
1d55fe45 181 $mailer->setFrom('"Support Polytechnique.org" <support@' . $globals->mail->domain . '>');
182 $mailer->addTo("$mailorg@" . $globals->mail->domain);
0337d704 183 $mailer->setSubject("perte de ton alias $mailorg dans un mois !");
1d55fe45 184 $mailer->addCc('"Support Polytechnique.org" <support@' . $globals->mail->domain . '>');
0337d704 185 $msg =
e945001b 186 "Bonjour,\n\n".
eaf30d86 187
e945001b 188 "Un homonyme vient de s'inscrire. La politique de Polytechnique.org est de fournir des\n".
faefdbb7 189 "adresses email devinables, nous ne pouvons donc pas conserver ton alias '$mailorg' qui\n".
a7de4ef7 190 "correspond maintenant à deux personnes.\n\n".
eaf30d86 191
a7de4ef7 192 "Tu gardes tout de même l'usage de cet alias pour un mois encore à compter de ce jour.\n\n".
eaf30d86 193
a7de4ef7 194 "Lorsque cet alias sera désactivé, l'adresse $mailorg@polytechnique.org renverra vers un \n".
195 "robot qui indiquera qu'il y a plusieurs personnes portant le même nom ;\n".
196 "cela évite que l'un des homonymes reçoive des courriels destinés à l'autre.\n\n".
eaf30d86 197
e945001b 198 "Pour te connecter au site, tu pourras utiliser comme identifiant n'importe lequel de tes\n".
199 "autres alias :\n".
0337d704 200 " ".join(', ', $als)."\n";
a7de4ef7 201 "Commence dès aujourd'hui à communiquer à tes correspondants la nouvelle adresse que tu comptes utiliser !\n\n".
eaf30d86 202
a7de4ef7 203 "En nous excusant pour le désagrément occasionné,\n".
e945001b 204 "Cordialement,\n\n".
eaf30d86 205
0337d704 206 "-- \n".
a7de4ef7 207 "L'équipe de Polytechnique.org\n".
208 "\"Le portail des élèves & anciens élèves de l'X\"";
0337d704 209 $mailer->SetTxtBody(wordwrap($msg,72));
210 $mailer->send();
211 }
212
213 $sub['forlife'] = $forlife;
214 $sub['bestalias'] = $mailorg2;
215 $sub['mailorg2'] = null;
216 } else {
217 $sub['forlife'] = $forlife;
218 $sub['bestalias'] = $mailorg;
219 $sub['mailorg2'] = $mailorg2;
220 }
221
222 return true;
223}
224
225// }}}
226// {{{ function finish_ins
227
228function finish_ins($sub_state)
229{
230 global $globals;
231 extract($sub_state);
232
97a82cd2
VZ
233 $hash = rand_url_id(12);
234 XDB::execute(
235 "UPDATE auth_user_md5
236 SET last_known_email = {?}
237 WHERE matricule = {?}", $email, $mat);
08cce2ff 238 XDB::execute(
3546b253
VZ
239 "REPLACE INTO register_pending (uid, forlife, bestalias, mailorg2, password, email, date, relance, naissance, hash, services)
240 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, NOW(), 0, {?}, {?}, {?})",
241 $uid, $forlife, $bestalias, $mailorg2, $password, $email, $naissance, $hash, implode(',', $services));
0337d704 242
1e33266a 243 $mymail = new PlMailer('register/inscrire.mail.tpl');
0337d704 244 $mymail->assign('mailorg', $bestalias);
245 $mymail->assign('lemail', $email);
0337d704 246 $mymail->assign('baseurl', $globals->baseurl);
247 $mymail->assign('hash', $hash);
1d55fe45 248 $mymail->assign('subj', $bestalias."@" . $globals->mail->domain);
0337d704 249 $mymail->send();
250}
251
252// }}}
a7de4ef7 253// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 254?>