r4288 renderInterfaceHTML(): new function to keep the HTML code from index.php
[racktables] / wwwroot / index.php
CommitLineData
b325120a 1<?php
90a3d6d8
DY
2ob_start();
3try {
87c744a9
DO
4// Code block below is a module request dispatcher. Turning it into a
5// function will break things because of the way require() works.
36ef72d9
DO
6if (array_key_exists ('module', $_REQUEST))
7{
8 switch ($_REQUEST['module'])
9 {
10 case 'tsuri':
4afb4c10 11 require_once 'inc/init.php';
36ef72d9
DO
12 genericAssertion ('uri', 'string');
13 proxyStaticURI ($_REQUEST['uri']);
14 break;
0415b520 15 case 'download':
4afb4c10 16 require_once 'inc/init.php';
0415b520
DO
17 $pageno = 'file';
18 $tabno = 'download';
19 fixContext();
20 if (!permitted())
21 {
22 require_once 'inc/interface.php';
87c744a9
DO
23 renderAccessDenied (FALSE);
24 break;
0415b520
DO
25 }
26
27 $asattach = (isset ($_REQUEST['asattach']) and $_REQUEST['asattach'] == 'no') ? FALSE : TRUE;
28 $file = getFile (getBypassValue());
29 header("Content-Type: {$file['type']}");
30 header("Content-Length: {$file['size']}");
31 if ($asattach)
32 header("Content-Disposition: attachment; filename={$file['name']}");
33 echo $file['contents'];
34 break;
4afb4c10
DO
35 case 'image':
36 require_once 'inc/render_image.php';
37 // 'progressbar's never change, attempt an IMS chortcut before loading init.php
38 checkIMSCondition();
39 require_once 'inc/init.php';
40 try
41 {
42 dispatchImageRequest();
43 }
44 catch (Exception $e)
45 {
87c744a9 46 ob_clean();
4afb4c10
DO
47 renderError();
48 }
49 break;
9f4f431c
DO
50 case 'ajax':
51 require_once 'inc/ajax-interface.php';
52 require_once 'inc/init.php';
53 try
54 {
55 dispatchAJAXRequest();
56 }
57 catch (InvalidRequestArgException $e)
58 {
59 ob_clean();
60 echo "NAK\nMalformed request";
61 }
62 catch (Exception $e)
63 {
64 ob_clean();
65 echo "NAK\nRuntime exception: ". $e->getMessage();
66 }
67 break;
87c744a9
DO
68 case 'redirect':
69 // Include init after ophandlers/snmp, not before, so local.php can redefine things.
70 require_once 'inc/ophandlers.php';
71 // snmp.php is an exception, it is treated by a special hack
72 if (isset ($_REQUEST['op']) and $_REQUEST['op'] == 'querySNMPData')
73 require_once 'inc/snmp.php';
74 require_once 'inc/init.php';
75 try
76 {
77 genericAssertion ('op', 'string');
78 $op = $_REQUEST['op'];
79 prepareNavigation();
80 $location = buildWideRedirectURL();
81 // FIXME: find a better way to handle this error
82 if ($op == 'addFile' && !isset($_FILES['file']['error']))
83 throw new RackTablesError ('File upload error, check upload_max_filesize in php.ini', RackTablesError::MISCONFIGURED);
84 fixContext();
85 if
86 (
87 !isset ($ophandler[$pageno][$tabno][$op]) or
88 !function_exists ($ophandler[$pageno][$tabno][$op])
89 )
90 throw new RackTablesError ("Invalid navigation data for '${pageno}-${tabno}-${op}'", RackTablesError::INTERNAL);
91 // We have a chance to handle an error before starting HTTP header.
92 if (!isset ($delayauth[$pageno][$tabno][$op]) and !permitted())
93 showError ('Operation not permitted');
94 else
95 {
96 // Call below does the job of bypass argument assertion, if such is required,
97 // so the ophandler function doesn't have to re-assert this portion of its
98 // arguments. And it would be even better to pass returned value to ophandler,
99 // so it is not necessary to remember the name of bypass in it.
100 getBypassValue();
101 if (strlen ($redirect_to = call_user_func ($ophandler[$pageno][$tabno][$op])))
102 $location = $redirect_to;
103 }
104 header ("Location: " . $location);
105 }
106 // known "soft" failures require a short error message
107 catch (InvalidRequestArgException2 $e)
108 {
109 ob_clean();
110 showError ($e->getMessage());
111 header ('Location: ' . $location);
112 }
113 catch (RTDatabaseError $e)
114 {
115 ob_clean();
116 showError ('Database error: ' . $e->getMessage());
117 header ('Location: ' . $location);
118 }
119 // any other error requires no special handling and will be caught outside
120 break;
36ef72d9
DO
121 default:
122 throw new InvalidRequestArgException ('module', $_REQUEST['module']);
123 }
0415b520 124 ob_end_flush();
36ef72d9
DO
125 exit;
126}
127
b52cc354
DO
128require_once 'inc/interface.php';
129# init.php has to be included after interface.php, otherwise the bits
130# set by local.php get lost
131require_once 'inc/init.php';
329ec966 132prepareNavigation();
da958e52
DO
133// no ctx override is necessary
134fixContext();
750d26d2 135redirectIfNecessary();
da958e52 136if (!permitted())
da958e52 137 renderAccessDenied();
878512c6 138header ('Content-Type: text/html; charset=UTF-8');
b0348307 139// Only store the tab name after clearance is got. Any failure is unhandleable.
fa5b2764
AA
140if (isset ($_REQUEST['tab']) and ! isset ($_SESSION['RTLT'][$pageno]['dont_remember']))
141 $_SESSION['RTLT'][$pageno] = array ('tabname' => $tabno, 'time' => time());
e673ee24 142
1d4d254b
AA
143// call the main handler - page or tab handler.
144// catch exception and show its error message instead of page/tab content
145try {
e45a78d7 146if (isset ($tabhandler[$pageno][$tabno]))
08408472 147 call_user_func ($tabhandler[$pageno][$tabno], getBypassValue());
e45a78d7 148elseif (isset ($page[$pageno]['handler']))
e673ee24
DO
149 $page[$pageno]['handler'] ($tabno);
150else
1d4d254b 151 showError ("Failed to find handler for page '${pageno}', tab '${tabno}'");
08408472 152$content = ob_get_clean();
1d4d254b
AA
153} catch (Exception $e) {
154 ob_clean();
155 $content = '';
156 showError ("Unhandled exception: " . $e->getMessage());
157}
158
08408472 159ob_start();
f97df464 160renderInterfaceHTML ($pageno, $tabno, $content);
65e557dd 161 ob_flush();
90a3d6d8
DY
162} catch (Exception $e) {
163 ob_end_clean();
164 printException($e);
165}
1d4d254b 166clearMessages(); // prevent message appearing in foreign tab
e410ebfc 167?>