proposal quick fix for rebuilding search_table even if the
[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{
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) {
604d5375 203 global $globals;
a0f05027 204 // The handler need a better auth with the current args
205 if (!call_user_func(array($globals->session, 'doAuth'))) {
206 $this->force_login($page);
207 }
208 $val = call_user_func_array($hook['hook'], $args);
209 }
210 return $val;
b62f8858 211 }
212
63528107 213 function force_login(&$page)
214 {
aa5f19ae 215 if (S::logged()) {
8b1f8e12 216 $page->changeTpl('core/password_prompt_logged.tpl');
c99ef281 217 $page->addJsLink('do_challenge_response_logged.js');
63528107 218 } else {
8b1f8e12 219 $page->changeTpl('core/password_prompt.tpl');
c99ef281 220 $page->addJsLink('do_challenge_response.js');
1f6a041e 221 }
222 $page->assign('platal', $this);
63528107 223 $page->run();
224 }
225
b62f8858 226 function run()
227 {
228 global $page;
229
8b1f8e12 230 new_skinned_page('platal/index.tpl');
b62f8858 231
232 if (empty($this->path)) {
c9178c75 233 $this->path = 'index';
234 }
235
e979cd2b 236 $page->assign('platal', $this);
b62f8858 237 switch ($this->call_hook($page)) {
238 case PL_FORBIDDEN:
239 $this->__mods['core']->handler_403($page);
240 break;
241
242 case PL_NOT_FOUND:
243 $this->__mods['core']->handler_404($page);
244 break;
245 }
e979cd2b 246
247 $page->assign('platal', $this);
b62f8858 248 $page->run();
249 }
8d8f7607 250
251 function on_subscribe($forlife, $uid, $promo, $pass)
252 {
253 $args = func_get_args();
254 foreach ($this->__mods as $mod) {
255 if (!is_callable($mod, 'on_subscribe'))
256 continue;
257 call_user_func_array(array($mod, 'on_subscribe'), $args);
258 }
259 }
b62f8858 260}
261
a7de4ef7 262// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
b62f8858 263?>