|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* User: jensk |
|
4
|
|
|
* Date: 13-3-2017 |
|
5
|
|
|
* Time: 15:16 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace library\cc; |
|
9
|
|
|
|
|
10
|
|
|
class ErrorHandlingUtil |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Displays the error in a human readable fashion for developers. |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $message |
|
16
|
|
|
* @param string $file |
|
17
|
|
|
* @param string $line |
|
18
|
|
|
* @param int $code |
|
19
|
|
|
* @param array $trace |
|
20
|
|
|
* @param string $httpHeader |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function renderError($message = '', $file = '', $line = '', $code = 0, $trace = array(), $httpHeader = 'HTTP/1.0 500 Internal Server Error') |
|
23
|
|
|
{ |
|
24
|
|
|
if (ob_get_contents()) ob_end_clean(); |
|
25
|
|
|
$line = intval($line); |
|
26
|
|
|
|
|
27
|
|
|
if (self::canShowError()) { |
|
28
|
|
|
self::showError($message, $file, $line, $code, $trace, $httpHeader); |
|
29
|
|
|
} else { |
|
30
|
|
|
self::dontShowError($message, $file, $line, $httpHeader); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Error handler specificly for json errors |
|
36
|
|
|
* |
|
37
|
|
|
* @param $file |
|
38
|
|
|
* @param $line |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function handleJsonError($file, $line) |
|
41
|
|
|
{ |
|
42
|
|
|
$jsonErrorNr = json_last_error(); |
|
43
|
|
|
$errstr = ''; |
|
44
|
|
|
switch ($jsonErrorNr) { |
|
45
|
|
|
case JSON_ERROR_NONE: |
|
46
|
|
|
$errstr .= ' - No errors' . PHP_EOL; |
|
47
|
|
|
break; |
|
48
|
|
|
case JSON_ERROR_DEPTH: |
|
49
|
|
|
$errstr .= ' - Maximum stack depth exceeded' . PHP_EOL; |
|
50
|
|
|
break; |
|
51
|
|
|
case JSON_ERROR_STATE_MISMATCH: |
|
52
|
|
|
$errstr .= ' - Underflow or the modes mismatch' . PHP_EOL; |
|
53
|
|
|
break; |
|
54
|
|
|
case JSON_ERROR_CTRL_CHAR: |
|
55
|
|
|
$errstr .= ' - Unexpected control character found' . PHP_EOL; |
|
56
|
|
|
break; |
|
57
|
|
|
case JSON_ERROR_SYNTAX: |
|
58
|
|
|
$errstr .= ' - Syntax error, malformed JSON' . PHP_EOL; |
|
59
|
|
|
break; |
|
60
|
|
|
case JSON_ERROR_UTF8: |
|
61
|
|
|
$errstr .= ' - Malformed UTF-8 characters, possibly incorrectly encoded' . PHP_EOL; |
|
62
|
|
|
break; |
|
63
|
|
|
default: |
|
64
|
|
|
$errstr = ' - Unknown error' . PHP_EOL; |
|
65
|
|
|
break; |
|
66
|
|
|
} |
|
67
|
|
|
\errorHandler($jsonErrorNr, $errstr, $file, $line); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private static function canShowError() |
|
71
|
|
|
{ |
|
72
|
|
|
if (PHP_SAPI === 'cli') { |
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
if (file_exists('../config.json') && !isset($_SESSION['cloudcontrol'])) { |
|
76
|
|
|
$config = file_get_contents('../config.json'); |
|
77
|
|
|
$config = json_decode($config); |
|
78
|
|
|
if (isset($config->showErrorsToAll)) { |
|
79
|
|
|
return $config->showErrorsToAll; |
|
80
|
|
|
} |
|
81
|
|
|
} else { |
|
82
|
|
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private static function renderCliException($message, $file, $line, $trace, $lines) |
|
87
|
|
|
{ |
|
88
|
|
|
if (ob_get_contents()) ob_end_clean(); |
|
89
|
|
|
include(__DIR__ . DIRECTORY_SEPARATOR . 'errortemplates/errorviewcli.php'); |
|
90
|
|
|
exit; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param $message |
|
95
|
|
|
* @param $file |
|
96
|
|
|
* @param $line |
|
97
|
|
|
* @param $httpHeader |
|
98
|
|
|
*/ |
|
99
|
|
|
private static function dontShowError($message, $file, $line, $httpHeader) |
|
100
|
|
|
{ |
|
101
|
|
|
header($_SERVER['SERVER_PROTOCOL'] . $httpHeader, true); |
|
102
|
|
|
header('X-Error-Message: ' . $message); |
|
103
|
|
|
header('X-Error-File: ' . $file); |
|
104
|
|
|
header('X-Error-Line: ' . $line); |
|
105
|
|
|
if (file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'errortemplates/errorviewcompact.php')) { |
|
106
|
|
|
include(__DIR__ . DIRECTORY_SEPARATOR . 'errortemplates/errorviewcompact.php'); |
|
107
|
|
|
} else { |
|
108
|
|
|
header('Content-type: application/json'); |
|
109
|
|
|
die(json_encode('An error occured.')); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param $message |
|
115
|
|
|
* @param $file |
|
116
|
|
|
* @param $line |
|
117
|
|
|
* @param $code |
|
118
|
|
|
* @param $trace |
|
119
|
|
|
* @param $httpHeader |
|
120
|
|
|
*/ |
|
121
|
|
|
private static function showError($message, $file, $line, $code, $trace, $httpHeader) |
|
122
|
|
|
{ |
|
123
|
|
|
$lines = self::getNumberedErrorLines($file, $line); |
|
124
|
|
|
|
|
125
|
|
|
if (PHP_SAPI === 'cli') { |
|
126
|
|
|
self::renderCliException($message, $file, $line, $trace, $lines); |
|
127
|
|
|
} else { |
|
128
|
|
|
self::renderGraphicException($message, $file, $line, $code, $trace, $httpHeader, $lines); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param $file |
|
134
|
|
|
* @param $line |
|
135
|
|
|
* |
|
136
|
|
|
* @return array |
|
137
|
|
|
*/ |
|
138
|
|
|
private static function getNumberedErrorLines($file, $line) |
|
139
|
|
|
{ |
|
140
|
|
|
$file_lines = file_exists($file) ? file($file) : array(); |
|
141
|
|
|
$range = ($line - 15) < 0 ? range(1, 30) : range($line - 15, $line + 15); |
|
142
|
|
|
$lines = array(); |
|
143
|
|
|
|
|
144
|
|
|
foreach ($range as $line_number) { |
|
145
|
|
|
if (isset($file_lines[$line_number - 1])) { |
|
146
|
|
|
$lines[$line_number] = $file_lines[$line_number - 1]; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $lines; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param $message |
|
155
|
|
|
* @param $file |
|
156
|
|
|
* @param $line |
|
157
|
|
|
* @param $code |
|
158
|
|
|
* @param $trace |
|
159
|
|
|
* @param $httpHeader |
|
160
|
|
|
* @param $lines |
|
161
|
|
|
*/ |
|
162
|
|
|
private static function renderGraphicException($message, $file, $line, $code, $trace, $httpHeader, $lines) |
|
163
|
|
|
{ |
|
164
|
|
|
$error = array( |
|
165
|
|
|
'message' => $message, |
|
166
|
|
|
'file' => $file, |
|
167
|
|
|
'line' => $line, |
|
168
|
|
|
'code' => $code, |
|
169
|
|
|
'lines' => $lines, |
|
170
|
|
|
'trace' => $trace, |
|
171
|
|
|
'httpHeader' => $httpHeader, |
|
172
|
|
|
); |
|
173
|
|
|
|
|
174
|
|
|
if (file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'errortemplates/errorviewdetailed.php')) { |
|
175
|
|
|
header($_SERVER['SERVER_PROTOCOL'] . $httpHeader, true); |
|
176
|
|
|
include(__DIR__ . DIRECTORY_SEPARATOR . 'errortemplates/errorviewdetailed.php'); |
|
177
|
|
|
} else { |
|
178
|
|
|
header($_SERVER['SERVER_PROTOCOL'] . $httpHeader, true); |
|
179
|
|
|
header('Content-type: application/json'); |
|
180
|
|
|
die(json_encode($error)); |
|
181
|
|
|
} |
|
182
|
|
|
exit; |
|
183
|
|
|
} |
|
184
|
|
|
} |