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