add cms, add todo
[ssproject1617.git] / testcms-final-anon / system / classes / error.php
1 <?php defined('IN_CMS') or die('No direct access allowed.');
2
3 class Error {
4
5 public static function native($code, $error, $file, $line) {
6 // no error reporting nothing to do
7 if(error_reporting() === 0) {
8 return;
9 }
10
11 $exception = new ErrorException($error, $code, 0, $file, $line);
12
13 if(in_array($code, Config::get('error.ignore', array()))) {
14 return static::log($exception);
15 }
16
17 static::exception($exception);
18 }
19
20 public static function shutdown() {
21 if(!is_null($error = error_get_last())) {
22 extract($error, EXTR_SKIP);
23 static::exception(new ErrorException($message, $type, 0, $file, $line));
24 }
25 }
26
27 public static function exception($e) {
28 // Clean the output buffer.
29 if(ob_get_level() > 0) {
30 ob_clean();
31 }
32
33 // log exception
34 static::log($e);
35
36 // Display error
37 if(Config::get('error.detail', true)) {
38 // Get the error file.
39 $file = $e->getFile();
40
41 // Trim the period off of the error message.
42 $message = rtrim($e->getMessage(), '.');
43
44 $line = $e->getLine();
45 $trace = $e->getTraceAsString();
46 $contexts = static::context($file, $e->getLine());
47
48 require PATH . 'system/admin/theme/error_php.php';
49 } else {
50 require PATH . 'system/admin/theme/error_500.php';
51 }
52
53 exit(1);
54 }
55
56 private static function context($path, $line, $padding = 5) {
57 if(file_exists($path)) {
58 $file = file($path, FILE_IGNORE_NEW_LINES);
59
60 array_unshift($file, '');
61
62 // Calculate the starting position.
63 $start = $line - $padding;
64
65 if($start < 0) {
66 $start = 0;
67 }
68
69 // Calculate the context length.
70 $length = ($line - $start) + $padding + 1;
71
72 if(($start + $length) > count($file) - 1) {
73 $length = null;
74 }
75
76 return array_slice($file, $start, $length, true);
77 }
78
79 return array();
80 }
81
82 public static function log($e) {
83 if(Config::get('error.log')) {
84 Log::exception($e);
85 }
86 }
87
88 }