Translated using Weblate.
[phpmyadmin.git] / libraries / Error_Handler.class.php
blobb6b9bf0d80e3ec1fbc9d1c207a78fdb0839de89d
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Holds class PMA_Error_Handler
6 * @package PhpMyAdmin
7 */
9 /**
12 require_once './libraries/Error.class.php';
14 /**
15 * handling errors
17 * @package PhpMyAdmin
19 class PMA_Error_Handler
21 /**
22 * holds errors to be displayed or reported later ...
24 * @var array of PMA_Error
26 protected $_errors = array();
28 /**
29 * Constructor - set PHP error handler
32 public function __construct()
34 set_error_handler(array($this, 'handleError'));
37 /**
38 * Destructor
40 * stores errors in session
43 public function __destruct()
45 if (isset($_SESSION)) {
46 if (! isset($_SESSION['errors'])) {
47 $_SESSION['errors'] = array();
50 if ($GLOBALS['cfg']['Error_Handler']['gather']) {
51 // remember all errors
52 $_SESSION['errors'] = array_merge($_SESSION['errors'], $this->_errors);
53 } else {
54 // remember only not displayed errors
55 foreach ($this->_errors as $key => $error) {
56 /**
57 * We don't want to store all errors here as it would explode user
58 * session. In case you want them all set
59 * $GLOBALS['cfg']['Error_Handler']['gather'] to true
61 if (count($_SESSION['errors']) >= 20) {
62 $error = new PMA_Error(0, __('Too many error messages, some are not displayed.'), __FILE__, __LINE__);
63 $_SESSION['errors'][$error->getHash()] = $error;
65 if (($error instanceof PMA_Error) && ! $error->isDisplayed()) {
66 $_SESSION['errors'][$key] = $error;
73 /**
74 * returns array with all errors
76 * @return array PMA_Error_Handler::$_errors
78 protected function getErrors()
80 $this->_checkSavedErrors();
81 return $this->_errors;
84 /**
85 * Error handler - called when errors are triggered/occured
87 * The following error types cannot be handled with a user defined function:
88 * E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR,
89 * E_COMPILE_WARNING,
90 * and most of E_STRICT raised in the file where set_error_handler() is called.
92 * Do not use the context parameter as we want to avoid storing the
93 * complete $GLOBALS inside $_SESSION['errors']
95 * @param integer $errno
96 * @param string $errstr
97 * @param string $errfile
98 * @param integer $errline
100 public function handleError($errno, $errstr, $errfile, $errline)
102 // create error object
103 $error = new PMA_Error($errno, htmlspecialchars($errstr), $errfile, $errline);
105 // do not repeat errors
106 $this->_errors[$error->getHash()] = $error;
108 switch ($error->getNumber()) {
109 case E_USER_NOTICE:
110 case E_USER_WARNING:
111 case E_STRICT:
112 case E_DEPRECATED:
113 case E_NOTICE:
114 case E_WARNING:
115 case E_CORE_WARNING:
116 case E_COMPILE_WARNING:
117 case E_USER_ERROR:
118 case E_RECOVERABLE_ERROR:
119 // just collect the error
120 // display is called from outside
121 break;
122 case E_ERROR:
123 case E_PARSE:
124 case E_CORE_ERROR:
125 case E_COMPILE_ERROR:
126 default:
127 // FATAL error, dislay it and exit
128 $this->_dispFatalError($error);
129 exit;
130 break;
135 * log error to configured log facility
137 * @todo finish!
138 * @param PMA_Error $error
139 * @return bool
141 protected function _logError($error)
143 return error_log($error->getMessage());
147 * trigger a custom error
149 * @param string $errorInfo
150 * @param integer $errorNumber
151 * @param string $file
152 * @param integer $line
154 public function triggerError($errorInfo, $errorNumber = null, $file = null, $line = null)
156 // we could also extract file and line from backtrace and call handleError() directly
157 trigger_error($errorInfo, $errorNumber);
161 * display fatal error and exit
163 * @param PMA_Error $error
165 protected function _dispFatalError($error)
167 if (! headers_sent()) {
168 $this->_dispPageStart($error);
170 $error->display();
171 $this->_dispPageEnd();
172 exit;
176 * display the whole error page with all errors
179 public function dispErrorPage()
181 if (! headers_sent()) {
182 $this->_dispPageStart();
184 $this->dispAllErrors();
185 $this->_dispPageEnd();
189 * display user errors not displayed
192 public function dispUserErrors()
194 foreach ($this->getErrors() as $error) {
195 if ($error->isUserError() && ! $error->isDisplayed()) {
196 $error->display();
202 * display HTML header
204 * @param PMA_error $error
206 protected function _dispPageStart($error = null)
208 echo '<html><head><title>';
209 if ($error) {
210 echo $error->getTitle();
211 } else {
212 echo 'phpMyAdmin error reporting page';
214 echo '</title></head>';
218 * display HTML footer
221 protected function _dispPageEnd()
223 echo '</body></html>';
227 * display all errors regardless already displayed or user errors
230 public function dispAllErrors()
232 foreach ($this->getErrors() as $error) {
233 $error->display();
238 * display errors not displayed
241 public function dispErrors()
243 if ($GLOBALS['cfg']['Error_Handler']['display']) {
244 foreach ($this->getErrors() as $error) {
245 if ($error instanceof PMA_Error) {
246 if (! $error->isDisplayed()) {
247 $error->display();
249 } else {
250 var_dump($error);
253 } else {
254 $this->dispUserErrors();
259 * look in session for saved errors
262 protected function _checkSavedErrors()
264 if (isset($_SESSION['errors'])) {
266 // restore saved errors
267 foreach ($_SESSION['errors'] as $hash => $error) {
268 if ($error instanceof PMA_Error && ! isset($this->_errors[$hash])) {
269 $this->_errors[$hash] = $error;
272 //$this->_errors = array_merge($_SESSION['errors'], $this->_errors);
274 // delet stored errors
275 $_SESSION['errors'] = array();
276 unset($_SESSION['errors']);
281 * return count of errors
283 * @return integer number of errors occoured
285 public function countErrors()
287 return count($this->getErrors());
291 * return count of user errors
293 * @return integer number of user errors occoured
295 public function countUserErrors()
297 $count = 0;
298 if ($this->countErrors()) {
299 foreach ($this->getErrors() as $error) {
300 if ($error->isUserError()) {
301 $count++;
306 return $count;
310 * whether use errors occured or not
312 * @return boolean
314 public function hasUserErrors()
316 return (bool) $this->countUserErrors();
320 * whether errors occured or not
322 * @return boolean
324 public function hasErrors()
326 return (bool) $this->countErrors();
330 * number of errors to be displayed
332 * @return integer number of errors to be displayed
334 public function countDisplayErrors()
336 if ($GLOBALS['cfg']['Error_Handler']['display']) {
337 return $this->countErrors();
338 } else {
339 return $this->countUserErrors();
344 * whether there are errors to display or not
346 * @return boolean
348 public function hasDisplayErrors()
350 return (bool) $this->countDisplayErrors();