123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595 |
- <?php
- namespace PhpMyAdmin;
- use PhpMyAdmin\Error;
- use PhpMyAdmin\Response;
- use PhpMyAdmin\Url;
- class ErrorHandler
- {
-
- protected $errors = array();
-
- protected $hide_location = false;
-
- protected $error_reporting = 0;
-
- public function __construct()
- {
-
- if (!defined('TESTSUITE')) {
- set_error_handler(array($this, 'handleError'));
- }
- if (function_exists('error_reporting')) {
- $this->error_reporting = error_reporting();
- }
- }
-
- public function __destruct()
- {
- if (isset($_SESSION)) {
- if (! isset($_SESSION['errors'])) {
- $_SESSION['errors'] = array();
- }
-
- foreach ($this->errors as $key => $error) {
-
- if (count($_SESSION['errors']) >= 10) {
- $error = new Error(
- 0,
- __('Too many error messages, some are not displayed.'),
- __FILE__,
- __LINE__
- );
- $_SESSION['errors'][$error->getHash()] = $error;
- break;
- } elseif (($error instanceof Error)
- && ! $error->isDisplayed()
- ) {
- $_SESSION['errors'][$key] = $error;
- }
- }
- }
- }
-
- public function setHideLocation($hide)
- {
- $this->hide_location = $hide;
- }
-
- public function getErrors($check=true)
- {
- if ($check) {
- $this->checkSavedErrors();
- }
- return $this->errors;
- }
-
- public function getCurrentErrors()
- {
- return $this->errors;
- }
-
- public function sliceErrors($count)
- {
- $errors = $this->getErrors(false);
- $this->errors = array_splice($errors, 0, $count);
- return array_splice($errors, $count);
- }
-
- public function handleError($errno, $errstr, $errfile, $errline)
- {
- if (function_exists('error_reporting')) {
-
- if (error_reporting() == 0 &&
- $this->error_reporting != 0 &&
- ($errno & (E_USER_WARNING | E_USER_ERROR | E_USER_NOTICE | E_USER_DEPRECATED)) == 0
- ) {
- return;
- }
- } else {
- if (($errno & (E_USER_WARNING | E_USER_ERROR | E_USER_NOTICE | E_USER_DEPRECATED)) == 0) {
- return;
- }
- }
- $this->addError($errstr, $errno, $errfile, $errline, true);
- }
-
- public function addError($errstr, $errno, $errfile, $errline, $escape = true)
- {
- if ($escape) {
- $errstr = htmlspecialchars($errstr);
- }
-
- $error = new Error(
- $errno,
- $errstr,
- $errfile,
- $errline
- );
- $error->setHideLocation($this->hide_location);
-
- $this->errors[$error->getHash()] = $error;
- switch ($error->getNumber()) {
- case E_STRICT:
- case E_DEPRECATED:
- case E_NOTICE:
- case E_WARNING:
- case E_CORE_WARNING:
- case E_COMPILE_WARNING:
- case E_RECOVERABLE_ERROR:
-
- $error->setBBCode(false);
- break;
- case E_USER_NOTICE:
- case E_USER_WARNING:
- case E_USER_ERROR:
- case E_USER_DEPRECATED:
-
-
- break;
- case E_ERROR:
- case E_PARSE:
- case E_CORE_ERROR:
- case E_COMPILE_ERROR:
- default:
-
- $this->dispFatalError($error);
- exit;
- }
- }
-
- public function triggerError($errorInfo, $errorNumber = null)
- {
-
-
- trigger_error($errorInfo, $errorNumber);
- }
-
- protected function dispFatalError($error)
- {
- if (! headers_sent()) {
- $this->dispPageStart($error);
- }
- $error->display();
- $this->dispPageEnd();
- exit;
- }
-
- public function dispUserErrors()
- {
- echo $this->getDispUserErrors();
- }
-
- public function getDispUserErrors()
- {
- $retval = '';
- foreach ($this->getErrors() as $error) {
- if ($error->isUserError() && ! $error->isDisplayed()) {
- $retval .= $error->getDisplay();
- }
- }
- return $retval;
- }
-
- protected function dispPageStart($error = null)
- {
- Response::getInstance()->disable();
- echo '<html><head><title>';
- if ($error) {
- echo $error->getTitle();
- } else {
- echo 'phpMyAdmin error reporting page';
- }
- echo '</title></head>';
- }
-
- protected function dispPageEnd()
- {
- echo '</body></html>';
- }
-
- public function getDispErrors()
- {
- $retval = '';
-
- if ($GLOBALS['cfg']['SendErrorReports'] != 'never') {
- foreach ($this->getErrors() as $error) {
- if (! $error->isDisplayed()) {
- $retval .= $error->getDisplay();
- }
- }
- } else {
- $retval .= $this->getDispUserErrors();
- }
-
-
- if ($GLOBALS['cfg']['SendErrorReports'] != 'never'
- && $this->countErrors() != $this->countUserErrors()
- ) {
-
- $retval .= '<form method="post" action="error_report.php"'
- . ' id="pma_report_errors_form"';
- if ($GLOBALS['cfg']['SendErrorReports'] == 'always') {
-
- $retval .= ' class="hide"';
- }
- $retval .= '>';
- $retval .= Url::getHiddenFields(array(
- 'exception_type' => 'php',
- 'send_error_report' => '1',
- 'server' => $GLOBALS['server'],
- ));
- $retval .= '<input type="submit" value="'
- . __('Report')
- . '" id="pma_report_errors" class="floatright">'
- . '<input type="checkbox" name="always_send"'
- . ' id="always_send_checkbox" value="true"/>'
- . '<label for="always_send_checkbox">'
- . __('Automatically send report next time')
- . '</label>';
- if ($GLOBALS['cfg']['SendErrorReports'] == 'ask') {
-
- $retval .= '<input type="submit" value="'
- . __('Ignore')
- . '" id="pma_ignore_errors_bottom" class="floatright">';
- }
- $retval .= '<input type="submit" value="'
- . __('Ignore All')
- . '" id="pma_ignore_all_errors_bottom" class="floatright">';
- $retval .= '</form>';
- }
- return $retval;
- }
-
- public function dispErrors()
- {
- echo $this->getDispErrors();
- }
-
- protected function checkSavedErrors()
- {
- if (isset($_SESSION['errors'])) {
-
- foreach ($_SESSION['errors'] as $hash => $error) {
- if ($error instanceof Error && ! isset($this->errors[$hash])) {
- $this->errors[$hash] = $error;
- }
- }
-
- $_SESSION['errors'] = array();
- unset($_SESSION['errors']);
- }
- }
-
- public function countErrors($check=true)
- {
- return count($this->getErrors($check));
- }
-
- public function countUserErrors()
- {
- $count = 0;
- if ($this->countErrors()) {
- foreach ($this->getErrors() as $error) {
- if ($error->isUserError()) {
- $count++;
- }
- }
- }
- return $count;
- }
-
- public function hasUserErrors()
- {
- return (bool) $this->countUserErrors();
- }
-
- public function hasErrors()
- {
- return (bool) $this->countErrors();
- }
-
- public function countDisplayErrors()
- {
- if ($GLOBALS['cfg']['SendErrorReports'] != 'never') {
- return $this->countErrors();
- }
- return $this->countUserErrors();
- }
-
- public function hasDisplayErrors()
- {
- return (bool) $this->countDisplayErrors();
- }
-
- public function savePreviousErrors()
- {
- unset($_SESSION['prev_errors']);
- $_SESSION['prev_errors'] = $GLOBALS['error_handler']->getCurrentErrors();
- }
-
- public function hasErrorsForPrompt()
- {
- return (
- $GLOBALS['cfg']['SendErrorReports'] != 'never'
- && $this->countErrors() != $this->countUserErrors()
- );
- }
-
- public function reportErrors()
- {
-
- if (!$this->hasErrors()
- || $this->countErrors() == $this->countUserErrors()
- ) {
-
- return;
- }
-
- $this->savePreviousErrors();
- $response = Response::getInstance();
- $jsCode = '';
- if ($GLOBALS['cfg']['SendErrorReports'] == 'always') {
- if ($response->isAjax()) {
-
- $response->addJSON('_sendErrorAlways', '1');
- } else {
-
- $jsCode .= '$("#pma_report_errors_form").submit();'
- . 'PMA_ajaxShowMessage(
- PMA_messages["phpErrorsBeingSubmitted"], false
- );';
-
- $jsCode .= '$("html, body").animate({
- scrollTop:$(document).height()
- }, "slow");';
- }
- } elseif ($GLOBALS['cfg']['SendErrorReports'] == 'ask') {
-
- if (!$response->isAjax()) {
-
- $jsCode = 'PMA_ajaxShowMessage(PMA_messages["phpErrorsFound"]);'
- . '$("#pma_ignore_errors_popup").bind("click", function() {
- PMA_ignorePhpErrors()
- });'
- . '$("#pma_ignore_all_errors_popup").bind("click",
- function() {
- PMA_ignorePhpErrors(false)
- });'
- . '$("#pma_ignore_errors_bottom").bind("click", function(e) {
- e.preventDefault();
- PMA_ignorePhpErrors()
- });'
- . '$("#pma_ignore_all_errors_bottom").bind("click",
- function(e) {
- e.preventDefault();
- PMA_ignorePhpErrors(false)
- });'
- . '$("html, body").animate({
- scrollTop:$(document).height()
- }, "slow");';
- }
- }
-
-
- $response->getFooter()->getScripts()->addCode($jsCode);
- }
- }
|