Set HTTP status code 500 if database connection fails. Should avoid pages
[diogenes.git] / include / diogenes.page.inc.php
CommitLineData
6855525e
JL
1<?php
2/*
3 * Copyright (C) 2003-2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21
22require_once 'diogenes/diogenes.core.page.inc.php';
23require_once 'diogenes/diogenes.misc.inc.php';
24
25/** This class describes a generic Diogenes page. This class
26 * is inherited to display barrel, admin or toplevel pages.
27 *
28 * @see DiogenesBarrel DiogenesAdmin DiogenesToplevel
29 */
30class DiogenesPage extends DiogenesCorePage
31 {
32
33 /** Handle to database */
34 var $dbh;
35
36 /** An array holding the contents of the 'head' tag */
37 var $head = array();
38
39 /** An array of items for the 'menu' area */
40 var $menu = array();
41
42 /** Whether we're into kill() or not */
43 var $_dying = false;
44
45 /** The constructor.
46 */
47 function DiogenesPage()
48 {
49 global $globals;
50 $this->dbh =& $globals->db;
51
52 // call parent constructor
53 $this->DiogenesCorePage();
54
55 // register Smarty functions
56 $this->register_function("menu","diogenes_func_menu");
57
58 // common Smarty assignments
59 $this->assign('poweredby', $globals->urlise(__("Powered by Diogenes") . " {$globals->version}"));
60 $this->assign('phplayersmenu', $this->url("phplayersmenu"));
61 $this->assign_by_ref('head', $this->head);
62 $this->assign_by_ref('menuitems', $this->menu);
63
64 // debugging assignments
65 $this->assign('msg_debug_bar', __("debugging"));
66 $this->assign('msg_debug_calltrace', __("call trace"));
67 $this->assign('msg_debug_dbtrace', __("database trace"));
68 $this->assign('msg_debug_plugins', __("plugins"));
69 }
70
71
72 /** Display a Smarty template.
73 *
74 * @param $template the template for the current page
75 * @param $master the master template
76 */
77 function display($template, $master = '') {
78 global $globals;
79
80 $this->assign('page_template', $template);
2a97441a 81 $this->makeMenu();
6855525e
JL
82 if ($globals->debugdatabase)
83 $this->assign('db_trace',$globals->db->trace_format($this));
84 if ($globals->debugplugins)
85 $this->assign('plugins_trace',$globals->plugins->trace_format($this));
86 if (($globals->debugplugins) || ($globals->debugdatabase))
87 $this->assign('debug_css', $this->url("common.css"));
6855525e
JL
88
89 if (!$master)
90 $master = $this->getTemplate();
91
92 parent::display($master);
93 }
94
95
96 /** Perform a logout. This should destroy both the session
97 * and the logger objects.
98 */
99 function doLogout()
100 {
101 global $globals;
102 $this->log('auth_logout', '');
103 unset($_SESSION['log']);
104 $_SESSION['session'] = new $globals->session;
105 }
106
107
108 /** Returns the master template for the current context.
109 */
110 function getTemplate()
111 {
112 global $globals;
113
114 if ($globals->template) {
115 // we have a system-wide default template, get its full path
116 $tpl = $this->templatePath($globals->template);
117 } else {
118 // fall back on the default template
119 $tpl = 'master.tpl';
120 }
121 return $tpl;
122 }
123
124
125 /** Returns the available master templates. */
126 function getTemplates()
127 {
128 global $globals;
129
130 // the default template
131 $templates[0] = "<default>";
132
133 // lookup templates in the template directory
134 if ($globals->template_dir && is_dir($globals->template_dir)) {
135 $files = System::find($globals->template_dir.' -maxdepth 1 -name *.tpl');
136 foreach ($files as $file)
137 $templates["global:".basename($file)] = "[global] ".basename($file);
138 }
139 return $templates;
140 }
141
142
143 /** Send an HTTP status header.
144 *
145 * @param code the HTTP status code
146 */
147 function httpStatus($code)
148 {
149 $message = array(
150 400 => "HTTP/1.0 400 Bad Request",
151 403 => "HTTP/1.0 403 Forbidden; Access Denied; Banned",
152 404 => "HTTP/1.0 404 Not Found",
153 500 => "HTTP/1.0 500 Internal Server Error",
154 );
155
156 if (!headers_sent())
157 header(isset($message[$code]) ? $message[$code] : "HTTP/1.0 $code");
158 }
159
160
161 /** Report an information.
162 *
163 * @param msg
164 */
165 function info($msg) {
166 $this->append('status',$msg);
167 }
168
169
170 /** Is the user logged in ? */
171 function isLogged() {
172 return isset($_SESSION['session']) && $_SESSION['session']->hasPerms('auth');
173 }
174
175
176 /** Is the user a root ("toplevel") admin ? */
177 function isRoot() {
178 return isset($_SESSION['session']) && $_SESSION['session']->hasPerms('root');
179 }
180
181
182 /** Die and display an error message.
183 *
184 * @param $msg the message to display
185 * @param $code the HTTP status code to send
186 */
187 function kill($msg, $code = 500) {
188 if ($this->_dying)
189 {
190 // We're in a loop of kills. This is very, very bad.
191 // We need to bale as quick as possible, because we can't rely on
192 // *any* system code to not be the source of the kill() call.
193 echo "<h1>Very fatal error: $msg</h1>\n";
194 exit;
195 }
196
197 $this->_dying = true;
198 $this->httpStatus($code);
199 $this->assign('greeting', __("Diogenes error"));
200 $this->assign('page', __("Error"));
201 $this->assign('page_content', "<p>$msg</p>");
202 $this->display('');
203 exit;
204 }
205
206
207 /** Display the dreaded "file not found page".
208 *
209 * @param msg optional extra error message
210 */
211 function kill404($msg = "") {
212 if ($msg)
213 $this->info($msg);
214 $this->kill( __("The requested document was not found."), 404);
215 }
216
217
218 /** Log an information.
219 *
220 * @param action
221 * @param data
222 */
223 function log($action,$data="") {
224 if (isset($_SESSION['log']) && is_object($_SESSION['log']))
225 $_SESSION['log']->log($action,$data);
226 }
227
228
229 /** Make the menu.
230 */
231 function makeMenu() {
232
233 }
234
235
236 /** Start session handling.
237 */
238 function startSession() {
239 global $globals;
240
241 session_start();
242 if (!isset($_SESSION['session']))
243 $_SESSION['session'] = new $globals->session;
244 }
245
246
247 /** Returns the path to a given template. */
248 function templatePath($template)
249 {
250 global $globals;
251
252 $bits = split(":", $template);
253 switch ($bits[0]) {
254 case "global":
255 $path = $globals->template_dir."/". $bits[1];
256 break;
257 default:
258 $this->kill("Unkown template type : '$template'");
259 }
260 return $path;
261 }
262
263
264 /** Adds a toolbar to the top of the page.
265 *
266 * @param title
267 * @param items
268 */
269 function toolbar($title, $items) {
270 $this->append('toolbars', array('title'=>$title, 'items'=>$items));
271 }
272
273
274 /** Returns the URL to a Diogenes barrel.
275 *
276 * @param alias
277 * @param vhost
278 * @param rel
279 */
280 function urlBarrel($alias,$vhost,$rel="") {
281 global $globals;
282 return $vhost ? "http://$vhost/$rel" : "{$globals->rooturl}site/$alias/$rel";
283 }
284
285}
286
287
288/** Displays a full menu.
289 *
290 * Parameters
291 * +items the menu items
292 * +style menu style (0, 1, 2)
293 * +theme menu theme
294 *
295 * @param params the function input
296 */
297function diogenes_func_menu($params)
298{
299 global $globals;
300
301 extract($params);
302 if (empty($items))
303 return;
304
305 switch($style) {
306 case 1: case 2:
307 include("phplayersmenu/PHPLIB.php");
308 include("phplayersmenu/layersmenu-common.inc.php");
309 include("phplayersmenu/treemenu.inc.php");
310 $tmp = "";
311 $firstlevel = 0;
312 $counter = 0;
313 foreach ($items as $item) {
314 $level = array_shift($item);
315 // remember the level of the first entry
316 if ($counter == 0)
317 $firstlevel = $level;
318 $dots = str_repeat(".",$level+1);
319 $link = array_shift($item);
320 $text = array_shift($item);
321 $expanded = array_shift($item);
322 $tmp .= "$dots|$link|$text||||$expanded\n";
323 $counter++;
324 }
325
326 $mid = new TreeMenu();
327 $mid->setLibjsdir($globals->root."/htdocs/phplayersmenu/");
328 $mid->setImgwww($globals->rooturl."phplayersmenu/$theme/");
329 $mid->setMenuStructureString($tmp);
330 $mid->parseStructureForMenu("diogenesmenu");
331 $out = $mid->newTreeMenu("diogenesmenu");
332
333 // this hack takes care of menus starting with 'orphan' child entries
334 if (($firstlevel > 0) && ($pos = strpos($out,"<div id=\"jt1\" class=\"treemenudiv\">"))) {
335 $insert = str_repeat("<div class=\"treemenudiv\">\n", $firstlevel);
336 $out = substr($out,0,$pos) . $insert . substr($out,$pos);
337 }
338 break;
339
340 case 0: default:
341 $out = "<div class=\"menu\">";
342 $oLevel = 0;
343 $oExpanded = 1;
344 foreach($items as $item) {
345 $level = $item[0];
346 $expanded = isset($item[3]) ? $item[3] : 0;
347 if ($oExpanded || $level <= $oLevel) {
348 $out .= diogenes_func_menu_item(compact("item"));
349 $oLevel = $level;
350 $oExpanded = $expanded;
351 }
352 }
353 $out .= "</div>";
354 break;
355 }
356 return $out;
357}
358
359?>