IP addresses inherit tags from their parent networks (#375)
[racktables] / wwwroot / inc / exceptions.php
1 <?php
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
7 // The default approach is to treat an error as fatal, in which case
8 // some message is output and the user is left there. Inheriting classes
9 // represent more specific cases, some of which can be handled in a
10 // "softer" way (see below).
11 class RackTablesError extends Exception
12 {
13 const INTERNAL = 2;
14 const DB_WRITE_FAILED = 3;
15 const NOT_AUTHENTICATED = 4;
16 const MISCONFIGURED = 6;
17 protected final function genHTMLPage ($title, $text)
18 {
19 global $helpdesk_banner;
20 header ('Content-Type: text/html; charset=UTF-8');
21 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n";
22 echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'."\n";
23 echo "<head><title>${title}</title>";
24 echo "</head><body>${text}";
25 if (isset ($helpdesk_banner))
26 echo '<hr>' . $helpdesk_banner;
27 echo '</body></html>';
28 }
29 public function dispatch()
30 {
31 $msgheader = array
32 (
33 self::NOT_AUTHENTICATED => 'Not authenticated',
34 self::MISCONFIGURED => 'Configuration error',
35 self::INTERNAL => 'Internal error',
36 self::DB_WRITE_FAILED => 'Database write failed',
37 );
38 $msgbody = array
39 (
40 self::NOT_AUTHENTICATED => '<h2>This system requires authentication. You should use a username and a password.</h2>',
41 self::MISCONFIGURED => '<h2>Configuration error</h2><br>' . $this->message,
42 self::INTERNAL => '<h2>Internal error</h2><br>' . $this->message,
43 self::DB_WRITE_FAILED => '<h2>Database write failed</h2><br>' . $this->message,
44 );
45 switch ($this->code)
46 {
47 case self::NOT_AUTHENTICATED:
48 header ('WWW-Authenticate: Basic realm="' . getConfigVar ('enterprise') . ' RackTables access"');
49 header ("HTTP/1.1 401 Unauthorized");
50 case self::MISCONFIGURED:
51 case self::INTERNAL:
52 case self::DB_WRITE_FAILED:
53 $this->genHTMLPage ($msgheader[$this->code], $msgbody[$this->code]);
54 break;
55 default:
56 throw new RackTablesError ('Dispatching error, unknown code ' . $this->code, RackTablesError::INTERNAL);
57 }
58 }
59 }
60
61 // this simplifies construction of RackTablesError, but is never caught
62 class EntityNotFoundException extends RackTablesError
63 {
64 function __construct($entity, $id)
65 {
66 parent::__construct ("Object '$entity'#'$id' does not exist");
67 }
68 public function dispatch()
69 {
70 RackTablesError::genHTMLPage ('Missing record', "<h2>Missing record</h2><br>" . $this->message);
71 }
72 }
73
74 // this simplifies construction of RackTablesError, but is never caught
75 class InvalidArgException extends RackTablesError
76 {
77 function __construct ($name, $value, $reason=NULL)
78 {
79 $message = "Argument '${name}' of value '".var_export($value,true)."' is invalid.";
80 if (!is_null($reason))
81 $message .= ' ('.$reason.')';
82 parent::__construct ($message, parent::INTERNAL);
83 }
84 }
85
86 // this simplifies construction and helps in catching "soft"
87 // errors (invalid input from the user)
88 class InvalidRequestArgException extends InvalidArgException
89 {
90 public function dispatch()
91 {
92 RackTablesError::genHTMLPage ('Assertion failed', '<h2>Assertion failed</h2><br>' . $this->message);
93 }
94 }
95
96 // this wraps certain known PDO errors and is caught in process.php
97 // as a "soft" error
98 class RTDatabaseError extends RackTablesError
99 {
100 public function dispatch()
101 {
102 RackTablesError::genHTMLPage ('Database soft error', '<h2>Database soft error</h2><br>' . $this->message);
103 }
104 }
105
106 // gateway failure is a common case of a "soft" error, some functions do catch this
107 class RTGatewayError extends RackTablesError
108 {
109 public function dispatch()
110 {
111 RackTablesError::genHTMLPage ('Gateway error', '<h2>Gateway error</h2><br>' . $this->message);
112 }
113 }
114
115 # "Permission denied" is a very common case, which in some situations is
116 # treated as a "soft" error.
117 class RTPermissionDenied extends RackTablesError
118 {
119 public function dispatch()
120 {
121 header ('Content-Type: text/html; charset=UTF-8');
122 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n";
123 echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'."\n";
124 echo "<head><title>RackTables: access denied</title>\n";
125 printPageHeaders();
126 echo "</head><body>";
127 global $pageno, $tabno,
128 $user_given_tags,
129 $target_given_tags,
130 $auto_tags,
131 $expl_tags,
132 $impl_tags;
133 echo "<table border=1 cellspacing=0 cellpadding=3 width='50%' align=center>\n";
134 echo '<tr><th colspan=2><h3>' . getImageHREF ('DENIED') . ' access denied ';
135 echo getImageHREF ('DENIED') . '</h3></th></tr>';
136 echo "<tr><th width='50%' class=tagchain>User given tags:</th><td class=tagchain>";
137 echo serializeTags ($user_given_tags) . "&nbsp;</td></tr>\n";
138 echo "<tr><th width='50%' class=tagchain>Target given tags:</th><td class=tagchain>";
139 echo serializeTags ($target_given_tags) . "&nbsp;</td></tr>\n";
140 echo "<tr><th width='50%' class=tagchain>Effective explicit tags:</th><td class=tagchain>";
141 echo serializeTags ($expl_tags) . "&nbsp;</td></tr>\n";
142 echo "<tr><th width='50%' class=tagchain>Effective implicit tags:</th><td class=tagchain>";
143 echo serializeTags ($impl_tags) . "&nbsp;</td></tr>\n";
144 echo "<tr><th width='50%' class=tagchain>Automatic tags:</th><td class=tagchain>";
145 echo serializeTags ($auto_tags) . "&nbsp;</td></tr>\n";
146 echo "<tr><th width='50%' class=tdright>Requested page:</th><td class=tdleft>${pageno}</td></tr>\n";
147 echo "<tr><th width='50%' class=tdright>Requested tab:</th><td class=tdleft>${tabno}</td></tr>\n";
148 echo "<tr><td colspan=2 align=center>Click <a href='index.php?logout'>here</a> to logout.</td></tr>\n";
149 echo "</table>\n";
150 echo "</body></html>";
151 }
152 }
153
154 class RackCodeError extends RackTablesError
155 {
156 protected $lineno;
157 function __construct ($message, $lineno = 'unknown')
158 {
159 # RackCodeError without a catch-block is very likely an internal error
160 parent::__construct ($message, self::INTERNAL);
161 $this->lineno = $lineno;
162 }
163 public function dispatch()
164 {
165 parent::genHTMLPage ('RackCode error', '<h2>RackCode error on line ' . $this->lineno . '</h2><br>' . $this->message);
166 }
167 }
168
169 function dumpArray($arr)
170 {
171 echo '<table class="exceptionParametersDump">';
172 foreach($arr as $key=>$value)
173 {
174 echo "<tr><th>$key</th><td>$value</td></tr>";
175 }
176 echo '</table>';
177 }
178
179 function stringTrace($trace)
180 {
181 $ret = '';
182 foreach($trace as $line) {
183 if (isset ($line['file']) && isset ($line['line']))
184 $ret .= $line['file'].':'.$line['line'].' ';
185 $ret .= $line['function'].'(';
186 $f = true;
187 if (isset($line['args']) and is_array($line['args'])) foreach ($line['args'] as $arg) {
188 if (!$f) $ret .= ', ';
189 if (is_string($arg))
190 $printarg = "'".$arg."'";
191 elseif (is_null($arg))
192 $printarg = 'NULL';
193 elseif (is_array($arg))
194 $printarg = print_r($arg, 1);
195 else
196 $printarg = $arg;
197 $ret .= $printarg;
198 $f = false;
199 }
200 $ret .= ")\n";
201 }
202 return $ret;
203 }
204
205 function printPDOException($e)
206 {
207 header("HTTP/1.1 500 Internal Server Error");
208 header ('Content-Type: text/html; charset=UTF-8');
209 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n";
210 echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'."\n";
211 echo "<head><title> PDO Exception </title>\n";
212 echo "<link rel=stylesheet type='text/css' href='?module=chrome&uri=css/pi.css' />\n";
213 echo "<link rel=icon href='?module=chrome&uri=pix/favicon.ico' type='image/x-icon' />\n";
214 echo '</head> <body>';
215 echo '<h2>Pdo exception: '.get_class($e).'</h2><code>'.$e->getMessage().'</code> (<code>'.$e->getCode().'</code>)';
216 echo '<p>at file <code>'.$e->getFile().'</code>, line <code>'.$e->getLine().'</code></p><pre>';
217 echo stringTrace($e->getTrace());
218 echo '</pre>';
219 echo '<h2>Error info:</h2>';
220 echo '<pre>';
221 print_r($e->errorInfo);
222 echo '</pre>';
223 echo '<h2>Parameters:</h2>';
224 echo '<h3>GET</h3>';
225 dumpArray($_GET);
226 echo '<h3>POST</h3>';
227 dumpArray($_POST);
228 echo '<h3>COOKIE</h3>';
229 dumpArray($_COOKIE);
230 echo '</body></html>';
231
232 }
233
234 function printGenericException($e)
235 {
236 header("HTTP/1.1 500 Internal Server Error");
237 header ('Content-Type: text/html; charset=UTF-8');
238 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n";
239 echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'."\n";
240 echo "<head><title> Exception </title>\n";
241 echo "<link rel=stylesheet type='text/css' href='?module=chrome&uri=css/pi.css' />\n";
242 echo "<link rel=icon href='?module=chrome&uri=pix/favicon.ico' type='image/x-icon' />\n";
243 echo '</head> <body>';
244 echo '<h2>Uncaught exception: '.get_class($e).'</h2><code>'.$e->getMessage().'</code> (<code>'.$e->getCode().'</code>)';
245 echo '<p>at file <code>'.$e->getFile().'</code>, line <code>'.$e->getLine().'</code></p><pre>';
246 echo stringTrace($e->getTrace());
247 echo '</pre>';
248 echo '<h2>Parameters:</h2>';
249 echo '<h3>GET</h3>';
250 dumpArray($_GET);
251 echo '<h3>POST</h3>';
252 dumpArray($_POST);
253 echo '<h3>COOKIE</h3>';
254 dumpArray($_COOKIE);
255 echo '</body></html>';
256
257 }
258
259 function printException($e)
260 {
261 if ($e instanceof RackTablesError)
262 $e->dispatch();
263 elseif ($e instanceof PDOException)
264 printPDOException($e);
265 else
266 printGenericException($e);
267 }
268
269 ?>