Fix issues with logger instantiation.
[platal.git] / classes / platal.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 define('PL_WIKI', 500);
26 define('PL_WIKI_HOOK', '@@WIKI@@');
27
28 abstract class Platal
29 {
30 private $__mods;
31 private $__hooks;
32
33 protected $https;
34
35 public $ns;
36 public $path;
37 public $argv;
38
39 static private $_page = null;
40
41 public function __construct()
42 {
43 global $platal, $session, $globals;
44 $platal = $this;
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();
51 $globals->init();
52
53 /* Get the current session: assign first, then activate the session.
54 */
55 $session = $this->buildSession();
56 if (!$session->startAvailableAuth()) {
57 Platal::page()->trigError("Données d'authentification invalides.");
58 }
59
60 $modules = func_get_args();
61 if (isset($modules[0]) && is_array($modules[0])) {
62 $modules = $modules[0];
63 }
64 $this->path = trim(Get::_get('n', null), '/');
65
66 $this->__mods = array();
67 $this->__hooks = array();
68
69 array_unshift($modules, 'core');
70 foreach ($modules as $module) {
71 $module = strtolower($module);
72 $this->__mods[$module] = $m = PLModule::factory($module);
73 $this->__hooks = $m->handlers() + $this->__hooks;
74 }
75
76 if ($globals->mode == '') {
77 pl_redirect('index.html');
78 }
79 }
80
81 public function pl_self($n = null)
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
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
103 protected function find_hook()
104 {
105 $p = $this->path;
106
107 while ($p) {
108 if (array_key_exists($p, $this->__hooks))
109 break;
110
111 $p = substr($p, 0, strrpos($p, '/'));
112 }
113
114 if (empty($this->__hooks[$p])) {
115 return null;
116 }
117
118 $hook = $this->__hooks[$p];
119
120 if (!is_callable($hook['hook']) && $hook['hook'] != PL_WIKI_HOOK) {
121 return null;
122 }
123
124 $this->https = ($hook['type'] & NO_HTTPS) ? false : true;
125 $this->argv = explode('/', substr($this->path, strlen($p)));
126 $this->argv[0] = $p;
127
128 return $hook;
129 }
130
131 protected function find_nearest_key($key, array &$array)
132 {
133 $keys = array_keys($array);
134 if (in_array($key, $keys)) {
135 return $key;
136 }
137
138 if (($pos = strpos($key, '.php')) !== false) {
139 $key = substr($key, 0, $pos);
140 }
141
142 $has_end = in_array("#final#", $keys);
143 if (strlen($key) > 24 && $has_end) {
144 return "#final#";
145 }
146
147 foreach ($keys as $k) {
148 if ($k == "#final#") {
149 continue;
150 }
151 $lev = levenshtein($key, $k);
152
153 if ((!isset($val) || $lev < $val)
154 && ($lev <= strlen($k)/2 || strpos($k, $key) !== false || strpos($key, $k) !== false)) {
155 $val = $lev;
156 $best = $k;
157 }
158 }
159 if (!isset($best) && $has_end) {
160 return "#final#";
161 } else if (isset($best)) {
162 return $best;
163 }
164 return null;
165 }
166
167 public function near_hook()
168 {
169 $hooks = array();
170 $leafs = array();
171 foreach ($this->__hooks as $hook=>$handler) {
172 if (!$this->check_perms($handler['perms'])) {
173 continue;
174 }
175 $parts = split('/', $hook);
176 $place =& $hooks;
177 foreach ($parts as $part) {
178 if (!isset($place[$part])) {
179 $place[$part] = array();
180 }
181 $place =& $place[$part];
182 }
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 }
191 $place["#final#"] = array();
192 }
193
194 // search for the nearest full path
195 $p = split('/', $this->path);
196 $place =& $hooks;
197 $link = '';
198 foreach ($p as $k) {
199 if (!isset($ended)) {
200 $key = $this->find_nearest_key($k, $place);
201 } else {
202 $key = $k;
203 }
204 if ($key == "#final#") {
205 if (!array_key_exists($link, $this->__hooks)) {
206 $link = '';
207 break;
208 }
209 $key = $k;
210 $ended = true;
211 }
212 if (!is_null($key)) {
213 if (!empty($link)) {
214 $link .= '/';
215 }
216 $link .= $key;
217 $place =& $place[$key];
218 } else {
219 $link = '';
220 break;
221 }
222 }
223 if ($link == $this->path) {
224 $link = '';
225 }
226 if ($link && levenshtein($link, $this->path) < strlen($link)/3) {
227 return $link;
228 }
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;
251 }
252
253 protected function check_perms($perms)
254 {
255 if (!$perms) { // No perms, no check
256 return true;
257 }
258 $s_perms = S::v('perms');
259 return $s_perms->hasFlagCombination($perms);
260 }
261
262 private function call_hook(PlPage &$page)
263 {
264 $hook = $this->find_hook();
265 if (empty($hook)) {
266 return PL_NOT_FOUND;
267 }
268 global $globals, $session;
269 if ($this->https && !@$_SERVER['HTTPS'] && $globals->core->secure_domain) {
270 http_redirect('https://' . $globals->core->secure_domain . $_SERVER['REQUEST_URI']);
271 }
272
273 $args = $this->argv;
274 $args[0] =& $page;
275
276 if ($hook['auth'] > S::v('auth', AUTH_PUBLIC)) {
277 if ($hook['type'] & DO_AUTH) {
278 if (!$session->start($hook['auth'])) {
279 $this->force_login($page);
280 }
281 } else {
282 return PL_FORBIDDEN;
283 }
284 }
285 if ($hook['auth'] != AUTH_PUBLIC && !$this->check_perms($hook['perms'])) {
286 if (self::notAllowed()) {
287 return PL_FORBIDDEN;
288 }
289 }
290
291 if ($hook['hook'] == PL_WIKI_HOOK) {
292 return PL_WIKI;
293 }
294 $val = call_user_func_array($hook['hook'], $args);
295 if ($val == PL_DO_AUTH) {
296 // The handler need a better auth with the current args
297 if (!$session->start($session->loggedLevel())) {
298 $this->force_login($page);
299 }
300 $val = call_user_func_array($hook['hook'], $args);
301 }
302 return $val;
303 }
304
305 /** Show the authentication form.
306 */
307 abstract public function force_login(PlPage& $page);
308
309 public function run()
310 {
311 $page =& self::page();
312
313 if (empty($this->path)) {
314 $this->path = 'index';
315 }
316
317 $page->assign('platal', $this);
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;
326
327 case PL_WIKI:
328 return PL_WIKI;
329 }
330
331 $page->assign('platal', $this);
332 $page->run();
333 }
334
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
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
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
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);
389
390 Platal::page()->kill($userfriendly);
391 }
392 }
393
394 public function &buildLogger($uid, $suid = 0)
395 {
396 if (defined('PL_LOGGER_CLASS')) {
397 $class = PL_LOGGER_CLASS;
398 return new $class($uid, $suid);
399 } else {
400 return PlLogger::dummy($uid, $suid);
401 }
402 }
403
404 protected function &buildPage()
405 {
406 $pageclass = PL_PAGE_CLASS;
407 $page = new $pageclass();
408 return $page;
409 }
410
411 static public function &page()
412 {
413 if (is_null(self::$_page)) {
414 global $platal;
415 self::$_page = $platal->buildPage();
416 }
417 return self::$_page;
418 }
419
420 protected function &buildSession()
421 {
422 $sessionclass = PL_SESSION_CLASS;
423 $session = new $sessionclass();
424 return $session;
425 }
426
427 static public function &session()
428 {
429 global $session;
430 return $session;
431 }
432
433 protected function &buildGlobals()
434 {
435 $globalclass = PL_GLOBALS_CLASS;
436 $globals = new $globalclass();
437 return $globals;
438 }
439
440 static public function &globals()
441 {
442 global $globals;
443 return $globals;
444 }
445 }
446
447 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
448 ?>