Merge remote-tracking branch 'origin/master'
[phpmyadmin/madhuracj.git] / libraries / Error_Handler.class.php
blobac132fa2fc35bea8af8b4cb99b9d4617753965cd
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
130 * @return bool
132 protected function _logError($error)
134 return error_log($error->getMessage());
138 * trigger a custom error
140 * @param string $errorInfo
141 * @param integer $errorNumber
142 * @param string $file
143 * @param integer $line
145 public function triggerError($errorInfo, $errorNumber = null, $file = null, $line = null)
147 // we could also extract file and line from backtrace and call handleError() directly
148 trigger_error($errorInfo, $errorNumber);
152 * display fatal error and exit
154 * @param PMA_Error $error
156 protected function _dispFatalError($error)
158 if (! headers_sent()) {
159 $this->_dispPageStart($error);
161 $error->display();
162 $this->_dispPageEnd();
163 exit;
167 * display the whole error page with all errors
170 public function dispErrorPage()
172 if (! headers_sent()) {
173 $this->_dispPageStart();
175 $this->dispAllErrors();
176 $this->_dispPageEnd();
180 * display user errors not displayed
183 public function dispUserErrors()
185 foreach ($this->getErrors() as $error) {
186 if ($error->isUserError() && ! $error->isDisplayed()) {
187 $error->display();
193 * display HTML header
195 * @param PMA_error $error
197 protected function _dispPageStart($error = null)
199 echo '<html><head><title>';
200 if ($error) {
201 echo $error->getTitle();
202 } else {
203 echo 'phpMyAdmin error reporting page';
205 echo '</title></head>';
209 * display HTML footer
212 protected function _dispPageEnd()
214 echo '</body></html>';
218 * display all errors regardless already displayed or user errors
221 public function dispAllErrors()
223 foreach ($this->getErrors() as $error) {
224 $error->display();
229 * display errors not displayed
232 public function dispErrors()
234 if ($GLOBALS['cfg']['Error_Handler']['display']) {
235 foreach ($this->getErrors() as $error) {
236 if ($error instanceof PMA_Error) {
237 if (! $error->isDisplayed()) {
238 $error->display();
240 } else {
241 var_dump($error);
244 } else {
245 $this->dispUserErrors();
250 * look in session for saved errors
253 protected function _checkSavedErrors()
255 if (isset($_SESSION['errors'])) {
257 // restore saved errors
258 foreach ($_SESSION['errors'] as $hash => $error) {
259 if ($error instanceof PMA_Error && ! isset($this->_errors[$hash])) {
260 $this->_errors[$hash] = $error;
263 //$this->_errors = array_merge($_SESSION['errors'], $this->_errors);
265 // delet stored errors
266 $_SESSION['errors'] = array();
267 unset($_SESSION['errors']);
272 * return count of errors
274 * @return integer number of errors occoured
276 public function countErrors()
278 return count($this->getErrors());
282 * return count of user errors
284 * @return integer number of user errors occoured
286 public function countUserErrors()
288 $count = 0;
289 if ($this->countErrors()) {
290 foreach ($this->getErrors() as $error) {
291 if ($error->isUserError()) {
292 $count++;
297 return $count;
301 * whether use errors occured or not
303 * @return boolean
305 public function hasUserErrors()
307 return (bool) $this->countUserErrors();
311 * whether errors occured or not
313 * @return boolean
315 public function hasErrors()
317 return (bool) $this->countErrors();
321 * number of errors to be displayed
323 * @return integer number of errors to be displayed
325 public function countDisplayErrors()
327 if ($GLOBALS['cfg']['Error_Handler']['display']) {
328 return $this->countErrors();
329 } else {
330 return $this->countUserErrors();
335 * whether there are errors to display or not
337 * @return boolean
339 public function hasDisplayErrors()
341 return (bool) $this->countDisplayErrors();