1 | <?php |
||||
2 | /** |
||||
3 | * @link http://www.yiiframework.com/ |
||||
4 | * @copyright Copyright (c) 2008 Yii Software LLC |
||||
5 | * @license http://www.yiiframework.com/license/ |
||||
6 | */ |
||||
7 | |||||
8 | namespace yii\web; |
||||
9 | |||||
10 | use Yii; |
||||
11 | use yii\base\InvalidConfigException; |
||||
12 | use yii\validators\IpValidator; |
||||
13 | |||||
14 | /** |
||||
15 | * The web Request class represents an HTTP request. |
||||
16 | * |
||||
17 | * It encapsulates the $_SERVER variable and resolves its inconsistency among different Web servers. |
||||
18 | * Also it provides an interface to retrieve request parameters from $_POST, $_GET, $_COOKIES and REST |
||||
19 | * parameters sent via other HTTP methods like PUT or DELETE. |
||||
20 | * |
||||
21 | * Request is configured as an application component in [[\yii\web\Application]] by default. |
||||
22 | * You can access that instance via `Yii::$app->request`. |
||||
23 | * |
||||
24 | * For more details and usage information on Request, see the [guide article on requests](guide:runtime-requests). |
||||
25 | * |
||||
26 | * @property-read string $absoluteUrl The currently requested absolute URL. This property is read-only. |
||||
27 | * @property array $acceptableContentTypes The content types ordered by the quality score. Types with the |
||||
28 | * highest scores will be returned first. The array keys are the content types, while the array values are the |
||||
29 | * corresponding quality score and other parameters as given in the header. |
||||
30 | * @property array $acceptableLanguages The languages ordered by the preference level. The first element |
||||
31 | * represents the most preferred language. |
||||
32 | * @property-read array $authCredentials That contains exactly two elements: - 0: the username sent via HTTP |
||||
33 | * authentication, `null` if the username is not given - 1: the password sent via HTTP authentication, `null` if |
||||
34 | * the password is not given. This property is read-only. |
||||
35 | * @property-read string|null $authPassword The password sent via HTTP authentication, `null` if the password |
||||
36 | * is not given. This property is read-only. |
||||
37 | * @property-read string|null $authUser The username sent via HTTP authentication, `null` if the username is |
||||
38 | * not given. This property is read-only. |
||||
39 | * @property string $baseUrl The relative URL for the application. |
||||
40 | * @property array $bodyParams The request parameters given in the request body. |
||||
41 | * @property-read string $contentType Request content-type. Null is returned if this information is not |
||||
42 | * available. This property is read-only. |
||||
43 | * @property-read CookieCollection $cookies The cookie collection. This property is read-only. |
||||
44 | * @property-read string $csrfToken The token used to perform CSRF validation. This property is read-only. |
||||
45 | * @property-read string $csrfTokenFromHeader The CSRF token sent via [[CSRF_HEADER]] by browser. Null is |
||||
46 | * returned if no such header is sent. This property is read-only. |
||||
47 | * @property-read array $eTags The entity tags. This property is read-only. |
||||
48 | * @property-read HeaderCollection $headers The header collection. This property is read-only. |
||||
49 | * @property string|null $hostInfo Schema and hostname part (with port number if needed) of the request URL |
||||
50 | * (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set. See |
||||
51 | * [[getHostInfo()]] for security related notes on this property. |
||||
52 | * @property-read string|null $hostName Hostname part of the request URL (e.g. `www.yiiframework.com`). This |
||||
53 | * property is read-only. |
||||
54 | * @property-read bool $isAjax Whether this is an AJAX (XMLHttpRequest) request. This property is read-only. |
||||
55 | * @property-read bool $isDelete Whether this is a DELETE request. This property is read-only. |
||||
56 | * @property-read bool $isFlash Whether this is an Adobe Flash or Adobe Flex request. This property is |
||||
57 | * read-only. |
||||
58 | * @property-read bool $isGet Whether this is a GET request. This property is read-only. |
||||
59 | * @property-read bool $isHead Whether this is a HEAD request. This property is read-only. |
||||
60 | * @property-read bool $isOptions Whether this is a OPTIONS request. This property is read-only. |
||||
61 | * @property-read bool $isPatch Whether this is a PATCH request. This property is read-only. |
||||
62 | * @property-read bool $isPjax Whether this is a PJAX request. This property is read-only. |
||||
63 | * @property-read bool $isPost Whether this is a POST request. This property is read-only. |
||||
64 | * @property-read bool $isPut Whether this is a PUT request. This property is read-only. |
||||
65 | * @property-read bool $isSecureConnection If the request is sent via secure channel (https). This property is |
||||
66 | * read-only. |
||||
67 | * @property-read string $method Request method, such as GET, POST, HEAD, PUT, PATCH, DELETE. The value |
||||
68 | * returned is turned into upper case. This property is read-only. |
||||
69 | * @property-read string|null $origin URL origin of a CORS request, `null` if not available. This property is |
||||
70 | * read-only. |
||||
71 | * @property string $pathInfo Part of the request URL that is after the entry script and before the question |
||||
72 | * mark. Note, the returned path info is already URL-decoded. |
||||
73 | * @property int $port Port number for insecure requests. |
||||
74 | * @property array $queryParams The request GET parameter values. |
||||
75 | * @property-read string $queryString Part of the request URL that is after the question mark. This property |
||||
76 | * is read-only. |
||||
77 | * @property string $rawBody The request body. |
||||
78 | * @property-read string|null $referrer URL referrer, null if not available. This property is read-only. |
||||
79 | * @property-read string|null $remoteHost Remote host name, `null` if not available. This property is |
||||
80 | * read-only. |
||||
81 | * @property-read string|null $remoteIP Remote IP address, `null` if not available. This property is |
||||
82 | * read-only. |
||||
83 | * @property string $scriptFile The entry script file path. |
||||
84 | * @property string $scriptUrl The relative URL of the entry script. |
||||
85 | * @property int $securePort Port number for secure requests. |
||||
86 | * @property-read string $serverName Server name, null if not available. This property is read-only. |
||||
87 | * @property-read int|null $serverPort Server port number, null if not available. This property is read-only. |
||||
88 | * @property string $url The currently requested relative URL. Note that the URI returned may be URL-encoded |
||||
89 | * depending on the client. |
||||
90 | * @property-read string|null $userAgent User agent, null if not available. This property is read-only. |
||||
91 | * @property-read string|null $userHost User host name, null if not available. This property is read-only. |
||||
92 | * @property-read string|null $userIP User IP address, null if not available. This property is read-only. |
||||
93 | * |
||||
94 | * @author Qiang Xue <[email protected]> |
||||
95 | * @since 2.0 |
||||
96 | * @SuppressWarnings(PHPMD.SuperGlobals) |
||||
97 | */ |
||||
98 | class Request extends \yii\base\Request |
||||
99 | { |
||||
100 | /** |
||||
101 | * The name of the HTTP header for sending CSRF token. |
||||
102 | */ |
||||
103 | const CSRF_HEADER = 'X-CSRF-Token'; |
||||
104 | /** |
||||
105 | * The length of the CSRF token mask. |
||||
106 | * @deprecated since 2.0.12. The mask length is now equal to the token length. |
||||
107 | */ |
||||
108 | const CSRF_MASK_LENGTH = 8; |
||||
109 | |||||
110 | /** |
||||
111 | * @var bool whether to enable CSRF (Cross-Site Request Forgery) validation. Defaults to true. |
||||
112 | * When CSRF validation is enabled, forms submitted to an Yii Web application must be originated |
||||
113 | * from the same application. If not, a 400 HTTP exception will be raised. |
||||
114 | * |
||||
115 | * Note, this feature requires that the user client accepts cookie. Also, to use this feature, |
||||
116 | * forms submitted via POST method must contain a hidden input whose name is specified by [[csrfParam]]. |
||||
117 | * You may use [[\yii\helpers\Html::beginForm()]] to generate his hidden input. |
||||
118 | * |
||||
119 | * In JavaScript, you may get the values of [[csrfParam]] and [[csrfToken]] via `yii.getCsrfParam()` and |
||||
120 | * `yii.getCsrfToken()`, respectively. The [[\yii\web\YiiAsset]] asset must be registered. |
||||
121 | * You also need to include CSRF meta tags in your pages by using [[\yii\helpers\Html::csrfMetaTags()]]. |
||||
122 | * |
||||
123 | * @see Controller::enableCsrfValidation |
||||
124 | * @see http://en.wikipedia.org/wiki/Cross-site_request_forgery |
||||
125 | */ |
||||
126 | public $enableCsrfValidation = true; |
||||
127 | /** |
||||
128 | * @var string the name of the token used to prevent CSRF. Defaults to '_csrf'. |
||||
129 | * This property is used only when [[enableCsrfValidation]] is true. |
||||
130 | */ |
||||
131 | public $csrfParam = '_csrf'; |
||||
132 | /** |
||||
133 | * @var array the configuration for creating the CSRF [[Cookie|cookie]]. This property is used only when |
||||
134 | * both [[enableCsrfValidation]] and [[enableCsrfCookie]] are true. |
||||
135 | */ |
||||
136 | public $csrfCookie = ['httpOnly' => true]; |
||||
137 | /** |
||||
138 | * @var bool whether to use cookie to persist CSRF token. If false, CSRF token will be stored |
||||
139 | * in session under the name of [[csrfParam]]. Note that while storing CSRF tokens in session increases |
||||
140 | * security, it requires starting a session for every page, which will degrade your site performance. |
||||
141 | */ |
||||
142 | public $enableCsrfCookie = true; |
||||
143 | /** |
||||
144 | * @var bool whether cookies should be validated to ensure they are not tampered. Defaults to true. |
||||
145 | */ |
||||
146 | public $enableCookieValidation = true; |
||||
147 | /** |
||||
148 | * @var string a secret key used for cookie validation. This property must be set if [[enableCookieValidation]] is true. |
||||
149 | */ |
||||
150 | public $cookieValidationKey; |
||||
151 | /** |
||||
152 | * @var string the name of the POST parameter that is used to indicate if a request is a PUT, PATCH or DELETE |
||||
153 | * request tunneled through POST. Defaults to '_method'. |
||||
154 | * @see getMethod() |
||||
155 | * @see getBodyParams() |
||||
156 | */ |
||||
157 | public $methodParam = '_method'; |
||||
158 | /** |
||||
159 | * @var array the parsers for converting the raw HTTP request body into [[bodyParams]]. |
||||
160 | * The array keys are the request `Content-Types`, and the array values are the |
||||
161 | * corresponding configurations for [[Yii::createObject|creating the parser objects]]. |
||||
162 | * A parser must implement the [[RequestParserInterface]]. |
||||
163 | * |
||||
164 | * To enable parsing for JSON requests you can use the [[JsonParser]] class like in the following example: |
||||
165 | * |
||||
166 | * ``` |
||||
167 | * [ |
||||
168 | * 'application/json' => 'yii\web\JsonParser', |
||||
169 | * ] |
||||
170 | * ``` |
||||
171 | * |
||||
172 | * To register a parser for parsing all request types you can use `'*'` as the array key. |
||||
173 | * This one will be used as a fallback in case no other types match. |
||||
174 | * |
||||
175 | * @see getBodyParams() |
||||
176 | */ |
||||
177 | public $parsers = []; |
||||
178 | /** |
||||
179 | * @var array the configuration for trusted security related headers. |
||||
180 | * |
||||
181 | * An array key is an IPv4 or IPv6 IP address in CIDR notation for matching a client. |
||||
182 | * |
||||
183 | * An array value is a list of headers to trust. These will be matched against |
||||
184 | * [[secureHeaders]] to determine which headers are allowed to be sent by a specified host. |
||||
185 | * The case of the header names must be the same as specified in [[secureHeaders]]. |
||||
186 | * |
||||
187 | * For example, to trust all headers listed in [[secureHeaders]] for IP addresses |
||||
188 | * in range `192.168.0.0-192.168.0.254` write the following: |
||||
189 | * |
||||
190 | * ```php |
||||
191 | * [ |
||||
192 | * '192.168.0.0/24', |
||||
193 | * ] |
||||
194 | * ``` |
||||
195 | * |
||||
196 | * To trust just the `X-Forwarded-For` header from `10.0.0.1`, use: |
||||
197 | * |
||||
198 | * ``` |
||||
199 | * [ |
||||
200 | * '10.0.0.1' => ['X-Forwarded-For'] |
||||
201 | * ] |
||||
202 | * ``` |
||||
203 | * |
||||
204 | * Default is to trust all headers except those listed in [[secureHeaders]] from all hosts. |
||||
205 | * Matches are tried in order and searching is stopped when IP matches. |
||||
206 | * |
||||
207 | * > Info: Matching is performed using [[IpValidator]]. |
||||
208 | * See [[IpValidator::::setRanges()|IpValidator::setRanges()]] |
||||
209 | * and [[IpValidator::networks]] for advanced matching. |
||||
210 | * |
||||
211 | * @see $secureHeaders |
||||
212 | * @since 2.0.13 |
||||
213 | */ |
||||
214 | public $trustedHosts = []; |
||||
215 | /** |
||||
216 | * @var array lists of headers that are, by default, subject to the trusted host configuration. |
||||
217 | * These headers will be filtered unless explicitly allowed in [[trustedHosts]]. |
||||
218 | * If the list contains the `Forwarded` header, processing will be done according to RFC 7239. |
||||
219 | * The match of header names is case-insensitive. |
||||
220 | * @see https://en.wikipedia.org/wiki/List_of_HTTP_header_fields |
||||
221 | * @see https://tools.ietf.org/html/rfc7239 |
||||
222 | * @see $trustedHosts |
||||
223 | * @since 2.0.13 |
||||
224 | */ |
||||
225 | public $secureHeaders = [ |
||||
226 | // Common: |
||||
227 | 'X-Forwarded-For', |
||||
228 | 'X-Forwarded-Host', |
||||
229 | 'X-Forwarded-Proto', |
||||
230 | |||||
231 | // Microsoft: |
||||
232 | 'Front-End-Https', |
||||
233 | 'X-Rewrite-Url', |
||||
234 | |||||
235 | // ngrok: |
||||
236 | 'X-Original-Host', |
||||
237 | ]; |
||||
238 | /** |
||||
239 | * @var string[] List of headers where proxies store the real client IP. |
||||
240 | * It's not advisable to put insecure headers here. |
||||
241 | * To use the `Forwarded` header according to RFC 7239, the header must be added to [[secureHeaders]] list. |
||||
242 | * The match of header names is case-insensitive. |
||||
243 | * @see $trustedHosts |
||||
244 | * @see $secureHeaders |
||||
245 | * @since 2.0.13 |
||||
246 | */ |
||||
247 | public $ipHeaders = [ |
||||
248 | 'X-Forwarded-For', // Common |
||||
249 | ]; |
||||
250 | /** |
||||
251 | * @var array list of headers to check for determining whether the connection is made via HTTPS. |
||||
252 | * The array keys are header names and the array value is a list of header values that indicate a secure connection. |
||||
253 | * The match of header names and values is case-insensitive. |
||||
254 | * It's not advisable to put insecure headers here. |
||||
255 | * @see $trustedHosts |
||||
256 | * @see $secureHeaders |
||||
257 | * @since 2.0.13 |
||||
258 | */ |
||||
259 | public $secureProtocolHeaders = [ |
||||
260 | 'X-Forwarded-Proto' => ['https'], // Common |
||||
261 | 'Front-End-Https' => ['on'], // Microsoft |
||||
262 | ]; |
||||
263 | |||||
264 | /** |
||||
265 | * @var CookieCollection Collection of request cookies. |
||||
266 | */ |
||||
267 | private $_cookies; |
||||
268 | /** |
||||
269 | * @var HeaderCollection Collection of request headers. |
||||
270 | */ |
||||
271 | private $_headers; |
||||
272 | |||||
273 | |||||
274 | /** |
||||
275 | * Resolves the current request into a route and the associated parameters. |
||||
276 | * @return array the first element is the route, and the second is the associated parameters. |
||||
277 | * @throws NotFoundHttpException if the request cannot be resolved. |
||||
278 | */ |
||||
279 | 1 | public function resolve() |
|||
280 | { |
||||
281 | 1 | $result = Yii::$app->getUrlManager()->parseRequest($this); |
|||
282 | 1 | if ($result !== false) { |
|||
283 | 1 | list($route, $params) = $result; |
|||
284 | 1 | if ($this->_queryParams === null) { |
|||
285 | 1 | $_GET = $params + $_GET; // preserve numeric keys |
|||
286 | } else { |
||||
287 | 1 | $this->_queryParams = $params + $this->_queryParams; |
|||
288 | } |
||||
289 | |||||
290 | 1 | return [$route, $this->getQueryParams()]; |
|||
291 | } |
||||
292 | |||||
293 | throw new NotFoundHttpException(Yii::t('yii', 'Page not found.')); |
||||
294 | } |
||||
295 | |||||
296 | /** |
||||
297 | * Filters headers according to the [[trustedHosts]]. |
||||
298 | * @param HeaderCollection $headerCollection |
||||
299 | * @since 2.0.13 |
||||
300 | */ |
||||
301 | 243 | protected function filterHeaders(HeaderCollection $headerCollection) |
|||
302 | { |
||||
303 | 243 | $trustedHeaders = $this->getTrustedHeaders(); |
|||
304 | |||||
305 | // remove all secure headers unless they are trusted |
||||
306 | 243 | foreach ($this->secureHeaders as $secureHeader) { |
|||
307 | 243 | if (!in_array($secureHeader, $trustedHeaders)) { |
|||
308 | 243 | $headerCollection->remove($secureHeader); |
|||
309 | } |
||||
310 | } |
||||
311 | 243 | } |
|||
312 | |||||
313 | /** |
||||
314 | * Trusted headers according to the [[trustedHosts]]. |
||||
315 | * @return array |
||||
316 | * @since 2.0.28 |
||||
317 | */ |
||||
318 | 243 | protected function getTrustedHeaders() |
|||
319 | { |
||||
320 | // do not trust any of the [[secureHeaders]] by default |
||||
321 | 243 | $trustedHeaders = []; |
|||
322 | |||||
323 | // check if the client is a trusted host |
||||
324 | 243 | if (!empty($this->trustedHosts)) { |
|||
325 | 73 | $validator = $this->getIpValidator(); |
|||
326 | 73 | $ip = $this->getRemoteIP(); |
|||
327 | 73 | foreach ($this->trustedHosts as $cidr => $headers) { |
|||
328 | 73 | if (!is_array($headers)) { |
|||
329 | 71 | $cidr = $headers; |
|||
330 | 71 | $headers = $this->secureHeaders; |
|||
331 | } |
||||
332 | 73 | $validator->setRanges($cidr); |
|||
333 | 73 | if ($validator->validate($ip)) { |
|||
334 | 38 | $trustedHeaders = $headers; |
|||
335 | 73 | break; |
|||
336 | } |
||||
337 | } |
||||
338 | } |
||||
339 | 243 | return $trustedHeaders; |
|||
340 | } |
||||
341 | |||||
342 | /** |
||||
343 | * Creates instance of [[IpValidator]]. |
||||
344 | * You can override this method to adjust validator or implement different matching strategy. |
||||
345 | * |
||||
346 | * @return IpValidator |
||||
347 | * @since 2.0.13 |
||||
348 | */ |
||||
349 | 163 | protected function getIpValidator() |
|||
350 | { |
||||
351 | 163 | return new IpValidator(); |
|||
352 | } |
||||
353 | |||||
354 | /** |
||||
355 | * Returns the header collection. |
||||
356 | * The header collection contains incoming HTTP headers. |
||||
357 | * @return HeaderCollection the header collection |
||||
358 | */ |
||||
359 | 243 | public function getHeaders() |
|||
360 | { |
||||
361 | 243 | if ($this->_headers === null) { |
|||
362 | 243 | $this->_headers = new HeaderCollection(); |
|||
363 | 243 | if (function_exists('getallheaders')) { |
|||
364 | $headers = getallheaders(); |
||||
365 | foreach ($headers as $name => $value) { |
||||
366 | $this->_headers->add($name, $value); |
||||
367 | } |
||||
368 | 243 | } elseif (function_exists('http_get_request_headers')) { |
|||
369 | $headers = http_get_request_headers(); |
||||
370 | foreach ($headers as $name => $value) { |
||||
371 | $this->_headers->add($name, $value); |
||||
372 | } |
||||
373 | } else { |
||||
374 | 243 | foreach ($_SERVER as $name => $value) { |
|||
375 | 239 | if (strncmp($name, 'HTTP_', 5) === 0) { |
|||
376 | 91 | $name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))); |
|||
377 | 239 | $this->_headers->add($name, $value); |
|||
378 | } |
||||
379 | } |
||||
380 | } |
||||
381 | 243 | $this->filterHeaders($this->_headers); |
|||
382 | } |
||||
383 | |||||
384 | 243 | return $this->_headers; |
|||
385 | } |
||||
386 | |||||
387 | /** |
||||
388 | * Returns the method of the current request (e.g. GET, POST, HEAD, PUT, PATCH, DELETE). |
||||
389 | * @return string request method, such as GET, POST, HEAD, PUT, PATCH, DELETE. |
||||
390 | * The value returned is turned into upper case. |
||||
391 | */ |
||||
392 | 42 | public function getMethod() |
|||
393 | { |
||||
394 | if ( |
||||
395 | 42 | isset($_POST[$this->methodParam]) |
|||
396 | // Never allow to downgrade request from WRITE methods (POST, PATCH, DELETE, etc) |
||||
397 | // to read methods (GET, HEAD, OPTIONS) for security reasons. |
||||
398 | 42 | && !in_array(strtoupper($_POST[$this->methodParam]), ['GET', 'HEAD', 'OPTIONS'], true) |
|||
399 | ) { |
||||
400 | 6 | return strtoupper($_POST[$this->methodParam]); |
|||
401 | } |
||||
402 | |||||
403 | 40 | if ($this->headers->has('X-Http-Method-Override')) { |
|||
404 | 1 | return strtoupper($this->headers->get('X-Http-Method-Override')); |
|||
405 | } |
||||
406 | |||||
407 | 39 | if (isset($_SERVER['REQUEST_METHOD'])) { |
|||
408 | 10 | return strtoupper($_SERVER['REQUEST_METHOD']); |
|||
409 | } |
||||
410 | |||||
411 | 30 | return 'GET'; |
|||
412 | } |
||||
413 | |||||
414 | /** |
||||
415 | * Returns whether this is a GET request. |
||||
416 | * @return bool whether this is a GET request. |
||||
417 | */ |
||||
418 | 2 | public function getIsGet() |
|||
419 | { |
||||
420 | 2 | return $this->getMethod() === 'GET'; |
|||
421 | } |
||||
422 | |||||
423 | /** |
||||
424 | * Returns whether this is an OPTIONS request. |
||||
425 | * @return bool whether this is a OPTIONS request. |
||||
426 | */ |
||||
427 | 3 | public function getIsOptions() |
|||
428 | { |
||||
429 | 3 | return $this->getMethod() === 'OPTIONS'; |
|||
430 | } |
||||
431 | |||||
432 | /** |
||||
433 | * Returns whether this is a HEAD request. |
||||
434 | * @return bool whether this is a HEAD request. |
||||
435 | */ |
||||
436 | 15 | public function getIsHead() |
|||
437 | { |
||||
438 | 15 | return $this->getMethod() === 'HEAD'; |
|||
439 | } |
||||
440 | |||||
441 | /** |
||||
442 | * Returns whether this is a POST request. |
||||
443 | * @return bool whether this is a POST request. |
||||
444 | */ |
||||
445 | public function getIsPost() |
||||
446 | { |
||||
447 | return $this->getMethod() === 'POST'; |
||||
448 | } |
||||
449 | |||||
450 | /** |
||||
451 | * Returns whether this is a DELETE request. |
||||
452 | * @return bool whether this is a DELETE request. |
||||
453 | */ |
||||
454 | public function getIsDelete() |
||||
455 | { |
||||
456 | return $this->getMethod() === 'DELETE'; |
||||
457 | } |
||||
458 | |||||
459 | /** |
||||
460 | * Returns whether this is a PUT request. |
||||
461 | * @return bool whether this is a PUT request. |
||||
462 | */ |
||||
463 | public function getIsPut() |
||||
464 | { |
||||
465 | return $this->getMethod() === 'PUT'; |
||||
466 | } |
||||
467 | |||||
468 | /** |
||||
469 | * Returns whether this is a PATCH request. |
||||
470 | * @return bool whether this is a PATCH request. |
||||
471 | */ |
||||
472 | public function getIsPatch() |
||||
473 | { |
||||
474 | return $this->getMethod() === 'PATCH'; |
||||
475 | } |
||||
476 | |||||
477 | /** |
||||
478 | * Returns whether this is an AJAX (XMLHttpRequest) request. |
||||
479 | * |
||||
480 | * Note that in case of cross domain requests, browser doesn't set the X-Requested-With header by default: |
||||
481 | * https://stackoverflow.com/questions/8163703/cross-domain-ajax-doesnt-send-x-requested-with-header |
||||
482 | * |
||||
483 | * In case you are using `fetch()`, pass header manually: |
||||
484 | * |
||||
485 | * ``` |
||||
486 | * fetch(url, { |
||||
487 | * method: 'GET', |
||||
488 | * headers: {'X-Requested-With': 'XMLHttpRequest'} |
||||
489 | * }) |
||||
490 | * ``` |
||||
491 | * |
||||
492 | * @return bool whether this is an AJAX (XMLHttpRequest) request. |
||||
493 | */ |
||||
494 | 16 | public function getIsAjax() |
|||
495 | { |
||||
496 | 16 | return $this->headers->get('X-Requested-With') === 'XMLHttpRequest'; |
|||
497 | } |
||||
498 | |||||
499 | /** |
||||
500 | * Returns whether this is a PJAX request. |
||||
501 | * @return bool whether this is a PJAX request |
||||
502 | */ |
||||
503 | 3 | public function getIsPjax() |
|||
504 | { |
||||
505 | 3 | return $this->getIsAjax() && $this->headers->has('X-Pjax'); |
|||
506 | } |
||||
507 | |||||
508 | /** |
||||
509 | * Returns whether this is an Adobe Flash or Flex request. |
||||
510 | * @return bool whether this is an Adobe Flash or Adobe Flex request. |
||||
511 | */ |
||||
512 | public function getIsFlash() |
||||
513 | { |
||||
514 | $userAgent = $this->headers->get('User-Agent', ''); |
||||
515 | return stripos($userAgent, 'Shockwave') !== false |
||||
516 | || stripos($userAgent, 'Flash') !== false; |
||||
517 | } |
||||
518 | |||||
519 | private $_rawBody; |
||||
520 | |||||
521 | /** |
||||
522 | * Returns the raw HTTP request body. |
||||
523 | * @return string the request body |
||||
524 | */ |
||||
525 | 4 | public function getRawBody() |
|||
526 | { |
||||
527 | 4 | if ($this->_rawBody === null) { |
|||
528 | 4 | $this->_rawBody = file_get_contents('php://input'); |
|||
529 | } |
||||
530 | |||||
531 | 4 | return $this->_rawBody; |
|||
532 | } |
||||
533 | |||||
534 | /** |
||||
535 | * Sets the raw HTTP request body, this method is mainly used by test scripts to simulate raw HTTP requests. |
||||
536 | * @param string $rawBody the request body |
||||
537 | */ |
||||
538 | public function setRawBody($rawBody) |
||||
539 | { |
||||
540 | $this->_rawBody = $rawBody; |
||||
541 | } |
||||
542 | |||||
543 | private $_bodyParams; |
||||
544 | |||||
545 | /** |
||||
546 | * Returns the request parameters given in the request body. |
||||
547 | * |
||||
548 | * Request parameters are determined using the parsers configured in [[parsers]] property. |
||||
549 | * If no parsers are configured for the current [[contentType]] it uses the PHP function `mb_parse_str()` |
||||
550 | * to parse the [[rawBody|request body]]. |
||||
551 | * @return array the request parameters given in the request body. |
||||
552 | * @throws \yii\base\InvalidConfigException if a registered parser does not implement the [[RequestParserInterface]]. |
||||
553 | * @see getMethod() |
||||
554 | * @see getBodyParam() |
||||
555 | * @see setBodyParams() |
||||
556 | */ |
||||
557 | 8 | public function getBodyParams() |
|||
558 | { |
||||
559 | 8 | if ($this->_bodyParams === null) { |
|||
560 | 5 | if (isset($_POST[$this->methodParam])) { |
|||
561 | 1 | $this->_bodyParams = $_POST; |
|||
562 | 1 | unset($this->_bodyParams[$this->methodParam]); |
|||
563 | 1 | return $this->_bodyParams; |
|||
564 | } |
||||
565 | |||||
566 | 4 | $rawContentType = $this->getContentType(); |
|||
567 | 4 | if (($pos = strpos($rawContentType, ';')) !== false) { |
|||
568 | // e.g. text/html; charset=UTF-8 |
||||
569 | $contentType = substr($rawContentType, 0, $pos); |
||||
570 | } else { |
||||
571 | 4 | $contentType = $rawContentType; |
|||
572 | } |
||||
573 | |||||
574 | 4 | if (isset($this->parsers[$contentType])) { |
|||
575 | $parser = Yii::createObject($this->parsers[$contentType]); |
||||
576 | if (!($parser instanceof RequestParserInterface)) { |
||||
577 | throw new InvalidConfigException("The '$contentType' request parser is invalid. It must implement the yii\\web\\RequestParserInterface."); |
||||
578 | } |
||||
579 | $this->_bodyParams = $parser->parse($this->getRawBody(), $rawContentType); |
||||
580 | 4 | } elseif (isset($this->parsers['*'])) { |
|||
581 | $parser = Yii::createObject($this->parsers['*']); |
||||
582 | if (!($parser instanceof RequestParserInterface)) { |
||||
583 | throw new InvalidConfigException('The fallback request parser is invalid. It must implement the yii\\web\\RequestParserInterface.'); |
||||
584 | } |
||||
585 | $this->_bodyParams = $parser->parse($this->getRawBody(), $rawContentType); |
||||
586 | 4 | } elseif ($this->getMethod() === 'POST') { |
|||
587 | // PHP has already parsed the body so we have all params in $_POST |
||||
588 | $this->_bodyParams = $_POST; |
||||
589 | } else { |
||||
590 | 4 | $this->_bodyParams = []; |
|||
591 | 4 | mb_parse_str($this->getRawBody(), $this->_bodyParams); |
|||
592 | } |
||||
593 | } |
||||
594 | |||||
595 | 8 | return $this->_bodyParams; |
|||
596 | } |
||||
597 | |||||
598 | /** |
||||
599 | * Sets the request body parameters. |
||||
600 | * @param array $values the request body parameters (name-value pairs) |
||||
601 | * @see getBodyParam() |
||||
602 | * @see getBodyParams() |
||||
603 | */ |
||||
604 | 6 | public function setBodyParams($values) |
|||
605 | { |
||||
606 | 6 | $this->_bodyParams = $values; |
|||
607 | 6 | } |
|||
608 | |||||
609 | /** |
||||
610 | * Returns the named request body parameter value. |
||||
611 | * If the parameter does not exist, the second parameter passed to this method will be returned. |
||||
612 | * @param string $name the parameter name |
||||
613 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||||
614 | * @return mixed the parameter value |
||||
615 | * @see getBodyParams() |
||||
616 | * @see setBodyParams() |
||||
617 | */ |
||||
618 | 7 | public function getBodyParam($name, $defaultValue = null) |
|||
619 | { |
||||
620 | 7 | $params = $this->getBodyParams(); |
|||
621 | |||||
622 | 7 | if (is_object($params)) { |
|||
623 | // unable to use `ArrayHelper::getValue()` due to different dots in key logic and lack of exception handling |
||||
624 | try { |
||||
625 | 1 | return $params->{$name}; |
|||
626 | 1 | } catch (\Exception $e) { |
|||
627 | 1 | return $defaultValue; |
|||
628 | } |
||||
629 | } |
||||
630 | |||||
631 | 7 | return isset($params[$name]) ? $params[$name] : $defaultValue; |
|||
632 | } |
||||
633 | |||||
634 | /** |
||||
635 | * Returns POST parameter with a given name. If name isn't specified, returns an array of all POST parameters. |
||||
636 | * |
||||
637 | * @param string $name the parameter name |
||||
638 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||||
639 | * @return array|mixed |
||||
640 | */ |
||||
641 | public function post($name = null, $defaultValue = null) |
||||
642 | { |
||||
643 | if ($name === null) { |
||||
644 | return $this->getBodyParams(); |
||||
645 | } |
||||
646 | |||||
647 | return $this->getBodyParam($name, $defaultValue); |
||||
648 | } |
||||
649 | |||||
650 | private $_queryParams; |
||||
651 | |||||
652 | /** |
||||
653 | * Returns the request parameters given in the [[queryString]]. |
||||
654 | * |
||||
655 | * This method will return the contents of `$_GET` if params where not explicitly set. |
||||
656 | * @return array the request GET parameter values. |
||||
657 | * @see setQueryParams() |
||||
658 | */ |
||||
659 | 65 | public function getQueryParams() |
|||
660 | { |
||||
661 | 65 | if ($this->_queryParams === null) { |
|||
662 | 58 | return $_GET; |
|||
663 | } |
||||
664 | |||||
665 | 9 | return $this->_queryParams; |
|||
666 | } |
||||
667 | |||||
668 | /** |
||||
669 | * Sets the request [[queryString]] parameters. |
||||
670 | * @param array $values the request query parameters (name-value pairs) |
||||
671 | * @see getQueryParam() |
||||
672 | * @see getQueryParams() |
||||
673 | */ |
||||
674 | 9 | public function setQueryParams($values) |
|||
675 | { |
||||
676 | 9 | $this->_queryParams = $values; |
|||
677 | 9 | } |
|||
678 | |||||
679 | /** |
||||
680 | * Returns GET parameter with a given name. If name isn't specified, returns an array of all GET parameters. |
||||
681 | * |
||||
682 | * @param string $name the parameter name |
||||
683 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||||
684 | * @return array|mixed |
||||
685 | */ |
||||
686 | 29 | public function get($name = null, $defaultValue = null) |
|||
687 | { |
||||
688 | 29 | if ($name === null) { |
|||
689 | return $this->getQueryParams(); |
||||
690 | } |
||||
691 | |||||
692 | 29 | return $this->getQueryParam($name, $defaultValue); |
|||
693 | } |
||||
694 | |||||
695 | /** |
||||
696 | * Returns the named GET parameter value. |
||||
697 | * If the GET parameter does not exist, the second parameter passed to this method will be returned. |
||||
698 | * @param string $name the GET parameter name. |
||||
699 | * @param mixed $defaultValue the default parameter value if the GET parameter does not exist. |
||||
700 | * @return mixed the GET parameter value |
||||
701 | * @see getBodyParam() |
||||
702 | */ |
||||
703 | 32 | public function getQueryParam($name, $defaultValue = null) |
|||
704 | { |
||||
705 | 32 | $params = $this->getQueryParams(); |
|||
706 | |||||
707 | 32 | return isset($params[$name]) ? $params[$name] : $defaultValue; |
|||
708 | } |
||||
709 | |||||
710 | private $_hostInfo; |
||||
711 | private $_hostName; |
||||
712 | |||||
713 | /** |
||||
714 | * Returns the schema and host part of the current request URL. |
||||
715 | * |
||||
716 | * The returned URL does not have an ending slash. |
||||
717 | * |
||||
718 | * By default this value is based on the user request information. This method will |
||||
719 | * return the value of `$_SERVER['HTTP_HOST']` if it is available or `$_SERVER['SERVER_NAME']` if not. |
||||
720 | * You may want to check out the [PHP documentation](https://secure.php.net/manual/en/reserved.variables.server.php) |
||||
721 | * for more information on these variables. |
||||
722 | * |
||||
723 | * You may explicitly specify it by setting the [[setHostInfo()|hostInfo]] property. |
||||
724 | * |
||||
725 | * > Warning: Dependent on the server configuration this information may not be |
||||
726 | * > reliable and [may be faked by the user sending the HTTP request](https://www.acunetix.com/vulnerabilities/web/host-header-attack). |
||||
727 | * > If the webserver is configured to serve the same site independent of the value of |
||||
728 | * > the `Host` header, this value is not reliable. In such situations you should either |
||||
729 | * > fix your webserver configuration or explicitly set the value by setting the [[setHostInfo()|hostInfo]] property. |
||||
730 | * > If you don't have access to the server configuration, you can setup [[\yii\filters\HostControl]] filter at |
||||
731 | * > application level in order to protect against such kind of attack. |
||||
732 | * |
||||
733 | * @property string|null schema and hostname part (with port number if needed) of the request URL |
||||
734 | * (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set. |
||||
735 | * See [[getHostInfo()]] for security related notes on this property. |
||||
736 | * @return string|null schema and hostname part (with port number if needed) of the request URL |
||||
737 | * (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set. |
||||
738 | * @see setHostInfo() |
||||
739 | */ |
||||
740 | 49 | public function getHostInfo() |
|||
741 | { |
||||
742 | 49 | if ($this->_hostInfo === null) { |
|||
743 | 45 | $secure = $this->getIsSecureConnection(); |
|||
744 | 45 | $http = $secure ? 'https' : 'http'; |
|||
745 | |||||
746 | 45 | if ($this->getSecureForwardedHeaderTrustedPart('host') !== null) { |
|||
747 | 8 | $this->_hostInfo = $http . '://' . $this->getSecureForwardedHeaderTrustedPart('host'); |
|||
748 | 37 | } elseif ($this->headers->has('X-Forwarded-Host')) { |
|||
749 | 3 | $this->_hostInfo = $http . '://' . trim(explode(',', $this->headers->get('X-Forwarded-Host'))[0]); |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
750 | 34 | } elseif ($this->headers->has('X-Original-Host')) { |
|||
751 | $this->_hostInfo = $http . '://' . trim(explode(',', $this->headers->get('X-Original-Host'))[0]); |
||||
752 | 34 | } elseif ($this->headers->has('Host')) { |
|||
753 | 11 | $this->_hostInfo = $http . '://' . $this->headers->get('Host'); |
|||
754 | 23 | } elseif (isset($_SERVER['SERVER_NAME'])) { |
|||
755 | 1 | $this->_hostInfo = $http . '://' . $_SERVER['SERVER_NAME']; |
|||
756 | 1 | $port = $secure ? $this->getSecurePort() : $this->getPort(); |
|||
757 | 1 | if (($port !== 80 && !$secure) || ($port !== 443 && $secure)) { |
|||
758 | $this->_hostInfo .= ':' . $port; |
||||
759 | } |
||||
760 | } |
||||
761 | } |
||||
762 | |||||
763 | 49 | return $this->_hostInfo; |
|||
764 | } |
||||
765 | |||||
766 | /** |
||||
767 | * Sets the schema and host part of the application URL. |
||||
768 | * This setter is provided in case the schema and hostname cannot be determined |
||||
769 | * on certain Web servers. |
||||
770 | * @param string|null $value the schema and host part of the application URL. The trailing slashes will be removed. |
||||
771 | * @see getHostInfo() for security related notes on this property. |
||||
772 | */ |
||||
773 | 58 | public function setHostInfo($value) |
|||
774 | { |
||||
775 | 58 | $this->_hostName = null; |
|||
776 | 58 | $this->_hostInfo = $value === null ? null : rtrim($value, '/'); |
|||
777 | 58 | } |
|||
778 | |||||
779 | /** |
||||
780 | * Returns the host part of the current request URL. |
||||
781 | * Value is calculated from current [[getHostInfo()|hostInfo]] property. |
||||
782 | * |
||||
783 | * > Warning: The content of this value may not be reliable, dependent on the server |
||||
784 | * > configuration. Please refer to [[getHostInfo()]] for more information. |
||||
785 | * |
||||
786 | * @return string|null hostname part of the request URL (e.g. `www.yiiframework.com`) |
||||
787 | * @see getHostInfo() |
||||
788 | * @since 2.0.10 |
||||
789 | */ |
||||
790 | 20 | public function getHostName() |
|||
791 | { |
||||
792 | 20 | if ($this->_hostName === null) { |
|||
793 | 20 | $this->_hostName = parse_url($this->getHostInfo(), PHP_URL_HOST); |
|||
794 | } |
||||
795 | |||||
796 | 20 | return $this->_hostName; |
|||
797 | } |
||||
798 | |||||
799 | private $_baseUrl; |
||||
800 | |||||
801 | /** |
||||
802 | * Returns the relative URL for the application. |
||||
803 | * This is similar to [[scriptUrl]] except that it does not include the script file name, |
||||
804 | * and the ending slashes are removed. |
||||
805 | * @return string the relative URL for the application |
||||
806 | * @see setScriptUrl() |
||||
807 | */ |
||||
808 | 433 | public function getBaseUrl() |
|||
809 | { |
||||
810 | 433 | if ($this->_baseUrl === null) { |
|||
811 | 431 | $this->_baseUrl = rtrim(dirname($this->getScriptUrl()), '\\/'); |
|||
812 | } |
||||
813 | |||||
814 | 433 | return $this->_baseUrl; |
|||
815 | } |
||||
816 | |||||
817 | /** |
||||
818 | * Sets the relative URL for the application. |
||||
819 | * By default the URL is determined based on the entry script URL. |
||||
820 | * This setter is provided in case you want to change this behavior. |
||||
821 | * @param string $value the relative URL for the application |
||||
822 | */ |
||||
823 | 2 | public function setBaseUrl($value) |
|||
824 | { |
||||
825 | 2 | $this->_baseUrl = $value; |
|||
826 | 2 | } |
|||
827 | |||||
828 | private $_scriptUrl; |
||||
829 | |||||
830 | /** |
||||
831 | * Returns the relative URL of the entry script. |
||||
832 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||||
833 | * @return string the relative URL of the entry script. |
||||
834 | * @throws InvalidConfigException if unable to determine the entry script URL |
||||
835 | */ |
||||
836 | 434 | public function getScriptUrl() |
|||
837 | { |
||||
838 | 434 | if ($this->_scriptUrl === null) { |
|||
839 | 1 | $scriptFile = $this->getScriptFile(); |
|||
840 | $scriptName = basename($scriptFile); |
||||
841 | if (isset($_SERVER['SCRIPT_NAME']) && basename($_SERVER['SCRIPT_NAME']) === $scriptName) { |
||||
842 | $this->_scriptUrl = $_SERVER['SCRIPT_NAME']; |
||||
843 | } elseif (isset($_SERVER['PHP_SELF']) && basename($_SERVER['PHP_SELF']) === $scriptName) { |
||||
844 | $this->_scriptUrl = $_SERVER['PHP_SELF']; |
||||
845 | } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $scriptName) { |
||||
846 | $this->_scriptUrl = $_SERVER['ORIG_SCRIPT_NAME']; |
||||
847 | } elseif (isset($_SERVER['PHP_SELF']) && ($pos = strpos($_SERVER['PHP_SELF'], '/' . $scriptName)) !== false) { |
||||
848 | $this->_scriptUrl = substr($_SERVER['SCRIPT_NAME'], 0, $pos) . '/' . $scriptName; |
||||
849 | } elseif (!empty($_SERVER['DOCUMENT_ROOT']) && strpos($scriptFile, $_SERVER['DOCUMENT_ROOT']) === 0) { |
||||
850 | $this->_scriptUrl = str_replace([$_SERVER['DOCUMENT_ROOT'], '\\'], ['', '/'], $scriptFile); |
||||
851 | } else { |
||||
852 | throw new InvalidConfigException('Unable to determine the entry script URL.'); |
||||
853 | } |
||||
854 | } |
||||
855 | |||||
856 | 433 | return $this->_scriptUrl; |
|||
857 | } |
||||
858 | |||||
859 | /** |
||||
860 | * Sets the relative URL for the application entry script. |
||||
861 | * This setter is provided in case the entry script URL cannot be determined |
||||
862 | * on certain Web servers. |
||||
863 | * @param string $value the relative URL for the application entry script. |
||||
864 | */ |
||||
865 | 444 | public function setScriptUrl($value) |
|||
866 | { |
||||
867 | 444 | $this->_scriptUrl = $value === null ? null : '/' . trim($value, '/'); |
|||
868 | 444 | } |
|||
869 | |||||
870 | private $_scriptFile; |
||||
871 | |||||
872 | /** |
||||
873 | * Returns the entry script file path. |
||||
874 | * The default implementation will simply return `$_SERVER['SCRIPT_FILENAME']`. |
||||
875 | * @return string the entry script file path |
||||
876 | * @throws InvalidConfigException |
||||
877 | */ |
||||
878 | 435 | public function getScriptFile() |
|||
879 | { |
||||
880 | 435 | if (isset($this->_scriptFile)) { |
|||
881 | 407 | return $this->_scriptFile; |
|||
882 | } |
||||
883 | |||||
884 | 28 | if (isset($_SERVER['SCRIPT_FILENAME'])) { |
|||
885 | 26 | return $_SERVER['SCRIPT_FILENAME']; |
|||
886 | } |
||||
887 | |||||
888 | 2 | throw new InvalidConfigException('Unable to determine the entry script file path.'); |
|||
889 | } |
||||
890 | |||||
891 | /** |
||||
892 | * Sets the entry script file path. |
||||
893 | * The entry script file path normally can be obtained from `$_SERVER['SCRIPT_FILENAME']`. |
||||
894 | * If your server configuration does not return the correct value, you may configure |
||||
895 | * this property to make it right. |
||||
896 | * @param string $value the entry script file path. |
||||
897 | */ |
||||
898 | 407 | public function setScriptFile($value) |
|||
899 | { |
||||
900 | 407 | $this->_scriptFile = $value; |
|||
901 | 407 | } |
|||
902 | |||||
903 | private $_pathInfo; |
||||
904 | |||||
905 | /** |
||||
906 | * Returns the path info of the currently requested URL. |
||||
907 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||||
908 | * The starting and ending slashes are both removed. |
||||
909 | * @return string part of the request URL that is after the entry script and before the question mark. |
||||
910 | * Note, the returned path info is already URL-decoded. |
||||
911 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||||
912 | */ |
||||
913 | 19 | public function getPathInfo() |
|||
914 | { |
||||
915 | 19 | if ($this->_pathInfo === null) { |
|||
916 | $this->_pathInfo = $this->resolvePathInfo(); |
||||
917 | } |
||||
918 | |||||
919 | 19 | return $this->_pathInfo; |
|||
920 | } |
||||
921 | |||||
922 | /** |
||||
923 | * Sets the path info of the current request. |
||||
924 | * This method is mainly provided for testing purpose. |
||||
925 | * @param string $value the path info of the current request |
||||
926 | */ |
||||
927 | 20 | public function setPathInfo($value) |
|||
928 | { |
||||
929 | 20 | $this->_pathInfo = $value === null ? null : ltrim($value, '/'); |
|||
930 | 20 | } |
|||
931 | |||||
932 | /** |
||||
933 | * Resolves the path info part of the currently requested URL. |
||||
934 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||||
935 | * The starting slashes are both removed (ending slashes will be kept). |
||||
936 | * @return string part of the request URL that is after the entry script and before the question mark. |
||||
937 | * Note, the returned path info is decoded. |
||||
938 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||||
939 | */ |
||||
940 | protected function resolvePathInfo() |
||||
941 | { |
||||
942 | $pathInfo = $this->getUrl(); |
||||
943 | |||||
944 | if (($pos = strpos($pathInfo, '?')) !== false) { |
||||
945 | $pathInfo = substr($pathInfo, 0, $pos); |
||||
946 | } |
||||
947 | |||||
948 | $pathInfo = urldecode($pathInfo); |
||||
949 | |||||
950 | // try to encode in UTF8 if not so |
||||
951 | // http://w3.org/International/questions/qa-forms-utf-8.html |
||||
952 | if (!preg_match('%^(?: |
||||
953 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
||||
954 | | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
||||
955 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
||||
956 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte |
||||
957 | | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates |
||||
958 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
||||
959 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
||||
960 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
||||
961 | )*$%xs', $pathInfo) |
||||
962 | ) { |
||||
963 | $pathInfo = $this->utf8Encode($pathInfo); |
||||
964 | } |
||||
965 | |||||
966 | $scriptUrl = $this->getScriptUrl(); |
||||
967 | $baseUrl = $this->getBaseUrl(); |
||||
968 | if (strpos($pathInfo, $scriptUrl) === 0) { |
||||
969 | $pathInfo = substr($pathInfo, strlen($scriptUrl)); |
||||
970 | } elseif ($baseUrl === '' || strpos($pathInfo, $baseUrl) === 0) { |
||||
971 | $pathInfo = substr($pathInfo, strlen($baseUrl)); |
||||
972 | } elseif (isset($_SERVER['PHP_SELF']) && strpos($_SERVER['PHP_SELF'], $scriptUrl) === 0) { |
||||
973 | $pathInfo = substr($_SERVER['PHP_SELF'], strlen($scriptUrl)); |
||||
974 | } else { |
||||
975 | throw new InvalidConfigException('Unable to determine the path info of the current request.'); |
||||
976 | } |
||||
977 | |||||
978 | if (strncmp($pathInfo, '/', 1) === 0) { |
||||
979 | $pathInfo = substr($pathInfo, 1); |
||||
980 | } |
||||
981 | |||||
982 | return (string) $pathInfo; |
||||
983 | } |
||||
984 | |||||
985 | /** |
||||
986 | * Encodes an ISO-8859-1 string to UTF-8 |
||||
987 | * @param string $s |
||||
988 | * @return string the UTF-8 translation of `s`. |
||||
989 | * @see https://github.com/symfony/polyfill-php72/blob/master/Php72.php#L24 |
||||
990 | */ |
||||
991 | private function utf8Encode($s) |
||||
992 | { |
||||
993 | $s .= $s; |
||||
994 | $len = \strlen($s); |
||||
995 | for ($i = $len >> 1, $j = 0; $i < $len; ++$i, ++$j) { |
||||
996 | switch (true) { |
||||
997 | case $s[$i] < "\x80": $s[$j] = $s[$i]; break; |
||||
998 | case $s[$i] < "\xC0": $s[$j] = "\xC2"; $s[++$j] = $s[$i]; break; |
||||
999 | default: $s[$j] = "\xC3"; $s[++$j] = \chr(\ord($s[$i]) - 64); break; |
||||
1000 | } |
||||
1001 | } |
||||
1002 | return substr($s, 0, $j); |
||||
1003 | } |
||||
1004 | |||||
1005 | /** |
||||
1006 | * Returns the currently requested absolute URL. |
||||
1007 | * This is a shortcut to the concatenation of [[hostInfo]] and [[url]]. |
||||
1008 | * @return string the currently requested absolute URL. |
||||
1009 | */ |
||||
1010 | 1 | public function getAbsoluteUrl() |
|||
1011 | { |
||||
1012 | 1 | return $this->getHostInfo() . $this->getUrl(); |
|||
1013 | } |
||||
1014 | |||||
1015 | private $_url; |
||||
1016 | |||||
1017 | /** |
||||
1018 | * Returns the currently requested relative URL. |
||||
1019 | * This refers to the portion of the URL that is after the [[hostInfo]] part. |
||||
1020 | * It includes the [[queryString]] part if any. |
||||
1021 | * @return string the currently requested relative URL. Note that the URI returned may be URL-encoded depending on the client. |
||||
1022 | * @throws InvalidConfigException if the URL cannot be determined due to unusual server configuration |
||||
1023 | */ |
||||
1024 | 11 | public function getUrl() |
|||
1025 | { |
||||
1026 | 11 | if ($this->_url === null) { |
|||
1027 | 3 | $this->_url = $this->resolveRequestUri(); |
|||
1028 | } |
||||
1029 | |||||
1030 | 11 | return $this->_url; |
|||
1031 | } |
||||
1032 | |||||
1033 | /** |
||||
1034 | * Sets the currently requested relative URL. |
||||
1035 | * The URI must refer to the portion that is after [[hostInfo]]. |
||||
1036 | * Note that the URI should be URL-encoded. |
||||
1037 | * @param string $value the request URI to be set |
||||
1038 | */ |
||||
1039 | 24 | public function setUrl($value) |
|||
1040 | { |
||||
1041 | 24 | $this->_url = $value; |
|||
1042 | 24 | } |
|||
1043 | |||||
1044 | /** |
||||
1045 | * Resolves the request URI portion for the currently requested URL. |
||||
1046 | * This refers to the portion that is after the [[hostInfo]] part. It includes the [[queryString]] part if any. |
||||
1047 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||||
1048 | * @return string|bool the request URI portion for the currently requested URL. |
||||
1049 | * Note that the URI returned may be URL-encoded depending on the client. |
||||
1050 | * @throws InvalidConfigException if the request URI cannot be determined due to unusual server configuration |
||||
1051 | */ |
||||
1052 | 3 | protected function resolveRequestUri() |
|||
1053 | { |
||||
1054 | 3 | if ($this->headers->has('X-Rewrite-Url')) { // IIS |
|||
1055 | $requestUri = $this->headers->get('X-Rewrite-Url'); |
||||
1056 | 3 | } elseif (isset($_SERVER['REQUEST_URI'])) { |
|||
1057 | 3 | $requestUri = $_SERVER['REQUEST_URI']; |
|||
1058 | 3 | if ($requestUri !== '' && $requestUri[0] !== '/') { |
|||
1059 | 3 | $requestUri = preg_replace('/^(http|https):\/\/[^\/]+/i', '', $requestUri); |
|||
1060 | } |
||||
1061 | } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0 CGI |
||||
1062 | $requestUri = $_SERVER['ORIG_PATH_INFO']; |
||||
1063 | if (!empty($_SERVER['QUERY_STRING'])) { |
||||
1064 | $requestUri .= '?' . $_SERVER['QUERY_STRING']; |
||||
1065 | } |
||||
1066 | } else { |
||||
1067 | throw new InvalidConfigException('Unable to determine the request URI.'); |
||||
1068 | } |
||||
1069 | |||||
1070 | 3 | return $requestUri; |
|||
1071 | } |
||||
1072 | |||||
1073 | /** |
||||
1074 | * Returns part of the request URL that is after the question mark. |
||||
1075 | * @return string part of the request URL that is after the question mark |
||||
1076 | */ |
||||
1077 | public function getQueryString() |
||||
1078 | { |
||||
1079 | return isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ''; |
||||
1080 | } |
||||
1081 | |||||
1082 | /** |
||||
1083 | * Return if the request is sent via secure channel (https). |
||||
1084 | * @return bool if the request is sent via secure channel (https) |
||||
1085 | */ |
||||
1086 | 73 | public function getIsSecureConnection() |
|||
1087 | { |
||||
1088 | 73 | if (isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1)) { |
|||
1089 | 2 | return true; |
|||
1090 | } |
||||
1091 | |||||
1092 | 71 | if (($proto = $this->getSecureForwardedHeaderTrustedPart('proto')) !== null) { |
|||
1093 | 9 | return strcasecmp($proto, 'https') === 0; |
|||
1094 | } |
||||
1095 | |||||
1096 | 62 | foreach ($this->secureProtocolHeaders as $header => $values) { |
|||
1097 | 62 | if (($headerValue = $this->headers->get($header, null)) !== null) { |
|||
1098 | 5 | foreach ($values as $value) { |
|||
1099 | 5 | if (strcasecmp($headerValue, $value) === 0) { |
|||
1100 | 62 | return true; |
|||
1101 | } |
||||
1102 | } |
||||
1103 | } |
||||
1104 | } |
||||
1105 | |||||
1106 | 59 | return false; |
|||
1107 | } |
||||
1108 | |||||
1109 | /** |
||||
1110 | * Returns the server name. |
||||
1111 | * @return string server name, null if not available |
||||
1112 | */ |
||||
1113 | 1 | public function getServerName() |
|||
1114 | { |
||||
1115 | 1 | return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : null; |
|||
1116 | } |
||||
1117 | |||||
1118 | /** |
||||
1119 | * Returns the server port number. |
||||
1120 | * @return int|null server port number, null if not available |
||||
1121 | */ |
||||
1122 | 2 | public function getServerPort() |
|||
1123 | { |
||||
1124 | 2 | return isset($_SERVER['SERVER_PORT']) ? (int) $_SERVER['SERVER_PORT'] : null; |
|||
1125 | } |
||||
1126 | |||||
1127 | /** |
||||
1128 | * Returns the URL referrer. |
||||
1129 | * @return string|null URL referrer, null if not available |
||||
1130 | */ |
||||
1131 | public function getReferrer() |
||||
1132 | { |
||||
1133 | return $this->headers->get('Referer'); |
||||
1134 | } |
||||
1135 | |||||
1136 | /** |
||||
1137 | * Returns the URL origin of a CORS request. |
||||
1138 | * |
||||
1139 | * The return value is taken from the `Origin` [[getHeaders()|header]] sent by the browser. |
||||
1140 | * |
||||
1141 | * Note that the origin request header indicates where a fetch originates from. |
||||
1142 | * It doesn't include any path information, but only the server name. |
||||
1143 | * It is sent with a CORS requests, as well as with POST requests. |
||||
1144 | * It is similar to the referer header, but, unlike this header, it doesn't disclose the whole path. |
||||
1145 | * Please refer to <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin> for more information. |
||||
1146 | * |
||||
1147 | * @return string|null URL origin of a CORS request, `null` if not available. |
||||
1148 | * @see getHeaders() |
||||
1149 | * @since 2.0.13 |
||||
1150 | */ |
||||
1151 | 1 | public function getOrigin() |
|||
1152 | { |
||||
1153 | 1 | return $this->getHeaders()->get('origin'); |
|||
1154 | } |
||||
1155 | |||||
1156 | /** |
||||
1157 | * Returns the user agent. |
||||
1158 | * @return string|null user agent, null if not available |
||||
1159 | */ |
||||
1160 | 1 | public function getUserAgent() |
|||
1161 | { |
||||
1162 | 1 | return $this->headers->get('User-Agent'); |
|||
1163 | } |
||||
1164 | |||||
1165 | /** |
||||
1166 | * Returns the user IP address from [[ipHeaders]]. |
||||
1167 | * @return string|null user IP address, null if not available |
||||
1168 | * @see $ipHeaders |
||||
1169 | * @since 2.0.28 |
||||
1170 | */ |
||||
1171 | 102 | protected function getUserIpFromIpHeaders() |
|||
1172 | { |
||||
1173 | 102 | $ip = $this->getSecureForwardedHeaderTrustedPart('for'); |
|||
1174 | 102 | if ($ip !== null && preg_match( |
|||
1175 | 14 | '/^\[?(?P<ip>(?:(?:(?:[0-9a-f]{1,4}:){1,6}(?:[0-9a-f]{1,4})?(?:(?::[0-9a-f]{1,4}){1,6}))|(?:[\d]{1,3}\.){3}[\d]{1,3}))\]?(?::(?P<port>[\d]+))?$/', |
|||
1176 | 14 | $ip, |
|||
1177 | 102 | $matches |
|||
1178 | )) { |
||||
1179 | 14 | $ip = $this->getUserIpFromIpHeader($matches['ip']); |
|||
1180 | 14 | if ($ip !== null) { |
|||
1181 | 14 | return $ip; |
|||
1182 | } |
||||
1183 | } |
||||
1184 | |||||
1185 | |||||
1186 | 88 | foreach ($this->ipHeaders as $ipHeader) { |
|||
1187 | 85 | if ($this->headers->has($ipHeader)) { |
|||
1188 | 10 | $ip = $this->getUserIpFromIpHeader($this->headers->get($ipHeader)); |
|||
1189 | 10 | if ($ip !== null) { |
|||
1190 | 85 | return $ip; |
|||
1191 | } |
||||
1192 | } |
||||
1193 | } |
||||
1194 | 78 | return null; |
|||
1195 | } |
||||
1196 | |||||
1197 | /** |
||||
1198 | * Returns the user IP address. |
||||
1199 | * The IP is determined using headers and / or `$_SERVER` variables. |
||||
1200 | * @return string|null user IP address, null if not available |
||||
1201 | */ |
||||
1202 | 102 | public function getUserIP() |
|||
1203 | { |
||||
1204 | 102 | $ip = $this->getUserIpFromIpHeaders(); |
|||
1205 | 102 | return $ip === null ? $this->getRemoteIP() : $ip; |
|||
1206 | } |
||||
1207 | |||||
1208 | /** |
||||
1209 | * Return user IP's from IP header. |
||||
1210 | * |
||||
1211 | * @param string $ips comma separated IP list |
||||
1212 | * @return string|null IP as string. Null is returned if IP can not be determined from header. |
||||
1213 | * @see $getUserHost |
||||
1214 | * @see $ipHeader |
||||
1215 | * @see $trustedHeaders |
||||
1216 | * @since 2.0.28 |
||||
1217 | */ |
||||
1218 | 24 | protected function getUserIpFromIpHeader($ips) |
|||
1219 | { |
||||
1220 | 24 | $ips = trim($ips); |
|||
1221 | 24 | if ($ips === '') { |
|||
1222 | return null; |
||||
1223 | } |
||||
1224 | 24 | $ips = preg_split('/\s*,\s*/', $ips, -1, PREG_SPLIT_NO_EMPTY); |
|||
1225 | 24 | krsort($ips); |
|||
1226 | 24 | $validator = $this->getIpValidator(); |
|||
1227 | 24 | $resultIp = null; |
|||
1228 | 24 | foreach ($ips as $ip) { |
|||
1229 | 24 | $validator->setRanges('any'); |
|||
1230 | 24 | if (!$validator->validate($ip) /* checking IP format */) { |
|||
1231 | 1 | break; |
|||
1232 | } |
||||
1233 | 24 | $resultIp = $ip; |
|||
1234 | 24 | $isTrusted = false; |
|||
1235 | 24 | foreach ($this->trustedHosts as $trustedCidr => $trustedCidrOrHeaders) { |
|||
1236 | 24 | if (!is_array($trustedCidrOrHeaders)) { |
|||
1237 | 24 | $trustedCidr = $trustedCidrOrHeaders; |
|||
1238 | } |
||||
1239 | 24 | $validator->setRanges($trustedCidr); |
|||
1240 | 24 | if ($validator->validate($ip) /* checking trusted range */) { |
|||
1241 | 7 | $isTrusted = true; |
|||
1242 | 24 | break; |
|||
1243 | } |
||||
1244 | } |
||||
1245 | 24 | if (!$isTrusted) { |
|||
1246 | 24 | break; |
|||
1247 | } |
||||
1248 | } |
||||
1249 | 24 | return $resultIp; |
|||
1250 | } |
||||
1251 | |||||
1252 | /** |
||||
1253 | * Returns the user host name. |
||||
1254 | * The HOST is determined using headers and / or `$_SERVER` variables. |
||||
1255 | * @return string|null user host name, null if not available |
||||
1256 | */ |
||||
1257 | public function getUserHost() |
||||
1258 | { |
||||
1259 | $userIp = $this->getUserIpFromIpHeaders(); |
||||
1260 | if($userIp === null) { |
||||
1261 | return $this->getRemoteHost(); |
||||
1262 | } |
||||
1263 | return gethostbyaddr($userIp); |
||||
1264 | } |
||||
1265 | |||||
1266 | /** |
||||
1267 | * Returns the IP on the other end of this connection. |
||||
1268 | * This is always the next hop, any headers are ignored. |
||||
1269 | * @return string|null remote IP address, `null` if not available. |
||||
1270 | * @since 2.0.13 |
||||
1271 | */ |
||||
1272 | 135 | public function getRemoteIP() |
|||
1273 | { |
||||
1274 | 135 | return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null; |
|||
1275 | } |
||||
1276 | |||||
1277 | /** |
||||
1278 | * Returns the host name of the other end of this connection. |
||||
1279 | * This is always the next hop, any headers are ignored. |
||||
1280 | * @return string|null remote host name, `null` if not available |
||||
1281 | * @see getUserHost() |
||||
1282 | * @see getRemoteIP() |
||||
1283 | * @since 2.0.13 |
||||
1284 | */ |
||||
1285 | public function getRemoteHost() |
||||
1286 | { |
||||
1287 | return isset($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : null; |
||||
1288 | } |
||||
1289 | |||||
1290 | /** |
||||
1291 | * @return string|null the username sent via HTTP authentication, `null` if the username is not given |
||||
1292 | * @see getAuthCredentials() to get both username and password in one call |
||||
1293 | */ |
||||
1294 | 9 | public function getAuthUser() |
|||
1295 | { |
||||
1296 | 9 | return $this->getAuthCredentials()[0]; |
|||
1297 | } |
||||
1298 | |||||
1299 | /** |
||||
1300 | * @return string|null the password sent via HTTP authentication, `null` if the password is not given |
||||
1301 | * @see getAuthCredentials() to get both username and password in one call |
||||
1302 | */ |
||||
1303 | 9 | public function getAuthPassword() |
|||
1304 | { |
||||
1305 | 9 | return $this->getAuthCredentials()[1]; |
|||
1306 | } |
||||
1307 | |||||
1308 | /** |
||||
1309 | * @return array that contains exactly two elements: |
||||
1310 | * - 0: the username sent via HTTP authentication, `null` if the username is not given |
||||
1311 | * - 1: the password sent via HTTP authentication, `null` if the password is not given |
||||
1312 | * @see getAuthUser() to get only username |
||||
1313 | * @see getAuthPassword() to get only password |
||||
1314 | * @since 2.0.13 |
||||
1315 | */ |
||||
1316 | 39 | public function getAuthCredentials() |
|||
1317 | { |
||||
1318 | 39 | $username = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : null; |
|||
1319 | 39 | $password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : null; |
|||
1320 | 39 | if ($username !== null || $password !== null) { |
|||
1321 | 21 | return [$username, $password]; |
|||
1322 | } |
||||
1323 | |||||
1324 | /* |
||||
1325 | * Apache with php-cgi does not pass HTTP Basic authentication to PHP by default. |
||||
1326 | * To make it work, add the following line to to your .htaccess file: |
||||
1327 | * |
||||
1328 | * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] |
||||
1329 | */ |
||||
1330 | 18 | $auth_token = $this->getHeaders()->get('HTTP_AUTHORIZATION') ?: $this->getHeaders()->get('REDIRECT_HTTP_AUTHORIZATION'); |
|||
1331 | 18 | if ($auth_token !== null && strncasecmp($auth_token, 'basic', 5) === 0) { |
|||
0 ignored issues
–
show
It seems like
$auth_token can also be of type array ; however, parameter $string1 of strncasecmp() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
1332 | $parts = array_map(function ($value) { |
||||
1333 | 18 | return strlen($value) === 0 ? null : $value; |
|||
1334 | 18 | }, explode(':', base64_decode(mb_substr($auth_token, 6)), 2)); |
|||
1335 | |||||
1336 | 18 | if (count($parts) < 2) { |
|||
1337 | 2 | return [$parts[0], null]; |
|||
1338 | } |
||||
1339 | |||||
1340 | 16 | return $parts; |
|||
1341 | } |
||||
1342 | |||||
1343 | return [null, null]; |
||||
1344 | } |
||||
1345 | |||||
1346 | private $_port; |
||||
1347 | |||||
1348 | /** |
||||
1349 | * Returns the port to use for insecure requests. |
||||
1350 | * Defaults to 80, or the port specified by the server if the current |
||||
1351 | * request is insecure. |
||||
1352 | * @return int port number for insecure requests. |
||||
1353 | * @see setPort() |
||||
1354 | */ |
||||
1355 | 1 | public function getPort() |
|||
1356 | { |
||||
1357 | 1 | if ($this->_port === null) { |
|||
1358 | 1 | $serverPort = $this->getServerPort(); |
|||
1359 | 1 | $this->_port = !$this->getIsSecureConnection() && $serverPort !== null ? $serverPort : 80; |
|||
1360 | } |
||||
1361 | |||||
1362 | 1 | return $this->_port; |
|||
1363 | } |
||||
1364 | |||||
1365 | /** |
||||
1366 | * Sets the port to use for insecure requests. |
||||
1367 | * This setter is provided in case a custom port is necessary for certain |
||||
1368 | * server configurations. |
||||
1369 | * @param int $value port number. |
||||
1370 | */ |
||||
1371 | public function setPort($value) |
||||
1372 | { |
||||
1373 | if ($value != $this->_port) { |
||||
1374 | $this->_port = (int) $value; |
||||
1375 | $this->_hostInfo = null; |
||||
1376 | } |
||||
1377 | } |
||||
1378 | |||||
1379 | private $_securePort; |
||||
1380 | |||||
1381 | /** |
||||
1382 | * Returns the port to use for secure requests. |
||||
1383 | * Defaults to 443, or the port specified by the server if the current |
||||
1384 | * request is secure. |
||||
1385 | * @return int port number for secure requests. |
||||
1386 | * @see setSecurePort() |
||||
1387 | */ |
||||
1388 | public function getSecurePort() |
||||
1389 | { |
||||
1390 | if ($this->_securePort === null) { |
||||
1391 | $serverPort = $this->getServerPort(); |
||||
1392 | $this->_securePort = $this->getIsSecureConnection() && $serverPort !== null ? $serverPort : 443; |
||||
1393 | } |
||||
1394 | |||||
1395 | return $this->_securePort; |
||||
1396 | } |
||||
1397 | |||||
1398 | /** |
||||
1399 | * Sets the port to use for secure requests. |
||||
1400 | * This setter is provided in case a custom port is necessary for certain |
||||
1401 | * server configurations. |
||||
1402 | * @param int $value port number. |
||||
1403 | */ |
||||
1404 | public function setSecurePort($value) |
||||
1405 | { |
||||
1406 | if ($value != $this->_securePort) { |
||||
1407 | $this->_securePort = (int) $value; |
||||
1408 | $this->_hostInfo = null; |
||||
1409 | } |
||||
1410 | } |
||||
1411 | |||||
1412 | private $_contentTypes; |
||||
1413 | |||||
1414 | /** |
||||
1415 | * Returns the content types acceptable by the end user. |
||||
1416 | * |
||||
1417 | * This is determined by the `Accept` HTTP header. For example, |
||||
1418 | * |
||||
1419 | * ```php |
||||
1420 | * $_SERVER['HTTP_ACCEPT'] = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||||
1421 | * $types = $request->getAcceptableContentTypes(); |
||||
1422 | * print_r($types); |
||||
1423 | * // displays: |
||||
1424 | * // [ |
||||
1425 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||||
1426 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||||
1427 | * // 'text/plain' => ['q' => 0.5], |
||||
1428 | * // ] |
||||
1429 | * ``` |
||||
1430 | * |
||||
1431 | * @return array the content types ordered by the quality score. Types with the highest scores |
||||
1432 | * will be returned first. The array keys are the content types, while the array values |
||||
1433 | * are the corresponding quality score and other parameters as given in the header. |
||||
1434 | */ |
||||
1435 | 5 | public function getAcceptableContentTypes() |
|||
1436 | { |
||||
1437 | 5 | if ($this->_contentTypes === null) { |
|||
1438 | 4 | if ($this->headers->get('Accept') !== null) { |
|||
1439 | 2 | $this->_contentTypes = $this->parseAcceptHeader($this->headers->get('Accept')); |
|||
1440 | } else { |
||||
1441 | 3 | $this->_contentTypes = []; |
|||
1442 | } |
||||
1443 | } |
||||
1444 | |||||
1445 | 5 | return $this->_contentTypes; |
|||
1446 | } |
||||
1447 | |||||
1448 | /** |
||||
1449 | * Sets the acceptable content types. |
||||
1450 | * Please refer to [[getAcceptableContentTypes()]] on the format of the parameter. |
||||
1451 | * @param array $value the content types that are acceptable by the end user. They should |
||||
1452 | * be ordered by the preference level. |
||||
1453 | * @see getAcceptableContentTypes() |
||||
1454 | * @see parseAcceptHeader() |
||||
1455 | */ |
||||
1456 | 1 | public function setAcceptableContentTypes($value) |
|||
1457 | { |
||||
1458 | 1 | $this->_contentTypes = $value; |
|||
1459 | 1 | } |
|||
1460 | |||||
1461 | /** |
||||
1462 | * Returns request content-type |
||||
1463 | * The Content-Type header field indicates the MIME type of the data |
||||
1464 | * contained in [[getRawBody()]] or, in the case of the HEAD method, the |
||||
1465 | * media type that would have been sent had the request been a GET. |
||||
1466 | * For the MIME-types the user expects in response, see [[acceptableContentTypes]]. |
||||
1467 | * @return string request content-type. Null is returned if this information is not available. |
||||
1468 | * @link https://tools.ietf.org/html/rfc2616#section-14.17 |
||||
1469 | * HTTP 1.1 header field definitions |
||||
1470 | */ |
||||
1471 | 4 | public function getContentType() |
|||
1472 | { |
||||
1473 | 4 | if (isset($_SERVER['CONTENT_TYPE'])) { |
|||
1474 | return $_SERVER['CONTENT_TYPE']; |
||||
1475 | } |
||||
1476 | |||||
1477 | //fix bug https://bugs.php.net/bug.php?id=66606 |
||||
1478 | 4 | return $this->headers->get('Content-Type'); |
|||
1479 | } |
||||
1480 | |||||
1481 | private $_languages; |
||||
1482 | |||||
1483 | /** |
||||
1484 | * Returns the languages acceptable by the end user. |
||||
1485 | * This is determined by the `Accept-Language` HTTP header. |
||||
1486 | * @return array the languages ordered by the preference level. The first element |
||||
1487 | * represents the most preferred language. |
||||
1488 | */ |
||||
1489 | 2 | public function getAcceptableLanguages() |
|||
1490 | { |
||||
1491 | 2 | if ($this->_languages === null) { |
|||
1492 | 1 | if ($this->headers->has('Accept-Language')) { |
|||
1493 | $this->_languages = array_keys($this->parseAcceptHeader($this->headers->get('Accept-Language'))); |
||||
1494 | } else { |
||||
1495 | 1 | $this->_languages = []; |
|||
1496 | } |
||||
1497 | } |
||||
1498 | |||||
1499 | 2 | return $this->_languages; |
|||
1500 | } |
||||
1501 | |||||
1502 | /** |
||||
1503 | * @param array $value the languages that are acceptable by the end user. They should |
||||
1504 | * be ordered by the preference level. |
||||
1505 | */ |
||||
1506 | 1 | public function setAcceptableLanguages($value) |
|||
1507 | { |
||||
1508 | 1 | $this->_languages = $value; |
|||
1509 | 1 | } |
|||
1510 | |||||
1511 | /** |
||||
1512 | * Parses the given `Accept` (or `Accept-Language`) header. |
||||
1513 | * |
||||
1514 | * This method will return the acceptable values with their quality scores and the corresponding parameters |
||||
1515 | * as specified in the given `Accept` header. The array keys of the return value are the acceptable values, |
||||
1516 | * while the array values consisting of the corresponding quality scores and parameters. The acceptable |
||||
1517 | * values with the highest quality scores will be returned first. For example, |
||||
1518 | * |
||||
1519 | * ```php |
||||
1520 | * $header = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||||
1521 | * $accepts = $request->parseAcceptHeader($header); |
||||
1522 | * print_r($accepts); |
||||
1523 | * // displays: |
||||
1524 | * // [ |
||||
1525 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||||
1526 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||||
1527 | * // 'text/plain' => ['q' => 0.5], |
||||
1528 | * // ] |
||||
1529 | * ``` |
||||
1530 | * |
||||
1531 | * @param string $header the header to be parsed |
||||
1532 | * @return array the acceptable values ordered by their quality score. The values with the highest scores |
||||
1533 | * will be returned first. |
||||
1534 | */ |
||||
1535 | 3 | public function parseAcceptHeader($header) |
|||
1536 | { |
||||
1537 | 3 | $accepts = []; |
|||
1538 | 3 | foreach (explode(',', $header) as $i => $part) { |
|||
1539 | 3 | $params = preg_split('/\s*;\s*/', trim($part), -1, PREG_SPLIT_NO_EMPTY); |
|||
1540 | 3 | if (empty($params)) { |
|||
1541 | 1 | continue; |
|||
1542 | } |
||||
1543 | $values = [ |
||||
1544 | 3 | 'q' => [$i, array_shift($params), 1], |
|||
1545 | ]; |
||||
1546 | 3 | foreach ($params as $param) { |
|||
1547 | 2 | if (strpos($param, '=') !== false) { |
|||
1548 | 2 | list($key, $value) = explode('=', $param, 2); |
|||
1549 | 2 | if ($key === 'q') { |
|||
1550 | 2 | $values['q'][2] = (float) $value; |
|||
1551 | } else { |
||||
1552 | 2 | $values[$key] = $value; |
|||
1553 | } |
||||
1554 | } else { |
||||
1555 | 2 | $values[] = $param; |
|||
1556 | } |
||||
1557 | } |
||||
1558 | 3 | $accepts[] = $values; |
|||
1559 | } |
||||
1560 | |||||
1561 | usort($accepts, function ($a, $b) { |
||||
1562 | 3 | $a = $a['q']; // index, name, q |
|||
1563 | 3 | $b = $b['q']; |
|||
1564 | 3 | if ($a[2] > $b[2]) { |
|||
1565 | 2 | return -1; |
|||
1566 | } |
||||
1567 | |||||
1568 | 2 | if ($a[2] < $b[2]) { |
|||
1569 | 1 | return 1; |
|||
1570 | } |
||||
1571 | |||||
1572 | 2 | if ($a[1] === $b[1]) { |
|||
1573 | return $a[0] > $b[0] ? 1 : -1; |
||||
1574 | } |
||||
1575 | |||||
1576 | 2 | if ($a[1] === '*/*') { |
|||
1577 | return 1; |
||||
1578 | } |
||||
1579 | |||||
1580 | 2 | if ($b[1] === '*/*') { |
|||
1581 | return -1; |
||||
1582 | } |
||||
1583 | |||||
1584 | 2 | $wa = $a[1][strlen($a[1]) - 1] === '*'; |
|||
1585 | 2 | $wb = $b[1][strlen($b[1]) - 1] === '*'; |
|||
1586 | 2 | if ($wa xor $wb) { |
|||
1587 | return $wa ? 1 : -1; |
||||
1588 | } |
||||
1589 | |||||
1590 | 2 | return $a[0] > $b[0] ? 1 : -1; |
|||
1591 | 3 | }); |
|||
1592 | |||||
1593 | 3 | $result = []; |
|||
1594 | 3 | foreach ($accepts as $accept) { |
|||
1595 | 3 | $name = $accept['q'][1]; |
|||
1596 | 3 | $accept['q'] = $accept['q'][2]; |
|||
1597 | 3 | $result[$name] = $accept; |
|||
1598 | } |
||||
1599 | |||||
1600 | 3 | return $result; |
|||
1601 | } |
||||
1602 | |||||
1603 | /** |
||||
1604 | * Returns the user-preferred language that should be used by this application. |
||||
1605 | * The language resolution is based on the user preferred languages and the languages |
||||
1606 | * supported by the application. The method will try to find the best match. |
||||
1607 | * @param array $languages a list of the languages supported by the application. If this is empty, the current |
||||
1608 | * application language will be returned without further processing. |
||||
1609 | * @return string the language that the application should use. |
||||
1610 | */ |
||||
1611 | 1 | public function getPreferredLanguage(array $languages = []) |
|||
1612 | { |
||||
1613 | 1 | if (empty($languages)) { |
|||
1614 | 1 | return Yii::$app->language; |
|||
1615 | } |
||||
1616 | 1 | foreach ($this->getAcceptableLanguages() as $acceptableLanguage) { |
|||
1617 | 1 | $acceptableLanguage = str_replace('_', '-', strtolower($acceptableLanguage)); |
|||
1618 | 1 | foreach ($languages as $language) { |
|||
1619 | 1 | $normalizedLanguage = str_replace('_', '-', strtolower($language)); |
|||
1620 | |||||
1621 | if ( |
||||
1622 | 1 | $normalizedLanguage === $acceptableLanguage // en-us==en-us |
|||
1623 | 1 | || strpos($acceptableLanguage, $normalizedLanguage . '-') === 0 // en==en-us |
|||
1624 | 1 | || strpos($normalizedLanguage, $acceptableLanguage . '-') === 0 // en-us==en |
|||
1625 | ) { |
||||
1626 | 1 | return $language; |
|||
1627 | } |
||||
1628 | } |
||||
1629 | } |
||||
1630 | |||||
1631 | 1 | return reset($languages); |
|||
1632 | } |
||||
1633 | |||||
1634 | /** |
||||
1635 | * Gets the Etags. |
||||
1636 | * |
||||
1637 | * @return array The entity tags |
||||
1638 | */ |
||||
1639 | public function getETags() |
||||
1640 | { |
||||
1641 | if ($this->headers->has('If-None-Match')) { |
||||
1642 | return preg_split('/[\s,]+/', str_replace('-gzip', '', $this->headers->get('If-None-Match')), -1, PREG_SPLIT_NO_EMPTY); |
||||
1643 | } |
||||
1644 | |||||
1645 | return []; |
||||
1646 | } |
||||
1647 | |||||
1648 | /** |
||||
1649 | * Returns the cookie collection. |
||||
1650 | * |
||||
1651 | * Through the returned cookie collection, you may access a cookie using the following syntax: |
||||
1652 | * |
||||
1653 | * ```php |
||||
1654 | * $cookie = $request->cookies['name'] |
||||
1655 | * if ($cookie !== null) { |
||||
1656 | * $value = $cookie->value; |
||||
1657 | * } |
||||
1658 | * |
||||
1659 | * // alternatively |
||||
1660 | * $value = $request->cookies->getValue('name'); |
||||
1661 | * ``` |
||||
1662 | * |
||||
1663 | * @return CookieCollection the cookie collection. |
||||
1664 | */ |
||||
1665 | 77 | public function getCookies() |
|||
1666 | { |
||||
1667 | 77 | if ($this->_cookies === null) { |
|||
1668 | 77 | $this->_cookies = new CookieCollection($this->loadCookies(), [ |
|||
1669 | 76 | 'readOnly' => true, |
|||
1670 | ]); |
||||
1671 | } |
||||
1672 | |||||
1673 | 76 | return $this->_cookies; |
|||
1674 | } |
||||
1675 | |||||
1676 | /** |
||||
1677 | * Converts `$_COOKIE` into an array of [[Cookie]]. |
||||
1678 | * @return array the cookies obtained from request |
||||
1679 | * @throws InvalidConfigException if [[cookieValidationKey]] is not set when [[enableCookieValidation]] is true |
||||
1680 | */ |
||||
1681 | 77 | protected function loadCookies() |
|||
1682 | { |
||||
1683 | 77 | $cookies = []; |
|||
1684 | 77 | if ($this->enableCookieValidation) { |
|||
1685 | 76 | if ($this->cookieValidationKey == '') { |
|||
1686 | 1 | throw new InvalidConfigException(get_class($this) . '::cookieValidationKey must be configured with a secret key.'); |
|||
1687 | } |
||||
1688 | 75 | foreach ($_COOKIE as $name => $value) { |
|||
1689 | if (!is_string($value)) { |
||||
1690 | continue; |
||||
1691 | } |
||||
1692 | $data = Yii::$app->getSecurity()->validateData($value, $this->cookieValidationKey); |
||||
1693 | if ($data === false) { |
||||
1694 | continue; |
||||
1695 | } |
||||
1696 | if (defined('PHP_VERSION_ID') && PHP_VERSION_ID >= 70000) { |
||||
1697 | $data = @unserialize($data, ['allowed_classes' => false]); |
||||
1698 | } else { |
||||
1699 | $data = @unserialize($data); |
||||
1700 | } |
||||
1701 | if (is_array($data) && isset($data[0], $data[1]) && $data[0] === $name) { |
||||
1702 | $cookies[$name] = Yii::createObject([ |
||||
1703 | 75 | 'class' => 'yii\web\Cookie', |
|||
1704 | 'name' => $name, |
||||
1705 | 'value' => $data[1], |
||||
1706 | 'expire' => null, |
||||
1707 | ]); |
||||
1708 | } |
||||
1709 | } |
||||
1710 | } else { |
||||
1711 | 1 | foreach ($_COOKIE as $name => $value) { |
|||
1712 | 1 | $cookies[$name] = Yii::createObject([ |
|||
1713 | 1 | 'class' => 'yii\web\Cookie', |
|||
1714 | 1 | 'name' => $name, |
|||
1715 | 1 | 'value' => $value, |
|||
1716 | 'expire' => null, |
||||
1717 | ]); |
||||
1718 | } |
||||
1719 | } |
||||
1720 | |||||
1721 | 76 | return $cookies; |
|||
1722 | } |
||||
1723 | |||||
1724 | private $_csrfToken; |
||||
1725 | |||||
1726 | /** |
||||
1727 | * Returns the token used to perform CSRF validation. |
||||
1728 | * |
||||
1729 | * This token is generated in a way to prevent [BREACH attacks](http://breachattack.com/). It may be passed |
||||
1730 | * along via a hidden field of an HTML form or an HTTP header value to support CSRF validation. |
||||
1731 | * @param bool $regenerate whether to regenerate CSRF token. When this parameter is true, each time |
||||
1732 | * this method is called, a new CSRF token will be generated and persisted (in session or cookie). |
||||
1733 | * @return string the token used to perform CSRF validation. |
||||
1734 | */ |
||||
1735 | 83 | public function getCsrfToken($regenerate = false) |
|||
1736 | { |
||||
1737 | 83 | if ($this->_csrfToken === null || $regenerate) { |
|||
1738 | 83 | $token = $this->loadCsrfToken(); |
|||
1739 | 82 | if ($regenerate || empty($token)) { |
|||
1740 | 79 | $token = $this->generateCsrfToken(); |
|||
1741 | } |
||||
1742 | 82 | $this->_csrfToken = Yii::$app->security->maskToken($token); |
|||
1743 | } |
||||
1744 | |||||
1745 | 82 | return $this->_csrfToken; |
|||
1746 | } |
||||
1747 | |||||
1748 | /** |
||||
1749 | * Loads the CSRF token from cookie or session. |
||||
1750 | * @return string the CSRF token loaded from cookie or session. Null is returned if the cookie or session |
||||
1751 | * does not have CSRF token. |
||||
1752 | */ |
||||
1753 | 83 | protected function loadCsrfToken() |
|||
1754 | { |
||||
1755 | 83 | if ($this->enableCsrfCookie) { |
|||
1756 | 79 | return $this->getCookies()->getValue($this->csrfParam); |
|||
1757 | } |
||||
1758 | |||||
1759 | 4 | return Yii::$app->getSession()->get($this->csrfParam); |
|||
1760 | } |
||||
1761 | |||||
1762 | /** |
||||
1763 | * Generates an unmasked random token used to perform CSRF validation. |
||||
1764 | * @return string the random token for CSRF validation. |
||||
1765 | */ |
||||
1766 | 79 | protected function generateCsrfToken() |
|||
1767 | { |
||||
1768 | 79 | $token = Yii::$app->getSecurity()->generateRandomString(); |
|||
1769 | 79 | if ($this->enableCsrfCookie) { |
|||
1770 | 78 | $cookie = $this->createCsrfCookie($token); |
|||
1771 | 78 | Yii::$app->getResponse()->getCookies()->add($cookie); |
|||
1772 | } else { |
||||
1773 | 1 | Yii::$app->getSession()->set($this->csrfParam, $token); |
|||
1774 | } |
||||
1775 | |||||
1776 | 79 | return $token; |
|||
1777 | } |
||||
1778 | |||||
1779 | /** |
||||
1780 | * @return string the CSRF token sent via [[CSRF_HEADER]] by browser. Null is returned if no such header is sent. |
||||
1781 | */ |
||||
1782 | 3 | public function getCsrfTokenFromHeader() |
|||
1783 | { |
||||
1784 | 3 | return $this->headers->get(static::CSRF_HEADER); |
|||
1785 | } |
||||
1786 | |||||
1787 | /** |
||||
1788 | * Creates a cookie with a randomly generated CSRF token. |
||||
1789 | * Initial values specified in [[csrfCookie]] will be applied to the generated cookie. |
||||
1790 | * @param string $token the CSRF token |
||||
1791 | * @return Cookie the generated cookie |
||||
1792 | * @see enableCsrfValidation |
||||
1793 | */ |
||||
1794 | 78 | protected function createCsrfCookie($token) |
|||
1795 | { |
||||
1796 | 78 | $options = $this->csrfCookie; |
|||
1797 | 78 | return Yii::createObject(array_merge($options, [ |
|||
1798 | 78 | 'class' => 'yii\web\Cookie', |
|||
1799 | 78 | 'name' => $this->csrfParam, |
|||
1800 | 78 | 'value' => $token, |
|||
1801 | ])); |
||||
1802 | } |
||||
1803 | |||||
1804 | /** |
||||
1805 | * Performs the CSRF validation. |
||||
1806 | * |
||||
1807 | * This method will validate the user-provided CSRF token by comparing it with the one stored in cookie or session. |
||||
1808 | * This method is mainly called in [[Controller::beforeAction()]]. |
||||
1809 | * |
||||
1810 | * Note that the method will NOT perform CSRF validation if [[enableCsrfValidation]] is false or the HTTP method |
||||
1811 | * is among GET, HEAD or OPTIONS. |
||||
1812 | * |
||||
1813 | * @param string $clientSuppliedToken the user-provided CSRF token to be validated. If null, the token will be retrieved from |
||||
1814 | * the [[csrfParam]] POST field or HTTP header. |
||||
1815 | * This parameter is available since version 2.0.4. |
||||
1816 | * @return bool whether CSRF token is valid. If [[enableCsrfValidation]] is false, this method will return true. |
||||
1817 | */ |
||||
1818 | 8 | public function validateCsrfToken($clientSuppliedToken = null) |
|||
1819 | { |
||||
1820 | 8 | $method = $this->getMethod(); |
|||
1821 | // only validate CSRF token on non-"safe" methods https://tools.ietf.org/html/rfc2616#section-9.1.1 |
||||
1822 | 8 | if (!$this->enableCsrfValidation || in_array($method, ['GET', 'HEAD', 'OPTIONS'], true)) { |
|||
1823 | 7 | return true; |
|||
1824 | } |
||||
1825 | |||||
1826 | 4 | $trueToken = $this->getCsrfToken(); |
|||
1827 | |||||
1828 | 4 | if ($clientSuppliedToken !== null) { |
|||
1829 | 2 | return $this->validateCsrfTokenInternal($clientSuppliedToken, $trueToken); |
|||
1830 | } |
||||
1831 | |||||
1832 | 3 | return $this->validateCsrfTokenInternal($this->getBodyParam($this->csrfParam), $trueToken) |
|||
1833 | 3 | || $this->validateCsrfTokenInternal($this->getCsrfTokenFromHeader(), $trueToken); |
|||
1834 | } |
||||
1835 | |||||
1836 | /** |
||||
1837 | * Validates CSRF token. |
||||
1838 | * |
||||
1839 | * @param string $clientSuppliedToken The masked client-supplied token. |
||||
1840 | * @param string $trueToken The masked true token. |
||||
1841 | * @return bool |
||||
1842 | */ |
||||
1843 | 4 | private function validateCsrfTokenInternal($clientSuppliedToken, $trueToken) |
|||
1844 | { |
||||
1845 | 4 | if (!is_string($clientSuppliedToken)) { |
|||
1846 | 3 | return false; |
|||
1847 | } |
||||
1848 | |||||
1849 | 4 | $security = Yii::$app->security; |
|||
1850 | |||||
1851 | 4 | return $security->compareString($security->unmaskToken($clientSuppliedToken), $security->unmaskToken($trueToken)); |
|||
1852 | } |
||||
1853 | |||||
1854 | /** |
||||
1855 | * Gets first `Forwarded` header value for token |
||||
1856 | * |
||||
1857 | * @param string $token Header token |
||||
1858 | * |
||||
1859 | * @return string|null |
||||
1860 | * |
||||
1861 | * @since 2.0.31 |
||||
1862 | */ |
||||
1863 | 163 | protected function getSecureForwardedHeaderTrustedPart($token) |
|||
1864 | { |
||||
1865 | 163 | $token = strtolower($token); |
|||
1866 | |||||
1867 | 163 | if ($parts = $this->getSecureForwardedHeaderTrustedParts()) { |
|||
1868 | 20 | $lastElement = array_pop($parts); |
|||
1869 | 20 | if ($lastElement && isset($lastElement[$token])) { |
|||
1870 | 20 | return $lastElement[$token]; |
|||
1871 | } |
||||
1872 | } |
||||
1873 | 148 | return null; |
|||
1874 | } |
||||
1875 | |||||
1876 | /** |
||||
1877 | * Gets only trusted `Forwarded` header parts |
||||
1878 | * |
||||
1879 | * @return array |
||||
1880 | * |
||||
1881 | * @since 2.0.31 |
||||
1882 | */ |
||||
1883 | 163 | protected function getSecureForwardedHeaderTrustedParts() |
|||
1884 | { |
||||
1885 | 163 | $validator = $this->getIpValidator(); |
|||
1886 | 163 | $trustedHosts = []; |
|||
1887 | 163 | foreach ($this->trustedHosts as $trustedCidr => $trustedCidrOrHeaders) { |
|||
1888 | 74 | if (!is_array($trustedCidrOrHeaders)) { |
|||
1889 | 72 | $trustedCidr = $trustedCidrOrHeaders; |
|||
1890 | } |
||||
1891 | 74 | $trustedHosts[] = $trustedCidr; |
|||
1892 | } |
||||
1893 | 163 | $validator->setRanges($trustedHosts); |
|||
1894 | |||||
1895 | return array_filter($this->getSecureForwardedHeaderParts(), function ($headerPart) use ($validator) { |
||||
1896 | 20 | return isset($headerPart['for']) ? !$validator->validate($headerPart['for']) : true; |
|||
1897 | 163 | }); |
|||
1898 | } |
||||
1899 | |||||
1900 | private $_secureForwardedHeaderParts; |
||||
1901 | |||||
1902 | /** |
||||
1903 | * Returns decoded forwarded header |
||||
1904 | * |
||||
1905 | * @return array |
||||
1906 | * |
||||
1907 | * @since 2.0.31 |
||||
1908 | */ |
||||
1909 | 163 | protected function getSecureForwardedHeaderParts() |
|||
1910 | { |
||||
1911 | 163 | if ($this->_secureForwardedHeaderParts !== null) { |
|||
1912 | 83 | return $this->_secureForwardedHeaderParts; |
|||
1913 | } |
||||
1914 | 163 | if (count(preg_grep('/^forwarded$/i', $this->secureHeaders)) === 0) { |
|||
1915 | 106 | return $this->_secureForwardedHeaderParts = []; |
|||
1916 | } |
||||
1917 | /* |
||||
1918 | * First header is always correct, because proxy CAN add headers |
||||
1919 | * after last one is found. |
||||
1920 | * Keep in mind that it is NOT enforced, therefore we cannot be |
||||
1921 | * sure, that this is really a first one. |
||||
1922 | * |
||||
1923 | * FPM keeps last header sent which is a bug. You need to merge |
||||
1924 | * headers together on your web server before letting FPM handle it |
||||
1925 | * @see https://bugs.php.net/bug.php?id=78844 |
||||
1926 | */ |
||||
1927 | 57 | $forwarded = $this->headers->get('Forwarded', ''); |
|||
1928 | 57 | if ($forwarded === '') { |
|||
1929 | 37 | return $this->_secureForwardedHeaderParts = []; |
|||
1930 | } |
||||
1931 | |||||
1932 | 20 | preg_match_all('/(?:[^",]++|"[^"]++")+/', $forwarded, $forwardedElements); |
|||
1933 | |||||
1934 | 20 | foreach ($forwardedElements[0] as $forwardedPairs) { |
|||
1935 | 20 | preg_match_all('/(?P<key>\w+)\s*=\s*(?:(?P<value>[^",;]*[^",;\s])|"(?P<value2>[^"]+)")/', $forwardedPairs, |
|||
1936 | 20 | $matches, PREG_SET_ORDER); |
|||
1937 | 20 | $this->_secureForwardedHeaderParts[] = array_reduce($matches, function ($carry, $item) { |
|||
1938 | 20 | $value = $item['value']; |
|||
1939 | 20 | if (isset($item['value2']) && $item['value2'] !== '') { |
|||
1940 | 4 | $value = $item['value2']; |
|||
1941 | } |
||||
1942 | 20 | $carry[strtolower($item['key'])] = $value; |
|||
1943 | 20 | return $carry; |
|||
1944 | 20 | }, []); |
|||
1945 | } |
||||
1946 | 20 | return $this->_secureForwardedHeaderParts; |
|||
1947 | } |
||||
1948 | } |
||||
1949 |