a1aade66e27c1f9bfbd211dc0589f43f25c1df8c
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;
45 $globalclass = PL_GLOBALS_CLASS
;
46 $globals = new $globalclass();
48 $sessionclass = PL_SESSION_CLASS
;
49 $session = new $sessionclass();
50 if (!$session->startAvailableAuth()) {
51 Platal
::page()->trigError("Données d'authentification invalides.");
54 $modules = func_get_args();
55 if (isset($modules[0]) && is_array($modules[0])) {
56 $modules = $modules[0];
58 $this->path
= trim(Get
::_get('n', null
), '/');
60 $this->__mods
= array();
61 $this->__hooks
= array();
63 array_unshift($modules, 'core');
64 foreach ($modules as $module) {
65 $module = strtolower($module);
66 $this->__mods
[$module] = $m = PLModule
::factory($module);
67 $this->__hooks
= $m->handlers() +
$this->__hooks
;
70 if ($globals->mode
== '') {
71 pl_redirect('index.html');
75 public function pl_self($n = null
)
81 return join('/', array_slice($this->argv
, 0, $n +
1));
83 if ($n <= -count($this->argv
))
84 return $this->argv
[0];
86 return join('/', array_slice($this->argv
, 0, $n));
89 public static function wiki_hook($auth = AUTH_PUBLIC
, $perms = 'user', $type = DO_AUTH
)
91 return array('hook' => PL_WIKI_HOOK
,
97 protected function find_hook()
102 if (array_key_exists($p, $this->__hooks
))
105 $p = substr($p, 0, strrpos($p, '/'));
108 if (empty($this->__hooks
[$p])) {
112 $hook = $this->__hooks
[$p];
114 if (!is_callable($hook['hook']) && $hook['hook'] != PL_WIKI_HOOK
) {
118 $this->https
= ($hook['type'] & NO_HTTPS
) ? false
: true
;
119 $this->argv
= explode('/', substr($this->path
, strlen($p)));
125 protected function find_nearest_key($key, array &$array)
127 $keys = array_keys($array);
128 if (in_array($key, $keys)) {
132 if (($pos = strpos($key, '.php')) !== false
) {
133 $key = substr($key, 0, $pos);
136 $has_end = in_array("#final#", $keys);
137 if (strlen($key) > 24 && $has_end) {
141 foreach ($keys as $k) {
142 if ($k == "#final#") {
145 $lev = levenshtein($key, $k);
147 if ((!isset($val) ||
$lev < $val)
148 && ($lev <= strlen($k)/2 ||
strpos($k, $key) !== false ||
strpos($key, $k) !== false
)) {
153 if (!isset($best) && $has_end) {
155 } else if (isset($best)) {
161 public function near_hook()
165 foreach ($this->__hooks
as $hook=>$handler) {
166 if (!$this->check_perms($handler['perms'])) {
169 $parts = split('/', $hook);
171 foreach ($parts as $part) {
172 if (!isset($place[$part])) {
173 $place[$part] = array();
175 $place =& $place[$part];
177 $leaf = $parts[count($parts)-1];
178 if (!isset($leafs[$leaf])) {
179 $leafs[$leaf] = $hook;
180 } else if (is_array($leafs[$leaf])) {
181 $leafs[$leaf][] = $hook;
183 $leafs[$leaf] = array($hook, $leafs[$leaf]);
185 $place["#final#"] = array();
188 // search for the nearest full path
189 $p = split('/', $this->path
);
193 if (!isset($ended)) {
194 $key = $this->find_nearest_key($k, $place);
198 if ($key == "#final#") {
199 if (!array_key_exists($link, $this->__hooks
)) {
206 if (!is_null($key)) {
211 $place =& $place[$key];
217 if ($link == $this->path
) {
220 if ($link && levenshtein($link, $this->path
) < strlen($link)/3) {
224 // search for missing namespace (the given name is a leaf)
225 $leaf = array_shift($p);
226 $args = count($p) ?
'/' . implode('/', $p) : '';
227 if (isset($leafs[$leaf]) && !is_array($leafs[$leaf]) && $leafs[$leaf] != $this->path
) {
228 return $leafs[$leaf] . $args;
232 foreach ($leafs as $k=>&$path) {
233 if (is_array($path)) {
236 $lev = levenshtein($leaf, $k);
238 if ((!isset($val) ||
$lev < $val)
239 && ($lev <= strlen($k)/2 ||
strpos($k, $leaf) !== false ||
strpos($leaf, $k) !== false
)) {
244 return $best == null ?
( $link ?
$link : null
) : $best . $args;
247 protected function check_perms($perms)
249 if (!$perms) { // No perms, no check
252 $s_perms = S
::v('perms');
253 return $s_perms->hasFlagCombination($perms);
256 private function call_hook(PlPage
&$page)
258 $hook = $this->find_hook();
262 global $globals, $session;
263 if ($this->https
&& !@$_SERVER['HTTPS'] && $globals->core
->secure_domain
) {
264 http_redirect('https://' . $globals->core
->secure_domain
. $_SERVER['REQUEST_URI']);
270 if ($hook['auth'] > S
::v('auth', AUTH_PUBLIC
)) {
271 if ($hook['type'] & DO_AUTH
) {
272 if (!$session->start($hook['auth'])) {
273 $this->force_login($page);
279 if ($hook['auth'] != AUTH_PUBLIC
&& !$this->check_perms($hook['perms'])) {
280 if (self
::notAllowed()) {
285 if ($hook['hook'] == PL_WIKI_HOOK
) {
288 $val = call_user_func_array($hook['hook'], $args);
289 if ($val == PL_DO_AUTH
) {
290 // The handler need a better auth with the current args
291 if (!$session->start($session->loggedLevel())) {
292 $this->force_login($page);
294 $val = call_user_func_array($hook['hook'], $args);
299 /** Show the authentication form.
301 abstract public function force_login(PlPage
& $page);
303 public function run()
305 $page =& self
::page();
307 if (empty($this->path
)) {
308 $this->path
= 'index';
311 $page->assign('platal', $this);
312 switch ($this->call_hook($page)) {
314 $this->__mods
['core']->handler_403($page);
318 $this->__mods
['core']->handler_404($page);
325 $page->assign('platal', $this);
329 public function error403()
331 $page =& self
::page();
333 $this->__mods
['core']->handler_403($page);
334 $page->assign('platal', $this);
338 public function error404()
340 $page =& self
::page();
342 $this->__mods
['core']->handler_404($page);
343 $page->assign('platal', $this);
347 public static function notAllowed()
350 self
::page()->trigWarning('Tu accèdes à cette page car tu es administrateur du site.');
357 public static function load($modname, $include = null
)
360 $modname = strtolower($modname);
361 if (isset($platal->__mods
[$modname])) {
362 if (is_null($include)) {
365 $platal->__mods
[$modname]->load($include);
367 if (is_null($include)) {
368 require_once PLModule
::path($modname) . '.php';
370 require_once PLModule
::path($modname) . '/' . $include;
375 public static function assert($cond, $error, $userfriendly)
378 if ($cond === false
) {
379 header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
380 $file = fopen($globals->spoolroot
. '/spool/tmp/assert_erros', 'a');
381 fwrite($file, '<pre>' . pl_entities($error) . '</pre>\n');
384 Platal
::page()->kill($userfriendly);
389 static public function &page()
392 if (is_null(self
::$_page)) {
393 $pageclass = PL_PAGE_CLASS
;
394 self
::$_page = new $pageclass();
399 static public function &session()
405 static public function &globals()
412 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: