-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.php
More file actions
357 lines (318 loc) · 9.1 KB
/
Copy pathrequest.php
File metadata and controls
357 lines (318 loc) · 9.1 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
<?php
namespace Puchiko\request;
class request {
private array $get;
private array $post;
private array $server;
private array $files;
/**
* Build a request wrapper from explicit GET/POST/SERVER/FILES arrays.
*
* @param array<string, mixed> $get GET values.
* @param array<string, mixed> $post POST values.
* @param array<string, mixed> $server SERVER values.
* @param array<string, mixed> $files FILES values.
* @return void
*/
public function __construct(
array $get = [],
array $post = [],
array $server = [],
array $files = []
) {
$this->get = $get;
$this->post = $post;
$this->server = $server;
$this->files = $files;
}
/**
* Build a request wrapper from PHP superglobals.
*
* @return self New request instance.
*/
public static function fromGlobals(): self {
return new self($_GET, $_POST, $_SERVER, $_FILES);
}
/**
* Get a parameter from the specified source (GET, POST) or both.
*
* @param string $name Parameter name.
* @param string|null $source 'GET', 'POST', or null for GET-then-POST lookup.
* @param mixed $default Default value if not found.
* @return mixed Resolved parameter value or default.
*/
public function getParameter(string $name, ?string $source = null, mixed $default = null): mixed {
return match (strtoupper($source ?? '')) {
'GET' => array_key_exists($name, $this->get) ? $this->get[$name] : $default,
'POST' => array_key_exists($name, $this->post) ? $this->post[$name] : $default,
default => array_key_exists($name, $this->get)
? $this->get[$name]
: (array_key_exists($name, $this->post) ? $this->post[$name] : $default),
};
}
/**
* Check whether a parameter exists in the specified source.
*
* @param string $name Parameter name.
* @param string|null $source 'GET', 'POST', or null for either source.
* @return bool True when parameter exists.
*/
public function hasParameter(string $name, ?string $source = null): bool {
return match (strtoupper($source ?? '')) {
'GET' => array_key_exists($name, $this->get),
'POST' => array_key_exists($name, $this->post),
default => array_key_exists($name, $this->get) || array_key_exists($name, $this->post),
};
}
/**
* Get a $_SERVER value.
*
* @param string $name Server key.
* @param mixed $default Default value when key is missing.
* @return mixed Server value or default.
*/
public function getServer(string $name, mixed $default = null): mixed {
return $this->server[$name] ?? $default;
}
/**
* Check whether a $_SERVER key exists.
*
* @param string $name Server key.
* @return bool True when key exists.
*/
public function hasServer(string $name): bool {
return isset($this->server[$name]);
}
/**
* Get file upload data by input name.
*
* @param string $name File input name.
* @return array<string, mixed>|null Upload metadata array or null.
*/
public function getFile(string $name): ?array {
return $this->files[$name] ?? null;
}
/**
* Check whether a file upload entry exists by input name.
*
* @param string $name File input name.
* @return bool True when upload entry exists.
*/
public function hasFile(string $name): bool {
return isset($this->files[$name]);
}
// ─── Convenience: request method ───
/**
* Get the HTTP request method.
*
* @return string Request method (for example GET/POST).
*/
public function getMethod(): string {
return $this->server['REQUEST_METHOD'] ?? '';
}
/**
* Check whether the current request method is POST.
*
* @return bool True for POST requests.
*/
public function isPost(): bool {
return $this->getMethod() === 'POST';
}
/**
* Check whether the current request method is GET.
*
* @return bool True for GET requests.
*/
public function isGet(): bool {
return $this->getMethod() === 'GET';
}
// ─── Convenience: commonly used server values ───
/**
* Get the HTTP referrer value.
*
* @param string $default Fallback value when header is missing.
* @return string Referrer URL or fallback.
*/
public function getReferer(string $default = ''): string {
return $this->server['HTTP_REFERER'] ?? $default;
}
/**
* Get the user-agent string.
*
* @param string $default Fallback value when header is missing.
* @return string User-agent string or fallback.
*/
public function getUserAgent(string $default = ''): string {
return $this->server['HTTP_USER_AGENT'] ?? $default;
}
/**
* Get the request remote IP address.
*
* @param string $default Fallback IP when key is missing.
* @return string Client IP address or fallback.
*/
public function getRemoteAddr(string $default = '127.0.0.1'): string {
return $this->server['REMOTE_ADDR'] ?? $default;
}
/**
* Return the real client IP address, proxy-aware.
*
* When REMOTE_ADDR is a loopback or private-network address (i.e. a trusted
* reverse proxy), the leftmost valid public IP from X-Forwarded-For is used.
* X-Forwarded-For is ignored when the direct connection comes from a public IP
* to prevent spoofing by ordinary clients.
*
* @return string The IP address string (IPv4 or IPv6), or '' on failure.
*/
public function getIp(): string {
$remoteAddr = $this->getRemoteAddr('');
if (!$this->isTrustedProxy($remoteAddr)) {
return $remoteAddr;
}
$forwarded = $this->server['HTTP_X_FORWARDED_FOR'] ?? '';
if ($forwarded !== '') {
foreach (array_map('trim', explode(',', $forwarded)) as $candidate) {
if (filter_var($candidate, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) {
return $candidate;
}
}
}
return $remoteAddr;
}
/**
* Return true if $ip is a loopback or private-network address.
*/
private function isTrustedProxy(string $ip): bool {
return filter_var(
$ip,
FILTER_VALIDATE_IP,
FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
) === false;
}
/**
* Get the user's IP address as an IPAddress object.
*
* @return IPAddress User's IP address.
*/
public function userIp(): IPAddress {
return new IPAddress($this->getRemoteAddr());
}
/**
* Get the HTTP host header, including port when present.
*
* @param string $default Fallback host when header is missing.
* @return string Host header value or fallback.
*/
public function getHttpHost(string $default = ''): string {
return $this->server['HTTP_HOST'] ?? $default;
}
/**
* Get the request timestamp as an integer.
*
* @return int Unix timestamp.
*/
public function getRequestTime(): int {
return (int) ($this->server['REQUEST_TIME'] ?? time());
}
/**
* Get the request timestamp with microsecond precision.
*
* @return float Unix timestamp with microseconds.
*/
public function getRequestTimeFloat(): float {
return (float) ($this->server['REQUEST_TIME_FLOAT'] ?? microtime(true));
}
/**
* Check whether the request is served over HTTPS.
*
* @return bool True when HTTPS is enabled.
*/
public function isHttps(): bool {
return !empty($this->server['HTTPS']) && $this->server['HTTPS'] !== 'off';
}
/**
* Check whether the request is an XMLHttpRequest/AJAX request.
*
* @return bool True when X-Requested-With indicates XMLHttpRequest.
*/
public function isAjax(): bool {
return isset($this->server['HTTP_X_REQUESTED_WITH'])
&& strtolower($this->server['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
}
/**
* Get the current script path from server data.
*
* @return string Script path (for example /index.php).
*/
public function getScriptName(): string {
return $this->server['SCRIPT_NAME'] ?? '';
}
/**
* Get the server hostname.
*
* @return string Server name.
*/
public function getServerName(): string {
return $this->server['SERVER_NAME'] ?? '';
}
/**
* Get the raw Accept-Encoding header value.
*
* @return string Accept-Encoding header.
*/
public function getAcceptEncoding(): string {
return $this->server['HTTP_ACCEPT_ENCODING'] ?? '';
}
/**
* Build the current absolute URL without query parameters.
*
* @return string Absolute URL without query string.
*/
public function getCurrentUrlNoQuery(): string {
$scheme = $this->isHttps() ? 'https' : 'http';
$host = $this->getHttpHost();
$path = $this->getServer('SCRIPT_NAME', '');
return $scheme . '://' . $host . $path;
}
/**
* Return all GET parameters.
*
* @return array<string, mixed> GET parameter map.
*/
public function allGet(): array {
return $this->get;
}
/**
* Return all POST parameters.
*
* @return array<string, mixed> POST parameter map.
*/
public function allPost(): array {
return $this->post;
}
/**
* Return all uploaded file metadata.
*
* @return array<string, mixed> FILES metadata map.
*/
public function allFiles(): array {
return $this->files;
}
/**
* Return all server/environment values.
*
* @return array<string, mixed> SERVER data map.
*/
public function allServer(): array {
return $this->server;
}
/**
* Check if the request is viewing a thread
*
* @return bool True if viewing a thread, false otherwise
*/
public function isViewingThread(): bool {
// Check if the 'res' parameter is present in GET or POST data
return $this->hasParameter('res', 'GET');
}
}