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