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