-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathReport.php
More file actions
422 lines (362 loc) · 12.8 KB
/
Copy pathReport.php
File metadata and controls
422 lines (362 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<?php
namespace App;
use App\Model\Table\IncidentsTable;
use Cake\Utility\Security;
use DateTime;
use stdClass;
use function array_filter;
use function array_keys;
use function array_merge;
use function bin2hex;
use function crc32;
use function date;
use function explode;
use function html_entity_decode;
use function htmlspecialchars_decode;
use function is_string;
use function json_decode;
use function openssl_random_pseudo_bytes;
use function parse_str;
use function parse_url;
use function strpos;
use const ENT_HTML5;
use const ENT_QUOTES;
use const PHP_URL_QUERY;
/**
* Represents an user report
*/
class Report extends stdClass
{
/** @var string */
private $internal____date = null;
/** @var string */
private $internal____eventId = null;
/** @var string */
private $internal____userMessage = null;
public function hasUserFeedback(): bool
{
return $this->internal____userMessage !== null;
}
public function getUserFeedback(): string
{
return $this->internal____userMessage ?? '';
}
public static function fromString(string $input): Report
{
$obj = json_decode($input);
return self::fromObject((object) $obj);
}
public static function fromObject(stdClass $input): Report
{
$obj = (object) $input;
$keys = array_keys((array) $obj);
$report = new Report();
foreach ($keys as $propertyName) {
if ($propertyName === 'steps') {
$report->internal____userMessage = $obj->{$propertyName};
} else {
$report->{$propertyName} = $obj->{$propertyName};
}
}
return $report;
}
public function setTimestamp(string $timestamp): void
{
$this->internal____date = $timestamp;
}
public function getEventId(): string
{
if ($this->internal____eventId === null) {
$this->internal____eventId = bin2hex((string) openssl_random_pseudo_bytes(16));
}
return $this->internal____eventId;
}
public function getTimestampUTC(): string
{
return $this->internal____date ?? date(DateTime::RFC3339);
}
public function getTags(): stdClass
{
/*
"pma_version": "4.8.6-dev",
"browser_name": "CHROME",
"browser_version": "72.0.3626.122",
"user_os": "Linux",
"server_software": "nginx/1.14.0",
"user_agent_string": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.122 Safari/537.36 Vivaldi/2.3.1440.61",
"locale": "fr",
"configuration_storage": "enabled",
"php_version": "7.2.16-1+ubuntu18.04.1+deb.sury.org+1",
"exception_type": "php",
*/
$release = $this->getPmaVersion();
$version = explode('.', $release, 3);
$tags = new stdClass();
$tags->version_major = $version[0];
$tags->version_series = $version[0] . '.' . $version[1];
//$tags->pma_version = $this->{'pma_version'} ?? null;
//$tags->browser_name = $this->{'browser_name'} ?? null;
//$tags->browser_version = $this->{'browser_version'} ?? null;
//$tags->user_os = $this->{'user_os'} ?? null;
$tags->server_software = $this->{'server_software'} ?? null;
$tags->user_agent_string = $this->{'user_agent_string'} ?? null;
$tags->locale = $this->{'locale'} ?? null;
$tags->configuration_storage = ($this->{'configuration_storage'} ?? '') === 'enabled'; // "enabled" or "disabled"
$tags->php_version = $this->{'php_version'} ?? null;
$tags->exception_type = $this->{'exception_type'} ?? null;// js or php
return $tags;
}
public function getContexts(): stdClass
{
$contexts = new stdClass();
$contexts->os = new stdClass();
$contexts->os->name = $this->{'user_os'} ?? null;
$contexts->browser = new stdClass();
$contexts->browser->name = $this->{'browser_name'} ?? null;
$contexts->browser->version = $this->{'browser_version'} ?? null;
return $contexts;
}
public function decode(string $text): string
{
return htmlspecialchars_decode(html_entity_decode($text, ENT_QUOTES | ENT_HTML5));
}
/**
* @return array<string,mixed>
*/
public function getExceptionJS(): array
{
$exception = new stdClass();
$exception->type = $this->decode($this->{'exception'}->name ?? '');
$exception->value = $this->decode($this->{'exception'}->message ?? '');
$exception->stacktrace = new stdClass();
$exception->stacktrace->frames = [];
$exStack = ($this->{'exception'} ?? (object) ['stack' => []])->{'stack'} ?? [];
foreach ($exStack as $stack) {
$exception->stacktrace->frames[] = [
'platform' => 'javascript',
'function' => $this->decode($stack->{'func'} ?? ''),
'lineno' => (int) ($stack->{'line'} ?? 0),
'colno' => (int) ($stack->{'column'} ?? 0),
'abs_path' => $stack->{'uri'} ?? '',
'filename' => $stack->{'scriptname'} ?? '',
];
}
return [
'platform' => 'javascript',
'exception' => [
'values' => [$exception],
],
'message' => $this->decode($this->{'exception'}->message ?? ''),
'culprit' => $this->{'script_name'} ?? $this->{'uri'} ?? null,
];
}
public function getExtras(): stdClass
{
return new stdClass();
}
public function getUserMessage(): stdClass
{
$userMessage = new stdClass();
$userMessage->{'message'} = $this->decode($this->{'description'} ?? '');
return $userMessage;
}
public function getUserId(): string
{
// Do not use the Ip as real data, protect the user !
$userIp = $_SERVER['HTTP_CLIENT_IP'] ?? $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? '';
$serverSoftware = $this->{'server_software'} ?? null;
$userAgentString = $this->{'user_agent_string'} ?? null;
$locale = $this->{'locale'} ?? null;
$configurationStorage = $this->{'configuration_storage'} ?? null;
$phpVersion = $this->{'php_version'} ?? null;
$userIp = Security::hash(
$userIp . crc32($userIp),
'sha256',
true // Enable app security salt
);// Make finding back the Ip near to impossible
$user = new stdClass();
// A user can be anonymously identified using the hash of the hashed IP + server software
// + the UA + the locale + is configuration storage enabled + php version
// Reversing the process would be near to impossible and anyway all the found data would be
// already known and public data
return Security::hash(
$userIp . $serverSoftware . $userAgentString . $locale . $configurationStorage . $phpVersion,
'sha256',
true // Enable app security salt
);
}
public function getUser(): stdClass
{
$user = new stdClass();
$user->id = $this->getUserId();
$user->ip_address = '0.0.0.0';
return $user;
}
private function findRoute(?string $uri): ?string
{
if ($uri === null) {
return null;
}
$query = parse_url($uri, PHP_URL_QUERY);// foo=bar&a=b
if (! is_string($query)) {
return null;
}
$output = [];
parse_str($query, $output);
return $output['route'] ?? null;
}
public function getRoute(): ?string
{
if (isset($this->{'exception'})) {
return $this->findRoute($this->{'exception'}->{'uri'} ?? null);
}
return null;
}
public function isMultiReports(): bool
{
return isset($this->{'exception_type'}) && $this->{'exception_type'} === 'php';
}
public function typeToLevel(string $type): string
{
switch ($type) {
case 'Internal error':
case 'Parsing Error':
case 'Error':
case 'Core Error':
return 'error';
case 'User Error':
case 'User Warning':
case 'User Notice':
return 'info';
case 'Warning':
case 'Runtime Notice':
case 'Deprecation Notice':
case 'Notice':
case 'Compile Warning':
return 'warning';
case 'Catchable Fatal Error':
return 'tatal';
default:
return 'error';
}
}
/**
* @return array<int,array<string,mixed>>
*/
public function getMultiDataToSend(): array
{
$reports = [];
/*
{
"lineNum": 272,
"file": "./libraries/classes/Plugins/Export/ExportXml.php",
"type": "Warning",
"msg": "count(): Parameter must be an array or an object that implements Countable",
"stackTrace": [
{
"file": "./libraries/classes/Plugins/Export/ExportXml.php",
"line": 272,
"function": "count",
"args": [
"NULL"
]
},
{
"file": "./export.php",
"line": 415,
"function": "exportHeader",
"class": "PhpMyAdmin\\Plugins\\Export\\ExportXml",
"type": "->"
}
],
"stackhash": "e6e0b1e1b9d90fee08a5ab8226e485fb"
}
*/
foreach ($this->{'errors'} as $error) {
$exception = new stdClass();
$exception->type = $this->decode($error->{'type'} ?? '');
$exception->value = $this->decode($error->{'msg'} ?? '');
$exception->stacktrace = new stdClass();
$exception->stacktrace->frames = [];
foreach ($error->{'stackTrace'} as $stack) {
$trace = [
'platform' => 'php',
'function' => $stack->{'function'} ?? '',
'lineno' => (int) ($stack->{'line'} ?? 0),
'filename' => $error->{'file'} ?? null,
];
if (isset($stack->{'class'})) {
$trace['package'] = $stack->{'class'};
}
if (isset($stack->{'type'})) {
$trace['symbol_addr'] = $stack->{'type'};
}
if (isset($stack->{'args'})) {// function arguments
$trace['vars'] = (object) $stack->{'args'};
}
$exception->stacktrace->frames[] = $trace;
}
$reports[] = [
'platform' => 'php',
'level' => $this->typeToLevel($error->{'type'}),
'exception' => [
'values' => [$exception],
],
'message' => $this->decode($error->{'msg'} ?? ''),
'culprit' => $error->{'file'},
];
}
return $reports;
}
public function getPmaVersion(): string
{
return IncidentsTable::getStrippedPmaVersion($this->{'pma_version'} ?? '');
}
/**
* @return array<string,mixed>
*/
public function toJson(): array
{
$exType = $this->{'exception_type'} ?? 'js';
// array_filter removes keys having null values
$release = $this->getPmaVersion();
return array_filter([
'sentry.interfaces.Message' => $this->getUserMessage(),
'release' => $release,
'dist' => $this->{'pma_version'} ?? '',
'platform' => $exType === 'js' ? 'javascript' : 'php',
'timestamp' => $this->getTimestampUTC(),
'tags' => $this->getTags(),
'extra' => $this->getExtras(),
'contexts' => $this->getContexts(),
'user' => $this->getUser(),
'transaction' => $this->getRoute(),
'environment' => strpos($release, '-dev') === false ? 'production' : 'development',
//TODO: 'level'
]);
}
/**
* @return array<int,array<string,mixed>>
*/
public function getReports(): array
{
if ($this->isMultiReports()) {
$reports = [];
foreach ($this->getMultiDataToSend() as $data) {
$reports[] = array_merge($this->toJson(), $data, [
'event_id' => $this->getEventId(),
]);
}
return $reports;
}
return [
array_merge(
$this->toJson(),
$this->getExceptionJs(),
[
'event_id' => $this->getEventId(),
]
),
];
}
}