merge the two files now that they are smaller.
[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),
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 {
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')) {
110 $fmt = Post::get('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')) {
120 $this->__set_rss_state(Post::getBool('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');
bce2f8eb 131 $url = Env::get('url');
132
133 if (Env::get('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'));
bce2f8eb 137 $log->log('carva_add', 'http://'.Env::get('url'));
138 $page->trig("Redirection activée vers <a href='http://$url'>$url</a>");
139 } elseif (Env::get('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
160 $page->assign('goback', Env::get('referer', 'login'));
161
162 if (Env::get('act_rss') == 'Activer') {
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
173 $_SESSION['password'] = $password = Post::get('response2');
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
183 if (Cookie::get('ORGaccess')) {
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');
1a5da857 202 $pass = Env::get('smtppass1');
cab08090 203 $log = S::v('log');
1a5da857 204
205 if (Env::get('op') == "Valider" && strlen($pass) >= 6
206 && Env::get('smtppass1') == Env::get('smtppass2'))
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");
212 } elseif (Env::get('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
235 if (!ereg('[0-3][0-9][0-1][0-9][1][9]([0-9]{2})', Env::get('birth'))) {
236 $page->trig_run('Date de naissance incorrecte ou incohérente');
237 }
238 $birth = sprintf('%s-%s-%s', substr(Env::get('birth'),4,4), substr(Env::get('birth'),2,2), substr(Env::get('birth'),0,2));
239
240 $mailorg = strtok(Env::get('login'), '@');
241
242 // paragraphe rajouté : si la date de naissance dans la base n'existe pas, on l'update
243 // avec celle fournie ici en espérant que c'est la bonne
244
08cce2ff 245 $res = XDB::query(
8858cfc1 246 "SELECT user_id, naissance
247 FROM auth_user_md5 AS u
248 INNER JOIN aliases AS a ON (u.user_id=a.id AND type!='homonyme')
249 WHERE a.alias={?} AND u.perms IN ('admin','user') AND u.deces=0", $mailorg);
250 list($uid, $naissance) = $res->fetchOneRow();
251
252 if ($naissance == $birth) {
253 $page->assign('ok', true);
254
255 $url = rand_url_id();
08cce2ff 256 XDB::execute('INSERT INTO perte_pass (certificat,uid,created) VALUES ({?},{?},NOW())', $url, $uid);
257 $res = XDB::query('SELECT email FROM emails WHERE uid = {?} AND NOT FIND_IN_SET("filter", flags)', $uid);
8858cfc1 258 $mails = implode(', ', $res->fetchColumn());
259
260 require_once "diogenes/diogenes.hermes.inc.php";
261 $mymail = new HermesMailer();
262 $mymail->setFrom('"Gestion des mots de passe" <support+password@polytechnique.org>');
263 $mymail->addTo($mails);
264 $mymail->setSubject('Ton certificat d\'authentification');
265 $mymail->setTxtBody("Visite la page suivante qui expire dans six heures :
266{$globals->baseurl}/tmpPWD/$url
267
268Si en cliquant dessus tu n'y arrives pas, copie intégralement l'adresse dans la barre de ton navigateur.
269
270--
271Polytechnique.org
272\"Le portail des élèves & anciens élèves de l'Ecole polytechnique\"".(Post::get('email') ? "
273
274Adresse de secours :
275 ".Post::get('email') : "")."
276
277Mail envoyé à ".Env::get('login'));
278 $mymail->send();
279
280 // on cree un objet logger et on log l'evenement
281 $logger = $_SESSION['log'] = new DiogenesCoreLogger($uid);
282 $logger->log('recovery', $emails);
283 } else {
284 $page->trig('Pas de résultat correspondant aux champs entrés dans notre base de données.');
285 }
8858cfc1 286 }
287
6c49d0af 288 function handler_tmpPWD(&$page, $certif = null)
289 {
08cce2ff 290 XDB::execute('DELETE FROM perte_pass
6c49d0af 291 WHERE DATE_SUB(NOW(), INTERVAL 380 MINUTE) > created');
292
08cce2ff 293 $res = XDB::query('SELECT uid FROM perte_pass WHERE certificat={?}', $certif);
6c49d0af 294 $ligne = $res->fetchOneAssoc();
295 if (!$ligne) {
296 $page->changeTpl('index.tpl');
297 $page->kill("Cette adresse n'existe pas ou n'existe plus sur le serveur.");
298 }
299
300 $uid = $ligne["uid"];
301 if (Post::has('response2')) {
302 $password = Post::get('response2');
303 $logger = new DiogenesCoreLogger($uid);
08cce2ff 304 XDB::query('UPDATE auth_user_md5 SET password={?}
6c49d0af 305 WHERE user_id={?} AND perms IN("admin","user")',
306 $password, $uid);
08cce2ff 307 XDB::query('DELETE FROM perte_pass WHERE certificat={?}', $certif);
6c49d0af 308 $logger->log("passwd","");
309 $page->changeTpl('tmpPWD.success.tpl');
310 } else {
311 $page->changeTpl('motdepasse.tpl');
312 $page->addJsLink('javascript/motdepasse.js');
313 }
6c49d0af 314 }
315
9bae6004 316 function handler_skin(&$page)
317 {
318 global $globals;
319
f09cee18 320 $page->changeTpl('skins.tpl');
9bae6004 321 $page->assign('xorg_title','Polytechnique.org - Skins');
322
323 if (Env::has('newskin')) { // formulaire soumis, traitons les données envoyées
08cce2ff 324 XDB::execute('UPDATE auth_user_quick
63528107 325 SET skin={?} WHERE user_id={?}',
326 Env::getInt('newskin'), S::v('uid'));
9bae6004 327 set_skin();
328 }
329
330 $sql = "SELECT s.*,auteur,count(*) AS nb
331 FROM skins AS s
332 LEFT JOIN auth_user_quick AS a ON s.id=a.skin
333 WHERE skin_tpl != '' AND ext != ''
334 GROUP BY id ORDER BY s.date DESC";
08cce2ff 335 $page->assign_by_ref('skins', XDB::iterator($sql));
9bae6004 336 }
4da0b8d7 337
5de0b7e1 338 function handler_exit(&$page, $level = null)
339 {
cab08090 340 if (S::has('suid')) {
341 if (S::has('suid')) {
342 $a4l = S::v('forlife');
343 $suid = S::v('suid');
344 $log = S::v('log');
345 $log->log("suid_stop", S::v('forlife') . " by " . $suid['forlife']);
5de0b7e1 346 $_SESSION = $suid;
cab08090 347 S::kill('suid');
5de0b7e1 348 redirect($globals->baseurl.'/admin/utilisateurs.php?login='.$a4l);
349 } else {
350 redirect("events");
351 }
352 }
353
354 if ($level == 'forget' || $level == 'forgetall') {
355 setcookie('ORGaccess', '', time() - 3600, '/', '', 0);
356 Cookie::kill('ORGaccess');
357 if (isset($_SESSION['log']))
358 $_SESSION['log']->log("cookie_off");
359 }
360
361 if ($level == 'forgetuid' || $level == 'forgetall') {
362 setcookie('ORGuid', '', time() - 3600, '/', '', 0);
363 Cookie::kill('ORGuid');
364 setcookie('ORGdomain', '', time() - 3600, '/', '', 0);
365 Cookie::kill('ORGdomain');
366 }
367
368 if (isset($_SESSION['log'])) {
369 $ref = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
370 $_SESSION['log']->log('deconnexion',$ref);
371 }
372
373 XorgSession::destroy();
374
375 if (Get::has('redirect')) {
376 redirect(rawurldecode(Get::get('redirect')));
377 } else {
378 $page->changeTpl('exit.tpl');
379 }
5de0b7e1 380 }
381
4da0b8d7 382 function handler_rss(&$page, $user = null, $hash = null)
383 {
4da0b8d7 384 require_once 'rss.inc.php';
385
386 $uid = init_rss('rss.tpl', $user, $hash);
387
08cce2ff 388 $rss = XDB::iterator(
4da0b8d7 389 'SELECT e.id, e.titre, e.texte, e.creation_date
390 FROM auth_user_md5 AS u
391 INNER JOIN evenements AS e ON ( (e.promo_min = 0 || e.promo_min <= u.promo)
392 AND (e.promo_max = 0 || e.promo_max >= u.promo) )
393 WHERE u.user_id = {?} AND FIND_IN_SET(e.flags, "valide")
394 AND peremption >= NOW()', $uid);
395 $page->assign('rss', $rss);
4da0b8d7 396 }
e59506eb 397}
398
399?>