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