2 /***************************************************************************
3 * Copyright (C) 2003-2007 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 public function __construct()
39 $modules = func_get_args();
40 if (is_array($modules[0])) {
41 $modules = $modules[0];
43 $this->path
= trim(Get
::_get('n', null
), '/');
45 $this->__mods
= array();
46 $this->__hooks
= array();
48 array_unshift($modules, 'core');
49 foreach ($modules as $module) {
50 $module = strtolower($module);
51 $this->__mods
[$module] = $m = PLModule
::factory($module);
52 $this->__hooks +
= $m->handlers();
56 public function pl_self($n = null
)
62 return join('/', array_slice($this->argv
, 0, $n +
1));
64 if ($n <= -count($this->argv
))
65 return $this->argv
[0];
67 return join('/', array_slice($this->argv
, 0, $n));
70 protected function find_hook()
75 if (array_key_exists($p, $this->__hooks
))
78 $p = substr($p, 0, strrpos($p, '/'));
81 if (empty($this->__hooks
[$p])) {
85 $hook = $this->__hooks
[$p];
87 if (!is_callable($hook['hook'])) {
91 $this->https
= ($hook['type'] & NO_HTTPS
) ? false
: true
;
92 $this->argv
= explode('/', substr($this->path
, strlen($p)));
98 protected function find_nearest_key($key, array &$array)
100 $keys = array_keys($array);
101 if (in_array($key, $keys)) {
105 if (($pos = strpos($key, '.php')) !== false
) {
106 $key = substr($key, 0, $pos);
109 $has_end = in_array("#final#", $keys);
110 if (strlen($key) > 24 && $has_end) {
114 foreach ($keys as $k) {
115 if ($k == "#final#") {
118 $lev = levenshtein($key, $k);
120 if ((!isset($val) ||
$lev < $val)
121 && ($lev <= strlen($k)/2 ||
strpos($k, $key) !== false ||
strpos($key, $k) !== false
)) {
126 if (!isset($best) && $has_end) {
128 } else if (isset($best)) {
134 public function near_hook()
138 foreach ($this->__hooks
as $hook=>$handler) {
139 if (!$this->check_perms($handler['perms'])) {
142 $parts = split('/', $hook);
144 foreach ($parts as $part) {
145 if (!isset($place[$part])) {
146 $place[$part] = array();
148 $place =& $place[$part];
150 $leaf = $parts[count($parts)-1];
151 if (!isset($leafs[$leaf])) {
152 $leafs[$leaf] = $hook;
153 } else if (is_array($leafs[$leaf])) {
154 $leafs[$leaf][] = $hook;
156 $leafs[$leaf] = array($hook, $leafs[$leaf]);
158 $place["#final#"] = array();
161 // search for the nearest full path
162 $p = split('/', $this->path
);
166 if (!isset($ended)) {
167 $key = $this->find_nearest_key($k, $place);
171 if ($key == "#final#") {
172 if (!array_key_exists($link, $this->__hooks
)) {
179 if (!is_null($key)) {
184 $place =& $place[$key];
190 if ($link == $this->path
) {
193 if ($link && levenshtein($link, $this->path
) < strlen($link)/3) {
197 // search for missing namespace (the given name is a leaf)
198 $leaf = array_shift($p);
199 $args = count($p) ?
'/' . implode('/', $p) : '';
200 if (isset($leafs[$leaf]) && !is_array($leafs[$leaf]) && $leafs[$leaf] != $this->path
) {
201 return $leafs[$leaf] . $args;
205 foreach ($leafs as $k=>&$path) {
206 if (is_array($path)) {
209 $lev = levenshtein($leaf, $k);
211 if ((!isset($val) ||
$lev < $val)
212 && ($lev <= strlen($k)/2 ||
strpos($k, $leaf) !== false ||
strpos($leaf, $k) !== false
)) {
217 return $best == null ?
( $link ?
$link : null
) : $best . $args;
220 protected function check_perms($perms)
222 if (!$perms) { // No perms, no check
225 $s_perms = S
::v('perms');
226 return $s_perms->hasFlagCombination($perms);
229 private function call_hook(PlatalPage
&$page)
231 $hook = $this->find_hook();
236 if ($this->https
&& !$_SERVER['HTTPS'] && $globals->core
->secure_domain
) {
237 http_redirect('https://' . $globals->core
->secure_domain
. $_SERVER['REQUEST_URI']);
243 if ($hook['auth'] > S
::v('auth', AUTH_PUBLIC
)) {
244 if ($hook['type'] & DO_AUTH
) {
245 if (!call_user_func(array($globals->session
, 'doAuth'))) {
246 $this->force_login($page);
252 if ($hook['auth'] != AUTH_PUBLIC
&& !$this->check_perms($hook['perms'])) {
256 $val = call_user_func_array($hook['hook'], $args);
257 if ($val & PL_DO_AUTH
) {
258 // The handler need a better auth with the current args
259 if (!call_user_func(array($globals->session
, 'doAuth'))) {
260 $this->force_login($page);
262 $val = call_user_func_array($hook['hook'], $args);
267 public function force_login(PlatalPage
&$page)
270 $page->changeTpl('core/password_prompt_logged.tpl');
271 $page->addJsLink('do_challenge_response_logged.js');
273 $page->changeTpl('core/password_prompt.tpl');
274 $page->addJsLink('do_challenge_response.js');
276 $page->assign('platal', $this);
280 public function run()
284 new_skinned_page('platal/index.tpl');
286 if (empty($this->path
)) {
287 $this->path
= 'index';
290 $page->assign('platal', $this);
291 switch ($this->call_hook($page)) {
293 $this->__mods
['core']->handler_403($page);
297 $this->__mods
['core']->handler_404($page);
301 $page->assign('platal', $this);
305 public function on_subscribe($forlife, $uid, $promo, $pass)
307 $args = func_get_args();
308 foreach ($this->__mods
as $mod) {
309 if (!is_callable($mod, 'on_subscribe'))
311 call_user_func_array(array($mod, 'on_subscribe'), $args);
316 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: