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);
35 public function __construct()
37 $modules = func_get_args();
38 if (is_array($modules[0])) {
39 $modules = $modules[0];
41 $this->path
= trim(Get
::_get('n', null
), '/');
43 $this->__mods
= array();
44 $this->__hooks
= array();
46 array_unshift($modules, 'core');
47 foreach ($modules as $module) {
48 $this->__mods
[$module] = $m = PLModule
::factory($module);
49 $this->__hooks +
= $m->handlers();
53 public function pl_self($n = null
)
59 return join('/', array_slice($this->argv
, 0, $n +
1));
61 if ($n <= -count($this->argv
))
62 return $this->argv
[0];
64 return join('/', array_slice($this->argv
, 0, $n));
67 protected function find_hook()
72 if (array_key_exists($p, $this->__hooks
))
75 $p = substr($p, 0, strrpos($p, '/'));
78 if (empty($this->__hooks
[$p])) {
82 $hook = $this->__hooks
[$p];
84 if (!is_callable($hook['hook'])) {
88 $this->argv
= explode('/', substr($this->path
, strlen($p)));
94 protected function find_nearest_key($key, array &$array)
96 $keys = array_keys($array);
97 if (in_array($key, $keys)) {
101 if (($pos = strpos($key, '.php')) !== false
) {
102 $key = substr($key, 0, $pos);
105 $has_end = in_array("#final#", $keys);
106 if (strlen($key) > 24 && $has_end) {
110 foreach ($keys as $k) {
111 if ($k == "#final#") {
114 $lev = levenshtein($key, $k);
115 if ((!isset($val) ||
$lev < $val) && $lev <= (strlen($k)*2)/3) {
120 if (!isset($best) && $has_end) {
128 public function near_hook()
131 foreach ($this->__hooks
as $hook=>$handler) {
132 if (!empty($handler['perms']) && $handler['perms'] != S
::v('perms')) {
135 $parts = split('/', $hook);
137 foreach ($parts as $part) {
138 if (!isset($place[$part])) {
139 $place[$part] = array();
141 $place =& $place[$part];
143 $place["#final#"] = array();
146 $p = split('/', $this->path
);
150 if (!isset($ended)) {
151 $key = $this->find_nearest_key($k, $place);
155 if ($key == "#final#") {
156 if (!array_key_exists($link, $this->__hooks
)) {
162 if (!is_null($key)) {
167 $place =& $place[$key];
172 if ($link != $this->path
) {
178 protected function check_perms($perms)
180 if (!$perms) { // No perms, no check
183 $s_perms = S
::v('perms');
185 // hook perms syntax is
186 $perms = explode(',', $perms);
187 foreach ($perms as $perm)
190 $rights = explode(':', $perm);
191 foreach ($rights as $right) {
192 if (($right{0} == '!' && $s_perms->hasFlag(substr($right, 1))) ||
!$s_perms->hasFlag($right)) {
203 private function call_hook(PlatalPage
&$page)
205 $hook = $this->find_hook();
213 if ($hook['auth'] > S
::v('auth', AUTH_PUBLIC
)) {
214 if ($hook['type'] == DO_AUTH
) {
217 if (!call_user_func(array($globals->session
, 'doAuth'))) {
218 $this->force_login($page);
224 if ($hook['auth'] != AUTH_PUBLIC
&& !$this->check_perms($hook['perms'])) {
228 $val = call_user_func_array($hook['hook'], $args);
229 if ($val == PL_DO_AUTH
) {
231 // The handler need a better auth with the current args
232 if (!call_user_func(array($globals->session
, 'doAuth'))) {
233 $this->force_login($page);
235 $val = call_user_func_array($hook['hook'], $args);
240 public function force_login(PlatalPage
&$page)
243 $page->changeTpl('core/password_prompt_logged.tpl');
244 $page->addJsLink('do_challenge_response_logged.js');
246 $page->changeTpl('core/password_prompt.tpl');
247 $page->addJsLink('do_challenge_response.js');
249 $page->assign('platal', $this);
253 public function run()
257 new_skinned_page('platal/index.tpl');
259 if (empty($this->path
)) {
260 $this->path
= 'index';
263 $page->assign('platal', $this);
264 switch ($this->call_hook($page)) {
266 $this->__mods
['core']->handler_403($page);
270 $this->__mods
['core']->handler_404($page);
274 $page->assign('platal', $this);
278 private function on_subscribe($forlife, $uid, $promo, $pass)
280 $args = func_get_args();
281 foreach ($this->__mods
as $mod) {
282 if (!is_callable($mod, 'on_subscribe'))
284 call_user_func_array(array($mod, 'on_subscribe'), $args);
289 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: