add jquery script, use it to autoload on xnet when xorg cookie is present
[platal.git] / modules / platal.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 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
22 function 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
36 class PlatalModule extends PLModule
37 {
38 function handlers()
39 {
40 return array(
41 'index' => $this->make_hook('index', AUTH_PUBLIC),
42 'cacert.pem' => $this->make_hook('cacert', AUTH_PUBLIC),
43 'changelog' => $this->make_hook('changelog', AUTH_PUBLIC),
44
45 // Preferences thingies
46 'prefs' => $this->make_hook('prefs', AUTH_COOKIE),
47 'prefs/rss' => $this->make_hook('prefs_rss', AUTH_COOKIE),
48 'prefs/webredirect'
49 => $this->make_hook('webredir', AUTH_MDP),
50 'prefs/skin' => $this->make_hook('skin', AUTH_COOKIE),
51
52 // password related thingies
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),
56 'recovery' => $this->make_hook('recovery', AUTH_PUBLIC),
57 'exit' => $this->make_hook('exit', AUTH_PUBLIC),
58 'deconnexion.php' => $this->make_hook('exit', AUTH_PUBLIC),
59 );
60 }
61
62 function handler_index(&$page)
63 {
64 if (S::logged()) {
65 pl_redirect('events');
66 }
67 }
68
69 function handler_cacert(&$page)
70 {
71 $data = file_get_contents("/etc/ssl/xorgCA/cacert.pem","r");
72 header("Pragma:");
73 header("Set-Cookie:");
74 header("Cache-Control:");
75 header("Expires:");
76 header("Content-Type: application/x-x509-ca-cert");
77 header("Content-Length: ".strlen($data));
78 echo $data;
79 exit;
80 }
81
82 function handler_changelog(&$page)
83 {
84 $page->changeTpl('platal/changeLog.tpl');
85
86 $clog = pl_entities(file_get_contents(dirname(__FILE__).'/../ChangeLog'));
87 require_once 'url_catcher.inc.php';
88 $clog = url_catcher($clog);
89 $clog = preg_replace('!(#[0-9]+(,[0-9]+)*)!e', 'bugize("\1")', $clog);
90 $clog = preg_replace('!vim:.*$!', '', $clog);
91 $page->assign('ChangeLog', $clog);
92 }
93
94 function __set_rss_state($state)
95 {
96 if ($state) {
97 $_SESSION['core_rss_hash'] = rand_url_id(16);
98 XDB::execute('UPDATE auth_user_quick
99 SET core_rss_hash={?} WHERE user_id={?}',
100 S::v('core_rss_hash'), S::v('uid'));
101 } else {
102 XDB::execute('UPDATE auth_user_quick
103 SET core_rss_hash="" WHERE user_id={?}',
104 S::v('uid'));
105 S::kill('core_rss_hash');
106 }
107 }
108
109 function handler_prefs(&$page)
110 {
111 $page->changeTpl('platal/preferences.tpl');
112 $page->assign('xorg_title','Polytechnique.org - Mes préférences');
113
114 if (Post::has('mail_fmt')) {
115 $fmt = Post::v('mail_fmt');
116 if ($fmt != 'texte') $fmt = 'html';
117 XDB::execute("UPDATE auth_user_quick
118 SET core_mail_fmt = '$fmt'
119 WHERE user_id = {?}",
120 S::v('uid'));
121 $_SESSION['mail_fmt'] = $fmt;
122 }
123
124 if (Post::has('rss')) {
125 $this->__set_rss_state(Post::b('rss'));
126 }
127 }
128
129 function handler_webredir(&$page)
130 {
131 $page->changeTpl('platal/webredirect.tpl');
132
133 $page->assign('xorg_title','Polytechnique.org - Redirection de page WEB');
134
135 $log =& S::v('log');
136 $url = Env::v('url');
137
138 if (Env::v('submit') == 'Valider' and Env::has('url')) {
139 XDB::execute('UPDATE auth_user_quick
140 SET redirecturl = {?} WHERE user_id = {?}',
141 $url, S::v('uid'));
142 $log->log('carva_add', 'http://'.Env::v('url'));
143 $page->trig("Redirection activée vers <a href='http://$url'>$url</a>");
144 } elseif (Env::v('submit') == "Supprimer") {
145 XDB::execute("UPDATE auth_user_quick
146 SET redirecturl = ''
147 WHERE user_id = {?}",
148 S::v('uid'));
149 $log->log("carva_del", $url);
150 Post::kill('url');
151 $page->trig('Redirection supprimée');
152 }
153
154 $res = XDB::query('SELECT redirecturl
155 FROM auth_user_quick
156 WHERE user_id = {?}',
157 S::v('uid'));
158 $page->assign('carva', $res->fetchOneCell());
159 }
160
161 function handler_prefs_rss(&$page)
162 {
163 $page->changeTpl('platal/filrss.tpl');
164
165 $page->assign('goback', Env::v('referer', 'login'));
166
167 if (Env::v('act_rss') == 'Activer') {
168 $this->__set_rss_state(true);
169 $page->trig("Ton Fil RSS est activé.");
170 }
171 }
172
173 function handler_password(&$page)
174 {
175 if (Post::has('response2')) {
176 require_once 'secure_hash.inc.php';
177
178 $_SESSION['password'] = $password = Post::v('response2');
179
180 XDB::execute('UPDATE auth_user_md5
181 SET password={?}
182 WHERE user_id={?}', $password,
183 S::v('uid'));
184
185 $log =& S::v('log');
186 $log->log('passwd', '');
187
188 if (Cookie::v('ORGaccess')) {
189 setcookie('ORGaccess', hash_encrypt($password), (time()+25920000), '/', '' ,0);
190 }
191
192 $page->changeTpl('platal/motdepasse.success.tpl');
193 $page->run();
194 }
195
196 $page->changeTpl('platal/motdepasse.tpl');
197 $page->addJsLink('motdepasse.js');
198 $page->assign('xorg_title','Polytechnique.org - Mon mot de passe');
199 }
200
201 function handler_smtppass(&$page)
202 {
203 $page->changeTpl('platal/acces_smtp.tpl');
204 $page->assign('xorg_title','Polytechnique.org - Acces SMTP/NNTP');
205
206 require_once 'wiki.inc.php';
207 wiki_require_page('Xorg.SMTPSécurisé');
208 wiki_require_page('Xorg.NNTPSécurisé');
209
210 $uid = S::v('uid');
211 $pass = Env::v('smtppass1');
212 $log = S::v('log');
213
214 if (Env::v('op') == "Valider" && strlen($pass) >= 6
215 && Env::v('smtppass1') == Env::v('smtppass2'))
216 {
217 XDB::execute('UPDATE auth_user_md5 SET smtppass = {?}
218 WHERE user_id = {?}', $pass, $uid);
219 $page->trig('Mot de passe enregistré');
220 $log->log("passwd_ssl");
221 } elseif (Env::v('op') == "Supprimer") {
222 XDB::execute('UPDATE auth_user_md5 SET smtppass = ""
223 WHERE user_id = {?}', $uid);
224 $page->trig('Compte SMTP et NNTP supprimé');
225 $log->log("passwd_del");
226 }
227
228 $res = XDB::query("SELECT IF(smtppass != '', 'actif', '')
229 FROM auth_user_md5
230 WHERE user_id = {?}", $uid);
231 $page->assign('actif', $res->fetchOneCell());
232 }
233
234 function handler_recovery(&$page)
235 {
236 global $globals;
237
238 $page->changeTpl('platal/recovery.tpl');
239
240 if (!Env::has('login') || !Env::has('birth')) {
241 return;
242 }
243
244 if (!ereg('[0-3][0-9][0-1][0-9][1][9]([0-9]{2})', Env::v('birth'))) {
245 $page->trig('Date de naissance incorrecte ou incohérente');
246 return;
247 }
248
249 $birth = sprintf('%s-%s-%s',
250 substr(Env::v('birth'), 4, 4),
251 substr(Env::v('birth'), 2, 2),
252 substr(Env::v('birth'), 0, 2));
253
254 $mailorg = strtok(Env::v('login'), '@');
255
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
258
259 $res = XDB::query(
260 "SELECT user_id, naissance
261 FROM auth_user_md5 AS u
262 INNER JOIN aliases AS a ON (u.user_id=a.id AND type != 'homonyme')
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();
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 }
284 $mymail = new PlMailer();
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
291 Si en cliquant dessus tu n'y arrives pas, copie intégralement l'adresse dans la barre de ton navigateur.
292
293 --
294 Polytechnique.org
295 \"Le portail des élèves & anciens élèves de l'Ecole polytechnique\"
296
297 Mail envoyé à ".Env::v('login') . (Post::has('email') ? "
298 Adresse de secours : " . Post::v('email') : ""));
299 $mymail->send();
300
301 // on cree un objet logger et on log l'evenement
302 $logger = $_SESSION['log'] = new CoreLogger($uid);
303 $logger->log('recovery', $mails);
304 } else {
305 $page->trig('Les informations que tu as rentrées ne permettent pas de récupérer ton mot de passe.<br />'.
306 'Si tu as un homonyme, utilise prenom.nom.promo comme login');
307 }
308 }
309
310 function handler_tmpPWD(&$page, $certif = null)
311 {
312 XDB::execute('DELETE FROM perte_pass
313 WHERE DATE_SUB(NOW(), INTERVAL 380 MINUTE) > created');
314
315 $res = XDB::query('SELECT uid FROM perte_pass WHERE certificat={?}', $certif);
316 $ligne = $res->fetchOneAssoc();
317 if (!$ligne) {
318 $page->changeTpl('platal/index.tpl');
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')) {
324 $password = Post::v('response2');
325 $logger = new CoreLogger($uid);
326 XDB::query('UPDATE auth_user_md5 SET password={?}
327 WHERE user_id={?} AND perms IN("admin","user")',
328 $password, $uid);
329 XDB::query('DELETE FROM perte_pass WHERE certificat={?}', $certif);
330 $logger->log("passwd","");
331 $page->changeTpl('platal/tmpPWD.success.tpl');
332 } else {
333 $page->changeTpl('platal/motdepasse.tpl');
334 $page->addJsLink('motdepasse.js');
335 }
336 }
337
338 function handler_skin(&$page)
339 {
340 global $globals;
341
342 $page->changeTpl('platal/skins.tpl');
343 $page->assign('xorg_title','Polytechnique.org - Skins');
344
345 if (Env::has('newskin')) { // formulaire soumis, traitons les données envoyées
346 XDB::execute('UPDATE auth_user_quick
347 SET skin={?} WHERE user_id={?}',
348 Env::i('newskin'), S::v('uid'));
349 S::kill('skin');
350 set_skin();
351 }
352
353 $res = XDB::query('SELECT id FROM skins WHERE skin_tpl={?}', S::v('skin'));
354 $page->assign('skin_id', $res->fetchOneCell());
355
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";
361 $page->assign('skins', XDB::iterator($sql));
362 }
363
364 function handler_exit(&$page, $level = null)
365 {
366 if (S::has('suid')) {
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);
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')) {
398 http_redirect(rawurldecode(Get::v('redirect')));
399 } else {
400 $page->changeTpl('platal/exit.tpl');
401 }
402 }
403 }
404
405 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
406 ?>