2 /***************************************************************************
3 * Copyright (C) 2003-2004 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 require_once 'xorg.misc.inc.php';
24 // {{{ function user_cmp
26 function user_cmp($prenom, $nom, $_prenom, $_nom)
28 $_nom = strtoupper(replace_accent($_nom));
29 $_prenom = strtoupper(replace_accent($_prenom));
30 $nom = strtoupper(replace_accent($nom));
31 $prenom = strtoupper(replace_accent($prenom));
33 $is_ok = strtoupper($_prenom) == strtoupper($prenom);
35 $tokens = preg_split("/[ \-']/", $nom, -1, PREG_SPLIT_NO_EMPTY
);
38 foreach ($tokens as $str) {
39 $is_ok &= strpos($_nom, $str)!==false
;
40 $maxlen = max($maxlen, strlen($str));
43 return $is_ok && ($maxlen > 2 ||
$maxlen == strlen($_nom));
47 // {{{ function get_X_mat
48 function get_X_mat($ourmat)
50 if (!preg_match('/^[0-9]{8}$/', $ourmat)) {
51 // le matricule de notre base doit comporter 8 chiffres
55 $year = intval(substr($ourmat, 0, 4));
56 $rang = intval(substr($ourmat, 5, 3));
59 } elseif ($year < 2000) {
60 $year = intval(substr(1900 - $year, 1, 3));
61 return sprintf('%02u0%03u', $year, $rang);
63 $year = intval(substr(1900 - $year, 1, 3));
64 return sprintf('%03u%03u', $year, $rang);
69 // {{{ function check_mat
71 function check_mat($promo, $mat, $nom, $prenom, &$ourmat, &$ourid)
74 if (!preg_match('/^[0-9][0-9][0-9][0-9][0-9][0-9]$/', $mat)) {
75 return "Le matricule doit comporter 6 chiffres.";
78 $year = intval(substr($mat, 0, 3));
79 $rang = intval(substr($mat, 3, 3));
80 if ($year > 200) { $year /= 10; };
82 return "ton matricule est incorrect";
84 $ourmat = sprintf('%04u%04u', 1900+
$year, $rang);
87 $res = $globals->xdb
->query(
88 'SELECT user_id, promo, perms IN ("admin","user"), nom, prenom
90 WHERE matricule={?} and deces = 0', $ourmat);
91 list ($uid, $_promo, $_already, $_nom, $_prenom) = $res->fetchOneRow();
92 if ($_already) { return "tu es déjà inscrit ou ton matricule est incorrect !"; }
93 if ($_promo != $promo) { return "erreur de matricule"; }
95 if (!user_cmp($prenom, $nom, $_prenom, $_nom)) {
96 return "erreur dans l'identification. Réessaie, il y a une erreur quelque part !";
104 // {{{ function check_old_mat
106 function check_old_mat($promo, $mat, $nom, $prenom, &$ourmat, &$ourid)
110 $res = $globals->xdb
->iterRow(
111 'SELECT user_id, nom, prenom, matricule
113 WHERE promo={?} AND deces=0 AND perms="pending"', $promo);
114 while (list($_uid, $_nom, $_prenom, $_mat) = $res->next()) {
115 if (user_cmp($prenom, $nom, $_prenom, $_nom)) {
122 $res = $globals->xdb
->iterRow(
123 'SELECT user_id, nom, prenom, matricule, alias
124 FROM auth_user_md5 AS u
125 INNER JOIN aliases AS a ON (u.user_id = a.id and FIND_IN_SET("bestalias", a.flags))
126 WHERE promo={?} AND deces=0 AND perms IN ("user","admin")', $promo);
127 while (list($_uid, $_nom, $_prenom, $_mat, $alias) = $res->next()) {
128 if (user_cmp($prenom, $nom, $_prenom, $_nom)) {
131 return "Tu es vraissemblablement déjà inscrit !";
134 return "erreur: vérifie que tu as bien orthographié ton nom !";
138 // {{{ function check_new_user
140 function check_new_user(&$sub)
145 $prenom = preg_replace("/[ \t]+/", ' ', trim($prenom));
146 $prenom = preg_replace("/--+/", '-', $prenom);
147 $prenom = preg_replace("/''+/", '\'', $prenom);
148 $prenom = make_firstname_case($prenom);
150 $nom = preg_replace("/[ \t]+/", ' ', trim($nom));
151 $nom = preg_replace("/--+/", '-', $nom);
152 $nom = preg_replace("/''+/", '\'', $nom);
153 $nom = strtoupper(replace_accent($nom));
155 if ($promo >= 1996) {
156 $res = check_mat($promo, $mat, $nom, $prenom, $ourmat, $ourid);
158 $res = check_old_mat($promo, $mat, $nom, $prenom, $ourmat, $ourid);
160 if ($res !== true
) { return $res; }
163 $sub['prenom'] = $prenom;
164 $sub['ourmat'] = $ourmat;
165 $sub['uid'] = $ourid;
171 // {{{ function create_aliases
173 function create_aliases (&$sub)
178 $mailorg = make_username($prenom, $nom);
179 $mailorg2 = $mailorg.sprintf(".%02u", ($promo%100
));
180 $forlife = make_forlife($prenom, $nom, $promo);
182 $res = $globals->xdb
->query('SELECT COUNT(*) FROM aliases WHERE alias={?}', $forlife);
183 if ($res->fetchOneCell() > 0) {
184 return "Tu as un homonyme dans ta promo, il faut traiter ce cas manuellement.<br />".
185 "envoie un mail à <a href=\"mailto:support@polytechnique.org\">support@polytechnique.org</a> en expliquant ta situation.";
188 $res = $globals->xdb
->query('SELECT id, type, expire FROM aliases WHERE alias={?}', $mailorg);
190 if ( $res->numRows() ) {
192 list($h_id, $h_type, $expire) = $res->fetchOneRow();
195 if ( $h_type != 'homonyme' and empty($expire) ) {
196 $globals->xdb
->execute('UPDATE aliases SET expire=ADDDATE(NOW(),INTERVAL 1 MONTH) WHERE alias={?}', $mailorg);
197 $globals->xdb
->execute('REPLACE INTO homonymes (homonyme_id,user_id) VALUES ({?},{?})', $h_id, $h_id);
198 $globals->xdb
->execute('REPLACE INTO homonymes (homonyme_id,user_id) VALUES ({?},{?})', $h_id, $uid);
199 $res = $globals->xdb
->query("SELECT alias FROM aliases WHERE id={?} AND expire IS NULL", $h_id);
200 $als = $res->fetchColumn();
202 require_once('diogenes/diogenes.hermes.inc.php');
203 $mailer = new HermesMailer();
204 $mailer->setFrom('"Support Polytechnique.org" <support@polytechnique.org>');
205 $mailer->addTo("$mailorg@polytechnique.org");
206 $mailer->setSubject("perte de ton alias $mailorg dans un mois !");
207 $mailer->addCc('"Support Polytechnique.org" <support@polytechnique.org>');
211 "Un homonyme vient de s'inscrire. La politique de Polytechnique.org est de fournir des\n".
212 "adresses mail devinables, nous ne pouvons donc pas conserver ton alias '$mailorg' qui\n".
213 "correspond maintenant à deux personnes.\n\n".
215 "Tu gardes tout de même l'usage de cet alias pour un mois encore à compter de ce jour.\n\n".
217 "Lorsque cet alias sera désactivé, l'adresse $mailorg@polytechnique.org renverra vers un \n".
218 "robot qui indiquera qu'il y a plusieurs personnes portant le même nom ;\n".
219 "cela évite que l'un des homonymes reçoive des courriels destinés à l'autre.\n\n".
221 "Pour te connecter au site, tu pourras utiliser comme identifiant n'importe lequel de tes\n".
223 " ".join(', ', $als)."\n";
224 "Commence dès aujourd'hui à communiquer à tes correspondants la nouvelle adresse que tu comptes utiliser !\n\n".
226 "En nous excusant pour le désagrément occasionné,\n".
230 "L'équipe de Polytechnique.org\n".
231 "\"Le portail des élèves & anciens élèves de l'X\"";
232 $mailer->SetTxtBody(wordwrap($msg,72));
236 $sub['forlife'] = $forlife;
237 $sub['bestalias'] = $mailorg2;
238 $sub['mailorg2'] = null
;
240 $sub['forlife'] = $forlife;
241 $sub['bestalias'] = $mailorg;
242 $sub['mailorg2'] = $mailorg2;
249 // {{{ function finish_ins
251 function finish_ins($sub_state)
257 $pass_md5 = md5($pass_clair);
258 $hash = rand_url_id(12);
260 $globals->xdb
->execute('UPDATE auth_user_md5 SET last_known_email={?} WHERE matricule = {?}', $email, $mat);
262 $globals->xdb
->execute(
263 "REPLACE INTO register_pending (uid, forlife, bestalias, mailorg2, password, email, date, relance, naissance, hash)
264 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, NOW(), 0, {?}, {?})",
265 $uid, $forlife, $bestalias, $mailorg2, $pass_md5, $email, $naissance, $hash);
267 require_once('xorg.mailer.inc.php');
268 $mymail = new XOrgMailer('inscrire.mail.tpl');
269 $mymail->assign('mailorg', $bestalias);
270 $mymail->assign('lemail', $email);
271 $mymail->assign('pass', $pass);
272 $mymail->assign('baseurl', $globals->baseurl
);
273 $mymail->assign('hash', $hash);
274 $mymail->assign('subj', $bestalias."@polytechnique.org");