-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRequest.php
executable file
·376 lines (341 loc) · 9.36 KB
/
Request.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
declare(strict_types=1);
/*
* This file is part of the Alight package.
*
* (c) June So <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alight;
class Request
{
public const ALLOW_METHODS = ['OPTIONS', 'HEAD', 'GET', 'POST', 'DELETE', 'PUT', 'PATCH'];
/**
* Property getter
*
* @param string $property
* @param string $key
* @param mixed $default
* @param mixed $set
* @return mixed
*/
private static function getter(array &$property, string $key, $default, $set = null)
{
if ($key) {
if ($set !== null) {
$property[$key] = $set;
}
if (isset($property[$key])) {
switch (gettype($default)) {
case 'boolean':
return (bool) $property[$key];
case 'integer':
return (int) $property[$key];
case 'double':
return (float) $property[$key];
case 'string':
return (string) $property[$key];
case 'array':
return (array) $property[$key];
default:
return $property[$key];
}
} else {
return $default;
}
} else {
return $property;
}
}
/**
* Get HTTP header value, or $default if unset
*
* @param string $key
* @param mixed $default
* @param mixed $set
* @return mixed
*/
public static function header(string $key = '', $default = null, $set = null)
{
static $header = null;
if ($header === null) {
$header = apache_request_headers() ?: [];
}
return self::getter($header, $key, $default, $set);
}
/**
* Get $_GET value, or $default if unset
*
* @param string $key
* @param mixed $default
* @param mixed $set
* @return mixed
*/
public static function get(string $key = '', $default = null, $set = null)
{
static $get = null;
if ($get === null) {
$get = $_GET ?: [];
}
return self::getter($get, $key, $default, $set);
}
/**
* Get $_POST value (including json body), or $default if unset
*
* @param string $key
* @param mixed $default
* @param mixed $set
* @return mixed
*/
public static function post(string $key = '', $default = null, $set = null)
{
static $post = null;
if ($post === null) {
$post = $_POST ?: [];
if (in_array(self::method(), ['POST', 'PUT', 'DELETE', 'PATCH']) && self::isJson()) {
if (Utility::isJson(self::body())) {
$post = json_decode(self::body(), true);
}
}
}
return self::getter($post, $key, $default, $set);
}
/**
* Simulate $_REQUEST, contains the contents of get(), post()
*
* @param string $key
* @param mixed $default
* @param mixed $set
* @return mixed
*/
public static function request(string $key = '', $default = null, $set = null)
{
static $request = null;
if ($request === null) {
$request = array_replace_recursive(self::get(), self::post());
}
return self::getter($request, $key, $default, $set);
}
/**
* Get $_COOKIE value, or $default if unset
*
* @param string $key
* @param mixed $default
* @param mixed $set
* @return mixed
*/
public static function cookie(string $key = '', $default = null, $set = null)
{
static $cookie = null;
if ($cookie === null) {
$cookie = $_COOKIE ?: [];
}
return self::getter($cookie, $key, $default, $set);
}
/**
* Get $_FILES value, or $default if unset
*
* @param string $key
* @param mixed $default
* @param mixed $set
* @return mixed
*/
public static function file(string $key = '', $default = null, $set = null)
{
static $file = null;
if ($file === null) {
$file = $_FILES ?: [];
}
return self::getter($file, $key, $default, $set);
}
/**
* Checks if it's an ajax request
*
* @return bool
*/
public static function isAjax(): bool
{
static $ajax = null;
if ($ajax === null) {
$ajax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
}
return $ajax;
}
/**
* Checks if it's an json request
*
* @return bool
*/
public static function isJson(): bool
{
static $json = null;
if ($json === null) {
$json = isset($_SERVER['CONTENT_TYPE']) && (strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false);
}
return $json;
}
/**
* Get the client IP (compatible proxy)
*
* @return string
*/
public static function ip(): string
{
static $ip = null;
if ($ip === null) {
$ipProxy = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']) : [];
$ip = $ipProxy[0] ?? ($_SERVER['REMOTE_ADDR'] ?? ($_SERVER['HTTP_CLIENT_IP'] ?? ''));
}
return $ip;
}
/**
* Get the User-Agent
*
* @return string
*/
public static function userAgent(): string
{
return $_SERVER['HTTP_USER_AGENT'] ?? '';
}
/**
* Get the Referrer
*
* @return string
*/
public static function referrer(): string
{
return $_SERVER['HTTP_REFERER'] ?? '';
}
/**
* Get the request method
*
* @return string
*/
public static function method(): string
{
static $method = null;
if ($method === null) {
if (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
$method = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
} elseif (Request::post('_method')) {
$method = strtoupper(Request::post('_method'));
} elseif (Request::get('_method')) {
$method = strtoupper(Request::get('_method'));
} else {
$method = strtoupper($_SERVER['REQUEST_METHOD'] ?? '');
}
}
return $method;
}
/**
* Get the request scheme
*
* @return string
*/
public static function scheme(): string
{
static $scheme = null;
if ($scheme === null) {
if (
(isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) === 'on')
||
(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')
||
(isset($_SERVER['HTTP_FRONT_END_HTTPS']) && $_SERVER['HTTP_FRONT_END_HTTPS'] === 'on')
||
(isset($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] === 'https')
) {
$scheme = 'https';
} else {
$scheme = 'http';
}
}
return $scheme;
}
/**
* Get the request host
*
* @return string
*/
public static function host(): string
{
static $host = null;
if ($host === null) {
if (isset($_SERVER['HTTP_HOST'])) {
$host = $_SERVER['HTTP_HOST'];
} elseif (isset($_SERVER['SERVER_NAME'])) {
$host = $_SERVER['SERVER_NAME'];
} else {
$host = '';
}
}
return $host;
}
/**
* Get the request origin
*
* @return string
*/
public static function origin(): string
{
static $origin = null;
if ($origin === null) {
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
}
return $origin;
}
/**
* Get the base url
*
* @return string
*/
public static function baseUrl(): string
{
return self::scheme() . '://' . self::host();
}
/**
* Get the request subdomain
*
* @return string
*/
public static function subdomain(): string
{
static $subdomain = null;
if ($subdomain === null) {
$configDomainLevel = (int) Config::get('app', 'domainLevel');
$subdomain = join('.', array_slice(explode('.', self::host()), 0, -$configDomainLevel));
}
return $subdomain;
}
/**
* Get the request path
*
* @return string
*/
public static function path(): string
{
static $path = null;
if ($path === null) {
$path = $_SERVER['REQUEST_URI'] ?? '';
if (false !== $pos = strpos($path, '?')) {
$path = substr($path, 0, $pos);
}
}
return $path;
}
/**
* Get the request body
*
* @return string
*/
public static function body(): string
{
static $body = null;
if ($body === null) {
$body = file_get_contents('php://input') ?: '';
}
return $body;
}
}