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