crappy stupid .in file
[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
1a5da857 46 'prefs' => $this->make_hook('prefs', AUTH_COOKIE),
47 'prefs/rss' => $this->make_hook('prefs_rss', AUTH_COOKIE),
bce2f8eb 48 'prefs/webredirect'
49 => $this->make_hook('webredir', AUTH_MDP),
1a5da857 50 '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()) {
c9178c75 67 redirect("events");
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 {
106 global $globals;
107
108 $page->changeTpl('preferences.tpl');
109 $page->assign('xorg_title','Polytechnique.org - Mes préférences');
110
111 if (Env::has('mail_fmt')) {
112 $fmt = Env::get('mail_fmt');
113 if ($fmt != 'texte') $fmt = 'html';
08cce2ff 114 XDB::execute("UPDATE auth_user_quick
e59506eb 115 SET core_mail_fmt = '$fmt'
116 WHERE user_id = {?}",
cab08090 117 S::v('uid'));
e59506eb 118 $_SESSION['mail_fmt'] = $fmt;
20d90835 119 redirect($globals->baseurl.'/preferences');
e59506eb 120 }
121
122 if (Env::has('rss')) {
7927d719 123 $this->__set_rss_state(Env::getBool('rss'));
e59506eb 124 }
125
126 $page->assign('prefs', $globals->hook->prefs());
127 }
9bae6004 128
bce2f8eb 129 function handler_webredir(&$page)
130 {
bce2f8eb 131 $page->changeTpl('webredirect.tpl');
132
133 $page->assign('xorg_title','Polytechnique.org - Redirection de page WEB');
134
cab08090 135 $log =& S::v('log');
bce2f8eb 136 $url = Env::get('url');
137
138 if (Env::get('submit') == 'Valider' and Env::has('url')) {
08cce2ff 139 XDB::execute('UPDATE auth_user_quick
bce2f8eb 140 SET redirecturl = {?} WHERE user_id = {?}',
cab08090 141 $url, S::v('uid'));
bce2f8eb 142 $log->log('carva_add', 'http://'.Env::get('url'));
143 $page->trig("Redirection activée vers <a href='http://$url'>$url</a>");
144 } elseif (Env::get('submit') == "Supprimer") {
08cce2ff 145 XDB::execute("UPDATE auth_user_quick
bce2f8eb 146 SET redirecturl = ''
147 WHERE user_id = {?}",
cab08090 148 S::v('uid'));
bce2f8eb 149 $log->log("carva_del", $url);
150 Post::kill('url');
151 $page->trig('Redirection supprimée');
152 }
153
08cce2ff 154 $res = XDB::query('SELECT redirecturl
bce2f8eb 155 FROM auth_user_quick
156 WHERE user_id = {?}',
cab08090 157 S::v('uid'));
bce2f8eb 158 $page->assign('carva', $res->fetchOneCell());
bce2f8eb 159 }
160
4da0b8d7 161 function handler_prefs_rss(&$page)
7927d719 162 {
7927d719 163 $page->changeTpl('filrss.tpl');
164
165 $page->assign('goback', Env::get('referer', 'login'));
166
167 if (Env::get('act_rss') == 'Activer') {
168 $this->__set_rss_state(true);
169 $page->trig("Ton Fil RSS est activé.");
170 }
7927d719 171 }
172
7c77c3ee 173 function handler_password(&$page)
174 {
7c77c3ee 175 if (Post::has('response2')) {
176 require_once 'secure_hash.inc.php';
177
178 $_SESSION['password'] = $password = Post::get('response2');
179
08cce2ff 180 XDB::execute('UPDATE auth_user_md5
7c77c3ee 181 SET password={?}
182 WHERE user_id={?}', $password,
cab08090 183 S::v('uid'));
7c77c3ee 184
cab08090 185 $log =& S::v('log');
7c77c3ee 186 $log->log('passwd', '');
187
188 if (Cookie::get('ORGaccess')) {
189 setcookie('ORGaccess', hash_encrypt($password), (time()+25920000), '/', '' ,0);
190 }
191
192 $page->changeTpl('motdepasse.success.tpl');
193 $page->run();
194 }
195
196 $page->changeTpl('motdepasse.tpl');
197 $page->addJsLink('javascript/motdepasse.js');
198 $page->assign('xorg_title','Polytechnique.org - Mon mot de passe');
7c77c3ee 199 }
200
1a5da857 201 function handler_smtppass(&$page)
202 {
1a5da857 203 $page->changeTpl('acces_smtp.tpl');
204 $page->assign('xorg_title','Polytechnique.org - Acces SMTP/NNTP');
205
cab08090 206 $uid = S::v('uid');
1a5da857 207 $pass = Env::get('smtppass1');
cab08090 208 $log = S::v('log');
1a5da857 209
210 if (Env::get('op') == "Valider" && strlen($pass) >= 6
211 && Env::get('smtppass1') == Env::get('smtppass2'))
212 {
08cce2ff 213 XDB::execute('UPDATE auth_user_md5 SET smtppass = {?}
1a5da857 214 WHERE user_id = {?}', $pass, $uid);
215 $page->trig('Mot de passe enregistré');
216 $log->log("passwd_ssl");
217 } elseif (Env::get('op') == "Supprimer") {
08cce2ff 218 XDB::execute('UPDATE auth_user_md5 SET smtppass = ""
1a5da857 219 WHERE user_id = {?}', $uid);
220 $page->trig('Compte SMTP et NNTP supprimé');
221 $log->log("passwd_del");
222 }
223
08cce2ff 224 $res = XDB::query("SELECT IF(smtppass != '', 'actif', '')
1a5da857 225 FROM auth_user_md5
226 WHERE user_id = {?}", $uid);
227 $page->assign('actif', $res->fetchOneCell());
1a5da857 228 }
229
8858cfc1 230 function handler_recovery(&$page)
231 {
232 global $globals;
233
234 $page->changeTpl('recovery.tpl');
235
236 if (!Env::has('login') || !Env::has('birth')) {
fd8f77de 237 return;
8858cfc1 238 }
239
240 if (!ereg('[0-3][0-9][0-1][0-9][1][9]([0-9]{2})', Env::get('birth'))) {
241 $page->trig_run('Date de naissance incorrecte ou incohérente');
242 }
243 $birth = sprintf('%s-%s-%s', substr(Env::get('birth'),4,4), substr(Env::get('birth'),2,2), substr(Env::get('birth'),0,2));
244
245 $mailorg = strtok(Env::get('login'), '@');
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
273Si en cliquant dessus tu n'y arrives pas, copie intégralement l'adresse dans la barre de ton navigateur.
274
275--
276Polytechnique.org
277\"Le portail des élèves & anciens élèves de l'Ecole polytechnique\"".(Post::get('email') ? "
278
279Adresse de secours :
280 ".Post::get('email') : "")."
281
282Mail envoyé à ".Env::get('login'));
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')) {
307 $password = Post::get('response2');
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={?}',
331 Env::getInt('newskin'), S::v('uid'));
9bae6004 332 set_skin();
333 }
334
335 $sql = "SELECT s.*,auteur,count(*) AS nb
336 FROM skins AS s
337 LEFT JOIN auth_user_quick AS a ON s.id=a.skin
338 WHERE skin_tpl != '' AND ext != ''
339 GROUP BY id ORDER BY s.date DESC";
08cce2ff 340 $page->assign_by_ref('skins', XDB::iterator($sql));
9bae6004 341 }
4da0b8d7 342
5de0b7e1 343 function handler_exit(&$page, $level = null)
344 {
cab08090 345 if (S::has('suid')) {
346 if (S::has('suid')) {
347 $a4l = S::v('forlife');
348 $suid = S::v('suid');
349 $log = S::v('log');
350 $log->log("suid_stop", S::v('forlife') . " by " . $suid['forlife']);
5de0b7e1 351 $_SESSION = $suid;
cab08090 352 S::kill('suid');
5de0b7e1 353 redirect($globals->baseurl.'/admin/utilisateurs.php?login='.$a4l);
354 } else {
355 redirect("events");
356 }
357 }
358
359 if ($level == 'forget' || $level == 'forgetall') {
360 setcookie('ORGaccess', '', time() - 3600, '/', '', 0);
361 Cookie::kill('ORGaccess');
362 if (isset($_SESSION['log']))
363 $_SESSION['log']->log("cookie_off");
364 }
365
366 if ($level == 'forgetuid' || $level == 'forgetall') {
367 setcookie('ORGuid', '', time() - 3600, '/', '', 0);
368 Cookie::kill('ORGuid');
369 setcookie('ORGdomain', '', time() - 3600, '/', '', 0);
370 Cookie::kill('ORGdomain');
371 }
372
373 if (isset($_SESSION['log'])) {
374 $ref = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
375 $_SESSION['log']->log('deconnexion',$ref);
376 }
377
378 XorgSession::destroy();
379
380 if (Get::has('redirect')) {
381 redirect(rawurldecode(Get::get('redirect')));
382 } else {
383 $page->changeTpl('exit.tpl');
384 }
5de0b7e1 385 }
386
4da0b8d7 387 function handler_rss(&$page, $user = null, $hash = null)
388 {
4da0b8d7 389 require_once 'rss.inc.php';
390
391 $uid = init_rss('rss.tpl', $user, $hash);
392
08cce2ff 393 $rss = XDB::iterator(
4da0b8d7 394 'SELECT e.id, e.titre, e.texte, e.creation_date
395 FROM auth_user_md5 AS u
396 INNER JOIN evenements AS e ON ( (e.promo_min = 0 || e.promo_min <= u.promo)
397 AND (e.promo_max = 0 || e.promo_max >= u.promo) )
398 WHERE u.user_id = {?} AND FIND_IN_SET(e.flags, "valide")
399 AND peremption >= NOW()', $uid);
400 $page->assign('rss', $rss);
4da0b8d7 401 }
e59506eb 402}
403
404?>