2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Holds class PMA_Error_Handler
9 if (! defined('PHPMYADMIN')) {
16 require_once './libraries/Error.class.php';
23 class PMA_Error_Handler
26 * holds errors to be displayed or reported later ...
30 protected $errors = array();
33 * Constructor - set PHP error handler
36 public function __construct()
39 * Do not set ourselves as error handler in case of testsuite.
41 * This behavior is not tested there and breaks other tests as they
42 * rely on PHPUnit doing it's own error handling which we break here.
44 if (!defined('TESTSUITE')) {
45 set_error_handler(array($this, 'handleError'));
52 * stores errors in session
55 public function __destruct()
57 if (isset($_SESSION)) {
58 if (! isset($_SESSION['errors'])) {
59 $_SESSION['errors'] = array();
62 // remember only not displayed errors
63 foreach ($this->errors
as $key => $error) {
65 * We don't want to store all errors here as it would
66 * explode user session.
68 if (count($_SESSION['errors']) >= 10) {
69 $error = new PMA_Error(
71 __('Too many error messages, some are not displayed.'),
75 $_SESSION['errors'][$error->getHash()] = $error;
77 } else if (($error instanceof PMA_Error
)
78 && ! $error->isDisplayed()
80 $_SESSION['errors'][$key] = $error;
87 * returns array with all errors
91 protected function getErrors()
93 $this->checkSavedErrors();
98 * returns the errors occurred in the current run only.
99 * Does not include the errors save din the SESSION
101 * @return PMA_Error[]
103 public function getCurrentErrors()
105 return $this->errors
;
109 * Error handler - called when errors are triggered/occurred
111 * This calls the addError() function, escaping the error string
112 * Ignores the errors wherever Error Control Operator (@) is used.
114 * @param integer $errno error number
115 * @param string $errstr error string
116 * @param string $errfile error file
117 * @param integer $errline error line
121 public function handleError($errno, $errstr, $errfile, $errline)
123 // check if Error Control Operator (@) was used.
124 if (error_reporting() == 0) {
127 $this->addError($errstr, $errno, $errfile, $errline, true);
131 * Add an error; can also be called directly (with or without escaping)
133 * The following error types cannot be handled with a user defined function:
134 * E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR,
136 * and most of E_STRICT raised in the file where set_error_handler() is called.
138 * Do not use the context parameter as we want to avoid storing the
139 * complete $GLOBALS inside $_SESSION['errors']
141 * @param string $errstr error string
142 * @param integer $errno error number
143 * @param string $errfile error file
144 * @param integer $errline error line
145 * @param boolean $escape whether to escape the error string
149 public function addError($errstr, $errno, $errfile, $errline, $escape = true)
152 $errstr = htmlspecialchars($errstr);
154 // create error object
155 $error = new PMA_Error(
162 // do not repeat errors
163 $this->errors
[$error->getHash()] = $error;
165 switch ($error->getNumber()) {
173 case E_COMPILE_WARNING
:
175 case E_RECOVERABLE_ERROR
:
176 // just collect the error
177 // display is called from outside
182 case E_COMPILE_ERROR
:
184 // FATAL error, display it and exit
185 $this->dispFatalError($error);
192 * log error to configured log facility
194 * @param PMA_Error $error the error
200 protected function logError($error)
202 return error_log($error->getMessage());
206 * trigger a custom error
208 * @param string $errorInfo error message
209 * @param integer $errorNumber error number
213 public function triggerError($errorInfo, $errorNumber = null)
215 // we could also extract file and line from backtrace
216 // and call handleError() directly
217 trigger_error($errorInfo, $errorNumber);
221 * display fatal error and exit
223 * @param PMA_Error $error the error
227 protected function dispFatalError($error)
229 if (! headers_sent()) {
230 $this->dispPageStart($error);
233 $this->dispPageEnd();
238 * Displays user errors not displayed
242 public function dispUserErrors()
244 echo $this->getDispUserErrors();
248 * Renders user errors not displayed
252 public function getDispUserErrors()
255 foreach ($this->getErrors() as $error) {
256 if ($error->isUserError() && ! $error->isDisplayed()) {
257 $retval .= $error->getDisplay();
264 * display HTML header
266 * @param PMA_error $error the error
270 protected function dispPageStart($error = null)
272 PMA_Response
::getInstance()->disable();
273 echo '<html><head><title>';
275 echo $error->getTitle();
277 echo 'phpMyAdmin error reporting page';
279 echo '</title></head>';
283 * display HTML footer
287 protected function dispPageEnd()
289 echo '</body></html>';
293 * renders errors not displayed
297 public function getDispErrors()
299 // Not sure why but seen in reports.phpmyadmin.net
300 if (empty($GLOBALS['cfg']['SendErrorReports'])) {
301 $GLOBALS['cfg']['SendErrorReports'] = 'ask';
304 // display errors if SendErrorReports is set to 'ask'.
305 if ($GLOBALS['cfg']['SendErrorReports'] != 'never') {
306 foreach ($this->getErrors() as $error) {
307 if ($error instanceof PMA_Error
) {
308 if (! $error->isDisplayed()) {
309 $retval .= $error->getDisplay();
312 $retval .= var_export($error, true);
316 $retval .= $this->getDispUserErrors();
318 // if preference is not 'never' and
319 // there are 'actual' errors to be reported
320 if ($GLOBALS['cfg']['SendErrorReports'] != 'never'
321 && $this->countErrors() != $this->countUserErrors()
323 // add report button.
324 $retval .= '<form method="post" action="error_report.php"'
325 . ' id="pma_report_errors_form"';
326 if ($GLOBALS['cfg']['SendErrorReports'] == 'always') {
327 // in case of 'always', generate 'invisible' form.
328 $retval .= ' style="display:none;"';
331 . '<input type="hidden" name="token" value="'
332 . $_SESSION[' PMA_token ']
334 . '<input type="hidden" name="exception_type" value="php"/>'
335 . '<input type="hidden" name="send_error_report" value="1" />'
336 . '<input type="submit" value="'
338 . '" id="pma_report_errors" class="floatright">'
339 . '<input type="checkbox" name="always_send"'
340 . ' id="always_send_checkbox" value="true"/>'
341 . '<label for="always_send_checkbox">'
342 . __('Automatically send report next time')
345 if ($GLOBALS['cfg']['SendErrorReports'] == 'ask') {
346 // add ignore buttons
347 $retval .= '<input type="submit" value="'
349 . '" id="pma_ignore_errors_bottom" class="floatright">';
351 $retval .= '<input type="submit" value="'
353 . '" id="pma_ignore_all_errors_bottom" class="floatright">';
354 $retval .= '</form>';
360 * displays errors not displayed
364 public function dispErrors()
366 echo $this->getDispErrors();
370 * look in session for saved errors
374 protected function checkSavedErrors()
376 if (isset($_SESSION['errors'])) {
378 // restore saved errors
379 foreach ($_SESSION['errors'] as $hash => $error) {
380 if ($error instanceof PMA_Error
&& ! isset($this->errors
[$hash])) {
381 $this->errors
[$hash] = $error;
384 //$this->errors = array_merge($_SESSION['errors'], $this->errors);
386 // delete stored errors
387 $_SESSION['errors'] = array();
388 unset($_SESSION['errors']);
393 * return count of errors
395 * @return integer number of errors occurred
397 public function countErrors()
399 return count($this->getErrors());
403 * return count of user errors
405 * @return integer number of user errors occurred
407 public function countUserErrors()
410 if ($this->countErrors()) {
411 foreach ($this->getErrors() as $error) {
412 if ($error->isUserError()) {
422 * whether use errors occurred or not
426 public function hasUserErrors()
428 return (bool) $this->countUserErrors();
432 * whether errors occurred or not
436 public function hasErrors()
438 return (bool) $this->countErrors();
442 * number of errors to be displayed
444 * @return integer number of errors to be displayed
446 public function countDisplayErrors()
448 if ($GLOBALS['cfg']['SendErrorReports'] != 'never') {
449 return $this->countErrors();
451 return $this->countUserErrors();
456 * whether there are errors to display or not
460 public function hasDisplayErrors()
462 return (bool) $this->countDisplayErrors();
466 * Deletes previously stored errors in SESSION.
467 * Saves current errors in session as previous errors.
468 * Required to save current errors in case 'ask'
472 public function savePreviousErrors()
474 unset($_SESSION['prev_errors']);
475 $_SESSION['prev_errors'] = $GLOBALS['error_handler']->getCurrentErrors();
479 * Function to check if there are any errors to be prompted.
480 * Needed because user warnings raised are
481 * also collected by global error handler.
482 * This distinguishes between the actual errors
483 * and user errors raised to warn user.
485 *@return boolean true if there are errors to be "prompted", false otherwise
487 public function hasErrorsForPrompt()
490 $GLOBALS['cfg']['SendErrorReports'] != 'never'
491 && $this->countErrors() != $this->countUserErrors()
496 * Function to report all the collected php errors.
497 * Must be called at the end of each script
498 * by the $GLOBALS['error_handler'] only.
502 public function reportErrors()
504 // if there're no actual errors,
505 if (!$this->hasErrors()
506 ||
$this->countErrors() == $this->countUserErrors()
508 // then simply return.
511 // Delete all the prev_errors in session & store new prev_errors in session
512 $this->savePreviousErrors();
513 $response = PMA_Response
::getInstance();
515 if ($GLOBALS['cfg']['SendErrorReports'] == 'always') {
516 if ($response->isAjax()) {
517 // set flag for automatic report submission.
518 $response->addJSON('_sendErrorAlways', '1');
520 // send the error reports asynchronously & without asking user
521 $jsCode .= '$("#pma_report_errors_form").submit();'
522 . 'PMA_ajaxShowMessage(
523 PMA_messages["phpErrorsBeingSubmitted"], false
525 // js code to appropriate focusing,
526 $jsCode .= '$("html, body").animate({
527 scrollTop:$(document).height()
530 } elseif ($GLOBALS['cfg']['SendErrorReports'] == 'ask') {
531 //ask user whether to submit errors or not.
532 if (!$response->isAjax()) {
533 // js code to show appropriate msgs, event binding & focusing.
534 $jsCode = 'PMA_ajaxShowMessage(PMA_messages["phpErrorsFound"]);'
535 . '$("#pma_ignore_errors_popup").bind("click", function() {
536 PMA_ignorePhpErrors()
538 . '$("#pma_ignore_all_errors_popup").bind("click",
540 PMA_ignorePhpErrors(false)
542 . '$("#pma_ignore_errors_bottom").bind("click", function(e) {
544 PMA_ignorePhpErrors()
546 . '$("#pma_ignore_all_errors_bottom").bind("click",
549 PMA_ignorePhpErrors(false)
551 . '$("html, body").animate({
552 scrollTop:$(document).height()
556 // The errors are already sent from the response.
557 // Just focus on errors division upon load event.
558 $response->getFooter()->getScripts()->addCode($jsCode);