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