A module can redefine a previously defined handler.
[platal.git] / classes / platal.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 abstract 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 static private $_page = null;
38
39 public function __construct()
40 {
41 global $platal, $session, $globals;
42 $platal =& $this;
43 $globalclass = PL_GLOBALS_CLASS;
44 $globals = new $globalclass();
45 $globals->init();
46 $sessionclass = PL_SESSION_CLASS;
47 $session = new $sessionclass();
48 if (!$session->startAvailableAuth()) {
49 Platal::page()->trigError('Données d\'authentification invalide.');
50 }
51
52 $modules = func_get_args();
53 if (isset($modules[0]) && is_array($modules[0])) {
54 $modules = $modules[0];
55 }
56 $this->path = trim(Get::_get('n', null), '/');
57
58 $this->__mods = array();
59 $this->__hooks = array();
60
61 array_unshift($modules, 'core');
62 foreach ($modules as $module) {
63 $module = strtolower($module);
64 $this->__mods[$module] = $m = PLModule::factory($module);
65 $this->__hooks = $m->handlers() + $this->__hooks;
66 }
67
68 if ($globals->mode == '') {
69 pl_redirect('index.html');
70 }
71 }
72
73 public function pl_self($n = null)
74 {
75 if (is_null($n))
76 return $this->path;
77
78 if ($n >= 0)
79 return join('/', array_slice($this->argv, 0, $n + 1));
80
81 if ($n <= -count($this->argv))
82 return $this->argv[0];
83
84 return join('/', array_slice($this->argv, 0, $n));
85 }
86
87 protected function find_hook()
88 {
89 $p = $this->path;
90
91 while ($p) {
92 if (array_key_exists($p, $this->__hooks))
93 break;
94
95 $p = substr($p, 0, strrpos($p, '/'));
96 }
97
98 if (empty($this->__hooks[$p])) {
99 return null;
100 }
101
102 $hook = $this->__hooks[$p];
103
104 if (!is_callable($hook['hook'])) {
105 return null;
106 }
107
108 $this->https = ($hook['type'] & NO_HTTPS) ? false : true;
109 $this->argv = explode('/', substr($this->path, strlen($p)));
110 $this->argv[0] = $p;
111
112 return $hook;
113 }
114
115 protected function find_nearest_key($key, array &$array)
116 {
117 $keys = array_keys($array);
118 if (in_array($key, $keys)) {
119 return $key;
120 }
121
122 if (($pos = strpos($key, '.php')) !== false) {
123 $key = substr($key, 0, $pos);
124 }
125
126 $has_end = in_array("#final#", $keys);
127 if (strlen($key) > 24 && $has_end) {
128 return "#final#";
129 }
130
131 foreach ($keys as $k) {
132 if ($k == "#final#") {
133 continue;
134 }
135 $lev = levenshtein($key, $k);
136
137 if ((!isset($val) || $lev < $val)
138 && ($lev <= strlen($k)/2 || strpos($k, $key) !== false || strpos($key, $k) !== false)) {
139 $val = $lev;
140 $best = $k;
141 }
142 }
143 if (!isset($best) && $has_end) {
144 return "#final#";
145 } else if (isset($best)) {
146 return $best;
147 }
148 return null;
149 }
150
151 public function near_hook()
152 {
153 $hooks = array();
154 $leafs = array();
155 foreach ($this->__hooks as $hook=>$handler) {
156 if (!$this->check_perms($handler['perms'])) {
157 continue;
158 }
159 $parts = split('/', $hook);
160 $place =& $hooks;
161 foreach ($parts as $part) {
162 if (!isset($place[$part])) {
163 $place[$part] = array();
164 }
165 $place =& $place[$part];
166 }
167 $leaf = $parts[count($parts)-1];
168 if (!isset($leafs[$leaf])) {
169 $leafs[$leaf] = $hook;
170 } else if (is_array($leafs[$leaf])) {
171 $leafs[$leaf][] = $hook;
172 } else {
173 $leafs[$leaf] = array($hook, $leafs[$leaf]);
174 }
175 $place["#final#"] = array();
176 }
177
178 // search for the nearest full path
179 $p = split('/', $this->path);
180 $place =& $hooks;
181 $link = '';
182 foreach ($p as $k) {
183 if (!isset($ended)) {
184 $key = $this->find_nearest_key($k, $place);
185 } else {
186 $key = $k;
187 }
188 if ($key == "#final#") {
189 if (!array_key_exists($link, $this->__hooks)) {
190 $link = '';
191 break;
192 }
193 $key = $k;
194 $ended = true;
195 }
196 if (!is_null($key)) {
197 if (!empty($link)) {
198 $link .= '/';
199 }
200 $link .= $key;
201 $place =& $place[$key];
202 } else {
203 $link = '';
204 break;
205 }
206 }
207 if ($link == $this->path) {
208 $link = '';
209 }
210 if ($link && levenshtein($link, $this->path) < strlen($link)/3) {
211 return $link;
212 }
213
214 // search for missing namespace (the given name is a leaf)
215 $leaf = array_shift($p);
216 $args = count($p) ? '/' . implode('/', $p) : '';
217 if (isset($leafs[$leaf]) && !is_array($leafs[$leaf]) && $leafs[$leaf] != $this->path) {
218 return $leafs[$leaf] . $args;
219 }
220 unset($val);
221 $best = null;
222 foreach ($leafs as $k=>&$path) {
223 if (is_array($path)) {
224 continue;
225 }
226 $lev = levenshtein($leaf, $k);
227
228 if ((!isset($val) || $lev < $val)
229 && ($lev <= strlen($k)/2 || strpos($k, $leaf) !== false || strpos($leaf, $k) !== false)) {
230 $val = $lev;
231 $best = $path;
232 }
233 }
234 return $best == null ? ( $link ? $link : null ) : $best . $args;
235 }
236
237 protected function check_perms($perms)
238 {
239 if (!$perms) { // No perms, no check
240 return true;
241 }
242 $s_perms = S::v('perms');
243 return $s_perms->hasFlagCombination($perms);
244 }
245
246 private function call_hook(PlPage &$page)
247 {
248 $hook = $this->find_hook();
249 if (empty($hook)) {
250 return PL_NOT_FOUND;
251 }
252 global $globals, $session;
253 if ($this->https && !@$_SERVER['HTTPS'] && $globals->core->secure_domain) {
254 http_redirect('https://' . $globals->core->secure_domain . $_SERVER['REQUEST_URI']);
255 }
256
257 $args = $this->argv;
258 $args[0] =& $page;
259
260 if ($hook['auth'] > S::v('auth', AUTH_PUBLIC)) {
261 if ($hook['type'] & DO_AUTH) {
262 if (!$session->start($hook['auth'])) {
263 $this->force_login($page);
264 }
265 } else {
266 return PL_FORBIDDEN;
267 }
268 }
269 if ($hook['auth'] != AUTH_PUBLIC && !$this->check_perms($hook['perms'])) {
270 if (S::admin()) {
271 $page->trigWarning('Tu accèdes à cette page car tu es administrateur du site.');
272 } else {
273 return PL_FORBIDDEN;
274 }
275 }
276
277 $val = call_user_func_array($hook['hook'], $args);
278 if ($val == PL_DO_AUTH) {
279 // The handler need a better auth with the current args
280 if (!$session->start($session->loggedLevel())) {
281 $this->force_login($page);
282 }
283 $val = call_user_func_array($hook['hook'], $args);
284 }
285 return $val;
286 }
287
288 /** Show the authentication form.
289 */
290 abstract public function force_login(PlPage& $page);
291
292 public function run()
293 {
294 $page =& self::page();
295
296 if (empty($this->path)) {
297 $this->path = 'index';
298 }
299
300 $page->assign('platal', $this);
301 switch ($this->call_hook($page)) {
302 case PL_FORBIDDEN:
303 $this->__mods['core']->handler_403($page);
304 break;
305
306 case PL_NOT_FOUND:
307 $this->__mods['core']->handler_404($page);
308 break;
309 }
310
311 $page->assign('platal', $this);
312 $page->run();
313 }
314
315 public function error403()
316 {
317 $page =& self::page();
318
319 $this->__mods['core']->handler_403($page);
320 $page->assign('platal', $this);
321 $page->run();
322 }
323
324 public function error404()
325 {
326 $page =& self::page();
327
328 $this->__mods['core']->handler_404($page);
329 $page->assign('platal', $this);
330 $page->run();
331 }
332
333 public static function load($modname, $include = null)
334 {
335 global $platal;
336 $modname = strtolower($modname);
337 if (isset($platal->__mods[$modname])) {
338 if (is_null($include)) {
339 return;
340 }
341 $platal->__mods[$modname]->load($include);
342 } else {
343 if (is_null($include)) {
344 require_once PLModule::path($modname) . '.php';
345 } else {
346 require_once PLModule::path($modname) . '/' . $include;
347 }
348 }
349 }
350
351 static public function &page()
352 {
353 global $platal;
354 if (is_null(self::$_page)) {
355 $pageclass = PL_PAGE_CLASS;
356 self::$_page = new $pageclass();
357 }
358 return self::$_page;
359 }
360
361 static public function &session()
362 {
363 global $session;
364 return $session;
365 }
366
367 static public function &globals()
368 {
369 global $globals;
370 return $globals;
371 }
372 }
373
374 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
375 ?>