2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Holds class PMA_Error_Handler
12 require_once './libraries/Error.class.php';
19 class PMA_Error_Handler
22 * holds errors to be displayed or reported later ...
24 * @var array of PMA_Error
26 protected $_errors = array();
29 * Constructor - set PHP error handler
32 public function __construct()
34 set_error_handler(array($this, 'handleError'));
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
);
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;
65 * returns array with all errors
67 * @return array PMA_Error_Handler::$_errors
69 protected function getErrors()
71 $this->_checkSavedErrors();
72 return $this->_errors
;
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,
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()) {
107 case E_COMPILE_WARNING
:
109 case E_RECOVERABLE_ERROR
:
110 // just collect the error
111 // display is called from outside
116 case E_COMPILE_ERROR
:
118 // FATAL error, dislay it and exit
119 $this->_dispFatalError($error);
126 * log error to configured log facility
129 * @param PMA_Error $error
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);
162 $this->_dispPageEnd();
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()) {
193 * display HTML header
195 * @param PMA_error $error
197 protected function _dispPageStart($error = null)
199 echo '<html><head><title>';
201 echo $error->getTitle();
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) {
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()) {
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()
289 if ($this->countErrors()) {
290 foreach ($this->getErrors() as $error) {
291 if ($error->isUserError()) {
301 * whether use errors occured or not
305 public function hasUserErrors()
307 return (bool) $this->countUserErrors();
311 * whether errors occured or not
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();
330 return $this->countUserErrors();
335 * whether there are errors to display or not
339 public function hasDisplayErrors()
341 return (bool) $this->countDisplayErrors();