ErrorReportController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Handle error report submission
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Controllers;
  7. use PhpMyAdmin\ErrorHandler;
  8. use PhpMyAdmin\ErrorReport;
  9. use PhpMyAdmin\Message;
  10. use PhpMyAdmin\Response;
  11. use PhpMyAdmin\Template;
  12. use PhpMyAdmin\UserPreferences;
  13. use function count;
  14. use function in_array;
  15. use function is_string;
  16. use function json_decode;
  17. use function time;
  18. /**
  19. * Handle error report submission
  20. */
  21. class ErrorReportController extends AbstractController
  22. {
  23. /** @var ErrorReport */
  24. private $errorReport;
  25. /** @var ErrorHandler */
  26. private $errorHandler;
  27. /**
  28. * @param Response $response
  29. */
  30. public function __construct(
  31. $response,
  32. Template $template,
  33. ErrorReport $errorReport,
  34. ErrorHandler $errorHandler
  35. ) {
  36. parent::__construct($response, $template);
  37. $this->errorReport = $errorReport;
  38. $this->errorHandler = $errorHandler;
  39. }
  40. public function index(): void
  41. {
  42. global $cfg;
  43. if (! isset($_POST['exception_type'])
  44. || ! in_array($_POST['exception_type'], ['js', 'php'])
  45. ) {
  46. return;
  47. }
  48. if (isset($_POST['send_error_report'])
  49. && ($_POST['send_error_report'] == true
  50. || $_POST['send_error_report'] == '1')
  51. ) {
  52. if ($_POST['exception_type'] === 'php') {
  53. /**
  54. * Prevent infinite error submission.
  55. * Happens in case error submissions fails.
  56. * If reporting is done in some time interval,
  57. * just clear them & clear json data too.
  58. */
  59. if (isset($_SESSION['prev_error_subm_time'], $_SESSION['error_subm_count'])
  60. && $_SESSION['error_subm_count'] >= 3
  61. && ($_SESSION['prev_error_subm_time'] - time()) <= 3000
  62. ) {
  63. $_SESSION['error_subm_count'] = 0;
  64. $_SESSION['prev_errors'] = '';
  65. $this->response->addJSON('stopErrorReportLoop', '1');
  66. } else {
  67. $_SESSION['prev_error_subm_time'] = time();
  68. $_SESSION['error_subm_count'] = isset($_SESSION['error_subm_count'])
  69. ? $_SESSION['error_subm_count'] + 1
  70. : 0;
  71. }
  72. }
  73. $reportData = $this->errorReport->getData($_POST['exception_type']);
  74. // report if and only if there were 'actual' errors.
  75. if (count($reportData) > 0) {
  76. $server_response = $this->errorReport->send($reportData);
  77. if (! is_string($server_response)) {
  78. $success = false;
  79. } else {
  80. $decoded_response = json_decode($server_response, true);
  81. $success = ! empty($decoded_response) ?
  82. $decoded_response['success'] : false;
  83. }
  84. /* Message to show to the user */
  85. if ($success) {
  86. if ((isset($_POST['automatic'])
  87. && $_POST['automatic'] === 'true')
  88. || $cfg['SendErrorReports'] === 'always'
  89. ) {
  90. $msg = __(
  91. 'An error has been detected and an error report has been '
  92. . 'automatically submitted based on your settings.'
  93. );
  94. } else {
  95. $msg = __('Thank you for submitting this report.');
  96. }
  97. } else {
  98. $msg = __(
  99. 'An error has been detected and an error report has been '
  100. . 'generated but failed to be sent.'
  101. );
  102. $msg .= ' ';
  103. $msg .= __(
  104. 'If you experience any '
  105. . 'problems please submit a bug report manually.'
  106. );
  107. }
  108. $msg .= ' ' . __('You may want to refresh the page.');
  109. /* Create message object */
  110. if ($success) {
  111. $msg = Message::notice($msg);
  112. } else {
  113. $msg = Message::error($msg);
  114. }
  115. /* Add message to response */
  116. if ($this->response->isAjax()) {
  117. if ($_POST['exception_type'] === 'js') {
  118. $this->response->addJSON('message', $msg);
  119. } else {
  120. $this->response->addJSON('errSubmitMsg', $msg);
  121. }
  122. } elseif ($_POST['exception_type'] === 'php') {
  123. $jsCode = 'Functions.ajaxShowMessage(\'<div class="alert alert-danger" role="alert">'
  124. . $msg
  125. . '</div>\', false);';
  126. $this->response->getFooter()->getScripts()->addCode($jsCode);
  127. }
  128. if ($_POST['exception_type'] === 'php') {
  129. // clear previous errors & save new ones.
  130. $this->errorHandler->savePreviousErrors();
  131. }
  132. /* Persist always send settings */
  133. if (isset($_POST['always_send'])
  134. && $_POST['always_send'] === 'true'
  135. ) {
  136. $userPreferences = new UserPreferences();
  137. $userPreferences->persistOption('SendErrorReports', 'always', 'ask');
  138. }
  139. }
  140. } elseif (! empty($_POST['get_settings'])) {
  141. $this->response->addJSON('report_setting', $cfg['SendErrorReports']);
  142. } elseif ($_POST['exception_type'] === 'js') {
  143. $this->response->addHTML($this->errorReport->getForm());
  144. } else {
  145. // clear previous errors & save new ones.
  146. $this->errorHandler->savePreviousErrors();
  147. }
  148. }
  149. }