| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2007 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_DO_AUTH', 300); |
| 23 | define('PL_FORBIDDEN', 403); |
| 24 | define('PL_NOT_FOUND', 404); |
| 25 | |
| 26 | class Platal |
| 27 | { |
| 28 | private $__mods; |
| 29 | private $__hooks; |
| 30 | |
| 31 | public $ns; |
| 32 | public $path; |
| 33 | public $argv; |
| 34 | |
| 35 | public function __construct() |
| 36 | { |
| 37 | $modules = func_get_args(); |
| 38 | if (is_array($modules[0])) { |
| 39 | $modules = $modules[0]; |
| 40 | } |
| 41 | $this->path = trim(Get::_get('n', null), '/'); |
| 42 | |
| 43 | $this->__mods = array(); |
| 44 | $this->__hooks = array(); |
| 45 | |
| 46 | array_unshift($modules, 'core'); |
| 47 | foreach ($modules as $module) { |
| 48 | $module = strtolower($module); |
| 49 | $this->__mods[$module] = $m = PLModule::factory($module); |
| 50 | $this->__hooks += $m->handlers(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public function pl_self($n = null) |
| 55 | { |
| 56 | if (is_null($n)) |
| 57 | return $this->path; |
| 58 | |
| 59 | if ($n >= 0) |
| 60 | return join('/', array_slice($this->argv, 0, $n + 1)); |
| 61 | |
| 62 | if ($n <= -count($this->argv)) |
| 63 | return $this->argv[0]; |
| 64 | |
| 65 | return join('/', array_slice($this->argv, 0, $n)); |
| 66 | } |
| 67 | |
| 68 | protected function find_hook() |
| 69 | { |
| 70 | $p = $this->path; |
| 71 | |
| 72 | while ($p) { |
| 73 | if (array_key_exists($p, $this->__hooks)) |
| 74 | break; |
| 75 | |
| 76 | $p = substr($p, 0, strrpos($p, '/')); |
| 77 | } |
| 78 | |
| 79 | if (empty($this->__hooks[$p])) { |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | $hook = $this->__hooks[$p]; |
| 84 | |
| 85 | if (!is_callable($hook['hook'])) { |
| 86 | return null; |
| 87 | } |
| 88 | |
| 89 | $this->argv = explode('/', substr($this->path, strlen($p))); |
| 90 | $this->argv[0] = $p; |
| 91 | |
| 92 | return $hook; |
| 93 | } |
| 94 | |
| 95 | protected function find_nearest_key($key, array &$array) |
| 96 | { |
| 97 | $keys = array_keys($array); |
| 98 | if (in_array($key, $keys)) { |
| 99 | return $key; |
| 100 | } |
| 101 | |
| 102 | if (($pos = strpos($key, '.php')) !== false) { |
| 103 | $key = substr($key, 0, $pos); |
| 104 | } |
| 105 | |
| 106 | $has_end = in_array("#final#", $keys); |
| 107 | if (strlen($key) > 24 && $has_end) { |
| 108 | return "#final#"; |
| 109 | } |
| 110 | |
| 111 | foreach ($keys as $k) { |
| 112 | if ($k == "#final#") { |
| 113 | continue; |
| 114 | } |
| 115 | $lev = levenshtein($key, $k); |
| 116 | if ((!isset($val) || $lev < $val) && $lev <= (strlen($k)*2)/3) { |
| 117 | $val = $lev; |
| 118 | $best = $k; |
| 119 | } |
| 120 | } |
| 121 | if (!isset($best) && $has_end) { |
| 122 | return "#final#"; |
| 123 | } else { |
| 124 | return $best; |
| 125 | } |
| 126 | return null; |
| 127 | } |
| 128 | |
| 129 | public function near_hook() |
| 130 | { |
| 131 | $hooks = array(); |
| 132 | foreach ($this->__hooks as $hook=>$handler) { |
| 133 | if (!empty($handler['perms']) && $handler['perms'] != S::v('perms')) { |
| 134 | continue; |
| 135 | } |
| 136 | $parts = split('/', $hook); |
| 137 | $place =& $hooks; |
| 138 | foreach ($parts as $part) { |
| 139 | if (!isset($place[$part])) { |
| 140 | $place[$part] = array(); |
| 141 | } |
| 142 | $place =& $place[$part]; |
| 143 | } |
| 144 | $place["#final#"] = array(); |
| 145 | } |
| 146 | |
| 147 | $p = split('/', $this->path); |
| 148 | $place =& $hooks; |
| 149 | $link = ''; |
| 150 | foreach ($p as $k) { |
| 151 | if (!isset($ended)) { |
| 152 | $key = $this->find_nearest_key($k, $place); |
| 153 | } else { |
| 154 | $key = $k; |
| 155 | } |
| 156 | if ($key == "#final#") { |
| 157 | if (!array_key_exists($link, $this->__hooks)) { |
| 158 | return null; |
| 159 | } |
| 160 | $key = $k; |
| 161 | $ended = true; |
| 162 | } |
| 163 | if (!is_null($key)) { |
| 164 | if (!empty($link)) { |
| 165 | $link .= '/'; |
| 166 | } |
| 167 | $link .= $key; |
| 168 | $place =& $place[$key]; |
| 169 | } else { |
| 170 | return null; |
| 171 | } |
| 172 | } |
| 173 | if ($link != $this->path) { |
| 174 | return $link; |
| 175 | } |
| 176 | return null; |
| 177 | } |
| 178 | |
| 179 | protected function check_perms($perms) |
| 180 | { |
| 181 | if (!$perms) { // No perms, no check |
| 182 | return true; |
| 183 | } |
| 184 | $s_perms = S::v('perms'); |
| 185 | return $s_perms->hasFlagCombination($perms); |
| 186 | } |
| 187 | |
| 188 | private function call_hook(PlatalPage &$page) |
| 189 | { |
| 190 | $hook = $this->find_hook(); |
| 191 | if (empty($hook)) { |
| 192 | return PL_NOT_FOUND; |
| 193 | } |
| 194 | |
| 195 | $args = $this->argv; |
| 196 | $args[0] = &$page; |
| 197 | |
| 198 | if ($hook['auth'] > S::v('auth', AUTH_PUBLIC)) { |
| 199 | if ($hook['type'] == DO_AUTH) { |
| 200 | global $globals; |
| 201 | |
| 202 | if (!call_user_func(array($globals->session, 'doAuth'))) { |
| 203 | $this->force_login($page); |
| 204 | } |
| 205 | } else { |
| 206 | return PL_FORBIDDEN; |
| 207 | } |
| 208 | } |
| 209 | if ($hook['auth'] != AUTH_PUBLIC && !$this->check_perms($hook['perms'])) { |
| 210 | return PL_FORBIDDEN; |
| 211 | } |
| 212 | |
| 213 | $val = call_user_func_array($hook['hook'], $args); |
| 214 | if ($val == PL_DO_AUTH) { |
| 215 | global $globals; |
| 216 | // The handler need a better auth with the current args |
| 217 | if (!call_user_func(array($globals->session, 'doAuth'))) { |
| 218 | $this->force_login($page); |
| 219 | } |
| 220 | $val = call_user_func_array($hook['hook'], $args); |
| 221 | } |
| 222 | return $val; |
| 223 | } |
| 224 | |
| 225 | public function force_login(PlatalPage &$page) |
| 226 | { |
| 227 | if (S::logged()) { |
| 228 | $page->changeTpl('core/password_prompt_logged.tpl'); |
| 229 | $page->addJsLink('do_challenge_response_logged.js'); |
| 230 | } else { |
| 231 | $page->changeTpl('core/password_prompt.tpl'); |
| 232 | $page->addJsLink('do_challenge_response.js'); |
| 233 | } |
| 234 | $page->assign('platal', $this); |
| 235 | $page->run(); |
| 236 | } |
| 237 | |
| 238 | public function run() |
| 239 | { |
| 240 | global $page; |
| 241 | |
| 242 | new_skinned_page('platal/index.tpl'); |
| 243 | |
| 244 | if (empty($this->path)) { |
| 245 | $this->path = 'index'; |
| 246 | } |
| 247 | |
| 248 | $page->assign('platal', $this); |
| 249 | switch ($this->call_hook($page)) { |
| 250 | case PL_FORBIDDEN: |
| 251 | $this->__mods['core']->handler_403($page); |
| 252 | break; |
| 253 | |
| 254 | case PL_NOT_FOUND: |
| 255 | $this->__mods['core']->handler_404($page); |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | $page->assign('platal', $this); |
| 260 | $page->run(); |
| 261 | } |
| 262 | |
| 263 | public function on_subscribe($forlife, $uid, $promo, $pass) |
| 264 | { |
| 265 | $args = func_get_args(); |
| 266 | foreach ($this->__mods as $mod) { |
| 267 | if (!is_callable($mod, 'on_subscribe')) |
| 268 | continue; |
| 269 | call_user_func_array(array($mod, 'on_subscribe'), $args); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 275 | ?> |