forked from forkcms/library
/
exception.php
434 lines (395 loc) · 17.7 KB
/
exception.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php
/**
* Spoon Library
*
* This source file is part of the Spoon Library. More information,
* documentation and tutorials can be found @ http://www.spoon-library.com
*
* @package spoon
* @subpackage exception
*
*
* @author Davy Hellemans <davy@spoon-library.com>
* @since 0.1.1
*/
/**
* This is the default spoon exception, that extends the default php exception
*
* @package spoon
* @subpackage exception
*
*
* @author Davy Hellemans <davy@spoon-library.com>
* @author Dieter Vanden Eynde <dieter@dieterve.be>
* @since 0.1.1
*/
class SpoonException extends Exception
{
/**
* Exception name
*
* @var string
*/
protected $name;
/**
* String to obfuscate
*
* @var array
*/
protected $obfuscate = [];
/**
* Class constructor.
*
* @param string $message The message that should be used.
* @param int[optional] $code A numeric code for the exceptions.
* @param mixed[optional] $obfuscate The string(s) that will be obfuscated.
*/
public function __construct($message, $code = 0, $obfuscate = null)
{
// parent constructor
parent::__construct((string) $message, (int) $code);
// set name
$this->name = get_class($this);
// obfuscating?
if ($obfuscate !== null) {
$this->obfuscate = (array) $obfuscate;
}
}
/**
* Retrieve the name of this exception.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Return an array of elements that need to be obfuscated.
*
* @return array
*/
public function getObfuscate()
{
return $this->obfuscate;
}
}
// Redefine the exception handler if we are not running in the command line.
if (!Spoon::inCli()) {
set_exception_handler('exceptionHandler');
}
/**
* Prints out the thrown exception in a more readable manner for a person using
* a web browser.
*
* @param SpoonException $exception
*/
function exceptionHandler($exception)
{
// fetch trace stack
$trace = $exception->getTrace();
// specific name
$name = (method_exists($exception, 'getName')) ? $exception->getName() : get_class($exception);
// spoon type exception
if (method_exists($exception, 'getName') && strtolower(substr($exception->getName(), 0, 5)) == 'spoon' && $exception->getCode() != 0) {
$documentation = '» <a href="http://www.spoon-library.com/exceptions/detail/' . $exception->getCode() . '">view documentation</a>';
}
// request uri?
if (!isset($_SERVER['HTTP_HOST'])) {
$_SERVER['HTTP_HOST'] = '';
}
if (!isset($_SERVER['REQUEST_URI'])) {
$_SERVER['REQUEST_URI'] = '';
}
if (!isset($_SERVER['REQUEST_METHOD'])) {
$_SERVER['REQUEST_METHOD'] = '';
}
// user agent
$userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '<i>(Unknown)</i>';
// custom callback?
if (Spoon::getExceptionCallback() != '') {
// function
if (!strpos(Spoon::getExceptionCallback(), '::')) {
// function actually has been defined
if (function_exists(Spoon::getExceptionCallback())) {
call_user_func_array(Spoon::getExceptionCallback(), [$exception, getOutput($exception)]);
} // something went wrong
else {
exit('The exception callback function (' . Spoon::getExceptionCallback() . ') could not be found.');
}
} else {
// method
$method = explode('::', Spoon::getExceptionCallback());
// 2 parameters and exists
if (count($method) == 2 && is_callable([$method[0], $method[1]])) {
call_user_func_array([$method[0], $method[1]], [$exception, getOutput($exception)]);
} // something went wrong
else {
exit('The exception callback function (' . Spoon::getExceptionCallback() . ') cound not be found.');
}
}
} else {
// on CLI we have no use for 2000 lines long report, show short info
if (Spoon::inCli()) {
echo $name . "\n";
echo 'Message: ' . $exception->getMessage() . "\n";
echo 'File: ' . $exception->getFile() . "\n";
echo 'Line: ' . $exception->getLine() . "\n";
} // debugging enabled (show output)
elseif (Spoon::getDebug()) {
echo getOutput($exception);
} // debugging disabled
else {
echo Spoon::getDebugMessage();
}
}
// mail it?
if (Spoon::getDebugEmail() != '') {
// e-mail headers
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-15\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-MSMail-Priority: Normal\n";
$headers .= "X-Mailer: SpoonLibrary Webmail\n";
$headers .= "From: Spoon Library <no-reply@spoon-library.com>\n";
// send email
@mail(Spoon::getDebugEmail(), 'Exception Occured', getOutput($exception), $headers);
}
// stop script execution
exit;
}
function getOutput($exception)
{
// specific name
$name = (method_exists($exception, 'getName')) ? $exception->getName() : get_class($exception);
$userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '<i>(Unknown)</i>';
// generate output
$output = '
<html>
<head>
<title>' . $name . '</title>
</head>
<body style="background-color: #F2F2F2; color: #0000000, font-family: Verdana, Tahoma, Arial; font-size 10px; margin: 0; padding: 0;">
<table width="100%">
<tr>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;"> </td>
<td style="width: 550px">
<table width="550px;">
<tr>
<td style="background-color: #EEEEEE; border: 1px solid #B2B2B2;">
<h1 style="font-size: 12px; margin: 5px 5px 12px 5px; padding: 0 0 5px 0; color: #000000; font-family: Verdana, Tahoma, Arial; border-bottom: 1px solid #999999;">' . $name . ' » Main</h1>
<table width="550px">
<tr>
<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Message</th>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $exception->getMessage() . '</td>
</tr>
<tr>
<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">File</th>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . dimCommonPathPrefix($exception->getFile()) . '</td>
</tr>
<tr>
<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Line</th>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $exception->getLine() . '</td>
</tr>
<tr>
<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Date</th>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . date('r') . '</td>
</tr>
<tr>
<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">URL</th>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . "\n";
// request URL
$output .= ' <a href="http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '">http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '</a>' . "\n";
$output .= ' </td>
</tr>
<tr>
<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Referring URL</th>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . "\n";
// referring URL
if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != '') {
$output .= ' <a href="' . $_SERVER['HTTP_REFERER'] . '">' . $_SERVER['HTTP_REFERER'] . '</a>' . "\n";
} else {
$output .= ' <i>(Unknown)</i>' . "\n";
}
$output .= ' </td>
</tr>
<tr>
<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Request Method</th>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $_SERVER['REQUEST_METHOD'] . '</td>
</tr>
<tr>
<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">User-agent</th>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $userAgent . '</td>
</tr>';
// no documentation ?
if (isset($documentation)) {
$output .= ' <tr>
<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Documentation</th>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $documentation . '</td>
</tr>';
}
// we know about the last error
if (error_get_last() !== null) {
// define message
$error = error_get_last();
// show output
$output .= ' <tr>
<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">PHP error</th>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $error['message'] . '</td>
</tr>';
}
$output .= ' </table>
</td>
</tr>
<tr>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;"> </td>
</tr>
<!-- stack -->
<tr>
<td style="background-color: #EEEEEE; border: 1px solid #B2B2B2;">
<h1 style="font-size: 12px; margin: 5px 5px 12px 5px; padding: 0 0 5px 0; color: #000000; font-family: Verdana, Tahoma, Arial; border-bottom: 1px solid #999999;">' . $name . ' » Trace</h1>
<table width="550px;">' . "\n";
// trace has items
if (count($exception->getTrace()) != 0) {
// fetch entire stack
$entireTraceStack = $exception->getTrace();
// loop elements
foreach ($entireTraceStack as $traceStack) {
// open defintion list
$output .= ' <tr>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">
<table width="550px;" style="border-top: 1px dotted; padding-top: 1ex;">
<tr>
<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">File</th>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . ((isset($traceStack['file'])) ? dimCommonPathPrefix($traceStack['file']) : '<i>(Unknown)</i>') . '
</td>
</tr>
<tr>
<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Line</th>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . ((isset($traceStack['line'])) ? $traceStack['line'] : '<i>(Unknown)</i>') . '
</td>
</tr>';
// class & function
if (isset($traceStack['class'])) {
$docUrl = 'http://www.google.com/search?btnI=&ie=utf-8&sourceid=navclient&q=' . urlencode(sprintf('site:php.net/manual OR site:www.spoon-library.com/docs class "%s"', $traceStack['class']));
$link = sprintf('<a href="%s">%s</a>', htmlspecialchars($docUrl), htmlspecialchars($traceStack['class']));
$output .= ' <tr>
<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Class</th>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $link . '</td>
</tr>';
}
if (isset($traceStack['function'])) {
$docUrl = isset($traceStack['class'])
? 'http://www.google.com/search?btnI=&ie=utf-8&sourceid=navclient&q=' . urlencode(sprintf('site:php.net/manual OR site:www.spoon-library.com/docs "%s::%s"', $traceStack['class'], $traceStack['function']))
: 'http://www.google.com/search?btnI=&ie=utf-8&sourceid=navclient&q=' . urlencode(sprintf('site:php.net/manual OR site:www.spoon-library.com/docs "%s"', $traceStack['function']));
$link = sprintf('<a href="%s">%s</a>', htmlspecialchars($docUrl), htmlspecialchars($traceStack['function']));
$output .= ' <tr>
<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Function</th>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $link . '</td>
</tr>';
}
// close defintion list
$output .= ' </table>
</td>
</tr>';
}
} // no trace
else {
$output .= ' <tr>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">No trace available.</td>
</tr>';
}
// output the superglobal variables, if any
$hasVars = false;
foreach (['GET', 'POST', 'COOKIE', 'FILES'] as $superGlobal) {
$hasVars |= count($GLOBALS['_'. $superGlobal ]);
}
if ($hasVars) {
$output .= ' </table>
</td>
<tr>
<tr>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;"> </td>
</tr>
<!-- variables -->
<tr>
<td style="background-color: #EEEEEE; border: 1px solid #B2B2B2;">
<h1 style="font-size: 12px; margin: 5px 5px 12px 5px; padding: 0 0 5px 0; color: #000000; font-family: Verdana, Tahoma, Arial; border-bottom: 1px solid #999999;">' . $name . ' » Variables</h1>
<table width="550px;">' . "\n";
}
// continue output generation
$output .= ' </table>
</td>
</tr>
</table>
</td>
<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;"> </td>
</tr>
</table>
</body>
</html>
';
// obfuscate
if (method_exists($exception, 'getObfuscate') && count($exception->getObfuscate()) != 0) {
$output = str_replace($exception->getObfuscate(), '***', $output);
}
return $output;
}
/**
* Helper function for the exception handler to pretty-print variables.
*
* @return string
*
* @param mixed $var A variable to dump
* @param mixed[optional] $varN Zero or more variables to dump.
*/
function exceptionHandlerDumper($var)
{
ob_start();
foreach (func_get_args() as $arg) {
if (is_string($arg)) {
echo $arg;
} else {
ob_start();
var_dump($arg);
$output = ob_get_clean();
echo substr($output, 0, -1);
}
}
$dump = ob_get_clean();
return preg_replace(
['@{\n}@', '@#[0-9]+@', '@\]=>\n\s*@'],
['{ }', '', '] => '],
$dump
);
}
/**
* Get the HTML with the longest common path for this file and the given
* path dimmed.
*
* @param string $path The path to compare against this file's path.
*
* @return string
*/
function dimCommonPathPrefix($path)
{
// determine the longest shared prefix
$prefix = dirname(__FILE__);
while (!empty($prefix) && $prefix !== '/' && $prefix !== '.') {
if (strpos($path, $prefix . DIRECTORY_SEPARATOR) === 0) {
break;
}
$prefix = dirname($prefix);
}
// generate the HTML to dim the prefix, if any
$html = empty($prefix) || $prefix === '/' || $prefix === '.'
? htmlspecialchars($path)
: sprintf(
'<span style="opacity: 0.33">%s/</span>%s',
htmlspecialchars($prefix),
htmlspecialchars(substr($path, strlen($prefix) + 1))
);
// insert word break hints to avoid overly long lines
$html = preg_replace('@[^<]/@', '$0<wbr/>', $html);
return $html;
}