xhtml validation, and small skin improvements.
[platal.git] / classes / platal.php
CommitLineData
b62f8858 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 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
26class Platal
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
2b1ee50b 37 public function __construct()
b62f8858 38 {
e77c7ea2 39 $modules = func_get_args();
2b1ee50b 40 if (is_array($modules[0])) {
41 $modules = $modules[0];
42 }
27472b85 43 $this->path = trim(Get::_get('n', null), '/');
b62f8858 44
45 $this->__mods = array();
46 $this->__hooks = array();
b62f8858 47
5de0b7e1 48 array_unshift($modules, 'core');
e77c7ea2 49 foreach ($modules as $module) {
a18afbdc 50 $module = strtolower($module);
c807f50d 51 $this->__mods[$module] = $m = PLModule::factory($module);
b62f8858 52 $this->__hooks += $m->handlers();
b62f8858 53 }
fe556813
FB
54
55 global $globals;
56 if ($globals->mode == '') {
57 pl_redirect('index.html');
58 }
b62f8858 59 }
60
2b1ee50b 61 public function pl_self($n = null)
d1ebc57a 62 {
63 if (is_null($n))
64 return $this->path;
65
66 if ($n >= 0)
67 return join('/', array_slice($this->argv, 0, $n + 1));
68
69 if ($n <= -count($this->argv))
70 return $this->argv[0];
71
72 return join('/', array_slice($this->argv, 0, $n));
73 }
74
2b1ee50b 75 protected function find_hook()
b62f8858 76 {
77 $p = $this->path;
78
4a5fb34b 79 while ($p) {
b62f8858 80 if (array_key_exists($p, $this->__hooks))
81 break;
82
83 $p = substr($p, 0, strrpos($p, '/'));
84 }
7c6e0aff 85
b62f8858 86 if (empty($this->__hooks[$p])) {
7c6e0aff 87 return null;
b62f8858 88 }
89
15a094c0 90 $hook = $this->__hooks[$p];
91
92 if (!is_callable($hook['hook'])) {
7c6e0aff 93 return null;
94 }
95
8fc4efa3 96 $this->https = ($hook['type'] & NO_HTTPS) ? false : true;
7c6e0aff 97 $this->argv = explode('/', substr($this->path, strlen($p)));
98 $this->argv[0] = $p;
99
100 return $hook;
101 }
102
2b1ee50b 103 protected function find_nearest_key($key, array &$array)
409de7a7 104 {
0d602b8f 105 $keys = array_keys($array);
409de7a7 106 if (in_array($key, $keys)) {
107 return $key;
108 }
0d602b8f 109
338a5934 110 if (($pos = strpos($key, '.php')) !== false) {
111 $key = substr($key, 0, $pos);
112 }
113
0d602b8f 114 $has_end = in_array("#final#", $keys);
115 if (strlen($key) > 24 && $has_end) {
116 return "#final#";
117 }
118
409de7a7 119 foreach ($keys as $k) {
0d602b8f 120 if ($k == "#final#") {
121 continue;
122 }
951db8e3 123 $lev = levenshtein($key, $k);
a1b4fd8a 124
6d407683
FB
125 if ((!isset($val) || $lev < $val)
126 && ($lev <= strlen($k)/2 || strpos($k, $key) !== false || strpos($key, $k) !== false)) {
951db8e3 127 $val = $lev;
128 $best = $k;
409de7a7 129 }
130 }
0d602b8f 131 if (!isset($best) && $has_end) {
409de7a7 132 return "#final#";
a1b4fd8a 133 } else if (isset($best)) {
951db8e3 134 return $best;
409de7a7 135 }
136 return null;
137 }
138
02838718 139 public function near_hook()
409de7a7 140 {
141 $hooks = array();
6d407683 142 $leafs = array();
409de7a7 143 foreach ($this->__hooks as $hook=>$handler) {
a1b4fd8a 144 if (!$this->check_perms($handler['perms'])) {
0d602b8f 145 continue;
146 }
409de7a7 147 $parts = split('/', $hook);
148 $place =& $hooks;
149 foreach ($parts as $part) {
150 if (!isset($place[$part])) {
151 $place[$part] = array();
152 }
eaf30d86 153 $place =& $place[$part];
409de7a7 154 }
6d407683
FB
155 $leaf = $parts[count($parts)-1];
156 if (!isset($leafs[$leaf])) {
157 $leafs[$leaf] = $hook;
158 } else if (is_array($leafs[$leaf])) {
159 $leafs[$leaf][] = $hook;
160 } else {
161 $leafs[$leaf] = array($hook, $leafs[$leaf]);
162 }
409de7a7 163 $place["#final#"] = array();
164 }
165
6d407683 166 // search for the nearest full path
409de7a7 167 $p = split('/', $this->path);
168 $place =& $hooks;
169 $link = '';
409de7a7 170 foreach ($p as $k) {
0d602b8f 171 if (!isset($ended)) {
409de7a7 172 $key = $this->find_nearest_key($k, $place);
6b8d257b 173 } else {
174 $key = $k;
409de7a7 175 }
6b8d257b 176 if ($key == "#final#") {
6b8d257b 177 if (!array_key_exists($link, $this->__hooks)) {
6d407683
FB
178 $link = '';
179 break;
6b8d257b 180 }
0d602b8f 181 $key = $k;
182 $ended = true;
409de7a7 183 }
184 if (!is_null($key)) {
185 if (!empty($link)) {
186 $link .= '/';
187 }
188 $link .= $key;
189 $place =& $place[$key];
190 } else {
6d407683
FB
191 $link = '';
192 break;
409de7a7 193 }
194 }
6d407683
FB
195 if ($link == $this->path) {
196 $link = '';
197 }
198 if ($link && levenshtein($link, $this->path) < strlen($link)/3) {
338a5934 199 return $link;
200 }
6d407683
FB
201
202 // search for missing namespace (the given name is a leaf)
203 $leaf = array_shift($p);
204 $args = count($p) ? '/' . implode('/', $p) : '';
205 if (isset($leafs[$leaf]) && !is_array($leafs[$leaf]) && $leafs[$leaf] != $this->path) {
206 return $leafs[$leaf] . $args;
207 }
208 unset($val);
209 $best = null;
210 foreach ($leafs as $k=>&$path) {
211 if (is_array($path)) {
212 continue;
213 }
214 $lev = levenshtein($leaf, $k);
215
216 if ((!isset($val) || $lev < $val)
217 && ($lev <= strlen($k)/2 || strpos($k, $leaf) !== false || strpos($leaf, $k) !== false)) {
218 $val = $lev;
219 $best = $path;
220 }
221 }
222 return $best == null ? ( $link ? $link : null ) : $best . $args;
409de7a7 223 }
224
bf517daf 225 protected function check_perms($perms)
226 {
227 if (!$perms) { // No perms, no check
228 return true;
229 }
230 $s_perms = S::v('perms');
aa5836d7 231 return $s_perms->hasFlagCombination($perms);
bf517daf 232 }
233
2b1ee50b 234 private function call_hook(PlatalPage &$page)
7c6e0aff 235 {
236 $hook = $this->find_hook();
409de7a7 237 if (empty($hook)) {
15a094c0 238 return PL_NOT_FOUND;
239 }
8fc4efa3 240 global $globals;
241 if ($this->https && !$_SERVER['HTTPS'] && $globals->core->secure_domain) {
242 http_redirect('https://' . $globals->core->secure_domain . $_SERVER['REQUEST_URI']);
243 }
15a094c0 244
ef42a9d6 245 $args = $this->argv;
246 $args[0] =& $page;
b62f8858 247
cab08090 248 if ($hook['auth'] > S::v('auth', AUTH_PUBLIC)) {
8fc4efa3 249 if ($hook['type'] & DO_AUTH) {
94c63478 250 if (!call_user_func(array($globals->session, 'doAuth'))) {
251 $this->force_login($page);
252 }
253 } else {
254 return PL_FORBIDDEN;
63528107 255 }
b62f8858 256 }
bf517daf 257 if ($hook['auth'] != AUTH_PUBLIC && !$this->check_perms($hook['perms'])) {
5777e7fc 258 return PL_FORBIDDEN;
259 }
260
a0f05027 261 $val = call_user_func_array($hook['hook'], $args);
8fc4efa3 262 if ($val & PL_DO_AUTH) {
a0f05027 263 // The handler need a better auth with the current args
264 if (!call_user_func(array($globals->session, 'doAuth'))) {
265 $this->force_login($page);
266 }
267 $val = call_user_func_array($hook['hook'], $args);
268 }
269 return $val;
b62f8858 270 }
271
862a62fb 272 public function force_login(PlatalPage &$page)
63528107 273 {
c3063cd3 274 header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
aa5f19ae 275 if (S::logged()) {
8b1f8e12 276 $page->changeTpl('core/password_prompt_logged.tpl');
c99ef281 277 $page->addJsLink('do_challenge_response_logged.js');
63528107 278 } else {
8b1f8e12 279 $page->changeTpl('core/password_prompt.tpl');
c99ef281 280 $page->addJsLink('do_challenge_response.js');
1f6a041e 281 }
282 $page->assign('platal', $this);
63528107 283 $page->run();
284 }
285
2b1ee50b 286 public function run()
b62f8858 287 {
288 global $page;
289
8b1f8e12 290 new_skinned_page('platal/index.tpl');
b62f8858 291
292 if (empty($this->path)) {
c9178c75 293 $this->path = 'index';
294 }
295
e979cd2b 296 $page->assign('platal', $this);
b62f8858 297 switch ($this->call_hook($page)) {
298 case PL_FORBIDDEN:
299 $this->__mods['core']->handler_403($page);
300 break;
301
302 case PL_NOT_FOUND:
303 $this->__mods['core']->handler_404($page);
304 break;
305 }
e979cd2b 306
307 $page->assign('platal', $this);
b62f8858 308 $page->run();
309 }
8d8f7607 310
fbc210fa 311 public function on_subscribe($forlife, $uid, $promo, $pass)
8d8f7607 312 {
313 $args = func_get_args();
314 foreach ($this->__mods as $mod) {
315 if (!is_callable($mod, 'on_subscribe'))
316 continue;
317 call_user_func_array(array($mod, 'on_subscribe'), $args);
318 }
319 }
b62f8858 320}
321
a7de4ef7 322// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
b62f8858 323?>