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