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