e5187e4e8dbf62f58af101fe17813230adb7a194
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 define('PL_DO_AUTH', 300);
23 define('PL_FORBIDDEN', 403);
24 define('PL_NOT_FOUND', 404);
25 define('PL_WIKI', 500);
26 define('PL_JSON', 501);
34 protected function __construct($auth = AUTH_PUBLIC
, $perms = 'user', $type = DO_AUTH
)
37 $this->perms
= $perms;
41 public function needAuth()
43 return $this->auth
> S
::i('auth', AUTH_PUBLIC
);
46 public function checkPerms()
48 if (!$this->perms ||
$this->auth
== AUTH_PUBLIC
) { // No perms, no check
51 $s_perms = S
::v('perms');
52 return $s_perms->hasFlagCombination($this->perms
);
55 public function hasType($type)
57 return ($this->type
& $type) == $type;
60 abstract protected function run(PlPage
&$page, array $args);
62 public function call(PlPage
&$page, array $args)
64 global $globals, $session, $platal;
65 if ($this->needAuth()) {
66 if ($this->hasType(DO_AUTH
)) {
67 if (!$session->start($this->auth
)) {
68 $platal->force_login($page);
75 if (!$this->checkPerms()) {
76 if (Platal
::notAllowed()) {
80 return $this->run($page, $args);
84 class PlStdHook
extends PlHook
88 public function __construct($callback, $auth = AUTH_PUBLIC
, $perms = 'user', $type = DO_AUTH
)
90 parent
::__construct($auth, $perms, $type);
91 $this->hook
= $callback;
94 protected function run(PlPage
&$page, array $args)
96 global $session, $platal;
99 $val = call_user_func_array($this->hook
, $args);
100 if ($val == PL_DO_AUTH
) {
101 if (!$session->start($session->loggedLevel())) {
102 $platal->force_login($page);
104 $val = call_user_func_array($this->hook
, $args);
110 class PlWikiHook
extends PlHook
112 public function __construct($auth = AUTH_PUBLIC
, $perms = 'user', $type = DO_AUTH
)
114 parent
::__construct($auth, $perms, $type);
117 protected function run(PlPage
&$page, array $args)
126 public $aliased = null
;
127 public $children = array();
129 public function addChild(array $path, PlHook
$hook)
132 $next = array_shift($path);
134 if ($next && $next{0} == '%') {
136 $next = $platal->hook_map(substr($next, 1));
141 @$child =& $this->children
[$next];
143 $child = new PlHookTree();
144 $this->children
[$next] =& $child;
145 $child->aliased
= $alias;
148 $child->hook
= $hook;
150 $child->addChild($path, $hook);
154 private function findChildAux(array $remain, array $matched, array $aliased)
157 if ($this->aliased
) {
161 $child = @$this->children
[$next];
163 array_shift($remain);
165 return $child->findChildAux($remain, $matched, $aliased);
168 return array($this->hook
, $matched, $remain, $aliased);
171 public function findChild(array $path)
173 return $this->findChildAux($path, array(), array());
176 private function findNearestChildAux(array $remain, array $matched, array $aliased)
179 if ($this->aliased
) {
183 $child = @$this->children
[$next];
188 foreach ($this->children
as $path=>$hook) {
189 $lev = levenshtein($next, $path);
190 if ($lev <= $nearest_lev
191 && ($lev < strlen($next) / 2 ||
strpos($next, $path) !== false
192 ||
strpos($path, $next) !== false
)) {
193 $sdx = levenshtein(soundex($next), soundex($path));
194 if ($lev == $nearest_lev ||
$sdx < $nearest_sdx) {
205 array_shift($remain);
207 return $child->findNearestChildAux($remain, $matched, $aliased);
209 if (($pos = strpos($next, '.php')) !== false
) {
210 $remain[0] = substr($next, 0, $pos);
211 return $this->findNearestChildAux($remain, $matched, $aliased);
214 return array($this->hook
, $matched, $remain, $aliased);
217 public function findNearestChild(array $path)
219 return $this->findNearestChildAux($path, array(), array());
223 abstract class Platal
232 public $argv = array();
234 static private $_page = null
;
236 public function __construct()
238 global $platal, $session, $globals;
241 /* Assign globals first, then call init: init must be used for operations
242 * that requires access to the content of $globals (e.g. XDB requires
243 * $globals to be assigned.
245 $globals = $this->buildGlobals();
248 /* Get the current session: assign first, then activate the session.
250 $session = $this->buildSession();
251 if (!$session->startAvailableAuth()) {
252 Platal
::page()->trigError("Données d'authentification invalides.");
255 $modules = func_get_args();
256 if (isset($modules[0]) && is_array($modules[0])) {
257 $modules = $modules[0];
259 $this->path
= trim(Get
::_get('n', null
), '/');
261 $this->mods
= array();
262 $this->hooks
= new PlHookTree();
264 array_unshift($modules, 'core');
265 foreach ($modules as $module) {
266 $module = strtolower($module);
267 $this->mods
[$module] = $m = PLModule
::factory($module);
268 $hooks = $m->handlers();
269 foreach ($hooks as $path=>$hook) {
270 $this->hooks
->addChild(explode('/', $path), $hook);
274 if ($globals->mode
== '') {
275 pl_redirect('index.html');
279 public function pl_self($n = null
)
285 return join('/', array_slice($this->argv
, 0, $n +
1));
287 if ($n <= -count($this->argv
))
288 return $this->argv
[0];
290 return join('/', array_slice($this->argv
, 0, $n));
293 public static function wiki_hook($auth = AUTH_PUBLIC
, $perms = 'user', $type = DO_AUTH
)
295 return new PlWikiHook($auth, $perms, $type);
298 public function hook_map($name)
303 protected function find_hook()
305 $p = explode('/', $this->path
);
306 list($hook, $matched, $remain, $aliased) = $this->hooks
->findChild($p);
310 $this->argv
= $remain;
311 array_unshift($this->argv
, implode('/', $matched));
312 if (!empty($aliased)) {
313 $this->ns
= implode('/', $aliased) . '/';
315 $this->https
= !$hook->hasType(NO_HTTPS
);
319 public function near_hook()
321 $p = explode('/', $this->path
);
322 list($hook, $matched, $remain, $aliased) = $this->hooks
->findNearestChild($p);
326 $url = implode('/', $matched);
327 if (!empty($remain)) {
328 $url .= '/' . implode('/', $remain);
330 if ($url == $this->path ||
levenshtein($url, $this->path
) > strlen($url) / 3
331 ||
!$hook->checkPerms()) {
337 private function call_hook(PlPage
&$page)
339 $hook = $this->find_hook();
343 global $globals, $session;
344 if ($this->https
&& !@$_SERVER['HTTPS'] && $globals->core
->secure_domain
) {
345 http_redirect('https://' . $globals->core
->secure_domain
. $_SERVER['REQUEST_URI']);
348 return $hook->call($page, $this->argv
);
351 /** Show the authentication form.
353 abstract public function force_login(PlPage
& $page);
355 public function run()
357 $page =& self
::page();
359 if (empty($this->path
)) {
360 $this->path
= 'index';
364 $page->assign('platal', $this);
365 $res = $this->call_hook($page);
368 $this->mods
['core']->handler_403($page);
372 $this->mods
['core']->handler_404($page);
378 } catch (Exception
$e) {
379 header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
380 PlErrorReport
::report($e);
381 if (self
::globals()->debug
) {
382 $page->kill(pl_entities($e->getMessage())
383 . '<pre>' . pl_entities("" . $e) . '</pre>');
385 $page->kill(pl_entities($e->getMessage()));
389 $page->assign('platal', $this);
390 if ($res == PL_JSON
) {
397 public function error403()
399 $page =& self
::page();
401 $this->mods
['core']->handler_403($page);
402 $page->assign('platal', $this);
406 public function error404()
408 $page =& self
::page();
410 $this->mods
['core']->handler_404($page);
411 $page->assign('platal', $this);
415 public static function notAllowed()
418 self
::page()->trigWarning('Tu accèdes à cette page car tu es administrateur du site.');
425 public static function load($modname, $include = null
)
428 $modname = strtolower($modname);
429 if (isset($platal->mods
[$modname])) {
430 if (is_null($include)) {
433 $platal->mods
[$modname]->load($include);
435 if (is_null($include)) {
436 require_once PLModule
::path($modname) . '.php';
438 require_once PLModule
::path($modname) . '/' . $include;
443 public static function assert($cond, $error, $userfriendly = null
)
445 if ($cond === false
) {
446 if ($userfriendly == null
) {
447 $userfriendly = "Une erreur interne s'est produite.
448 Merci de réessayer la manipulation qui a déclenché l'erreur ;
449 si cela ne fonctionne toujours pas, merci de nous signaler le problème rencontré.";
451 throw new PlException($userfriendly, $error);
455 public function &buildLogger($uid, $suid = 0)
457 if (defined('PL_LOGGER_CLASS')) {
458 $class = PL_LOGGER_CLASS
;
459 $logger = new $class($uid, $suid);
462 return PlLogger
::dummy($uid, $suid);
466 protected function &buildPage()
468 $pageclass = PL_PAGE_CLASS
;
469 $page = new $pageclass();
473 static public function &page()
475 if (is_null(self
::$_page)) {
477 self
::$_page = $platal->buildPage();
482 protected function &buildSession()
484 $sessionclass = PL_SESSION_CLASS
;
485 $session = new $sessionclass();
489 static public function &session()
495 protected function &buildGlobals()
497 $globalclass = PL_GLOBALS_CLASS
;
498 $globals = new $globalclass();
502 static public function &globals()
509 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: