1
|
|
|
<?php |
2
|
|
|
namespace vsc; |
3
|
|
|
|
4
|
|
|
use vsc\infrastructure\vsc; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Function to turn the triggered errors into exceptions |
8
|
|
|
* @author troelskn at gmail dot com |
9
|
|
|
* @see http://php.net/manual/en/class.errorexception.php |
10
|
|
|
* @param $iSeverity |
11
|
|
|
* @param $sMessage |
12
|
|
|
* @param $sFilename |
13
|
|
|
* @param $iLineNo |
14
|
|
|
* @throws ExceptionError |
15
|
|
|
* @return void |
16
|
|
|
*/ |
17
|
|
|
function exceptions_error_handler($iSeverity, $sMessage, $sFilename, $iLineNo) { |
18
|
1 |
|
if (error_reporting() == 0) { |
19
|
|
|
return; |
20
|
|
|
} |
21
|
|
|
|
22
|
1 |
|
if (error_reporting() & $iSeverity) { |
23
|
1 |
|
throw new ExceptionError($sMessage, 0, $iSeverity, $sFilename, $iLineNo); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
function getPaths() { |
28
|
1 |
|
return explode(PATH_SEPARATOR, get_include_path()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
function cleanBuffers($iLevel = null) { |
32
|
1 |
|
$aErrors = array(); |
33
|
1 |
|
if (is_null($iLevel)) { |
34
|
1 |
|
$iLevel = ob_get_level(); |
35
|
1 |
|
} |
36
|
1 |
|
for ($i = 0; $i < min(1, $iLevel); $i++) { |
37
|
1 |
|
ob_end_clean(); |
38
|
1 |
|
} |
39
|
|
|
|
40
|
1 |
|
$iLevel = ob_get_level(); |
41
|
1 |
|
for ($i = 0; $i < $iLevel; $i++) { |
42
|
|
|
if (ob_get_length() > 0) { |
43
|
|
|
$s = ob_get_clean(); |
|
|
|
|
44
|
|
|
$aErrors[$i] = $s; |
45
|
|
|
} else { |
46
|
|
|
ob_end_clean(); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
return $aErrors; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
function getErrorHeaderOutput($e = null) { |
54
|
3 |
|
$sRet = ''; |
55
|
3 |
|
if (!vsc::isCli()) { |
56
|
1 |
|
if (!headers_sent()) { |
57
|
|
|
header('HTTP/1.1 500 Internal Server Error'); |
58
|
|
|
} |
59
|
1 |
|
$sRet = '<?xml version="1.0" encoding="utf-8"?>'; |
|
|
|
|
60
|
1 |
|
$sRet .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'; |
61
|
1 |
|
$sRet .= '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'; |
62
|
1 |
|
$sRet .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">'; |
63
|
1 |
|
$sRet .= '<head>'; |
64
|
1 |
|
$sRet .= '<style>ul {padding:0; font-size:0.8em} li {padding:0.2em;display:inline} address {position:fixed;bottom:0;}</style>'; |
65
|
1 |
|
$sRet .= '<title>Internal Error' . (!($e instanceof \Exception) ? '' : ': ' . substr($e->getMessage(), 0, 20) . '...') . '</title>'; |
66
|
1 |
|
$sRet .= '</head>'; |
67
|
1 |
|
$sRet .= '<body>'; |
68
|
1 |
|
$sRet .= '<strong>Internal Error' . (!($e instanceof \Exception) ? '' : ': ' . $e->getMessage()) . '</strong>'; |
69
|
1 |
|
$sRet .= '<address>© VSC</address>'; |
70
|
1 |
|
$sRet .= '<ul>'; |
71
|
1 |
|
$sRet .= '<li><a href="#" onclick="p = document.getElementById(\'trace\'); if (p.style.display==\'block\') p.style.display=\'none\';else p.style.display=\'block\'; return false">toggle trace</a></li>'; |
72
|
1 |
|
if (defined('ROOT_MAIL')) { |
73
|
|
|
$sRet .= '<li><a href="javascript: p = document.getElementById(\'trace\'); document.location.href =\'mailto:' . ROOT_MAIL . '?subject=Problems&body=\' + p.innerHTML; return false">mail me</a></li>'; |
74
|
|
|
} |
75
|
1 |
|
$sRet .= '</ul>'; |
76
|
|
|
|
77
|
1 |
View Code Duplication |
if ($e instanceof \Exception) |
|
|
|
|
78
|
1 |
|
$sRet .= '<p style="font-size:.8em">Triggered in <strong>' . $e->getFile() . '</strong> at line ' . $e->getLine() . '</p>'; |
79
|
|
|
|
80
|
1 |
|
$sRet .= '<pre style="position:fixed;bottom:2em;display:' . (vsc::getEnv()->isDevelopment() ? 'block' : 'none') . ';font-size:.8em" id="trace">'; |
81
|
1 |
|
} |
82
|
|
|
|
83
|
3 |
|
return $sRet; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param \Exception $e |
88
|
|
|
*/ |
89
|
|
|
function _e($e) { |
90
|
|
|
$aErrors = cleanBuffers(); |
91
|
|
|
$sRet = ''; |
|
|
|
|
92
|
|
|
if (!vsc::isCli()) { |
93
|
|
|
header('HTTP/1.1 500 Internal Server Error'); |
94
|
|
|
echo getErrorHeaderOutput($e); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (isDebug()) { |
98
|
|
|
echo ($e instanceof \Exception) ? $e->getTraceAsString() : ''; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (count($aErrors) > 0) { |
102
|
|
|
if (!vsc::isCli()) { echo '<h2>'; } |
103
|
|
|
echo 'Previous Errors'; |
104
|
|
|
if (!vsc::isCli()) { echo '</h2>'; } |
105
|
|
|
if (!vsc::isCli()) { echo '<p>'; } |
106
|
|
|
echo implode($aErrors, vsc::isCli() ? "\n" : '<br/>'); |
107
|
|
|
if (!vsc::isCli()) { echo '</p>'; } |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (!vsc::isCli()) { |
111
|
|
|
echo '</pre>'; |
112
|
|
|
echo '</body>'; |
113
|
|
|
echo '</html>'; |
114
|
|
|
} else { |
115
|
|
View Code Duplication |
if ($e instanceof \Exception) { |
|
|
|
|
116
|
|
|
$sRet .= $e->getMessage() . "\n"; |
117
|
|
|
$sRet .= "\t" . $e->getFile() . ' at line ' . $e->getLine() . "\n"; |
118
|
|
|
} |
119
|
|
|
echo $sRet; |
120
|
|
|
} |
121
|
|
|
exit (0); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (!function_exists('isDebug')) { |
125
|
|
|
function isDebug() { |
126
|
1 |
|
return true; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.