1. Check existence of mb_string, mysql and xml extensions before installation.
[openemr.git] / phpmyadmin / libraries / Error_Handler.class.php
blob6abd863f73444e9452d590918e4fe7a20b9cb705
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Holds class PMA_Error_Handler
6 * @package PhpMyAdmin
7 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 require_once './libraries/Error.class.php';
18 /**
19 * handling errors
21 * @package PhpMyAdmin
23 class PMA_Error_Handler
25 /**
26 * holds errors to be displayed or reported later ...
28 * @var PMA_Error[]
30 protected $errors = array();
32 /**
33 * Constructor - set PHP error handler
36 public function __construct()
38 /**
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'));
49 /**
50 * Destructor
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) {
64 /**
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.'),
72 __FILE__,
73 __LINE__
75 $_SESSION['errors'][$error->getHash()] = $error;
76 break;
77 } else if (($error instanceof PMA_Error)
78 && ! $error->isDisplayed()
79 ) {
80 $_SESSION['errors'][$key] = $error;
86 /**
87 * returns array with all errors
89 * @return PMA_Error[]
91 protected function getErrors()
93 $this->checkSavedErrors();
94 return $this->errors;
97 /**
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
119 * @return void
121 public function handleError($errno, $errstr, $errfile, $errline)
123 // check if Error Control Operator (@) was used.
124 if (error_reporting() == 0) {
125 return;
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,
135 * E_COMPILE_WARNING,
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
147 * @return void
149 public function addError($errstr, $errno, $errfile, $errline, $escape = true)
151 if ($escape) {
152 $errstr = htmlspecialchars($errstr);
154 // create error object
155 $error = new PMA_Error(
156 $errno,
157 $errstr,
158 $errfile,
159 $errline
162 // do not repeat errors
163 $this->errors[$error->getHash()] = $error;
165 switch ($error->getNumber()) {
166 case E_USER_NOTICE:
167 case E_USER_WARNING:
168 case E_STRICT:
169 case E_DEPRECATED:
170 case E_NOTICE:
171 case E_WARNING:
172 case E_CORE_WARNING:
173 case E_COMPILE_WARNING:
174 case E_USER_ERROR:
175 case E_RECOVERABLE_ERROR:
176 // just collect the error
177 // display is called from outside
178 break;
179 case E_ERROR:
180 case E_PARSE:
181 case E_CORE_ERROR:
182 case E_COMPILE_ERROR:
183 default:
184 // FATAL error, display it and exit
185 $this->dispFatalError($error);
186 exit;
192 * log error to configured log facility
194 * @param PMA_Error $error the error
196 * @return bool
198 * @todo finish!
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
211 * @return void
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
225 * @return void
227 protected function dispFatalError($error)
229 if (! headers_sent()) {
230 $this->dispPageStart($error);
232 $error->display();
233 $this->dispPageEnd();
234 exit;
238 * Displays user errors not displayed
240 * @return void
242 public function dispUserErrors()
244 echo $this->getDispUserErrors();
248 * Renders user errors not displayed
250 * @return string
252 public function getDispUserErrors()
254 $retval = '';
255 foreach ($this->getErrors() as $error) {
256 if ($error->isUserError() && ! $error->isDisplayed()) {
257 $retval .= $error->getDisplay();
260 return $retval;
264 * display HTML header
266 * @param PMA_error $error the error
268 * @return void
270 protected function dispPageStart($error = null)
272 PMA_Response::getInstance()->disable();
273 echo '<html><head><title>';
274 if ($error) {
275 echo $error->getTitle();
276 } else {
277 echo 'phpMyAdmin error reporting page';
279 echo '</title></head>';
283 * display HTML footer
285 * @return void
287 protected function dispPageEnd()
289 echo '</body></html>';
293 * renders errors not displayed
295 * @return string
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';
303 $retval = '';
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();
311 } else {
312 $retval .= var_export($error, true);
315 } else {
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;"';
330 $retval .= '>'
331 . '<input type="hidden" name="token" value="'
332 . $_SESSION[' PMA_token ']
333 . '"/>'
334 . '<input type="hidden" name="exception_type" value="php"/>'
335 . '<input type="hidden" name="send_error_report" value="1" />'
336 . '<input type="submit" value="'
337 . __('Report')
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')
343 . '</label>';
345 if ($GLOBALS['cfg']['SendErrorReports'] == 'ask') {
346 // add ignore buttons
347 $retval .= '<input type="submit" value="'
348 . __('Ignore')
349 . '" id="pma_ignore_errors_bottom" class="floatright">';
351 $retval .= '<input type="submit" value="'
352 . __('Ignore All')
353 . '" id="pma_ignore_all_errors_bottom" class="floatright">';
354 $retval .= '</form>';
356 return $retval;
360 * displays errors not displayed
362 * @return void
364 public function dispErrors()
366 echo $this->getDispErrors();
370 * look in session for saved errors
372 * @return void
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()
409 $count = 0;
410 if ($this->countErrors()) {
411 foreach ($this->getErrors() as $error) {
412 if ($error->isUserError()) {
413 $count++;
418 return $count;
422 * whether use errors occurred or not
424 * @return boolean
426 public function hasUserErrors()
428 return (bool) $this->countUserErrors();
432 * whether errors occurred or not
434 * @return boolean
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();
450 } else {
451 return $this->countUserErrors();
456 * whether there are errors to display or not
458 * @return boolean
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'
470 * @return void
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()
489 return (
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.
500 * @return void
502 public function reportErrors()
504 // if there're no actual errors,
505 if (!$this->hasErrors()
506 || $this->countErrors() == $this->countUserErrors()
508 // then simply return.
509 return;
511 // Delete all the prev_errors in session & store new prev_errors in session
512 $this->savePreviousErrors();
513 $response = PMA_Response::getInstance();
514 $jsCode = '';
515 if ($GLOBALS['cfg']['SendErrorReports'] == 'always') {
516 if ($response->isAjax()) {
517 // set flag for automatic report submission.
518 $response->addJSON('_sendErrorAlways', '1');
519 } else {
520 // send the error reports asynchronously & without asking user
521 $jsCode .= '$("#pma_report_errors_form").submit();'
522 . 'PMA_ajaxShowMessage(
523 PMA_messages["phpErrorsBeingSubmitted"], false
524 );';
525 // js code to appropriate focusing,
526 $jsCode .= '$("html, body").animate({
527 scrollTop:$(document).height()
528 }, "slow");';
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()
537 });'
538 . '$("#pma_ignore_all_errors_popup").bind("click",
539 function() {
540 PMA_ignorePhpErrors(false)
541 });'
542 . '$("#pma_ignore_errors_bottom").bind("click", function(e) {
543 e.preventDefaulut();
544 PMA_ignorePhpErrors()
545 });'
546 . '$("#pma_ignore_all_errors_bottom").bind("click",
547 function(e) {
548 e.preventDefault();
549 PMA_ignorePhpErrors(false)
550 });'
551 . '$("html, body").animate({
552 scrollTop:$(document).height()
553 }, "slow");';
556 // The errors are already sent from the response.
557 // Just focus on errors division upon load event.
558 $response->getFooter()->getScripts()->addCode($jsCode);