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