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_FORBIDDEN', 403);
23 define('PL_NOT_FOUND', 404);
36 $modules = func_get_args();
37 $this->path
= trim(Get
::_get('n', null
), '/');
39 $this->__mods
= array();
40 $this->__hooks
= array();
42 array_unshift($modules, 'core');
43 foreach ($modules as $module) {
44 $this->__mods
[$module] = $m = PLModule
::factory($module);
45 $this->__hooks +
= $m->handlers();
49 function pl_self($n = null
)
55 return join('/', array_slice($this->argv
, 0, $n +
1));
57 if ($n <= -count($this->argv
))
58 return $this->argv
[0];
60 return join('/', array_slice($this->argv
, 0, $n));
68 if (array_key_exists($p, $this->__hooks
))
71 $p = substr($p, 0, strrpos($p, '/'));
74 if (empty($this->__hooks
[$p])) {
78 $hook = $this->__hooks
[$p];
80 if (!is_callable($hook['hook'])) {
84 $this->argv
= explode('/', substr($this->path
, strlen($p)));
90 function find_nearest_key($key, &$array)
92 $keys = array_keys($array);
93 if (in_array($key, $keys)) {
97 if (($pos = strpos($key, '.php')) !== false
) {
98 $key = substr($key, 0, $pos);
101 $has_end = in_array("#final#", $keys);
102 if (strlen($key) > 24 && $has_end) {
106 foreach ($keys as $k) {
107 if ($k == "#final#") {
110 $lev = levenshtein($key, $k);
111 if ((!isset($val) ||
$lev < $val) && $lev <= (strlen($k)*2)/3) {
116 if (!isset($best) && $has_end) {
127 foreach ($this->__hooks
as $hook=>$handler) {
128 if (!empty($handler['perms']) && $handler['perms'] != S
::v('perms')) {
131 $parts = split('/', $hook);
133 foreach ($parts as $part) {
134 if (!isset($place[$part])) {
135 $place[$part] = array();
137 $place =& $place[$part];
139 $place["#final#"] = array();
142 $p = split('/', $this->path
);
146 if (!isset($ended)) {
147 $key = $this->find_nearest_key($k, $place);
151 if ($key == "#final#") {
152 if (!array_key_exists($link, $this->__hooks
)) {
158 if (!is_null($key)) {
163 $place =& $place[$key];
168 if ($link != $this->path
) {
174 function call_hook(&$page)
176 $hook = $this->find_hook();
184 if (!empty($hook['perms']) && $hook['perms'] != S
::v('perms')) {
188 if ($hook['auth'] > S
::v('auth', AUTH_PUBLIC
)) {
189 if ($hook['type'] == DO_AUTH
) {
192 if (!call_user_func(array($globals->session
, 'doAuth'))) {
193 $this->force_login($page);
200 return call_user_func_array($hook['hook'], $args);
203 function force_login(&$page)
206 $page->changeTpl('core/password_prompt_logged.tpl');
207 $page->addJsLink('do_challenge_response_logged.js');
209 $page->changeTpl('core/password_prompt.tpl');
210 $page->addJsLink('do_challenge_response.js');
219 new_skinned_page('platal/index.tpl');
221 if (empty($this->path
)) {
222 $this->path
= 'index';
225 $page->assign('platal', $this);
226 switch ($this->call_hook($page)) {
228 $this->__mods
['core']->handler_403($page);
232 $this->__mods
['core']->handler_404($page);
236 $page->assign('platal', $this);
240 function on_subscribe($forlife, $uid, $promo, $pass)
242 $args = func_get_args();
243 foreach ($this->__mods
as $mod) {
244 if (!is_callable($mod, 'on_subscribe'))
246 call_user_func_array(array($mod, 'on_subscribe'), $args);