| 3 |
liveuser |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\HttpFoundation;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
|
|
|
15 |
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
|
|
|
16 |
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
|
|
17 |
|
|
|
18 |
// Help opcache.preload discover always-needed symbols
|
|
|
19 |
class_exists(AcceptHeader::class);
|
|
|
20 |
class_exists(FileBag::class);
|
|
|
21 |
class_exists(HeaderBag::class);
|
|
|
22 |
class_exists(HeaderUtils::class);
|
|
|
23 |
class_exists(InputBag::class);
|
|
|
24 |
class_exists(ParameterBag::class);
|
|
|
25 |
class_exists(ServerBag::class);
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Request represents an HTTP request.
|
|
|
29 |
*
|
|
|
30 |
* The methods dealing with URL accept / return a raw path (% encoded):
|
|
|
31 |
* * getBasePath
|
|
|
32 |
* * getBaseUrl
|
|
|
33 |
* * getPathInfo
|
|
|
34 |
* * getRequestUri
|
|
|
35 |
* * getUri
|
|
|
36 |
* * getUriForPath
|
|
|
37 |
*
|
|
|
38 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
39 |
*/
|
|
|
40 |
class Request
|
|
|
41 |
{
|
|
|
42 |
const HEADER_FORWARDED = 0b00001; // When using RFC 7239
|
|
|
43 |
const HEADER_X_FORWARDED_FOR = 0b00010;
|
|
|
44 |
const HEADER_X_FORWARDED_HOST = 0b00100;
|
|
|
45 |
const HEADER_X_FORWARDED_PROTO = 0b01000;
|
|
|
46 |
const HEADER_X_FORWARDED_PORT = 0b10000;
|
|
|
47 |
const HEADER_X_FORWARDED_ALL = 0b11110; // All "X-Forwarded-*" headers
|
|
|
48 |
const HEADER_X_FORWARDED_AWS_ELB = 0b11010; // AWS ELB doesn't send X-Forwarded-Host
|
|
|
49 |
|
|
|
50 |
const METHOD_HEAD = 'HEAD';
|
|
|
51 |
const METHOD_GET = 'GET';
|
|
|
52 |
const METHOD_POST = 'POST';
|
|
|
53 |
const METHOD_PUT = 'PUT';
|
|
|
54 |
const METHOD_PATCH = 'PATCH';
|
|
|
55 |
const METHOD_DELETE = 'DELETE';
|
|
|
56 |
const METHOD_PURGE = 'PURGE';
|
|
|
57 |
const METHOD_OPTIONS = 'OPTIONS';
|
|
|
58 |
const METHOD_TRACE = 'TRACE';
|
|
|
59 |
const METHOD_CONNECT = 'CONNECT';
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* @var string[]
|
|
|
63 |
*/
|
|
|
64 |
protected static $trustedProxies = [];
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* @var string[]
|
|
|
68 |
*/
|
|
|
69 |
protected static $trustedHostPatterns = [];
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* @var string[]
|
|
|
73 |
*/
|
|
|
74 |
protected static $trustedHosts = [];
|
|
|
75 |
|
|
|
76 |
protected static $httpMethodParameterOverride = false;
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Custom parameters.
|
|
|
80 |
*
|
|
|
81 |
* @var ParameterBag
|
|
|
82 |
*/
|
|
|
83 |
public $attributes;
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Request body parameters ($_POST).
|
|
|
87 |
*
|
|
|
88 |
* @var InputBag|ParameterBag
|
|
|
89 |
*/
|
|
|
90 |
public $request;
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Query string parameters ($_GET).
|
|
|
94 |
*
|
|
|
95 |
* @var InputBag
|
|
|
96 |
*/
|
|
|
97 |
public $query;
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Server and execution environment parameters ($_SERVER).
|
|
|
101 |
*
|
|
|
102 |
* @var ServerBag
|
|
|
103 |
*/
|
|
|
104 |
public $server;
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Uploaded files ($_FILES).
|
|
|
108 |
*
|
|
|
109 |
* @var FileBag
|
|
|
110 |
*/
|
|
|
111 |
public $files;
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Cookies ($_COOKIE).
|
|
|
115 |
*
|
|
|
116 |
* @var InputBag
|
|
|
117 |
*/
|
|
|
118 |
public $cookies;
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Headers (taken from the $_SERVER).
|
|
|
122 |
*
|
|
|
123 |
* @var HeaderBag
|
|
|
124 |
*/
|
|
|
125 |
public $headers;
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* @var string|resource|false|null
|
|
|
129 |
*/
|
|
|
130 |
protected $content;
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* @var array
|
|
|
134 |
*/
|
|
|
135 |
protected $languages;
|
|
|
136 |
|
|
|
137 |
/**
|
|
|
138 |
* @var array
|
|
|
139 |
*/
|
|
|
140 |
protected $charsets;
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* @var array
|
|
|
144 |
*/
|
|
|
145 |
protected $encodings;
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* @var array
|
|
|
149 |
*/
|
|
|
150 |
protected $acceptableContentTypes;
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* @var string
|
|
|
154 |
*/
|
|
|
155 |
protected $pathInfo;
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* @var string
|
|
|
159 |
*/
|
|
|
160 |
protected $requestUri;
|
|
|
161 |
|
|
|
162 |
/**
|
|
|
163 |
* @var string
|
|
|
164 |
*/
|
|
|
165 |
protected $baseUrl;
|
|
|
166 |
|
|
|
167 |
/**
|
|
|
168 |
* @var string
|
|
|
169 |
*/
|
|
|
170 |
protected $basePath;
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* @var string
|
|
|
174 |
*/
|
|
|
175 |
protected $method;
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* @var string
|
|
|
179 |
*/
|
|
|
180 |
protected $format;
|
|
|
181 |
|
|
|
182 |
/**
|
|
|
183 |
* @var SessionInterface
|
|
|
184 |
*/
|
|
|
185 |
protected $session;
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* @var string
|
|
|
189 |
*/
|
|
|
190 |
protected $locale;
|
|
|
191 |
|
|
|
192 |
/**
|
|
|
193 |
* @var string
|
|
|
194 |
*/
|
|
|
195 |
protected $defaultLocale = 'en';
|
|
|
196 |
|
|
|
197 |
/**
|
|
|
198 |
* @var array
|
|
|
199 |
*/
|
|
|
200 |
protected static $formats;
|
|
|
201 |
|
|
|
202 |
protected static $requestFactory;
|
|
|
203 |
|
|
|
204 |
/**
|
|
|
205 |
* @var string|null
|
|
|
206 |
*/
|
|
|
207 |
private $preferredFormat;
|
|
|
208 |
private $isHostValid = true;
|
|
|
209 |
private $isForwardedValid = true;
|
|
|
210 |
|
|
|
211 |
/**
|
|
|
212 |
* @var bool|null
|
|
|
213 |
*/
|
|
|
214 |
private $isSafeContentPreferred;
|
|
|
215 |
|
|
|
216 |
private static $trustedHeaderSet = -1;
|
|
|
217 |
|
|
|
218 |
private static $forwardedParams = [
|
|
|
219 |
self::HEADER_X_FORWARDED_FOR => 'for',
|
|
|
220 |
self::HEADER_X_FORWARDED_HOST => 'host',
|
|
|
221 |
self::HEADER_X_FORWARDED_PROTO => 'proto',
|
|
|
222 |
self::HEADER_X_FORWARDED_PORT => 'host',
|
|
|
223 |
];
|
|
|
224 |
|
|
|
225 |
/**
|
|
|
226 |
* Names for headers that can be trusted when
|
|
|
227 |
* using trusted proxies.
|
|
|
228 |
*
|
|
|
229 |
* The FORWARDED header is the standard as of rfc7239.
|
|
|
230 |
*
|
|
|
231 |
* The other headers are non-standard, but widely used
|
|
|
232 |
* by popular reverse proxies (like Apache mod_proxy or Amazon EC2).
|
|
|
233 |
*/
|
|
|
234 |
private static $trustedHeaders = [
|
|
|
235 |
self::HEADER_FORWARDED => 'FORWARDED',
|
|
|
236 |
self::HEADER_X_FORWARDED_FOR => 'X_FORWARDED_FOR',
|
|
|
237 |
self::HEADER_X_FORWARDED_HOST => 'X_FORWARDED_HOST',
|
|
|
238 |
self::HEADER_X_FORWARDED_PROTO => 'X_FORWARDED_PROTO',
|
|
|
239 |
self::HEADER_X_FORWARDED_PORT => 'X_FORWARDED_PORT',
|
|
|
240 |
];
|
|
|
241 |
|
|
|
242 |
/**
|
|
|
243 |
* @param array $query The GET parameters
|
|
|
244 |
* @param array $request The POST parameters
|
|
|
245 |
* @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
|
|
|
246 |
* @param array $cookies The COOKIE parameters
|
|
|
247 |
* @param array $files The FILES parameters
|
|
|
248 |
* @param array $server The SERVER parameters
|
|
|
249 |
* @param string|resource|null $content The raw body data
|
|
|
250 |
*/
|
|
|
251 |
public function __construct(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
|
|
|
252 |
{
|
|
|
253 |
$this->initialize($query, $request, $attributes, $cookies, $files, $server, $content);
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
/**
|
|
|
257 |
* Sets the parameters for this request.
|
|
|
258 |
*
|
|
|
259 |
* This method also re-initializes all properties.
|
|
|
260 |
*
|
|
|
261 |
* @param array $query The GET parameters
|
|
|
262 |
* @param array $request The POST parameters
|
|
|
263 |
* @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
|
|
|
264 |
* @param array $cookies The COOKIE parameters
|
|
|
265 |
* @param array $files The FILES parameters
|
|
|
266 |
* @param array $server The SERVER parameters
|
|
|
267 |
* @param string|resource|null $content The raw body data
|
|
|
268 |
*/
|
|
|
269 |
public function initialize(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
|
|
|
270 |
{
|
|
|
271 |
$this->request = new ParameterBag($request);
|
|
|
272 |
$this->query = new InputBag($query);
|
|
|
273 |
$this->attributes = new ParameterBag($attributes);
|
|
|
274 |
$this->cookies = new InputBag($cookies);
|
|
|
275 |
$this->files = new FileBag($files);
|
|
|
276 |
$this->server = new ServerBag($server);
|
|
|
277 |
$this->headers = new HeaderBag($this->server->getHeaders());
|
|
|
278 |
|
|
|
279 |
$this->content = $content;
|
|
|
280 |
$this->languages = null;
|
|
|
281 |
$this->charsets = null;
|
|
|
282 |
$this->encodings = null;
|
|
|
283 |
$this->acceptableContentTypes = null;
|
|
|
284 |
$this->pathInfo = null;
|
|
|
285 |
$this->requestUri = null;
|
|
|
286 |
$this->baseUrl = null;
|
|
|
287 |
$this->basePath = null;
|
|
|
288 |
$this->method = null;
|
|
|
289 |
$this->format = null;
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
/**
|
|
|
293 |
* Creates a new request with values from PHP's super globals.
|
|
|
294 |
*
|
|
|
295 |
* @return static
|
|
|
296 |
*/
|
|
|
297 |
public static function createFromGlobals()
|
|
|
298 |
{
|
|
|
299 |
$request = self::createRequestFromFactory($_GET, $_POST, [], $_COOKIE, $_FILES, $_SERVER);
|
|
|
300 |
|
|
|
301 |
if ($_POST) {
|
|
|
302 |
$request->request = new InputBag($_POST);
|
|
|
303 |
} elseif (0 === strpos($request->headers->get('CONTENT_TYPE'), 'application/x-www-form-urlencoded')
|
|
|
304 |
&& \in_array(strtoupper($request->server->get('REQUEST_METHOD', 'GET')), ['PUT', 'DELETE', 'PATCH'])
|
|
|
305 |
) {
|
|
|
306 |
parse_str($request->getContent(), $data);
|
|
|
307 |
$request->request = new InputBag($data);
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
return $request;
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
/**
|
|
|
314 |
* Creates a Request based on a given URI and configuration.
|
|
|
315 |
*
|
|
|
316 |
* The information contained in the URI always take precedence
|
|
|
317 |
* over the other information (server and parameters).
|
|
|
318 |
*
|
|
|
319 |
* @param string $uri The URI
|
|
|
320 |
* @param string $method The HTTP method
|
|
|
321 |
* @param array $parameters The query (GET) or request (POST) parameters
|
|
|
322 |
* @param array $cookies The request cookies ($_COOKIE)
|
|
|
323 |
* @param array $files The request files ($_FILES)
|
|
|
324 |
* @param array $server The server parameters ($_SERVER)
|
|
|
325 |
* @param string|resource|null $content The raw body data
|
|
|
326 |
*
|
|
|
327 |
* @return static
|
|
|
328 |
*/
|
|
|
329 |
public static function create(string $uri, string $method = 'GET', array $parameters = [], array $cookies = [], array $files = [], array $server = [], $content = null)
|
|
|
330 |
{
|
|
|
331 |
$server = array_replace([
|
|
|
332 |
'SERVER_NAME' => 'localhost',
|
|
|
333 |
'SERVER_PORT' => 80,
|
|
|
334 |
'HTTP_HOST' => 'localhost',
|
|
|
335 |
'HTTP_USER_AGENT' => 'Symfony',
|
|
|
336 |
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
|
337 |
'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.5',
|
|
|
338 |
'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
|
|
|
339 |
'REMOTE_ADDR' => '127.0.0.1',
|
|
|
340 |
'SCRIPT_NAME' => '',
|
|
|
341 |
'SCRIPT_FILENAME' => '',
|
|
|
342 |
'SERVER_PROTOCOL' => 'HTTP/1.1',
|
|
|
343 |
'REQUEST_TIME' => time(),
|
|
|
344 |
], $server);
|
|
|
345 |
|
|
|
346 |
$server['PATH_INFO'] = '';
|
|
|
347 |
$server['REQUEST_METHOD'] = strtoupper($method);
|
|
|
348 |
|
|
|
349 |
$components = parse_url($uri);
|
|
|
350 |
if (isset($components['host'])) {
|
|
|
351 |
$server['SERVER_NAME'] = $components['host'];
|
|
|
352 |
$server['HTTP_HOST'] = $components['host'];
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
if (isset($components['scheme'])) {
|
|
|
356 |
if ('https' === $components['scheme']) {
|
|
|
357 |
$server['HTTPS'] = 'on';
|
|
|
358 |
$server['SERVER_PORT'] = 443;
|
|
|
359 |
} else {
|
|
|
360 |
unset($server['HTTPS']);
|
|
|
361 |
$server['SERVER_PORT'] = 80;
|
|
|
362 |
}
|
|
|
363 |
}
|
|
|
364 |
|
|
|
365 |
if (isset($components['port'])) {
|
|
|
366 |
$server['SERVER_PORT'] = $components['port'];
|
|
|
367 |
$server['HTTP_HOST'] .= ':'.$components['port'];
|
|
|
368 |
}
|
|
|
369 |
|
|
|
370 |
if (isset($components['user'])) {
|
|
|
371 |
$server['PHP_AUTH_USER'] = $components['user'];
|
|
|
372 |
}
|
|
|
373 |
|
|
|
374 |
if (isset($components['pass'])) {
|
|
|
375 |
$server['PHP_AUTH_PW'] = $components['pass'];
|
|
|
376 |
}
|
|
|
377 |
|
|
|
378 |
if (!isset($components['path'])) {
|
|
|
379 |
$components['path'] = '/';
|
|
|
380 |
}
|
|
|
381 |
|
|
|
382 |
switch (strtoupper($method)) {
|
|
|
383 |
case 'POST':
|
|
|
384 |
case 'PUT':
|
|
|
385 |
case 'DELETE':
|
|
|
386 |
if (!isset($server['CONTENT_TYPE'])) {
|
|
|
387 |
$server['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
|
|
|
388 |
}
|
|
|
389 |
// no break
|
|
|
390 |
case 'PATCH':
|
|
|
391 |
$request = $parameters;
|
|
|
392 |
$query = [];
|
|
|
393 |
break;
|
|
|
394 |
default:
|
|
|
395 |
$request = [];
|
|
|
396 |
$query = $parameters;
|
|
|
397 |
break;
|
|
|
398 |
}
|
|
|
399 |
|
|
|
400 |
$queryString = '';
|
|
|
401 |
if (isset($components['query'])) {
|
|
|
402 |
parse_str(html_entity_decode($components['query']), $qs);
|
|
|
403 |
|
|
|
404 |
if ($query) {
|
|
|
405 |
$query = array_replace($qs, $query);
|
|
|
406 |
$queryString = http_build_query($query, '', '&');
|
|
|
407 |
} else {
|
|
|
408 |
$query = $qs;
|
|
|
409 |
$queryString = $components['query'];
|
|
|
410 |
}
|
|
|
411 |
} elseif ($query) {
|
|
|
412 |
$queryString = http_build_query($query, '', '&');
|
|
|
413 |
}
|
|
|
414 |
|
|
|
415 |
$server['REQUEST_URI'] = $components['path'].('' !== $queryString ? '?'.$queryString : '');
|
|
|
416 |
$server['QUERY_STRING'] = $queryString;
|
|
|
417 |
|
|
|
418 |
return self::createRequestFromFactory($query, $request, [], $cookies, $files, $server, $content);
|
|
|
419 |
}
|
|
|
420 |
|
|
|
421 |
/**
|
|
|
422 |
* Sets a callable able to create a Request instance.
|
|
|
423 |
*
|
|
|
424 |
* This is mainly useful when you need to override the Request class
|
|
|
425 |
* to keep BC with an existing system. It should not be used for any
|
|
|
426 |
* other purpose.
|
|
|
427 |
*/
|
|
|
428 |
public static function setFactory(?callable $callable)
|
|
|
429 |
{
|
|
|
430 |
self::$requestFactory = $callable;
|
|
|
431 |
}
|
|
|
432 |
|
|
|
433 |
/**
|
|
|
434 |
* Clones a request and overrides some of its parameters.
|
|
|
435 |
*
|
|
|
436 |
* @param array $query The GET parameters
|
|
|
437 |
* @param array $request The POST parameters
|
|
|
438 |
* @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
|
|
|
439 |
* @param array $cookies The COOKIE parameters
|
|
|
440 |
* @param array $files The FILES parameters
|
|
|
441 |
* @param array $server The SERVER parameters
|
|
|
442 |
*
|
|
|
443 |
* @return static
|
|
|
444 |
*/
|
|
|
445 |
public function duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)
|
|
|
446 |
{
|
|
|
447 |
$dup = clone $this;
|
|
|
448 |
if (null !== $query) {
|
|
|
449 |
$dup->query = new InputBag($query);
|
|
|
450 |
}
|
|
|
451 |
if (null !== $request) {
|
|
|
452 |
$dup->request = new ParameterBag($request);
|
|
|
453 |
}
|
|
|
454 |
if (null !== $attributes) {
|
|
|
455 |
$dup->attributes = new ParameterBag($attributes);
|
|
|
456 |
}
|
|
|
457 |
if (null !== $cookies) {
|
|
|
458 |
$dup->cookies = new InputBag($cookies);
|
|
|
459 |
}
|
|
|
460 |
if (null !== $files) {
|
|
|
461 |
$dup->files = new FileBag($files);
|
|
|
462 |
}
|
|
|
463 |
if (null !== $server) {
|
|
|
464 |
$dup->server = new ServerBag($server);
|
|
|
465 |
$dup->headers = new HeaderBag($dup->server->getHeaders());
|
|
|
466 |
}
|
|
|
467 |
$dup->languages = null;
|
|
|
468 |
$dup->charsets = null;
|
|
|
469 |
$dup->encodings = null;
|
|
|
470 |
$dup->acceptableContentTypes = null;
|
|
|
471 |
$dup->pathInfo = null;
|
|
|
472 |
$dup->requestUri = null;
|
|
|
473 |
$dup->baseUrl = null;
|
|
|
474 |
$dup->basePath = null;
|
|
|
475 |
$dup->method = null;
|
|
|
476 |
$dup->format = null;
|
|
|
477 |
|
|
|
478 |
if (!$dup->get('_format') && $this->get('_format')) {
|
|
|
479 |
$dup->attributes->set('_format', $this->get('_format'));
|
|
|
480 |
}
|
|
|
481 |
|
|
|
482 |
if (!$dup->getRequestFormat(null)) {
|
|
|
483 |
$dup->setRequestFormat($this->getRequestFormat(null));
|
|
|
484 |
}
|
|
|
485 |
|
|
|
486 |
return $dup;
|
|
|
487 |
}
|
|
|
488 |
|
|
|
489 |
/**
|
|
|
490 |
* Clones the current request.
|
|
|
491 |
*
|
|
|
492 |
* Note that the session is not cloned as duplicated requests
|
|
|
493 |
* are most of the time sub-requests of the main one.
|
|
|
494 |
*/
|
|
|
495 |
public function __clone()
|
|
|
496 |
{
|
|
|
497 |
$this->query = clone $this->query;
|
|
|
498 |
$this->request = clone $this->request;
|
|
|
499 |
$this->attributes = clone $this->attributes;
|
|
|
500 |
$this->cookies = clone $this->cookies;
|
|
|
501 |
$this->files = clone $this->files;
|
|
|
502 |
$this->server = clone $this->server;
|
|
|
503 |
$this->headers = clone $this->headers;
|
|
|
504 |
}
|
|
|
505 |
|
|
|
506 |
/**
|
|
|
507 |
* Returns the request as a string.
|
|
|
508 |
*
|
|
|
509 |
* @return string The request
|
|
|
510 |
*/
|
|
|
511 |
public function __toString()
|
|
|
512 |
{
|
|
|
513 |
try {
|
|
|
514 |
$content = $this->getContent();
|
|
|
515 |
} catch (\LogicException $e) {
|
|
|
516 |
if (\PHP_VERSION_ID >= 70400) {
|
|
|
517 |
throw $e;
|
|
|
518 |
}
|
|
|
519 |
|
|
|
520 |
return trigger_error($e, \E_USER_ERROR);
|
|
|
521 |
}
|
|
|
522 |
|
|
|
523 |
$cookieHeader = '';
|
|
|
524 |
$cookies = [];
|
|
|
525 |
|
|
|
526 |
foreach ($this->cookies as $k => $v) {
|
|
|
527 |
$cookies[] = $k.'='.$v;
|
|
|
528 |
}
|
|
|
529 |
|
|
|
530 |
if (!empty($cookies)) {
|
|
|
531 |
$cookieHeader = 'Cookie: '.implode('; ', $cookies)."\r\n";
|
|
|
532 |
}
|
|
|
533 |
|
|
|
534 |
return
|
|
|
535 |
sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n".
|
|
|
536 |
$this->headers.
|
|
|
537 |
$cookieHeader."\r\n".
|
|
|
538 |
$content;
|
|
|
539 |
}
|
|
|
540 |
|
|
|
541 |
/**
|
|
|
542 |
* Overrides the PHP global variables according to this request instance.
|
|
|
543 |
*
|
|
|
544 |
* It overrides $_GET, $_POST, $_REQUEST, $_SERVER, $_COOKIE.
|
|
|
545 |
* $_FILES is never overridden, see rfc1867
|
|
|
546 |
*/
|
|
|
547 |
public function overrideGlobals()
|
|
|
548 |
{
|
|
|
549 |
$this->server->set('QUERY_STRING', static::normalizeQueryString(http_build_query($this->query->all(), '', '&')));
|
|
|
550 |
|
|
|
551 |
$_GET = $this->query->all();
|
|
|
552 |
$_POST = $this->request->all();
|
|
|
553 |
$_SERVER = $this->server->all();
|
|
|
554 |
$_COOKIE = $this->cookies->all();
|
|
|
555 |
|
|
|
556 |
foreach ($this->headers->all() as $key => $value) {
|
|
|
557 |
$key = strtoupper(str_replace('-', '_', $key));
|
|
|
558 |
if (\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH', 'CONTENT_MD5'], true)) {
|
|
|
559 |
$_SERVER[$key] = implode(', ', $value);
|
|
|
560 |
} else {
|
|
|
561 |
$_SERVER['HTTP_'.$key] = implode(', ', $value);
|
|
|
562 |
}
|
|
|
563 |
}
|
|
|
564 |
|
|
|
565 |
$request = ['g' => $_GET, 'p' => $_POST, 'c' => $_COOKIE];
|
|
|
566 |
|
|
|
567 |
$requestOrder = ini_get('request_order') ?: ini_get('variables_order');
|
|
|
568 |
$requestOrder = preg_replace('#[^cgp]#', '', strtolower($requestOrder)) ?: 'gp';
|
|
|
569 |
|
|
|
570 |
$_REQUEST = [[]];
|
|
|
571 |
|
|
|
572 |
foreach (str_split($requestOrder) as $order) {
|
|
|
573 |
$_REQUEST[] = $request[$order];
|
|
|
574 |
}
|
|
|
575 |
|
|
|
576 |
$_REQUEST = array_merge(...$_REQUEST);
|
|
|
577 |
}
|
|
|
578 |
|
|
|
579 |
/**
|
|
|
580 |
* Sets a list of trusted proxies.
|
|
|
581 |
*
|
|
|
582 |
* You should only list the reverse proxies that you manage directly.
|
|
|
583 |
*
|
|
|
584 |
* @param array $proxies A list of trusted proxies, the string 'REMOTE_ADDR' will be replaced with $_SERVER['REMOTE_ADDR']
|
|
|
585 |
* @param int $trustedHeaderSet A bit field of Request::HEADER_*, to set which headers to trust from your proxies
|
|
|
586 |
*
|
|
|
587 |
* @throws \InvalidArgumentException When $trustedHeaderSet is invalid
|
|
|
588 |
*/
|
|
|
589 |
public static function setTrustedProxies(array $proxies, int $trustedHeaderSet)
|
|
|
590 |
{
|
|
|
591 |
self::$trustedProxies = array_reduce($proxies, function ($proxies, $proxy) {
|
|
|
592 |
if ('REMOTE_ADDR' !== $proxy) {
|
|
|
593 |
$proxies[] = $proxy;
|
|
|
594 |
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
|
|
|
595 |
$proxies[] = $_SERVER['REMOTE_ADDR'];
|
|
|
596 |
}
|
|
|
597 |
|
|
|
598 |
return $proxies;
|
|
|
599 |
}, []);
|
|
|
600 |
self::$trustedHeaderSet = $trustedHeaderSet;
|
|
|
601 |
}
|
|
|
602 |
|
|
|
603 |
/**
|
|
|
604 |
* Gets the list of trusted proxies.
|
|
|
605 |
*
|
|
|
606 |
* @return array An array of trusted proxies
|
|
|
607 |
*/
|
|
|
608 |
public static function getTrustedProxies()
|
|
|
609 |
{
|
|
|
610 |
return self::$trustedProxies;
|
|
|
611 |
}
|
|
|
612 |
|
|
|
613 |
/**
|
|
|
614 |
* Gets the set of trusted headers from trusted proxies.
|
|
|
615 |
*
|
|
|
616 |
* @return int A bit field of Request::HEADER_* that defines which headers are trusted from your proxies
|
|
|
617 |
*/
|
|
|
618 |
public static function getTrustedHeaderSet()
|
|
|
619 |
{
|
|
|
620 |
return self::$trustedHeaderSet;
|
|
|
621 |
}
|
|
|
622 |
|
|
|
623 |
/**
|
|
|
624 |
* Sets a list of trusted host patterns.
|
|
|
625 |
*
|
|
|
626 |
* You should only list the hosts you manage using regexs.
|
|
|
627 |
*
|
|
|
628 |
* @param array $hostPatterns A list of trusted host patterns
|
|
|
629 |
*/
|
|
|
630 |
public static function setTrustedHosts(array $hostPatterns)
|
|
|
631 |
{
|
|
|
632 |
self::$trustedHostPatterns = array_map(function ($hostPattern) {
|
|
|
633 |
return sprintf('{%s}i', $hostPattern);
|
|
|
634 |
}, $hostPatterns);
|
|
|
635 |
// we need to reset trusted hosts on trusted host patterns change
|
|
|
636 |
self::$trustedHosts = [];
|
|
|
637 |
}
|
|
|
638 |
|
|
|
639 |
/**
|
|
|
640 |
* Gets the list of trusted host patterns.
|
|
|
641 |
*
|
|
|
642 |
* @return array An array of trusted host patterns
|
|
|
643 |
*/
|
|
|
644 |
public static function getTrustedHosts()
|
|
|
645 |
{
|
|
|
646 |
return self::$trustedHostPatterns;
|
|
|
647 |
}
|
|
|
648 |
|
|
|
649 |
/**
|
|
|
650 |
* Normalizes a query string.
|
|
|
651 |
*
|
|
|
652 |
* It builds a normalized query string, where keys/value pairs are alphabetized,
|
|
|
653 |
* have consistent escaping and unneeded delimiters are removed.
|
|
|
654 |
*
|
|
|
655 |
* @return string A normalized query string for the Request
|
|
|
656 |
*/
|
|
|
657 |
public static function normalizeQueryString(?string $qs)
|
|
|
658 |
{
|
|
|
659 |
if ('' === ($qs ?? '')) {
|
|
|
660 |
return '';
|
|
|
661 |
}
|
|
|
662 |
|
|
|
663 |
parse_str($qs, $qs);
|
|
|
664 |
ksort($qs);
|
|
|
665 |
|
|
|
666 |
return http_build_query($qs, '', '&', \PHP_QUERY_RFC3986);
|
|
|
667 |
}
|
|
|
668 |
|
|
|
669 |
/**
|
|
|
670 |
* Enables support for the _method request parameter to determine the intended HTTP method.
|
|
|
671 |
*
|
|
|
672 |
* Be warned that enabling this feature might lead to CSRF issues in your code.
|
|
|
673 |
* Check that you are using CSRF tokens when required.
|
|
|
674 |
* If the HTTP method parameter override is enabled, an html-form with method "POST" can be altered
|
|
|
675 |
* and used to send a "PUT" or "DELETE" request via the _method request parameter.
|
|
|
676 |
* If these methods are not protected against CSRF, this presents a possible vulnerability.
|
|
|
677 |
*
|
|
|
678 |
* The HTTP method can only be overridden when the real HTTP method is POST.
|
|
|
679 |
*/
|
|
|
680 |
public static function enableHttpMethodParameterOverride()
|
|
|
681 |
{
|
|
|
682 |
self::$httpMethodParameterOverride = true;
|
|
|
683 |
}
|
|
|
684 |
|
|
|
685 |
/**
|
|
|
686 |
* Checks whether support for the _method request parameter is enabled.
|
|
|
687 |
*
|
|
|
688 |
* @return bool True when the _method request parameter is enabled, false otherwise
|
|
|
689 |
*/
|
|
|
690 |
public static function getHttpMethodParameterOverride()
|
|
|
691 |
{
|
|
|
692 |
return self::$httpMethodParameterOverride;
|
|
|
693 |
}
|
|
|
694 |
|
|
|
695 |
/**
|
|
|
696 |
* Gets a "parameter" value from any bag.
|
|
|
697 |
*
|
|
|
698 |
* This method is mainly useful for libraries that want to provide some flexibility. If you don't need the
|
|
|
699 |
* flexibility in controllers, it is better to explicitly get request parameters from the appropriate
|
|
|
700 |
* public property instead (attributes, query, request).
|
|
|
701 |
*
|
|
|
702 |
* Order of precedence: PATH (routing placeholders or custom attributes), GET, BODY
|
|
|
703 |
*
|
|
|
704 |
* @param mixed $default The default value if the parameter key does not exist
|
|
|
705 |
*
|
|
|
706 |
* @return mixed
|
|
|
707 |
*/
|
|
|
708 |
public function get(string $key, $default = null)
|
|
|
709 |
{
|
|
|
710 |
if ($this !== $result = $this->attributes->get($key, $this)) {
|
|
|
711 |
return $result;
|
|
|
712 |
}
|
|
|
713 |
|
|
|
714 |
if ($this->query->has($key)) {
|
|
|
715 |
return $this->query->all()[$key];
|
|
|
716 |
}
|
|
|
717 |
|
|
|
718 |
if ($this->request->has($key)) {
|
|
|
719 |
return $this->request->all()[$key];
|
|
|
720 |
}
|
|
|
721 |
|
|
|
722 |
return $default;
|
|
|
723 |
}
|
|
|
724 |
|
|
|
725 |
/**
|
|
|
726 |
* Gets the Session.
|
|
|
727 |
*
|
|
|
728 |
* @return SessionInterface The session
|
|
|
729 |
*/
|
|
|
730 |
public function getSession()
|
|
|
731 |
{
|
|
|
732 |
$session = $this->session;
|
|
|
733 |
if (!$session instanceof SessionInterface && null !== $session) {
|
|
|
734 |
$this->setSession($session = $session());
|
|
|
735 |
}
|
|
|
736 |
|
|
|
737 |
if (null === $session) {
|
|
|
738 |
throw new \BadMethodCallException('Session has not been set.');
|
|
|
739 |
}
|
|
|
740 |
|
|
|
741 |
return $session;
|
|
|
742 |
}
|
|
|
743 |
|
|
|
744 |
/**
|
|
|
745 |
* Whether the request contains a Session which was started in one of the
|
|
|
746 |
* previous requests.
|
|
|
747 |
*
|
|
|
748 |
* @return bool
|
|
|
749 |
*/
|
|
|
750 |
public function hasPreviousSession()
|
|
|
751 |
{
|
|
|
752 |
// the check for $this->session avoids malicious users trying to fake a session cookie with proper name
|
|
|
753 |
return $this->hasSession() && $this->cookies->has($this->getSession()->getName());
|
|
|
754 |
}
|
|
|
755 |
|
|
|
756 |
/**
|
|
|
757 |
* Whether the request contains a Session object.
|
|
|
758 |
*
|
|
|
759 |
* This method does not give any information about the state of the session object,
|
|
|
760 |
* like whether the session is started or not. It is just a way to check if this Request
|
|
|
761 |
* is associated with a Session instance.
|
|
|
762 |
*
|
|
|
763 |
* @return bool true when the Request contains a Session object, false otherwise
|
|
|
764 |
*/
|
|
|
765 |
public function hasSession()
|
|
|
766 |
{
|
|
|
767 |
return null !== $this->session;
|
|
|
768 |
}
|
|
|
769 |
|
|
|
770 |
public function setSession(SessionInterface $session)
|
|
|
771 |
{
|
|
|
772 |
$this->session = $session;
|
|
|
773 |
}
|
|
|
774 |
|
|
|
775 |
/**
|
|
|
776 |
* @internal
|
|
|
777 |
*/
|
|
|
778 |
public function setSessionFactory(callable $factory)
|
|
|
779 |
{
|
|
|
780 |
$this->session = $factory;
|
|
|
781 |
}
|
|
|
782 |
|
|
|
783 |
/**
|
|
|
784 |
* Returns the client IP addresses.
|
|
|
785 |
*
|
|
|
786 |
* In the returned array the most trusted IP address is first, and the
|
|
|
787 |
* least trusted one last. The "real" client IP address is the last one,
|
|
|
788 |
* but this is also the least trusted one. Trusted proxies are stripped.
|
|
|
789 |
*
|
|
|
790 |
* Use this method carefully; you should use getClientIp() instead.
|
|
|
791 |
*
|
|
|
792 |
* @return array The client IP addresses
|
|
|
793 |
*
|
|
|
794 |
* @see getClientIp()
|
|
|
795 |
*/
|
|
|
796 |
public function getClientIps()
|
|
|
797 |
{
|
|
|
798 |
$ip = $this->server->get('REMOTE_ADDR');
|
|
|
799 |
|
|
|
800 |
if (!$this->isFromTrustedProxy()) {
|
|
|
801 |
return [$ip];
|
|
|
802 |
}
|
|
|
803 |
|
|
|
804 |
return $this->getTrustedValues(self::HEADER_X_FORWARDED_FOR, $ip) ?: [$ip];
|
|
|
805 |
}
|
|
|
806 |
|
|
|
807 |
/**
|
|
|
808 |
* Returns the client IP address.
|
|
|
809 |
*
|
|
|
810 |
* This method can read the client IP address from the "X-Forwarded-For" header
|
|
|
811 |
* when trusted proxies were set via "setTrustedProxies()". The "X-Forwarded-For"
|
|
|
812 |
* header value is a comma+space separated list of IP addresses, the left-most
|
|
|
813 |
* being the original client, and each successive proxy that passed the request
|
|
|
814 |
* adding the IP address where it received the request from.
|
|
|
815 |
*
|
|
|
816 |
* If your reverse proxy uses a different header name than "X-Forwarded-For",
|
|
|
817 |
* ("Client-Ip" for instance), configure it via the $trustedHeaderSet
|
|
|
818 |
* argument of the Request::setTrustedProxies() method instead.
|
|
|
819 |
*
|
|
|
820 |
* @return string|null The client IP address
|
|
|
821 |
*
|
|
|
822 |
* @see getClientIps()
|
|
|
823 |
* @see https://wikipedia.org/wiki/X-Forwarded-For
|
|
|
824 |
*/
|
|
|
825 |
public function getClientIp()
|
|
|
826 |
{
|
|
|
827 |
$ipAddresses = $this->getClientIps();
|
|
|
828 |
|
|
|
829 |
return $ipAddresses[0];
|
|
|
830 |
}
|
|
|
831 |
|
|
|
832 |
/**
|
|
|
833 |
* Returns current script name.
|
|
|
834 |
*
|
|
|
835 |
* @return string
|
|
|
836 |
*/
|
|
|
837 |
public function getScriptName()
|
|
|
838 |
{
|
|
|
839 |
return $this->server->get('SCRIPT_NAME', $this->server->get('ORIG_SCRIPT_NAME', ''));
|
|
|
840 |
}
|
|
|
841 |
|
|
|
842 |
/**
|
|
|
843 |
* Returns the path being requested relative to the executed script.
|
|
|
844 |
*
|
|
|
845 |
* The path info always starts with a /.
|
|
|
846 |
*
|
|
|
847 |
* Suppose this request is instantiated from /mysite on localhost:
|
|
|
848 |
*
|
|
|
849 |
* * http://localhost/mysite returns an empty string
|
|
|
850 |
* * http://localhost/mysite/about returns '/about'
|
|
|
851 |
* * http://localhost/mysite/enco%20ded returns '/enco%20ded'
|
|
|
852 |
* * http://localhost/mysite/about?var=1 returns '/about'
|
|
|
853 |
*
|
|
|
854 |
* @return string The raw path (i.e. not urldecoded)
|
|
|
855 |
*/
|
|
|
856 |
public function getPathInfo()
|
|
|
857 |
{
|
|
|
858 |
if (null === $this->pathInfo) {
|
|
|
859 |
$this->pathInfo = $this->preparePathInfo();
|
|
|
860 |
}
|
|
|
861 |
|
|
|
862 |
return $this->pathInfo;
|
|
|
863 |
}
|
|
|
864 |
|
|
|
865 |
/**
|
|
|
866 |
* Returns the root path from which this request is executed.
|
|
|
867 |
*
|
|
|
868 |
* Suppose that an index.php file instantiates this request object:
|
|
|
869 |
*
|
|
|
870 |
* * http://localhost/index.php returns an empty string
|
|
|
871 |
* * http://localhost/index.php/page returns an empty string
|
|
|
872 |
* * http://localhost/web/index.php returns '/web'
|
|
|
873 |
* * http://localhost/we%20b/index.php returns '/we%20b'
|
|
|
874 |
*
|
|
|
875 |
* @return string The raw path (i.e. not urldecoded)
|
|
|
876 |
*/
|
|
|
877 |
public function getBasePath()
|
|
|
878 |
{
|
|
|
879 |
if (null === $this->basePath) {
|
|
|
880 |
$this->basePath = $this->prepareBasePath();
|
|
|
881 |
}
|
|
|
882 |
|
|
|
883 |
return $this->basePath;
|
|
|
884 |
}
|
|
|
885 |
|
|
|
886 |
/**
|
|
|
887 |
* Returns the root URL from which this request is executed.
|
|
|
888 |
*
|
|
|
889 |
* The base URL never ends with a /.
|
|
|
890 |
*
|
|
|
891 |
* This is similar to getBasePath(), except that it also includes the
|
|
|
892 |
* script filename (e.g. index.php) if one exists.
|
|
|
893 |
*
|
|
|
894 |
* @return string The raw URL (i.e. not urldecoded)
|
|
|
895 |
*/
|
|
|
896 |
public function getBaseUrl()
|
|
|
897 |
{
|
|
|
898 |
if (null === $this->baseUrl) {
|
|
|
899 |
$this->baseUrl = $this->prepareBaseUrl();
|
|
|
900 |
}
|
|
|
901 |
|
|
|
902 |
return $this->baseUrl;
|
|
|
903 |
}
|
|
|
904 |
|
|
|
905 |
/**
|
|
|
906 |
* Gets the request's scheme.
|
|
|
907 |
*
|
|
|
908 |
* @return string
|
|
|
909 |
*/
|
|
|
910 |
public function getScheme()
|
|
|
911 |
{
|
|
|
912 |
return $this->isSecure() ? 'https' : 'http';
|
|
|
913 |
}
|
|
|
914 |
|
|
|
915 |
/**
|
|
|
916 |
* Returns the port on which the request is made.
|
|
|
917 |
*
|
|
|
918 |
* This method can read the client port from the "X-Forwarded-Port" header
|
|
|
919 |
* when trusted proxies were set via "setTrustedProxies()".
|
|
|
920 |
*
|
|
|
921 |
* The "X-Forwarded-Port" header must contain the client port.
|
|
|
922 |
*
|
|
|
923 |
* @return int|string can be a string if fetched from the server bag
|
|
|
924 |
*/
|
|
|
925 |
public function getPort()
|
|
|
926 |
{
|
|
|
927 |
if ($this->isFromTrustedProxy() && $host = $this->getTrustedValues(self::HEADER_X_FORWARDED_PORT)) {
|
|
|
928 |
$host = $host[0];
|
|
|
929 |
} elseif ($this->isFromTrustedProxy() && $host = $this->getTrustedValues(self::HEADER_X_FORWARDED_HOST)) {
|
|
|
930 |
$host = $host[0];
|
|
|
931 |
} elseif (!$host = $this->headers->get('HOST')) {
|
|
|
932 |
return $this->server->get('SERVER_PORT');
|
|
|
933 |
}
|
|
|
934 |
|
|
|
935 |
if ('[' === $host[0]) {
|
|
|
936 |
$pos = strpos($host, ':', strrpos($host, ']'));
|
|
|
937 |
} else {
|
|
|
938 |
$pos = strrpos($host, ':');
|
|
|
939 |
}
|
|
|
940 |
|
|
|
941 |
if (false !== $pos && $port = substr($host, $pos + 1)) {
|
|
|
942 |
return (int) $port;
|
|
|
943 |
}
|
|
|
944 |
|
|
|
945 |
return 'https' === $this->getScheme() ? 443 : 80;
|
|
|
946 |
}
|
|
|
947 |
|
|
|
948 |
/**
|
|
|
949 |
* Returns the user.
|
|
|
950 |
*
|
|
|
951 |
* @return string|null
|
|
|
952 |
*/
|
|
|
953 |
public function getUser()
|
|
|
954 |
{
|
|
|
955 |
return $this->headers->get('PHP_AUTH_USER');
|
|
|
956 |
}
|
|
|
957 |
|
|
|
958 |
/**
|
|
|
959 |
* Returns the password.
|
|
|
960 |
*
|
|
|
961 |
* @return string|null
|
|
|
962 |
*/
|
|
|
963 |
public function getPassword()
|
|
|
964 |
{
|
|
|
965 |
return $this->headers->get('PHP_AUTH_PW');
|
|
|
966 |
}
|
|
|
967 |
|
|
|
968 |
/**
|
|
|
969 |
* Gets the user info.
|
|
|
970 |
*
|
|
|
971 |
* @return string A user name and, optionally, scheme-specific information about how to gain authorization to access the server
|
|
|
972 |
*/
|
|
|
973 |
public function getUserInfo()
|
|
|
974 |
{
|
|
|
975 |
$userinfo = $this->getUser();
|
|
|
976 |
|
|
|
977 |
$pass = $this->getPassword();
|
|
|
978 |
if ('' != $pass) {
|
|
|
979 |
$userinfo .= ":$pass";
|
|
|
980 |
}
|
|
|
981 |
|
|
|
982 |
return $userinfo;
|
|
|
983 |
}
|
|
|
984 |
|
|
|
985 |
/**
|
|
|
986 |
* Returns the HTTP host being requested.
|
|
|
987 |
*
|
|
|
988 |
* The port name will be appended to the host if it's non-standard.
|
|
|
989 |
*
|
|
|
990 |
* @return string
|
|
|
991 |
*/
|
|
|
992 |
public function getHttpHost()
|
|
|
993 |
{
|
|
|
994 |
$scheme = $this->getScheme();
|
|
|
995 |
$port = $this->getPort();
|
|
|
996 |
|
|
|
997 |
if (('http' == $scheme && 80 == $port) || ('https' == $scheme && 443 == $port)) {
|
|
|
998 |
return $this->getHost();
|
|
|
999 |
}
|
|
|
1000 |
|
|
|
1001 |
return $this->getHost().':'.$port;
|
|
|
1002 |
}
|
|
|
1003 |
|
|
|
1004 |
/**
|
|
|
1005 |
* Returns the requested URI (path and query string).
|
|
|
1006 |
*
|
|
|
1007 |
* @return string The raw URI (i.e. not URI decoded)
|
|
|
1008 |
*/
|
|
|
1009 |
public function getRequestUri()
|
|
|
1010 |
{
|
|
|
1011 |
if (null === $this->requestUri) {
|
|
|
1012 |
$this->requestUri = $this->prepareRequestUri();
|
|
|
1013 |
}
|
|
|
1014 |
|
|
|
1015 |
return $this->requestUri;
|
|
|
1016 |
}
|
|
|
1017 |
|
|
|
1018 |
/**
|
|
|
1019 |
* Gets the scheme and HTTP host.
|
|
|
1020 |
*
|
|
|
1021 |
* If the URL was called with basic authentication, the user
|
|
|
1022 |
* and the password are not added to the generated string.
|
|
|
1023 |
*
|
|
|
1024 |
* @return string The scheme and HTTP host
|
|
|
1025 |
*/
|
|
|
1026 |
public function getSchemeAndHttpHost()
|
|
|
1027 |
{
|
|
|
1028 |
return $this->getScheme().'://'.$this->getHttpHost();
|
|
|
1029 |
}
|
|
|
1030 |
|
|
|
1031 |
/**
|
|
|
1032 |
* Generates a normalized URI (URL) for the Request.
|
|
|
1033 |
*
|
|
|
1034 |
* @return string A normalized URI (URL) for the Request
|
|
|
1035 |
*
|
|
|
1036 |
* @see getQueryString()
|
|
|
1037 |
*/
|
|
|
1038 |
public function getUri()
|
|
|
1039 |
{
|
|
|
1040 |
if (null !== $qs = $this->getQueryString()) {
|
|
|
1041 |
$qs = '?'.$qs;
|
|
|
1042 |
}
|
|
|
1043 |
|
|
|
1044 |
return $this->getSchemeAndHttpHost().$this->getBaseUrl().$this->getPathInfo().$qs;
|
|
|
1045 |
}
|
|
|
1046 |
|
|
|
1047 |
/**
|
|
|
1048 |
* Generates a normalized URI for the given path.
|
|
|
1049 |
*
|
|
|
1050 |
* @param string $path A path to use instead of the current one
|
|
|
1051 |
*
|
|
|
1052 |
* @return string The normalized URI for the path
|
|
|
1053 |
*/
|
|
|
1054 |
public function getUriForPath(string $path)
|
|
|
1055 |
{
|
|
|
1056 |
return $this->getSchemeAndHttpHost().$this->getBaseUrl().$path;
|
|
|
1057 |
}
|
|
|
1058 |
|
|
|
1059 |
/**
|
|
|
1060 |
* Returns the path as relative reference from the current Request path.
|
|
|
1061 |
*
|
|
|
1062 |
* Only the URIs path component (no schema, host etc.) is relevant and must be given.
|
|
|
1063 |
* Both paths must be absolute and not contain relative parts.
|
|
|
1064 |
* Relative URLs from one resource to another are useful when generating self-contained downloadable document archives.
|
|
|
1065 |
* Furthermore, they can be used to reduce the link size in documents.
|
|
|
1066 |
*
|
|
|
1067 |
* Example target paths, given a base path of "/a/b/c/d":
|
|
|
1068 |
* - "/a/b/c/d" -> ""
|
|
|
1069 |
* - "/a/b/c/" -> "./"
|
|
|
1070 |
* - "/a/b/" -> "../"
|
|
|
1071 |
* - "/a/b/c/other" -> "other"
|
|
|
1072 |
* - "/a/x/y" -> "../../x/y"
|
|
|
1073 |
*
|
|
|
1074 |
* @return string The relative target path
|
|
|
1075 |
*/
|
|
|
1076 |
public function getRelativeUriForPath(string $path)
|
|
|
1077 |
{
|
|
|
1078 |
// be sure that we are dealing with an absolute path
|
|
|
1079 |
if (!isset($path[0]) || '/' !== $path[0]) {
|
|
|
1080 |
return $path;
|
|
|
1081 |
}
|
|
|
1082 |
|
|
|
1083 |
if ($path === $basePath = $this->getPathInfo()) {
|
|
|
1084 |
return '';
|
|
|
1085 |
}
|
|
|
1086 |
|
|
|
1087 |
$sourceDirs = explode('/', isset($basePath[0]) && '/' === $basePath[0] ? substr($basePath, 1) : $basePath);
|
|
|
1088 |
$targetDirs = explode('/', substr($path, 1));
|
|
|
1089 |
array_pop($sourceDirs);
|
|
|
1090 |
$targetFile = array_pop($targetDirs);
|
|
|
1091 |
|
|
|
1092 |
foreach ($sourceDirs as $i => $dir) {
|
|
|
1093 |
if (isset($targetDirs[$i]) && $dir === $targetDirs[$i]) {
|
|
|
1094 |
unset($sourceDirs[$i], $targetDirs[$i]);
|
|
|
1095 |
} else {
|
|
|
1096 |
break;
|
|
|
1097 |
}
|
|
|
1098 |
}
|
|
|
1099 |
|
|
|
1100 |
$targetDirs[] = $targetFile;
|
|
|
1101 |
$path = str_repeat('../', \count($sourceDirs)).implode('/', $targetDirs);
|
|
|
1102 |
|
|
|
1103 |
// A reference to the same base directory or an empty subdirectory must be prefixed with "./".
|
|
|
1104 |
// This also applies to a segment with a colon character (e.g., "file:colon") that cannot be used
|
|
|
1105 |
// as the first segment of a relative-path reference, as it would be mistaken for a scheme name
|
|
|
1106 |
// (see https://tools.ietf.org/html/rfc3986#section-4.2).
|
|
|
1107 |
return !isset($path[0]) || '/' === $path[0]
|
|
|
1108 |
|| false !== ($colonPos = strpos($path, ':')) && ($colonPos < ($slashPos = strpos($path, '/')) || false === $slashPos)
|
|
|
1109 |
? "./$path" : $path;
|
|
|
1110 |
}
|
|
|
1111 |
|
|
|
1112 |
/**
|
|
|
1113 |
* Generates the normalized query string for the Request.
|
|
|
1114 |
*
|
|
|
1115 |
* It builds a normalized query string, where keys/value pairs are alphabetized
|
|
|
1116 |
* and have consistent escaping.
|
|
|
1117 |
*
|
|
|
1118 |
* @return string|null A normalized query string for the Request
|
|
|
1119 |
*/
|
|
|
1120 |
public function getQueryString()
|
|
|
1121 |
{
|
|
|
1122 |
$qs = static::normalizeQueryString($this->server->get('QUERY_STRING'));
|
|
|
1123 |
|
|
|
1124 |
return '' === $qs ? null : $qs;
|
|
|
1125 |
}
|
|
|
1126 |
|
|
|
1127 |
/**
|
|
|
1128 |
* Checks whether the request is secure or not.
|
|
|
1129 |
*
|
|
|
1130 |
* This method can read the client protocol from the "X-Forwarded-Proto" header
|
|
|
1131 |
* when trusted proxies were set via "setTrustedProxies()".
|
|
|
1132 |
*
|
|
|
1133 |
* The "X-Forwarded-Proto" header must contain the protocol: "https" or "http".
|
|
|
1134 |
*
|
|
|
1135 |
* @return bool
|
|
|
1136 |
*/
|
|
|
1137 |
public function isSecure()
|
|
|
1138 |
{
|
|
|
1139 |
if ($this->isFromTrustedProxy() && $proto = $this->getTrustedValues(self::HEADER_X_FORWARDED_PROTO)) {
|
|
|
1140 |
return \in_array(strtolower($proto[0]), ['https', 'on', 'ssl', '1'], true);
|
|
|
1141 |
}
|
|
|
1142 |
|
|
|
1143 |
$https = $this->server->get('HTTPS');
|
|
|
1144 |
|
|
|
1145 |
return !empty($https) && 'off' !== strtolower($https);
|
|
|
1146 |
}
|
|
|
1147 |
|
|
|
1148 |
/**
|
|
|
1149 |
* Returns the host name.
|
|
|
1150 |
*
|
|
|
1151 |
* This method can read the client host name from the "X-Forwarded-Host" header
|
|
|
1152 |
* when trusted proxies were set via "setTrustedProxies()".
|
|
|
1153 |
*
|
|
|
1154 |
* The "X-Forwarded-Host" header must contain the client host name.
|
|
|
1155 |
*
|
|
|
1156 |
* @return string
|
|
|
1157 |
*
|
|
|
1158 |
* @throws SuspiciousOperationException when the host name is invalid or not trusted
|
|
|
1159 |
*/
|
|
|
1160 |
public function getHost()
|
|
|
1161 |
{
|
|
|
1162 |
if ($this->isFromTrustedProxy() && $host = $this->getTrustedValues(self::HEADER_X_FORWARDED_HOST)) {
|
|
|
1163 |
$host = $host[0];
|
|
|
1164 |
} elseif (!$host = $this->headers->get('HOST')) {
|
|
|
1165 |
if (!$host = $this->server->get('SERVER_NAME')) {
|
|
|
1166 |
$host = $this->server->get('SERVER_ADDR', '');
|
|
|
1167 |
}
|
|
|
1168 |
}
|
|
|
1169 |
|
|
|
1170 |
// trim and remove port number from host
|
|
|
1171 |
// host is lowercase as per RFC 952/2181
|
|
|
1172 |
$host = strtolower(preg_replace('/:\d+$/', '', trim($host)));
|
|
|
1173 |
|
|
|
1174 |
// as the host can come from the user (HTTP_HOST and depending on the configuration, SERVER_NAME too can come from the user)
|
|
|
1175 |
// check that it does not contain forbidden characters (see RFC 952 and RFC 2181)
|
|
|
1176 |
// use preg_replace() instead of preg_match() to prevent DoS attacks with long host names
|
|
|
1177 |
if ($host && '' !== preg_replace('/(?:^\[)?[a-zA-Z0-9-:\]_]+\.?/', '', $host)) {
|
|
|
1178 |
if (!$this->isHostValid) {
|
|
|
1179 |
return '';
|
|
|
1180 |
}
|
|
|
1181 |
$this->isHostValid = false;
|
|
|
1182 |
|
|
|
1183 |
throw new SuspiciousOperationException(sprintf('Invalid Host "%s".', $host));
|
|
|
1184 |
}
|
|
|
1185 |
|
|
|
1186 |
if (\count(self::$trustedHostPatterns) > 0) {
|
|
|
1187 |
// to avoid host header injection attacks, you should provide a list of trusted host patterns
|
|
|
1188 |
|
|
|
1189 |
if (\in_array($host, self::$trustedHosts)) {
|
|
|
1190 |
return $host;
|
|
|
1191 |
}
|
|
|
1192 |
|
|
|
1193 |
foreach (self::$trustedHostPatterns as $pattern) {
|
|
|
1194 |
if (preg_match($pattern, $host)) {
|
|
|
1195 |
self::$trustedHosts[] = $host;
|
|
|
1196 |
|
|
|
1197 |
return $host;
|
|
|
1198 |
}
|
|
|
1199 |
}
|
|
|
1200 |
|
|
|
1201 |
if (!$this->isHostValid) {
|
|
|
1202 |
return '';
|
|
|
1203 |
}
|
|
|
1204 |
$this->isHostValid = false;
|
|
|
1205 |
|
|
|
1206 |
throw new SuspiciousOperationException(sprintf('Untrusted Host "%s".', $host));
|
|
|
1207 |
}
|
|
|
1208 |
|
|
|
1209 |
return $host;
|
|
|
1210 |
}
|
|
|
1211 |
|
|
|
1212 |
/**
|
|
|
1213 |
* Sets the request method.
|
|
|
1214 |
*/
|
|
|
1215 |
public function setMethod(string $method)
|
|
|
1216 |
{
|
|
|
1217 |
$this->method = null;
|
|
|
1218 |
$this->server->set('REQUEST_METHOD', $method);
|
|
|
1219 |
}
|
|
|
1220 |
|
|
|
1221 |
/**
|
|
|
1222 |
* Gets the request "intended" method.
|
|
|
1223 |
*
|
|
|
1224 |
* If the X-HTTP-Method-Override header is set, and if the method is a POST,
|
|
|
1225 |
* then it is used to determine the "real" intended HTTP method.
|
|
|
1226 |
*
|
|
|
1227 |
* The _method request parameter can also be used to determine the HTTP method,
|
|
|
1228 |
* but only if enableHttpMethodParameterOverride() has been called.
|
|
|
1229 |
*
|
|
|
1230 |
* The method is always an uppercased string.
|
|
|
1231 |
*
|
|
|
1232 |
* @return string The request method
|
|
|
1233 |
*
|
|
|
1234 |
* @see getRealMethod()
|
|
|
1235 |
*/
|
|
|
1236 |
public function getMethod()
|
|
|
1237 |
{
|
|
|
1238 |
if (null !== $this->method) {
|
|
|
1239 |
return $this->method;
|
|
|
1240 |
}
|
|
|
1241 |
|
|
|
1242 |
$this->method = strtoupper($this->server->get('REQUEST_METHOD', 'GET'));
|
|
|
1243 |
|
|
|
1244 |
if ('POST' !== $this->method) {
|
|
|
1245 |
return $this->method;
|
|
|
1246 |
}
|
|
|
1247 |
|
|
|
1248 |
$method = $this->headers->get('X-HTTP-METHOD-OVERRIDE');
|
|
|
1249 |
|
|
|
1250 |
if (!$method && self::$httpMethodParameterOverride) {
|
|
|
1251 |
$method = $this->request->get('_method', $this->query->get('_method', 'POST'));
|
|
|
1252 |
}
|
|
|
1253 |
|
|
|
1254 |
if (!\is_string($method)) {
|
|
|
1255 |
return $this->method;
|
|
|
1256 |
}
|
|
|
1257 |
|
|
|
1258 |
$method = strtoupper($method);
|
|
|
1259 |
|
|
|
1260 |
if (\in_array($method, ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'PATCH', 'PURGE', 'TRACE'], true)) {
|
|
|
1261 |
return $this->method = $method;
|
|
|
1262 |
}
|
|
|
1263 |
|
|
|
1264 |
if (!preg_match('/^[A-Z]++$/D', $method)) {
|
|
|
1265 |
throw new SuspiciousOperationException(sprintf('Invalid method override "%s".', $method));
|
|
|
1266 |
}
|
|
|
1267 |
|
|
|
1268 |
return $this->method = $method;
|
|
|
1269 |
}
|
|
|
1270 |
|
|
|
1271 |
/**
|
|
|
1272 |
* Gets the "real" request method.
|
|
|
1273 |
*
|
|
|
1274 |
* @return string The request method
|
|
|
1275 |
*
|
|
|
1276 |
* @see getMethod()
|
|
|
1277 |
*/
|
|
|
1278 |
public function getRealMethod()
|
|
|
1279 |
{
|
|
|
1280 |
return strtoupper($this->server->get('REQUEST_METHOD', 'GET'));
|
|
|
1281 |
}
|
|
|
1282 |
|
|
|
1283 |
/**
|
|
|
1284 |
* Gets the mime type associated with the format.
|
|
|
1285 |
*
|
|
|
1286 |
* @return string|null The associated mime type (null if not found)
|
|
|
1287 |
*/
|
|
|
1288 |
public function getMimeType(string $format)
|
|
|
1289 |
{
|
|
|
1290 |
if (null === static::$formats) {
|
|
|
1291 |
static::initializeFormats();
|
|
|
1292 |
}
|
|
|
1293 |
|
|
|
1294 |
return isset(static::$formats[$format]) ? static::$formats[$format][0] : null;
|
|
|
1295 |
}
|
|
|
1296 |
|
|
|
1297 |
/**
|
|
|
1298 |
* Gets the mime types associated with the format.
|
|
|
1299 |
*
|
|
|
1300 |
* @return array The associated mime types
|
|
|
1301 |
*/
|
|
|
1302 |
public static function getMimeTypes(string $format)
|
|
|
1303 |
{
|
|
|
1304 |
if (null === static::$formats) {
|
|
|
1305 |
static::initializeFormats();
|
|
|
1306 |
}
|
|
|
1307 |
|
|
|
1308 |
return isset(static::$formats[$format]) ? static::$formats[$format] : [];
|
|
|
1309 |
}
|
|
|
1310 |
|
|
|
1311 |
/**
|
|
|
1312 |
* Gets the format associated with the mime type.
|
|
|
1313 |
*
|
|
|
1314 |
* @return string|null The format (null if not found)
|
|
|
1315 |
*/
|
|
|
1316 |
public function getFormat(?string $mimeType)
|
|
|
1317 |
{
|
|
|
1318 |
$canonicalMimeType = null;
|
|
|
1319 |
if (false !== $pos = strpos($mimeType, ';')) {
|
|
|
1320 |
$canonicalMimeType = trim(substr($mimeType, 0, $pos));
|
|
|
1321 |
}
|
|
|
1322 |
|
|
|
1323 |
if (null === static::$formats) {
|
|
|
1324 |
static::initializeFormats();
|
|
|
1325 |
}
|
|
|
1326 |
|
|
|
1327 |
foreach (static::$formats as $format => $mimeTypes) {
|
|
|
1328 |
if (\in_array($mimeType, (array) $mimeTypes)) {
|
|
|
1329 |
return $format;
|
|
|
1330 |
}
|
|
|
1331 |
if (null !== $canonicalMimeType && \in_array($canonicalMimeType, (array) $mimeTypes)) {
|
|
|
1332 |
return $format;
|
|
|
1333 |
}
|
|
|
1334 |
}
|
|
|
1335 |
|
|
|
1336 |
return null;
|
|
|
1337 |
}
|
|
|
1338 |
|
|
|
1339 |
/**
|
|
|
1340 |
* Associates a format with mime types.
|
|
|
1341 |
*
|
|
|
1342 |
* @param string|array $mimeTypes The associated mime types (the preferred one must be the first as it will be used as the content type)
|
|
|
1343 |
*/
|
|
|
1344 |
public function setFormat(?string $format, $mimeTypes)
|
|
|
1345 |
{
|
|
|
1346 |
if (null === static::$formats) {
|
|
|
1347 |
static::initializeFormats();
|
|
|
1348 |
}
|
|
|
1349 |
|
|
|
1350 |
static::$formats[$format] = \is_array($mimeTypes) ? $mimeTypes : [$mimeTypes];
|
|
|
1351 |
}
|
|
|
1352 |
|
|
|
1353 |
/**
|
|
|
1354 |
* Gets the request format.
|
|
|
1355 |
*
|
|
|
1356 |
* Here is the process to determine the format:
|
|
|
1357 |
*
|
|
|
1358 |
* * format defined by the user (with setRequestFormat())
|
|
|
1359 |
* * _format request attribute
|
|
|
1360 |
* * $default
|
|
|
1361 |
*
|
|
|
1362 |
* @see getPreferredFormat
|
|
|
1363 |
*
|
|
|
1364 |
* @return string|null The request format
|
|
|
1365 |
*/
|
|
|
1366 |
public function getRequestFormat(?string $default = 'html')
|
|
|
1367 |
{
|
|
|
1368 |
if (null === $this->format) {
|
|
|
1369 |
$this->format = $this->attributes->get('_format');
|
|
|
1370 |
}
|
|
|
1371 |
|
|
|
1372 |
return null === $this->format ? $default : $this->format;
|
|
|
1373 |
}
|
|
|
1374 |
|
|
|
1375 |
/**
|
|
|
1376 |
* Sets the request format.
|
|
|
1377 |
*/
|
|
|
1378 |
public function setRequestFormat(?string $format)
|
|
|
1379 |
{
|
|
|
1380 |
$this->format = $format;
|
|
|
1381 |
}
|
|
|
1382 |
|
|
|
1383 |
/**
|
|
|
1384 |
* Gets the format associated with the request.
|
|
|
1385 |
*
|
|
|
1386 |
* @return string|null The format (null if no content type is present)
|
|
|
1387 |
*/
|
|
|
1388 |
public function getContentType()
|
|
|
1389 |
{
|
|
|
1390 |
return $this->getFormat($this->headers->get('CONTENT_TYPE'));
|
|
|
1391 |
}
|
|
|
1392 |
|
|
|
1393 |
/**
|
|
|
1394 |
* Sets the default locale.
|
|
|
1395 |
*/
|
|
|
1396 |
public function setDefaultLocale(string $locale)
|
|
|
1397 |
{
|
|
|
1398 |
$this->defaultLocale = $locale;
|
|
|
1399 |
|
|
|
1400 |
if (null === $this->locale) {
|
|
|
1401 |
$this->setPhpDefaultLocale($locale);
|
|
|
1402 |
}
|
|
|
1403 |
}
|
|
|
1404 |
|
|
|
1405 |
/**
|
|
|
1406 |
* Get the default locale.
|
|
|
1407 |
*
|
|
|
1408 |
* @return string
|
|
|
1409 |
*/
|
|
|
1410 |
public function getDefaultLocale()
|
|
|
1411 |
{
|
|
|
1412 |
return $this->defaultLocale;
|
|
|
1413 |
}
|
|
|
1414 |
|
|
|
1415 |
/**
|
|
|
1416 |
* Sets the locale.
|
|
|
1417 |
*/
|
|
|
1418 |
public function setLocale(string $locale)
|
|
|
1419 |
{
|
|
|
1420 |
$this->setPhpDefaultLocale($this->locale = $locale);
|
|
|
1421 |
}
|
|
|
1422 |
|
|
|
1423 |
/**
|
|
|
1424 |
* Get the locale.
|
|
|
1425 |
*
|
|
|
1426 |
* @return string
|
|
|
1427 |
*/
|
|
|
1428 |
public function getLocale()
|
|
|
1429 |
{
|
|
|
1430 |
return null === $this->locale ? $this->defaultLocale : $this->locale;
|
|
|
1431 |
}
|
|
|
1432 |
|
|
|
1433 |
/**
|
|
|
1434 |
* Checks if the request method is of specified type.
|
|
|
1435 |
*
|
|
|
1436 |
* @param string $method Uppercase request method (GET, POST etc)
|
|
|
1437 |
*
|
|
|
1438 |
* @return bool
|
|
|
1439 |
*/
|
|
|
1440 |
public function isMethod(string $method)
|
|
|
1441 |
{
|
|
|
1442 |
return $this->getMethod() === strtoupper($method);
|
|
|
1443 |
}
|
|
|
1444 |
|
|
|
1445 |
/**
|
|
|
1446 |
* Checks whether or not the method is safe.
|
|
|
1447 |
*
|
|
|
1448 |
* @see https://tools.ietf.org/html/rfc7231#section-4.2.1
|
|
|
1449 |
*
|
|
|
1450 |
* @return bool
|
|
|
1451 |
*/
|
|
|
1452 |
public function isMethodSafe()
|
|
|
1453 |
{
|
|
|
1454 |
return \in_array($this->getMethod(), ['GET', 'HEAD', 'OPTIONS', 'TRACE']);
|
|
|
1455 |
}
|
|
|
1456 |
|
|
|
1457 |
/**
|
|
|
1458 |
* Checks whether or not the method is idempotent.
|
|
|
1459 |
*
|
|
|
1460 |
* @return bool
|
|
|
1461 |
*/
|
|
|
1462 |
public function isMethodIdempotent()
|
|
|
1463 |
{
|
|
|
1464 |
return \in_array($this->getMethod(), ['HEAD', 'GET', 'PUT', 'DELETE', 'TRACE', 'OPTIONS', 'PURGE']);
|
|
|
1465 |
}
|
|
|
1466 |
|
|
|
1467 |
/**
|
|
|
1468 |
* Checks whether the method is cacheable or not.
|
|
|
1469 |
*
|
|
|
1470 |
* @see https://tools.ietf.org/html/rfc7231#section-4.2.3
|
|
|
1471 |
*
|
|
|
1472 |
* @return bool True for GET and HEAD, false otherwise
|
|
|
1473 |
*/
|
|
|
1474 |
public function isMethodCacheable()
|
|
|
1475 |
{
|
|
|
1476 |
return \in_array($this->getMethod(), ['GET', 'HEAD']);
|
|
|
1477 |
}
|
|
|
1478 |
|
|
|
1479 |
/**
|
|
|
1480 |
* Returns the protocol version.
|
|
|
1481 |
*
|
|
|
1482 |
* If the application is behind a proxy, the protocol version used in the
|
|
|
1483 |
* requests between the client and the proxy and between the proxy and the
|
|
|
1484 |
* server might be different. This returns the former (from the "Via" header)
|
|
|
1485 |
* if the proxy is trusted (see "setTrustedProxies()"), otherwise it returns
|
|
|
1486 |
* the latter (from the "SERVER_PROTOCOL" server parameter).
|
|
|
1487 |
*
|
|
|
1488 |
* @return string
|
|
|
1489 |
*/
|
|
|
1490 |
public function getProtocolVersion()
|
|
|
1491 |
{
|
|
|
1492 |
if ($this->isFromTrustedProxy()) {
|
|
|
1493 |
preg_match('~^(HTTP/)?([1-9]\.[0-9]) ~', $this->headers->get('Via'), $matches);
|
|
|
1494 |
|
|
|
1495 |
if ($matches) {
|
|
|
1496 |
return 'HTTP/'.$matches[2];
|
|
|
1497 |
}
|
|
|
1498 |
}
|
|
|
1499 |
|
|
|
1500 |
return $this->server->get('SERVER_PROTOCOL');
|
|
|
1501 |
}
|
|
|
1502 |
|
|
|
1503 |
/**
|
|
|
1504 |
* Returns the request body content.
|
|
|
1505 |
*
|
|
|
1506 |
* @param bool $asResource If true, a resource will be returned
|
|
|
1507 |
*
|
|
|
1508 |
* @return string|resource The request body content or a resource to read the body stream
|
|
|
1509 |
*
|
|
|
1510 |
* @throws \LogicException
|
|
|
1511 |
*/
|
|
|
1512 |
public function getContent(bool $asResource = false)
|
|
|
1513 |
{
|
|
|
1514 |
$currentContentIsResource = \is_resource($this->content);
|
|
|
1515 |
|
|
|
1516 |
if (true === $asResource) {
|
|
|
1517 |
if ($currentContentIsResource) {
|
|
|
1518 |
rewind($this->content);
|
|
|
1519 |
|
|
|
1520 |
return $this->content;
|
|
|
1521 |
}
|
|
|
1522 |
|
|
|
1523 |
// Content passed in parameter (test)
|
|
|
1524 |
if (\is_string($this->content)) {
|
|
|
1525 |
$resource = fopen('php://temp', 'r+');
|
|
|
1526 |
fwrite($resource, $this->content);
|
|
|
1527 |
rewind($resource);
|
|
|
1528 |
|
|
|
1529 |
return $resource;
|
|
|
1530 |
}
|
|
|
1531 |
|
|
|
1532 |
$this->content = false;
|
|
|
1533 |
|
|
|
1534 |
return fopen('php://input', 'rb');
|
|
|
1535 |
}
|
|
|
1536 |
|
|
|
1537 |
if ($currentContentIsResource) {
|
|
|
1538 |
rewind($this->content);
|
|
|
1539 |
|
|
|
1540 |
return stream_get_contents($this->content);
|
|
|
1541 |
}
|
|
|
1542 |
|
|
|
1543 |
if (null === $this->content || false === $this->content) {
|
|
|
1544 |
$this->content = file_get_contents('php://input');
|
|
|
1545 |
}
|
|
|
1546 |
|
|
|
1547 |
return $this->content;
|
|
|
1548 |
}
|
|
|
1549 |
|
|
|
1550 |
/**
|
|
|
1551 |
* Gets the Etags.
|
|
|
1552 |
*
|
|
|
1553 |
* @return array The entity tags
|
|
|
1554 |
*/
|
|
|
1555 |
public function getETags()
|
|
|
1556 |
{
|
|
|
1557 |
return preg_split('/\s*,\s*/', $this->headers->get('if_none_match'), null, \PREG_SPLIT_NO_EMPTY);
|
|
|
1558 |
}
|
|
|
1559 |
|
|
|
1560 |
/**
|
|
|
1561 |
* @return bool
|
|
|
1562 |
*/
|
|
|
1563 |
public function isNoCache()
|
|
|
1564 |
{
|
|
|
1565 |
return $this->headers->hasCacheControlDirective('no-cache') || 'no-cache' == $this->headers->get('Pragma');
|
|
|
1566 |
}
|
|
|
1567 |
|
|
|
1568 |
/**
|
|
|
1569 |
* Gets the preferred format for the response by inspecting, in the following order:
|
|
|
1570 |
* * the request format set using setRequestFormat;
|
|
|
1571 |
* * the values of the Accept HTTP header.
|
|
|
1572 |
*
|
|
|
1573 |
* Note that if you use this method, you should send the "Vary: Accept" header
|
|
|
1574 |
* in the response to prevent any issues with intermediary HTTP caches.
|
|
|
1575 |
*/
|
|
|
1576 |
public function getPreferredFormat(?string $default = 'html'): ?string
|
|
|
1577 |
{
|
|
|
1578 |
if (null !== $this->preferredFormat || null !== $this->preferredFormat = $this->getRequestFormat(null)) {
|
|
|
1579 |
return $this->preferredFormat;
|
|
|
1580 |
}
|
|
|
1581 |
|
|
|
1582 |
foreach ($this->getAcceptableContentTypes() as $mimeType) {
|
|
|
1583 |
if ($this->preferredFormat = $this->getFormat($mimeType)) {
|
|
|
1584 |
return $this->preferredFormat;
|
|
|
1585 |
}
|
|
|
1586 |
}
|
|
|
1587 |
|
|
|
1588 |
return $default;
|
|
|
1589 |
}
|
|
|
1590 |
|
|
|
1591 |
/**
|
|
|
1592 |
* Returns the preferred language.
|
|
|
1593 |
*
|
|
|
1594 |
* @param string[] $locales An array of ordered available locales
|
|
|
1595 |
*
|
|
|
1596 |
* @return string|null The preferred locale
|
|
|
1597 |
*/
|
|
|
1598 |
public function getPreferredLanguage(array $locales = null)
|
|
|
1599 |
{
|
|
|
1600 |
$preferredLanguages = $this->getLanguages();
|
|
|
1601 |
|
|
|
1602 |
if (empty($locales)) {
|
|
|
1603 |
return isset($preferredLanguages[0]) ? $preferredLanguages[0] : null;
|
|
|
1604 |
}
|
|
|
1605 |
|
|
|
1606 |
if (!$preferredLanguages) {
|
|
|
1607 |
return $locales[0];
|
|
|
1608 |
}
|
|
|
1609 |
|
|
|
1610 |
$extendedPreferredLanguages = [];
|
|
|
1611 |
foreach ($preferredLanguages as $language) {
|
|
|
1612 |
$extendedPreferredLanguages[] = $language;
|
|
|
1613 |
if (false !== $position = strpos($language, '_')) {
|
|
|
1614 |
$superLanguage = substr($language, 0, $position);
|
|
|
1615 |
if (!\in_array($superLanguage, $preferredLanguages)) {
|
|
|
1616 |
$extendedPreferredLanguages[] = $superLanguage;
|
|
|
1617 |
}
|
|
|
1618 |
}
|
|
|
1619 |
}
|
|
|
1620 |
|
|
|
1621 |
$preferredLanguages = array_values(array_intersect($extendedPreferredLanguages, $locales));
|
|
|
1622 |
|
|
|
1623 |
return isset($preferredLanguages[0]) ? $preferredLanguages[0] : $locales[0];
|
|
|
1624 |
}
|
|
|
1625 |
|
|
|
1626 |
/**
|
|
|
1627 |
* Gets a list of languages acceptable by the client browser.
|
|
|
1628 |
*
|
|
|
1629 |
* @return array Languages ordered in the user browser preferences
|
|
|
1630 |
*/
|
|
|
1631 |
public function getLanguages()
|
|
|
1632 |
{
|
|
|
1633 |
if (null !== $this->languages) {
|
|
|
1634 |
return $this->languages;
|
|
|
1635 |
}
|
|
|
1636 |
|
|
|
1637 |
$languages = AcceptHeader::fromString($this->headers->get('Accept-Language'))->all();
|
|
|
1638 |
$this->languages = [];
|
|
|
1639 |
foreach ($languages as $lang => $acceptHeaderItem) {
|
|
|
1640 |
if (false !== strpos($lang, '-')) {
|
|
|
1641 |
$codes = explode('-', $lang);
|
|
|
1642 |
if ('i' === $codes[0]) {
|
|
|
1643 |
// Language not listed in ISO 639 that are not variants
|
|
|
1644 |
// of any listed language, which can be registered with the
|
|
|
1645 |
// i-prefix, such as i-cherokee
|
|
|
1646 |
if (\count($codes) > 1) {
|
|
|
1647 |
$lang = $codes[1];
|
|
|
1648 |
}
|
|
|
1649 |
} else {
|
|
|
1650 |
for ($i = 0, $max = \count($codes); $i < $max; ++$i) {
|
|
|
1651 |
if (0 === $i) {
|
|
|
1652 |
$lang = strtolower($codes[0]);
|
|
|
1653 |
} else {
|
|
|
1654 |
$lang .= '_'.strtoupper($codes[$i]);
|
|
|
1655 |
}
|
|
|
1656 |
}
|
|
|
1657 |
}
|
|
|
1658 |
}
|
|
|
1659 |
|
|
|
1660 |
$this->languages[] = $lang;
|
|
|
1661 |
}
|
|
|
1662 |
|
|
|
1663 |
return $this->languages;
|
|
|
1664 |
}
|
|
|
1665 |
|
|
|
1666 |
/**
|
|
|
1667 |
* Gets a list of charsets acceptable by the client browser.
|
|
|
1668 |
*
|
|
|
1669 |
* @return array List of charsets in preferable order
|
|
|
1670 |
*/
|
|
|
1671 |
public function getCharsets()
|
|
|
1672 |
{
|
|
|
1673 |
if (null !== $this->charsets) {
|
|
|
1674 |
return $this->charsets;
|
|
|
1675 |
}
|
|
|
1676 |
|
|
|
1677 |
return $this->charsets = array_keys(AcceptHeader::fromString($this->headers->get('Accept-Charset'))->all());
|
|
|
1678 |
}
|
|
|
1679 |
|
|
|
1680 |
/**
|
|
|
1681 |
* Gets a list of encodings acceptable by the client browser.
|
|
|
1682 |
*
|
|
|
1683 |
* @return array List of encodings in preferable order
|
|
|
1684 |
*/
|
|
|
1685 |
public function getEncodings()
|
|
|
1686 |
{
|
|
|
1687 |
if (null !== $this->encodings) {
|
|
|
1688 |
return $this->encodings;
|
|
|
1689 |
}
|
|
|
1690 |
|
|
|
1691 |
return $this->encodings = array_keys(AcceptHeader::fromString($this->headers->get('Accept-Encoding'))->all());
|
|
|
1692 |
}
|
|
|
1693 |
|
|
|
1694 |
/**
|
|
|
1695 |
* Gets a list of content types acceptable by the client browser.
|
|
|
1696 |
*
|
|
|
1697 |
* @return array List of content types in preferable order
|
|
|
1698 |
*/
|
|
|
1699 |
public function getAcceptableContentTypes()
|
|
|
1700 |
{
|
|
|
1701 |
if (null !== $this->acceptableContentTypes) {
|
|
|
1702 |
return $this->acceptableContentTypes;
|
|
|
1703 |
}
|
|
|
1704 |
|
|
|
1705 |
return $this->acceptableContentTypes = array_keys(AcceptHeader::fromString($this->headers->get('Accept'))->all());
|
|
|
1706 |
}
|
|
|
1707 |
|
|
|
1708 |
/**
|
|
|
1709 |
* Returns true if the request is a XMLHttpRequest.
|
|
|
1710 |
*
|
|
|
1711 |
* It works if your JavaScript library sets an X-Requested-With HTTP header.
|
|
|
1712 |
* It is known to work with common JavaScript frameworks:
|
|
|
1713 |
*
|
|
|
1714 |
* @see https://wikipedia.org/wiki/List_of_Ajax_frameworks#JavaScript
|
|
|
1715 |
*
|
|
|
1716 |
* @return bool true if the request is an XMLHttpRequest, false otherwise
|
|
|
1717 |
*/
|
|
|
1718 |
public function isXmlHttpRequest()
|
|
|
1719 |
{
|
|
|
1720 |
return 'XMLHttpRequest' == $this->headers->get('X-Requested-With');
|
|
|
1721 |
}
|
|
|
1722 |
|
|
|
1723 |
/**
|
|
|
1724 |
* Checks whether the client browser prefers safe content or not according to RFC8674.
|
|
|
1725 |
*
|
|
|
1726 |
* @see https://tools.ietf.org/html/rfc8674
|
|
|
1727 |
*/
|
|
|
1728 |
public function preferSafeContent(): bool
|
|
|
1729 |
{
|
|
|
1730 |
if (null !== $this->isSafeContentPreferred) {
|
|
|
1731 |
return $this->isSafeContentPreferred;
|
|
|
1732 |
}
|
|
|
1733 |
|
|
|
1734 |
if (!$this->isSecure()) {
|
|
|
1735 |
// see https://tools.ietf.org/html/rfc8674#section-3
|
|
|
1736 |
$this->isSafeContentPreferred = false;
|
|
|
1737 |
|
|
|
1738 |
return $this->isSafeContentPreferred;
|
|
|
1739 |
}
|
|
|
1740 |
|
|
|
1741 |
$this->isSafeContentPreferred = AcceptHeader::fromString($this->headers->get('Prefer'))->has('safe');
|
|
|
1742 |
|
|
|
1743 |
return $this->isSafeContentPreferred;
|
|
|
1744 |
}
|
|
|
1745 |
|
|
|
1746 |
/*
|
|
|
1747 |
* The following methods are derived from code of the Zend Framework (1.10dev - 2010-01-24)
|
|
|
1748 |
*
|
|
|
1749 |
* Code subject to the new BSD license (https://framework.zend.com/license).
|
|
|
1750 |
*
|
|
|
1751 |
* Copyright (c) 2005-2010 Zend Technologies USA Inc. (https://www.zend.com/)
|
|
|
1752 |
*/
|
|
|
1753 |
|
|
|
1754 |
protected function prepareRequestUri()
|
|
|
1755 |
{
|
|
|
1756 |
$requestUri = '';
|
|
|
1757 |
|
|
|
1758 |
if ('1' == $this->server->get('IIS_WasUrlRewritten') && '' != $this->server->get('UNENCODED_URL')) {
|
|
|
1759 |
// IIS7 with URL Rewrite: make sure we get the unencoded URL (double slash problem)
|
|
|
1760 |
$requestUri = $this->server->get('UNENCODED_URL');
|
|
|
1761 |
$this->server->remove('UNENCODED_URL');
|
|
|
1762 |
$this->server->remove('IIS_WasUrlRewritten');
|
|
|
1763 |
} elseif ($this->server->has('REQUEST_URI')) {
|
|
|
1764 |
$requestUri = $this->server->get('REQUEST_URI');
|
|
|
1765 |
|
|
|
1766 |
if ('' !== $requestUri && '/' === $requestUri[0]) {
|
|
|
1767 |
// To only use path and query remove the fragment.
|
|
|
1768 |
if (false !== $pos = strpos($requestUri, '#')) {
|
|
|
1769 |
$requestUri = substr($requestUri, 0, $pos);
|
|
|
1770 |
}
|
|
|
1771 |
} else {
|
|
|
1772 |
// HTTP proxy reqs setup request URI with scheme and host [and port] + the URL path,
|
|
|
1773 |
// only use URL path.
|
|
|
1774 |
$uriComponents = parse_url($requestUri);
|
|
|
1775 |
|
|
|
1776 |
if (isset($uriComponents['path'])) {
|
|
|
1777 |
$requestUri = $uriComponents['path'];
|
|
|
1778 |
}
|
|
|
1779 |
|
|
|
1780 |
if (isset($uriComponents['query'])) {
|
|
|
1781 |
$requestUri .= '?'.$uriComponents['query'];
|
|
|
1782 |
}
|
|
|
1783 |
}
|
|
|
1784 |
} elseif ($this->server->has('ORIG_PATH_INFO')) {
|
|
|
1785 |
// IIS 5.0, PHP as CGI
|
|
|
1786 |
$requestUri = $this->server->get('ORIG_PATH_INFO');
|
|
|
1787 |
if ('' != $this->server->get('QUERY_STRING')) {
|
|
|
1788 |
$requestUri .= '?'.$this->server->get('QUERY_STRING');
|
|
|
1789 |
}
|
|
|
1790 |
$this->server->remove('ORIG_PATH_INFO');
|
|
|
1791 |
}
|
|
|
1792 |
|
|
|
1793 |
// normalize the request URI to ease creating sub-requests from this request
|
|
|
1794 |
$this->server->set('REQUEST_URI', $requestUri);
|
|
|
1795 |
|
|
|
1796 |
return $requestUri;
|
|
|
1797 |
}
|
|
|
1798 |
|
|
|
1799 |
/**
|
|
|
1800 |
* Prepares the base URL.
|
|
|
1801 |
*
|
|
|
1802 |
* @return string
|
|
|
1803 |
*/
|
|
|
1804 |
protected function prepareBaseUrl()
|
|
|
1805 |
{
|
|
|
1806 |
$filename = basename($this->server->get('SCRIPT_FILENAME'));
|
|
|
1807 |
|
|
|
1808 |
if (basename($this->server->get('SCRIPT_NAME')) === $filename) {
|
|
|
1809 |
$baseUrl = $this->server->get('SCRIPT_NAME');
|
|
|
1810 |
} elseif (basename($this->server->get('PHP_SELF')) === $filename) {
|
|
|
1811 |
$baseUrl = $this->server->get('PHP_SELF');
|
|
|
1812 |
} elseif (basename($this->server->get('ORIG_SCRIPT_NAME')) === $filename) {
|
|
|
1813 |
$baseUrl = $this->server->get('ORIG_SCRIPT_NAME'); // 1and1 shared hosting compatibility
|
|
|
1814 |
} else {
|
|
|
1815 |
// Backtrack up the script_filename to find the portion matching
|
|
|
1816 |
// php_self
|
|
|
1817 |
$path = $this->server->get('PHP_SELF', '');
|
|
|
1818 |
$file = $this->server->get('SCRIPT_FILENAME', '');
|
|
|
1819 |
$segs = explode('/', trim($file, '/'));
|
|
|
1820 |
$segs = array_reverse($segs);
|
|
|
1821 |
$index = 0;
|
|
|
1822 |
$last = \count($segs);
|
|
|
1823 |
$baseUrl = '';
|
|
|
1824 |
do {
|
|
|
1825 |
$seg = $segs[$index];
|
|
|
1826 |
$baseUrl = '/'.$seg.$baseUrl;
|
|
|
1827 |
++$index;
|
|
|
1828 |
} while ($last > $index && (false !== $pos = strpos($path, $baseUrl)) && 0 != $pos);
|
|
|
1829 |
}
|
|
|
1830 |
|
|
|
1831 |
// Does the baseUrl have anything in common with the request_uri?
|
|
|
1832 |
$requestUri = $this->getRequestUri();
|
|
|
1833 |
if ('' !== $requestUri && '/' !== $requestUri[0]) {
|
|
|
1834 |
$requestUri = '/'.$requestUri;
|
|
|
1835 |
}
|
|
|
1836 |
|
|
|
1837 |
if ($baseUrl && null !== $prefix = $this->getUrlencodedPrefix($requestUri, $baseUrl)) {
|
|
|
1838 |
// full $baseUrl matches
|
|
|
1839 |
return $prefix;
|
|
|
1840 |
}
|
|
|
1841 |
|
|
|
1842 |
if ($baseUrl && null !== $prefix = $this->getUrlencodedPrefix($requestUri, rtrim(\dirname($baseUrl), '/'.\DIRECTORY_SEPARATOR).'/')) {
|
|
|
1843 |
// directory portion of $baseUrl matches
|
|
|
1844 |
return rtrim($prefix, '/'.\DIRECTORY_SEPARATOR);
|
|
|
1845 |
}
|
|
|
1846 |
|
|
|
1847 |
$truncatedRequestUri = $requestUri;
|
|
|
1848 |
if (false !== $pos = strpos($requestUri, '?')) {
|
|
|
1849 |
$truncatedRequestUri = substr($requestUri, 0, $pos);
|
|
|
1850 |
}
|
|
|
1851 |
|
|
|
1852 |
$basename = basename($baseUrl);
|
|
|
1853 |
if (empty($basename) || !strpos(rawurldecode($truncatedRequestUri), $basename)) {
|
|
|
1854 |
// no match whatsoever; set it blank
|
|
|
1855 |
return '';
|
|
|
1856 |
}
|
|
|
1857 |
|
|
|
1858 |
// If using mod_rewrite or ISAPI_Rewrite strip the script filename
|
|
|
1859 |
// out of baseUrl. $pos !== 0 makes sure it is not matching a value
|
|
|
1860 |
// from PATH_INFO or QUERY_STRING
|
|
|
1861 |
if (\strlen($requestUri) >= \strlen($baseUrl) && (false !== $pos = strpos($requestUri, $baseUrl)) && 0 !== $pos) {
|
|
|
1862 |
$baseUrl = substr($requestUri, 0, $pos + \strlen($baseUrl));
|
|
|
1863 |
}
|
|
|
1864 |
|
|
|
1865 |
return rtrim($baseUrl, '/'.\DIRECTORY_SEPARATOR);
|
|
|
1866 |
}
|
|
|
1867 |
|
|
|
1868 |
/**
|
|
|
1869 |
* Prepares the base path.
|
|
|
1870 |
*
|
|
|
1871 |
* @return string base path
|
|
|
1872 |
*/
|
|
|
1873 |
protected function prepareBasePath()
|
|
|
1874 |
{
|
|
|
1875 |
$baseUrl = $this->getBaseUrl();
|
|
|
1876 |
if (empty($baseUrl)) {
|
|
|
1877 |
return '';
|
|
|
1878 |
}
|
|
|
1879 |
|
|
|
1880 |
$filename = basename($this->server->get('SCRIPT_FILENAME'));
|
|
|
1881 |
if (basename($baseUrl) === $filename) {
|
|
|
1882 |
$basePath = \dirname($baseUrl);
|
|
|
1883 |
} else {
|
|
|
1884 |
$basePath = $baseUrl;
|
|
|
1885 |
}
|
|
|
1886 |
|
|
|
1887 |
if ('\\' === \DIRECTORY_SEPARATOR) {
|
|
|
1888 |
$basePath = str_replace('\\', '/', $basePath);
|
|
|
1889 |
}
|
|
|
1890 |
|
|
|
1891 |
return rtrim($basePath, '/');
|
|
|
1892 |
}
|
|
|
1893 |
|
|
|
1894 |
/**
|
|
|
1895 |
* Prepares the path info.
|
|
|
1896 |
*
|
|
|
1897 |
* @return string path info
|
|
|
1898 |
*/
|
|
|
1899 |
protected function preparePathInfo()
|
|
|
1900 |
{
|
|
|
1901 |
if (null === ($requestUri = $this->getRequestUri())) {
|
|
|
1902 |
return '/';
|
|
|
1903 |
}
|
|
|
1904 |
|
|
|
1905 |
// Remove the query string from REQUEST_URI
|
|
|
1906 |
if (false !== $pos = strpos($requestUri, '?')) {
|
|
|
1907 |
$requestUri = substr($requestUri, 0, $pos);
|
|
|
1908 |
}
|
|
|
1909 |
if ('' !== $requestUri && '/' !== $requestUri[0]) {
|
|
|
1910 |
$requestUri = '/'.$requestUri;
|
|
|
1911 |
}
|
|
|
1912 |
|
|
|
1913 |
if (null === ($baseUrl = $this->getBaseUrl())) {
|
|
|
1914 |
return $requestUri;
|
|
|
1915 |
}
|
|
|
1916 |
|
|
|
1917 |
$pathInfo = substr($requestUri, \strlen($baseUrl));
|
|
|
1918 |
if (false === $pathInfo || '' === $pathInfo) {
|
|
|
1919 |
// If substr() returns false then PATH_INFO is set to an empty string
|
|
|
1920 |
return '/';
|
|
|
1921 |
}
|
|
|
1922 |
|
|
|
1923 |
return (string) $pathInfo;
|
|
|
1924 |
}
|
|
|
1925 |
|
|
|
1926 |
/**
|
|
|
1927 |
* Initializes HTTP request formats.
|
|
|
1928 |
*/
|
|
|
1929 |
protected static function initializeFormats()
|
|
|
1930 |
{
|
|
|
1931 |
static::$formats = [
|
|
|
1932 |
'html' => ['text/html', 'application/xhtml+xml'],
|
|
|
1933 |
'txt' => ['text/plain'],
|
|
|
1934 |
'js' => ['application/javascript', 'application/x-javascript', 'text/javascript'],
|
|
|
1935 |
'css' => ['text/css'],
|
|
|
1936 |
'json' => ['application/json', 'application/x-json'],
|
|
|
1937 |
'jsonld' => ['application/ld+json'],
|
|
|
1938 |
'xml' => ['text/xml', 'application/xml', 'application/x-xml'],
|
|
|
1939 |
'rdf' => ['application/rdf+xml'],
|
|
|
1940 |
'atom' => ['application/atom+xml'],
|
|
|
1941 |
'rss' => ['application/rss+xml'],
|
|
|
1942 |
'form' => ['application/x-www-form-urlencoded'],
|
|
|
1943 |
];
|
|
|
1944 |
}
|
|
|
1945 |
|
|
|
1946 |
private function setPhpDefaultLocale(string $locale): void
|
|
|
1947 |
{
|
|
|
1948 |
// if either the class Locale doesn't exist, or an exception is thrown when
|
|
|
1949 |
// setting the default locale, the intl module is not installed, and
|
|
|
1950 |
// the call can be ignored:
|
|
|
1951 |
try {
|
|
|
1952 |
if (class_exists('Locale', false)) {
|
|
|
1953 |
\Locale::setDefault($locale);
|
|
|
1954 |
}
|
|
|
1955 |
} catch (\Exception $e) {
|
|
|
1956 |
}
|
|
|
1957 |
}
|
|
|
1958 |
|
|
|
1959 |
/**
|
|
|
1960 |
* Returns the prefix as encoded in the string when the string starts with
|
|
|
1961 |
* the given prefix, null otherwise.
|
|
|
1962 |
*/
|
|
|
1963 |
private function getUrlencodedPrefix(string $string, string $prefix): ?string
|
|
|
1964 |
{
|
|
|
1965 |
if (0 !== strpos(rawurldecode($string), $prefix)) {
|
|
|
1966 |
return null;
|
|
|
1967 |
}
|
|
|
1968 |
|
|
|
1969 |
$len = \strlen($prefix);
|
|
|
1970 |
|
|
|
1971 |
if (preg_match(sprintf('#^(%%[[:xdigit:]]{2}|.){%d}#', $len), $string, $match)) {
|
|
|
1972 |
return $match[0];
|
|
|
1973 |
}
|
|
|
1974 |
|
|
|
1975 |
return null;
|
|
|
1976 |
}
|
|
|
1977 |
|
|
|
1978 |
private static function createRequestFromFactory(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null): self
|
|
|
1979 |
{
|
|
|
1980 |
if (self::$requestFactory) {
|
|
|
1981 |
$request = (self::$requestFactory)($query, $request, $attributes, $cookies, $files, $server, $content);
|
|
|
1982 |
|
|
|
1983 |
if (!$request instanceof self) {
|
|
|
1984 |
throw new \LogicException('The Request factory must return an instance of Symfony\Component\HttpFoundation\Request.');
|
|
|
1985 |
}
|
|
|
1986 |
|
|
|
1987 |
return $request;
|
|
|
1988 |
}
|
|
|
1989 |
|
|
|
1990 |
return new static($query, $request, $attributes, $cookies, $files, $server, $content);
|
|
|
1991 |
}
|
|
|
1992 |
|
|
|
1993 |
/**
|
|
|
1994 |
* Indicates whether this request originated from a trusted proxy.
|
|
|
1995 |
*
|
|
|
1996 |
* This can be useful to determine whether or not to trust the
|
|
|
1997 |
* contents of a proxy-specific header.
|
|
|
1998 |
*
|
|
|
1999 |
* @return bool true if the request came from a trusted proxy, false otherwise
|
|
|
2000 |
*/
|
|
|
2001 |
public function isFromTrustedProxy()
|
|
|
2002 |
{
|
|
|
2003 |
return self::$trustedProxies && IpUtils::checkIp($this->server->get('REMOTE_ADDR'), self::$trustedProxies);
|
|
|
2004 |
}
|
|
|
2005 |
|
|
|
2006 |
private function getTrustedValues(int $type, string $ip = null): array
|
|
|
2007 |
{
|
|
|
2008 |
$clientValues = [];
|
|
|
2009 |
$forwardedValues = [];
|
|
|
2010 |
|
|
|
2011 |
if ((self::$trustedHeaderSet & $type) && $this->headers->has(self::$trustedHeaders[$type])) {
|
|
|
2012 |
foreach (explode(',', $this->headers->get(self::$trustedHeaders[$type])) as $v) {
|
|
|
2013 |
$clientValues[] = (self::HEADER_X_FORWARDED_PORT === $type ? '0.0.0.0:' : '').trim($v);
|
|
|
2014 |
}
|
|
|
2015 |
}
|
|
|
2016 |
|
|
|
2017 |
if ((self::$trustedHeaderSet & self::HEADER_FORWARDED) && $this->headers->has(self::$trustedHeaders[self::HEADER_FORWARDED])) {
|
|
|
2018 |
$forwarded = $this->headers->get(self::$trustedHeaders[self::HEADER_FORWARDED]);
|
|
|
2019 |
$parts = HeaderUtils::split($forwarded, ',;=');
|
|
|
2020 |
$forwardedValues = [];
|
|
|
2021 |
$param = self::$forwardedParams[$type];
|
|
|
2022 |
foreach ($parts as $subParts) {
|
|
|
2023 |
if (null === $v = HeaderUtils::combine($subParts)[$param] ?? null) {
|
|
|
2024 |
continue;
|
|
|
2025 |
}
|
|
|
2026 |
if (self::HEADER_X_FORWARDED_PORT === $type) {
|
|
|
2027 |
if (']' === substr($v, -1) || false === $v = strrchr($v, ':')) {
|
|
|
2028 |
$v = $this->isSecure() ? ':443' : ':80';
|
|
|
2029 |
}
|
|
|
2030 |
$v = '0.0.0.0'.$v;
|
|
|
2031 |
}
|
|
|
2032 |
$forwardedValues[] = $v;
|
|
|
2033 |
}
|
|
|
2034 |
}
|
|
|
2035 |
|
|
|
2036 |
if (null !== $ip) {
|
|
|
2037 |
$clientValues = $this->normalizeAndFilterClientIps($clientValues, $ip);
|
|
|
2038 |
$forwardedValues = $this->normalizeAndFilterClientIps($forwardedValues, $ip);
|
|
|
2039 |
}
|
|
|
2040 |
|
|
|
2041 |
if ($forwardedValues === $clientValues || !$clientValues) {
|
|
|
2042 |
return $forwardedValues;
|
|
|
2043 |
}
|
|
|
2044 |
|
|
|
2045 |
if (!$forwardedValues) {
|
|
|
2046 |
return $clientValues;
|
|
|
2047 |
}
|
|
|
2048 |
|
|
|
2049 |
if (!$this->isForwardedValid) {
|
|
|
2050 |
return null !== $ip ? ['0.0.0.0', $ip] : [];
|
|
|
2051 |
}
|
|
|
2052 |
$this->isForwardedValid = false;
|
|
|
2053 |
|
|
|
2054 |
throw new ConflictingHeadersException(sprintf('The request has both a trusted "%s" header and a trusted "%s" header, conflicting with each other. You should either configure your proxy to remove one of them, or configure your project to distrust the offending one.', self::$trustedHeaders[self::HEADER_FORWARDED], self::$trustedHeaders[$type]));
|
|
|
2055 |
}
|
|
|
2056 |
|
|
|
2057 |
private function normalizeAndFilterClientIps(array $clientIps, string $ip): array
|
|
|
2058 |
{
|
|
|
2059 |
if (!$clientIps) {
|
|
|
2060 |
return [];
|
|
|
2061 |
}
|
|
|
2062 |
$clientIps[] = $ip; // Complete the IP chain with the IP the request actually came from
|
|
|
2063 |
$firstTrustedIp = null;
|
|
|
2064 |
|
|
|
2065 |
foreach ($clientIps as $key => $clientIp) {
|
|
|
2066 |
if (strpos($clientIp, '.')) {
|
|
|
2067 |
// Strip :port from IPv4 addresses. This is allowed in Forwarded
|
|
|
2068 |
// and may occur in X-Forwarded-For.
|
|
|
2069 |
$i = strpos($clientIp, ':');
|
|
|
2070 |
if ($i) {
|
|
|
2071 |
$clientIps[$key] = $clientIp = substr($clientIp, 0, $i);
|
|
|
2072 |
}
|
|
|
2073 |
} elseif (0 === strpos($clientIp, '[')) {
|
|
|
2074 |
// Strip brackets and :port from IPv6 addresses.
|
|
|
2075 |
$i = strpos($clientIp, ']', 1);
|
|
|
2076 |
$clientIps[$key] = $clientIp = substr($clientIp, 1, $i - 1);
|
|
|
2077 |
}
|
|
|
2078 |
|
|
|
2079 |
if (!filter_var($clientIp, \FILTER_VALIDATE_IP)) {
|
|
|
2080 |
unset($clientIps[$key]);
|
|
|
2081 |
|
|
|
2082 |
continue;
|
|
|
2083 |
}
|
|
|
2084 |
|
|
|
2085 |
if (IpUtils::checkIp($clientIp, self::$trustedProxies)) {
|
|
|
2086 |
unset($clientIps[$key]);
|
|
|
2087 |
|
|
|
2088 |
// Fallback to this when the client IP falls into the range of trusted proxies
|
|
|
2089 |
if (null === $firstTrustedIp) {
|
|
|
2090 |
$firstTrustedIp = $clientIp;
|
|
|
2091 |
}
|
|
|
2092 |
}
|
|
|
2093 |
}
|
|
|
2094 |
|
|
|
2095 |
// Now the IP chain contains only untrusted proxies and the client IP
|
|
|
2096 |
return $clientIps ? array_reverse($clientIps) : [$firstTrustedIp];
|
|
|
2097 |
}
|
|
|
2098 |
}
|