0926991a19d4c4086fd8034a2011c4e2daf479f9
[racktables] / inc / exceptions.php
1 <?php
2
3 class EntityNotFoundException extends Exception {
4 private $entity;
5 private $id;
6 function __construct($entity, $id)
7 {
8 parent::__construct ("Object '$entity'#'$id' does not exist");
9 $this->entity = $entity;
10 $this->id = $id;
11 }
12 function getEntity()
13 {
14 return $this->entity;
15 }
16 function getId()
17 {
18 return $this->id;
19 }
20 }
21
22 class RealmNotFoundException extends Exception {
23 private $realm;
24 function __construct($realm)
25 {
26 parent::__construct ("Realm '$realm' does not exist");
27 $this->realm = $realm;
28 }
29 function getRealm()
30 {
31 return $this->realm;
32 }
33 }
34
35 class NotUniqueException extends Exception
36 {
37 private $subject;
38 function __construct ($what = NULL)
39 {
40 $this->subject = $what;
41 parent::__construct ('Cannot add duplicate record' . ($what === NULL ? '' : " (${what} must be unique)"));
42 }
43 function getSubject()
44 {
45 return $this->subject;
46 }
47 }
48
49 class InvalidArgException extends Exception
50 {
51 private $name;
52 private $value;
53 private $reason;
54 function __construct ($name, $value, $reason=NULL)
55 {
56 $message = "Argument '${name}' of value '".var_export($value,true)."' is invalid.";
57 if (!is_null($reason)) {
58 $message .= ' ('.$reason.')';
59 }
60 parent::__construct ($message);
61 $this->name = $name;
62 $this->value = $value;
63 }
64 function getName()
65 {
66 return $this->name;
67 }
68 function getValue()
69 {
70 return $this->value;
71 }
72 }
73
74 class InvalidRequestArgException extends Exception
75 {
76 private $name;
77 private $value;
78 private $reason;
79 function __construct ($name, $value, $reason=NULL)
80 {
81 $message = "Request parameter '${name}' of value '".var_export($value,true)."' is invalid.";
82 if (!is_null($reason)) {
83 $message .= ' ('.$reason.')';
84 }
85 parent::__construct ($message);
86 $this->name = $name;
87 $this->value = $value;
88 }
89 function getName()
90 {
91 return $this->name;
92 }
93 function getValue()
94 {
95 return $this->value;
96 }
97 }
98
99 function dumpArray($arr)
100 {
101 echo '<table class="exceptionParametersDump">';
102 foreach($arr as $key=>$value)
103 {
104 echo "<tr><th>$key</th><td>$value</td></tr>";
105 }
106 echo '</table>';
107 }
108
109 function stringTrace($trace)
110 {
111 $ret = '';
112 foreach($trace as $line) {
113 $ret .= $line['file'].':'.$line['line'].' '.$line['function'].'(';
114 $f = true;
115 if (isset($line['args']) and is_array($line['args'])) foreach ($line['args'] as $arg) {
116 if (!$f) $ret .= ', ';
117 if (is_string($arg))
118 $printarg = "'".$arg."'";
119 elseif (is_null($arg))
120 $printarg = 'NULL';
121 elseif (is_array($arg))
122 $printarg = print_r($arg, 1);
123 else
124 $printarg = $arg;
125 $ret .= $printarg;
126 $f = false;
127 }
128 $ret .= ")\n";
129 }
130 return $ret;
131 }
132
133 function print404($e)
134 {
135 header("HTTP/1.1 404 Not Found");
136 header ('Content-Type: text/html; charset=UTF-8');
137 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n";
138 echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'."\n";
139 echo "<head><title> Exception </title>\n";
140 printPageHeaders();
141 echo '</head> <body>';
142 echo '<h2>Object: '.$e->getEntity().'#'.$e->getId().' not found</h2>';
143 echo '</body></html>';
144
145 }
146
147 function printPDOException($e)
148 {
149 header("HTTP/1.1 500 Internal Server Error");
150 header ('Content-Type: text/html; charset=UTF-8');
151 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n";
152 echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'."\n";
153 echo "<head><title> PDO Exception </title>\n";
154 printPageHeaders();
155 echo '</head> <body>';
156 echo '<h2>Pdo exception: '.get_class($e).'</h2><code>'.$e->getMessage().'</code> (<code>'.$e->getCode().'</code>)';
157 echo '<p>at file <code>'.$e->getFile().'</code>, line <code>'.$e->getLine().'</code></p><pre>';
158 echo stringTrace($e->getTrace());
159 echo '</pre>';
160 echo '<h2>Error info:</h2>';
161 echo '<pre>';
162 print_r($e->errorInfo);
163 echo '</pre>';
164 echo '<h2>Parameters:</h2>';
165 echo '<h3>GET</h3>';
166 dumpArray($_GET);
167 echo '<h3>POST</h3>';
168 dumpArray($_POST);
169 echo '<h3>COOKIE</h3>';
170 dumpArray($_COOKIE);
171 echo '</body></html>';
172
173 }
174
175 function printGenericException($e)
176 {
177 header("HTTP/1.1 500 Internal Server Error");
178 header ('Content-Type: text/html; charset=UTF-8');
179 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n";
180 echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'."\n";
181 echo "<head><title> Exception </title>\n";
182 echo "<link rel=stylesheet type='text/css' href=css/pi.css />\n";
183 echo "<link rel=icon href='pix/racktables.ico' type='image/x-icon' />";
184 echo '</head> <body>';
185 echo '<h2>Uncaught exception: '.get_class($e).'</h2><code>'.$e->getMessage().'</code> (<code>'.$e->getCode().'</code>)';
186 echo '<p>at file <code>'.$e->getFile().'</code>, line <code>'.$e->getLine().'</code></p><pre>';
187 echo stringTrace($e->getTrace());
188 echo '</pre>';
189 echo '<h2>Parameters:</h2>';
190 echo '<h3>GET</h3>';
191 dumpArray($_GET);
192 echo '<h3>POST</h3>';
193 dumpArray($_POST);
194 echo '<h3>COOKIE</h3>';
195 dumpArray($_COOKIE);
196 echo '</body></html>';
197
198 }
199
200 function printException($e)
201 {
202 if (get_class ($e) == 'Exception')
203 switch ($e->getCode())
204 {
205 case E_NOT_AUTHENTICATED:
206 header ('WWW-Authenticate: Basic realm="' . getConfigVar ('enterprise') . ' RackTables access"');
207 header ("HTTP/1.1 401 Unauthorized");
208 case E_MISCONFIGURED:
209 header ('Content-Type: text/html; charset=UTF-8');
210 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n";
211 echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'."\n";
212 $msgheader = array
213 (
214 E_NOT_AUTHENTICATED => 'Not authenticated',
215 E_MISCONFIGURED => 'Configuration error',
216 );
217 $msgbody = array
218 (
219 E_NOT_AUTHENTICATED => '<h2>This system requires authentication. You should use a username and a password.</h2>',
220 E_MISCONFIGURED => '<h2>Configuration error</h2><br>' . $e->getMessage(),
221 );
222 echo '<head><title>' . $msgheader[$e->getCode()] . '</title>';
223 echo '</head><body>' . $msgbody[$e->getCode()] . '</body></html>';
224 return;
225 default:
226 }
227 if (get_class($e) == 'EntityNotFoundException')
228 print404($e);
229 elseif (get_class($e) == 'PDOException')
230 printPDOException($e);
231 else
232 printGenericException($e);
233 }
234
235 ?>