Translated using Weblate (Portuguese)
[phpmyadmin.git] / error_report.php
blob079ceebd720f22751bc614466498d7960c2c9877
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Handle error report submission
6 * @package PhpMyAdmin
7 */
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 global $containerBuilder;
22 require_once ROOT_PATH . 'libraries/common.inc.php';
24 if (! isset($_POST['exception_type'])
25 || ! in_array($_POST['exception_type'], ['js', 'php'])
26 ) {
27 die('Oops, something went wrong!!');
30 $response = Response::getInstance();
32 /** @var ErrorReport $errorReport */
33 $errorReport = $containerBuilder->get('error_report');
35 if (isset($_POST['send_error_report'])
36 && ($_POST['send_error_report'] == true
37 || $_POST['send_error_report'] == '1')
38 ) {
39 if ($_POST['exception_type'] == 'php') {
40 /**
41 * Prevent infinite error submission.
42 * Happens in case error submissions fails.
43 * If reporting is done in some time interval,
44 * just clear them & clear json data too.
46 if (isset($_SESSION['prev_error_subm_time'], $_SESSION['error_subm_count'])
47 && $_SESSION['error_subm_count'] >= 3
48 && ($_SESSION['prev_error_subm_time'] - time()) <= 3000
49 ) {
50 $_SESSION['error_subm_count'] = 0;
51 $_SESSION['prev_errors'] = '';
52 $response->addJSON('stopErrorReportLoop', '1');
53 } else {
54 $_SESSION['prev_error_subm_time'] = time();
55 $_SESSION['error_subm_count'] = (
56 isset($_SESSION['error_subm_count'])
57 ? ($_SESSION['error_subm_count'] + 1)
58 : 0
62 $reportData = $errorReport->getData($_POST['exception_type']);
63 // report if and only if there were 'actual' errors.
64 if (count($reportData) > 0) {
65 $server_response = $errorReport->send($reportData);
66 if (! is_string($server_response)) {
67 $success = false;
68 } else {
69 $decoded_response = json_decode($server_response, true);
70 $success = ! empty($decoded_response) ?
71 $decoded_response["success"] : false;
74 /* Message to show to the user */
75 if ($success) {
76 if ((isset($_POST['automatic'])
77 && $_POST['automatic'] === "true")
78 || $GLOBALS['cfg']['SendErrorReports'] == 'always'
79 ) {
80 $msg = __(
81 'An error has been detected and an error report has been '
82 . 'automatically submitted based on your settings.'
84 } else {
85 $msg = __('Thank you for submitting this report.');
87 } else {
88 $msg = __(
89 'An error has been detected and an error report has been '
90 . 'generated but failed to be sent.'
92 . ' '
93 . __(
94 'If you experience any '
95 . 'problems please submit a bug report manually.'
98 $msg .= ' ' . __('You may want to refresh the page.');
100 /* Create message object */
101 if ($success) {
102 $msg = Message::notice($msg);
103 } else {
104 $msg = Message::error($msg);
107 /* Add message to response */
108 if ($response->isAjax()) {
109 if ($_POST['exception_type'] == 'js') {
110 $response->addJSON('message', $msg);
111 } else {
112 $response->addJSON('errSubmitMsg', $msg);
114 } elseif ($_POST['exception_type'] == 'php') {
115 $jsCode = 'Functions.ajaxShowMessage("<div class=\"error\">'
116 . $msg
117 . '</div>", false);';
118 $response->getFooter()->getScripts()->addCode($jsCode);
121 if ($_POST['exception_type'] == 'php') {
122 // clear previous errors & save new ones.
123 $GLOBALS['error_handler']->savePreviousErrors();
126 /* Persist always send settings */
127 if (isset($_POST['always_send'])
128 && $_POST['always_send'] === "true"
130 $userPreferences = new UserPreferences();
131 $userPreferences->persistOption("SendErrorReports", "always", "ask");
134 } elseif (! empty($_POST['get_settings'])) {
135 $response->addJSON('report_setting', $GLOBALS['cfg']['SendErrorReports']);
136 } elseif ($_POST['exception_type'] == 'js') {
137 $response->addHTML($errorReport->getForm());
138 } else {
139 // clear previous errors & save new ones.
140 $GLOBALS['error_handler']->savePreviousErrors();