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