Passed
Push — develop ( a5e531...6c1b0f )
by Jens
03:22
created

ErrorHandlingUtil::handleJsonError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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