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 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 if ($globals->mode
== '') {
57 pl_redirect('index.html');
61 public function pl_self($n = null
)
67 return join('/', array_slice($this->argv
, 0, $n +
1));
69 if ($n <= -count($this->argv
))
70 return $this->argv
[0];
72 return join('/', array_slice($this->argv
, 0, $n));
75 protected function find_hook()
80 if (array_key_exists($p, $this->__hooks
))
83 $p = substr($p, 0, strrpos($p, '/'));
86 if (empty($this->__hooks
[$p])) {
90 $hook = $this->__hooks
[$p];
92 if (!is_callable($hook['hook'])) {
96 $this->https
= ($hook['type'] & NO_HTTPS
) ? false
: true
;
97 $this->argv
= explode('/', substr($this->path
, strlen($p)));
103 protected function find_nearest_key($key, array &$array)
105 $keys = array_keys($array);
106 if (in_array($key, $keys)) {
110 if (($pos = strpos($key, '.php')) !== false
) {
111 $key = substr($key, 0, $pos);
114 $has_end = in_array("#final#", $keys);
115 if (strlen($key) > 24 && $has_end) {
119 foreach ($keys as $k) {
120 if ($k == "#final#") {
123 $lev = levenshtein($key, $k);
125 if ((!isset($val) ||
$lev < $val)
126 && ($lev <= strlen($k)/2 ||
strpos($k, $key) !== false ||
strpos($key, $k) !== false
)) {
131 if (!isset($best) && $has_end) {
133 } else if (isset($best)) {
139 public function near_hook()
143 foreach ($this->__hooks
as $hook=>$handler) {
144 if (!$this->check_perms($handler['perms'])) {
147 $parts = split('/', $hook);
149 foreach ($parts as $part) {
150 if (!isset($place[$part])) {
151 $place[$part] = array();
153 $place =& $place[$part];
155 $leaf = $parts[count($parts)-1];
156 if (!isset($leafs[$leaf])) {
157 $leafs[$leaf] = $hook;
158 } else if (is_array($leafs[$leaf])) {
159 $leafs[$leaf][] = $hook;
161 $leafs[$leaf] = array($hook, $leafs[$leaf]);
163 $place["#final#"] = array();
166 // search for the nearest full path
167 $p = split('/', $this->path
);
171 if (!isset($ended)) {
172 $key = $this->find_nearest_key($k, $place);
176 if ($key == "#final#") {
177 if (!array_key_exists($link, $this->__hooks
)) {
184 if (!is_null($key)) {
189 $place =& $place[$key];
195 if ($link == $this->path
) {
198 if ($link && levenshtein($link, $this->path
) < strlen($link)/3) {
202 // search for missing namespace (the given name is a leaf)
203 $leaf = array_shift($p);
204 $args = count($p) ?
'/' . implode('/', $p) : '';
205 if (isset($leafs[$leaf]) && !is_array($leafs[$leaf]) && $leafs[$leaf] != $this->path
) {
206 return $leafs[$leaf] . $args;
210 foreach ($leafs as $k=>&$path) {
211 if (is_array($path)) {
214 $lev = levenshtein($leaf, $k);
216 if ((!isset($val) ||
$lev < $val)
217 && ($lev <= strlen($k)/2 ||
strpos($k, $leaf) !== false ||
strpos($leaf, $k) !== false
)) {
222 return $best == null ?
( $link ?
$link : null
) : $best . $args;
225 protected function check_perms($perms)
227 if (!$perms) { // No perms, no check
230 $s_perms = S
::v('perms');
231 return $s_perms->hasFlagCombination($perms);
234 private function call_hook(PlatalPage
&$page)
236 $hook = $this->find_hook();
241 if ($this->https
&& !$_SERVER['HTTPS'] && $globals->core
->secure_domain
) {
242 http_redirect('https://' . $globals->core
->secure_domain
. $_SERVER['REQUEST_URI']);
248 if ($hook['auth'] > S
::v('auth', AUTH_PUBLIC
)) {
249 if ($hook['type'] & DO_AUTH
) {
250 if (!call_user_func(array($globals->session
, 'doAuth'))) {
251 $this->force_login($page);
257 if ($hook['auth'] != AUTH_PUBLIC
&& !$this->check_perms($hook['perms'])) {
261 $val = call_user_func_array($hook['hook'], $args);
262 if ($val == PL_DO_AUTH
) {
263 // The handler need a better auth with the current args
264 if (!call_user_func(array($globals->session
, 'doAuth'))) {
265 $this->force_login($page);
267 $val = call_user_func_array($hook['hook'], $args);
272 public function force_login(PlatalPage
&$page)
274 header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
276 $page->changeTpl('core/password_prompt_logged.tpl');
277 $page->addJsLink('do_challenge_response_logged.js');
279 $page->changeTpl('core/password_prompt.tpl');
280 $page->addJsLink('do_challenge_response.js');
282 $page->assign('platal', $this);
286 public function run()
290 new_skinned_page('platal/index.tpl');
292 if (empty($this->path
)) {
293 $this->path
= 'index';
296 $page->assign('platal', $this);
297 switch ($this->call_hook($page)) {
299 $this->__mods
['core']->handler_403($page);
303 $this->__mods
['core']->handler_404($page);
307 $page->assign('platal', $this);
311 public function on_subscribe($forlife, $uid, $promo, $pass)
313 $args = func_get_args();
314 foreach ($this->__mods
as $mod) {
315 if (!is_callable($mod, 'on_subscribe'))
317 call_user_func_array(array($mod, 'on_subscribe'), $args);
322 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: