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