-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
141 lines (119 loc) · 4.09 KB
/
index.php
File metadata and controls
141 lines (119 loc) · 4.09 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
<?php
/**
*
* Seyyed Ali Mohammadiyeh (Max Base)
* https://github.com/BaseMax/server-http-logger/new/main
* Universal request receiver & logger
* Captures GET, POST, RAW body, JSON, headers, files, cookies, server info
* Stores each request in a unique timestamped directory
*
*/
declare(strict_types=1);
error_reporting(E_ALL);
ini_set('display_errors', '0');
/* ===================== CONFIG ===================== */
$BASE_UPLOAD_DIR = __DIR__ . '/uploads';
/* ===================== HELPERS ===================== */
function microtime_string(): string {
$mt = microtime(true);
$dt = DateTime::createFromFormat('U.u', sprintf('%.6f', $mt));
return $dt->format('H-i-s.u');
}
function random_id(int $len = 10): string {
return bin2hex(random_bytes($len / 2));
}
function ensure_dir(string $dir): void {
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
}
function get_headers_safe(): array {
if (function_exists('getallheaders')) {
return getallheaders();
}
$headers = [];
foreach ($_SERVER as $key => $value) {
if (strpos($key, 'HTTP_') === 0) {
$headers[str_replace('_', '-', substr($key, 5))] = $value;
}
}
return $headers;
}
function save_json(string $file, $data): void {
file_put_contents(
$file,
json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
);
}
/* ===================== CREATE REQUEST DIR ===================== */
$dateDir = date('Y-m-d');
$timePart = microtime_string();
$randomDir = $timePart . '_' . random_id();
$rootDir = $BASE_UPLOAD_DIR . '/' . $dateDir . '/' . $randomDir;
$filesDir = $rootDir . '/files';
ensure_dir($filesDir);
/* ===================== COLLECT DATA ===================== */
$rawBody = file_get_contents('php://input');
$jsonBody = null;
if (!empty($rawBody)) {
$decoded = json_decode($rawBody, true);
if (json_last_error() === JSON_ERROR_NONE) {
$jsonBody = $decoded;
}
}
$meta = [
'timestamp' => date('c'),
'microtime' => microtime(true),
'method' => $_SERVER['REQUEST_METHOD'] ?? null,
'request_uri' => $_SERVER['REQUEST_URI'] ?? null,
'remote_ip' => $_SERVER['REMOTE_ADDR'] ?? null,
'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? null,
'content_type' => $_SERVER['CONTENT_TYPE'] ?? null,
'content_length'=> $_SERVER['CONTENT_LENGTH'] ?? null,
];
/* ===================== SAVE DATA ===================== */
save_json($rootDir . '/meta.json', $meta);
save_json($rootDir . '/headers.json', get_headers_safe());
save_json($rootDir . '/get.json', $_GET);
save_json($rootDir . '/post.json', $_POST);
save_json($rootDir . '/cookies.json', $_COOKIE);
save_json($rootDir . '/server.json', $_SERVER);
if ($rawBody !== '') {
file_put_contents($rootDir . '/raw_body.bin', $rawBody);
}
if ($jsonBody !== null) {
save_json($rootDir . '/json_body.json', $jsonBody);
}
/* ===================== HANDLE FILE UPLOADS ===================== */
if (!empty($_FILES)) {
$i = 1;
foreach ($_FILES as $field => $fileInfo) {
if (is_array($fileInfo['name'])) {
// Multiple files input
foreach ($fileInfo['name'] as $idx => $name) {
if ($fileInfo['error'][$idx] === UPLOAD_ERR_OK) {
$safeName = basename($name);
$target = $filesDir . "/file_{$i}_{$safeName}";
move_uploaded_file($fileInfo['tmp_name'][$idx], $target);
$i++;
}
}
} else {
// Single file input
if ($fileInfo['error'] === UPLOAD_ERR_OK) {
$safeName = basename($fileInfo['name']);
$target = $filesDir . "/file_{$i}_{$safeName}";
move_uploaded_file($fileInfo['tmp_name'], $target);
$i++;
}
}
}
}
/* ===================== RESPONSE ===================== */
http_response_code(200);
header('Content-Type: application/json');
echo json_encode([
'status' => 'ok',
'saved_to' => $rootDir,
'timestamp' => date('c')
], JSON_PRETTY_PRINT);