-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErrorReport2Client.php
346 lines (286 loc) · 8.48 KB
/
ErrorReport2Client.php
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
<?php
namespace ErrorReport2;
use SPFW\module\DB;
use SPFW\system\config\Config;
use SPFW\system\config\Environment;
use SPFW\system\Core;
use SPFW\system\CoreException;
use SPFW\system\error\AbstractAction;
use SPFW\system\error\Error;
use SPFW\system\routing\Request;
/**
* ErrorReport2 Client
*
* @package ErrorReport2
* @version 2.1.2
*/
final class ErrorReport2Client extends AbstractAction
{
private const ER2_VERSION = '2.1.2';
private const ER2_PROTOCOL_VERSION = 2;
private const DEFAULT_SERVICE_ID = 'My SPFW App';
private const DEFAULT_TIMEOUT_IN_SECONDS = 5;
private string $server_url;
private string $api_token;
private string $service_identifier;
private int $timeout = self::DEFAULT_TIMEOUT_IN_SECONDS;
private bool $transmit_cookies = true;
private bool $transmit_database_queries = true;
private bool $transmit_environment = true;
private bool $transmit_get_parameter = true;
private bool $transmit_post_parameter = true;
private bool $transmit_session_variables = true;
private array $cookie_param_block_list = [];
private array $get_param_block_list = [];
private array $post_param_block_list = [];
private array $session_param_block_list = [];
public function __construct(string $server_url, string $api_token, string $service_identifier = self::DEFAULT_SERVICE_ID)
{
$this->server_url = $server_url;
$this->api_token = $api_token;
$this->service_identifier = $service_identifier;
}
public function applyToError(array $errors, ?string $request_hash) : bool
{
$data = $this->prepareAll($request_hash);
$data['errors'] = $this->prepareErrorData($errors);
return $this->send($data);
}
public function applyToThrowable(\Throwable $throwable, ?string $request_hash) : bool
{
$data = $this->prepareAll($request_hash);
$data['throwable'] = $this->prepareThrowableData($throwable);
return $this->send($data);
}
public function blockCookieParameter(string $param_name) : self
{
$this->cookie_param_block_list[] = $param_name;
return $this;
}
public function blockGetParameter(string $param_name) : self
{
$this->get_param_block_list[] = $param_name;
return $this;
}
public function blockPostParameter(string $param_name) : self
{
$this->post_param_block_list[] = $param_name;
return $this;
}
public function blockSessionParameter(string $param_name) : self
{
$this->session_param_block_list[] = $param_name;
return $this;
}
public function checkConfig(bool $strict = false) : bool
{
// TODO: Check for valid URI and API token
return true;
}
public function disableTransmittingCookies() : self
{
$this->transmit_cookies = false;
return $this;
}
public function disableTransmittingDatabaseQueries() : self
{
$this->transmit_database_queries = false;
return $this;
}
public function disableTransmittingEnvironmentVariables() : self
{
$this->transmit_environment = false;
return $this;
}
public function disableTransmittingGetParameter() : self
{
$this->transmit_get_parameter = false;
return $this;
}
public function disableTransmittingPostParameter() : self
{
$this->transmit_post_parameter = false;
return $this;
}
public function disableTransmittingSessionVariables() : self
{
$this->transmit_session_variables = false;
return $this;
}
private function prepareAll(?string $session_id) : array
{
$request = Request::current();
return [
'authentication' => $this->prepareAuthenticationData(),
'general' => $this->prepareGeneralData($session_id),
'environment' => $this->prepareEnvironmentData(),
'request' => $this->prepareRequestData($request),
'database' => $this->prepareDatabaseData(),
'cookies' => $this->prepareCookieData(),
'get' => $this->prepareGetData(),
'post' => $this->preparePostData(),
'session' => $this->prepareSessionData(),
];
}
private function prepareAuthenticationData() : array
{
return [
'token' => $this->api_token,
'service_id' => $this->service_identifier,
'er2_version' => self::ER2_VERSION,
'er2_protocol_version' => self::ER2_PROTOCOL_VERSION
];
}
private function prepareCookieData() : ?array
{
if (!$this->transmit_cookies) {
return null;
}
/** @noinspection GlobalVariableUsageInspection */
return $this->prepareVariables($_COOKIE, $this->cookie_param_block_list);
}
private function prepareDatabaseData() : ?array
{
if (!$this->transmit_database_queries) {
return null;
}
$database_data = [];
foreach (DB::databases() as $database_index => $database) {
$database_data[$database_index] = $database->getQueries();
}
return $database_data;
}
private function prepareEnvironmentData() : ?array
{
if (!$this->transmit_environment) {
return null;
}
/** @noinspection GlobalVariableUsageInspection */
return $_SERVER;
}
/**
* @param Error[] $errors
*
* @return array Json-serializable array
*/
private function prepareErrorData(array $errors) : array
{
$data = [];
$filtered_errors = $this->filterErrorApplicants($errors);
foreach ($filtered_errors as $error) {
$data[] = [
'fatal' => $error->isFatalError(),
'error_no' => $error->getErrno(),
'message' => $error->getErrstr(),
'file' => $error->getErrfile(),
'line' => $error->getErrline(),
];
}
return $data;
}
/**
* @param null|string $session_id
*
* @return array<string,mixed>
*/
private function prepareGeneralData(?string $session_id) : array
{
try {
$global_config = Core::activeInstance()->getEnvironment();
} catch (CoreException $e) {
$global_config = null;
}
$current_time = new \DateTime();
/** @noinspection PhpDeprecationInspection */
return [
'er2_session_id' => $session_id ?? 'No session id',
'timestamp' => $current_time->format('Y-m-d\TH:i:s'),
'host_name' => php_uname('n'),
'host_os' => PHP_OS,
'host_os_release' => php_uname('r'),
'host_os_version' => php_uname('v'),
'php_version' => PHP_VERSION,
'php_mode' => PHP_SAPI,
'php_mem_usage' => memory_get_peak_usage(),
'environment_name' => $global_config instanceof Environment ? \get_class($global_config) : null,
'debug_mode' => $global_config instanceof Config ? $global_config->isDebugMode() : null,
];
}
private function prepareGetData() : ?array
{
if (!$this->transmit_get_parameter) {
return null;
}
/** @noinspection GlobalVariableUsageInspection */
return $this->prepareVariables($_GET, $this->get_param_block_list);
}
private function preparePostData() : ?array
{
if (!$this->transmit_post_parameter) {
return null;
}
/** @noinspection GlobalVariableUsageInspection */
return $this->prepareVariables($_POST, $this->post_param_block_list);
}
private function prepareRequestData(Request $request) : array
{
$url = $request->getUrl();
return [
'method' => $url->getHttpMethod(),
'domain' => $url->getDomain(),
'subdomain' => $url->getSubdomain(),
'tcp_port' => $url->getTcpPort(),
'path' => $url->getPath(),
'cli' => $url->isCli(),
'secure_connection' => $url->isSecure(),
];
}
private function prepareSessionData() : ?array
{
if (!$this->transmit_session_variables) {
return null;
}
/** @noinspection GlobalVariableUsageInspection */
return $this->prepareVariables($_SESSION ?? [], $this->session_param_block_list);
}
private function prepareThrowableData(\Throwable $throwable) : array
{
return [
'class_name' => \get_class($throwable),
'error_no' => $throwable->getCode(),
'message' => $throwable->getMessage(),
'file' => $throwable->getFile(),
'line' => $throwable->getLine(),
'previous' => $throwable->getPrevious() !== null ? $this->prepareThrowableData($throwable->getPrevious()) : null,
];
}
private function prepareVariables(array $variables, array $block_list) : array
{
$prepared_variables = [];
foreach ($variables as $key => $value) {
if (!\in_array($key, $block_list, true)) {
$prepared_variables[$key] = var_export($value, true);
}
}
return $prepared_variables;
}
private function send(array $data) : bool
{
$options = [
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'timeout' => $this->timeout,
'content' => json_encode($data, JSON_THROW_ON_ERROR)
]
];
$context = stream_context_create($options);
return file_get_contents($this->server_url, false, $context);
}
public function setTimeout(int $timeout_in_seconds) : self
{
$this->timeout = $timeout_in_seconds;
return $this;
}
}
?>