-> Add a search in the URL tree leafs to guess the valid URL.
[platal.git] / classes / platal.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 define('PL_DO_AUTH', 300);
23 define('PL_FORBIDDEN', 403);
24 define('PL_NOT_FOUND', 404);
25
26 class Platal
27 {
28 private $__mods;
29 private $__hooks;
30
31 protected $https;
32
33 public $ns;
34 public $path;
35 public $argv;
36
37 public function __construct()
38 {
39 $modules = func_get_args();
40 if (is_array($modules[0])) {
41 $modules = $modules[0];
42 }
43 $this->path = trim(Get::_get('n', null), '/');
44
45 $this->__mods = array();
46 $this->__hooks = array();
47
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();
53 }
54 }
55
56 public function pl_self($n = null)
57 {
58 if (is_null($n))
59 return $this->path;
60
61 if ($n >= 0)
62 return join('/', array_slice($this->argv, 0, $n + 1));
63
64 if ($n <= -count($this->argv))
65 return $this->argv[0];
66
67 return join('/', array_slice($this->argv, 0, $n));
68 }
69
70 protected function find_hook()
71 {
72 $p = $this->path;
73
74 while ($p) {
75 if (array_key_exists($p, $this->__hooks))
76 break;
77
78 $p = substr($p, 0, strrpos($p, '/'));
79 }
80
81 if (empty($this->__hooks[$p])) {
82 return null;
83 }
84
85 $hook = $this->__hooks[$p];
86
87 if (!is_callable($hook['hook'])) {
88 return null;
89 }
90
91 $this->https = ($hook['type'] & NO_HTTPS) ? false : true;
92 $this->argv = explode('/', substr($this->path, strlen($p)));
93 $this->argv[0] = $p;
94
95 return $hook;
96 }
97
98 protected function find_nearest_key($key, array &$array)
99 {
100 $keys = array_keys($array);
101 if (in_array($key, $keys)) {
102 return $key;
103 }
104
105 if (($pos = strpos($key, '.php')) !== false) {
106 $key = substr($key, 0, $pos);
107 }
108
109 $has_end = in_array("#final#", $keys);
110 if (strlen($key) > 24 && $has_end) {
111 return "#final#";
112 }
113
114 foreach ($keys as $k) {
115 if ($k == "#final#") {
116 continue;
117 }
118 $lev = levenshtein($key, $k);
119
120 if ((!isset($val) || $lev < $val)
121 && ($lev <= strlen($k)/2 || strpos($k, $key) !== false || strpos($key, $k) !== false)) {
122 $val = $lev;
123 $best = $k;
124 }
125 }
126 if (!isset($best) && $has_end) {
127 return "#final#";
128 } else if (isset($best)) {
129 return $best;
130 }
131 return null;
132 }
133
134 public function near_hook()
135 {
136 $hooks = array();
137 $leafs = array();
138 foreach ($this->__hooks as $hook=>$handler) {
139 if (!$this->check_perms($handler['perms'])) {
140 continue;
141 }
142 $parts = split('/', $hook);
143 $place =& $hooks;
144 foreach ($parts as $part) {
145 if (!isset($place[$part])) {
146 $place[$part] = array();
147 }
148 $place =& $place[$part];
149 }
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;
155 } else {
156 $leafs[$leaf] = array($hook, $leafs[$leaf]);
157 }
158 $place["#final#"] = array();
159 }
160
161 // search for the nearest full path
162 $p = split('/', $this->path);
163 $place =& $hooks;
164 $link = '';
165 foreach ($p as $k) {
166 if (!isset($ended)) {
167 $key = $this->find_nearest_key($k, $place);
168 } else {
169 $key = $k;
170 }
171 if ($key == "#final#") {
172 if (!array_key_exists($link, $this->__hooks)) {
173 $link = '';
174 break;
175 }
176 $key = $k;
177 $ended = true;
178 }
179 if (!is_null($key)) {
180 if (!empty($link)) {
181 $link .= '/';
182 }
183 $link .= $key;
184 $place =& $place[$key];
185 } else {
186 $link = '';
187 break;
188 }
189 }
190 if ($link == $this->path) {
191 $link = '';
192 }
193 if ($link && levenshtein($link, $this->path) < strlen($link)/3) {
194 return $link;
195 }
196
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;
202 }
203 unset($val);
204 $best = null;
205 foreach ($leafs as $k=>&$path) {
206 if (is_array($path)) {
207 continue;
208 }
209 $lev = levenshtein($leaf, $k);
210
211 if ((!isset($val) || $lev < $val)
212 && ($lev <= strlen($k)/2 || strpos($k, $leaf) !== false || strpos($leaf, $k) !== false)) {
213 $val = $lev;
214 $best = $path;
215 }
216 }
217 return $best == null ? ( $link ? $link : null ) : $best . $args;
218 }
219
220 protected function check_perms($perms)
221 {
222 if (!$perms) { // No perms, no check
223 return true;
224 }
225 $s_perms = S::v('perms');
226 return $s_perms->hasFlagCombination($perms);
227 }
228
229 private function call_hook(PlatalPage &$page)
230 {
231 $hook = $this->find_hook();
232 if (empty($hook)) {
233 return PL_NOT_FOUND;
234 }
235 global $globals;
236 if ($this->https && !$_SERVER['HTTPS'] && $globals->core->secure_domain) {
237 http_redirect('https://' . $globals->core->secure_domain . $_SERVER['REQUEST_URI']);
238 }
239
240 $args = $this->argv;
241 $args[0] =& $page;
242
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);
247 }
248 } else {
249 return PL_FORBIDDEN;
250 }
251 }
252 if ($hook['auth'] != AUTH_PUBLIC && !$this->check_perms($hook['perms'])) {
253 return PL_FORBIDDEN;
254 }
255
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);
261 }
262 $val = call_user_func_array($hook['hook'], $args);
263 }
264 return $val;
265 }
266
267 public function force_login(PlatalPage &$page)
268 {
269 if (S::logged()) {
270 $page->changeTpl('core/password_prompt_logged.tpl');
271 $page->addJsLink('do_challenge_response_logged.js');
272 } else {
273 $page->changeTpl('core/password_prompt.tpl');
274 $page->addJsLink('do_challenge_response.js');
275 }
276 $page->assign('platal', $this);
277 $page->run();
278 }
279
280 public function run()
281 {
282 global $page;
283
284 new_skinned_page('platal/index.tpl');
285
286 if (empty($this->path)) {
287 $this->path = 'index';
288 }
289
290 $page->assign('platal', $this);
291 switch ($this->call_hook($page)) {
292 case PL_FORBIDDEN:
293 $this->__mods['core']->handler_403($page);
294 break;
295
296 case PL_NOT_FOUND:
297 $this->__mods['core']->handler_404($page);
298 break;
299 }
300
301 $page->assign('platal', $this);
302 $page->run();
303 }
304
305 public function on_subscribe($forlife, $uid, $promo, $pass)
306 {
307 $args = func_get_args();
308 foreach ($this->__mods as $mod) {
309 if (!is_callable($mod, 'on_subscribe'))
310 continue;
311 call_user_func_array(array($mod, 'on_subscribe'), $args);
312 }
313 }
314 }
315
316 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
317 ?>