e59506eb |
1 | <?php |
2 | /*************************************************************************** |
3 | * Copyright (C) 2003-2006 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 | |
5de0b7e1 |
22 | function bugize($list) |
23 | { |
24 | $list = split(',', $list); |
25 | $ans = array(); |
26 | |
27 | foreach ($list as $bug) { |
28 | $clean = str_replace('#', '', $bug); |
29 | $ans[] = "<a href='http://trackers.polytechnique.org/task/$clean'>$bug</a>"; |
30 | } |
31 | |
32 | return join(',', $ans); |
33 | } |
34 | |
35 | |
e59506eb |
36 | class PlatalModule extends PLModule |
37 | { |
38 | function handlers() |
39 | { |
40 | return array( |
c9178c75 |
41 | 'index' => $this->make_hook('index', AUTH_PUBLIC), |
42 | 'cacert.pem' => $this->make_hook('cacert', AUTH_PUBLIC), |
5de0b7e1 |
43 | 'changelog' => $this->make_hook('changelog', AUTH_PUBLIC), |
44 | |
4da0b8d7 |
45 | // Preferences thingies |
bee33d93 |
46 | 'prefs' => $this->make_hook('prefs', AUTH_COOKIE), |
47 | 'prefs/rss' => $this->make_hook('prefs_rss', AUTH_COOKIE), |
bce2f8eb |
48 | 'prefs/webredirect' |
bee33d93 |
49 | => $this->make_hook('webredir', AUTH_MDP), |
50 | 'prefs/skin' => $this->make_hook('skin', AUTH_COOKIE), |
4da0b8d7 |
51 | |
52 | // password related thingies |
1a5da857 |
53 | 'password' => $this->make_hook('password', AUTH_MDP), |
54 | 'tmpPWD' => $this->make_hook('tmpPWD', AUTH_PUBLIC), |
55 | 'password/smtp' => $this->make_hook('smtppass', AUTH_MDP), |
8858cfc1 |
56 | 'recovery' => $this->make_hook('recovery', AUTH_PUBLIC), |
5de0b7e1 |
57 | 'exit' => $this->make_hook('exit', AUTH_PUBLIC), |
4da0b8d7 |
58 | |
59 | // happenings related thingies |
60 | 'rss' => $this->make_hook('rss', AUTH_PUBLIC), |
e59506eb |
61 | ); |
62 | } |
63 | |
c9178c75 |
64 | function handler_index(&$page) |
65 | { |
cab08090 |
66 | if (S::logged()) { |
8b00e0e0 |
67 | pl_redirect('events'); |
c9178c75 |
68 | } |
c9178c75 |
69 | } |
70 | |
5de0b7e1 |
71 | function handler_cacert(&$page) |
72 | { |
73 | $data = file_get_contents('/etc/ssl/xorgCA/cacert.pem'); |
74 | header('Content-Type: application/x-x509-ca-cert'); |
75 | header('Content-Length: '.strlen($data)); |
76 | echo $data; |
77 | exit; |
78 | } |
79 | |
80 | function handler_changelog(&$page) |
81 | { |
82 | $page->changeTpl('changeLog.tpl'); |
83 | |
84 | $clog = htmlentities(file_get_contents(dirname(__FILE__).'/../ChangeLog')); |
85 | $clog = preg_replace('!(#[0-9]+(,[0-9]+)*)!e', 'bugize("\1")', $clog); |
86 | $page->assign('ChangeLog', $clog); |
87 | } |
88 | |
7927d719 |
89 | function __set_rss_state($state) |
90 | { |
7927d719 |
91 | if ($state) { |
92 | $_SESSION['core_rss_hash'] = rand_url_id(16); |
08cce2ff |
93 | XDB::execute('UPDATE auth_user_quick |
7927d719 |
94 | SET core_rss_hash={?} WHERE user_id={?}', |
cab08090 |
95 | S::v('core_rss_hash'), S::v('uid')); |
7927d719 |
96 | } else { |
08cce2ff |
97 | XDB::execute('UPDATE auth_user_quick |
7927d719 |
98 | SET core_rss_hash="" WHERE user_id={?}', |
cab08090 |
99 | S::v('uid')); |
100 | S::kill('core_rss_hash'); |
7927d719 |
101 | } |
102 | } |
103 | |
e59506eb |
104 | function handler_prefs(&$page) |
105 | { |
e59506eb |
106 | $page->changeTpl('preferences.tpl'); |
107 | $page->assign('xorg_title','Polytechnique.org - Mes préférences'); |
108 | |
bee33d93 |
109 | if (Post::has('mail_fmt')) { |
5e2307dc |
110 | $fmt = Post::v('mail_fmt'); |
e59506eb |
111 | if ($fmt != 'texte') $fmt = 'html'; |
08cce2ff |
112 | XDB::execute("UPDATE auth_user_quick |
e59506eb |
113 | SET core_mail_fmt = '$fmt' |
114 | WHERE user_id = {?}", |
cab08090 |
115 | S::v('uid')); |
e59506eb |
116 | $_SESSION['mail_fmt'] = $fmt; |
e59506eb |
117 | } |
118 | |
bee33d93 |
119 | if (Post::has('rss')) { |
5e2307dc |
120 | $this->__set_rss_state(Post::b('rss')); |
e59506eb |
121 | } |
e59506eb |
122 | } |
9bae6004 |
123 | |
bce2f8eb |
124 | function handler_webredir(&$page) |
125 | { |
bce2f8eb |
126 | $page->changeTpl('webredirect.tpl'); |
127 | |
128 | $page->assign('xorg_title','Polytechnique.org - Redirection de page WEB'); |
129 | |
cab08090 |
130 | $log =& S::v('log'); |
5e2307dc |
131 | $url = Env::v('url'); |
bce2f8eb |
132 | |
5e2307dc |
133 | if (Env::v('submit') == 'Valider' and Env::has('url')) { |
08cce2ff |
134 | XDB::execute('UPDATE auth_user_quick |
bce2f8eb |
135 | SET redirecturl = {?} WHERE user_id = {?}', |
cab08090 |
136 | $url, S::v('uid')); |
5e2307dc |
137 | $log->log('carva_add', 'http://'.Env::v('url')); |
bce2f8eb |
138 | $page->trig("Redirection activée vers <a href='http://$url'>$url</a>"); |
5e2307dc |
139 | } elseif (Env::v('submit') == "Supprimer") { |
08cce2ff |
140 | XDB::execute("UPDATE auth_user_quick |
bce2f8eb |
141 | SET redirecturl = '' |
142 | WHERE user_id = {?}", |
cab08090 |
143 | S::v('uid')); |
bce2f8eb |
144 | $log->log("carva_del", $url); |
145 | Post::kill('url'); |
146 | $page->trig('Redirection supprimée'); |
147 | } |
148 | |
08cce2ff |
149 | $res = XDB::query('SELECT redirecturl |
bce2f8eb |
150 | FROM auth_user_quick |
151 | WHERE user_id = {?}', |
cab08090 |
152 | S::v('uid')); |
bce2f8eb |
153 | $page->assign('carva', $res->fetchOneCell()); |
bce2f8eb |
154 | } |
155 | |
4da0b8d7 |
156 | function handler_prefs_rss(&$page) |
7927d719 |
157 | { |
7927d719 |
158 | $page->changeTpl('filrss.tpl'); |
159 | |
5e2307dc |
160 | $page->assign('goback', Env::v('referer', 'login')); |
7927d719 |
161 | |
5e2307dc |
162 | if (Env::v('act_rss') == 'Activer') { |
7927d719 |
163 | $this->__set_rss_state(true); |
164 | $page->trig("Ton Fil RSS est activé."); |
165 | } |
7927d719 |
166 | } |
167 | |
7c77c3ee |
168 | function handler_password(&$page) |
169 | { |
7c77c3ee |
170 | if (Post::has('response2')) { |
171 | require_once 'secure_hash.inc.php'; |
172 | |
5e2307dc |
173 | $_SESSION['password'] = $password = Post::v('response2'); |
7c77c3ee |
174 | |
08cce2ff |
175 | XDB::execute('UPDATE auth_user_md5 |
7c77c3ee |
176 | SET password={?} |
177 | WHERE user_id={?}', $password, |
cab08090 |
178 | S::v('uid')); |
7c77c3ee |
179 | |
cab08090 |
180 | $log =& S::v('log'); |
7c77c3ee |
181 | $log->log('passwd', ''); |
182 | |
5e2307dc |
183 | if (Cookie::v('ORGaccess')) { |
7c77c3ee |
184 | setcookie('ORGaccess', hash_encrypt($password), (time()+25920000), '/', '' ,0); |
185 | } |
186 | |
187 | $page->changeTpl('motdepasse.success.tpl'); |
188 | $page->run(); |
189 | } |
190 | |
191 | $page->changeTpl('motdepasse.tpl'); |
192 | $page->addJsLink('javascript/motdepasse.js'); |
193 | $page->assign('xorg_title','Polytechnique.org - Mon mot de passe'); |
7c77c3ee |
194 | } |
195 | |
1a5da857 |
196 | function handler_smtppass(&$page) |
197 | { |
1a5da857 |
198 | $page->changeTpl('acces_smtp.tpl'); |
199 | $page->assign('xorg_title','Polytechnique.org - Acces SMTP/NNTP'); |
200 | |
cab08090 |
201 | $uid = S::v('uid'); |
5e2307dc |
202 | $pass = Env::v('smtppass1'); |
cab08090 |
203 | $log = S::v('log'); |
1a5da857 |
204 | |
5e2307dc |
205 | if (Env::v('op') == "Valider" && strlen($pass) >= 6 |
206 | && Env::v('smtppass1') == Env::v('smtppass2')) |
1a5da857 |
207 | { |
08cce2ff |
208 | XDB::execute('UPDATE auth_user_md5 SET smtppass = {?} |
1a5da857 |
209 | WHERE user_id = {?}', $pass, $uid); |
210 | $page->trig('Mot de passe enregistré'); |
211 | $log->log("passwd_ssl"); |
5e2307dc |
212 | } elseif (Env::v('op') == "Supprimer") { |
08cce2ff |
213 | XDB::execute('UPDATE auth_user_md5 SET smtppass = "" |
1a5da857 |
214 | WHERE user_id = {?}', $uid); |
215 | $page->trig('Compte SMTP et NNTP supprimé'); |
216 | $log->log("passwd_del"); |
217 | } |
218 | |
08cce2ff |
219 | $res = XDB::query("SELECT IF(smtppass != '', 'actif', '') |
1a5da857 |
220 | FROM auth_user_md5 |
221 | WHERE user_id = {?}", $uid); |
222 | $page->assign('actif', $res->fetchOneCell()); |
1a5da857 |
223 | } |
224 | |
8858cfc1 |
225 | function handler_recovery(&$page) |
226 | { |
227 | global $globals; |
228 | |
229 | $page->changeTpl('recovery.tpl'); |
230 | |
231 | if (!Env::has('login') || !Env::has('birth')) { |
fd8f77de |
232 | return; |
8858cfc1 |
233 | } |
234 | |
5e2307dc |
235 | if (!ereg('[0-3][0-9][0-1][0-9][1][9]([0-9]{2})', Env::v('birth'))) { |
c9110c6c |
236 | $page->trig('Date de naissance incorrecte ou incohérente'); |
237 | return; |
8858cfc1 |
238 | } |
c9110c6c |
239 | |
240 | $birth = sprintf('%s-%s-%s', |
5e2307dc |
241 | substr(Env::v('birth'), 4, 4), |
242 | substr(Env::v('birth'), 2, 2), |
243 | substr(Env::v('birth'), 0, 2)); |
8858cfc1 |
244 | |
5e2307dc |
245 | $mailorg = strtok(Env::v('login'), '@'); |
8858cfc1 |
246 | |
247 | // paragraphe rajouté : si la date de naissance dans la base n'existe pas, on l'update |
248 | // avec celle fournie ici en espérant que c'est la bonne |
249 | |
08cce2ff |
250 | $res = XDB::query( |
8858cfc1 |
251 | "SELECT user_id, naissance |
252 | FROM auth_user_md5 AS u |
253 | INNER JOIN aliases AS a ON (u.user_id=a.id AND type!='homonyme') |
254 | WHERE a.alias={?} AND u.perms IN ('admin','user') AND u.deces=0", $mailorg); |
255 | list($uid, $naissance) = $res->fetchOneRow(); |
256 | |
257 | if ($naissance == $birth) { |
258 | $page->assign('ok', true); |
259 | |
260 | $url = rand_url_id(); |
08cce2ff |
261 | XDB::execute('INSERT INTO perte_pass (certificat,uid,created) VALUES ({?},{?},NOW())', $url, $uid); |
262 | $res = XDB::query('SELECT email FROM emails WHERE uid = {?} AND NOT FIND_IN_SET("filter", flags)', $uid); |
8858cfc1 |
263 | $mails = implode(', ', $res->fetchColumn()); |
264 | |
265 | require_once "diogenes/diogenes.hermes.inc.php"; |
266 | $mymail = new HermesMailer(); |
267 | $mymail->setFrom('"Gestion des mots de passe" <support+password@polytechnique.org>'); |
268 | $mymail->addTo($mails); |
269 | $mymail->setSubject('Ton certificat d\'authentification'); |
270 | $mymail->setTxtBody("Visite la page suivante qui expire dans six heures : |
271 | {$globals->baseurl}/tmpPWD/$url |
272 | |
273 | Si en cliquant dessus tu n'y arrives pas, copie intégralement l'adresse dans la barre de ton navigateur. |
274 | |
275 | -- |
276 | Polytechnique.org |
5e2307dc |
277 | \"Le portail des élèves & anciens élèves de l'Ecole polytechnique\"".(Post::v('email') ? " |
8858cfc1 |
278 | |
279 | Adresse de secours : |
5e2307dc |
280 | ".Post::v('email') : "")." |
8858cfc1 |
281 | |
5e2307dc |
282 | Mail envoyé à ".Env::v('login')); |
8858cfc1 |
283 | $mymail->send(); |
284 | |
285 | // on cree un objet logger et on log l'evenement |
286 | $logger = $_SESSION['log'] = new DiogenesCoreLogger($uid); |
287 | $logger->log('recovery', $emails); |
288 | } else { |
289 | $page->trig('Pas de résultat correspondant aux champs entrés dans notre base de données.'); |
290 | } |
8858cfc1 |
291 | } |
292 | |
6c49d0af |
293 | function handler_tmpPWD(&$page, $certif = null) |
294 | { |
08cce2ff |
295 | XDB::execute('DELETE FROM perte_pass |
6c49d0af |
296 | WHERE DATE_SUB(NOW(), INTERVAL 380 MINUTE) > created'); |
297 | |
08cce2ff |
298 | $res = XDB::query('SELECT uid FROM perte_pass WHERE certificat={?}', $certif); |
6c49d0af |
299 | $ligne = $res->fetchOneAssoc(); |
300 | if (!$ligne) { |
301 | $page->changeTpl('index.tpl'); |
302 | $page->kill("Cette adresse n'existe pas ou n'existe plus sur le serveur."); |
303 | } |
304 | |
305 | $uid = $ligne["uid"]; |
306 | if (Post::has('response2')) { |
5e2307dc |
307 | $password = Post::v('response2'); |
6c49d0af |
308 | $logger = new DiogenesCoreLogger($uid); |
08cce2ff |
309 | XDB::query('UPDATE auth_user_md5 SET password={?} |
6c49d0af |
310 | WHERE user_id={?} AND perms IN("admin","user")', |
311 | $password, $uid); |
08cce2ff |
312 | XDB::query('DELETE FROM perte_pass WHERE certificat={?}', $certif); |
6c49d0af |
313 | $logger->log("passwd",""); |
314 | $page->changeTpl('tmpPWD.success.tpl'); |
315 | } else { |
316 | $page->changeTpl('motdepasse.tpl'); |
317 | $page->addJsLink('javascript/motdepasse.js'); |
318 | } |
6c49d0af |
319 | } |
320 | |
9bae6004 |
321 | function handler_skin(&$page) |
322 | { |
323 | global $globals; |
324 | |
f09cee18 |
325 | $page->changeTpl('skins.tpl'); |
9bae6004 |
326 | $page->assign('xorg_title','Polytechnique.org - Skins'); |
327 | |
328 | if (Env::has('newskin')) { // formulaire soumis, traitons les données envoyées |
08cce2ff |
329 | XDB::execute('UPDATE auth_user_quick |
63528107 |
330 | SET skin={?} WHERE user_id={?}', |
5e2307dc |
331 | Env::i('newskin'), S::v('uid')); |
92e6a287 |
332 | S::kill('skin'); |
9bae6004 |
333 | set_skin(); |
334 | } |
335 | |
92e6a287 |
336 | $res = XDB::query('SELECT id FROM skins WHERE skin_tpl={?}', S::v('skin')); |
337 | $page->assign('skin_id', $res->fetchOneCell()); |
338 | |
9bae6004 |
339 | $sql = "SELECT s.*,auteur,count(*) AS nb |
340 | FROM skins AS s |
341 | LEFT JOIN auth_user_quick AS a ON s.id=a.skin |
342 | WHERE skin_tpl != '' AND ext != '' |
343 | GROUP BY id ORDER BY s.date DESC"; |
08cce2ff |
344 | $page->assign_by_ref('skins', XDB::iterator($sql)); |
9bae6004 |
345 | } |
4da0b8d7 |
346 | |
5de0b7e1 |
347 | function handler_exit(&$page, $level = null) |
348 | { |
cab08090 |
349 | if (S::has('suid')) { |
350 | if (S::has('suid')) { |
351 | $a4l = S::v('forlife'); |
352 | $suid = S::v('suid'); |
353 | $log = S::v('log'); |
354 | $log->log("suid_stop", S::v('forlife') . " by " . $suid['forlife']); |
5de0b7e1 |
355 | $_SESSION = $suid; |
cab08090 |
356 | S::kill('suid'); |
8b00e0e0 |
357 | pl_redirect('admin/utilisateurs.php', 'login='.$a4l); |
5de0b7e1 |
358 | } else { |
8b00e0e0 |
359 | pl_redirect('events'); |
5de0b7e1 |
360 | } |
361 | } |
362 | |
363 | if ($level == 'forget' || $level == 'forgetall') { |
364 | setcookie('ORGaccess', '', time() - 3600, '/', '', 0); |
365 | Cookie::kill('ORGaccess'); |
366 | if (isset($_SESSION['log'])) |
367 | $_SESSION['log']->log("cookie_off"); |
368 | } |
369 | |
370 | if ($level == 'forgetuid' || $level == 'forgetall') { |
371 | setcookie('ORGuid', '', time() - 3600, '/', '', 0); |
372 | Cookie::kill('ORGuid'); |
373 | setcookie('ORGdomain', '', time() - 3600, '/', '', 0); |
374 | Cookie::kill('ORGdomain'); |
375 | } |
376 | |
377 | if (isset($_SESSION['log'])) { |
378 | $ref = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; |
379 | $_SESSION['log']->log('deconnexion',$ref); |
380 | } |
381 | |
382 | XorgSession::destroy(); |
383 | |
384 | if (Get::has('redirect')) { |
5e2307dc |
385 | http_redirect(rawurldecode(Get::v('redirect'))); |
5de0b7e1 |
386 | } else { |
387 | $page->changeTpl('exit.tpl'); |
388 | } |
5de0b7e1 |
389 | } |
390 | |
4da0b8d7 |
391 | function handler_rss(&$page, $user = null, $hash = null) |
392 | { |
4da0b8d7 |
393 | require_once 'rss.inc.php'; |
394 | |
395 | $uid = init_rss('rss.tpl', $user, $hash); |
396 | |
08cce2ff |
397 | $rss = XDB::iterator( |
4da0b8d7 |
398 | 'SELECT e.id, e.titre, e.texte, e.creation_date |
399 | FROM auth_user_md5 AS u |
400 | INNER JOIN evenements AS e ON ( (e.promo_min = 0 || e.promo_min <= u.promo) |
401 | AND (e.promo_max = 0 || e.promo_max >= u.promo) ) |
402 | WHERE u.user_id = {?} AND FIND_IN_SET(e.flags, "valide") |
403 | AND peremption >= NOW()', $uid); |
404 | $page->assign('rss', $rss); |
4da0b8d7 |
405 | } |
e59506eb |
406 | } |
407 | |
408 | ?> |