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