Add Platal::assert() function
[platal.git] / classes / platal.php
CommitLineData
b62f8858 1<?php
2/***************************************************************************
2ab75571 3 * Copyright (C) 2003-2010 Polytechnique.org *
b62f8858 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
a0f05027 22define('PL_DO_AUTH', 300);
b62f8858 23define('PL_FORBIDDEN', 403);
24define('PL_NOT_FOUND', 404);
25
c158b99a 26abstract class Platal
b62f8858 27{
2b1ee50b 28 private $__mods;
29 private $__hooks;
b62f8858 30
8fc4efa3 31 protected $https;
32
2b1ee50b 33 public $ns;
34 public $path;
35 public $argv;
b62f8858 36
abde67b1
FB
37 static private $_page = null;
38
2b1ee50b 39 public function __construct()
b62f8858 40 {
c0799142 41 global $platal, $session, $globals;
abde67b1
FB
42 $platal =& $this;
43 $globalclass = PL_GLOBALS_CLASS;
44 $globals = new $globalclass();
d95d46a7 45 $globals->init();
abde67b1
FB
46 $sessionclass = PL_SESSION_CLASS;
47 $session = new $sessionclass();
c0799142 48 if (!$session->startAvailableAuth()) {
2d08839a 49 Platal::page()->trigError("Données d'authentification invalides.");
c0799142 50 }
abde67b1 51
e77c7ea2 52 $modules = func_get_args();
5640f093 53 if (isset($modules[0]) && is_array($modules[0])) {
2b1ee50b 54 $modules = $modules[0];
55 }
27472b85 56 $this->path = trim(Get::_get('n', null), '/');
b62f8858 57
58 $this->__mods = array();
59 $this->__hooks = array();
b62f8858 60
5de0b7e1 61 array_unshift($modules, 'core');
e77c7ea2 62 foreach ($modules as $module) {
a18afbdc 63 $module = strtolower($module);
c807f50d 64 $this->__mods[$module] = $m = PLModule::factory($module);
0709dd7d 65 $this->__hooks = $m->handlers() + $this->__hooks;
b62f8858 66 }
fe556813 67
fe556813
FB
68 if ($globals->mode == '') {
69 pl_redirect('index.html');
70 }
b62f8858 71 }
72
2b1ee50b 73 public function pl_self($n = null)
d1ebc57a 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
2b1ee50b 87 protected function find_hook()
b62f8858 88 {
89 $p = $this->path;
90
4a5fb34b 91 while ($p) {
b62f8858 92 if (array_key_exists($p, $this->__hooks))
93 break;
94
95 $p = substr($p, 0, strrpos($p, '/'));
96 }
7c6e0aff 97
b62f8858 98 if (empty($this->__hooks[$p])) {
7c6e0aff 99 return null;
b62f8858 100 }
101
15a094c0 102 $hook = $this->__hooks[$p];
103
104 if (!is_callable($hook['hook'])) {
7c6e0aff 105 return null;
106 }
107
8fc4efa3 108 $this->https = ($hook['type'] & NO_HTTPS) ? false : true;
7c6e0aff 109 $this->argv = explode('/', substr($this->path, strlen($p)));
110 $this->argv[0] = $p;
111
112 return $hook;
113 }
114
2b1ee50b 115 protected function find_nearest_key($key, array &$array)
409de7a7 116 {
0d602b8f 117 $keys = array_keys($array);
409de7a7 118 if (in_array($key, $keys)) {
119 return $key;
120 }
0d602b8f 121
338a5934 122 if (($pos = strpos($key, '.php')) !== false) {
123 $key = substr($key, 0, $pos);
124 }
125
0d602b8f 126 $has_end = in_array("#final#", $keys);
127 if (strlen($key) > 24 && $has_end) {
128 return "#final#";
129 }
130
409de7a7 131 foreach ($keys as $k) {
0d602b8f 132 if ($k == "#final#") {
133 continue;
134 }
951db8e3 135 $lev = levenshtein($key, $k);
a1b4fd8a 136
6d407683
FB
137 if ((!isset($val) || $lev < $val)
138 && ($lev <= strlen($k)/2 || strpos($k, $key) !== false || strpos($key, $k) !== false)) {
951db8e3 139 $val = $lev;
140 $best = $k;
409de7a7 141 }
142 }
0d602b8f 143 if (!isset($best) && $has_end) {
409de7a7 144 return "#final#";
a1b4fd8a 145 } else if (isset($best)) {
951db8e3 146 return $best;
409de7a7 147 }
148 return null;
149 }
150
02838718 151 public function near_hook()
409de7a7 152 {
153 $hooks = array();
6d407683 154 $leafs = array();
409de7a7 155 foreach ($this->__hooks as $hook=>$handler) {
a1b4fd8a 156 if (!$this->check_perms($handler['perms'])) {
0d602b8f 157 continue;
158 }
409de7a7 159 $parts = split('/', $hook);
160 $place =& $hooks;
161 foreach ($parts as $part) {
162 if (!isset($place[$part])) {
163 $place[$part] = array();
164 }
eaf30d86 165 $place =& $place[$part];
409de7a7 166 }
6d407683
FB
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 }
409de7a7 175 $place["#final#"] = array();
176 }
177
6d407683 178 // search for the nearest full path
409de7a7 179 $p = split('/', $this->path);
180 $place =& $hooks;
181 $link = '';
409de7a7 182 foreach ($p as $k) {
0d602b8f 183 if (!isset($ended)) {
409de7a7 184 $key = $this->find_nearest_key($k, $place);
6b8d257b 185 } else {
186 $key = $k;
409de7a7 187 }
6b8d257b 188 if ($key == "#final#") {
6b8d257b 189 if (!array_key_exists($link, $this->__hooks)) {
6d407683
FB
190 $link = '';
191 break;
6b8d257b 192 }
0d602b8f 193 $key = $k;
194 $ended = true;
409de7a7 195 }
196 if (!is_null($key)) {
197 if (!empty($link)) {
198 $link .= '/';
199 }
200 $link .= $key;
201 $place =& $place[$key];
202 } else {
6d407683
FB
203 $link = '';
204 break;
409de7a7 205 }
206 }
6d407683
FB
207 if ($link == $this->path) {
208 $link = '';
209 }
210 if ($link && levenshtein($link, $this->path) < strlen($link)/3) {
338a5934 211 return $link;
212 }
6d407683
FB
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;
409de7a7 235 }
236
bf517daf 237 protected function check_perms($perms)
238 {
239 if (!$perms) { // No perms, no check
240 return true;
241 }
242 $s_perms = S::v('perms');
aa5836d7 243 return $s_perms->hasFlagCombination($perms);
bf517daf 244 }
245
04334c61 246 private function call_hook(PlPage &$page)
7c6e0aff 247 {
248 $hook = $this->find_hook();
409de7a7 249 if (empty($hook)) {
15a094c0 250 return PL_NOT_FOUND;
251 }
abde67b1 252 global $globals, $session;
748b27d2 253 if ($this->https && !@$_SERVER['HTTPS'] && $globals->core->secure_domain) {
8fc4efa3 254 http_redirect('https://' . $globals->core->secure_domain . $_SERVER['REQUEST_URI']);
255 }
15a094c0 256
ef42a9d6 257 $args = $this->argv;
258 $args[0] =& $page;
b62f8858 259
cab08090 260 if ($hook['auth'] > S::v('auth', AUTH_PUBLIC)) {
8fc4efa3 261 if ($hook['type'] & DO_AUTH) {
c0799142 262 if (!$session->start($hook['auth'])) {
94c63478 263 $this->force_login($page);
264 }
265 } else {
266 return PL_FORBIDDEN;
63528107 267 }
b62f8858 268 }
bf517daf 269 if ($hook['auth'] != AUTH_PUBLIC && !$this->check_perms($hook['perms'])) {
179658ec 270 if (self::notAllowed()) {
db3659bb
FB
271 return PL_FORBIDDEN;
272 }
5777e7fc 273 }
274
a0f05027 275 $val = call_user_func_array($hook['hook'], $args);
a96c675c 276 if ($val == PL_DO_AUTH) {
a0f05027 277 // The handler need a better auth with the current args
30b4d214 278 if (!$session->start($session->loggedLevel())) {
a0f05027 279 $this->force_login($page);
280 }
281 $val = call_user_func_array($hook['hook'], $args);
282 }
283 return $val;
b62f8858 284 }
285
c158b99a
FB
286 /** Show the authentication form.
287 */
288 abstract public function force_login(PlPage& $page);
63528107 289
2b1ee50b 290 public function run()
b62f8858 291 {
abde67b1 292 $page =& self::page();
b62f8858 293
294 if (empty($this->path)) {
c9178c75 295 $this->path = 'index';
296 }
297
e979cd2b 298 $page->assign('platal', $this);
b62f8858 299 switch ($this->call_hook($page)) {
300 case PL_FORBIDDEN:
301 $this->__mods['core']->handler_403($page);
302 break;
303
304 case PL_NOT_FOUND:
305 $this->__mods['core']->handler_404($page);
306 break;
307 }
e979cd2b 308
309 $page->assign('platal', $this);
b62f8858 310 $page->run();
311 }
8d8f7607 312
b0a04fb2
FB
313 public function error403()
314 {
315 $page =& self::page();
316
317 $this->__mods['core']->handler_403($page);
318 $page->assign('platal', $this);
319 $page->run();
320 }
321
322 public function error404()
323 {
324 $page =& self::page();
325
326 $this->__mods['core']->handler_404($page);
327 $page->assign('platal', $this);
328 $page->run();
329 }
330
179658ec
FB
331 public static function notAllowed()
332 {
333 if (S::admin()) {
334 self::page()->trigWarning('Tu accèdes à cette page car tu es administrateur du site.');
335 return false;
336 } else {
337 return true;
338 }
339 }
340
ac2f544d
FB
341 public static function load($modname, $include = null)
342 {
343 global $platal;
344 $modname = strtolower($modname);
345 if (isset($platal->__mods[$modname])) {
346 if (is_null($include)) {
347 return;
348 }
349 $platal->__mods[$modname]->load($include);
350 } else {
351 if (is_null($include)) {
352 require_once PLModule::path($modname) . '.php';
353 } else {
354 require_once PLModule::path($modname) . '/' . $include;
355 }
356 }
357 }
358
ec60dba7
RB
359 public static function assert($cond, $error, $userfriendly)
360 {
361 global $globals;
362 if ($cond === false) {
363 header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
364 $file = fopen($globals->spoolroot . '/spool/tmp/assert_erros', 'a');
365 fwrite($file, '<pre>' . pl_entities($error) . '</pre>\n');
366 fclose($file);
367
368 Platal::page()->kill($userfriendly);
369 }
370 }
371
372
abde67b1
FB
373 static public function &page()
374 {
d7610c35 375 global $platal;
abde67b1
FB
376 if (is_null(self::$_page)) {
377 $pageclass = PL_PAGE_CLASS;
4bbff48d 378 self::$_page = new $pageclass();
abde67b1
FB
379 }
380 return self::$_page;
381 }
47fa97fe
FB
382
383 static public function &session()
384 {
385 global $session;
386 return $session;
387 }
388
389 static public function &globals()
390 {
391 global $globals;
392 return $globals;
393 }
b62f8858 394}
395
a7de4ef7 396// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
b62f8858 397?>