a5abd6a1b0d9cff1ab27fad01e282819e0398836
[platal.git] / modules / register / register.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
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
22 // {{{ function check_mat
23
24 function check_mat($promo, $mat, &$nom, &$prenom, &$ourmat, &$ourid, &$watch, &$naiss)
25 {
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
39 $res = XDB::query(
40 'SELECT user_id, promo, perms IN ("admin","user"), nom, prenom, FIND_IN_SET(\'watch\', flags), naissance_ini
41 FROM auth_user_md5
42 WHERE matricule={?} and deces = 0', $ourmat);
43 list ($uid, $_promo, $_already, $_nom, $_prenom, $watch, $naiss) = $res->fetchOneRow();
44 if ($_already) { return "tu es déjà inscrit ou ton matricule est incorrect !"; }
45 if ($_promo != $promo) { return "erreur de matricule"; }
46
47 if (!user_cmp($prenom, $nom, $_prenom, $_nom)) {
48 return "erreur dans l'identification. Réessaie, il y a une erreur quelque part !";
49 }
50
51 $nom = $_nom;
52 $prenom = $_prenom;
53 $ourid = $uid;
54 return true;
55 }
56
57 // }}}
58 // {{{ function check_old_mat
59
60 function check_old_mat($promo, $mat, &$nom, &$prenom, &$ourmat, &$ourid, &$watch, &$naiss)
61 {
62 $res = XDB::iterRow(
63 'SELECT user_id, nom, prenom, matricule, FIND_IN_SET(\'watch\', flags), naissance_ini
64 FROM auth_user_md5
65 WHERE promo={?} AND deces=0 AND perms="pending"', $promo);
66 while (list($_uid, $_nom, $_prenom, $_mat, $watch, $naiss) = $res->next()) {
67 if (user_cmp($prenom, $nom, $_prenom, $_nom)) {
68 $nom = $_nom;
69 $prenom = $_prenom;
70 $ourid = $_uid;
71 $ourmat = $_mat;
72 return true;
73 }
74 }
75
76 $res = XDB::iterRow(
77 'SELECT user_id, nom, prenom, matricule, alias, FIND_IN_SET(\'watch\', u.flags), naissance_ini
78 FROM auth_user_md5 AS u
79 INNER JOIN aliases AS a ON (u.user_id = a.uid and FIND_IN_SET("bestalias", a.flags))
80 WHERE promo={?} AND deces=0 AND perms IN ("user","admin")', $promo);
81 while (list($_uid, $_nom, $_prenom, $_mat, $alias, $watch, $naiss) = $res->next()) {
82 if (user_cmp($prenom, $nom, $_prenom, $_nom)) {
83 $ourid = $_uid;
84 $ourmat = $_mat;
85 return "Tu es vraisemblablement déjà inscrit !";
86 }
87 }
88 return "erreur: vérifie que tu as bien orthographié ton nom !";
89 }
90
91 // }}}
92 // {{{ function check_new_user
93
94 function check_new_user(&$sub)
95 {
96 extract($sub);
97
98 $prenom = preg_replace("/[ \t]+/", ' ', trim($prenom));
99 $prenom = preg_replace("/--+/", '-', $prenom);
100 $prenom = preg_replace("/''+/", '\'', $prenom);
101 $prenom = make_firstname_case($prenom);
102
103 $nom = preg_replace("/[ \t]+/", ' ', trim($nom));
104 $nom = preg_replace("/--+/", '-', $nom);
105 $nom = preg_replace("/''+/", '\'', $nom);
106 $nom = mb_strtoupper($nom);
107
108 if ($promo >= 1996) {
109 $res = check_mat($promo, $mat, $nom, $prenom, $ourmat, $ourid, $watch, $naiss);
110 } else {
111 $res = check_old_mat($promo, $mat, $nom, $prenom, $ourmat, $ourid, $watch, $naiss);
112 }
113 if ($res !== true) { return $res; }
114
115 $sub['nom'] = $nom;
116 $sub['prenom'] = $prenom;
117 $sub['ourmat'] = $ourmat;
118 $sub['uid'] = $ourid;
119 $sub['watch'] = $watch;
120 $sub['naissance_ini'] = $naiss;
121
122 return true;
123 }
124
125 // }}}
126 // {{{ function create_aliases
127
128 function create_aliases (&$sub)
129 {
130 global $globals;
131 extract ($sub);
132
133 $mailorg = makeUsername($prenom, $nom);
134 $mailorg2 = $mailorg.sprintf(".%02u", ($promo%100));
135
136 $res = XDB::query("SELECT hruid FROM auth_user_md5 WHERE user_id = {?} AND hruid != ''", $uid);
137 if ($res->numRows() == 0) {
138 return "Tu n'as pas d'adresse à vie pré-attribuée.<br />"
139 . "Envoie un mail à <a href=\"mailto:support@{$globals->mail->domain}</a>\">"
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();
145 }
146
147 $res = XDB::query('SELECT uid, type, expire FROM aliases WHERE alias={?}', $mailorg);
148 if ($res->numRows()) {
149 list($h_id, $h_type, $expire) = $res->fetchOneRow();
150 if ($h_type != 'homonyme' and empty($expire)) {
151 XDB::execute('UPDATE aliases SET expire=ADDDATE(NOW(),INTERVAL 1 MONTH) WHERE alias={?}', $mailorg);
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);
154 $res = XDB::query("SELECT alias FROM aliases WHERE uid={?} AND expire IS NULL", $h_id);
155 $als = $res->fetchColumn();
156
157 $mailer = new PlMailer();
158 $mailer->setFrom('"Support Polytechnique.org" <support@' . $globals->mail->domain . '>');
159 $mailer->addTo("$mailorg@" . $globals->mail->domain);
160 $mailer->setSubject("perte de ton alias $mailorg dans un mois !");
161 $mailer->addCc('"Support Polytechnique.org" <support@' . $globals->mail->domain . '>');
162 $msg =
163 "Bonjour,\n\n".
164
165 "Un homonyme vient de s'inscrire. La politique de Polytechnique.org est de fournir des\n".
166 "adresses email devinables, nous ne pouvons donc pas conserver ton alias '$mailorg' qui\n".
167 "correspond maintenant à deux personnes.\n\n".
168
169 "Tu gardes tout de même l'usage de cet alias pour un mois encore à compter de ce jour.\n\n".
170
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".
174
175 "Pour te connecter au site, tu pourras utiliser comme identifiant n'importe lequel de tes\n".
176 "autres alias :\n".
177 " ".join(', ', $als)."\n";
178 "Commence dès aujourd'hui à communiquer à tes correspondants la nouvelle adresse que tu comptes utiliser !\n\n".
179
180 "En nous excusant pour le désagrément occasionné,\n".
181 "Cordialement,\n\n".
182
183 "-- \n".
184 "L'équipe de Polytechnique.org\n".
185 "\"Le portail des élèves & anciens élèves de l'X\"";
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
205 function finish_ins($sub_state)
206 {
207 global $globals;
208 extract($sub_state);
209
210 $hash = rand_url_id(12);
211 XDB::execute(
212 "UPDATE auth_user_md5
213 SET last_known_email = {?}
214 WHERE matricule = {?}", $email, $mat);
215 XDB::execute(
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));
219
220 $mymail = new PlMailer('register/inscrire.mail.tpl');
221 $mymail->assign('mailorg', $bestalias);
222 $mymail->assign('lemail', $email);
223 $mymail->assign('baseurl', $globals->baseurl);
224 $mymail->assign('hash', $hash);
225 $mymail->assign('subj', $bestalias."@" . $globals->mail->domain);
226 $mymail->send();
227 }
228
229 // }}}
230 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
231 ?>