2a69b729c6fb04d2d7e8da313f0b7c27c0c09828
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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);
37 static private $_page = null
;
39 public function __construct()
41 global $platal, $session, $globals;
43 $globalclass = PL_GLOBALS_CLASS
;
44 $globals = new $globalclass();
45 $sessionclass = PL_SESSION_CLASS
;
46 $session = new $sessionclass();
47 if (!$session->startAvailableAuth()) {
48 Platal
::page()->trigError('Données d\'authentification invalide.');
51 $modules = func_get_args();
52 if (is_array($modules[0])) {
53 $modules = $modules[0];
55 $this->path
= trim(Get
::_get('n', null
), '/');
57 $this->__mods
= array();
58 $this->__hooks
= array();
60 array_unshift($modules, 'core');
61 foreach ($modules as $module) {
62 $module = strtolower($module);
63 $this->__mods
[$module] = $m = PLModule
::factory($module);
64 $this->__hooks +
= $m->handlers();
67 if ($globals->mode
== '') {
68 pl_redirect('index.html');
72 public function pl_self($n = null
)
78 return join('/', array_slice($this->argv
, 0, $n +
1));
80 if ($n <= -count($this->argv
))
81 return $this->argv
[0];
83 return join('/', array_slice($this->argv
, 0, $n));
86 protected function find_hook()
91 if (array_key_exists($p, $this->__hooks
))
94 $p = substr($p, 0, strrpos($p, '/'));
97 if (empty($this->__hooks
[$p])) {
101 $hook = $this->__hooks
[$p];
103 if (!is_callable($hook['hook'])) {
107 $this->https
= ($hook['type'] & NO_HTTPS
) ? false
: true
;
108 $this->argv
= explode('/', substr($this->path
, strlen($p)));
114 protected function find_nearest_key($key, array &$array)
116 $keys = array_keys($array);
117 if (in_array($key, $keys)) {
121 if (($pos = strpos($key, '.php')) !== false
) {
122 $key = substr($key, 0, $pos);
125 $has_end = in_array("#final#", $keys);
126 if (strlen($key) > 24 && $has_end) {
130 foreach ($keys as $k) {
131 if ($k == "#final#") {
134 $lev = levenshtein($key, $k);
136 if ((!isset($val) ||
$lev < $val)
137 && ($lev <= strlen($k)/2 ||
strpos($k, $key) !== false ||
strpos($key, $k) !== false
)) {
142 if (!isset($best) && $has_end) {
144 } else if (isset($best)) {
150 public function near_hook()
154 foreach ($this->__hooks
as $hook=>$handler) {
155 if (!$this->check_perms($handler['perms'])) {
158 $parts = split('/', $hook);
160 foreach ($parts as $part) {
161 if (!isset($place[$part])) {
162 $place[$part] = array();
164 $place =& $place[$part];
166 $leaf = $parts[count($parts)-1];
167 if (!isset($leafs[$leaf])) {
168 $leafs[$leaf] = $hook;
169 } else if (is_array($leafs[$leaf])) {
170 $leafs[$leaf][] = $hook;
172 $leafs[$leaf] = array($hook, $leafs[$leaf]);
174 $place["#final#"] = array();
177 // search for the nearest full path
178 $p = split('/', $this->path
);
182 if (!isset($ended)) {
183 $key = $this->find_nearest_key($k, $place);
187 if ($key == "#final#") {
188 if (!array_key_exists($link, $this->__hooks
)) {
195 if (!is_null($key)) {
200 $place =& $place[$key];
206 if ($link == $this->path
) {
209 if ($link && levenshtein($link, $this->path
) < strlen($link)/3) {
213 // search for missing namespace (the given name is a leaf)
214 $leaf = array_shift($p);
215 $args = count($p) ?
'/' . implode('/', $p) : '';
216 if (isset($leafs[$leaf]) && !is_array($leafs[$leaf]) && $leafs[$leaf] != $this->path
) {
217 return $leafs[$leaf] . $args;
221 foreach ($leafs as $k=>&$path) {
222 if (is_array($path)) {
225 $lev = levenshtein($leaf, $k);
227 if ((!isset($val) ||
$lev < $val)
228 && ($lev <= strlen($k)/2 ||
strpos($k, $leaf) !== false ||
strpos($leaf, $k) !== false
)) {
233 return $best == null ?
( $link ?
$link : null
) : $best . $args;
236 protected function check_perms($perms)
238 if (!$perms) { // No perms, no check
241 $s_perms = S
::v('perms');
242 return $s_perms->hasFlagCombination($perms);
245 private function call_hook(PlPage
&$page)
247 $hook = $this->find_hook();
251 global $globals, $session;
252 if ($this->https
&& !$_SERVER['HTTPS'] && $globals->core
->secure_domain
) {
253 http_redirect('https://' . $globals->core
->secure_domain
. $_SERVER['REQUEST_URI']);
259 if ($hook['auth'] > S
::v('auth', AUTH_PUBLIC
)) {
260 if ($hook['type'] & DO_AUTH
) {
261 if (!$session->start($hook['auth'])) {
262 $this->force_login($page);
268 if ($hook['auth'] != AUTH_PUBLIC
&& !$this->check_perms($hook['perms'])) {
272 $val = call_user_func_array($hook['hook'], $args);
273 if ($val == PL_DO_AUTH
) {
274 // The handler need a better auth with the current args
275 if (!$session->start($hook['auth'])) {
276 $this->force_login($page);
278 $val = call_user_func_array($hook['hook'], $args);
283 public function force_login(PlPage
&$page)
285 header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
287 $page->changeTpl('core/password_prompt_logged.tpl');
288 $page->addJsLink('do_challenge_response_logged.js');
290 $page->changeTpl('core/password_prompt.tpl');
291 $page->addJsLink('do_challenge_response.js');
293 $page->assign('platal', $this);
297 public function run()
299 $page =& self
::page();
301 if (empty($this->path
)) {
302 $this->path
= 'index';
305 $page->assign('platal', $this);
306 switch ($this->call_hook($page)) {
308 $this->__mods
['core']->handler_403($page);
312 $this->__mods
['core']->handler_404($page);
316 $page->assign('platal', $this);
320 public function on_subscribe($forlife, $uid, $promo, $pass)
322 $args = func_get_args();
323 foreach ($this->__mods
as $mod) {
324 if (!is_callable($mod, 'on_subscribe'))
326 call_user_func_array(array($mod, 'on_subscribe'), $args);
330 static public function &page()
333 if (is_null(self
::$_page)) {
334 $pageclass = PL_PAGE_CLASS
;
335 self
::$_page = new $pageclass();
340 static public function &session()
346 static public function &globals()
353 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: