Merge remote-tracking branch 'origin/master' into drizzle
[phpmyadmin.git] / libraries / Error_Handler.class.php
blob1a9cdb0169ee4d591ef633e57fd149608ab34ce8
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 if (($error instanceof PMA_Error) && ! $error->isDisplayed()) {
57 $_SESSION['errors'][$key] = $error;
64 /**
65 * returns array with all errors
67 * @return array PMA_Error_Handler::$_errors
69 protected function getErrors()
71 $this->_checkSavedErrors();
72 return $this->_errors;
75 /**
76 * Error handler - called when errors are triggered/occured
78 * The following error types cannot be handled with a user defined function:
79 * E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR,
80 * E_COMPILE_WARNING,
81 * and most of E_STRICT raised in the file where set_error_handler() is called.
83 * Do not use the context parameter as we want to avoid storing the
84 * complete $GLOBALS inside $_SESSION['errors']
86 * @param integer $errno
87 * @param string $errstr
88 * @param string $errfile
89 * @param integer $errline
91 public function handleError($errno, $errstr, $errfile, $errline)
93 // create error object
94 $error = new PMA_Error($errno, $errstr, $errfile, $errline);
96 // do not repeat errors
97 $this->_errors[$error->getHash()] = $error;
99 switch ($error->getNumber()) {
100 case E_USER_NOTICE:
101 case E_USER_WARNING:
102 case E_STRICT:
103 case E_DEPRECATED:
104 case E_NOTICE:
105 case E_WARNING:
106 case E_CORE_WARNING:
107 case E_COMPILE_WARNING:
108 case E_USER_ERROR:
109 case E_RECOVERABLE_ERROR:
110 // just collect the error
111 // display is called from outside
112 break;
113 case E_ERROR:
114 case E_PARSE:
115 case E_CORE_ERROR:
116 case E_COMPILE_ERROR:
117 default:
118 // FATAL error, dislay it and exit
119 $this->_dispFatalError($error);
120 exit;
121 break;
126 * log error to configured log facility
128 * @todo finish!
129 * @param PMA_Error $error
131 protected function _logError($error)
133 return error_log($error->getMessage());
137 * trigger a custom error
139 * @param string $errorInfo
140 * @param integer $errorNumber
141 * @param string $file
142 * @param integer $line
144 public function triggerError($errorInfo, $errorNumber = null, $file = null, $line = null)
146 // we could also extract file and line from backtrace and call handleError() directly
147 trigger_error($errorInfo, $errorNumber);
151 * display fatal error and exit
153 * @param PMA_Error $error
155 protected function _dispFatalError($error)
157 if (! headers_sent()) {
158 $this->_dispPageStart($error);
160 $error->display();
161 $this->_dispPageEnd();
162 exit;
166 * display the whole error page with all errors
169 public function dispErrorPage()
171 if (! headers_sent()) {
172 $this->_dispPageStart();
174 $this->dispAllErrors();
175 $this->_dispPageEnd();
179 * display user errors not displayed
182 public function dispUserErrors()
184 foreach ($this->getErrors() as $error) {
185 if ($error->isUserError() && ! $error->isDisplayed()) {
186 $error->display();
192 * display HTML header
194 * @param PMA_error $error
196 protected function _dispPageStart($error = null)
198 echo '<html><head><title>';
199 if ($error) {
200 echo $error->getTitle();
201 } else {
202 echo 'phpMyAdmin error reporting page';
204 echo '</title></head>';
208 * display HTML footer
211 protected function _dispPageEnd()
213 echo '</body></html>';
217 * display all errors regardless already displayed or user errors
220 public function dispAllErrors()
222 foreach ($this->getErrors() as $error) {
223 $error->display();
228 * display errors not displayed
231 public function dispErrors()
233 if ($GLOBALS['cfg']['Error_Handler']['display']) {
234 foreach ($this->getErrors() as $error) {
235 if ($error instanceof PMA_Error) {
236 if (! $error->isDisplayed()) {
237 $error->display();
239 } else {
240 var_dump($error);
243 } else {
244 $this->dispUserErrors();
249 * look in session for saved errors
252 protected function _checkSavedErrors()
254 if (isset($_SESSION['errors'])) {
256 // restore saved errors
257 foreach ($_SESSION['errors'] as $hash => $error) {
258 if ($error instanceof PMA_Error && ! isset($this->_errors[$hash])) {
259 $this->_errors[$hash] = $error;
262 //$this->_errors = array_merge($_SESSION['errors'], $this->_errors);
264 // delet stored errors
265 $_SESSION['errors'] = array();
266 unset($_SESSION['errors']);
271 * return count of errors
273 * @return integer number of errors occoured
275 public function countErrors()
277 return count($this->getErrors());
281 * return count of user errors
283 * @return integer number of user errors occoured
285 public function countUserErrors()
287 $count = 0;
288 if ($this->countErrors()) {
289 foreach ($this->getErrors() as $error) {
290 if ($error->isUserError()) {
291 $count++;
296 return $count;
300 * whether use errors occured or not
302 * @return boolean
304 public function hasUserErrors()
306 return (bool) $this->countUserErrors();
310 * whether errors occured or not
312 * @return boolean
314 public function hasErrors()
316 return (bool) $this->countErrors();
320 * number of errors to be displayed
322 * @return integer number of errors to be displayed
324 public function countDisplayErrors()
326 if ($GLOBALS['cfg']['Error_Handler']['display']) {
327 return $this->countErrors();
328 } else {
329 return $this->countUserErrors();
334 * whether there are errors to display or not
336 * @return boolean
338 public function hasDisplayErrors()
340 return (bool) $this->countDisplayErrors();