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 $modules = func_get_args();
38 $this->path
= trim(Get
::_get('n', null
), '/');
40 $this->__mods
= array();
41 $this->__hooks
= array();
43 array_unshift($modules, 'core');
44 foreach ($modules as $module) {
45 $this->__mods
[$module] = $m = PLModule
::factory($module);
46 $this->__hooks +
= $m->handlers();
50 function pl_self($n = null
)
56 return join('/', array_slice($this->argv
, 0, $n +
1));
58 if ($n <= -count($this->argv
))
59 return $this->argv
[0];
61 return join('/', array_slice($this->argv
, 0, $n));
69 if (array_key_exists($p, $this->__hooks
))
72 $p = substr($p, 0, strrpos($p, '/'));
75 if (empty($this->__hooks
[$p])) {
79 $hook = $this->__hooks
[$p];
81 if (!is_callable($hook['hook'])) {
85 $this->argv
= explode('/', substr($this->path
, strlen($p)));
91 function find_nearest_key($key, &$array)
93 $keys = array_keys($array);
94 if (in_array($key, $keys)) {
98 if (($pos = strpos($key, '.php')) !== false
) {
99 $key = substr($key, 0, $pos);
102 $has_end = in_array("#final#", $keys);
103 if (strlen($key) > 24 && $has_end) {
107 foreach ($keys as $k) {
108 if ($k == "#final#") {
111 $lev = levenshtein($key, $k);
112 if ((!isset($val) ||
$lev < $val) && $lev <= (strlen($k)*2)/3) {
117 if (!isset($best) && $has_end) {
128 foreach ($this->__hooks
as $hook=>$handler) {
129 if (!empty($handler['perms']) && $handler['perms'] != S
::v('perms')) {
132 $parts = split('/', $hook);
134 foreach ($parts as $part) {
135 if (!isset($place[$part])) {
136 $place[$part] = array();
138 $place =& $place[$part];
140 $place["#final#"] = array();
143 $p = split('/', $this->path
);
147 if (!isset($ended)) {
148 $key = $this->find_nearest_key($k, $place);
152 if ($key == "#final#") {
153 if (!array_key_exists($link, $this->__hooks
)) {
159 if (!is_null($key)) {
164 $place =& $place[$key];
169 if ($link != $this->path
) {
175 function call_hook(&$page)
177 $hook = $this->find_hook();
185 if ($hook['auth'] > S
::v('auth', AUTH_PUBLIC
)) {
186 if ($hook['type'] == DO_AUTH
) {
189 if (!call_user_func(array($globals->session
, 'doAuth'))) {
190 $this->force_login($page);
197 if (!empty($hook['perms']) && $hook['perms'] != S
::v('perms')) {
201 $val = call_user_func_array($hook['hook'], $args);
202 if ($val == PL_DO_AUTH
) {
203 // The handler need a better auth with the current args
204 if (!call_user_func(array($globals->session
, 'doAuth'))) {
205 $this->force_login($page);
207 $val = call_user_func_array($hook['hook'], $args);
212 function force_login(&$page)
215 $page->changeTpl('core/password_prompt_logged.tpl');
216 $page->addJsLink('do_challenge_response_logged.js');
218 $page->changeTpl('core/password_prompt.tpl');
219 $page->addJsLink('do_challenge_response.js');
228 new_skinned_page('platal/index.tpl');
230 if (empty($this->path
)) {
231 $this->path
= 'index';
234 $page->assign('platal', $this);
235 switch ($this->call_hook($page)) {
237 $this->__mods
['core']->handler_403($page);
241 $this->__mods
['core']->handler_404($page);
245 $page->assign('platal', $this);
249 function on_subscribe($forlife, $uid, $promo, $pass)
251 $args = func_get_args();
252 foreach ($this->__mods
as $mod) {
253 if (!is_callable($mod, 'on_subscribe'))
255 call_user_func_array(array($mod, 'on_subscribe'), $args);