fixup transparency in a few images
[racktables] / wwwroot / index.php
CommitLineData
b325120a 1<?php
cddbb9fd
DO
2
3# This file is a part of RackTables, a datacenter and server room management
4# framework. See accompanying file "COPYING" for the full copyright and
5# licensing information.
6
90a3d6d8 7ob_start();
b00cc78c 8require_once 'inc/pre-init.php';
90a3d6d8 9try {
b090b2da
RQ
10 // Switch block below is a module request dispatcher.
11 // Dispatches based on module request.
12 // The last string 'interface' is the default.
13 $requestedModule = array_key_exists ('module', $_REQUEST) ? $_REQUEST['module'] : 'interface';
14
15 switch ($requestedModule)
36ef72d9 16 {
b090b2da 17 case 'interface':
c5dfde62
DO
18 require_once 'inc/interface.php';
19 // init.php has to be included after interface.php, otherwise the bits
20 // set by local.php get lost
21 require_once 'inc/init.php';
22 prepareNavigation();
bdf587b8 23 requireExtraFiles ($interface_requires);
c5dfde62
DO
24 // Security context is built on the requested page/tab/bypass data,
25 // do not override.
26 fixContext();
27 redirectIfNecessary();
3ec33017 28 assertPermission();
c5dfde62 29 header ('Content-Type: text/html; charset=UTF-8');
c5dfde62
DO
30 // call the main handler - page or tab handler.
31 if (isset ($tabhandler[$pageno][$tabno]))
04104016 32 {
71eb9d50 33 if (! is_callable ($tabhandler[$pageno][$tabno]))
04104016 34 throw new RackTablesError ("Missing handler function for node '${pageno}-${tabno}'", RackTablesError::INTERNAL);
c5dfde62 35 call_user_func ($tabhandler[$pageno][$tabno], getBypassValue());
04104016 36 }
c5dfde62 37 elseif (isset ($page[$pageno]['handler']))
04104016 38 {
71eb9d50 39 if (! is_callable ($page[$pageno]['handler']))
04104016 40 throw new RackTablesError ("Missing handler function for node '${pageno}'", RackTablesError::INTERNAL);
c5dfde62 41 $page[$pageno]['handler'] ($tabno);
04104016 42 }
c5dfde62
DO
43 else
44 throw new RackTablesError ("Failed to find handler for page '${pageno}', tab '${tabno}'", RackTablesError::INTERNAL);
45 // Embed the current text in OB into interface layout (the latter also
46 // empties color message buffer).
47 $contents = ob_get_contents();
48 ob_clean();
49 renderInterfaceHTML ($pageno, $tabno, $contents);
50 break;
b090b2da
RQ
51
52 case 'chrome':
4afb4c10 53 require_once 'inc/init.php';
5c42f907 54 require_once 'inc/solutions.php';
af7acfcc 55 proxyStaticURI (genericAssertion ('uri', 'string'));
36ef72d9 56 break;
b090b2da
RQ
57
58 case 'download':
4afb4c10 59 require_once 'inc/init.php';
0415b520
DO
60 $pageno = 'file';
61 $tabno = 'download';
62 fixContext();
3ec33017 63 assertPermission();
0415b520
DO
64 $file = getFile (getBypassValue());
65 header("Content-Type: {$file['type']}");
66 header("Content-Length: {$file['size']}");
55eefced 67 if (! array_key_exists ('asattach', $_REQUEST) || $_REQUEST['asattach'] != 'no')
0415b520
DO
68 header("Content-Disposition: attachment; filename={$file['name']}");
69 echo $file['contents'];
70 break;
b090b2da
RQ
71
72 case 'image':
b849b7e1
DO
73 # The difference between "image" and "download" ways to serve the same
74 # picture file is that the former is used in <IMG SRC=...> construct,
75 # and the latter is accessed as a standalone URL and can reply with any
ff9b6ad3 76 # Content-Type. Hence "image" module indicates failures with internally
b849b7e1
DO
77 # built images, and "download" can return a full-fledged "permission
78 # denied" or "exception" HTML page instead of the file requested.
8b912171 79 require_once 'inc/init.php'; // for authentication check
5c42f907 80 require_once 'inc/solutions.php';
4afb4c10
DO
81 try
82 {
83 dispatchImageRequest();
84 }
85 catch (Exception $e)
86 {
87c744a9 87 ob_clean();
8d3ab47c 88 throw ($e instanceof RTImageError) ? $e : new RTImageError;
4afb4c10
DO
89 }
90 break;
b090b2da
RQ
91
92 case 'svg':
fa7634c7
DO
93 require_once 'inc/init.php';
94 require_once 'inc/solutions.php';
95 header ('Content-Type: image/svg+xml');
96 echo '<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>' . "\n";
97 echo '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . "\n";
98 try
99 {
af7acfcc
DO
100 $view = genericAssertion ('view', 'string');
101 if (! array_key_exists ($view, $svghandler))
102 throw new InvalidRequestArgException ('view', $view, 'undefined view');
103 if (! is_callable ($svghandler[$view]))
fa7634c7 104 throw new RackTablesError ('missing handler function', RackTablesError::INTERNAL);
af7acfcc 105 call_user_func ($svghandler[$view]);
fa7634c7
DO
106 }
107 catch (RTPermissionDenied $e)
108 {
109 ob_clean();
110 printSVGMessageBar ('permission denied', array ('fill' => 'white'), array ('fill' => 'black', 'stroke' => 'gray'));
111 }
112 catch (InvalidRequestArgException $e)
113 {
114 ob_clean();
115 printSVGMessageBar ('malformed HTTP request', array(), array ('fill' => 'yellow', 'stroke' => 'black'));
116 }
117 catch (EntityNotFoundException $e)
118 {
119 ob_clean();
120 printSVGMessageBar ('no such record', array(), array ('fill' => 'yellow', 'stroke' => 'black'));
121 }
122 catch (RackTablesError $e)
123 {
124 ob_clean();
125 printSVGMessageBar ('RT error: ' . $e->getMessage(), array(), array ('fill' => 'red', 'stroke' => 'black'));
126 }
127 catch (Exception $e)
128 {
129 ob_clean();
130 printSVGMessageBar ('unknown error', array(), array ('fill' => 'red', 'stroke' => 'black'));
131 }
132 break;
b090b2da
RQ
133
134 case 'progressbar':
a069b30d
DO
135 # Unlike images (and like static content), progress bars are processed
136 # without a permission check, but only for authenticated users.
137 require_once 'inc/init.php';
138 require_once 'inc/solutions.php';
4487fd91
DO
139 try
140 {
4487fd91
DO
141 // 'progressbar's never change, make browser cache the result
142 if (checkCachedResponse (0, CACHE_DURATION))
143 break;
af7acfcc 144 renderProgressBarImage (genericAssertion ('done', 'uint0'));
4487fd91
DO
145 }
146 catch (Exception $e)
147 {
148 ob_clean();
8d3ab47c 149 throw ($e instanceof RTImageError) ? $e : new RTImageError ('pbar_error');
4487fd91 150 }
a069b30d 151 break;
b090b2da
RQ
152
153 case 'progressbar4':
4318ced5
AA
154 # Unlike images (and like static content), progress bars are processed
155 # without a permission check, but only for authenticated users.
156 require_once 'inc/init.php';
157 require_once 'inc/solutions.php';
158 try
159 {
af7acfcc
DO
160 renderProgressBar4Image
161 (
162 genericAssertion ('px1', 'uint0'),
163 genericAssertion ('px2', 'uint0'),
164 genericAssertion ('px3', 'uint0')
165 );
4318ced5
AA
166 }
167 catch (Exception $e)
168 {
169 ob_clean();
8d3ab47c 170 throw ($e instanceof RTImageError) ? $e : new RTImageError ('pbar_error');
4318ced5
AA
171 }
172 break;
b090b2da
RQ
173
174 case 'ajax':
7bbe121d 175 require_once 'inc/init.php';
9f4f431c 176 require_once 'inc/ajax-interface.php';
4318ced5 177 require_once 'inc/solutions.php';
9f4f431c
DO
178 try
179 {
af7acfcc 180 $ac = genericAssertion ('ac', 'string');
7bbe121d
AA
181 if (isset ($ajaxhandler[$ac]))
182 $ajaxhandler[$ac]();
183 else
184 {
185 ob_clean();
186 echo "NAK\nMalformed request";
187 }
9f4f431c 188 }
fce31eff
DO
189 catch (RTPermissionDenied $e)
190 {
191 ob_clean();
192 # FIXME: the remote client could be expecting JSON data instead
193 echo "NAK\nPermission denied";
194 }
9f4f431c
DO
195 catch (Exception $e)
196 {
197 ob_clean();
198 echo "NAK\nRuntime exception: ". $e->getMessage();
199 }
200 break;
b090b2da
RQ
201
202 case 'redirect':
87c744a9
DO
203 // Include init after ophandlers/snmp, not before, so local.php can redefine things.
204 require_once 'inc/ophandlers.php';
205 // snmp.php is an exception, it is treated by a special hack
55eefced 206 if (isset ($_REQUEST['op']) && $_REQUEST['op'] == 'querySNMPData')
87c744a9
DO
207 require_once 'inc/snmp.php';
208 require_once 'inc/init.php';
209 try
210 {
af7acfcc 211 $op = genericAssertion ('op', 'string');
87c744a9 212 prepareNavigation();
8ab645cf 213 $location = buildRedirectURL();
87c744a9
DO
214 // FIXME: find a better way to handle this error
215 if ($op == 'addFile' && !isset($_FILES['file']['error']))
216 throw new RackTablesError ('File upload error, check upload_max_filesize in php.ini', RackTablesError::MISCONFIGURED);
217 fixContext();
218 if
219 (
55eefced 220 ! isset ($ophandler[$pageno][$tabno][$op]) ||
71eb9d50 221 ! is_callable ($ophandler[$pageno][$tabno][$op])
87c744a9
DO
222 )
223 throw new RackTablesError ("Invalid navigation data for '${pageno}-${tabno}-${op}'", RackTablesError::INTERNAL);
224 // We have a chance to handle an error before starting HTTP header.
6459b55a 225 if (!isset ($delayauth["${pageno}-${tabno}-${op}"]))
3ec33017
DO
226 assertPermission();
227 # Call below does the job of bypass argument assertion, if such is required,
228 # so the ophandler function doesn't have to re-assert this portion of its
229 # arguments. And it would be even better to pass returned value to ophandler,
230 # so it is not necessary to remember the name of bypass in it.
231 getBypassValue();
5aab5cd6 232 if ('' != $redirect_to = call_user_func ($ophandler[$pageno][$tabno][$op]))
3ec33017 233 $location = $redirect_to;
87c744a9
DO
234 }
235 // known "soft" failures require a short error message
93c946ac 236 catch (InvalidRequestArgException $e)
87c744a9
DO
237 {
238 ob_clean();
239 showError ($e->getMessage());
87c744a9
DO
240 }
241 catch (RTDatabaseError $e)
242 {
243 ob_clean();
244 showError ('Database error: ' . $e->getMessage());
87c744a9 245 }
3ec33017
DO
246 catch (RTPermissionDenied $e)
247 {
248 ob_clean();
249 showError ('Operation not permitted');
250 }
c1e766e5
DO
251 catch (Exception $e)
252 {
253 ob_clean();
254 printException ($e);
255 break;
256 }
1acbe865 257 showMySQLWarnings();
a2b16202 258 redirectUser ($location);
87c744a9
DO
259 // any other error requires no special handling and will be caught outside
260 break;
b090b2da
RQ
261
262 case 'popup':
e0ce8064 263 require_once 'inc/popup.php';
e0ce8064 264 require_once 'inc/init.php';
5d4ca0f3
AA
265 prepareNavigation();
266 fixContext();
267 assertPermission();
268 $helper = assertStringArg ('helper');
269
270 header ('Content-Type: text/html; charset=UTF-8');
271 // call the main handler - page or tab handler.
55eefced 272 if (isset ($popuphandler[$helper]) && is_callable ($popuphandler[$helper]))
5d4ca0f3
AA
273 call_user_func ($popuphandler[$helper], $helper);
274 else
275 throw new RackTablesError ("Missing handler function for node '${handler}'", RackTablesError::INTERNAL);
276 $contents = ob_get_contents();
277 ob_clean();
278 renderPopupHTML ($contents);
e0ce8064 279 break;
b090b2da
RQ
280
281 case 'upgrade':
964b0388 282 require_once 'inc/config.php'; // for CODE_VERSION
23fe6ea1 283 require_once 'inc/database.php';
964b0388 284 require_once 'inc/dictionary.php';
71066ef1 285 require_once 'inc/functions.php'; // for ip translation functions
964b0388 286 require_once 'inc/upgrade.php';
964b0388
DO
287 renderUpgraderHTML();
288 break;
b090b2da
RQ
289
290 case 'installer':
e37cfe5f 291 require_once 'inc/dictionary.php';
72933f2a 292 require_once 'inc/config.php';
e37cfe5f
DO
293 require_once 'inc/install.php';
294 renderInstallerHTML();
295 break;
b090b2da 296
36ef72d9 297 default:
b090b2da 298 throw new InvalidRequestArgException ('module', $requestedModule);
36ef72d9 299 }
0415b520 300 ob_end_flush();
36ef72d9 301}
c5dfde62
DO
302catch (Exception $e)
303{
90a3d6d8 304 ob_end_clean();
d112b653 305 printException ($e);
90a3d6d8 306}
e410ebfc 307?>