Translated using Weblate (Hindi)
[phpmyadmin.git] / libraries / Error_Handler.class.php
blob5cffd9880f3e45ee5b1a78af114879e6dfcc3bbc
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 array of 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 array PMA_Error_Handler::$_errors
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 array of current errors
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 $retval = '';
300 // display errors if SendErrorReports is set to 'ask'.
301 if ($GLOBALS['cfg']['SendErrorReports'] != 'never') {
302 foreach ($this->getErrors() as $error) {
303 if ($error instanceof PMA_Error) {
304 if (! $error->isDisplayed()) {
305 $retval .= $error->getDisplay();
307 } else {
308 ob_start();
309 var_dump($error);
310 $retval .= ob_get_contents();
311 ob_end_clean();
314 } else {
315 $retval .= $this->getDispUserErrors();
317 // if preference is not 'never' and
318 // there are 'actual' errors to be reported
319 if ($GLOBALS['cfg']['SendErrorReports'] != 'never'
320 && $this->countErrors() != $this->countUserErrors()
322 // add report button.
323 $retval .= '<form method="post" action="error_report.php"'
324 . ' id="pma_report_errors_form"';
325 if ($GLOBALS['cfg']['SendErrorReports'] == 'always') {
326 // in case of 'always', generate 'invisible' form.
327 $retval .= ' style="display:none;"';
329 $retval .= '>'
330 . '<input type="hidden" name="token" value="'
331 . $_SESSION[' PMA_token ']
332 . '"/>'
333 . '<input type="hidden" name="exception_type" value="php"/>'
334 . '<input type="hidden" name="send_error_report" value="1" />'
335 . '<input type="submit" value="'
336 . __('Report')
337 . '" id="pma_report_errors" style="float: right; margin: 20px;">'
338 . '<input type="checkbox" name="always_send"'
339 . ' id="always_send_checkbox" value="true"/>'
340 . '<label for="always_send_checkbox">'
341 . __('Automatically send report next time')
342 . '</label>'
343 . '</form>';
345 if ($GLOBALS['cfg']['SendErrorReports'] == 'ask') {
346 // add ignore buttons
347 $retval .= '<input type="submit" value="'
348 . __('Ignore')
349 . '" id="pma_ignore_errors_bottom"'
350 . ' style="float: right; margin: 20px;">';
352 $retval .= '<input type="submit" value="'
353 . __('Ignore All')
354 . '" id="pma_ignore_all_errors_bottom"'
355 . ' style="float: right; margin: 20px;">';
357 return $retval;
361 * displays errors not displayed
363 * @return void
365 public function dispErrors()
367 echo $this->getDispErrors();
371 * look in session for saved errors
373 * @return void
375 protected function checkSavedErrors()
377 if (isset($_SESSION['errors'])) {
379 // restore saved errors
380 foreach ($_SESSION['errors'] as $hash => $error) {
381 if ($error instanceof PMA_Error && ! isset($this->errors[$hash])) {
382 $this->errors[$hash] = $error;
385 //$this->errors = array_merge($_SESSION['errors'], $this->errors);
387 // delete stored errors
388 $_SESSION['errors'] = array();
389 unset($_SESSION['errors']);
394 * return count of errors
396 * @return integer number of errors occurred
398 public function countErrors()
400 return count($this->getErrors());
404 * return count of user errors
406 * @return integer number of user errors occurred
408 public function countUserErrors()
410 $count = 0;
411 if ($this->countErrors()) {
412 foreach ($this->getErrors() as $error) {
413 if ($error->isUserError()) {
414 $count++;
419 return $count;
423 * whether use errors occurred or not
425 * @return boolean
427 public function hasUserErrors()
429 return (bool) $this->countUserErrors();
433 * whether errors occurred or not
435 * @return boolean
437 public function hasErrors()
439 return (bool) $this->countErrors();
443 * number of errors to be displayed
445 * @return integer number of errors to be displayed
447 public function countDisplayErrors()
449 if ($GLOBALS['cfg']['SendErrorReports'] != 'never') {
450 return $this->countErrors();
451 } else {
452 return $this->countUserErrors();
457 * whether there are errors to display or not
459 * @return boolean
461 public function hasDisplayErrors()
463 return (bool) $this->countDisplayErrors();
467 * Deletes previously stored errors in SESSION.
468 * Saves current errors in session as previous errors.
469 * Required to save current errors in case 'ask'
471 * @return void
473 public function savePreviousErrors()
475 unset($_SESSION['prev_errors']);
476 $_SESSION['prev_errors'] = $GLOBALS['error_handler']->getCurrentErrors();
480 * Function to check if there are any errors to be prompted.
481 * Needed because user warnings raised are
482 * also collected by global error handler.
483 * This distinguishes between the actual errors
484 * and user errors raised to warn user.
486 *@return boolean true if there are errors to be "prompted", false otherwise
488 public function hasErrorsForPrompt()
490 return (
491 $GLOBALS['cfg']['SendErrorReports'] != 'never'
492 && $this->countErrors() != $this->countUserErrors()
497 * Function to report all the collected php errors.
498 * Must be called at the end of each script
499 * by the $GLOBALS['error_handler'] only.
501 * @return void
504 public function reportErrors()
506 // if there're no actual errors,
507 if (!$this->hasErrors()
508 || $this->countErrors() == $this->countUserErrors()
510 // then simply return.
511 return;
513 // Delete all the prev_errors in session & store new prev_errors in session
514 $this->savePreviousErrors();
515 $response = PMA_Response::getInstance();
516 $jsCode = '';
517 if ($GLOBALS['cfg']['SendErrorReports'] == 'always') {
518 if ($response->isAjax()) {
519 // set flag for automatic report submission.
520 $response->addJSON('_sendErrorAlways', '1');
521 } else {
522 // send the error reports asynchronously & without asking user
523 $jsCode .= '$("#pma_report_errors_form").submit();'
524 . 'PMA_ajaxShowMessage(
525 PMA_messages["phpErrorsBeingSubmitted"], false
526 );';
527 // js code to appropriate focusing,
528 $jsCode .= '$("html, body").animate({
529 scrollTop:$(document).height()
530 }, "slow");';
532 } elseif ($GLOBALS['cfg']['SendErrorReports'] == 'ask') {
533 //ask user whether to submit errors or not.
534 if (!$response->isAjax()) {
535 // js code to show appropriate msgs, event binding & focusing.
536 $jsCode = 'PMA_ajaxShowMessage(PMA_messages["phpErrorsFound"]);'
537 . '$("#pma_ignore_errors_popup").bind("click", function() {
538 PMA_ignorePhpErrors()
539 });'
540 . '$("#pma_ignore_all_errors_popup").bind("click",
541 function() {
542 PMA_ignorePhpErrors(false)
543 });'
544 . '$("#pma_ignore_errors_bottom").bind("click", function() {
545 PMA_ignorePhpErrors()
546 });'
547 . '$("#pma_ignore_all_errors_bottom").bind("click",
548 function() {
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);