error_report.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Handle error report submission
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. use PhpMyAdmin\ErrorReport;
  9. use PhpMyAdmin\Message;
  10. use PhpMyAdmin\Response;
  11. use PhpMyAdmin\UserPreferences;
  12. use PhpMyAdmin\Utils\HttpRequest;
  13. require_once 'libraries/common.inc.php';
  14. if (!isset($_POST['exception_type'])
  15. ||!in_array($_POST['exception_type'], array('js', 'php'))
  16. ) {
  17. die('Oops, something went wrong!!');
  18. }
  19. $response = Response::getInstance();
  20. $errorReport = new ErrorReport(new HttpRequest());
  21. if (isset($_POST['send_error_report'])
  22. && ($_POST['send_error_report'] == true
  23. || $_POST['send_error_report'] == '1')
  24. ) {
  25. if ($_POST['exception_type'] == 'php') {
  26. /**
  27. * Prevent infinite error submission.
  28. * Happens in case error submissions fails.
  29. * If reporting is done in some time interval,
  30. * just clear them & clear json data too.
  31. */
  32. if (isset($_SESSION['prev_error_subm_time'])
  33. && isset($_SESSION['error_subm_count'])
  34. && $_SESSION['error_subm_count'] >= 3
  35. && ($_SESSION['prev_error_subm_time']-time()) <= 3000
  36. ) {
  37. $_SESSION['error_subm_count'] = 0;
  38. $_SESSION['prev_errors'] = '';
  39. $response->addJSON('_stopErrorReportLoop', '1');
  40. } else {
  41. $_SESSION['prev_error_subm_time'] = time();
  42. $_SESSION['error_subm_count'] = (
  43. (isset($_SESSION['error_subm_count']))
  44. ? ($_SESSION['error_subm_count']+1)
  45. : (0)
  46. );
  47. }
  48. }
  49. $reportData = $errorReport->getData($_POST['exception_type']);
  50. // report if and only if there were 'actual' errors.
  51. if (count($reportData) > 0) {
  52. $server_response = $errorReport->send($reportData);
  53. if (! is_string($server_response)) {
  54. $success = false;
  55. } else {
  56. $decoded_response = json_decode($server_response, true);
  57. $success = !empty($decoded_response) ?
  58. $decoded_response["success"] : false;
  59. }
  60. /* Message to show to the user */
  61. if ($success) {
  62. if ((isset($_POST['automatic'])
  63. && $_POST['automatic'] === "true")
  64. || $GLOBALS['cfg']['SendErrorReports'] == 'always'
  65. ) {
  66. $msg = __(
  67. 'An error has been detected and an error report has been '
  68. . 'automatically submitted based on your settings.'
  69. );
  70. } else {
  71. $msg = __('Thank you for submitting this report.');
  72. }
  73. } else {
  74. $msg = __(
  75. 'An error has been detected and an error report has been '
  76. . 'generated but failed to be sent.'
  77. )
  78. . ' '
  79. . __(
  80. 'If you experience any '
  81. . 'problems please submit a bug report manually.'
  82. );
  83. }
  84. $msg .= ' ' . __('You may want to refresh the page.');
  85. /* Create message object */
  86. if ($success) {
  87. $msg = Message::notice($msg);
  88. } else {
  89. $msg = Message::error($msg);
  90. }
  91. /* Add message to response */
  92. if ($response->isAjax()) {
  93. if ($_POST['exception_type'] == 'js') {
  94. $response->addJSON('message', $msg);
  95. } else {
  96. $response->addJSON('_errSubmitMsg', $msg);
  97. }
  98. } elseif ($_POST['exception_type'] == 'php') {
  99. $jsCode = 'PMA_ajaxShowMessage("<div class=\"error\">'
  100. . $msg
  101. . '</div>", false);';
  102. $response->getFooter()->getScripts()->addCode($jsCode);
  103. }
  104. if ($_POST['exception_type'] == 'php') {
  105. // clear previous errors & save new ones.
  106. $GLOBALS['error_handler']->savePreviousErrors();
  107. }
  108. /* Persist always send settings */
  109. if (isset($_POST['always_send'])
  110. && $_POST['always_send'] === "true"
  111. ) {
  112. $userPreferences = new UserPreferences();
  113. $userPreferences->persistOption("SendErrorReports", "always", "ask");
  114. }
  115. }
  116. } elseif (! empty($_POST['get_settings'])) {
  117. $response->addJSON('report_setting', $GLOBALS['cfg']['SendErrorReports']);
  118. } else {
  119. if ($_POST['exception_type'] == 'js') {
  120. $response->addHTML($errorReport->getForm());
  121. } else {
  122. // clear previous errors & save new ones.
  123. $GLOBALS['error_handler']->savePreviousErrors();
  124. }
  125. }