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