Translated using Weblate (Dutch)
[phpmyadmin.git] / libraries / error_report.lib.php
blob807520b10854034871cfd092517892fe9f58e472
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Error reporting functions used to generate and submit error reports
6 * @package PhpMyAdmin
7 */
8 use PMA\libraries\URL;
10 if (! defined('PHPMYADMIN')) {
11 exit;
14 /**
15 * The generated file that contains the line numbers for the js files
16 * If you change any of the js files you can run the scripts/line-counts.sh
18 if (is_readable('js/line_counts.php')) {
19 include_once 'js/line_counts.php';
22 /**
23 * the url where to submit reports to
25 define('SUBMISSION_URL', "https://reports.phpmyadmin.net/incidents/create");
27 /**
28 * returns the pretty printed error report data collected from the
29 * current configuration or from the request parameters sent by the
30 * error reporting js code.
32 * @return String the report
34 function PMA_getPrettyReportData()
36 $report = PMA_getReportData();
38 return json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
41 /**
42 * returns the error report data collected from the current configuration or
43 * from the request parameters sent by the error reporting js code.
45 * @param string $exception_type whether exception is 'js' or 'php'
47 * @return array error report if success, Empty Array otherwise
49 function PMA_getReportData($exception_type = 'js')
51 $relParams = PMA_getRelationsParam();
52 // common params for both, php & js exceptions
53 $report = array(
54 "pma_version" => PMA_VERSION,
55 "browser_name" => PMA_USR_BROWSER_AGENT,
56 "browser_version" => PMA_USR_BROWSER_VER,
57 "user_os" => PMA_USR_OS,
58 "server_software" => $_SERVER['SERVER_SOFTWARE'],
59 "user_agent_string" => $_SERVER['HTTP_USER_AGENT'],
60 "locale" => $_COOKIE['pma_lang'],
61 "configuration_storage" =>
62 is_null($relParams['db']) ? "disabled" :
63 "enabled",
64 "php_version" => phpversion()
67 if ($exception_type == 'js') {
68 if (empty($_REQUEST['exception'])) {
69 return array();
71 $exception = $_REQUEST['exception'];
72 $exception["stack"] = PMA_translateStacktrace($exception["stack"]);
73 List($uri, $script_name) = PMA_sanitizeUrl($exception["url"]);
74 $exception["uri"] = $uri;
75 unset($exception["url"]);
77 $report ["exception_type"] = 'js';
78 $report ["exception"] = $exception;
79 $report ["script_name"] = $script_name;
80 $report ["microhistory"] = $_REQUEST['microhistory'];
82 if (! empty($_REQUEST['description'])) {
83 $report['steps'] = $_REQUEST['description'];
85 } elseif ($exception_type == 'php') {
86 $errors = array();
87 // create php error report
88 $i = 0;
89 if (!isset($_SESSION['prev_errors'])
90 || $_SESSION['prev_errors'] == ''
91 ) {
92 return array();
94 foreach ($_SESSION['prev_errors'] as $errorObj) {
95 /* @var $errorObj PMA\libraries\Error */
96 if ($errorObj->getLine()
97 && $errorObj->getType()
98 && $errorObj->getNumber() != E_USER_WARNING
99 ) {
100 $errors[$i++] = array(
101 "lineNum" => $errorObj->getLine(),
102 "file" => $errorObj->getFile(),
103 "type" => $errorObj->getType(),
104 "msg" => $errorObj->getOnlyMessage(),
105 "stackTrace" => $errorObj->getBacktrace(5),
106 "stackhash" => $errorObj->getHash()
112 // if there were no 'actual' errors to be submitted.
113 if ($i==0) {
114 return array(); // then return empty array
116 $report ["exception_type"] = 'php';
117 $report["errors"] = $errors;
118 } else {
119 return array();
122 return $report;
126 * Sanitize a url to remove the identifiable host name and extract the
127 * current script name from the url fragment
129 * It returns two things in an array. The first is the uri without the
130 * hostname and identifying query params. The second is the name of the
131 * php script in the url
133 * @param String $url the url to sanitize
135 * @return array the uri and script name
137 function PMA_sanitizeUrl($url)
139 $components = parse_url($url);
140 if (isset($components["fragment"])
141 && preg_match("<PMAURL-\d+:>", $components["fragment"], $matches)
143 $uri = str_replace($matches[0], "", $components["fragment"]);
144 $url = "https://example.com/" . $uri;
145 $components = parse_url($url);
148 // get script name
149 preg_match("<([a-zA-Z\-_\d]*\.php)$>", $components["path"], $matches);
150 if (count($matches) < 2) {
151 $script_name = 'index.php';
152 } else {
153 $script_name = $matches[1];
156 // remove deployment specific details to make uri more generic
157 if (isset($components["query"])) {
158 parse_str($components["query"], $query_array);
159 unset($query_array["db"]);
160 unset($query_array["table"]);
161 unset($query_array["token"]);
162 unset($query_array["server"]);
163 $query = http_build_query($query_array);
164 } else {
165 $query = '';
168 $uri = $script_name . "?" . $query;
169 return array($uri, $script_name);
173 * Sends report data to the error reporting server
175 * @param array $report the report info to be sent
177 * @return String the reply of the server
179 function PMA_sendErrorReport($report)
181 $response = PMA\libraries\Util::httpRequest(
182 SUBMISSION_URL,
183 "POST",
184 false,
185 json_encode($report),
186 "Content-Type: application/json"
188 return $response;
192 * Returns number of lines in given javascript file.
194 * @param string $filename javascript filename
196 * @return Number of lines
198 * @todo Should gracefully handle non existing files
200 function PMA_countLines($filename)
202 global $LINE_COUNT;
203 if (defined('LINE_COUNTS')) {
204 return $LINE_COUNT[$filename];
207 // ensure that the file is inside the phpMyAdmin folder
208 $depath = 1;
209 foreach (explode('/', $filename) as $part) {
210 if ($part == '..') {
211 $depath--;
212 } elseif ($part != '.' || $part === '') {
213 $depath++;
215 if ($depath < 0) {
216 return 0;
220 $linecount = 0;
221 $handle = fopen('./js/' . $filename, 'r');
222 while (!feof($handle)) {
223 $line = fgets($handle);
224 if ($line === false) {
225 break;
227 $linecount++;
229 fclose($handle);
230 return $linecount;
234 * returns the translated line number and the file name from the cumulative line
235 * number and an array of files
237 * uses the $LINE_COUNT global array of file names and line numbers
239 * @param array $filenames list of files in order of concatenation
240 * @param Integer $cumulative_number the cumulative line number in the
241 * concatenated files
243 * @return array the filename and line number
244 * Returns two variables in an array:
245 * - A String $filename the filename where the requested cumulative number
246 * exists
247 * - Integer $linenumber the translated line number in the returned file
249 function PMA_getLineNumber($filenames, $cumulative_number)
251 $cumulative_sum = 0;
252 foreach ($filenames as $filename) {
253 $filecount = PMA_countLines($filename);
254 if ($cumulative_number <= $cumulative_sum + $filecount + 2) {
255 $linenumber = $cumulative_number - $cumulative_sum;
256 break;
258 $cumulative_sum += $filecount + 2;
260 if (! isset($filename)) {
261 $filename = '';
263 return array($filename, $linenumber);
267 * translates the cumulative line numbers in the stack trace as well as sanitize
268 * urls and trim long lines in the context
270 * @param array $stack the stack trace
272 * @return array $stack the modified stack trace
274 function PMA_translateStacktrace($stack)
276 foreach ($stack as &$level) {
277 foreach ($level["context"] as &$line) {
278 if (mb_strlen($line) > 80) {
279 $line = mb_substr($line, 0, 75) . "//...";
282 if (preg_match("<js/get_scripts.js.php\?(.*)>", $level["url"], $matches)) {
283 parse_str($matches[1], $vars);
284 List($file_name, $line_number) = PMA_getLineNumber(
285 $vars["scripts"], $level["line"]
287 $level["filename"] = $file_name;
288 $level["line"] = $line_number;
289 } else {
290 unset($level["context"]);
291 List($uri, $script_name) = PMA_sanitizeUrl($level["url"]);
292 $level["uri"] = $uri;
293 $level["scriptname"] = $script_name;
295 unset($level["url"]);
297 unset($level);
298 return $stack;
302 * generates the error report form to collect user description and preview the
303 * report before being sent
305 * @return String the form
307 function PMA_getErrorReportForm()
309 $datas = array(
310 'report_data' => PMA_getPrettyReportData(),
311 'hidden_inputs' => URL::getHiddenInputs(),
312 'hidden_fields' => null,
315 $reportData = PMA_getReportData();
316 if (!empty($reportData)) {
317 $datas['hidden_fields'] = URL::getHiddenFields($reportData);
320 return PMA\libraries\Template::get('error/report_form')
321 ->render($datas);