Update auth.php with LDAP option to specify port
[racktables-incomplete-works] / 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();
23 // Security context is built on the requested page/tab/bypass data,
24 // do not override.
25 fixContext();
26 redirectIfNecessary();
3ec33017 27 assertPermission();
c5dfde62 28 header ('Content-Type: text/html; charset=UTF-8');
c5dfde62
DO
29 // call the main handler - page or tab handler.
30 if (isset ($tabhandler[$pageno][$tabno]))
04104016 31 {
71eb9d50 32 if (! is_callable ($tabhandler[$pageno][$tabno]))
04104016 33 throw new RackTablesError ("Missing handler function for node '${pageno}-${tabno}'", RackTablesError::INTERNAL);
c5dfde62 34 call_user_func ($tabhandler[$pageno][$tabno], getBypassValue());
04104016 35 }
c5dfde62 36 elseif (isset ($page[$pageno]['handler']))
04104016 37 {
71eb9d50 38 if (! is_callable ($page[$pageno]['handler']))
04104016 39 throw new RackTablesError ("Missing handler function for node '${pageno}'", RackTablesError::INTERNAL);
c5dfde62 40 $page[$pageno]['handler'] ($tabno);
04104016 41 }
c5dfde62
DO
42 else
43 throw new RackTablesError ("Failed to find handler for page '${pageno}', tab '${tabno}'", RackTablesError::INTERNAL);
44 // Embed the current text in OB into interface layout (the latter also
45 // empties color message buffer).
46 $contents = ob_get_contents();
47 ob_clean();
48 renderInterfaceHTML ($pageno, $tabno, $contents);
49 break;
b090b2da
RQ
50
51 case 'chrome':
4afb4c10 52 require_once 'inc/init.php';
5c42f907 53 require_once 'inc/solutions.php';
36ef72d9
DO
54 genericAssertion ('uri', 'string');
55 proxyStaticURI ($_REQUEST['uri']);
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']}");
39cfa9a7 67 if (! array_key_exists ('asattach', $_REQUEST) or $_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
76 # Content-type. Hence "image" module indicates failures with internally
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 }
3ec33017
DO
85 catch (RTPermissionDenied $e)
86 {
87 ob_clean();
88 renderAccessDeniedImage();
89 }
4afb4c10
DO
90 catch (Exception $e)
91 {
87c744a9 92 ob_clean();
11566bd6 93 renderErrorImage();
4afb4c10
DO
94 }
95 break;
b090b2da
RQ
96
97 case 'svg':
fa7634c7
DO
98 require_once 'inc/init.php';
99 require_once 'inc/solutions.php';
100 header ('Content-Type: image/svg+xml');
101 echo '<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>' . "\n";
102 echo '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . "\n";
103 try
104 {
105 genericAssertion ('view', 'string');
106 if (! array_key_exists ($_REQUEST['view'], $svghandler))
107 throw new InvalidRequestArgException ('view', $_REQUEST['view'], 'undefined view');
71eb9d50 108 if (! is_callable ($svghandler[$_REQUEST['view']]))
fa7634c7
DO
109 throw new RackTablesError ('missing handler function', RackTablesError::INTERNAL);
110 call_user_func ($svghandler[$_REQUEST['view']]);
111 }
112 catch (RTPermissionDenied $e)
113 {
114 ob_clean();
115 printSVGMessageBar ('permission denied', array ('fill' => 'white'), array ('fill' => 'black', 'stroke' => 'gray'));
116 }
117 catch (InvalidRequestArgException $e)
118 {
119 ob_clean();
120 printSVGMessageBar ('malformed HTTP request', array(), array ('fill' => 'yellow', 'stroke' => 'black'));
121 }
122 catch (EntityNotFoundException $e)
123 {
124 ob_clean();
125 printSVGMessageBar ('no such record', array(), array ('fill' => 'yellow', 'stroke' => 'black'));
126 }
127 catch (RackTablesError $e)
128 {
129 ob_clean();
130 printSVGMessageBar ('RT error: ' . $e->getMessage(), array(), array ('fill' => 'red', 'stroke' => 'black'));
131 }
132 catch (Exception $e)
133 {
134 ob_clean();
135 printSVGMessageBar ('unknown error', array(), array ('fill' => 'red', 'stroke' => 'black'));
136 }
137 break;
b090b2da
RQ
138
139 case 'progressbar':
a069b30d
DO
140 # Unlike images (and like static content), progress bars are processed
141 # without a permission check, but only for authenticated users.
142 require_once 'inc/init.php';
143 require_once 'inc/solutions.php';
4487fd91
DO
144 try
145 {
146 genericAssertion ('done', 'uint0');
147 // 'progressbar's never change, make browser cache the result
148 if (checkCachedResponse (0, CACHE_DURATION))
149 break;
150 renderProgressBarImage ($_REQUEST['done']);
151 }
152 catch (Exception $e)
153 {
154 ob_clean();
155 renderProgressBarError();
156 }
a069b30d 157 break;
b090b2da
RQ
158
159 case 'progressbar4':
4318ced5
AA
160 # Unlike images (and like static content), progress bars are processed
161 # without a permission check, but only for authenticated users.
162 require_once 'inc/init.php';
163 require_once 'inc/solutions.php';
164 try
165 {
166 renderProgressBar4Image ($_REQUEST['px1'], $_REQUEST['px2'], $_REQUEST['px3']);
167 }
168 catch (Exception $e)
169 {
170 ob_clean();
171 renderProgressBarError();
172 }
173 break;
b090b2da
RQ
174
175 case 'ajax':
7bbe121d 176 require_once 'inc/init.php';
9f4f431c 177 require_once 'inc/ajax-interface.php';
4318ced5 178 require_once 'inc/solutions.php';
9f4f431c
DO
179 try
180 {
7bbe121d
AA
181 genericAssertion ('ac', 'string');
182 $ac = $_REQUEST['ac'];
183 if (isset ($ajaxhandler[$ac]))
184 $ajaxhandler[$ac]();
185 else
186 {
187 ob_clean();
188 echo "NAK\nMalformed request";
189 }
9f4f431c 190 }
fce31eff
DO
191 catch (RTPermissionDenied $e)
192 {
193 ob_clean();
194 # FIXME: the remote client could be expecting JSON data instead
195 echo "NAK\nPermission denied";
196 }
9f4f431c
DO
197 catch (Exception $e)
198 {
199 ob_clean();
200 echo "NAK\nRuntime exception: ". $e->getMessage();
201 }
202 break;
b090b2da
RQ
203
204 case 'redirect':
87c744a9
DO
205 // Include init after ophandlers/snmp, not before, so local.php can redefine things.
206 require_once 'inc/ophandlers.php';
207 // snmp.php is an exception, it is treated by a special hack
208 if (isset ($_REQUEST['op']) and $_REQUEST['op'] == 'querySNMPData')
209 require_once 'inc/snmp.php';
210 require_once 'inc/init.php';
211 try
212 {
213 genericAssertion ('op', 'string');
214 $op = $_REQUEST['op'];
215 prepareNavigation();
8ab645cf 216 $location = buildRedirectURL();
87c744a9
DO
217 // FIXME: find a better way to handle this error
218 if ($op == 'addFile' && !isset($_FILES['file']['error']))
219 throw new RackTablesError ('File upload error, check upload_max_filesize in php.ini', RackTablesError::MISCONFIGURED);
220 fixContext();
221 if
222 (
223 !isset ($ophandler[$pageno][$tabno][$op]) or
71eb9d50 224 ! is_callable ($ophandler[$pageno][$tabno][$op])
87c744a9
DO
225 )
226 throw new RackTablesError ("Invalid navigation data for '${pageno}-${tabno}-${op}'", RackTablesError::INTERNAL);
227 // We have a chance to handle an error before starting HTTP header.
6459b55a 228 if (!isset ($delayauth["${pageno}-${tabno}-${op}"]))
3ec33017
DO
229 assertPermission();
230 # Call below does the job of bypass argument assertion, if such is required,
231 # so the ophandler function doesn't have to re-assert this portion of its
232 # arguments. And it would be even better to pass returned value to ophandler,
233 # so it is not necessary to remember the name of bypass in it.
234 getBypassValue();
235 if (strlen ($redirect_to = call_user_func ($ophandler[$pageno][$tabno][$op])))
236 $location = $redirect_to;
87c744a9
DO
237 }
238 // known "soft" failures require a short error message
93c946ac 239 catch (InvalidRequestArgException $e)
87c744a9
DO
240 {
241 ob_clean();
242 showError ($e->getMessage());
87c744a9
DO
243 }
244 catch (RTDatabaseError $e)
245 {
246 ob_clean();
247 showError ('Database error: ' . $e->getMessage());
87c744a9 248 }
3ec33017
DO
249 catch (RTPermissionDenied $e)
250 {
251 ob_clean();
252 showError ('Operation not permitted');
253 }
a2b16202 254 redirectUser ($location);
87c744a9
DO
255 // any other error requires no special handling and will be caught outside
256 break;
b090b2da
RQ
257
258 case 'popup':
e0ce8064 259 require_once 'inc/popup.php';
e0ce8064
DO
260 require_once 'inc/init.php';
261 renderPopupHTML();
262 break;
b090b2da
RQ
263
264 case 'upgrade':
964b0388
DO
265 require_once 'inc/config.php'; // for CODE_VERSION
266 require_once 'inc/dictionary.php';
71066ef1 267 require_once 'inc/functions.php'; // for ip translation functions
964b0388 268 require_once 'inc/upgrade.php';
964b0388
DO
269 renderUpgraderHTML();
270 break;
b090b2da
RQ
271
272 case 'installer':
e37cfe5f 273 require_once 'inc/dictionary.php';
72933f2a 274 require_once 'inc/config.php';
e37cfe5f
DO
275 require_once 'inc/install.php';
276 renderInstallerHTML();
277 break;
b090b2da 278
36ef72d9 279 default:
b090b2da 280 throw new InvalidRequestArgException ('module', $requestedModule);
36ef72d9 281 }
0415b520 282 ob_end_flush();
36ef72d9 283}
c5dfde62
DO
284catch (Exception $e)
285{
90a3d6d8 286 ob_end_clean();
d112b653 287 printException ($e);
90a3d6d8 288}
e410ebfc 289?>