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