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