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);
33 protected function __construct($auth = AUTH_PUBLIC
, $perms = 'user', $type = DO_AUTH
)
36 $this->perms
= $perms;
40 public function needAuth()
42 return $this->auth
> S
::i('auth', AUTH_PUBLIC
);
45 public function checkPerms()
47 if (!$this->perms ||
$this->auth
== AUTH_PUBLIC
) { // No perms, no check
50 $s_perms = S
::v('perms');
51 return $s_perms->hasFlagCombination($this->perms
);
54 public function hasType($type)
56 return ($this->type
& $type) == $type;
59 abstract protected function run(PlPage
&$page, array $args);
61 public function call(PlPage
&$page, array $args)
63 global $globals, $session, $platal;
64 if ($this->needAuth()) {
65 if ($this->hasType(DO_AUTH
)) {
66 if (!$session->start($this->auth
)) {
67 $platal->force_login($page);
73 if (!$this->checkPerms()) {
74 if (!Platal
::notAllowed()) {
78 return $this->run($page, $args);
82 class PlStdHook
extends PlHook
86 public function __construct($callback, $auth = AUTH_PUBLIC
, $perms = 'user', $type = DO_AUTH
)
88 parent
::__construct($auth, $perms, $type);
89 $this->hook
= $callback;
92 protected function run(PlPage
&$page, array $args)
94 global $session, $platal;
97 $val = call_user_func_array($this->hook
, $args);
98 if ($val == PL_DO_AUTH
) {
99 if (!$session->start($session->loggedLevel())) {
100 $platal->force_login($page);
102 $val = call_user_func_array($this->hook
, $args);
108 class PlWikiHook
extends PlHook
110 public function __construct($auth = AUTH_PUBLIC
, $perms = 'user', $type = DO_AUTH
)
112 parent
::__construct($auth, $perms, $type);
115 protected function run(PlPage
&$page, array $args)
124 public $aliased = null
;
125 public $children = array();
127 public function addChild(array $path, PlHook
$hook)
130 $next = array_shift($path);
132 if ($next && $next{0} == '%') {
134 $next = $platal->hook_map(substr($next, 1));
139 @$child =& $this->children
[$next];
141 $child = new PlHookTree();
142 $this->children
[$next] =& $child;
143 $child->aliased
= $alias;
146 $child->hook
= $hook;
148 $child->addChild($path, $hook);
152 private function findChildAux(array $remain, array $matched, array $aliased)
155 if ($this->aliased
) {
159 $child = @$this->children
[$next];
161 array_shift($remain);
163 return $child->findChildAux($remain, $matched, $aliased);
166 return array($this->hook
, $matched, $remain, $aliased);
169 public function findChild(array $path)
171 return $this->findChildAux($path, array(), array());
174 private function findNearestChildAux(array $remain, array $matched, array $aliased)
177 if ($this->aliased
) {
181 $child = @$this->children
[$next];
186 foreach ($this->children
as $path=>$hook) {
187 $lev = levenshtein($next, $path);
188 if ($lev <= $nearest_lev
189 && ($lev < strlen($next) / 2 ||
strpos($next, $path) !== false
190 ||
strpos($path, $next) !== false
)) {
191 $sdx = levenshtein(soundex($next), soundex($path));
192 if ($lev == $nearest_lev ||
$sdx < $nearest_sdx) {
203 array_shift($remain);
205 return $child->findNearestChildAux($remain, $matched, $aliased);
207 if (($pos = strpos($next, '.php')) !== false
) {
208 $remain[0] = substr($next, 0, $pos);
209 return $this->findNearestChildAux($remain, $matched, $aliased);
212 return array($this->hook
, $matched, $remain, $aliased);
215 public function findNearestChild(array $path)
217 return $this->findNearestChildAux($path, array(), array());
222 abstract class Platal
233 static private $_page = null
;
235 public function __construct()
237 global $platal, $session, $globals;
240 /* Assign globals first, then call init: init must be used for operations
241 * that requires access to the content of $globals (e.g. XDB requires
242 * $globals to be assigned.
244 $globals = $this->buildGlobals();
247 /* Get the current session: assign first, then activate the session.
249 $session = $this->buildSession();
250 if (!$session->startAvailableAuth()) {
251 Platal
::page()->trigError("Données d'authentification invalides.");
254 $modules = func_get_args();
255 if (isset($modules[0]) && is_array($modules[0])) {
256 $modules = $modules[0];
258 $this->path
= trim(Get
::_get('n', null
), '/');
260 $this->mods
= array();
261 $this->hooks
= new PlHookTree();
263 array_unshift($modules, 'core');
264 foreach ($modules as $module) {
265 $module = strtolower($module);
266 $this->mods
[$module] = $m = PLModule
::factory($module);
267 $hooks = $m->handlers();
268 foreach ($hooks as $path=>$hook) {
269 $this->hooks
->addChild(split('/', $path), $hook);
273 if ($globals->mode
== '') {
274 pl_redirect('index.html');
278 public function pl_self($n = null
)
284 return join('/', array_slice($this->argv
, 0, $n +
1));
286 if ($n <= -count($this->argv
))
287 return $this->argv
[0];
289 return join('/', array_slice($this->argv
, 0, $n));
292 public static function wiki_hook($auth = AUTH_PUBLIC
, $perms = 'user', $type = DO_AUTH
)
294 return new PlWikiHook($auth, $perms, $type);
297 public function hook_map($name)
302 protected function find_hook()
304 $p = split('/', $this->path
);
305 list($hook, $matched, $remain, $aliased) = $this->hooks
->findChild($p);
309 $this->argv
= $remain;
310 array_unshift($this->argv
, implode('/', $matched));
311 if (!empty($aliased)) {
312 $this->ns
= implode('/', $aliased) . '/';
314 $this->https
= !$hook->hasType(NO_HTTPS
);
318 public function near_hook()
320 $p = split('/', $this->path
);
321 list($hook, $matched, $remain, $aliased) = $this->hooks
->findNearestChild($p);
325 $url = implode('/', $matched);
326 if (!empty($remain)) {
327 $url .= '/' . implode('/', $remain);
329 if ($url == $this->path ||
levenshtein($url, $this->path
) > strlen($url) / 3
330 ||
!$hook->checkPerms()) {
336 private function call_hook(PlPage
&$page)
338 $hook = $this->find_hook();
342 global $globals, $session;
343 if ($this->https
&& !@$_SERVER['HTTPS'] && $globals->core
->secure_domain
) {
344 http_redirect('https://' . $globals->core
->secure_domain
. $_SERVER['REQUEST_URI']);
347 return $hook->call($page, $this->argv
);
350 /** Show the authentication form.
352 abstract public function force_login(PlPage
& $page);
354 public function run()
356 $page =& self
::page();
358 if (empty($this->path
)) {
359 $this->path
= 'index';
362 $page->assign('platal', $this);
363 switch ($this->call_hook($page)) {
365 $this->mods
['core']->handler_403($page);
369 $this->mods
['core']->handler_404($page);
376 $page->assign('platal', $this);
380 public function error403()
382 $page =& self
::page();
384 $this->mods
['core']->handler_403($page);
385 $page->assign('platal', $this);
389 public function error404()
391 $page =& self
::page();
393 $this->mods
['core']->handler_404($page);
394 $page->assign('platal', $this);
398 public static function notAllowed()
401 self
::page()->trigWarning('Tu accèdes à cette page car tu es administrateur du site.');
408 public static function load($modname, $include = null
)
411 $modname = strtolower($modname);
412 if (isset($platal->mods
[$modname])) {
413 if (is_null($include)) {
416 $platal->mods
[$modname]->load($include);
418 if (is_null($include)) {
419 require_once PLModule
::path($modname) . '.php';
421 require_once PLModule
::path($modname) . '/' . $include;
426 public static function assert($cond, $error, $userfriendly)
429 if ($cond === false
) {
430 header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
431 $file = fopen($globals->spoolroot
. '/spool/tmp/assert_erros', 'a');
432 fwrite($file, '<pre>' . pl_entities($error) . '</pre>\n');
435 Platal
::page()->kill($userfriendly);
439 public function &buildLogger($uid, $suid = 0)
441 if (defined('PL_LOGGER_CLASS')) {
442 $class = PL_LOGGER_CLASS
;
443 return new $class($uid, $suid);
445 return PlLogger
::dummy($uid, $suid);
449 protected function &buildPage()
451 $pageclass = PL_PAGE_CLASS
;
452 $page = new $pageclass();
456 static public function &page()
458 if (is_null(self
::$_page)) {
460 self
::$_page = $platal->buildPage();
465 protected function &buildSession()
467 $sessionclass = PL_SESSION_CLASS
;
468 $session = new $sessionclass();
472 static public function &session()
478 protected function &buildGlobals()
480 $globalclass = PL_GLOBALS_CLASS
;
481 $globals = new $globalclass();
485 static public function &globals()
492 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: