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