2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Handle error report submission
8 declare(strict_types
=1);
10 use PhpMyAdmin\ErrorReport
;
11 use PhpMyAdmin\Message
;
12 use PhpMyAdmin\Response
;
13 use PhpMyAdmin\UserPreferences
;
14 use PhpMyAdmin\Utils\HttpRequest
;
16 if (! defined('ROOT_PATH')) {
17 define('ROOT_PATH', __DIR__
. DIRECTORY_SEPARATOR
);
20 require_once ROOT_PATH
. 'libraries/common.inc.php';
22 if (! isset($_POST['exception_type'])
23 ||
! in_array($_POST['exception_type'], ['js', 'php'])
25 die('Oops, something went wrong!!');
28 $response = Response
::getInstance();
30 /** @var ErrorReport $errorReport */
31 $errorReport = $containerBuilder->get('error_report');
33 if (isset($_POST['send_error_report'])
34 && ($_POST['send_error_report'] == true
35 ||
$_POST['send_error_report'] == '1')
37 if ($_POST['exception_type'] == 'php') {
39 * Prevent infinite error submission.
40 * Happens in case error submissions fails.
41 * If reporting is done in some time interval,
42 * just clear them & clear json data too.
44 if (isset($_SESSION['prev_error_subm_time'])
45 && isset($_SESSION['error_subm_count'])
46 && $_SESSION['error_subm_count'] >= 3
47 && ($_SESSION['prev_error_subm_time'] - time()) <= 3000
49 $_SESSION['error_subm_count'] = 0;
50 $_SESSION['prev_errors'] = '';
51 $response->addJSON('stopErrorReportLoop', '1');
53 $_SESSION['prev_error_subm_time'] = time();
54 $_SESSION['error_subm_count'] = (
55 isset($_SESSION['error_subm_count'])
56 ?
($_SESSION['error_subm_count'] +
1)
61 $reportData = $errorReport->getData($_POST['exception_type']);
62 // report if and only if there were 'actual' errors.
63 if (count($reportData) > 0) {
64 $server_response = $errorReport->send($reportData);
65 if (! is_string($server_response)) {
68 $decoded_response = json_decode($server_response, true);
69 $success = ! empty($decoded_response) ?
70 $decoded_response["success"] : false;
73 /* Message to show to the user */
75 if ((isset($_POST['automatic'])
76 && $_POST['automatic'] === "true")
77 ||
$GLOBALS['cfg']['SendErrorReports'] == 'always'
80 'An error has been detected and an error report has been '
81 . 'automatically submitted based on your settings.'
84 $msg = __('Thank you for submitting this report.');
88 'An error has been detected and an error report has been '
89 . 'generated but failed to be sent.'
93 'If you experience any '
94 . 'problems please submit a bug report manually.'
97 $msg .= ' ' . __('You may want to refresh the page.');
99 /* Create message object */
101 $msg = Message
::notice($msg);
103 $msg = Message
::error($msg);
106 /* Add message to response */
107 if ($response->isAjax()) {
108 if ($_POST['exception_type'] == 'js') {
109 $response->addJSON('message', $msg);
111 $response->addJSON('errSubmitMsg', $msg);
113 } elseif ($_POST['exception_type'] == 'php') {
114 $jsCode = 'Functions.ajaxShowMessage("<div class=\"error\">'
116 . '</div>", false);';
117 $response->getFooter()->getScripts()->addCode($jsCode);
120 if ($_POST['exception_type'] == 'php') {
121 // clear previous errors & save new ones.
122 $GLOBALS['error_handler']->savePreviousErrors();
125 /* Persist always send settings */
126 if (isset($_POST['always_send'])
127 && $_POST['always_send'] === "true"
129 $userPreferences = new UserPreferences();
130 $userPreferences->persistOption("SendErrorReports", "always", "ask");
133 } elseif (! empty($_POST['get_settings'])) {
134 $response->addJSON('report_setting', $GLOBALS['cfg']['SendErrorReports']);
135 } elseif ($_POST['exception_type'] == 'js') {
136 $response->addHTML($errorReport->getForm());
138 // clear previous errors & save new ones.
139 $GLOBALS['error_handler']->savePreviousErrors();