2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 function bugize($list)
24 $list = split(',', $list);
27 foreach ($list as $bug) {
28 $clean = str_replace('#', '', $bug);
29 $ans[] = "<a href='http://trackers.polytechnique.org/task/$clean'>$bug</a>";
32 return join(',', $ans);
36 class PlatalModule
extends PLModule
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
),
45 // Preferences thingies
46 'prefs' => $this->make_hook('prefs', AUTH_COOKIE
),
47 'prefs/rss' => $this->make_hook('prefs_rss', AUTH_COOKIE
),
48 'prefs/webredirect' => $this->make_hook('webredir', AUTH_MDP
),
49 'prefs/skin' => $this->make_hook('skin', AUTH_COOKIE
),
51 // password related thingies
52 'password' => $this->make_hook('password', AUTH_MDP
),
53 'tmpPWD' => $this->make_hook('tmpPWD', AUTH_PUBLIC
),
54 'password/smtp' => $this->make_hook('smtppass', AUTH_MDP
),
55 'recovery' => $this->make_hook('recovery', AUTH_PUBLIC
),
56 'exit' => $this->make_hook('exit', AUTH_PUBLIC
),
57 'review' => $this->make_hook('review', AUTH_PUBLIC
),
58 'deconnexion.php' => $this->make_hook('exit', AUTH_PUBLIC
),
62 function handler_index(&$page)
64 // Include X-XRDS-Location response-header for Yadis discovery
66 header('X-XRDS-Location: ' . $globals->baseurl
. '/openid/xrds');
68 // Redirect to the suitable page
70 pl_redirect('events');
71 } else if (!@$GLOBALS['IS_XNET_SITE']) {
72 $this->handler_review($page);
76 function handler_cacert(&$page)
78 $data = file_get_contents("/etc/ssl/xorgCA/cacert.pem","r");
80 header("Set-Cookie:");
81 header("Cache-Control:");
83 header("Content-Type: application/x-x509-ca-cert");
84 header("Content-Length: ".strlen($data));
89 function handler_changelog(&$page, $core = null
)
91 $page->changeTpl('platal/changeLog.tpl');
93 function formatChangeLog($file) {
94 $clog = pl_entities(file_get_contents($file));
95 $clog = preg_replace('/===+\s*/', '</pre><hr /><pre>', $clog);
96 // url catch only (not all wiki syntax)
97 $clog = preg_replace(array(
98 '/((?:https?|ftp):\/\/(?:\.*,*[\w@~%$£µ&i#\-+=_\/\?;])*)/ui',
99 '/(\s|^)www\.((?:\.*,*[\w@~%$£µ&i#\-+=_\/\?;])*)/iu',
100 '/(?:mailto:)?([a-z0-9.\-+_]+@([\-.+_]?[a-z0-9])+)/i'),
102 '<a href="\\0">\\0</a>',
103 '\\1<a href="http://www.\\2">www.\\2</a>',
104 '<a href="mailto:\\0">\\0</a>'),
106 $clog = preg_replace('!(#[0-9]+(,[0-9]+)*)!e', 'bugize("\1")', $clog);
107 $clog = preg_replace('!vim:.*$!', '', $clog);
108 return preg_replace("!(<hr />(\\s|\n)*)?<pre>(\s|\n)*</pre>((\\s|\n)*<hr />)?!m", "", "<pre>$clog</pre>");
110 if ($core != 'core') {
111 $page->assign('core', false
);
112 $page->assign('ChangeLog', formatChangeLog(dirname(__FILE__
).'/../ChangeLog'));
114 $page->assign('core', true
);
115 $page->assign('ChangeLog', formatChangeLog(dirname(__FILE__
).'/../core/ChangeLog'));
119 function __set_rss_state($state)
122 S
::set('token', rand_url_id(16));
123 XDB
::execute('UPDATE accounts
125 WHERE uid = {?}', S
::s('token'), S
::i('uid'));
128 XDB
::execute('UPDATE accounts
130 WHERE uid = {?}', S
::i('uid'));
134 function handler_prefs(&$page)
136 $page->changeTpl('platal/preferences.tpl');
137 $page->setTitle('Mes préférences');
139 if (Post
::has('email_format')) {
140 $fmt = Post
::s('email_format');
141 XDB
::execute("UPDATE accounts
142 SET email_format = {?}
145 S
::set('email_format', $fmt);
148 if (Post
::has('rss')) {
149 $this->__set_rss_state(Post
::b('rss'));
152 # FIXME: this code is not multi-domain compatible. We should decide how
153 # carva will extend to users not in the main domain.
154 $res = XDB
::query("SELECT alias
156 WHERE id = {?} AND FIND_IN_SET('bestalias', flags)",
158 $page->assign('bestalias', $res->fetchOneCell());
161 function handler_webredir(&$page)
163 $page->changeTpl('platal/webredirect.tpl');
164 $page->setTitle('Redirection de page WEB');
166 if (Env
::v('submit') == 'Valider' && !Env
::blank('url')) {
167 if (Env
::blank('url')) {
168 $page->trigError('URL invalide');
170 $url = Env
::t('url');
171 XDB
::execute('REPLACE INTO carvas (uid, url)
174 S
::logger()->log('carva_add', 'http://' . $url);
175 $page->trigSuccess("Redirection activée vers <a href='http://$url'>$url</a>");
177 } elseif (Env
::v('submit') == 'Supprimer') {
178 XDB
::execute('DELETE FROM carvas
179 WHERE uid = {?}', S
::i('uid'));
181 S
::logger()->log('carva_del');
182 $page->trigSuccess('Redirection supprimée');
185 $url = XDB
::fetchOneCell('SELECT url
187 WHERE uid = {?}', S
::i('uid'));
188 $page->assign('carva', $url);
190 # FIXME: this code is not multi-domain compatible. We should decide how
191 # carva will extend to users not in the main domain.
192 $res = XDB
::query("SELECT alias
194 WHERE id = {?} AND FIND_IN_SET('bestalias', flags)",
196 $page->assign('bestalias', $res->fetchOneCell());
199 function handler_prefs_rss(&$page)
201 $page->changeTpl('platal/filrss.tpl');
203 $page->assign('goback', Env
::v('referer', 'login'));
205 if (Env
::v('act_rss') == 'Activer') {
206 $this->__set_rss_state(true
);
207 $page->trigSuccess("Ton Fil RSS est activé.");
211 function handler_password(&$page)
215 if (Post
::has('response2')) {
216 S
::assert_xsrf_token();
218 S
::set('password', $password = Post
::v('response2'));
219 XDB
::execute('UPDATE accounts
221 WHERE uid={?}', $password,
224 // If GoogleApps is enabled, and the user did choose to use synchronized passwords,
225 // updates the Google Apps password as well.
226 if ($globals->mailstorage
->googleapps_domain
) {
227 require_once 'googleapps.inc.php';
228 $account = new GoogleAppsAccount(S
::user());
229 if ($account->active() && $account->sync_password
) {
230 $account->set_password($password);
234 S
::logger()->log('passwd');
235 Platal
::session()->setAccessCookie(true
);
237 $page->changeTpl('platal/motdepasse.success.tpl');
241 $page->changeTpl('platal/motdepasse.tpl');
242 $page->addJsLink('motdepasse.js');
243 $page->setTitle('Mon mot de passe');
246 function handler_smtppass(&$page)
248 $page->changeTpl('platal/acces_smtp.tpl');
249 $page->setTitle('Acces SMTP/NNTP');
251 $wp = new PlWikiPage('Xorg.SMTPSécurisé');
253 $wp = new PlWikiPage('Xorg.NNTPSécurisé');
257 $pass = Env
::v('smtppass1');
259 if (Env
::v('op') == "Valider" && strlen($pass) >= 6
260 && Env
::v('smtppass1') == Env
::v('smtppass2')) {
261 XDB
::execute('UPDATE accounts
262 SET weak_password = {?}
263 WHERE uid = {?}', $pass, $uid);
264 $page->trigSuccess('Mot de passe enregistré');
265 S
::logger()->log("passwd_ssl");
266 } elseif (Env
::v('op') == "Supprimer") {
267 XDB
::execute('UPDATE accounts
268 SET weak_password = NULL
269 WHERE uid = {?}', $uid);
270 $page->trigSuccess('Compte SMTP et NNTP supprimé');
271 S
::logger()->log("passwd_del");
274 $res = XDB
::query("SELECT weak_password IS NOT NULL
276 WHERE uid = {?}", $uid);
277 $page->assign('actif', $res->fetchOneCell());
280 function handler_recovery(&$page)
284 $page->changeTpl('platal/recovery.tpl');
286 if (!Env
::has('login') ||
!Env
::has('birth')) {
290 if (!ereg('[0-3][0-9][0-1][0-9][1][9]([0-9]{2})', Env
::v('birth'))) {
291 $page->trigError('Date de naissance incorrecte ou incohérente');
295 $birth = sprintf('%s-%s-%s',
296 substr(Env
::v('birth'), 4, 4),
297 substr(Env
::v('birth'), 2, 2),
298 substr(Env
::v('birth'), 0, 2));
300 $mailorg = strtok(Env
::v('login'), '@');
302 $profile = Profile
::get(Env
::t('login'));
303 if (is_null($profile) ||
$profile->birthdate
!= $birth) {
304 $page->trigError('Les informations que tu as rentrées ne permettent pas de récupérer ton mot de passe.<br />'.
305 'Si tu as un homonyme, utilise prenom.nom.promo comme login');
309 $user = $profile->owner();
310 if ($user->state
!= 'active') {
311 $page->trigError('Ton compte n\'est pas activé.');
315 $res = XDB
::query("SELECT COUNT(*)
317 WHERE uid = {?} AND flags != 'panne' AND flags != 'filter'", $user->id());
318 $count = intval($res->fetchOneCell());
320 $page->assign('no_addr', true
);
324 $page->assign('ok', true
);
326 $url = rand_url_id();
327 XDB
::execute('INSERT INTO perte_pass (certificat,uid,created)
328 VALUES ({?},{?},NOW())', $url, $user->id());
329 $res = XDB
::query('SELECT email
331 WHERE uid = {?} AND email = {?}',
332 $user->id(), Post
::v('email'));
333 if ($res->numRows()) {
334 $mails = $res->fetchOneCell();
336 $res = XDB
::query("SELECT email
338 WHERE uid = {?} AND NOT FIND_IN_SET('filter', flags)", $user->id());
339 $mails = implode(', ', $res->fetchColumn());
341 $mymail = new PlMailer();
342 $mymail->setFrom('"Gestion des mots de passe" <support+password@' . $globals->mail
->domain
. '>');
343 $mymail->addTo($mails);
344 $mymail->setSubject("Ton certificat d'authentification");
345 $mymail->setTxtBody("Visite la page suivante qui expire dans six heures :
346 {$globals->baseurl}/tmpPWD/$url
348 Si en cliquant dessus tu n'y arrives pas, copie intégralement l'adresse dans la barre de ton navigateur. Si tu n'as pas utilisé ce lien dans six heures, tu peux tout simplement recommencer cette procédure.
352 \"Le portail des élèves & anciens élèves de l'École polytechnique\"
354 Email envoyé à ".Env
::v('login') . (Post
::has('email') ?
"
355 Adresse de secours : " . Post
::v('email') : ""));
358 // on cree un objet logger et on log l'evenement
359 S
::logger($user->id())->log('recovery', $mails);
362 function handler_tmpPWD(&$page, $certif = null
)
365 // XXX: recovery requires data from the profile
366 XDB
::execute('DELETE FROM perte_pass
367 WHERE DATE_SUB(NOW(), INTERVAL 380 MINUTE) > created');
369 $res = XDB
::query('SELECT uid
370 FROM perte_pass WHERE certificat={?}', $certif);
371 $ligne = $res->fetchOneAssoc();
373 $page->changeTpl('platal/index.tpl');
374 $page->kill("Cette adresse n'existe pas ou n'existe plus sur le serveur.");
377 $uid = $ligne["uid"];
378 if (Post
::has('response2')) {
379 $password = Post
::v('response2');
380 XDB
::query('UPDATE accounts
382 WHERE uid = {?} AND state = \'active\'',
384 XDB
::query('DELETE FROM perte_pass
385 WHERE certificat={?}', $certif);
387 // If GoogleApps is enabled, and the user did choose to use synchronized passwords,
388 // updates the Google Apps password as well.
389 if ($globals->mailstorage
->googleapps_domain
) {
390 require_once 'googleapps.inc.php';
391 $account = new GoogleAppsAccount(User
::getSilent($uid));
392 if ($account->active() && $account->sync_password
) {
393 $account->set_password($password);
397 S
::logger($uid)->log("passwd", "");
398 $page->changeTpl('platal/tmpPWD.success.tpl');
400 $page->changeTpl('platal/motdepasse.tpl');
401 $page->addJsLink('motdepasse.js');
405 function handler_skin(&$page)
409 $page->changeTpl('platal/skins.tpl');
410 $page->setTitle('Skins');
412 if (Env
::has('newskin')) { // formulaire soumis, traitons les données envoyées
413 XDB
::execute('UPDATE accounts
416 Env
::i('newskin'), S
::i('uid'));
418 Platal
::session()->setSkin();
421 $res = XDB
::query('SELECT id
423 WHERE skin_tpl = {?}', S
::v('skin'));
424 $page->assign('skin_id', $res->fetchOneCell());
426 $sql = 'SELECT s.*, auteur, COUNT(*) AS nb
428 LEFT JOIN accounts AS a ON (a.skin = s.id)
429 WHERE skin_tpl != \'\' AND ext != \'\'
430 GROUP BY id ORDER BY s.date DESC';
431 $page->assign('skins', XDB
::iterator($sql));
434 function handler_exit(&$page, $level = null
)
437 S
::logger()->log('suid_stop', S
::user()->login() . " by " . S
::suid('hruid'));
438 Platal
::session()->stopSUID();
439 pl_redirect('admin/user/' . S
::user()->login());
442 if ($level == 'forget' ||
$level == 'forgetall') {
443 Platal
::session()->killAccessCookie();
446 if ($level == 'forgetuid' ||
$level == 'forgetall') {
447 Platal
::session()->killLoginFormCookies();
451 S
::logger()->log('deconnexion', @$_SERVER['HTTP_REFERER']);
452 Platal
::session()->destroy();
455 if (Get
::has('redirect')) {
456 http_redirect(rawurldecode(Get
::v('redirect')));
458 $page->changeTpl('platal/exit.tpl');
462 function handler_review(&$page, $action = null
, $mode = null
)
464 // Include X-XRDS-Location response-header for Yadis discovery
466 header('X-XRDS-Location: ' . $globals->baseurl
. '/openid/xrds');
468 $this->load('review.inc.php');
470 if (@$GLOBALS['IS_XNET_SITE']) {
473 $wp = new PlWikiPage($dom . '.Admin');
474 $conf = explode('%0a', $wp->getField('text'));
475 $wiz = new PlWizard('Tour d\'horizon', PlPage
::getCoreTpl('plwizard.tpl'), true
);
476 foreach ($conf as $line) {
477 $list = preg_split('/\s*[*|]\s*/', $line, -1, PREG_SPLIT_NO_EMPTY
);
478 $wiz->addPage('ReviewPage', $list[0], $list[1]);
480 $wiz->apply($page, 'review', $action, $mode);
484 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: