forked from dszulist/mkphp-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathError.php
More file actions
323 lines (284 loc) · 10.7 KB
/
Error.php
File metadata and controls
323 lines (284 loc) · 10.7 KB
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
<?php
/**
* MK_Error
*
* Obsługa błędów php/js zgłaszanie na maila etc.
*
* @category MK
* @package MK_Error
* @author bskrzypkowiak
*/
class MK_Error
{
/**
* Dane użytkownika z sesji
*
* @var array
*/
private static $userData = array ();
/**
* @var string
*/
private static $mailAdmin = 'Szczegółowy komunikat został wysłany do Administratora.';
/**
* Ignorowanie określonych klas z metodami
*/
private static $traceIgnorePath = array (
'MK_Error::handler',
'MK_Error::getExtendedTrace',
'MK::shutdownFunction',
'printr'
);
/**
* Ustawienie większej ilości klas do ignorowania dla fireBugSqlDump()
*
* @param $tracePath
*
* @internal param array $classArray
*/
public static function setMoreTraceIgnorePath($tracePath)
{
self::$traceIgnorePath = array_merge(self::$traceIgnorePath, $tracePath);
}
/**
* Uproszczony raport błędu dla Exception.
* Zapisanie zdarzenia w pliku tekstowym i wysłanie do logs.madkom.pl (dla developer:false)
*
* try {
* // code
* } catch (Exception $e) {
* die(MK_Error::getSimpleMessage($e));
* }
*
* @param Exception $exceptionClass
*
* @return string
*/
public static function getSimpleInfo(Exception $exceptionClass)
{
return '<pre>' . self::fromException($exceptionClass->getMessage(), $exceptionClass->getFile(), strval($exceptionClass->getLine()), self::getExtendedTrace($exceptionClass)) . '</pre>';
}
/**
* Rozbudowany raport ścieżki błędu
*
* @param Exception $exception
*
* @return string
*/
public static function getExtendedTrace($exception = null)
{
$traceKey = 1;
$msg = " #" . $traceKey++ . "\t";
if($exception instanceof Exception) {
$msg .= $exception->getFile() . '(' . $exception->getLine() . ')';
$traceArray = $exception->getTrace();
} else {
$traceArray = debug_backtrace();
}
// Odwrócenie kolejności czytania tablicy ze śladami
$traceArray = array_reverse($traceArray, true);
foreach ($traceArray as $trace) {
$_class = isset($trace['class']) ? $trace['class'] : '';
$_type = isset($trace['type']) ? $trace['type'] : '';
$_function = isset($trace['function']) ? $trace['function'] : '';
$_file = isset($trace['file']) ? $trace['file'] : '';
$_line = isset($trace['line']) ? $trace['line'] : -1;
// Ignorowanie klas z metodami (self::$traceIgnorePath)
$classTypeFunction = $_class . $_type . $_function;
if(in_array($classTypeFunction, self::$traceIgnorePath)) {
continue;
}
// Śledzenie pliku, wraz z wywoływaną klasą, metodą i argumentami:
$msg .= "\n #" . $traceKey++ . "\t" . $_file . '(' . $_line . '): ' . $classTypeFunction . '(';
// Odczytanie argumentów
if(isset($trace['args']) && count($trace['args']) > 0) {
foreach ($trace['args'] as $argsKey => $argsValue) {
$msg .= ($argsKey ? ' , ' : ' ');
$msg .= print_r($argsValue, true);
}
}
$msg .= ' )';
}
return $msg . "\n";
}
/**
* Tworzy szczegółowe informacje dla raportu błędu
*
* @param string $file (default: "(null)")
* @param string $line (default: "(null)")
*
* @return string
*/
private static function _prepareMessage($file = '(null)', $line = '(null)')
{
$userDataMsg = '';
if(!empty(self::$userData)) {
$userDataMsg = "\nUżytkownik:\n";
foreach (self::$userData as $key => $val) {
$userDataMsg .= " " . $key . ": " . $val . "\n";
}
}
$requestMethodMsg = '';
if(isset($_REQUEST) && count($_REQUEST) > 0) {
$requestMethodMsg = " \$_REQUEST:\n";
foreach ($_REQUEST as $key => $val) {
$requestMethodMsg .= " * " . $key . ": " . $val . "\n";
}
}
$devMessage = " Host:\t" . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'brak danych') . "\n"
. " Plik:\t{$file}\n"
. " Linia:\t{$line}\n"
. "\nBaza danych:\n"
. " Host:\t" . DB_HOST . "\n"
. " Nazwa:\t" . DB_NAME . "\n"
. $userDataMsg
. "\nInformacje dodatkowe:\n"
. " REMOTE_ADDR:\t" . (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'brak danych') . "\n"
. " SERVER_ADDR:\t" . (isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : 'brak danych') . "\n"
. " REQUEST_URI:\t" . (isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : 'brak danych') . "\n" . $requestMethodMsg
. " USER_AGENT:\t" . (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'brak danych') . "\n";
if(defined('APP_PATH')) {
$devMessage .= " APP_PATH:\t" . APP_PATH . "\n"
. " COOKIES_PATH:\t" . MK_COOKIES_PATH . "\n";
}
return $devMessage . "\n";
}
/**
* Obsługa błędów PHP. Zapisywanie informacji do pliku.
*
* @param Integer $type
* @param String $message (default: "")
* @param String $file (default: "")
* @param int|string $line (default: "")
* @param Mixed $errContext (default: array())
* @param String $debugBacktrace (default: "")
*
* @return Boolean
*/
public static function handler($type, $message = "", $file = "", $line = "", $errContext = array (), $debugBacktrace = "")
{
if(!($type & error_reporting())) {
return true;
}
// W przypadku tego błędu nie logujemy ponieważ nie ma on się pojawiać
if(preg_match('#pg_fetch_array\(\)( \[[^]]+\])*: Unable to jump to row [0-9]+ on PostgreSQL result index [0-9]+#i', $message)) {
return true;
}
$devMessage = self::_prepareMessage($file, $line) . "Komunikat błędu:\n " . $message . "\n\n";
$md5 = md5($message . $file . $line);
$devMessage .= "ERROR CODE: " . $type . "\n";
$devMessage .= "ERROR TYPE: " . self::getErrorType($type) . "\n";
if(count($errContext) > 0) {
$devMessage .= "Informacje szczegółowe:\n " . substr(print_r($errContext, true), 0, 10240) . "\n\n";
}
$devMessage .= "Backtrace:\n" . substr(empty($debugBacktrace) ? print_r(debug_backtrace(), true) : $debugBacktrace, 0, 1024) . "\n";
if(MK_DEBUG === true) {
return "Błąd \"php\"\t" . $md5 . "\n" . $devMessage;
}
if(!class_exists('MK_Logs')) {
include_once('Logs.php');
}
// Tutaj zwracamy informacje dla uzytkownika - w tym przypadku wyrzucamy wyjatek ktory zwróci jsona z informacja o obedzie ktora zostanie wyswietlana uzytkownikowi w postaci okna z błędem
if(($type !== E_NOTICE) && ($type < 2048)) {
return 'Nieoczekiwany błąd! ' . self::$mailAdmin;
}
$logs = new MK_Logs(APP_PATH);
$logs->saveToFile('php', $devMessage, $md5);
return false;
}
public static function getErrorType($errno)
{
switch ($errno) {
case E_NOTICE:
case E_USER_NOTICE:
return 'Notice';
break;
case E_WARNING:
case E_USER_WARNING:
case E_STRICT:
return 'Warning';
break;
case E_ERROR:
case E_USER_ERROR:
return 'Fatal Error';
break;
default:
return 'Unknown Error';
break;
}
}
/**
* Obsługa błędów zwróconych przez aplikację. Zapisywanie informacji do pliku.
*
* @param String $message (default: "")
* @param String $file (default: "")
* @param int|string $line (default: "")
* @param String $debugBacktrace (default: "")
*
* @return Boolean
*/
public static function fromException($message = "", $file = "", $line = "", $debugBacktrace = "")
{
$md5 = md5($message . $file . $line);
$devMessage = self::_prepareMessage($file, $line) . "Komunikat:\n " . $message . "\n\n"
. "Backtrace:\n" . substr(empty($debugBacktrace) ? print_r(debug_backtrace(), true) : $debugBacktrace, 0, 1024) . "\n";
if(MK_DEBUG === true) {
return "Błąd \"exception\"\t" . $md5 . "\n" . $devMessage;
}
$logs = new MK_Logs(APP_PATH);
$logs->saveToFile('exception', $devMessage, $md5);
return $message;
}
/**
* Obsługa błędów w bazie danych. Zapisywanie informacji do pliku.
*
* @param string $message
* @param string $file (default: "")
* @param integer $line (default: 0)
* @param string $debugBacktrace (default: "")
*
* @return string
*/
public static function fromDataBase($message = "", $file = "", $line = 0, $debugBacktrace = "")
{
$md5 = md5($message . $file . $line);
$devMessage = self::_prepareMessage($file, $line) . "Komunikat:\n " . $message . "\n\n"
. "Backtrace:\n" . substr(empty($debugBacktrace) ? print_r(debug_backtrace(), true) : $debugBacktrace, 0, 1024) . "\n";
if(MK_DEBUG === true) {
return "Błąd \"db\"\t" . $md5 . "\n" . $devMessage;
}
$logs = new MK_Logs(APP_PATH);
$logs->saveToFile('db', $devMessage, $md5);
return $message;
}
/**
* Obsługa błędu JavaScript odczytanego z ciastka. Zapisywanie informacji do pliku.
*
* @return string
*/
public static function fromJavaScript()
{
if(isset($_COOKIE['ys-javascriptErrorLog'])) {
MK_Cookie::clear('ys-javascriptErrorLog');
$errorObject = json_decode(substr($_COOKIE['ys-javascriptErrorLog'], 2));
$md5 = md5(print_r($errorObject, true));
$devMessage = self::_prepareMessage() . "Komunikat:\n " . substr(print_r($errorObject, true), 0, 1024) . "\n\n";
if(MK_DEBUG === true) {
return "Błąd \"js\"\t" . $md5 . "\n" . $devMessage;
}
$logs = new MK_Logs(APP_PATH);
$logs->saveToFile('js', $devMessage, $md5);
return 'Błąd JavaScript. ' . self::$mailAdmin;
}
return null;
}
/**
* Ustawia dane użytkownika z sesji
*
* @param array $userData
*/
public static function setUserData(array $userData)
{
self::$userData = $userData;
}
}