-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErrorManager.php
1124 lines (947 loc) · 27.9 KB
/
ErrorManager.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Simple error and exception handler
*
* When enabled (with ErrorManager::enable(ErrorManager::DEVELOPMENT)) it will
* catch any error, warning or exception and display it along with useful debug
* information. If enabled it will also log the errors to a file and/or send
* every error by email.
*
* In production mode no details are given, but a unique reference to the log
* or email is displayed.
*
* This is similar in a way to http://tracy.nette.org/
*
* @author bohwaz <http://bohwaz.net/>
*/
class ErrorManager
{
/**
* Prod/dev modes
*/
const PRODUCTION = 1;
const DEVELOPMENT = 2;
const CLI_DEVELOPMENT = 4;
/**
* Term colors
*/
const RED = '[1;41m';
const RED_FAINT = '[1m';
const YELLOW = '[33m';
/**
* true = catch exceptions, false = do nothing
* @var null
*/
static protected $enabled = null;
/**
* HTML template used for displaying production errors
* @var string
*/
static protected $production_error_template = '<!DOCTYPE html><html><head><title>Internal server error</title>
<style type="text/css">
body {font-family: sans-serif; }
code, p, h1 { max-width: 400px; margin: 1em auto; display: block; }
code { text-align: right; color: #666; }
a { color: blue; }
form { text-align: center; }
input { padding: .3em; }
</style></head><body><h1>Server error</h1><p>Sorry but the server encountered an internal error and was unable
to complete your request. Please try again later.</p>
<if(sent)><p>The webmaster has been noticed and this will be fixed ASAP.</p></if>
<if(logged)><code>Error reference: <b>{$ref}</b></code></if>
<if(report)><form method="post" action="{$report_url}"><input type="hidden" value="{$report_json}" /><input type="submit" value="Report this error" /></report_form></if>
<p><a href="/">← Go back to the homepage</a></p>
</body></html>';
/**
* E-Mail address where to send errors
* @var boolean
*/
static protected $email_errors = false;
/**
* Reporting URL
*/
static protected $report_url = null;
/**
* Reporting automatically?
*/
static protected $report_auto = true;
/**
* Custom context
*/
static protected $context = [];
/**
* Custom exception handlers
* @var array
*/
static protected $custom_handlers = [];
/**
* Does the terminal support ANSI colors
* @var boolean
*/
static protected $term_color = false;
/**
* Will be set to true when catching an exception to avoid double catching
* with the shutdown function
* @var boolean
*/
static protected $catching = false;
/**
* Used to store timers and memory consumption
* @var array
*/
static protected $run_trace = [];
/**
* Handles PHP shutdown on fatal error to be able to catch the error
* @return void
*/
static public function shutdownHandler()
{
// Stop here if disabled or if the script ended with an exception
if (!self::$enabled || self::$catching)
return false;
$error = error_get_last();
if ($error && in_array($error['type'], [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE, E_RECOVERABLE_ERROR, E_USER_ERROR], TRUE))
{
// Don't exit at the end, as there might be other shutdown handlers
// after this one
self::exceptionHandler(new \ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']), false);
}
}
/**
* Internal error handler to throw them as exceptions
* (private use)
*/
static public function errorHandler($severity, $message, $file, $line)
{
if (!(error_reporting() & $severity)) {
// Don't report this error (for example @unlink)
return;
}
$types = [
E_ERROR => 'Fatal error',
E_USER_ERROR => 'User error',
E_RECOVERABLE_ERROR => 'Recoverable error',
E_CORE_ERROR => 'Core error',
E_COMPILE_ERROR => 'Compile error',
E_PARSE => 'Parse error',
E_WARNING => 'Warning',
E_CORE_WARNING => 'Core warning',
E_COMPILE_WARNING => 'Compile warning',
E_USER_WARNING => 'User warning',
E_NOTICE => 'Notice',
E_USER_NOTICE => 'User notice',
E_STRICT => 'Strict standards',
E_DEPRECATED => 'Deprecated',
E_USER_DEPRECATED => 'User deprecated',
];
$type = array_key_exists($severity, $types) ? $types[$severity] : 'Unknown error';
$message = $type . ': ' . $message;
// Catch ASSERT_BAIL errors differently because throwing an exception
// in this case results in an execution shutdown, and shutdown handler
// isn't even called. See https://bugs.php.net/bug.php?id=53619
if (assert_options(ASSERT_ACTIVE) && assert_options(ASSERT_BAIL) && substr($message, 0, 18) == 'Warning: assert():')
{
$message .= ' (ASSERT_BAIL detected)';
self::exceptionHandler(new \ErrorException($message, 0, $severity, $file, $line));
return true;
}
throw new \ErrorException($message, 0, $severity, $file, $line);
return true;
}
/**
* Main exception handler
* @param object $e Exception or Error (PHP 7) object
* @param boolean $exit Exit the script at the end
* @return void
*/
static public function exceptionHandler($e, $exit = true)
{
self::$catching = true;
try {
self::reportException($e, $exit);
}
catch (\Throwable $e2) {
echo $e2;
echo PHP_EOL . PHP_EOL . $e;
exit(1);
}
catch (\Exception $e2) {
echo $e2;
echo PHP_EOL . PHP_EOL . $e;
exit(1);
}
return true;
}
/**
* Main exception handler
* @param object $e Exception or Error (PHP 7) object
* @param boolean $exit Exit the script at the end
* @return void
*/
static public function reportException($e, $exit)
{
foreach (self::$custom_handlers as $class=>$callback)
{
if ($e instanceOf $class)
{
call_user_func($callback, $e);
$e = false;
break;
}
}
if ($e === false)
{
if ($exit)
{
exit(1);
}
else
{
return;
}
}
extract(self::buildExceptionReport($e, false));
unset($e);
// Log exception to file
if (ini_get('log_errors'))
{
error_log($log);
}
// Disable any output if it was buffering
if (ob_get_level())
{
ob_end_clean();
}
$is_curl = 0 === strpos($_SERVER['HTTP_USER_AGENT'] ?? '', 'curl/');
$is_cli = PHP_SAPI == 'cli';
if (!$is_cli) {
http_response_code(500);
}
if ($is_curl && !headers_sent()) {
header('Content-Type: text/plain; charset=utf-8', true);
}
if (($is_cli || $is_curl) && (self::$enabled & self::DEVELOPMENT || self::$enabled & self::CLI_DEVELOPMENT))
{
foreach ($report->errors as $e)
{
self::termPrint(sprintf(' /!\\ %s ', $e->type), self::RED);
self::termPrint($e->message, self::RED_FAINT);
if (isset($e->line))
{
self::termPrint(sprintf('Line %d in %s', $e->line, $e->file), self::
YELLOW);
}
// Ignore the error stack belonging to ErrorManager
foreach ($e->backtrace as $i=>$t)
{
$file = !empty($t->file) ? $t->file : '[internal function]';
$line = !empty($t->line) ? '(' . $t->line . ')' : '';
if (isset($t->args))
{
$args = $t->args;
foreach ($args as &$arg)
{
if (strlen($arg) > 20)
{
$arg = substr($arg, 0, 19) . '…';
}
}
unset($arg);
self::termPrint(sprintf('#%d %s%s: %s(%s)', $i, $file, $line, $t->function, implode(', ', $args)));
}
else
{
self::termPrint(sprintf('#%d %s%s', $i, $file, $line));
}
}
}
}
else if (($is_cli || $is_curl) && self::$enabled & self::PRODUCTION) {
self::termPrint(' /!\\ An internal server error occurred ', self::RED);
self::termPrint(' Error reference was: ' . $report->context->id, self::YELLOW);
}
else if (self::$enabled & self::PRODUCTION)
{
@header_remove('Content-Disposition');
@header('Content-Type: text/html; charset=utf-8', true);
self::htmlProduction($report);
}
else
{
if (!headers_sent()) {
header_remove();
header('Content-Type: text/html; charset=UTF-8', true);
header('HTTP/1.1 500 Internal Server Error', true);
}
echo $html_report;
}
// Log exception to email
if (self::$email_errors) {
self::sendEmail($title, $report, $log, $html_report);
}
// Send report to URL
if (self::$report_auto && self::$report_url) {
self::sendReport($report, self::$report_url);
}
if ($exit)
{
exit(1);
}
}
static public function reportExceptionSilent(\Throwable $e): void
{
extract(self::buildExceptionReport($e));
// Log exception to file
if (ini_get('log_errors'))
{
error_log($log);
}
if (self::$email_errors) {
self::sendEmail($title, $report, $log, $html_report);
}
}
static protected function sendEmail(string $title, \stdClass $report, string $log, string $html): void
{
// From: sender
$from = !empty($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : basename($report->context->root_directory ?? __FILE__);
$msgid = $report->context->id . '@' . $from;
$boundary = sprintf('-----=%s', md5(uniqid(rand())));
$header = sprintf("MIME-Version: 1.0\r\nFrom: \"%s\" <%s>\r\nIn-Reply-To: <%s>\r\nMessage-Id: <%s>\r\n", $from, self::$email_errors, $msgid, $msgid);
$header.= sprintf("Content-Type: multipart/mixed; boundary=\"%s\"\r\n", $boundary);
$header.= "\r\n";
$msg = "This message contains multiple MIME parts.\r\n\r\n";
$msg.= sprintf("--%s\r\n", $boundary);
$msg.= "Content-Type: text/plain; charset=\"utf-8\"\r\n";
$msg.= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$msg.= $log . "\r\n\r\n";
$msg.= sprintf("--%s\r\n", $boundary);
$msg.= "Content-Type: text/html; charset=\"utf-8\"\r\n";
$msg.= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$msg.= $html . "\r\n\r\n";
$msg.= sprintf("--%s--", $boundary);
mail(self::$email_errors, sprintf('Error #%s: %s', $report->context->id, $title), $msg, $header);
}
/**
* Prints a line to STDERR, eventually using a color
*/
static public function termPrint($message, $color = null)
{
if (!defined('\STDERR')) {
echo $message . PHP_EOL;
return;
}
if ($color && self::$term_color)
{
$message = chr(27) . $color . $message . chr(27) . "[0m";
}
fwrite(\STDERR, $message . PHP_EOL);
}
/**
* Return file location without the document root
*/
static protected function getFileLocation($file)
{
if (!empty(self::$context['root_directory']) && ($pos = strpos($file, self::$context['root_directory'])) === 0)
{
return '...' . substr($file, strlen(self::$context['root_directory']));
}
return $file;
}
static public function buildExceptionReport(\Throwable $e, bool $force_html = false): array
{
$report = self::makeReport($e);
$log = sprintf('=========== Error ref. %s ===========', $report->context->id)
. PHP_EOL . PHP_EOL . (string) $e . PHP_EOL . PHP_EOL
. '<errorReport>' . PHP_EOL . json_encode($report, \JSON_PRETTY_PRINT)
. PHP_EOL . '</errorReport>' . PHP_EOL;
$html_report = null;
if ($force_html || self::$enabled & self::DEVELOPMENT || self::$email_errors) {
$html_report = self::htmlReport($report);
}
$title = $e->getMessage();
return compact('report', 'log', 'html_report', 'title');
}
/**
* Generates a report from an exception
*/
static public function makeReport($e)
{
$report = (object) [
'errors' => [],
];
while ($e !== null)
{
$class = get_class($e);
$error = (object) [
'message' => $e->getMessage(),
'errorCode' => $e->getCode(),
'type' => in_array($class, ['ErrorException', 'Error']) ? 'PHP error' : $class,
'backtrace' => [
(object) [
'file' => $e->getFile() ? self::getFileLocation($e->getFile()) : null,
'line' => $e->getLine(),
'code' => $e->getFile() && $e->getLine() ? self::getSource($e->getFile(), $e->getLine()) : null,
],
],
];
foreach ($e->getTrace() as $i=>$t)
{
// Ignore the error stack from ErrorManager
if (isset($t['class']) && $t['class'] === __CLASS__
&& ($t['function'] === 'shutdownHandler' || $t['function'] === 'errorHandler'))
{
continue;
}
$args = [];
// Display call arguments
if (!empty($t['args']))
{
// Find arguments variables names via reflection
try {
if (isset($t['class']))
{
$r = new \ReflectionMethod($t['class'], $t['function']);
}
else
{
$r = new \ReflectionFunction($t['function']);
}
$params = $r->getParameters();
}
catch (\Exception $_ignore) {
$params = [];
}
foreach ($t['args'] as $name => $value)
{
if (array_key_exists($name, $params))
{
$name = '$' . $params[$name]->name;
}
if (is_string($value))
{
$value = self::getFileLocation($value);
}
$args[$name] = self::dump($value);
if (strlen($args[$name]) > 2000)
{
$args[$name] = substr($args[$name], 0, 1999) . '…';
}
}
}
$trace = (object) [
// Add class name to function
'function' => isset($t['class']) ? $t['class'] . $t['type'] . $t['function'] : $t['function'],
];
if (isset($t['file']))
{
$trace->file = self::getFileLocation($t['file']);
}
if (isset($t['line']))
{
$trace->line = (int) $t['line'];
}
if (count($args))
{
$trace->args = $args;
}
if (isset($trace->file) && isset($trace->line))
{
$trace->code = self::getSource($t['file'], $t['line']);
}
$error->backtrace[] = $trace;
}
$report->errors[] = $error;
$e = $e->getPrevious();
}
unset($error, $e, $params, $t);
$context = array_merge([
'id' => base_convert(substr(sha1(json_encode($report->errors)), 0, 10), 16, 36),
'date' => date(DATE_ATOM),
'os' => PHP_OS,
'language' => 'PHP ' . PHP_VERSION,
'environment' => self::$enabled & self::DEVELOPMENT ? 'development' : 'production:' . self::$enabled,
'php_sapi' => PHP_SAPI,
'remote_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null,
'http_method' => isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : null,
'http_files' => self::dump($_FILES),
'http_post' => self::dump($_POST, true),
'duration' => isset(self::$context['request_started']) ? (microtime(true) - self::$context['request_started'])*1000 : null,
'memory_peak' => memory_get_peak_usage(true),
'memory_used' => memory_get_usage(true),
], self::$context);
ksort($context);
unset($context['request_started']);
$report->context = (object) $context;
if (!empty($_SERVER['HTTP_HOST']) && !empty($_SERVER['REQUEST_URI']))
{
$proto = empty($_SERVER['HTTPS']) ? 'http' : 'https';
$report->context->url = $proto . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
return $report;
}
/**
* Displays an exception as HTML debug page
*/
static public function htmlException(\stdClass $e): string
{
$out = sprintf('<section><header><h1>%s</h1><h2>%s</h2></header>',
$e->type, nl2br(htmlspecialchars($e->message)));
foreach ($e->backtrace as $i=>$t)
{
$out .= '<article>';
if (isset($t->file) && isset($t->line))
{
$dir = dirname($t->file);
$dir = $dir == '/' ? $dir : $dir . '/';
$out .= sprintf('<h3>in %s<b>%s</b>:<i>%d</i></h3>', htmlspecialchars($dir), htmlspecialchars(basename($t->file)), $t->line);
}
if (isset($t->function))
{
$out .= sprintf('<h4>→ %s <i>(%d arg.)</i></h4>', htmlspecialchars($t->function), isset($t->args) ? count($t->args) : 0);
// Display call arguments
if (!empty($t->args))
{
$out .= '<table>';
foreach ($t->args as $name => $value)
{
$out .= sprintf('<tr><th>%s</th><td><pre>%s</pre></td>', htmlspecialchars($name), htmlspecialchars($value));
}
$out .= '</table>';
}
}
// Display source code
if (isset($t->code) && isset($t->line))
{
$out .= self::htmlSource($t->code, $t->line);
}
$out .= '</article>';
}
$out .= '</section>';
return $out;
}
static public function htmlSource(array $source, $line)
{
$out = '';
foreach ($source as $i => $code)
{
$html = '<b>' . ($i) . '</b>' . htmlspecialchars($code, ENT_QUOTES);
if ($i == $line)
{
$html = '<u>' . $html . '</u>';
}
$out .= $html . PHP_EOL;
}
return '<pre><code>' . $out . '</code></pre>';
}
/**
* Get source code
* @param string $file File location
* @param integer $line Line to highlight
* @return array
*/
static public function getSource($file, $line)
{
$out = [];
$start = max(0, $line - 5);
if (!file_exists($file)) {
return [$line => 'Source file not found'];
}
$file = new \SplFileObject($file);
$file->seek($start);
for ($i = $start + 1; $i < $start+10; $i++)
{
if ($file->eof())
{
break;
}
$out[$i] = trim($file->current(), "\r\n");
$file->next();
}
unset($file);
return $out;
}
static public function htmlProduction(\stdClass $report)
{
if (!headers_sent()) {
header_remove();
header('HTTP/1.1 500 Internal Server Error', true, 500);
header('Content-Type: text/html; charset=UTF-8', true);
}
echo self::htmlTemplate(self::$production_error_template, $report);
}
static public function htmlTemplate($str, \stdClass $report)
{
$str = strtr($str, [
'{$ref}' => $report->context->id,
'{$report_json}' => htmlspecialchars(base64_encode(json_encode($report)), ENT_QUOTES),
'{$report_url}' => htmlspecialchars((string) self::$report_url),
]);
$str = preg_replace_callback('!<if\((sent|logged|report|email|log)\)>(.*?)</if>!is', function ($match) {
switch ($match[1]) {
case 'sent':
case 'email':
return self::$email_errors || (self::$report_auto && self::$report_url) ? $match[2] : '';
case 'logged':
case 'log':
return ini_get('error_log') ? $match[2] : '';
case 'report':
return (!self::$report_auto && self::$report_url) ? $match[2] : '';
}
}, $str);
return $str;
}
static public function htmlReport(\stdClass $report): string
{
$out = '';
// Display debug
$out .= self::htmlTemplate(ini_get('error_prepend_string'), $report);
foreach ($report->errors as $e)
{
$out .= self::htmlException($e);
}
$out .= '<section><article><h2>Context</h2><table>';
foreach ($report->context as $name => $value)
{
$out .= sprintf('<tr><th>%s</th><td>%s</td></tr>',
htmlspecialchars($name),
htmlspecialchars($value ?? ''));
}
$out .= '</table></article></section>';
$out .= self::htmlTemplate(ini_get('error_append_string'), $report);
return $out;
}
/**
* Enable error manager
* @param integer $type Type of error management (ErrorManager::PRODUCTION or ErrorManager::DEVELOPMENT)
* You can also use ErrorManager::PRODUCTION | ErrorManager::CLI_DEVELOPMENT to get error messages in CLI but still hide errors
* on web front-end.
* @return void
*/
static public function enable($type = self::DEVELOPMENT)
{
if (self::$enabled)
return true;
self::$context['request_started'] = $_SERVER['REQUEST_TIME_FLOAT'] ?? microtime(true);
self::$enabled = $type;
self::$term_color = function_exists('posix_isatty') && defined('\STDOUT') && @posix_isatty(\STDOUT);
ini_set('display_errors', false);
ini_set('log_errors', false);
ini_set('html_errors', false);
ini_set('zend.exception_ignore_args', false); // We want to get the args in exceptions (since PHP 7.4)
error_reporting($type & self::DEVELOPMENT ? -1 : E_ALL & ~E_DEPRECATED & ~E_STRICT);
if ($type & self::DEVELOPMENT && PHP_SAPI != 'cli')
{
self::setHtmlHeader('<!DOCTYPE html><meta charset="utf-8" /><style type="text/css">
body { font-family: sans-serif; } * { margin: 0; padding: 0; }
u, code b, i, h3 { font-style: normal; font-weight: normal; text-decoration: none; }
#icn { color: #fff; font-size: 2em; float: right; margin: 1em; padding: 1em; background: #900; border-radius: 50%; }
section header { background: #fdd; padding: 1em; }
section article { margin: 1em; }
section article h3, section article h4 { font-size: 1em; font-family: mono; }
code { border: 1px dotted #ccc; display: block; }
code b { margin-right: 1em; color: #999; }
code u { background: #fcc; display: inline-block; width: 100%; }
table { border-collapse: collapse; margin: 1em; } td, th { border: 1px solid #ccc; padding: .2em .5em; text-align: left;
vertical-align: top; }
</style>
<pre id="icn"> \__/<br /> (xx)<br />//||\\\\</pre>');
}
register_shutdown_function([self::class, 'shutdownHandler']);
set_exception_handler([__CLASS__, 'exceptionHandler']);
set_error_handler([__CLASS__, 'errorHandler']);
if ($type & self::DEVELOPMENT)
{
self::startTimer('_global');
}
// Assign default context
static $defaults = [
'hostname' => 'SERVER_NAME',
'http_user_agent' => 'HTTP_USER_AGENT',
'http_referrer' => 'HTTP_REFERER',
'user_addr' => 'REMOTE_ADDR',
'server_addr' => 'SERVER_ADDR',
'root_directory' => 'DOCUMENT_ROOT',
];
foreach ($defaults as $a => $b)
{
if (isset($_SERVER[$b]) && !isset(self::$context[$a]))
{
self::$context[$a] = $_SERVER[$b];
}
}
}
/**
* Reset error management to PHP defaults
* @return boolean
*/
static public function disable()
{
self::$enabled = false;
ini_set('error_prepend_string', null);
ini_set('error_append_string', null);
ini_set('log_errors', false);
ini_set('display_errors', false);
ini_set('error_reporting', E_ALL & ~E_DEPRECATED & ~E_STRICT);
restore_error_handler();
return restore_exception_handler();
}
/**
* Sets a microsecond timer to track time and memory usage
* @param string $name Timer name
*/
static public function startTimer($name)
{
self::$run_trace[$name] = [microtime(true), memory_get_usage()];
}
/**
* Stops a timer and return time spent and memory used
* @param string $name Timer name
*/
static public function stopTimer($name)
{
self::$run_trace[$name] = [
microtime(true) - self::$run_trace[$name][0],
memory_get_usage() - self::$run_trace[$name][1],
];
return self::$run_trace[$name];
}
/**
* Sets a log file to record errors
* @param string $file Error log file
*/
static public function setLogFile($file)
{
ini_set('log_errors', true);
return ini_set('error_log', $file);
}
/**
* Sets an email address that should receive the logs
* Set to FALSE to disable email sending (default)
* @param string $email Email address
*/
static public function setEmail($email)
{
self::$email_errors = $email;
}
/**
* @deprecated
*/
static public function setExtraDebugEnv($env)
{
self::setContext($env);
}
/**
* Set the report context
* @param mixed $env Variable content, could be application version, or an array of information...
*/
static public function setContext(array $context)
{
self::$context = array_merge(self::$context, $context);
}
/**
* Enable or disable reporting of errors to a remote URL
* @param null|string $url Reporting URL
* @param boolean $auto Automatic reporting? If not users will be able to report the error by clicking a button on the error page
*/
static public function setRemoteReporting($url, $auto)
{
self::$report_url = empty($url) ? null : $url;
self::$report_auto = (bool) $auto;
}
/**
* Set the HTML header used by the debug error page
* @param string $html HTML header
*/
static public function setHtmlHeader($html)
{
ini_set('error_prepend_string', $html);
}
/**
* Set the HTML footer used by the debug error page
* @param string $html HTML footer
*/
static public function setHtmlFooter($html)
{
ini_set('error_append_string', $html);
}
/**
* Set the content of the HTML template used to display an error in production
* {$ref} will be replaced by the error reference if log or email is enabled
* <if(email)>...</if> block will be removed if email reporting is disabled
* <if(log)>...</if> block will be removed if log reporting is disabled
* @param string $html HTML template
*/
static public function setProductionErrorTemplate($html)
{
self::$production_error_template = $html;
}
static public function setCustomExceptionHandler($class, Callable $callback)
{
self::$custom_handlers[$class] = $callback;
}
static public function debug(...$vars)
{
echo '<pre>';
foreach ($vars as $var) {
echo self::dump($var);
echo '<hr />';
}
echo '</pre>';
}
/**
* Copy of var_dump but returns a string instead of a variable
* @param mixed $var variable to dump
* @param bool $hide_values Do not return values if set to TRUE
* @param integer $level Indentation level (internal use)
* @return string
*/
static public function dump($var, $hide_values = false, $level = 0)
{
if ($level > 20)
{
return '*RECURSION*';
}
switch (gettype($var))
{
case 'boolean':
return 'bool(' . ($var ? 'true' : 'false') . ')';
case 'integer':
return 'int(' . $var . ')';
case 'double':
return 'float(' . $var . ')';
case 'string':
return 'string(' . strlen($var) . ') "' . ($hide_values ? '***HIDDEN***' : $var) . '"';
case 'NULL':
return 'NULL';
case 'resource':
return 'resource(' . (int)$var . ') of type (' . get_resource_type($var) . ')';
case 'array':
case 'object':
if (is_object($var))
{
$out = 'object(' . get_class($var) . ') (' . count((array) $var) . ') {' . PHP_EOL;
}
else
{
$out = 'array(' . count((array) $var) . ') {' . PHP_EOL;
}
$level++;
if ($var instanceof \Traversable) {
$var2 = [];
// Iterate as long as we can
while (@$var->valid()) {
$var2[] = $var->current();
$var->next();
}
$var = $var2;
}
foreach ((array)$var as $key=>$value)
{
$out .= str_repeat(' ', $level * 2);
$out .= is_string($key) ? '["' . $key . '"]' : '[' . $key . ']';
$out .= '=> ' . self::dump($value, $hide_values, $level) . PHP_EOL;
}