750680f8028dca6e9015620283e3fa9ffb5c691b
[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_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 $ns;
33 var $path;
34 var $argv;
35
36 function Platal()
37 {
38 $modules = func_get_args();
39 $this->path = trim(Get::_get('p', null), '/');
40
41 $this->__mods = array();
42 $this->__hooks = array();
43
44 array_unshift($modules, 'core');
45 foreach ($modules as $module) {
46 $this->__mods[$module] = $m = PLModule::factory($this, $module);
47 $this->__hooks += $m->handlers();
48 }
49 }
50
51 function find_hook()
52 {
53 $p = $this->path;
54
55 while ($p) {
56 if (array_key_exists($p, $this->__hooks))
57 break;
58
59 $p = substr($p, 0, strrpos($p, '/'));
60 }
61
62 if (empty($this->__hooks[$p])) {
63 return null;
64 }
65
66 $hook = $this->__hooks[$p];
67
68 if (!is_callable($hook['hook'])) {
69 return null;
70 }
71
72 $this->argv = explode('/', substr($this->path, strlen($p)));
73 $this->argv[0] = $p;
74
75 return $hook;
76 }
77
78 function call_hook(&$page)
79 {
80 $hook = $this->find_hook();
81
82 if (is_null($hook)) {
83 return PL_NOT_FOUND;
84 }
85
86 $args = $this->argv;
87 $args[0] = &$page;
88
89 if ($hook['auth'] > Session::get('auth', AUTH_PUBLIC)) {
90 $_SESSION['session']->doAuth($page);
91 }
92
93 return call_user_func_array($hook['hook'], $args);
94 }
95
96 function run()
97 {
98 global $page;
99
100 new_skinned_page('index.tpl', AUTH_PUBLIC);
101
102 if (empty($this->path)) {
103 $this->path = 'index';
104 }
105
106 switch ($this->call_hook($page)) {
107 case PL_FORBIDDEN:
108 $this->__mods['core']->handler_403($page);
109 break;
110
111 case PL_NOT_FOUND:
112 $this->__mods['core']->handler_404($page);
113 break;
114 }
115 $page->assign_by_ref('platal', $this);
116 $page->run();
117 }
118 }
119
120 ?>