2d988bccfebd361aa0d6d355a946ee265140a470
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_WIKI_HOOK', '@@WIKI@@');
39 static private $_page = null
;
41 public function __construct()
43 global $platal, $session, $globals;
46 /* Assign globals first, then call init: init must be used for operations
47 * that requires access to the content of $globals (e.g. XDB requires
48 * $globals to be assigned.
50 $globals = $this->buildGlobals();
53 /* Get the current session: assign first, then activate the session.
55 $session = $this->buildSession();
56 if (!$session->startAvailableAuth()) {
57 Platal
::page()->trigError("Données d'authentification invalides.");
60 $modules = func_get_args();
61 if (isset($modules[0]) && is_array($modules[0])) {
62 $modules = $modules[0];
64 $this->path
= trim(Get
::_get('n', null
), '/');
66 $this->__mods
= array();
67 $this->__hooks
= array();
69 array_unshift($modules, 'core');
70 foreach ($modules as $module) {
71 $module = strtolower($module);
72 $this->__mods
[$module] = $m = PLModule
::factory($module);
73 $this->__hooks
= $m->handlers() +
$this->__hooks
;
76 if ($globals->mode
== '') {
77 pl_redirect('index.html');
81 public function pl_self($n = null
)
87 return join('/', array_slice($this->argv
, 0, $n +
1));
89 if ($n <= -count($this->argv
))
90 return $this->argv
[0];
92 return join('/', array_slice($this->argv
, 0, $n));
95 public static function wiki_hook($auth = AUTH_PUBLIC
, $perms = 'user', $type = DO_AUTH
)
97 return array('hook' => PL_WIKI_HOOK
,
103 protected function find_hook()
108 if (array_key_exists($p, $this->__hooks
))
111 $p = substr($p, 0, strrpos($p, '/'));
114 if (empty($this->__hooks
[$p])) {
118 $hook = $this->__hooks
[$p];
120 if (!is_callable($hook['hook']) && $hook['hook'] != PL_WIKI_HOOK
) {
124 $this->https
= ($hook['type'] & NO_HTTPS
) ? false
: true
;
125 $this->argv
= explode('/', substr($this->path
, strlen($p)));
131 protected function find_nearest_key($key, array &$array)
133 $keys = array_keys($array);
134 if (in_array($key, $keys)) {
138 if (($pos = strpos($key, '.php')) !== false
) {
139 $key = substr($key, 0, $pos);
142 $has_end = in_array("#final#", $keys);
143 if (strlen($key) > 24 && $has_end) {
147 foreach ($keys as $k) {
148 if ($k == "#final#") {
151 $lev = levenshtein($key, $k);
153 if ((!isset($val) ||
$lev < $val)
154 && ($lev <= strlen($k)/2 ||
strpos($k, $key) !== false ||
strpos($key, $k) !== false
)) {
159 if (!isset($best) && $has_end) {
161 } else if (isset($best)) {
167 public function near_hook()
171 foreach ($this->__hooks
as $hook=>$handler) {
172 if (!$this->check_perms($handler['perms'])) {
175 $parts = split('/', $hook);
177 foreach ($parts as $part) {
178 if (!isset($place[$part])) {
179 $place[$part] = array();
181 $place =& $place[$part];
183 $leaf = $parts[count($parts)-1];
184 if (!isset($leafs[$leaf])) {
185 $leafs[$leaf] = $hook;
186 } else if (is_array($leafs[$leaf])) {
187 $leafs[$leaf][] = $hook;
189 $leafs[$leaf] = array($hook, $leafs[$leaf]);
191 $place["#final#"] = array();
194 // search for the nearest full path
195 $p = split('/', $this->path
);
199 if (!isset($ended)) {
200 $key = $this->find_nearest_key($k, $place);
204 if ($key == "#final#") {
205 if (!array_key_exists($link, $this->__hooks
)) {
212 if (!is_null($key)) {
217 $place =& $place[$key];
223 if ($link == $this->path
) {
226 if ($link && levenshtein($link, $this->path
) < strlen($link)/3) {
230 // search for missing namespace (the given name is a leaf)
231 $leaf = array_shift($p);
232 $args = count($p) ?
'/' . implode('/', $p) : '';
233 if (isset($leafs[$leaf]) && !is_array($leafs[$leaf]) && $leafs[$leaf] != $this->path
) {
234 return $leafs[$leaf] . $args;
238 foreach ($leafs as $k=>&$path) {
239 if (is_array($path)) {
242 $lev = levenshtein($leaf, $k);
244 if ((!isset($val) ||
$lev < $val)
245 && ($lev <= strlen($k)/2 ||
strpos($k, $leaf) !== false ||
strpos($leaf, $k) !== false
)) {
250 return $best == null ?
( $link ?
$link : null
) : $best . $args;
253 protected function check_perms($perms)
255 if (!$perms) { // No perms, no check
258 $s_perms = S
::v('perms');
259 return $s_perms->hasFlagCombination($perms);
262 private function call_hook(PlPage
&$page)
264 $hook = $this->find_hook();
268 global $globals, $session;
269 if ($this->https
&& !@$_SERVER['HTTPS'] && $globals->core
->secure_domain
) {
270 http_redirect('https://' . $globals->core
->secure_domain
. $_SERVER['REQUEST_URI']);
276 if ($hook['auth'] > S
::v('auth', AUTH_PUBLIC
)) {
277 if ($hook['type'] & DO_AUTH
) {
278 if (!$session->start($hook['auth'])) {
279 $this->force_login($page);
285 if ($hook['auth'] != AUTH_PUBLIC
&& !$this->check_perms($hook['perms'])) {
286 if (self
::notAllowed()) {
291 if ($hook['hook'] == PL_WIKI_HOOK
) {
294 $val = call_user_func_array($hook['hook'], $args);
295 if ($val == PL_DO_AUTH
) {
296 // The handler need a better auth with the current args
297 if (!$session->start($session->loggedLevel())) {
298 $this->force_login($page);
300 $val = call_user_func_array($hook['hook'], $args);
305 /** Show the authentication form.
307 abstract public function force_login(PlPage
& $page);
309 public function run()
311 $page =& self
::page();
313 if (empty($this->path
)) {
314 $this->path
= 'index';
317 $page->assign('platal', $this);
318 switch ($this->call_hook($page)) {
320 $this->__mods
['core']->handler_403($page);
324 $this->__mods
['core']->handler_404($page);
331 $page->assign('platal', $this);
335 public function error403()
337 $page =& self
::page();
339 $this->__mods
['core']->handler_403($page);
340 $page->assign('platal', $this);
344 public function error404()
346 $page =& self
::page();
348 $this->__mods
['core']->handler_404($page);
349 $page->assign('platal', $this);
353 public static function notAllowed()
356 self
::page()->trigWarning('Tu accèdes à cette page car tu es administrateur du site.');
363 public static function load($modname, $include = null
)
366 $modname = strtolower($modname);
367 if (isset($platal->__mods
[$modname])) {
368 if (is_null($include)) {
371 $platal->__mods
[$modname]->load($include);
373 if (is_null($include)) {
374 require_once PLModule
::path($modname) . '.php';
376 require_once PLModule
::path($modname) . '/' . $include;
381 public static function assert($cond, $error, $userfriendly)
384 if ($cond === false
) {
385 header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
386 $file = fopen($globals->spoolroot
. '/spool/tmp/assert_erros', 'a');
387 fwrite($file, '<pre>' . pl_entities($error) . '</pre>\n');
390 Platal
::page()->kill($userfriendly);
394 public function &buildLogger($uid, $suid = 0)
396 if (defined('PL_LOGGER_CLASS')) {
397 $class = PL_LOGGER_CLASS
;
398 return new $class($uid, $suid);
400 return PlLogger
::dummy($uid, $suid);
404 protected function &buildPage()
406 $pageclass = PL_PAGE_CLASS
;
407 $page = new $pageclass();
411 static public function &page()
413 if (is_null(self
::$_page)) {
415 self
::$_page = $platal->buildPage();
420 protected function &buildSession()
422 $sessionclass = PL_SESSION_CLASS
;
423 $session = new $sessionclass();
427 static public function &session()
433 protected function &buildGlobals()
435 $globalclass = PL_GLOBALS_CLASS
;
436 $globals = new $globalclass();
440 static public function &globals()
447 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: