Modify PlSet to allow for custom PlFilter getters
[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);
bfb9093b
FB
25define('PL_WIKI', 500);
26define('PL_WIKI_HOOK', '@@WIKI@@');
b62f8858 27
c158b99a 28abstract class Platal
b62f8858 29{
2b1ee50b 30 private $__mods;
31 private $__hooks;
b62f8858 32
8fc4efa3 33 protected $https;
34
2b1ee50b 35 public $ns;
36 public $path;
37 public $argv;
b62f8858 38
abde67b1
FB
39 static private $_page = null;
40
2b1ee50b 41 public function __construct()
b62f8858 42 {
c0799142 43 global $platal, $session, $globals;
faeb823b 44 $platal = $this;
ca6384fb
FB
45
46 /* Assign globals first, then call init: init must be used for operations
47 * that requires access to the content of $globals (e.g. XDB requires
48 * $globals to be assigned.
49 */
50 $globals = $this->buildGlobals();
d95d46a7 51 $globals->init();
ca6384fb
FB
52
53 /* Get the current session: assign first, then activate the session.
54 */
55 $session = $this->buildSession();
c0799142 56 if (!$session->startAvailableAuth()) {
2d08839a 57 Platal::page()->trigError("Données d'authentification invalides.");
c0799142 58 }
abde67b1 59
e77c7ea2 60 $modules = func_get_args();
5640f093 61 if (isset($modules[0]) && is_array($modules[0])) {
2b1ee50b 62 $modules = $modules[0];
63 }
27472b85 64 $this->path = trim(Get::_get('n', null), '/');
b62f8858 65
66 $this->__mods = array();
67 $this->__hooks = array();
b62f8858 68
5de0b7e1 69 array_unshift($modules, 'core');
e77c7ea2 70 foreach ($modules as $module) {
a18afbdc 71 $module = strtolower($module);
c807f50d 72 $this->__mods[$module] = $m = PLModule::factory($module);
0709dd7d 73 $this->__hooks = $m->handlers() + $this->__hooks;
b62f8858 74 }
fe556813 75
fe556813
FB
76 if ($globals->mode == '') {
77 pl_redirect('index.html');
78 }
b62f8858 79 }
80
2b1ee50b 81 public function pl_self($n = null)
d1ebc57a 82 {
83 if (is_null($n))
84 return $this->path;
85
86 if ($n >= 0)
87 return join('/', array_slice($this->argv, 0, $n + 1));
88
89 if ($n <= -count($this->argv))
90 return $this->argv[0];
91
92 return join('/', array_slice($this->argv, 0, $n));
93 }
94
bfb9093b
FB
95 public static function wiki_hook($auth = AUTH_PUBLIC, $perms = 'user', $type = DO_AUTH)
96 {
97 return array('hook' => PL_WIKI_HOOK,
98 'auth' => $auth,
99 'perms' => $perms,
100 'type' => $type);
101 }
102
2b1ee50b 103 protected function find_hook()
b62f8858 104 {
105 $p = $this->path;
106
4a5fb34b 107 while ($p) {
b62f8858 108 if (array_key_exists($p, $this->__hooks))
109 break;
110
111 $p = substr($p, 0, strrpos($p, '/'));
112 }
7c6e0aff 113
b62f8858 114 if (empty($this->__hooks[$p])) {
7c6e0aff 115 return null;
b62f8858 116 }
117
15a094c0 118 $hook = $this->__hooks[$p];
119
fd669a57 120 if (!is_callable($hook['hook']) && $hook['hook'] != PL_WIKI_HOOK) {
7c6e0aff 121 return null;
122 }
123
8fc4efa3 124 $this->https = ($hook['type'] & NO_HTTPS) ? false : true;
7c6e0aff 125 $this->argv = explode('/', substr($this->path, strlen($p)));
126 $this->argv[0] = $p;
127
128 return $hook;
129 }
130
2b1ee50b 131 protected function find_nearest_key($key, array &$array)
409de7a7 132 {
0d602b8f 133 $keys = array_keys($array);
409de7a7 134 if (in_array($key, $keys)) {
135 return $key;
136 }
0d602b8f 137
338a5934 138 if (($pos = strpos($key, '.php')) !== false) {
139 $key = substr($key, 0, $pos);
140 }
141
0d602b8f 142 $has_end = in_array("#final#", $keys);
143 if (strlen($key) > 24 && $has_end) {
144 return "#final#";
145 }
146
409de7a7 147 foreach ($keys as $k) {
0d602b8f 148 if ($k == "#final#") {
149 continue;
150 }
951db8e3 151 $lev = levenshtein($key, $k);
a1b4fd8a 152
6d407683
FB
153 if ((!isset($val) || $lev < $val)
154 && ($lev <= strlen($k)/2 || strpos($k, $key) !== false || strpos($key, $k) !== false)) {
951db8e3 155 $val = $lev;
156 $best = $k;
409de7a7 157 }
158 }
0d602b8f 159 if (!isset($best) && $has_end) {
409de7a7 160 return "#final#";
a1b4fd8a 161 } else if (isset($best)) {
951db8e3 162 return $best;
409de7a7 163 }
164 return null;
165 }
166
02838718 167 public function near_hook()
409de7a7 168 {
169 $hooks = array();
6d407683 170 $leafs = array();
409de7a7 171 foreach ($this->__hooks as $hook=>$handler) {
a1b4fd8a 172 if (!$this->check_perms($handler['perms'])) {
0d602b8f 173 continue;
174 }
409de7a7 175 $parts = split('/', $hook);
176 $place =& $hooks;
177 foreach ($parts as $part) {
178 if (!isset($place[$part])) {
179 $place[$part] = array();
180 }
eaf30d86 181 $place =& $place[$part];
409de7a7 182 }
6d407683
FB
183 $leaf = $parts[count($parts)-1];
184 if (!isset($leafs[$leaf])) {
185 $leafs[$leaf] = $hook;
186 } else if (is_array($leafs[$leaf])) {
187 $leafs[$leaf][] = $hook;
188 } else {
189 $leafs[$leaf] = array($hook, $leafs[$leaf]);
190 }
409de7a7 191 $place["#final#"] = array();
192 }
193
6d407683 194 // search for the nearest full path
409de7a7 195 $p = split('/', $this->path);
196 $place =& $hooks;
197 $link = '';
409de7a7 198 foreach ($p as $k) {
0d602b8f 199 if (!isset($ended)) {
409de7a7 200 $key = $this->find_nearest_key($k, $place);
6b8d257b 201 } else {
202 $key = $k;
409de7a7 203 }
6b8d257b 204 if ($key == "#final#") {
6b8d257b 205 if (!array_key_exists($link, $this->__hooks)) {
6d407683
FB
206 $link = '';
207 break;
6b8d257b 208 }
0d602b8f 209 $key = $k;
210 $ended = true;
409de7a7 211 }
212 if (!is_null($key)) {
213 if (!empty($link)) {
214 $link .= '/';
215 }
216 $link .= $key;
217 $place =& $place[$key];
218 } else {
6d407683
FB
219 $link = '';
220 break;
409de7a7 221 }
222 }
6d407683
FB
223 if ($link == $this->path) {
224 $link = '';
225 }
226 if ($link && levenshtein($link, $this->path) < strlen($link)/3) {
338a5934 227 return $link;
228 }
6d407683
FB
229
230 // search for missing namespace (the given name is a leaf)
231 $leaf = array_shift($p);
232 $args = count($p) ? '/' . implode('/', $p) : '';
233 if (isset($leafs[$leaf]) && !is_array($leafs[$leaf]) && $leafs[$leaf] != $this->path) {
234 return $leafs[$leaf] . $args;
235 }
236 unset($val);
237 $best = null;
238 foreach ($leafs as $k=>&$path) {
239 if (is_array($path)) {
240 continue;
241 }
242 $lev = levenshtein($leaf, $k);
243
244 if ((!isset($val) || $lev < $val)
245 && ($lev <= strlen($k)/2 || strpos($k, $leaf) !== false || strpos($leaf, $k) !== false)) {
246 $val = $lev;
247 $best = $path;
248 }
249 }
250 return $best == null ? ( $link ? $link : null ) : $best . $args;
409de7a7 251 }
252
bf517daf 253 protected function check_perms($perms)
254 {
255 if (!$perms) { // No perms, no check
256 return true;
257 }
258 $s_perms = S::v('perms');
aa5836d7 259 return $s_perms->hasFlagCombination($perms);
bf517daf 260 }
261
04334c61 262 private function call_hook(PlPage &$page)
7c6e0aff 263 {
264 $hook = $this->find_hook();
409de7a7 265 if (empty($hook)) {
15a094c0 266 return PL_NOT_FOUND;
267 }
abde67b1 268 global $globals, $session;
748b27d2 269 if ($this->https && !@$_SERVER['HTTPS'] && $globals->core->secure_domain) {
8fc4efa3 270 http_redirect('https://' . $globals->core->secure_domain . $_SERVER['REQUEST_URI']);
271 }
15a094c0 272
ef42a9d6 273 $args = $this->argv;
274 $args[0] =& $page;
b62f8858 275
cab08090 276 if ($hook['auth'] > S::v('auth', AUTH_PUBLIC)) {
8fc4efa3 277 if ($hook['type'] & DO_AUTH) {
c0799142 278 if (!$session->start($hook['auth'])) {
94c63478 279 $this->force_login($page);
280 }
281 } else {
282 return PL_FORBIDDEN;
63528107 283 }
b62f8858 284 }
bf517daf 285 if ($hook['auth'] != AUTH_PUBLIC && !$this->check_perms($hook['perms'])) {
179658ec 286 if (self::notAllowed()) {
db3659bb
FB
287 return PL_FORBIDDEN;
288 }
5777e7fc 289 }
290
bfb9093b
FB
291 if ($hook['hook'] == PL_WIKI_HOOK) {
292 return PL_WIKI;
293 }
a0f05027 294 $val = call_user_func_array($hook['hook'], $args);
a96c675c 295 if ($val == PL_DO_AUTH) {
a0f05027 296 // The handler need a better auth with the current args
30b4d214 297 if (!$session->start($session->loggedLevel())) {
a0f05027 298 $this->force_login($page);
299 }
300 $val = call_user_func_array($hook['hook'], $args);
301 }
302 return $val;
b62f8858 303 }
304
c158b99a
FB
305 /** Show the authentication form.
306 */
307 abstract public function force_login(PlPage& $page);
63528107 308
2b1ee50b 309 public function run()
b62f8858 310 {
abde67b1 311 $page =& self::page();
b62f8858 312
313 if (empty($this->path)) {
c9178c75 314 $this->path = 'index';
315 }
316
e979cd2b 317 $page->assign('platal', $this);
b62f8858 318 switch ($this->call_hook($page)) {
319 case PL_FORBIDDEN:
320 $this->__mods['core']->handler_403($page);
321 break;
322
323 case PL_NOT_FOUND:
324 $this->__mods['core']->handler_404($page);
325 break;
bfb9093b
FB
326
327 case PL_WIKI:
328 return PL_WIKI;
b62f8858 329 }
e979cd2b 330
331 $page->assign('platal', $this);
b62f8858 332 $page->run();
333 }
8d8f7607 334
b0a04fb2
FB
335 public function error403()
336 {
337 $page =& self::page();
338
339 $this->__mods['core']->handler_403($page);
340 $page->assign('platal', $this);
341 $page->run();
342 }
343
344 public function error404()
345 {
346 $page =& self::page();
347
348 $this->__mods['core']->handler_404($page);
349 $page->assign('platal', $this);
350 $page->run();
351 }
352
179658ec
FB
353 public static function notAllowed()
354 {
355 if (S::admin()) {
356 self::page()->trigWarning('Tu accèdes à cette page car tu es administrateur du site.');
357 return false;
358 } else {
359 return true;
360 }
361 }
362
ac2f544d
FB
363 public static function load($modname, $include = null)
364 {
365 global $platal;
366 $modname = strtolower($modname);
367 if (isset($platal->__mods[$modname])) {
368 if (is_null($include)) {
369 return;
370 }
371 $platal->__mods[$modname]->load($include);
372 } else {
373 if (is_null($include)) {
374 require_once PLModule::path($modname) . '.php';
375 } else {
376 require_once PLModule::path($modname) . '/' . $include;
377 }
378 }
379 }
380
ca6384fb
FB
381 public static function assert($cond, $error, $userfriendly)
382 {
383 global $globals;
384 if ($cond === false) {
385 header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
386 $file = fopen($globals->spoolroot . '/spool/tmp/assert_erros', 'a');
387 fwrite($file, '<pre>' . pl_entities($error) . '</pre>\n');
388 fclose($file);
ec60dba7 389
ca6384fb
FB
390 Platal::page()->kill($userfriendly);
391 }
392 }
393
faeb823b 394 public function &buildLogger($uid, $suid = 0)
ca6384fb 395 {
c7608455 396 if (defined('PL_LOGGER_CLASS')) {
ca6384fb
FB
397 $class = PL_LOGGER_CLASS;
398 return new $class($uid, $suid);
399 } else {
faeb823b 400 return PlLogger::dummy($uid, $suid);
ca6384fb
FB
401 }
402 }
ec60dba7 403
ca6384fb
FB
404 protected function &buildPage()
405 {
406 $pageclass = PL_PAGE_CLASS;
407 $page = new $pageclass();
408 return $page;
409 }
ec60dba7 410
abde67b1
FB
411 static public function &page()
412 {
abde67b1 413 if (is_null(self::$_page)) {
ca6384fb
FB
414 global $platal;
415 self::$_page = $platal->buildPage();
abde67b1
FB
416 }
417 return self::$_page;
418 }
47fa97fe 419
ca6384fb
FB
420 protected function &buildSession()
421 {
422 $sessionclass = PL_SESSION_CLASS;
423 $session = new $sessionclass();
424 return $session;
425 }
426
47fa97fe
FB
427 static public function &session()
428 {
429 global $session;
430 return $session;
431 }
432
ca6384fb
FB
433 protected function &buildGlobals()
434 {
435 $globalclass = PL_GLOBALS_CLASS;
436 $globals = new $globalclass();
437 return $globals;
438 }
439
47fa97fe
FB
440 static public function &globals()
441 {
442 global $globals;
443 return $globals;
444 }
b62f8858 445}
446
a7de4ef7 447// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
b62f8858 448?>