1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Ajde_Exception_Handler extends Ajde_Object_Static |
4
|
|
|
{ |
5
|
|
|
public static function __bootstrap() |
6
|
|
|
{ |
7
|
|
|
// making xdebug.overload_var_dump = 1 work |
8
|
|
|
//if (Config::get('debug')) { |
9
|
|
|
ini_set('html_errors', 1); |
10
|
|
|
//} |
11
|
|
|
// TODO: why is this defined here? also in index.php! |
12
|
|
|
set_error_handler(['Ajde_Exception_Handler', 'errorHandler']); |
13
|
|
|
set_exception_handler(['Ajde_Exception_Handler', 'handler']); |
14
|
|
|
|
15
|
|
|
return true; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public static function errorHandler($errno, $errstr, $errfile, $errline) |
19
|
|
|
{ |
20
|
|
|
if (error_reporting() > 0) { |
21
|
|
|
$message = sprintf("PHP error: %s in %s on line %s", $errstr, $errfile, $errline); |
22
|
|
|
error_log($message); |
23
|
|
|
dump($message); |
24
|
|
|
|
25
|
|
|
// TODO: only possible in PHP >= 5.3 ? |
26
|
|
|
// try |
27
|
|
|
// { |
28
|
|
|
throw new ErrorException($errstr, 0, $errno, $errfile, $errline); |
29
|
|
|
// } catch(Exception $exception) { |
30
|
|
|
// } |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static function handler(Exception $exception) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
try { |
37
|
|
|
if (Config::getInstance()->debug === true) { |
|
|
|
|
38
|
|
|
if (!((get_class($exception) == 'Ajde_Exception' || is_subclass_of($exception, |
39
|
|
|
'Ajde_Exception')) && !$exception->traceOnOutput()) |
|
|
|
|
40
|
|
|
) { |
41
|
|
|
Ajde_Exception_Log::logException($exception); |
42
|
|
|
echo self::trace($exception); |
43
|
|
|
} else { |
44
|
|
|
Ajde_Exception_Log::logException($exception); |
45
|
|
|
Ajde_Http_Response::redirectServerError(); |
46
|
|
|
} |
47
|
|
|
} else { |
48
|
|
|
Ajde_Exception_Log::logException($exception); |
49
|
|
|
Ajde_Http_Response::redirectServerError(); |
50
|
|
|
} |
51
|
|
|
} catch (Exception $exception) { |
52
|
|
|
error_log(self::trace($exception, self::EXCEPTION_TRACE_LOG)); |
53
|
|
|
die("An uncatched exception occured within the error handler, see the server error_log for details"); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
const EXCEPTION_TRACE_HTML = 1; |
58
|
|
|
const EXCEPTION_TRACE_ONLY = 3; |
59
|
|
|
const EXCEPTION_TRACE_LOG = 2; |
60
|
|
|
|
61
|
|
|
public static function trace(Exception $exception, $output = self::EXCEPTION_TRACE_HTML) |
62
|
|
|
{ |
63
|
|
|
$simpleJsonTrace = false; |
64
|
|
|
if ($simpleJsonTrace && Ajde::app()->hasDocument() && Ajde::app()->getDocument()->getFormat() == 'json') { |
|
|
|
|
65
|
|
|
$output = self::EXCEPTION_TRACE_LOG; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$type = self::getTypeDescription($exception); |
69
|
|
|
|
70
|
|
|
switch ($output) { |
71
|
|
|
case self::EXCEPTION_TRACE_HTML: |
72
|
|
|
if (ob_get_level()) { |
73
|
|
|
ob_clean(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$traceMessage = '<ol reversed="reversed">'; |
77
|
|
|
self::$firstApplicationFileExpanded = false; |
78
|
|
|
foreach ($exception->getTrace() as $item) { |
79
|
|
|
$arguments = null; |
80
|
|
|
if (!empty($item['args'])) { |
81
|
|
|
ob_start(); |
82
|
|
|
var_dump($item['args']); |
|
|
|
|
83
|
|
|
$dump = ob_get_clean(); |
84
|
|
|
$arguments = sprintf(' with arguments: %s', $dump); |
85
|
|
|
} |
86
|
|
|
$traceMessage .= sprintf("<li><code><em>%s</em>%s<strong>%s</strong></code><br/>in %s<br/> \n", |
87
|
|
|
!empty($item['class']) ? $item['class'] : '<unknown class>', |
88
|
|
|
!empty($item['type']) ? $item['type'] : '::', |
89
|
|
|
!empty($item['function']) ? $item['function'] : '<unknown function>', |
90
|
|
|
self::embedScript( |
91
|
|
|
issetor($item['file'], null), |
92
|
|
|
issetor($item['line'], null), |
93
|
|
|
$arguments, |
94
|
|
|
false |
95
|
|
|
)); |
96
|
|
|
$traceMessage .= '</li>'; |
97
|
|
|
} |
98
|
|
|
$traceMessage .= '</ol>'; |
99
|
|
|
|
100
|
|
|
$exceptionDocumentation = ''; |
101
|
|
|
if ($exception instanceof Ajde_Exception && $exception->getCode()) { |
102
|
|
|
$exceptionDocumentation = sprintf("<div style='margin-top: 4px;'><img src='" . Config::get('site_root') . MEDIA_URI . "_core/globe_16.png' style='vertical-align: bottom;' title='Primary key' width='16' height='16' /> <a href='%s'>Documentation on error %s</a> </div>", |
103
|
|
|
Ajde_Core_Documentation::getUrl($exception->getCode()), |
104
|
|
|
$exception->getCode() |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$exceptionMessage = sprintf("<summary style='background-image: url(\"" . Config::get('site_root') . MEDIA_URI . "_core/warning_48.png\");'><h3 style='margin:0;'>%s:</h3><h2 style='margin:0;'>%s</h2> Exception thrown in %s%s</summary><h3>Trace:</h3>\n", |
109
|
|
|
$type, |
110
|
|
|
$exception->getMessage(), |
111
|
|
|
self::embedScript( |
112
|
|
|
$exception->getFile(), |
113
|
|
|
$exception->getLine(), |
114
|
|
|
$arguments, |
|
|
|
|
115
|
|
|
false //!self::$firstApplicationFileExpanded |
116
|
|
|
), |
117
|
|
|
$exceptionDocumentation |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$exceptionDump = ''; |
121
|
|
|
if (class_exists(Ajde_Dump::class)) { |
122
|
|
|
if ($dumps = Ajde_Dump::getAll()) { |
123
|
|
|
$exceptionDump .= '<h2>Dumps</h2>'; |
124
|
|
|
foreach ($dumps as $source => $dump) { |
125
|
|
|
ob_start(); |
126
|
|
|
echo $source; |
127
|
|
|
if (class_exists(Kint::class)) { |
128
|
|
|
Kint::dump($dump[0]); |
129
|
|
|
} else { |
130
|
|
|
echo '<pre>'; |
131
|
|
|
var_dump($dump[0]); |
132
|
|
|
echo '</pre>'; |
133
|
|
|
} |
134
|
|
|
$exceptionDump .= ob_get_clean() . '<h2>Error message</h2>'; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$style = false; |
140
|
|
|
if (file_exists(CORE_DIR . MODULE_DIR . '_core/res/css/debugger/handler.css')) { |
141
|
|
|
$style = file_get_contents(CORE_DIR . MODULE_DIR . '_core/res/css/debugger/handler.css'); |
142
|
|
|
} |
143
|
|
|
if ($style === false) { |
144
|
|
|
// For shutdown() call |
145
|
|
|
$style = 'body {font: 13px sans-serif;} a {color: #005D9A;} a:hover {color: #9A0092;} h2 {color: #005D9A;} span > a {color: #9A0092;}'; |
146
|
|
|
} |
147
|
|
|
$style = '<style>' . $style . '</style>'; |
148
|
|
|
$script = '<script>document.getElementsByTagName("base")[0].href="";</script>'; |
149
|
|
|
|
150
|
|
|
if (Ajde::app()->getRequest()->isAjax()) { |
151
|
|
|
$collapsed = $exceptionDump . $exceptionMessage . $traceMessage; |
152
|
|
|
$header = ''; |
153
|
|
|
} else { |
154
|
|
|
$collapsed = '<div id="details">' . $exceptionDump . $exceptionMessage . $traceMessage . '</div>'; |
155
|
|
|
$header = '<header><h1><img src="' . Config::get('site_root') . MEDIA_URI . 'ajde-small.png">Something went wrong</h1><a href="javascript:history.go(-1);">Go back</a> <a href="#details">Show details</a></header>'; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$message = $style . $script . $header . $collapsed; |
159
|
|
|
break; |
160
|
|
|
case self::EXCEPTION_TRACE_ONLY: |
161
|
|
|
$message = ''; |
162
|
|
View Code Duplication |
foreach (array_reverse($exception->getTrace()) as $i => $line) { |
|
|
|
|
163
|
|
|
$message .= $i . '. ' . (isset($line['file']) ? $line['file'] : 'unknown file') . ' on line ' . (isset($line['line']) ? $line['line'] : 'unknown line'); |
164
|
|
|
$message .= PHP_EOL; |
165
|
|
|
} |
166
|
|
|
break; |
167
|
|
|
case self::EXCEPTION_TRACE_LOG: |
168
|
|
|
$message = 'UNCAUGHT EXCEPTION' . PHP_EOL; |
169
|
|
|
$message .= "\tRequest " . $_SERVER["REQUEST_URI"] . " triggered:" . PHP_EOL; |
170
|
|
|
$message .= sprintf("\t%s: %s in %s on line %s", |
171
|
|
|
$type, |
172
|
|
|
$exception->getMessage(), |
173
|
|
|
$exception->getFile(), |
174
|
|
|
$exception->getLine() |
175
|
|
|
); |
176
|
|
View Code Duplication |
foreach (array_reverse($exception->getTrace()) as $i => $line) { |
|
|
|
|
177
|
|
|
$message .= PHP_EOL; |
178
|
|
|
$message .= "\t" . $i . '. ' . (isset($line['file']) ? $line['file'] : 'unknown file') . ' on line ' . (isset($line['line']) ? $line['line'] : 'unknown line'); |
179
|
|
|
} |
180
|
|
|
break; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $message; |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public static function getTypeDescription(Exception $exception) |
187
|
|
|
{ |
188
|
|
|
if ($exception instanceof ErrorException) { |
189
|
|
|
$type = "PHP Error " . self::getErrorType($exception->getSeverity()); |
190
|
|
|
} elseif ($exception instanceof Ajde_Exception) { |
191
|
|
|
$type = "Application exception" . ($exception->getCode() ? ' ' . $exception->getCode() : ''); |
192
|
|
|
} else { |
193
|
|
|
$type = "PHP exception " . $exception->getCode(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $type; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
View Code Duplication |
public static function getExceptionChannelMap(Exception $exception) |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
if ($exception instanceof ErrorException) { |
202
|
|
|
return Ajde_Log::CHANNEL_ERROR; |
203
|
|
|
} elseif ($exception instanceof Ajde_Core_Exception_Routing) { |
204
|
|
|
return Ajde_Log::CHANNEL_ROUTING; |
205
|
|
|
} elseif ($exception instanceof Ajde_Core_Exception_Security) { |
206
|
|
|
return Ajde_Log::CHANNEL_SECURITY; |
207
|
|
|
} elseif ($exception instanceof Ajde_Exception) { |
208
|
|
|
return Ajde_Log::CHANNEL_APPLICATION; |
209
|
|
|
} else { |
210
|
|
|
return Ajde_Log::CHANNEL_EXCEPTION; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
View Code Duplication |
public static function getExceptionLevelMap(Exception $exception) |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
if ($exception instanceof ErrorException) { |
217
|
|
|
return Ajde_Log::LEVEL_ERROR; |
218
|
|
|
} elseif ($exception instanceof Ajde_Core_Exception_Routing) { |
219
|
|
|
return Ajde_Log::LEVEL_WARNING; |
220
|
|
|
} elseif ($exception instanceof Ajde_Core_Exception_Security) { |
221
|
|
|
return Ajde_Log::LEVEL_WARNING; |
222
|
|
|
} elseif ($exception instanceof Ajde_Exception) { |
223
|
|
|
return Ajde_Log::LEVEL_ERROR; |
224
|
|
|
} else { |
225
|
|
|
return Ajde_Log::LEVEL_ERROR; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public static function getErrorType($type) |
230
|
|
|
{ |
231
|
|
|
switch ($type) { |
232
|
|
|
case 1: |
233
|
|
|
return "E_ERROR"; |
234
|
|
|
case 2: |
235
|
|
|
return "E_WARNING"; |
236
|
|
|
case 4: |
237
|
|
|
return "E_PARSE"; |
238
|
|
|
case 8: |
239
|
|
|
return "E_NOTICE"; |
240
|
|
|
case 16: |
241
|
|
|
return "E_CORE_ERROR"; |
242
|
|
|
case 32: |
243
|
|
|
return "E_CORE_WARNING"; |
244
|
|
|
case 64: |
245
|
|
|
return "E_COMPILE_ERROR"; |
246
|
|
|
case 128: |
247
|
|
|
return "E_COMPILE_WARNING"; |
248
|
|
|
case 256: |
249
|
|
|
return "E_USER_ERROR"; |
250
|
|
|
case 512: |
251
|
|
|
return "E_USER_WARNING"; |
252
|
|
|
case 1024: |
253
|
|
|
return "E_USER_NOTICE"; |
254
|
|
|
case 2048: |
255
|
|
|
return "E_STRICT"; |
256
|
|
|
case 4096: |
257
|
|
|
return "E_RECOVERABLE_ERROR"; |
258
|
|
|
case 8192: |
259
|
|
|
return "E_DEPRECATED"; |
260
|
|
|
case 16384: |
261
|
|
|
return "E_USER_DEPRECATED"; |
262
|
|
|
case 30719: |
263
|
|
|
return "E_ALL"; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
static $firstApplicationFileExpanded = false; |
268
|
|
|
|
269
|
|
|
protected static function embedScript($filename = null, $line = null, $arguments = null, $expand = false) |
270
|
|
|
{ |
271
|
|
|
$lineOffset = 5; |
272
|
|
|
$file = ''; |
273
|
|
|
|
274
|
|
|
// in case of eval, filename looks like File.php(30) : eval()'d code |
275
|
|
|
if (substr_count($filename, '(')) { |
276
|
|
|
list($filename) = explode('(', $filename); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
if (isset($filename) && isset($line)) { |
280
|
|
|
$lines = file($filename); |
281
|
|
|
for ($i = max(0, $line - $lineOffset - 1); $i < min($line + $lineOffset, count($lines)); $i++) { |
282
|
|
|
$lineNumber = str_repeat(" ", 4 - strlen($i + 1)) . ($i + 1); |
283
|
|
|
if ($i == $line - 1) { |
284
|
|
|
$file .= "{{{}}}" . $lineNumber . ' ' . ($lines[$i]) . "{{{/}}}"; |
285
|
|
|
} else { |
286
|
|
|
$file .= $lineNumber . ' ' . ($lines[$i]); |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
if (substr_count($filename, str_replace('/', DIRECTORY_SEPARATOR, APP_DIR))) { |
292
|
|
|
$filename = '<span style="color: red;">' . $filename . '</span>'; |
293
|
|
|
if (self::$firstApplicationFileExpanded === false) { |
294
|
|
|
$expand = true; |
295
|
|
|
} |
296
|
|
|
self::$firstApplicationFileExpanded = true; |
297
|
|
|
} |
298
|
|
|
$file = highlight_string("<?php" . $file, true); |
299
|
|
|
$file = str_replace('<?php', '', $file); |
300
|
|
|
$file = str_replace('<code>', |
301
|
|
|
"<code style='display:block;border:1px solid silver;margin:5px 0 5px 0;background-color:#f1f1f1;'>", $file); |
302
|
|
|
$file = str_replace('{{{}}}', "<div style='background-color: #ffff9e;'>", $file); |
303
|
|
|
$file = str_replace('{{{/}}}', "</div>", $file); |
304
|
|
|
|
305
|
|
|
$id = md5(microtime() . $filename . $line); |
306
|
|
|
|
307
|
|
|
return sprintf( |
308
|
|
|
"<a |
309
|
|
|
onclick='document.getElementById(\"$id\").style.display = document.getElementById(\"$id\").style.display == \"block\" ? \"none\" : \"block\";' |
310
|
|
|
href='javascript:void(0);' |
311
|
|
|
><i>%s</i> on line <b>%s</b></a> <div id='$id' style='display:%s;'>%s%s</div>", |
312
|
|
|
$filename, |
313
|
|
|
$line, |
314
|
|
|
$expand ? "block" : "none", |
315
|
|
|
$file, |
316
|
|
|
$arguments |
317
|
|
|
); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
} |
321
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.