XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
errorhandler.php
1<?php
14
15
17{
24 public $_errors = [];
25
32 public $_showErrors = false;
33
40 public $_isFatal = false;
41
49 public function __construct()
50 {
51 set_error_handler('XoopsErrorHandler_HandleError');
52 register_shutdown_function('XoopsErrorHandler_Shutdown');
53 }
54
60 public static function &getInstance()
61 {
62 static $instance = null;
63 if (empty($instance)) {
64 $instance = new XoopsErrorHandler();
65 }
66 return $instance;
67 }
68
76 public function activate($showErrors=false)
77 {
78 $this->_showErrors = $showErrors;
79 }
80
88 public function handleError($error)
89 {
90 if (E_USER_ERROR == $error['errno']) {
91 $this->_isFatal = true;
92 exit($error['errstr']);
93 }
94 if (($error['errno'] & error_reporting()) !== $error['errno']) {
95 return;
96 }
97 $this->_errors[] = $error;
98 }
99
119 public function renderErrors()
120 {
121 //
122 // TODO We should plan new style about the following lines.
123 //
124 $output = '';
125 if ($this->_isFatal) {
126 $output .= 'This page cannot be displayed due to an internal error.<br><br>';
127 $output .= 'If you are the administrator of this site, please visit the <a href="https://github.com/xoopscube/">XOOPSCube Project Site</a> for assistance.<br><br>';
128 }
129 if (!$this->_showErrors || empty($this->_errors)) {
130 return $output;
131 }
132
133 $output = [];
134 foreach ($this->_errors as $error) {
135 switch ($error['errno']) {
136 case E_USER_NOTICE:
137 $out = 'Notice [Xoops]: ';
138 break;
139 case E_USER_WARNING:
140 $out = 'Warning [Xoops]: ';
141 break;
142 case E_USER_ERROR:
143 $out = 'Error [Xoops]: ';
144 break;
145 case E_USER_DEPRECATED:
146 $out = 'Deprecated [Xoops]: ';
147 break;
148 case E_NOTICE:
149 $out = 'Notice [PHP]: ';
150 break;
151 case E_WARNING:
152 $out = 'Warning [PHP]: ';
153 break;
154 case E_DEPRECATED:
155 $out = 'Deprecated [PHP]: ';
156 break;
157 case E_STRICT:
158 $out = 'Strict [PHP]: ';
159 break;
160 case E_ERROR:
161 $out = 'Fatal [PHP]: ';
162 break;
163 default:
164 $out = 'Unknown Condition [' . $error['errno'] . ']: ';
165 }
166
167
168 // output
169 $out .= sprintf('%s in file %s line %s', $error['errstr'], $error['errfile'], $error['errline']);
170 $md5 = md5($out);
171 $count =[];
172 if (isset($output[$md5])) {
173
174 $output[$md5] = preg_replace('/\‍(\d+\‍)$/', '(' . ++$count[$md5] . ')', $output[$md5]);
175 } else {
176 $output[$md5] = $out . ' (1)';
177 $count[$md5] = 1;
178 }
179 }
180 $ret = '<div class="alert error">';
181 $ret .= implode("<br>\n", $output);
182 $ret .= '</div>';
183 return $ret;
184 }
185}
186
201function XoopsErrorHandler_HandleError($errNo, $errStr, $errFile, $errLine)
202{
203 // NOTE: we only store relative pathnames
204 $new_error = [
205 'errno' => $errNo,
206 'errstr' => $errStr,
207 'errfile' => str_replace([XOOPS_ROOT_PATH, XOOPS_TRUST_PATH], ['(html)', '(trust)'], $errFile),
208 'errline' => $errLine
209 ];
210 $error_handler =& XoopsErrorHandler::getInstance();
211 $error_handler->handleError($new_error);
212}
213
220function XoopsErrorHandler_Shutdown()
221{
222 $error = error_get_last();
223 if (E_ERROR === $error['type']) {
224 XoopsErrorHandler_HandleError($error['type'], $error['message'], $error['file'], $error['line']);
225 }
226 $error_handler =& XoopsErrorHandler::getInstance();
227 echo $error_handler->renderErrors();
228}
activate($showErrors=false)
static & getInstance()