Fix the valid URL guess on 404
[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) && $lev <= (strlen($k)*2)/3) {
121 $val = $lev;
122 $best = $k;
123 }
124 }
125 if (!isset($best) && $has_end) {
126 return "#final#";
127 } else if (isset($best)) {
128 return $best;
129 }
130 return null;
131 }
132
133 public function near_hook()
134 {
135 $hooks = array();
136 foreach ($this->__hooks as $hook=>$handler) {
137 if (!$this->check_perms($handler['perms'])) {
138 continue;
139 }
140 $parts = split('/', $hook);
141 $place =& $hooks;
142 foreach ($parts as $part) {
143 if (!isset($place[$part])) {
144 $place[$part] = array();
145 }
146 $place =& $place[$part];
147 }
148 $place["#final#"] = array();
149 }
150
151 $p = split('/', $this->path);
152 $place =& $hooks;
153 $link = '';
154 foreach ($p as $k) {
155 if (!isset($ended)) {
156 $key = $this->find_nearest_key($k, $place);
157 } else {
158 $key = $k;
159 }
160 if ($key == "#final#") {
161 if (!array_key_exists($link, $this->__hooks)) {
162 return null;
163 }
164 $key = $k;
165 $ended = true;
166 }
167 if (!is_null($key)) {
168 if (!empty($link)) {
169 $link .= '/';
170 }
171 $link .= $key;
172 $place =& $place[$key];
173 } else {
174 return null;
175 }
176 }
177 if ($link != $this->path) {
178 return $link;
179 }
180 return null;
181 }
182
183 protected function check_perms($perms)
184 {
185 if (!$perms) { // No perms, no check
186 return true;
187 }
188 $s_perms = S::v('perms');
189 return $s_perms->hasFlagCombination($perms);
190 }
191
192 private function call_hook(PlatalPage &$page)
193 {
194 $hook = $this->find_hook();
195 if (empty($hook)) {
196 return PL_NOT_FOUND;
197 }
198 global $globals;
199 if ($this->https && !$_SERVER['HTTPS'] && $globals->core->secure_domain) {
200 http_redirect('https://' . $globals->core->secure_domain . $_SERVER['REQUEST_URI']);
201 }
202
203 $args = $this->argv;
204 $args[0] =& $page;
205
206 if ($hook['auth'] > S::v('auth', AUTH_PUBLIC)) {
207 if ($hook['type'] & DO_AUTH) {
208 if (!call_user_func(array($globals->session, 'doAuth'))) {
209 $this->force_login($page);
210 }
211 } else {
212 return PL_FORBIDDEN;
213 }
214 }
215 if ($hook['auth'] != AUTH_PUBLIC && !$this->check_perms($hook['perms'])) {
216 return PL_FORBIDDEN;
217 }
218
219 $val = call_user_func_array($hook['hook'], $args);
220 if ($val & PL_DO_AUTH) {
221 // The handler need a better auth with the current args
222 if (!call_user_func(array($globals->session, 'doAuth'))) {
223 $this->force_login($page);
224 }
225 $val = call_user_func_array($hook['hook'], $args);
226 }
227 return $val;
228 }
229
230 public function force_login(PlatalPage &$page)
231 {
232 if (S::logged()) {
233 $page->changeTpl('core/password_prompt_logged.tpl');
234 $page->addJsLink('do_challenge_response_logged.js');
235 } else {
236 $page->changeTpl('core/password_prompt.tpl');
237 $page->addJsLink('do_challenge_response.js');
238 }
239 $page->assign('platal', $this);
240 $page->run();
241 }
242
243 public function run()
244 {
245 global $page;
246
247 new_skinned_page('platal/index.tpl');
248
249 if (empty($this->path)) {
250 $this->path = 'index';
251 }
252
253 $page->assign('platal', $this);
254 switch ($this->call_hook($page)) {
255 case PL_FORBIDDEN:
256 $this->__mods['core']->handler_403($page);
257 break;
258
259 case PL_NOT_FOUND:
260 $this->__mods['core']->handler_404($page);
261 break;
262 }
263
264 $page->assign('platal', $this);
265 $page->run();
266 }
267
268 public function on_subscribe($forlife, $uid, $promo, $pass)
269 {
270 $args = func_get_args();
271 foreach ($this->__mods as $mod) {
272 if (!is_callable($mod, 'on_subscribe'))
273 continue;
274 call_user_func_array(array($mod, 'on_subscribe'), $args);
275 }
276 }
277 }
278
279 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
280 ?>