Fix date in password update pages
[platal.git] / modules / platal.php
... / ...
CommitLineData
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
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
36class 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 // happenings related thingies
61 'rss' => $this->make_hook('rss', AUTH_PUBLIC),
62 );
63 }
64
65 function handler_index(&$page)
66 {
67 if (S::logged()) {
68 pl_redirect('events');
69 }
70 }
71
72 function handler_cacert(&$page)
73 {
74 $data = file_get_contents("/etc/ssl/xorgCA/cacert.pem","r");
75 header("Pragma:");
76 header("Set-Cookie:");
77 header("Cache-Control:");
78 header("Expires:");
79 header("Content-Type: application/x-x509-ca-cert");
80 header("Content-Length: ".strlen($data));
81 echo $data;
82 exit;
83 }
84
85 function handler_changelog(&$page)
86 {
87 $page->changeTpl('changeLog.tpl');
88 $page->addJsLink('wiki.js');
89
90 $clog = htmlentities(file_get_contents(dirname(__FILE__).'/../ChangeLog'));
91 $clog = preg_replace('!(#[0-9]+(,[0-9]+)*)!e', 'bugize("\1")', $clog);
92 $clog = preg_replace('!([-a-z0-9_.]+@[-a-z0-9_.]+)!ie',
93 '"<script type=\'text/javascript\'>Nix.decode(\"" . str_rot13("\1") . "\"); </script>"', $clog);
94 $clog = preg_replace('!vim:.*$!', '', $clog);
95 $page->assign('ChangeLog', $clog);
96 }
97
98 function __set_rss_state($state)
99 {
100 if ($state) {
101 $_SESSION['core_rss_hash'] = rand_url_id(16);
102 XDB::execute('UPDATE auth_user_quick
103 SET core_rss_hash={?} WHERE user_id={?}',
104 S::v('core_rss_hash'), S::v('uid'));
105 } else {
106 XDB::execute('UPDATE auth_user_quick
107 SET core_rss_hash="" WHERE user_id={?}',
108 S::v('uid'));
109 S::kill('core_rss_hash');
110 }
111 }
112
113 function handler_prefs(&$page)
114 {
115 $page->changeTpl('preferences.tpl');
116 $page->assign('xorg_title','Polytechnique.org - Mes préférences');
117
118 if (Post::has('mail_fmt')) {
119 $fmt = Post::v('mail_fmt');
120 if ($fmt != 'texte') $fmt = 'html';
121 XDB::execute("UPDATE auth_user_quick
122 SET core_mail_fmt = '$fmt'
123 WHERE user_id = {?}",
124 S::v('uid'));
125 $_SESSION['mail_fmt'] = $fmt;
126 }
127
128 if (Post::has('rss')) {
129 $this->__set_rss_state(Post::b('rss'));
130 }
131 }
132
133 function handler_webredir(&$page)
134 {
135 $page->changeTpl('webredirect.tpl');
136
137 $page->assign('xorg_title','Polytechnique.org - Redirection de page WEB');
138
139 $log =& S::v('log');
140 $url = Env::v('url');
141
142 if (Env::v('submit') == 'Valider' and Env::has('url')) {
143 XDB::execute('UPDATE auth_user_quick
144 SET redirecturl = {?} WHERE user_id = {?}',
145 $url, S::v('uid'));
146 $log->log('carva_add', 'http://'.Env::v('url'));
147 $page->trig("Redirection activée vers <a href='http://$url'>$url</a>");
148 } elseif (Env::v('submit') == "Supprimer") {
149 XDB::execute("UPDATE auth_user_quick
150 SET redirecturl = ''
151 WHERE user_id = {?}",
152 S::v('uid'));
153 $log->log("carva_del", $url);
154 Post::kill('url');
155 $page->trig('Redirection supprimée');
156 }
157
158 $res = XDB::query('SELECT redirecturl
159 FROM auth_user_quick
160 WHERE user_id = {?}',
161 S::v('uid'));
162 $page->assign('carva', $res->fetchOneCell());
163 }
164
165 function handler_prefs_rss(&$page)
166 {
167 $page->changeTpl('filrss.tpl');
168
169 $page->assign('goback', Env::v('referer', 'login'));
170
171 if (Env::v('act_rss') == 'Activer') {
172 $this->__set_rss_state(true);
173 $page->trig("Ton Fil RSS est activé.");
174 }
175 }
176
177 function handler_password(&$page)
178 {
179 if (Post::has('response2')) {
180 require_once 'secure_hash.inc.php';
181
182 $_SESSION['password'] = $password = Post::v('response2');
183
184 XDB::execute('UPDATE auth_user_md5
185 SET password={?}
186 WHERE user_id={?}', $password,
187 S::v('uid'));
188
189 $log =& S::v('log');
190 $log->log('passwd', '');
191
192 if (Cookie::v('ORGaccess')) {
193 setcookie('ORGaccess', hash_encrypt($password), (time()+25920000), '/', '' ,0);
194 }
195
196 $page->changeTpl('motdepasse.success.tpl');
197 $page->assign('now', strftime("%Y%m%d%H%M%S"));
198 $page->run();
199 }
200
201 $page->changeTpl('motdepasse.tpl');
202 $page->addJsLink('motdepasse.js');
203 $page->assign('xorg_title','Polytechnique.org - Mon mot de passe');
204 }
205
206 function handler_smtppass(&$page)
207 {
208 $page->changeTpl('acces_smtp.tpl');
209 $page->assign('xorg_title','Polytechnique.org - Acces SMTP/NNTP');
210
211 $uid = S::v('uid');
212 $pass = Env::v('smtppass1');
213 $log = S::v('log');
214
215 if (Env::v('op') == "Valider" && strlen($pass) >= 6
216 && Env::v('smtppass1') == Env::v('smtppass2'))
217 {
218 XDB::execute('UPDATE auth_user_md5 SET smtppass = {?}
219 WHERE user_id = {?}', $pass, $uid);
220 $page->trig('Mot de passe enregistré');
221 $log->log("passwd_ssl");
222 } elseif (Env::v('op') == "Supprimer") {
223 XDB::execute('UPDATE auth_user_md5 SET smtppass = ""
224 WHERE user_id = {?}', $uid);
225 $page->trig('Compte SMTP et NNTP supprimé');
226 $log->log("passwd_del");
227 }
228
229 $res = XDB::query("SELECT IF(smtppass != '', 'actif', '')
230 FROM auth_user_md5
231 WHERE user_id = {?}", $uid);
232 $page->assign('actif', $res->fetchOneCell());
233 }
234
235 function handler_recovery(&$page)
236 {
237 global $globals;
238
239 $page->changeTpl('recovery.tpl');
240
241 if (!Env::has('login') || !Env::has('birth')) {
242 return;
243 }
244
245 if (!ereg('[0-3][0-9][0-1][0-9][1][9]([0-9]{2})', Env::v('birth'))) {
246 $page->trig('Date de naissance incorrecte ou incohérente');
247 return;
248 }
249
250 $birth = sprintf('%s-%s-%s',
251 substr(Env::v('birth'), 4, 4),
252 substr(Env::v('birth'), 2, 2),
253 substr(Env::v('birth'), 0, 2));
254
255 $mailorg = strtok(Env::v('login'), '@');
256
257 // paragraphe rajouté : si la date de naissance dans la base n'existe pas, on l'update
258 // avec celle fournie ici en espérant que c'est la bonne
259
260 $res = XDB::query(
261 "SELECT user_id, naissance
262 FROM auth_user_md5 AS u
263 INNER JOIN aliases AS a ON (u.user_id=a.id AND type != 'homonyme')
264 WHERE a.alias={?} AND u.perms IN ('admin','user') AND u.deces=0", $mailorg);
265 list($uid, $naissance) = $res->fetchOneRow();
266
267 if ($naissance == $birth) {
268 $page->assign('ok', true);
269
270 $url = rand_url_id();
271 XDB::execute('INSERT INTO perte_pass (certificat,uid,created) VALUES ({?},{?},NOW())', $url, $uid);
272 $res = XDB::query('SELECT email FROM emails WHERE uid = {?} AND NOT FIND_IN_SET("filter", flags)', $uid);
273 $mails = implode(', ', $res->fetchColumn());
274
275 require_once "diogenes/diogenes.hermes.inc.php";
276 $mymail = new HermesMailer();
277 $mymail->setFrom('"Gestion des mots de passe" <support+password@polytechnique.org>');
278 $mymail->addTo($mails);
279 $mymail->setSubject('Ton certificat d\'authentification');
280 $mymail->setTxtBody("Visite la page suivante qui expire dans six heures :
281{$globals->baseurl}/tmpPWD/$url
282
283Si en cliquant dessus tu n'y arrives pas, copie intégralement l'adresse dans la barre de ton navigateur.
284
285--
286Polytechnique.org
287\"Le portail des élèves & anciens élèves de l'Ecole polytechnique\"".(Post::v('email') ? "
288
289Adresse de secours :
290 ".Post::v('email') : "")."
291
292Mail envoyé à ".Env::v('login'));
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', $emails);
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('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('tmpPWD.success.tpl');
326 $page->assign('now', strftime("%Y%m%d%H%M%S"));
327 } else {
328 $page->changeTpl('motdepasse.tpl');
329 $page->addJsLink('motdepasse.js');
330 }
331 }
332
333 function handler_skin(&$page)
334 {
335 global $globals;
336
337 $page->changeTpl('skins.tpl');
338 $page->assign('xorg_title','Polytechnique.org - Skins');
339
340 if (Env::has('newskin')) { // formulaire soumis, traitons les données envoyées
341 XDB::execute('UPDATE auth_user_quick
342 SET skin={?} WHERE user_id={?}',
343 Env::i('newskin'), S::v('uid'));
344 S::kill('skin');
345 set_skin();
346 }
347
348 $res = XDB::query('SELECT id FROM skins WHERE skin_tpl={?}', S::v('skin'));
349 $page->assign('skin_id', $res->fetchOneCell());
350
351 $sql = "SELECT s.*,auteur,count(*) AS nb
352 FROM skins AS s
353 LEFT JOIN auth_user_quick AS a ON s.id=a.skin
354 WHERE skin_tpl != '' AND ext != ''
355 GROUP BY id ORDER BY s.date DESC";
356 $page->assign_by_ref('skins', XDB::iterator($sql));
357 }
358
359 function handler_exit(&$page, $level = null)
360 {
361 if (S::has('suid')) {
362 if (S::has('suid')) {
363 $a4l = S::v('forlife');
364 $suid = S::v('suid');
365 $log = S::v('log');
366 $log->log("suid_stop", S::v('forlife') . " by " . $suid['forlife']);
367 $_SESSION = $suid;
368 S::kill('suid');
369 pl_redirect('admin/utilisateurs.php', 'login='.$a4l);
370 } else {
371 pl_redirect('events');
372 }
373 }
374
375 if ($level == 'forget' || $level == 'forgetall') {
376 setcookie('ORGaccess', '', time() - 3600, '/', '', 0);
377 Cookie::kill('ORGaccess');
378 if (isset($_SESSION['log']))
379 $_SESSION['log']->log("cookie_off");
380 }
381
382 if ($level == 'forgetuid' || $level == 'forgetall') {
383 setcookie('ORGuid', '', time() - 3600, '/', '', 0);
384 Cookie::kill('ORGuid');
385 setcookie('ORGdomain', '', time() - 3600, '/', '', 0);
386 Cookie::kill('ORGdomain');
387 }
388
389 if (isset($_SESSION['log'])) {
390 $ref = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
391 $_SESSION['log']->log('deconnexion',$ref);
392 }
393
394 XorgSession::destroy();
395
396 if (Get::has('redirect')) {
397 http_redirect(rawurldecode(Get::v('redirect')));
398 } else {
399 $page->changeTpl('exit.tpl');
400 }
401 }
402
403 function handler_rss(&$page, $user = null, $hash = null)
404 {
405 require_once 'rss.inc.php';
406
407 $uid = init_rss('rss.tpl', $user, $hash);
408
409 $rss = XDB::iterator(
410 'SELECT e.id, e.titre, e.texte, e.creation_date,
411 IF(u2.nom_usage = "", u2.nom, u2.nom_usage) AS nom, u2.prenom, u2.promo
412 FROM auth_user_md5 AS u
413 INNER JOIN evenements AS e ON ( (e.promo_min = 0 || e.promo_min <= u.promo)
414 AND (e.promo_max = 0 || e.promo_max >= u.promo) )
415 INNER JOIN auth_user_md5 AS u2 ON (u2.user_id = e.user_id)
416 WHERE u.user_id = {?} AND FIND_IN_SET(e.flags, "valide")
417 AND peremption >= NOW()', $uid);
418 $page->assign('rss', $rss);
419 }
420}
421
422?>