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