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'], $_SESSION['error_subm_count'])
45 && $_SESSION['error_subm_count'] >= 3
46 && ($_SESSION['prev_error_subm_time'] - time()) <= 3000
48 $_SESSION['error_subm_count'] = 0;
49 $_SESSION['prev_errors'] = '';
50 $response->addJSON('stopErrorReportLoop', '1');
52 $_SESSION['prev_error_subm_time'] = time();
53 $_SESSION['error_subm_count'] = (
54 isset($_SESSION['error_subm_count'])
55 ?
($_SESSION['error_subm_count'] +
1)
60 $reportData = $errorReport->getData($_POST['exception_type']);
61 // report if and only if there were 'actual' errors.
62 if (count($reportData) > 0) {
63 $server_response = $errorReport->send($reportData);
64 if (! is_string($server_response)) {
67 $decoded_response = json_decode($server_response, true);
68 $success = ! empty($decoded_response) ?
69 $decoded_response["success"] : false;
72 /* Message to show to the user */
74 if ((isset($_POST['automatic'])
75 && $_POST['automatic'] === "true")
76 ||
$GLOBALS['cfg']['SendErrorReports'] == 'always'
79 'An error has been detected and an error report has been '
80 . 'automatically submitted based on your settings.'
83 $msg = __('Thank you for submitting this report.');
87 'An error has been detected and an error report has been '
88 . 'generated but failed to be sent.'
92 'If you experience any '
93 . 'problems please submit a bug report manually.'
96 $msg .= ' ' . __('You may want to refresh the page.');
98 /* Create message object */
100 $msg = Message
::notice($msg);
102 $msg = Message
::error($msg);
105 /* Add message to response */
106 if ($response->isAjax()) {
107 if ($_POST['exception_type'] == 'js') {
108 $response->addJSON('message', $msg);
110 $response->addJSON('errSubmitMsg', $msg);
112 } elseif ($_POST['exception_type'] == 'php') {
113 $jsCode = 'Functions.ajaxShowMessage("<div class=\"error\">'
115 . '</div>", false);';
116 $response->getFooter()->getScripts()->addCode($jsCode);
119 if ($_POST['exception_type'] == 'php') {
120 // clear previous errors & save new ones.
121 $GLOBALS['error_handler']->savePreviousErrors();
124 /* Persist always send settings */
125 if (isset($_POST['always_send'])
126 && $_POST['always_send'] === "true"
128 $userPreferences = new UserPreferences();
129 $userPreferences->persistOption("SendErrorReports", "always", "ask");
132 } elseif (! empty($_POST['get_settings'])) {
133 $response->addJSON('report_setting', $GLOBALS['cfg']['SendErrorReports']);
134 } elseif ($_POST['exception_type'] == 'js') {
135 $response->addHTML($errorReport->getForm());
137 // clear previous errors & save new ones.
138 $GLOBALS['error_handler']->savePreviousErrors();