-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
135 lines (106 loc) · 3.2 KB
/
index.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
<?php
use Aws\DynamoDb\DynamoDbClient;
use Aws\DynamoDb\Exception\DynamoDbException;
use Aws\DynamoDb\Marshaler;
use Aws\Result;
use Aws\Sdk;
use M1ke\JsonExplore\JsonExplore;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
require __DIR__.'/vendor/autoload.php';
require __DIR__.'/config.php';
/**
* @param array $entity
* @param string $table_name
* @return Result
* @throws RuntimeException
*/
function insert(array $entity, string $table_name){
$client = getDynamo();
$item_params = insertParams($entity, $table_name);
try {
return $client->putItem($item_params);
}
catch (DynamoDbException $e) {
throw new RuntimeException('There was an error saving data to DynamoDB.');
}
}
function insertParams(array $entity, string $table_name): array{
$marshaler = new Marshaler();
$item_marshal = $marshaler->marshalItem($entity);
return ['TableName' => $table_name, 'Item' => $item_marshal];
}
function sqlDate(bool $use_microtime = false): string{
$microtime = explode('.', (string)microtime(true))[1];
return date('Y-m-d H:i:s').($use_microtime ? '.'.$microtime : '');
}
function getDynamo(): DynamoDbClient{
$args = [
'region' => AWS_REGION,
'version' => 'latest',
];
return (new Sdk($args))->createDynamoDb();
}
function getLogger(): Logger{
$log = new Logger('name');
$log->pushHandler(new StreamHandler('php://stderr', Logger::WARNING));
return $log;
}
function logWebhook(string $body, ?JsonExplore $json, ?JsonExplore $query, ?JsonExplore $form, Logger $log){
if (!$form && !$json && !$query){
$log->info('No data in form, JSON input, or query string, will not save log');
return;
}
$entity = [
'id' => uniqid('', true),
'datetime' => sqlDate(true),
'json' => $json ? $body : null,
'json_parsed' => $json ? $json->asPathString() : null,
'form' => $_POST ? json_encode($_POST) : null,
'form_parsed' => $form ? $form->asPathString() : null,
'query' => $_GET ? json_encode($_GET) : '',
'query_parsed' => $query ? $query->asPathString() : null,
'processed' => false,
'ttl' => time()+TTL_SECONDS,
];
$entity = array_filter($entity, static fn ($n) => $n!==null && $n!=='');
insert($entity, LOG_TABLE);
}
function dataFromJson(string $json, Logger $log): ?JsonExplore{
if (!$json){
$log->info('Request body was empty');
return null;
}
try {
return JsonExplore::fromJson($json)->analyse();
}
catch (InvalidArgumentException $e) {
$log->warning('JSON parse error with message: '.json_last_error_msg().". String content was: $json");
return null;
}
}
function dataFromQuery(array $query, Logger $log): ?JsonExplore{
if (!$query){
$log->info('Query string was empty');
return null;
}
return JsonExplore::fromArray($query)->analyse();
}
function dataFromForm(array $form, Logger $log): ?JsonExplore{
if (!$form){
$log->info('Form body was empty');
return null;
}
return JsonExplore::fromArray($form)->analyse();
}
$log = getLogger();
try {
$body = (string)file_get_contents('php://input');
$json_explored = dataFromJson($body, $log);
$query_explored = dataFromQuery($_GET, $log);
$form_explored = dataFromForm($_POST, $log);
logWebhook($body, $json_explored, $query_explored, $form_explored, $log);
}
catch (Exception $e) {
$log->error($e->getMessage());
}