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