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 | { |
92 | $keys = array_keys($array); |
93 | if (in_array($key, $keys)) { |
94 | return $key; |
95 | } |
96 | foreach ($keys as $k) { |
97 | if (strpos($key, $k) !== false || strpos($k, $key) !== false) { |
98 | return $k; |
99 | } |
100 | } |
101 | if (in_array("#final#", $keys)) { |
102 | return "#final#"; |
103 | } |
104 | return null; |
105 | } |
106 | |
6b8d257b |
107 | function near_hook() |
409de7a7 |
108 | { |
109 | $hooks = array(); |
110 | foreach ($this->__hooks as $hook=>$handler) { |
111 | $parts = split('/', $hook); |
112 | $place =& $hooks; |
113 | foreach ($parts as $part) { |
114 | if (!isset($place[$part])) { |
115 | $place[$part] = array(); |
116 | } |
117 | $place =& $place[$part]; |
118 | } |
119 | $place["#final#"] = array(); |
120 | } |
121 | |
122 | $p = split('/', $this->path); |
123 | $place =& $hooks; |
124 | $link = ''; |
125 | $ended = false; |
126 | foreach ($p as $k) { |
127 | if (!$ended) { |
128 | $key = $this->find_nearest_key($k, $place); |
6b8d257b |
129 | } else { |
130 | $key = $k; |
409de7a7 |
131 | } |
6b8d257b |
132 | if ($key == "#final#") { |
409de7a7 |
133 | $key = $k; |
134 | $ended = true; |
6b8d257b |
135 | if (!array_key_exists($link, $this->__hooks)) { |
136 | return null; |
137 | } |
409de7a7 |
138 | } |
139 | if (!is_null($key)) { |
140 | if (!empty($link)) { |
141 | $link .= '/'; |
142 | } |
143 | $link .= $key; |
144 | $place =& $place[$key]; |
145 | } else { |
146 | return null; |
147 | } |
148 | } |
149 | return $link; |
150 | } |
151 | |
7c6e0aff |
152 | function call_hook(&$page) |
153 | { |
154 | $hook = $this->find_hook(); |
409de7a7 |
155 | if (empty($hook)) { |
15a094c0 |
156 | return PL_NOT_FOUND; |
157 | } |
158 | |
7c6e0aff |
159 | $args = $this->argv; |
160 | $args[0] = &$page; |
b62f8858 |
161 | |
14224ff0 |
162 | if (strlen($hook['perms']) && $hook['perms'] != Session::v('perms')) { |
163 | return PL_FORBIDDEN; |
164 | } |
165 | |
cab08090 |
166 | if ($hook['auth'] > S::v('auth', AUTH_PUBLIC)) { |
94c63478 |
167 | if ($hook['type'] == DO_AUTH) { |
168 | global $globals; |
169 | |
170 | if (!call_user_func(array($globals->session, 'doAuth'))) { |
171 | $this->force_login($page); |
172 | } |
173 | } else { |
174 | return PL_FORBIDDEN; |
63528107 |
175 | } |
b62f8858 |
176 | } |
7c6e0aff |
177 | |
b62f8858 |
178 | return call_user_func_array($hook['hook'], $args); |
179 | } |
180 | |
63528107 |
181 | function force_login(&$page) |
182 | { |
aa5f19ae |
183 | if (S::logged()) { |
63528107 |
184 | $page->changeTpl('password_prompt_logged.tpl'); |
c99ef281 |
185 | $page->addJsLink('do_challenge_response_logged.js'); |
63528107 |
186 | } else { |
187 | $page->changeTpl('password_prompt.tpl'); |
c99ef281 |
188 | $page->addJsLink('do_challenge_response.js'); |
63528107 |
189 | } |
190 | $page->run(); |
191 | } |
192 | |
b62f8858 |
193 | function run() |
194 | { |
195 | global $page; |
196 | |
801fcad8 |
197 | new_skinned_page('index.tpl'); |
b62f8858 |
198 | |
199 | if (empty($this->path)) { |
c9178c75 |
200 | $this->path = 'index'; |
201 | } |
202 | |
e979cd2b |
203 | $page->assign('platal', $this); |
b62f8858 |
204 | switch ($this->call_hook($page)) { |
205 | case PL_FORBIDDEN: |
206 | $this->__mods['core']->handler_403($page); |
207 | break; |
208 | |
209 | case PL_NOT_FOUND: |
210 | $this->__mods['core']->handler_404($page); |
211 | break; |
212 | } |
e979cd2b |
213 | |
214 | $page->assign('platal', $this); |
b62f8858 |
215 | $page->run(); |
216 | } |
8d8f7607 |
217 | |
218 | function on_subscribe($forlife, $uid, $promo, $pass) |
219 | { |
220 | $args = func_get_args(); |
221 | foreach ($this->__mods as $mod) { |
222 | if (!is_callable($mod, 'on_subscribe')) |
223 | continue; |
224 | call_user_func_array(array($mod, 'on_subscribe'), $args); |
225 | } |
226 | } |
b62f8858 |
227 | } |
228 | |
229 | ?> |