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