Repository navigation
Expand file tree
/
Copy pathRequest.php
More file actions
executable file
·479 lines (423 loc) · 13.5 KB
/
Copy pathRequest.php
File metadata and controls
executable file
·479 lines (423 loc) · 13.5 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<?php
/**
* Webiny Framework (http://www.webiny.com/framework)
*
* @copyright Copyright Webiny LTD
*/
namespace Webiny\Component\Http;
use Webiny\Component\Http\Request\Env;
use Webiny\Component\Http\Request\File;
use Webiny\Component\Http\Request\Files;
use Webiny\Component\Http\Request\Headers;
use Webiny\Component\Http\Request\Payload;
use Webiny\Component\Http\Request\Query;
use Webiny\Component\Http\Request\Post;
use Webiny\Component\Http\Request\RequestException;
use Webiny\Component\Http\Request\Server;
use Webiny\Component\StdLib\SingletonTrait;
use Webiny\Component\StdLib\StdLibTrait;
use Webiny\Component\StdLib\StdObject\UrlObject\UrlObject;
/**
* Request class holds the information about current request.
*
* @package Webiny\
*/
class Request
{
use SingletonTrait, StdLibTrait;
const HEADER_CLIENT_IP = 'X_FORWARDED_FOR';
const HEADER_CLIENT_HOST = 'X_FORWARDED_HOST';
const HEADER_CLIENT_PROTO = 'X_FORWARDED_PROTO';
const HEADER_CLIENT_PORT = 'X_FORWARDED_PORT';
/**
* @var array Array of IPs from trusted proxies.
*/
private $_trustedProxies = [];
/**
* @var string
*/
private $_currentUrl = '';
/**
* @var Query
*/
private $_query;
/**
* @var Post
*/
private $_post;
/**
* @var Payload
*/
private $_payload;
/**
* @var Files
*/
private $_files;
/**
* @var Server
*/
private $_server;
/**
* @var Env
*/
private $_env;
/**
* @var Headers
*/
private $_headers;
/**
* This function prepare the Request and all of its sub-classes.
* This class is called automatically by SingletonTrait.
*/
protected function _init()
{
$this->_query = new Query();
$this->_post = new Post();
$this->_payload = new Payload();
$this->_server = new Server();
$this->_files = new Files();
$this->_env = new Env();
$this->_headers = new Headers();
if (Http::getConfig()->TrustedProxies) {
$this->_trustedProxies = Http::getConfig()->TrustedProxies->toArray();
}
}
/**
* Get a value from $_GET param for the given $key.
* If key doesn't not exist, $value will be returned and assigned under that key.
*
* @param string $key Key for which you wish to get the value.
* @param mixed $value Default value that will be returned if $key doesn't exist.
*
* @return mixed Value of the given $key.
*/
public function query($key = null, $value = null)
{
return $this->isNull($key) ? $this->_query->getAll() : $this->_query->get($key, $value);
}
/**
* Get a value from $_POST param for the given $key.
* If key doesn't not exist, $value will be returned and assigned under that key.
*
* @param string $key Key for which you wish to get the value.
* @param mixed $value Default value that will be returned if $key doesn't exist.
*
* @return mixed Value of the given $key.
*/
public function post($key = null, $value = null)
{
return $this->isNull($key) ? $this->_post->getAll() : $this->_post->get($key, $value);
}
/**
* Returns an array object with all post parameters.
*
* @return Post
*/
public function getPost()
{
return $this->_post;
}
/**
* Get a value from HTTP Headers
* If key doesn't not exist, $value will be returned and assigned under that key.
*
* @param string $key Key for which you wish to get the value.
* @param mixed $value Default value that will be returned if $key doesn't exist.
*
* @return mixed Value of the given $key.
*/
public function header($key = null, $value = null)
{
return $this->isNull($key) ? $this->_headers->getAll() : $this->_headers->get($key, $value);
}
/**
* Get a value from request payload param for the given $key.
* If key doesn't not exist, $value will be returned and assigned under that key.
*
* @param string $key Key for which you wish to get the value.
* @param mixed $value Default value that will be returned if $key doesn't exist.
*
* @return mixed Value of the given $key.
*/
public function payload($key = null, $value = null)
{
return $this->isNull($key) ? $this->_payload->getAll() : $this->_payload->get($key, $value);
}
/**
* Returns an array object with all post parameters.
*
* @return Payload
*/
public function getPayload()
{
return $this->_payload;
}
/**
* Get a value from $_ENV param for the given $key.
* If key doesn't not exist, $value will be returned and assigned under that key.
*
* @param string $key Key for which you wish to get the value.
* @param mixed $value Default value that will be returned if $key doesn't exist.
*
* @return mixed Value of the given $key.
*/
public function env($key = null, $value = null)
{
return $this->isNull($key) ? $this->_env->getAll() : $this->_env->get($key, $value);
}
/**
* Access to the $_SERVER parameter over a object wrapper.
*
* @return Server
*/
public function server()
{
return $this->_server;
}
/**
* Get the File object for the given $name.
* If you have a multi-dimensional upload field name, than you should pass the optional $arrayOffset param to get the
* right File object.
*
* @param string $name Name of the upload field.
* @param null|int $arrayOffset Optional array offset for multi-dimensional upload fields.
*
* @throws \Exception|Request\Files\FilesException
* @return Files\File
*/
public function files($name, $arrayOffset = null)
{
try {
return $this->_files->get($name, $arrayOffset);
} catch (Files\FilesException $e) {
throw $e;
}
}
/**
* Array of IPs from trusted proxies.
* @return array
*/
public function getTrustedProxies()
{
return $this->_trustedProxies;
}
/**
* Get a list of trusted headers.
*
* @return array List of trusted headers.
*/
public function getTrustedHeaders()
{
$trustedHeaders = Http::getConfig()->TrustedHeaders;
return [
'client_ip' => $trustedHeaders->get('client_ip', self::HEADER_CLIENT_IP),
'client_host' => $trustedHeaders->get('client_host', self::HEADER_CLIENT_HOST),
'client_proto' => $trustedHeaders->get('client_proto', self::HEADER_CLIENT_PROTO),
'client_port' => $trustedHeaders->get('client_port', self::HEADER_CLIENT_PORT),
];
}
/**
* Get current url with schema, host, port, request uri and query string.
* You can get the result in a form of a string or as a url standard object.
*
* @param bool $asUrlObject In which format you want to get the result, url standard object or a string.
*
* @return string|\Webiny\Component\StdLib\StdObject\UrlObject\UrlObject Current url.
*/
public function getCurrentUrl($asUrlObject = false)
{
if ($this->_currentUrl == '') {
// schema
$pageURL = 'http';
if ($this->isRequestSecured()) {
$pageURL = 'https';
}
$pageURL .= "://";
// port, server name and request uri
$host = $this->getHostName();
/*
$port = $this->getConnectionPort();
if($port && $port != "80") {
$pageURL .= $host . ":" . $port . $this->server()->requestUri();
} else {
$pageURL .= $host . $this->server()->requestUri();
}*/
$pageURL .= $host . $this->server()->requestUri();
// query
$query = $this->server()->queryString();
if ($query) {
$pageURL .= '?' . $query;
}
$this->_currentUrl = $pageURL;
}
if ($asUrlObject) {
return $this->url($this->_currentUrl);
} else {
return $this->_currentUrl;
}
}
/**
* Get client ip address.
* This function check and validates headers from trusted proxies.
*
* @throws RequestException
* @return string Client IP address.
*/
public function getClientIp()
{
$remoteAddress = $this->server()->remoteAddress();
$fwdClientIp = $this->server()->get($this->getTrustedHeaders()['client_ip']);
if ($fwdClientIp && $remoteAddress && in_array($remoteAddress, $this->getTrustedProxies())) {
// Use the forwarded IP address, typically set when the
// client is using a proxy server.
// Format: "X-Forwarded-For: client1, proxy1, proxy2"
$clientIps = explode(',', $fwdClientIp);
$clientIp = array_shift($clientIps);
} elseif ($this->server()->httpClientIp() && $remoteAddress && in_array($remoteAddress,
$this->getTrustedProxies()
)
) {
// Use the forwarded IP address, typically set when the
// client is using a proxy server.
$clientIps = explode(',', $this->server()->httpClientIp());
$clientIp = array_shift($clientIps);
} elseif ($this->server()->remoteAddress()) {
// The remote IP address
$clientIp = $this->server()->remoteAddress();
} else {
throw new RequestException('Unable to get client IP address.');
}
return $clientIp;
}
/**
* Check if connection is secured.
* This function check the forwarded headers from trusted proxies.
*
* @return bool True if connection is secured (https), otherwise false is returned.
*/
public function isRequestSecured()
{
$remoteAddress = $this->server()->remoteAddress();
$protocol = $this->server()->serverProtocol();
$fwdProto = $this->server()->get($this->getTrustedHeaders()['client_proto']);
if ($fwdProto && $fwdProto != '' && in_array($remoteAddress, $this->getTrustedProxies())) {
$protocol = $fwdProto;
}
$protocol = strtolower($protocol);
$isSecured = in_array($protocol, [
'https',
'on',
'1'
]
);
if (!$isSecured) {
if (in_array(strtolower($this->server()->https()), [
'https',
'on',
'1'
]
)) {
$isSecured = true;
}
}
return $isSecured;
}
/**
* Return the connection port number.
* This function check the forwarded headers from trusted proxies.
*
* @return int Port number.
*/
public function getConnectionPort()
{
$remoteAddress = $this->server()->remoteAddress();
$port = $this->server()->serverPort();
$fwdPort = $this->server()->get($this->getTrustedHeaders()['client_port']);
if ($fwdPort && $fwdPort != '' && in_array($remoteAddress, $this->getTrustedProxies())) {
$port = $fwdPort;
}
return $port;
}
/**
* Returns the host name.
* This function check the forwarded headers from trusted proxies.
*
* @return string Host name
*/
public function getHostName()
{
$remoteAddress = $this->server()->remoteAddress();
$host = $this->server()->serverName();
$fwdHost = $this->server()->get($this->getTrustedHeaders()['client_host']);
if ($fwdHost && $fwdHost != '' && in_array($remoteAddress, $this->getTrustedProxies())) {
$host = $fwdHost;
}
return strtolower($host);
}
/**
* Redirect the request to the given url.
*
* @param string|UrlObject $url
* @param string|int|array $headers Headers that you wish to send with your request.
*/
public function redirect($url, $headers = null)
{
if (!$this->isStdObject($url)) {
$url = $this->url($url);
}
$url->goToUrl($headers);
}
/**
* Checks if current request method is POST.
*
* @return bool True if it's POST.
*/
public function isPost()
{
return $this->str($this->server()->requestMethod())->equals('POST');
}
/**
* Checks if current request method is GET.
*
* @return bool True if it's GET.
*/
public function isGet()
{
return $this->str($this->server()->requestMethod())->equals('GET');
}
/**
* Checks if current request method is DELETE.
*
* @return bool True if it's DELETE.
*/
public function isDelete()
{
return $this->str($this->server()->requestMethod())->equals('DELETE');
}
/**
* Checks if current request method is PUT.
*
* @return bool True if it's PUT.
*/
public function isPut()
{
return $this->str($this->server()->requestMethod())->equals('PUT');
}
/**
* Checks if current request method is PATCH.
*
* @return bool True if it's PATCH.
*/
public function isPatch()
{
return $this->str($this->server()->requestMethod())->equals('PATCH');
}
/**
* Get request method from HTTP headers
* @return string
*/
public function getRequestMethod()
{
return $this->server()->requestMethod();
}
}