lot of various code simplifications, including removing useless settings,
[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
b62f8858 22define('PL_FORBIDDEN', 403);
23define('PL_NOT_FOUND', 404);
24
25class Platal
26{
27 var $__mods;
28 var $__hooks;
29
5de0b7e1 30 var $ns;
b62f8858 31 var $path;
7b728d84 32 var $argv;
b62f8858 33
34 function Platal()
35 {
e77c7ea2 36 $modules = func_get_args();
0025a0c5 37 $this->path = trim(Get::_get('p', null), '/');
b62f8858 38
39 $this->__mods = array();
40 $this->__hooks = array();
b62f8858 41
5de0b7e1 42 array_unshift($modules, 'core');
e77c7ea2 43 foreach ($modules as $module) {
5de0b7e1 44 $this->__mods[$module] = $m = PLModule::factory($this, $module);
b62f8858 45 $this->__hooks += $m->handlers();
b62f8858 46 }
b62f8858 47 }
48
7c6e0aff 49 function find_hook()
b62f8858 50 {
51 $p = $this->path;
52
4a5fb34b 53 while ($p) {
b62f8858 54 if (array_key_exists($p, $this->__hooks))
55 break;
56
57 $p = substr($p, 0, strrpos($p, '/'));
58 }
7c6e0aff 59
b62f8858 60 if (empty($this->__hooks[$p])) {
7c6e0aff 61 return null;
b62f8858 62 }
63
15a094c0 64 $hook = $this->__hooks[$p];
65
66 if (!is_callable($hook['hook'])) {
7c6e0aff 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)) {
15a094c0 81 return PL_NOT_FOUND;
82 }
83
7c6e0aff 84 $args = $this->argv;
85 $args[0] = &$page;
b62f8858 86
cab08090 87 if ($hook['auth'] > S::v('auth', AUTH_PUBLIC)) {
88 // FIXME: don't use 'session' object anymore
63528107 89 if (!$_SESSION['session']->doAuth()) {
90 $this->force_login($page);
91 }
b62f8858 92 }
7c6e0aff 93
b62f8858 94 return call_user_func_array($hook['hook'], $args);
95 }
96
63528107 97 function force_login(&$page)
98 {
99 if (S::logged() and !$new_name) {
100 $page->changeTpl('password_prompt_logged.tpl');
101 $page->addJsLink('javascript/do_challenge_response_logged.js');
102 } else {
103 $page->changeTpl('password_prompt.tpl');
104 $page->addJsLink('javascript/do_challenge_response.js');
105 }
106 $page->run();
107 }
108
b62f8858 109 function run()
110 {
111 global $page;
112
801fcad8 113 new_skinned_page('index.tpl');
b62f8858 114
115 if (empty($this->path)) {
c9178c75 116 $this->path = 'index';
117 }
118
b62f8858 119 switch ($this->call_hook($page)) {
120 case PL_FORBIDDEN:
121 $this->__mods['core']->handler_403($page);
122 break;
123
124 case PL_NOT_FOUND:
125 $this->__mods['core']->handler_404($page);
126 break;
127 }
11e24bec 128 $page->assign_by_ref('platal', $this);
b62f8858 129 $page->run();
130 }
131}
132
133?>