Implement token distance with levenshtein
[platal.git] / classes / platal.php
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
22 define('PL_FORBIDDEN', 403);
23 define('PL_NOT_FOUND', 404);
24
25 class Platal
26 {
27 var $__mods;
28 var $__hooks;
29
30 var $ns;
31 var $path;
32 var $argv;
33
34 function Platal()
35 {
36 $modules = func_get_args();
37 $this->path = trim(Get::_get('n', null), '/');
38
39 $this->__mods = array();
40 $this->__hooks = array();
41
42 array_unshift($modules, 'core');
43 foreach ($modules as $module) {
44 $this->__mods[$module] = $m = PLModule::factory($module);
45 $this->__hooks += $m->handlers();
46 }
47 }
48
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
63 function find_hook()
64 {
65 $p = $this->path;
66
67 while ($p) {
68 if (array_key_exists($p, $this->__hooks))
69 break;
70
71 $p = substr($p, 0, strrpos($p, '/'));
72 }
73
74 if (empty($this->__hooks[$p])) {
75 return null;
76 }
77
78 $hook = $this->__hooks[$p];
79
80 if (!is_callable($hook['hook'])) {
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
90 function find_nearest_key($key, &$array)
91 {
92 $keys = array_keys($array);
93 if (in_array($key, $keys)) {
94 return $key;
95 }
96 $val = null;
97 $best = null;
98 foreach ($keys as $k) {
99 $lev = levenshtein($key, $k);
100 if ((is_null($val) || $lev < $val) && $lev < strlen($k)/2) {
101 $val = $lev;
102 $best = $k;
103 }
104 }
105 if (is_null($best) && in_array("#final#", $keys)) {
106 return "#final#";
107 } else {
108 return $best;
109 }
110 return null;
111 }
112
113 function near_hook()
114 {
115 $hooks = array();
116 foreach ($this->__hooks as $hook=>$handler) {
117 $parts = split('/', $hook);
118 $place =& $hooks;
119 foreach ($parts as $part) {
120 if (!isset($place[$part])) {
121 $place[$part] = array();
122 }
123 $place =& $place[$part];
124 }
125 $place["#final#"] = array();
126 }
127
128 $p = split('/', $this->path);
129 $place =& $hooks;
130 $link = '';
131 $ended = false;
132 foreach ($p as $k) {
133 if (!$ended) {
134 $key = $this->find_nearest_key($k, $place);
135 } else {
136 $key = $k;
137 }
138 if ($key == "#final#") {
139 $key = $k;
140 $ended = true;
141 if (!array_key_exists($link, $this->__hooks)) {
142 return null;
143 }
144 }
145 if (!is_null($key)) {
146 if (!empty($link)) {
147 $link .= '/';
148 }
149 $link .= $key;
150 $place =& $place[$key];
151 } else {
152 return null;
153 }
154 }
155 return $link;
156 }
157
158 function call_hook(&$page)
159 {
160 $hook = $this->find_hook();
161 if (empty($hook)) {
162 return PL_NOT_FOUND;
163 }
164
165 $args = $this->argv;
166 $args[0] = &$page;
167
168 if (strlen($hook['perms']) && $hook['perms'] != Session::v('perms')) {
169 return PL_FORBIDDEN;
170 }
171
172 if ($hook['auth'] > S::v('auth', AUTH_PUBLIC)) {
173 if ($hook['type'] == DO_AUTH) {
174 global $globals;
175
176 if (!call_user_func(array($globals->session, 'doAuth'))) {
177 $this->force_login($page);
178 }
179 } else {
180 return PL_FORBIDDEN;
181 }
182 }
183
184 return call_user_func_array($hook['hook'], $args);
185 }
186
187 function force_login(&$page)
188 {
189 if (S::logged()) {
190 $page->changeTpl('password_prompt_logged.tpl');
191 $page->addJsLink('do_challenge_response_logged.js');
192 } else {
193 $page->changeTpl('password_prompt.tpl');
194 $page->addJsLink('do_challenge_response.js');
195 }
196 $page->run();
197 }
198
199 function run()
200 {
201 global $page;
202
203 new_skinned_page('index.tpl');
204
205 if (empty($this->path)) {
206 $this->path = 'index';
207 }
208
209 $page->assign('platal', $this);
210 switch ($this->call_hook($page)) {
211 case PL_FORBIDDEN:
212 $this->__mods['core']->handler_403($page);
213 break;
214
215 case PL_NOT_FOUND:
216 $this->__mods['core']->handler_404($page);
217 break;
218 }
219
220 $page->assign('platal', $this);
221 $page->run();
222 }
223
224 function on_subscribe($forlife, $uid, $promo, $pass)
225 {
226 $args = func_get_args();
227 foreach ($this->__mods as $mod) {
228 if (!is_callable($mod, 'on_subscribe'))
229 continue;
230 call_user_func_array(array($mod, 'on_subscribe'), $args);
231 }
232 }
233 }
234
235 ?>