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