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