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 | |
22 | define('PL_OK', 0); |
23 | define('PL_NEEDLOGIN', 1); |
24 | define('PL_FORBIDDEN', 403); |
25 | define('PL_NOT_FOUND', 404); |
26 | |
27 | class Platal |
28 | { |
29 | var $__mods; |
30 | var $__hooks; |
31 | |
32 | var $path; |
33 | var $menu; |
34 | var $auth; |
35 | |
36 | function Platal() |
37 | { |
38 | $this->path = Get::_get('p', null); |
39 | |
40 | $this->__mods = array(); |
41 | $this->__hooks = array(); |
42 | $this->menu = array(); |
43 | |
44 | foreach (glob(dirname(__FILE__).'/../modules/*.php') as $module) { |
45 | $module = basename($module, '.php'); |
46 | $m =& PLModule::factory($this, $module); |
47 | $this->__mods[$module] =& $m; |
48 | $this->__hooks += $m->handlers(); |
49 | $this->menu = array_merge($this->menu, $m->menu_entries()); |
50 | } |
51 | |
52 | krsort($this->__hooks); |
53 | usort($this->menu, array($this, '_menu_cmp')); |
54 | } |
55 | |
56 | function load_class($cls) |
57 | { |
58 | require_once dirname(__FILE__).'/../classes/'.$cls.'.php'; |
59 | } |
60 | |
61 | function _menu_cmp($a, $b) |
62 | { |
63 | return strcasecmp($a['path'], $b['path']); |
64 | } |
65 | |
66 | function call_hook(&$page) |
67 | { |
68 | $p = $this->path; |
69 | |
70 | while ($path) { |
71 | if (array_key_exists($p, $this->__hooks)) |
72 | break; |
73 | |
74 | $p = substr($p, 0, strrpos($p, '/')); |
75 | } |
76 | if (empty($this->__hooks[$p])) { |
77 | return PL_NOT_FOUND; |
78 | } |
79 | |
80 | $args = array_merge(array(&$page), |
81 | explode('/', substr($this->path, strlen($p) + 1))); |
82 | |
83 | $hook = $this->__hooks[$p]; |
84 | if ($hook['auth'] > Session::get('auth', AUTH_PUBLIC)) { |
85 | $_SESSION['session']->doAuth($page); |
86 | } |
87 | return call_user_func_array($hook['hook'], $args); |
88 | } |
89 | |
90 | function run() |
91 | { |
92 | global $page; |
93 | |
94 | new_skinned_page('index.tpl', AUTH_PUBLIC); |
95 | |
96 | if (empty($this->path)) { |
97 | $page->run(); |
98 | } |
99 | |
100 | switch ($this->call_hook($page)) { |
101 | case PL_FORBIDDEN: |
102 | $this->__mods['core']->handler_403($page); |
103 | break; |
104 | |
105 | case PL_NOT_FOUND: |
106 | $this->__mods['core']->handler_404($page); |
107 | break; |
108 | } |
109 | $page->run(); |
110 | } |
111 | } |
112 | |
113 | ?> |