-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
212 lines (195 loc) · 5.84 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
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
<?php
/*
捨場 — Suteba
https://paulglushak.com/suteba
*/
define('DUMP_DIR', 'data');
define('DS', DIRECTORY_SEPARATOR);
class Suteba
{
/**
* Allowed keys (or buckets)
*
* @var array keys
*/
private $keys = [
'test' => '098f6bcd4621d373cade4e832627b4f6',
'poato' => 'potato'
];
public $requestKey, $requestKeyholder, $requestPayload, $requestAction, $requestType,
$requestMethod, $requestTime, $requestAgent, $requestSource;
public function __construct()
{
if (isset($_GET['key']) && $this->isAllowedToRequest($_GET['key'])) {
/* If the key is allowed, save info and handle the request */
$this->requestKey = $_GET['key'];
$this->requestKeyholder = $this->getKeyholder($this->requestKey);
if (isset($_GET['action'])) {
$this->requestAction = $_GET['action'];
}
$this->requestPayload = file_get_contents('php://input');
$this->requestMethod = $_SERVER['REQUEST_METHOD'];
$this->requestType = $_SERVER['CONTENT_TYPE'];
$this->requestTime = date("l jS F \@ g:i:s a", $_SERVER['REQUEST_TIME']);
$this->requestAgent = $_SERVER['HTTP_USER_AGENT'];
$this->requestSource = $_SERVER['REMOTE_ADDR'];
$this->handleRequest($this->requestKey, $this->requestAction);
} else {
/* If no key, or key not allowed return 401 */
$this->response(401);
}
}
/**
* Is the key allowed?
*
* @param string $key
* @return boolean
*/
private function isAllowedToRequest(string $key)
{
return in_array($key, array_values($this->keys));
}
/**
* Return key "label" or "owner"
*
* @param string $key
* @return void
*/
private function getKeyholder(string $key)
{
return array_keys($this->keys, $key)[0];
}
/**
* Handle the request based on the action. Default is to dump the request.
* action=list will return all available requests under a key
*
* @param string $key
* @param string $action
* @return void
*/
private function handleRequest(string $key, $action = '')
{
echo $action;
switch ($action) {
case 'list':
$this->getRequests($key);
break;
exit;
case '':
$data = $this->prepareRequestDump();
$filename = $this->getUUID();
$dump = $this->dumpRequest($this->requestKey, $filename, $data);
if ($dump) {
/* Return request ID and path to it */
$message = <<<EOD
{
"id": "{$dump['id']}",
"path": "{$dump['path']}"
}
EOD;
$this->response(200, $message);
} else {
/* Saving failed */
$this->response(500, '{"error": "Error saving request"}');
}
break;
}
}
/**
* Prepare data to be saved to TXT file.
*
* @return string
*/
private function prepareRequestDump()
{
return (string) <<<EOD
Request Key: {$this->requestKey} ({$this->requestKeyholder})
Request Method: {$this->requestMethod}
Content Type: {$this->requestType}
User Agent: {$this->requestAgent}
Received From: {$this->requestSource}
Recevied At: {$this->requestTime}
Request Body:
{$this->requestPayload}
EOD;
}
/**
* Save request locally and return path and ID, or false if saving fails.
*
* @param string $key
* @param string $filename
* @param string $data
* @return array|bool
*/
private function dumpRequest(string $key, string $filename, string $data)
{
$path = DUMP_DIR.DS.$key.DS.$filename.'.txt';
$dirname = dirname($path);
if (!is_dir($dirname)) {
mkdir($dirname, 0755, true);
}
$fh = fopen($path, 'w');
if (fwrite($fh, $data)) {
fclose($fh);
return ['id'=>$filename, 'path'=>$path];
} else {
return false;
}
}
/**
* Generate unique request ID using MD5.
*
* @return string
*/
private function getUUID()
{
return (string) md5($this->requestSource . $this->requestTime);
}
/**
* Response handler
*
* @param integer $code
* @param string $message
* @return string
*/
private function response(int $code, string $message = '')
{
header('Content-Type: application/json');
switch ($code) {
case '401':
http_response_code(401);
echo '{"error": "Not Allowed"}';
exit;
break;
case '200':
http_response_code(200);
echo $message;
break;
default:
http_response_code($code);
echo $message;
break;
}
}
/**
* Get all requests for a key and echo HTML.
*
* @param string $key
* @return void
*/
private function getRequests(string $key)
{
echo "<h1>Showing requests for {$key} ($this->requestKeyholder)</h1>";
if (is_dir("data/{$key}")) {
echo '<table>';
$files = scandir("data/{$key}");
$files = array_diff($files, array('..', '.'));
foreach ($files as $file) {
print_r("<tr><td><a href='data/{$key}/{$file}'>{$file}</a></td>");
print_r('<td>' . date("F d Y H:i:s", filemtime("data/{$key}/{$file}")) . '</td></tr>');
}
echo '</table>';
}
}
}
$suteba = new Suteba();