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