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