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