Commit | Line | Data |
---|---|---|
b325120a | 1 | <?php |
90a3d6d8 | 2 | ob_start(); |
91bd1d6e DO |
3 | # Neither "throw/catch" for custom exceptions nor printException() will |
4 | # work without first loading exceptions.php. | |
5 | require_once 'inc/exceptions.php'; | |
90a3d6d8 | 6 | try { |
87c744a9 DO |
7 | // Code block below is a module request dispatcher. Turning it into a |
8 | // function will break things because of the way require() works. | |
c5dfde62 | 9 | switch (TRUE) |
36ef72d9 | 10 | { |
c5dfde62 DO |
11 | case ! array_key_exists ('module', $_REQUEST): |
12 | case 'interface' == $_REQUEST['module']: | |
13 | require_once 'inc/interface.php'; | |
14 | // init.php has to be included after interface.php, otherwise the bits | |
15 | // set by local.php get lost | |
16 | require_once 'inc/init.php'; | |
17 | prepareNavigation(); | |
18 | // Security context is built on the requested page/tab/bypass data, | |
19 | // do not override. | |
20 | fixContext(); | |
21 | redirectIfNecessary(); | |
22 | if (! permitted()) | |
23 | { | |
24 | renderAccessDenied (FALSE); | |
25 | break; | |
26 | } | |
27 | header ('Content-Type: text/html; charset=UTF-8'); | |
28 | // Only store the tab name after clearance is got. Any failure is unhandleable. | |
29 | if (isset ($_REQUEST['tab']) and ! isset ($_SESSION['RTLT'][$pageno]['dont_remember'])) | |
30 | $_SESSION['RTLT'][$pageno] = array ('tabname' => $tabno, 'time' => time()); | |
31 | // call the main handler - page or tab handler. | |
32 | if (isset ($tabhandler[$pageno][$tabno])) | |
33 | call_user_func ($tabhandler[$pageno][$tabno], getBypassValue()); | |
34 | elseif (isset ($page[$pageno]['handler'])) | |
35 | $page[$pageno]['handler'] ($tabno); | |
36 | else | |
37 | throw new RackTablesError ("Failed to find handler for page '${pageno}', tab '${tabno}'", RackTablesError::INTERNAL); | |
38 | // Embed the current text in OB into interface layout (the latter also | |
39 | // empties color message buffer). | |
40 | $contents = ob_get_contents(); | |
41 | ob_clean(); | |
42 | renderInterfaceHTML ($pageno, $tabno, $contents); | |
43 | break; | |
19f22ad8 | 44 | case 'chrome' == $_REQUEST['module']: |
4afb4c10 | 45 | require_once 'inc/init.php'; |
36ef72d9 DO |
46 | genericAssertion ('uri', 'string'); |
47 | proxyStaticURI ($_REQUEST['uri']); | |
48 | break; | |
c5dfde62 | 49 | case 'download' == $_REQUEST['module']: |
4afb4c10 | 50 | require_once 'inc/init.php'; |
0415b520 DO |
51 | $pageno = 'file'; |
52 | $tabno = 'download'; | |
53 | fixContext(); | |
54 | if (!permitted()) | |
55 | { | |
87c744a9 DO |
56 | renderAccessDenied (FALSE); |
57 | break; | |
0415b520 | 58 | } |
0415b520 DO |
59 | $file = getFile (getBypassValue()); |
60 | header("Content-Type: {$file['type']}"); | |
61 | header("Content-Length: {$file['size']}"); | |
39cfa9a7 | 62 | if (! array_key_exists ('asattach', $_REQUEST) or $_REQUEST['asattach'] != 'no') |
0415b520 DO |
63 | header("Content-Disposition: attachment; filename={$file['name']}"); |
64 | echo $file['contents']; | |
65 | break; | |
c5dfde62 | 66 | case 'image' == $_REQUEST['module']: |
8b912171 | 67 | require_once 'inc/init.php'; // for authentication check |
5beb7c53 AA |
68 | // 'progressbar's never change, attempt an IMS shortcut before loading init.php |
69 | if (@$_REQUEST['img'] == 'progressbar') | |
8b912171 | 70 | if (checkCachedResponse (0, CACHE_DURATION)) |
5beb7c53 | 71 | exit; |
4afb4c10 | 72 | require_once 'inc/render_image.php'; |
4afb4c10 DO |
73 | try |
74 | { | |
75 | dispatchImageRequest(); | |
76 | } | |
77 | catch (Exception $e) | |
78 | { | |
87c744a9 | 79 | ob_clean(); |
11566bd6 | 80 | renderErrorImage(); |
4afb4c10 DO |
81 | } |
82 | break; | |
c5dfde62 | 83 | case 'ajax' == $_REQUEST['module']: |
9f4f431c DO |
84 | require_once 'inc/ajax-interface.php'; |
85 | require_once 'inc/init.php'; | |
86 | try | |
87 | { | |
88 | dispatchAJAXRequest(); | |
89 | } | |
90 | catch (InvalidRequestArgException $e) | |
91 | { | |
92 | ob_clean(); | |
93 | echo "NAK\nMalformed request"; | |
94 | } | |
95 | catch (Exception $e) | |
96 | { | |
97 | ob_clean(); | |
98 | echo "NAK\nRuntime exception: ". $e->getMessage(); | |
99 | } | |
100 | break; | |
c5dfde62 | 101 | case 'redirect' == $_REQUEST['module']: |
87c744a9 DO |
102 | // Include init after ophandlers/snmp, not before, so local.php can redefine things. |
103 | require_once 'inc/ophandlers.php'; | |
104 | // snmp.php is an exception, it is treated by a special hack | |
105 | if (isset ($_REQUEST['op']) and $_REQUEST['op'] == 'querySNMPData') | |
106 | require_once 'inc/snmp.php'; | |
107 | require_once 'inc/init.php'; | |
108 | try | |
109 | { | |
110 | genericAssertion ('op', 'string'); | |
111 | $op = $_REQUEST['op']; | |
112 | prepareNavigation(); | |
113 | $location = buildWideRedirectURL(); | |
114 | // FIXME: find a better way to handle this error | |
115 | if ($op == 'addFile' && !isset($_FILES['file']['error'])) | |
116 | throw new RackTablesError ('File upload error, check upload_max_filesize in php.ini', RackTablesError::MISCONFIGURED); | |
117 | fixContext(); | |
118 | if | |
119 | ( | |
120 | !isset ($ophandler[$pageno][$tabno][$op]) or | |
121 | !function_exists ($ophandler[$pageno][$tabno][$op]) | |
122 | ) | |
123 | throw new RackTablesError ("Invalid navigation data for '${pageno}-${tabno}-${op}'", RackTablesError::INTERNAL); | |
124 | // We have a chance to handle an error before starting HTTP header. | |
125 | if (!isset ($delayauth[$pageno][$tabno][$op]) and !permitted()) | |
126 | showError ('Operation not permitted'); | |
127 | else | |
128 | { | |
129 | // Call below does the job of bypass argument assertion, if such is required, | |
130 | // so the ophandler function doesn't have to re-assert this portion of its | |
131 | // arguments. And it would be even better to pass returned value to ophandler, | |
132 | // so it is not necessary to remember the name of bypass in it. | |
133 | getBypassValue(); | |
134 | if (strlen ($redirect_to = call_user_func ($ophandler[$pageno][$tabno][$op]))) | |
135 | $location = $redirect_to; | |
136 | } | |
137 | header ("Location: " . $location); | |
138 | } | |
139 | // known "soft" failures require a short error message | |
93c946ac | 140 | catch (InvalidRequestArgException $e) |
87c744a9 DO |
141 | { |
142 | ob_clean(); | |
143 | showError ($e->getMessage()); | |
144 | header ('Location: ' . $location); | |
145 | } | |
146 | catch (RTDatabaseError $e) | |
147 | { | |
148 | ob_clean(); | |
149 | showError ('Database error: ' . $e->getMessage()); | |
150 | header ('Location: ' . $location); | |
151 | } | |
152 | // any other error requires no special handling and will be caught outside | |
153 | break; | |
e0ce8064 DO |
154 | case 'popup' == $_REQUEST['module']: |
155 | require_once 'inc/popup.php'; | |
e0ce8064 DO |
156 | require_once 'inc/init.php'; |
157 | renderPopupHTML(); | |
158 | break; | |
964b0388 DO |
159 | case 'upgrade' == $_REQUEST['module']: |
160 | require_once 'inc/config.php'; // for CODE_VERSION | |
161 | require_once 'inc/dictionary.php'; | |
162 | require_once 'inc/upgrade.php'; | |
163 | // Enforce default value for now, releases prior to 0.17.0 didn't support 'httpd' auth source. | |
164 | $user_auth_src = 'database'; | |
165 | if (FALSE === @include_once 'inc/secret.php') | |
e37cfe5f | 166 | die ('<center>There is no working RackTables instance here, <a href="?module=installer">install</a>?</center>'); |
964b0388 DO |
167 | try |
168 | { | |
169 | $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password); | |
170 | } | |
171 | catch (PDOException $e) | |
172 | { | |
173 | die ("Database connection failed:\n\n" . $e->getMessage()); | |
174 | } | |
175 | renderUpgraderHTML(); | |
176 | break; | |
91bd1d6e | 177 | case 'installer' == $_REQUEST['module']: |
e37cfe5f DO |
178 | require_once 'inc/dictionary.php'; |
179 | require_once 'inc/install.php'; | |
180 | renderInstallerHTML(); | |
181 | break; | |
36ef72d9 DO |
182 | default: |
183 | throw new InvalidRequestArgException ('module', $_REQUEST['module']); | |
184 | } | |
0415b520 | 185 | ob_end_flush(); |
36ef72d9 | 186 | } |
c5dfde62 DO |
187 | catch (Exception $e) |
188 | { | |
90a3d6d8 | 189 | ob_end_clean(); |
91bd1d6e DO |
190 | # prevent message appearing in foreign tab |
191 | if (isset ($_SESSION['log'])) | |
192 | unset ($_SESSION['log']); | |
c5dfde62 | 193 | printException ($e); |
90a3d6d8 | 194 | } |
e410ebfc | 195 | ?> |