PHP5-ize all base classes...
[platal.git] / classes / platal.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 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 public $ns;
32 public $path;
33 public $argv;
34
35 public function __construct()
36 {
37 $modules = func_get_args();
38 if (is_array($modules[0])) {
39 $modules = $modules[0];
40 }
41 $this->path = trim(Get::_get('n', null), '/');
42
43 $this->__mods = array();
44 $this->__hooks = array();
45
46 array_unshift($modules, 'core');
47 foreach ($modules as $module) {
48 $this->__mods[$module] = $m = PLModule::factory($module);
49 $this->__hooks += $m->handlers();
50 }
51 }
52
53 public function pl_self($n = null)
54 {
55 if (is_null($n))
56 return $this->path;
57
58 if ($n >= 0)
59 return join('/', array_slice($this->argv, 0, $n + 1));
60
61 if ($n <= -count($this->argv))
62 return $this->argv[0];
63
64 return join('/', array_slice($this->argv, 0, $n));
65 }
66
67 protected function find_hook()
68 {
69 $p = $this->path;
70
71 while ($p) {
72 if (array_key_exists($p, $this->__hooks))
73 break;
74
75 $p = substr($p, 0, strrpos($p, '/'));
76 }
77
78 if (empty($this->__hooks[$p])) {
79 return null;
80 }
81
82 $hook = $this->__hooks[$p];
83
84 if (!is_callable($hook['hook'])) {
85 return null;
86 }
87
88 $this->argv = explode('/', substr($this->path, strlen($p)));
89 $this->argv[0] = $p;
90
91 return $hook;
92 }
93
94 protected function find_nearest_key($key, array &$array)
95 {
96 $keys = array_keys($array);
97 if (in_array($key, $keys)) {
98 return $key;
99 }
100
101 if (($pos = strpos($key, '.php')) !== false) {
102 $key = substr($key, 0, $pos);
103 }
104
105 $has_end = in_array("#final#", $keys);
106 if (strlen($key) > 24 && $has_end) {
107 return "#final#";
108 }
109
110 foreach ($keys as $k) {
111 if ($k == "#final#") {
112 continue;
113 }
114 $lev = levenshtein($key, $k);
115 if ((!isset($val) || $lev < $val) && $lev <= (strlen($k)*2)/3) {
116 $val = $lev;
117 $best = $k;
118 }
119 }
120 if (!isset($best) && $has_end) {
121 return "#final#";
122 } else {
123 return $best;
124 }
125 return null;
126 }
127
128 protected function near_hook()
129 {
130 $hooks = array();
131 foreach ($this->__hooks as $hook=>$handler) {
132 if (!empty($handler['perms']) && $handler['perms'] != S::v('perms')) {
133 continue;
134 }
135 $parts = split('/', $hook);
136 $place =& $hooks;
137 foreach ($parts as $part) {
138 if (!isset($place[$part])) {
139 $place[$part] = array();
140 }
141 $place =& $place[$part];
142 }
143 $place["#final#"] = array();
144 }
145
146 $p = split('/', $this->path);
147 $place =& $hooks;
148 $link = '';
149 foreach ($p as $k) {
150 if (!isset($ended)) {
151 $key = $this->find_nearest_key($k, $place);
152 } else {
153 $key = $k;
154 }
155 if ($key == "#final#") {
156 if (!array_key_exists($link, $this->__hooks)) {
157 return null;
158 }
159 $key = $k;
160 $ended = true;
161 }
162 if (!is_null($key)) {
163 if (!empty($link)) {
164 $link .= '/';
165 }
166 $link .= $key;
167 $place =& $place[$key];
168 } else {
169 return null;
170 }
171 }
172 if ($link != $this->path) {
173 return $link;
174 }
175 return null;
176 }
177
178 private function call_hook(PlatalPage &$page)
179 {
180 $hook = $this->find_hook();
181 if (empty($hook)) {
182 return PL_NOT_FOUND;
183 }
184
185 $args = $this->argv;
186 $args[0] = &$page;
187
188 if ($hook['auth'] > S::v('auth', AUTH_PUBLIC)) {
189 if ($hook['type'] == DO_AUTH) {
190 global $globals;
191
192 if (!call_user_func(array($globals->session, 'doAuth'))) {
193 $this->force_login($page);
194 }
195 } else {
196 return PL_FORBIDDEN;
197 }
198 }
199
200 if (!empty($hook['perms']) && $hook['perms'] != S::v('perms')) {
201 return PL_FORBIDDEN;
202 }
203
204 $val = call_user_func_array($hook['hook'], $args);
205 if ($val == PL_DO_AUTH) {
206 global $globals;
207 // The handler need a better auth with the current args
208 if (!call_user_func(array($globals->session, 'doAuth'))) {
209 $this->force_login($page);
210 }
211 $val = call_user_func_array($hook['hook'], $args);
212 }
213 return $val;
214 }
215
216 protected function force_login(PlatalPage &$page)
217 {
218 if (S::logged()) {
219 $page->changeTpl('core/password_prompt_logged.tpl');
220 $page->addJsLink('do_challenge_response_logged.js');
221 } else {
222 $page->changeTpl('core/password_prompt.tpl');
223 $page->addJsLink('do_challenge_response.js');
224 }
225 $page->assign('platal', $this);
226 $page->run();
227 }
228
229 public function run()
230 {
231 global $page;
232
233 new_skinned_page('platal/index.tpl');
234
235 if (empty($this->path)) {
236 $this->path = 'index';
237 }
238
239 $page->assign('platal', $this);
240 switch ($this->call_hook($page)) {
241 case PL_FORBIDDEN:
242 $this->__mods['core']->handler_403($page);
243 break;
244
245 case PL_NOT_FOUND:
246 $this->__mods['core']->handler_404($page);
247 break;
248 }
249
250 $page->assign('platal', $this);
251 $page->run();
252 }
253
254 private function on_subscribe($forlife, $uid, $promo, $pass)
255 {
256 $args = func_get_args();
257 foreach ($this->__mods as $mod) {
258 if (!is_callable($mod, 'on_subscribe'))
259 continue;
260 call_user_func_array(array($mod, 'on_subscribe'), $args);
261 }
262 }
263 }
264
265 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
266 ?>