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()
35 * Do not set ourselves as error handler in case of testsuite.
37 * This behavior is not tested there and breaks other tests as they
38 * rely on PHPUnit doing it's own error handling which we break here.
40 if (!defined('TESTSUITE')) {
41 set_error_handler(array($this, 'handleError'));
48 * stores errors in session
51 public function __destruct()
53 if (isset($_SESSION)) {
54 if (! isset($_SESSION['errors'])) {
55 $_SESSION['errors'] = array();
58 if (isset($GLOBALS['cfg']['Error_Handler'])
59 && $GLOBALS['cfg']['Error_Handler']['gather']
61 // remember all errors
62 $_SESSION['errors'] = array_merge(
67 // remember only not displayed errors
68 foreach ($this->errors
as $key => $error) {
70 * We don't want to store all errors here as it would
71 * explode user session. In case you want them all set
72 * $GLOBALS['cfg']['Error_Handler']['gather'] to true
74 if (count($_SESSION['errors']) >= 20) {
75 $error = new PMA_Error(
77 __('Too many error messages, some are not displayed.'),
81 $_SESSION['errors'][$error->getHash()] = $error;
83 } else if (($error instanceof PMA_Error
)
84 && ! $error->isDisplayed()
86 $_SESSION['errors'][$key] = $error;
94 * returns array with all errors
96 * @return array PMA_Error_Handler::$_errors
98 protected function getErrors()
100 $this->checkSavedErrors();
101 return $this->errors
;
105 * Error handler - called when errors are triggered/occured
107 * This calls the addError() function, escaping the error string
109 * @param integer $errno error number
110 * @param string $errstr error string
111 * @param string $errfile error file
112 * @param integer $errline error line
116 public function handleError($errno, $errstr, $errfile, $errline)
118 $this->addError($errstr, $errno, $errfile, $errline, true);
122 * Add an error; can also be called directly (with or without escaping)
124 * The following error types cannot be handled with a user defined function:
125 * E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR,
127 * and most of E_STRICT raised in the file where set_error_handler() is called.
129 * Do not use the context parameter as we want to avoid storing the
130 * complete $GLOBALS inside $_SESSION['errors']
132 * @param string $errstr error string
133 * @param integer $errno error number
134 * @param string $errfile error file
135 * @param integer $errline error line
136 * @param boolean $escape whether to escape the error string
140 public function addError($errstr, $errno, $errfile, $errline, $escape = true)
143 $errstr = htmlspecialchars($errstr);
145 // create error object
146 $error = new PMA_Error(
153 // do not repeat errors
154 $this->errors
[$error->getHash()] = $error;
156 switch ($error->getNumber()) {
164 case E_COMPILE_WARNING
:
166 case E_RECOVERABLE_ERROR
:
167 // just collect the error
168 // display is called from outside
173 case E_COMPILE_ERROR
:
175 // FATAL error, dislay it and exit
176 $this->dispFatalError($error);
184 * log error to configured log facility
186 * @param PMA_Error $error the error
192 protected function logError($error)
194 return error_log($error->getMessage());
198 * trigger a custom error
200 * @param string $errorInfo error message
201 * @param integer $errorNumber error number
202 * @param string $file file name
203 * @param integer $line line number
207 public function triggerError($errorInfo, $errorNumber = null,
208 $file = null, $line = null
210 // we could also extract file and line from backtrace
211 // and call handleError() directly
212 trigger_error($errorInfo, $errorNumber);
216 * display fatal error and exit
218 * @param PMA_Error $error the error
222 protected function dispFatalError($error)
224 if (! headers_sent()) {
225 $this->dispPageStart($error);
228 $this->dispPageEnd();
233 * display the whole error page with all errors
237 public function dispErrorPage()
239 if (! headers_sent()) {
240 $this->dispPageStart();
242 $this->dispAllErrors();
243 $this->dispPageEnd();
247 * Displays user errors not displayed
251 public function dispUserErrors()
253 echo $this->getDispUserErrors();
257 * Renders user errors not displayed
261 public function getDispUserErrors()
264 foreach ($this->getErrors() as $error) {
265 if ($error->isUserError() && ! $error->isDisplayed()) {
266 $retval .= $error->getDisplay();
273 * display HTML header
275 * @param PMA_error $error the error
279 protected function dispPageStart($error = null)
281 PMA_Response
::getInstance()->disable();
282 echo '<html><head><title>';
284 echo $error->getTitle();
286 echo 'phpMyAdmin error reporting page';
288 echo '</title></head>';
292 * display HTML footer
296 protected function dispPageEnd()
298 echo '</body></html>';
302 * display all errors regardless already displayed or user errors
306 public function dispAllErrors()
308 foreach ($this->getErrors() as $error) {
314 * renders errors not displayed
318 public function getDispErrors()
321 if ($GLOBALS['cfg']['Error_Handler']['display']) {
322 foreach ($this->getErrors() as $error) {
323 if ($error instanceof PMA_Error
) {
324 if (! $error->isDisplayed()) {
325 $retval .= $error->getDisplay();
330 $retval .= ob_get_contents();
335 $retval .= $this->getDispUserErrors();
341 * displays errors not displayed
345 public function dispErrors()
347 echo $this->getDispErrors();
351 * look in session for saved errors
355 protected function checkSavedErrors()
357 if (isset($_SESSION['errors'])) {
359 // restore saved errors
360 foreach ($_SESSION['errors'] as $hash => $error) {
361 if ($error instanceof PMA_Error
&& ! isset($this->errors
[$hash])) {
362 $this->errors
[$hash] = $error;
365 //$this->errors = array_merge($_SESSION['errors'], $this->errors);
367 // delete stored errors
368 $_SESSION['errors'] = array();
369 unset($_SESSION['errors']);
374 * return count of errors
376 * @return integer number of errors occoured
378 public function countErrors()
380 return count($this->getErrors());
384 * return count of user errors
386 * @return integer number of user errors occoured
388 public function countUserErrors()
391 if ($this->countErrors()) {
392 foreach ($this->getErrors() as $error) {
393 if ($error->isUserError()) {
403 * whether use errors occured or not
407 public function hasUserErrors()
409 return (bool) $this->countUserErrors();
413 * whether errors occured or not
417 public function hasErrors()
419 return (bool) $this->countErrors();
423 * number of errors to be displayed
425 * @return integer number of errors to be displayed
427 public function countDisplayErrors()
429 if ($GLOBALS['cfg']['Error_Handler']['display']) {
430 return $this->countErrors();
432 return $this->countUserErrors();
437 * whether there are errors to display or not
441 public function hasDisplayErrors()
443 return (bool) $this->countDisplayErrors();