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