switch to an explicit list of modules.
[platal.git] / classes / Platal.php
CommitLineData
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
22define('PL_OK', 0);
23define('PL_NEEDLOGIN', 1);
24define('PL_FORBIDDEN', 403);
25define('PL_NOT_FOUND', 404);
26
27class Platal
28{
29 var $__mods;
30 var $__hooks;
31
32 var $path;
7b728d84 33 var $argv;
b62f8858 34
35 function Platal()
36 {
e77c7ea2 37 $modules = func_get_args();
0025a0c5 38 $this->path = trim(Get::_get('p', null), '/');
b62f8858 39
40 $this->__mods = array();
41 $this->__hooks = array();
b62f8858 42
e77c7ea2 43 foreach ($modules as $module) {
b62f8858 44 $m =& PLModule::factory($this, $module);
45 $this->__mods[$module] =& $m;
46 $this->__hooks += $m->handlers();
b62f8858 47 }
b62f8858 48 }
49
7c6e0aff 50 function find_hook()
b62f8858 51 {
52 $p = $this->path;
53
4a5fb34b 54 while ($p) {
b62f8858 55 if (array_key_exists($p, $this->__hooks))
56 break;
57
58 $p = substr($p, 0, strrpos($p, '/'));
59 }
7c6e0aff 60
b62f8858 61 if (empty($this->__hooks[$p])) {
7c6e0aff 62 return null;
b62f8858 63 }
64
15a094c0 65 $hook = $this->__hooks[$p];
66
67 if (!is_callable($hook['hook'])) {
7c6e0aff 68 return null;
69 }
70
71 $this->argv = explode('/', substr($this->path, strlen($p)));
72 $this->argv[0] = $p;
73
74 return $hook;
75 }
76
77 function call_hook(&$page)
78 {
79 $hook = $this->find_hook();
80
81 if (is_null($hook)) {
15a094c0 82 return PL_NOT_FOUND;
83 }
84
7c6e0aff 85 $args = $this->argv;
86 $args[0] = &$page;
b62f8858 87
b62f8858 88 if ($hook['auth'] > Session::get('auth', AUTH_PUBLIC)) {
89 $_SESSION['session']->doAuth($page);
90 }
7c6e0aff 91
b62f8858 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)) {
0889eb33 102 $this->__mods['core']->handler_index($page);
103 } else
b62f8858 104 switch ($this->call_hook($page)) {
105 case PL_FORBIDDEN:
106 $this->__mods['core']->handler_403($page);
107 break;
108
109 case PL_NOT_FOUND:
110 $this->__mods['core']->handler_404($page);
111 break;
112 }
11e24bec 113 $page->assign_by_ref('platal', $this);
b62f8858 114 $page->run();
115 }
116}
117
118?>