| 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 | var $__mods; |
| 29 | var $__hooks; |
| 30 | |
| 31 | var $ns; |
| 32 | var $path; |
| 33 | var $argv; |
| 34 | |
| 35 | function Platal() |
| 36 | { |
| 37 | $modules = func_get_args(); |
| 38 | $this->path = trim(Get::_get('n', null), '/'); |
| 39 | |
| 40 | $this->__mods = array(); |
| 41 | $this->__hooks = array(); |
| 42 | |
| 43 | array_unshift($modules, 'core'); |
| 44 | foreach ($modules as $module) { |
| 45 | $this->__mods[$module] = $m = PLModule::factory($module); |
| 46 | $this->__hooks += $m->handlers(); |
| 47 | } |
| 48 | } |
| 49 | |
| 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 | |
| 64 | function find_hook() |
| 65 | { |
| 66 | $p = $this->path; |
| 67 | |
| 68 | while ($p) { |
| 69 | if (array_key_exists($p, $this->__hooks)) |
| 70 | break; |
| 71 | |
| 72 | $p = substr($p, 0, strrpos($p, '/')); |
| 73 | } |
| 74 | |
| 75 | if (empty($this->__hooks[$p])) { |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | $hook = $this->__hooks[$p]; |
| 80 | |
| 81 | if (!is_callable($hook['hook'])) { |
| 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 | |
| 91 | function find_nearest_key($key, &$array) |
| 92 | { |
| 93 | $keys = array_keys($array); |
| 94 | if (in_array($key, $keys)) { |
| 95 | return $key; |
| 96 | } |
| 97 | |
| 98 | if (($pos = strpos($key, '.php')) !== false) { |
| 99 | $key = substr($key, 0, $pos); |
| 100 | } |
| 101 | |
| 102 | $has_end = in_array("#final#", $keys); |
| 103 | if (strlen($key) > 24 && $has_end) { |
| 104 | return "#final#"; |
| 105 | } |
| 106 | |
| 107 | foreach ($keys as $k) { |
| 108 | if ($k == "#final#") { |
| 109 | continue; |
| 110 | } |
| 111 | $lev = levenshtein($key, $k); |
| 112 | if ((!isset($val) || $lev < $val) && $lev <= (strlen($k)*2)/3) { |
| 113 | $val = $lev; |
| 114 | $best = $k; |
| 115 | } |
| 116 | } |
| 117 | if (!isset($best) && $has_end) { |
| 118 | return "#final#"; |
| 119 | } else { |
| 120 | return $best; |
| 121 | } |
| 122 | return null; |
| 123 | } |
| 124 | |
| 125 | function near_hook() |
| 126 | { |
| 127 | $hooks = array(); |
| 128 | foreach ($this->__hooks as $hook=>$handler) { |
| 129 | if (!empty($handler['perms']) && $handler['perms'] != S::v('perms')) { |
| 130 | continue; |
| 131 | } |
| 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 = ''; |
| 146 | foreach ($p as $k) { |
| 147 | if (!isset($ended)) { |
| 148 | $key = $this->find_nearest_key($k, $place); |
| 149 | } else { |
| 150 | $key = $k; |
| 151 | } |
| 152 | if ($key == "#final#") { |
| 153 | if (!array_key_exists($link, $this->__hooks)) { |
| 154 | return null; |
| 155 | } |
| 156 | $key = $k; |
| 157 | $ended = true; |
| 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 | } |
| 169 | if ($link != $this->path) { |
| 170 | return $link; |
| 171 | } |
| 172 | return null; |
| 173 | } |
| 174 | |
| 175 | function call_hook(&$page) |
| 176 | { |
| 177 | $hook = $this->find_hook(); |
| 178 | if (empty($hook)) { |
| 179 | return PL_NOT_FOUND; |
| 180 | } |
| 181 | |
| 182 | $args = $this->argv; |
| 183 | $args[0] = &$page; |
| 184 | |
| 185 | if ($hook['auth'] > S::v('auth', AUTH_PUBLIC)) { |
| 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; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if (!empty($hook['perms']) && $hook['perms'] != S::v('perms')) { |
| 198 | return PL_FORBIDDEN; |
| 199 | } |
| 200 | |
| 201 | $val = call_user_func_array($hook['hook'], $args); |
| 202 | if ($val == PL_DO_AUTH) { |
| 203 | global $globals; |
| 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; |
| 211 | } |
| 212 | |
| 213 | function force_login(&$page) |
| 214 | { |
| 215 | if (S::logged()) { |
| 216 | $page->changeTpl('core/password_prompt_logged.tpl'); |
| 217 | $page->addJsLink('do_challenge_response_logged.js'); |
| 218 | } else { |
| 219 | $page->changeTpl('core/password_prompt.tpl'); |
| 220 | $page->addJsLink('do_challenge_response.js'); |
| 221 | } |
| 222 | $page->assign('platal', $this); |
| 223 | $page->run(); |
| 224 | } |
| 225 | |
| 226 | function run() |
| 227 | { |
| 228 | global $page; |
| 229 | |
| 230 | new_skinned_page('platal/index.tpl'); |
| 231 | |
| 232 | if (empty($this->path)) { |
| 233 | $this->path = 'index'; |
| 234 | } |
| 235 | |
| 236 | $page->assign('platal', $this); |
| 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 | } |
| 246 | |
| 247 | $page->assign('platal', $this); |
| 248 | $page->run(); |
| 249 | } |
| 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 | } |
| 260 | } |
| 261 | |
| 262 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 263 | ?> |