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