Complex classes like Request often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
95 | class Request extends \yii\base\Request |
||
96 | { |
||
97 | /** |
||
98 | * The name of the HTTP header for sending CSRF token. |
||
99 | */ |
||
100 | const CSRF_HEADER = 'X-CSRF-Token'; |
||
101 | /** |
||
102 | * The length of the CSRF token mask. |
||
103 | * @deprecated 2.0.12 The mask length is now equal to the token length. |
||
104 | */ |
||
105 | const CSRF_MASK_LENGTH = 8; |
||
106 | |||
107 | /** |
||
108 | * @var bool whether to enable CSRF (Cross-Site Request Forgery) validation. Defaults to true. |
||
109 | * When CSRF validation is enabled, forms submitted to an Yii Web application must be originated |
||
110 | * from the same application. If not, a 400 HTTP exception will be raised. |
||
111 | * |
||
112 | * Note, this feature requires that the user client accepts cookie. Also, to use this feature, |
||
113 | * forms submitted via POST method must contain a hidden input whose name is specified by [[csrfParam]]. |
||
114 | * You may use [[\yii\helpers\Html::beginForm()]] to generate his hidden input. |
||
115 | * |
||
116 | * In JavaScript, you may get the values of [[csrfParam]] and [[csrfToken]] via `yii.getCsrfParam()` and |
||
117 | * `yii.getCsrfToken()`, respectively. The [[\yii\web\YiiAsset]] asset must be registered. |
||
118 | * You also need to include CSRF meta tags in your pages by using [[\yii\helpers\Html::csrfMetaTags()]]. |
||
119 | * |
||
120 | * @see Controller::enableCsrfValidation |
||
121 | * @see http://en.wikipedia.org/wiki/Cross-site_request_forgery |
||
122 | */ |
||
123 | public $enableCsrfValidation = true; |
||
124 | /** |
||
125 | * @var string the name of the token used to prevent CSRF. Defaults to '_csrf'. |
||
126 | * This property is used only when [[enableCsrfValidation]] is true. |
||
127 | */ |
||
128 | public $csrfParam = '_csrf'; |
||
129 | /** |
||
130 | * @var array the configuration for creating the CSRF [[Cookie|cookie]]. This property is used only when |
||
131 | * both [[enableCsrfValidation]] and [[enableCsrfCookie]] are true. |
||
132 | */ |
||
133 | public $csrfCookie = ['httpOnly' => true]; |
||
134 | /** |
||
135 | * @var bool whether to use cookie to persist CSRF token. If false, CSRF token will be stored |
||
136 | * in session under the name of [[csrfParam]]. Note that while storing CSRF tokens in session increases |
||
137 | * security, it requires starting a session for every page, which will degrade your site performance. |
||
138 | */ |
||
139 | public $enableCsrfCookie = true; |
||
140 | /** |
||
141 | * @var bool whether cookies should be validated to ensure they are not tampered. Defaults to true. |
||
142 | */ |
||
143 | public $enableCookieValidation = true; |
||
144 | /** |
||
145 | * @var string a secret key used for cookie validation. This property must be set if [[enableCookieValidation]] is true. |
||
146 | */ |
||
147 | public $cookieValidationKey; |
||
148 | /** |
||
149 | * @var string the name of the POST parameter that is used to indicate if a request is a PUT, PATCH or DELETE |
||
150 | * request tunneled through POST. Defaults to '_method'. |
||
151 | * @see getMethod() |
||
152 | * @see getBodyParams() |
||
153 | */ |
||
154 | public $methodParam = '_method'; |
||
155 | /** |
||
156 | * @var array the parsers for converting the raw HTTP request body into [[bodyParams]]. |
||
157 | * The array keys are the request `Content-Types`, and the array values are the |
||
158 | * corresponding configurations for [[Yii::createObject|creating the parser objects]]. |
||
159 | * A parser must implement the [[RequestParserInterface]]. |
||
160 | * |
||
161 | * To enable parsing for JSON requests you can use the [[JsonParser]] class like in the following example: |
||
162 | * |
||
163 | * ``` |
||
164 | * [ |
||
165 | * 'application/json' => 'yii\web\JsonParser', |
||
166 | * ] |
||
167 | * ``` |
||
168 | * |
||
169 | * To register a parser for parsing all request types you can use `'*'` as the array key. |
||
170 | * This one will be used as a fallback in case no other types match. |
||
171 | * |
||
172 | * @see getBodyParams() |
||
173 | */ |
||
174 | public $parsers = []; |
||
175 | /** |
||
176 | * @var array the configuration for trusted security related headers. |
||
177 | * |
||
178 | * An array key is an IPv4 or IPv6 IP address in CIDR notation for matching a client. |
||
179 | * |
||
180 | * An array value is a list of headers to trust. These will be matched against |
||
181 | * [[secureHeaders]] to determine which headers are allowed to be sent by a specified host. |
||
182 | * The case of the header names must be the same as specified in [[secureHeaders]]. |
||
183 | * |
||
184 | * For example, to trust all headers listed in [[secureHeaders]] for IP addresses |
||
185 | * in range `192.168.0.0-192.168.0.254` write the following: |
||
186 | * |
||
187 | * ```php |
||
188 | * [ |
||
189 | * '192.168.0.0/24', |
||
190 | * ] |
||
191 | * ``` |
||
192 | * |
||
193 | * To trust just the `X-Forwarded-For` header from `10.0.0.1`, use: |
||
194 | * |
||
195 | * ``` |
||
196 | * [ |
||
197 | * '10.0.0.1' => ['X-Forwarded-For'] |
||
198 | * ] |
||
199 | * ``` |
||
200 | * |
||
201 | * Default is to trust all headers except those listed in [[secureHeaders]] from all hosts. |
||
202 | * Matches are tried in order and searching is stopped when IP matches. |
||
203 | * |
||
204 | * > Info: Matching is performed using [[IpValidator]]. |
||
205 | * See [[IpValidator::::setRanges()|IpValidator::setRanges()]] |
||
206 | * and [[IpValidator::networks]] for advanced matching. |
||
207 | * |
||
208 | * @see $secureHeaders |
||
209 | * @since 2.0.13 |
||
210 | */ |
||
211 | public $trustedHosts = []; |
||
212 | /** |
||
213 | * @var array lists of headers that are, by default, subject to the trusted host configuration. |
||
214 | * These headers will be filtered unless explicitly allowed in [[trustedHosts]]. |
||
215 | * The match of header names is case-insensitive. |
||
216 | * @see https://en.wikipedia.org/wiki/List_of_HTTP_header_fields |
||
217 | * @see $trustedHosts |
||
218 | * @since 2.0.13 |
||
219 | */ |
||
220 | public $secureHeaders = [ |
||
221 | 'X-Forwarded-For', |
||
222 | 'X-Forwarded-Host', |
||
223 | 'X-Forwarded-Proto', |
||
224 | 'Front-End-Https', |
||
225 | 'X-Rewrite-Url', |
||
226 | ]; |
||
227 | /** |
||
228 | * @var string[] List of headers where proxies store the real client IP. |
||
229 | * It's not advisable to put insecure headers here. |
||
230 | * The match of header names is case-insensitive. |
||
231 | * @see $trustedHosts |
||
232 | * @see $secureHeaders |
||
233 | * @since 2.0.13 |
||
234 | */ |
||
235 | public $ipHeaders = [ |
||
236 | 'X-Forwarded-For', |
||
237 | ]; |
||
238 | /** |
||
239 | * @var array list of headers to check for determining whether the connection is made via HTTPS. |
||
240 | * The array keys are header names and the array value is a list of header values that indicate a secure connection. |
||
241 | * The match of header names and values is case-insensitive. |
||
242 | * It's not advisable to put insecure headers here. |
||
243 | * @see $trustedHosts |
||
244 | * @see $secureHeaders |
||
245 | * @since 2.0.13 |
||
246 | */ |
||
247 | public $secureProtocolHeaders = [ |
||
248 | 'X-Forwarded-Proto' => ['https'], |
||
249 | 'Front-End-Https' => ['on'], |
||
250 | ]; |
||
251 | |||
252 | /** |
||
253 | * @var CookieCollection Collection of request cookies. |
||
254 | */ |
||
255 | private $_cookies; |
||
256 | /** |
||
257 | * @var HeaderCollection Collection of request headers. |
||
258 | */ |
||
259 | private $_headers; |
||
260 | |||
261 | |||
262 | /** |
||
263 | * Resolves the current request into a route and the associated parameters. |
||
264 | * @return array the first element is the route, and the second is the associated parameters. |
||
265 | * @throws NotFoundHttpException if the request cannot be resolved. |
||
266 | */ |
||
267 | 1 | public function resolve() |
|
268 | { |
||
269 | 1 | $result = Yii::$app->getUrlManager()->parseRequest($this); |
|
270 | 1 | if ($result !== false) { |
|
271 | 1 | list($route, $params) = $result; |
|
272 | 1 | if ($this->_queryParams === null) { |
|
273 | 1 | $_GET = $params + $_GET; // preserve numeric keys |
|
274 | } else { |
||
275 | 1 | $this->_queryParams = $params + $this->_queryParams; |
|
276 | } |
||
277 | |||
278 | 1 | return [$route, $this->getQueryParams()]; |
|
279 | } |
||
280 | |||
281 | throw new NotFoundHttpException(Yii::t('yii', 'Page not found.')); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Filters headers according to the [[trustedHosts]]. |
||
286 | * @param HeaderCollection $headerCollection |
||
287 | * @since 2.0.13 |
||
288 | */ |
||
289 | 143 | protected function filterHeaders(HeaderCollection $headerCollection) |
|
290 | { |
||
291 | // do not trust any of the [[secureHeaders]] by default |
||
292 | 143 | $trustedHeaders = []; |
|
293 | |||
294 | // check if the client is a trusted host |
||
295 | 143 | if (!empty($this->trustedHosts)) { |
|
296 | 19 | $validator = $this->getIpValidator(); |
|
297 | 19 | $ip = $this->getRemoteIP(); |
|
298 | 19 | foreach ($this->trustedHosts as $cidr => $headers) { |
|
299 | 19 | if (!is_array($headers)) { |
|
300 | 19 | $cidr = $headers; |
|
301 | 19 | $headers = $this->secureHeaders; |
|
302 | } |
||
303 | 19 | $validator->setRanges($cidr); |
|
304 | 19 | if ($validator->validate($ip)) { |
|
305 | 3 | $trustedHeaders = $headers; |
|
306 | 19 | break; |
|
307 | } |
||
308 | } |
||
309 | } |
||
310 | |||
311 | // filter all secure headers unless they are trusted |
||
312 | 143 | foreach ($this->secureHeaders as $secureHeader) { |
|
313 | 143 | if (!in_array($secureHeader, $trustedHeaders)) { |
|
314 | 143 | $headerCollection->remove($secureHeader); |
|
315 | } |
||
316 | } |
||
317 | 143 | } |
|
318 | |||
319 | /** |
||
320 | * Creates instance of [[IpValidator]]. |
||
321 | * You can override this method to adjust validator or implement different matching strategy. |
||
322 | * |
||
323 | * @return IpValidator |
||
324 | * @since 2.0.13 |
||
325 | */ |
||
326 | 19 | protected function getIpValidator() |
|
327 | { |
||
328 | 19 | return new IpValidator(); |
|
329 | } |
||
330 | |||
331 | /** |
||
332 | * Returns the header collection. |
||
333 | * The header collection contains incoming HTTP headers. |
||
334 | * @return HeaderCollection the header collection |
||
335 | */ |
||
336 | 143 | public function getHeaders() |
|
337 | { |
||
338 | 143 | if ($this->_headers === null) { |
|
339 | 143 | $this->_headers = new HeaderCollection(); |
|
340 | 143 | if (function_exists('getallheaders')) { |
|
341 | $headers = getallheaders(); |
||
342 | foreach ($headers as $name => $value) { |
||
343 | $this->_headers->add($name, $value); |
||
344 | } |
||
345 | 143 | } elseif (function_exists('http_get_request_headers')) { |
|
346 | $headers = http_get_request_headers(); |
||
347 | foreach ($headers as $name => $value) { |
||
348 | $this->_headers->add($name, $value); |
||
349 | } |
||
350 | } else { |
||
351 | 143 | foreach ($_SERVER as $name => $value) { |
|
352 | 140 | if (strncmp($name, 'HTTP_', 5) === 0) { |
|
353 | 37 | $name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))); |
|
354 | 140 | $this->_headers->add($name, $value); |
|
355 | } |
||
356 | } |
||
357 | } |
||
358 | 143 | $this->filterHeaders($this->_headers); |
|
359 | } |
||
360 | |||
361 | 143 | return $this->_headers; |
|
362 | } |
||
363 | |||
364 | /** |
||
365 | * Returns the method of the current request (e.g. GET, POST, HEAD, PUT, PATCH, DELETE). |
||
366 | * @return string request method, such as GET, POST, HEAD, PUT, PATCH, DELETE. |
||
367 | * The value returned is turned into upper case. |
||
368 | */ |
||
369 | 24 | public function getMethod() |
|
370 | { |
||
371 | 24 | if (isset($_POST[$this->methodParam])) { |
|
372 | 4 | return strtoupper($_POST[$this->methodParam]); |
|
373 | } |
||
374 | |||
375 | 21 | if ($this->headers->has('X-Http-Method-Override')) { |
|
376 | 1 | return strtoupper($this->headers->get('X-Http-Method-Override')); |
|
377 | } |
||
378 | |||
379 | 20 | if (isset($_SERVER['REQUEST_METHOD'])) { |
|
380 | 5 | return strtoupper($_SERVER['REQUEST_METHOD']); |
|
381 | } |
||
382 | |||
383 | 16 | return 'GET'; |
|
384 | } |
||
385 | |||
386 | /** |
||
387 | * Returns whether this is a GET request. |
||
388 | * @return bool whether this is a GET request. |
||
389 | */ |
||
390 | 2 | public function getIsGet() |
|
391 | { |
||
392 | 2 | return $this->getMethod() === 'GET'; |
|
393 | } |
||
394 | |||
395 | /** |
||
396 | * Returns whether this is an OPTIONS request. |
||
397 | * @return bool whether this is a OPTIONS request. |
||
398 | */ |
||
399 | 1 | public function getIsOptions() |
|
400 | { |
||
401 | 1 | return $this->getMethod() === 'OPTIONS'; |
|
402 | } |
||
403 | |||
404 | /** |
||
405 | * Returns whether this is a HEAD request. |
||
406 | * @return bool whether this is a HEAD request. |
||
407 | */ |
||
408 | 9 | public function getIsHead() |
|
409 | { |
||
410 | 9 | return $this->getMethod() === 'HEAD'; |
|
411 | } |
||
412 | |||
413 | /** |
||
414 | * Returns whether this is a POST request. |
||
415 | * @return bool whether this is a POST request. |
||
416 | */ |
||
417 | public function getIsPost() |
||
418 | { |
||
419 | return $this->getMethod() === 'POST'; |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Returns whether this is a DELETE request. |
||
424 | * @return bool whether this is a DELETE request. |
||
425 | */ |
||
426 | public function getIsDelete() |
||
427 | { |
||
428 | return $this->getMethod() === 'DELETE'; |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * Returns whether this is a PUT request. |
||
433 | * @return bool whether this is a PUT request. |
||
434 | */ |
||
435 | public function getIsPut() |
||
436 | { |
||
437 | return $this->getMethod() === 'PUT'; |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Returns whether this is a PATCH request. |
||
442 | * @return bool whether this is a PATCH request. |
||
443 | */ |
||
444 | public function getIsPatch() |
||
445 | { |
||
446 | return $this->getMethod() === 'PATCH'; |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * Returns whether this is an AJAX (XMLHttpRequest) request. |
||
451 | * |
||
452 | * Note that jQuery doesn't set the header in case of cross domain |
||
453 | * requests: https://stackoverflow.com/questions/8163703/cross-domain-ajax-doesnt-send-x-requested-with-header |
||
454 | * |
||
455 | * @return bool whether this is an AJAX (XMLHttpRequest) request. |
||
456 | */ |
||
457 | 14 | public function getIsAjax() |
|
458 | { |
||
459 | 14 | return $this->headers->get('X-Requested-With') === 'XMLHttpRequest'; |
|
460 | } |
||
461 | |||
462 | /** |
||
463 | * Returns whether this is a PJAX request. |
||
464 | * @return bool whether this is a PJAX request |
||
465 | */ |
||
466 | 3 | public function getIsPjax() |
|
467 | { |
||
468 | 3 | return $this->getIsAjax() && $this->headers->has('X-Pjax'); |
|
469 | } |
||
470 | |||
471 | /** |
||
472 | * Returns whether this is an Adobe Flash or Flex request. |
||
473 | * @return bool whether this is an Adobe Flash or Adobe Flex request. |
||
474 | */ |
||
475 | public function getIsFlash() |
||
476 | { |
||
477 | $userAgent = $this->headers->get('User-Agent', ''); |
||
478 | return stripos($userAgent, 'Shockwave') !== false |
||
479 | || stripos($userAgent, 'Flash') !== false; |
||
480 | } |
||
481 | |||
482 | private $_rawBody; |
||
483 | |||
484 | /** |
||
485 | * Returns the raw HTTP request body. |
||
486 | * @return string the request body |
||
487 | */ |
||
488 | public function getRawBody() |
||
489 | { |
||
490 | if ($this->_rawBody === null) { |
||
491 | $this->_rawBody = file_get_contents('php://input'); |
||
492 | } |
||
493 | |||
494 | return $this->_rawBody; |
||
495 | } |
||
496 | |||
497 | /** |
||
498 | * Sets the raw HTTP request body, this method is mainly used by test scripts to simulate raw HTTP requests. |
||
499 | * @param string $rawBody the request body |
||
500 | */ |
||
501 | public function setRawBody($rawBody) |
||
505 | |||
506 | private $_bodyParams; |
||
507 | |||
508 | /** |
||
509 | * Returns the request parameters given in the request body. |
||
510 | * |
||
511 | * Request parameters are determined using the parsers configured in [[parsers]] property. |
||
512 | * If no parsers are configured for the current [[contentType]] it uses the PHP function `mb_parse_str()` |
||
513 | * to parse the [[rawBody|request body]]. |
||
514 | * @return array the request parameters given in the request body. |
||
515 | * @throws \yii\base\InvalidConfigException if a registered parser does not implement the [[RequestParserInterface]]. |
||
516 | * @see getMethod() |
||
517 | * @see getBodyParam() |
||
518 | * @see setBodyParams() |
||
519 | */ |
||
520 | 3 | public function getBodyParams() |
|
521 | { |
||
522 | 3 | if ($this->_bodyParams === null) { |
|
523 | 1 | if (isset($_POST[$this->methodParam])) { |
|
524 | 1 | $this->_bodyParams = $_POST; |
|
525 | 1 | unset($this->_bodyParams[$this->methodParam]); |
|
526 | 1 | return $this->_bodyParams; |
|
527 | } |
||
528 | |||
529 | $rawContentType = $this->getContentType(); |
||
530 | if (($pos = strpos($rawContentType, ';')) !== false) { |
||
531 | // e.g. text/html; charset=UTF-8 |
||
532 | $contentType = substr($rawContentType, 0, $pos); |
||
533 | } else { |
||
534 | $contentType = $rawContentType; |
||
535 | } |
||
536 | |||
537 | if (isset($this->parsers[$contentType])) { |
||
538 | $parser = Yii::createObject($this->parsers[$contentType]); |
||
539 | if (!($parser instanceof RequestParserInterface)) { |
||
540 | throw new InvalidConfigException("The '$contentType' request parser is invalid. It must implement the yii\\web\\RequestParserInterface."); |
||
541 | } |
||
542 | $this->_bodyParams = $parser->parse($this->getRawBody(), $rawContentType); |
||
543 | } elseif (isset($this->parsers['*'])) { |
||
544 | $parser = Yii::createObject($this->parsers['*']); |
||
545 | if (!($parser instanceof RequestParserInterface)) { |
||
546 | throw new InvalidConfigException('The fallback request parser is invalid. It must implement the yii\\web\\RequestParserInterface.'); |
||
547 | } |
||
548 | $this->_bodyParams = $parser->parse($this->getRawBody(), $rawContentType); |
||
549 | } elseif ($this->getMethod() === 'POST') { |
||
550 | // PHP has already parsed the body so we have all params in $_POST |
||
551 | $this->_bodyParams = $_POST; |
||
552 | } else { |
||
553 | $this->_bodyParams = []; |
||
554 | mb_parse_str($this->getRawBody(), $this->_bodyParams); |
||
555 | } |
||
556 | } |
||
557 | |||
558 | 3 | return $this->_bodyParams; |
|
559 | } |
||
560 | |||
561 | /** |
||
562 | * Sets the request body parameters. |
||
563 | * @param array $values the request body parameters (name-value pairs) |
||
564 | * @see getBodyParam() |
||
565 | * @see getBodyParams() |
||
566 | */ |
||
567 | 2 | public function setBodyParams($values) |
|
571 | |||
572 | /** |
||
573 | * Returns the named request body parameter value. |
||
574 | * If the parameter does not exist, the second parameter passed to this method will be returned. |
||
575 | * @param string $name the parameter name |
||
576 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
577 | * @return mixed the parameter value |
||
578 | * @see getBodyParams() |
||
579 | * @see setBodyParams() |
||
580 | */ |
||
581 | 3 | public function getBodyParam($name, $defaultValue = null) |
|
587 | |||
588 | /** |
||
589 | * Returns POST parameter with a given name. If name isn't specified, returns an array of all POST parameters. |
||
590 | * |
||
591 | * @param string $name the parameter name |
||
592 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
593 | * @return array|mixed |
||
594 | */ |
||
595 | public function post($name = null, $defaultValue = null) |
||
603 | |||
604 | private $_queryParams; |
||
605 | |||
606 | /** |
||
607 | * Returns the request parameters given in the [[queryString]]. |
||
608 | * |
||
609 | * This method will return the contents of `$_GET` if params where not explicitly set. |
||
610 | * @return array the request GET parameter values. |
||
611 | * @see setQueryParams() |
||
612 | */ |
||
613 | 31 | public function getQueryParams() |
|
621 | |||
622 | /** |
||
623 | * Sets the request [[queryString]] parameters. |
||
624 | * @param array $values the request query parameters (name-value pairs) |
||
625 | * @see getQueryParam() |
||
626 | * @see getQueryParams() |
||
627 | */ |
||
628 | 7 | public function setQueryParams($values) |
|
632 | |||
633 | /** |
||
634 | * Returns GET parameter with a given name. If name isn't specified, returns an array of all GET parameters. |
||
635 | * |
||
636 | * @param string $name the parameter name |
||
637 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
638 | * @return array|mixed |
||
639 | */ |
||
640 | 19 | public function get($name = null, $defaultValue = null) |
|
648 | |||
649 | /** |
||
650 | * Returns the named GET parameter value. |
||
651 | * If the GET parameter does not exist, the second parameter passed to this method will be returned. |
||
652 | * @param string $name the GET parameter name. |
||
653 | * @param mixed $defaultValue the default parameter value if the GET parameter does not exist. |
||
654 | * @return mixed the GET parameter value |
||
655 | * @see getBodyParam() |
||
656 | */ |
||
657 | 22 | public function getQueryParam($name, $defaultValue = null) |
|
663 | |||
664 | private $_hostInfo; |
||
665 | private $_hostName; |
||
666 | |||
667 | /** |
||
668 | * Returns the schema and host part of the current request URL. |
||
669 | * |
||
670 | * The returned URL does not have an ending slash. |
||
671 | * |
||
672 | * By default this value is based on the user request information. This method will |
||
673 | * return the value of `$_SERVER['HTTP_HOST']` if it is available or `$_SERVER['SERVER_NAME']` if not. |
||
674 | * You may want to check out the [PHP documentation](http://php.net/manual/en/reserved.variables.server.php) |
||
675 | * for more information on these variables. |
||
676 | * |
||
677 | * You may explicitly specify it by setting the [[setHostInfo()|hostInfo]] property. |
||
678 | * |
||
679 | * > Warning: Dependent on the server configuration this information may not be |
||
680 | * > reliable and [may be faked by the user sending the HTTP request](https://www.acunetix.com/vulnerabilities/web/host-header-attack). |
||
681 | * > If the webserver is configured to serve the same site independent of the value of |
||
682 | * > the `Host` header, this value is not reliable. In such situations you should either |
||
683 | * > fix your webserver configuration or explicitly set the value by setting the [[setHostInfo()|hostInfo]] property. |
||
684 | * > If you don't have access to the server configuration, you can setup [[\yii\filters\HostControl]] filter at |
||
685 | * > application level in order to protect against such kind of attack. |
||
686 | * |
||
687 | * @property string|null schema and hostname part (with port number if needed) of the request URL |
||
688 | * (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set. |
||
689 | * See [[getHostInfo()]] for security related notes on this property. |
||
690 | * @return string|null schema and hostname part (with port number if needed) of the request URL |
||
691 | * (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set. |
||
692 | * @see setHostInfo() |
||
693 | */ |
||
694 | 24 | public function getHostInfo() |
|
712 | |||
713 | /** |
||
714 | * Sets the schema and host part of the application URL. |
||
715 | * This setter is provided in case the schema and hostname cannot be determined |
||
716 | * on certain Web servers. |
||
717 | * @param string|null $value the schema and host part of the application URL. The trailing slashes will be removed. |
||
718 | * @see getHostInfo() for security related notes on this property. |
||
719 | */ |
||
720 | 57 | public function setHostInfo($value) |
|
725 | |||
726 | /** |
||
727 | * Returns the host part of the current request URL. |
||
728 | * Value is calculated from current [[getHostInfo()|hostInfo]] property. |
||
729 | * |
||
730 | * > Warning: The content of this value may not be reliable, dependent on the server |
||
731 | * > configuration. Please refer to [[getHostInfo()]] for more information. |
||
732 | * |
||
733 | * @return string|null hostname part of the request URL (e.g. `www.yiiframework.com`) |
||
734 | * @see getHostInfo() |
||
735 | * @since 2.0.10 |
||
736 | */ |
||
737 | 11 | public function getHostName() |
|
745 | |||
746 | private $_baseUrl; |
||
747 | |||
748 | /** |
||
749 | * Returns the relative URL for the application. |
||
750 | * This is similar to [[scriptUrl]] except that it does not include the script file name, |
||
751 | * and the ending slashes are removed. |
||
752 | * @return string the relative URL for the application |
||
753 | * @see setScriptUrl() |
||
754 | */ |
||
755 | 290 | public function getBaseUrl() |
|
763 | |||
764 | /** |
||
765 | * Sets the relative URL for the application. |
||
766 | * By default the URL is determined based on the entry script URL. |
||
767 | * This setter is provided in case you want to change this behavior. |
||
768 | * @param string $value the relative URL for the application |
||
769 | */ |
||
770 | 1 | public function setBaseUrl($value) |
|
774 | |||
775 | private $_scriptUrl; |
||
776 | |||
777 | /** |
||
778 | * Returns the relative URL of the entry script. |
||
779 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
780 | * @return string the relative URL of the entry script. |
||
781 | * @throws InvalidConfigException if unable to determine the entry script URL |
||
782 | */ |
||
783 | 291 | public function getScriptUrl() |
|
784 | { |
||
785 | 291 | if ($this->_scriptUrl === null) { |
|
786 | 1 | $scriptFile = $this->getScriptFile(); |
|
787 | $scriptName = basename($scriptFile); |
||
788 | if (isset($_SERVER['SCRIPT_NAME']) && basename($_SERVER['SCRIPT_NAME']) === $scriptName) { |
||
789 | $this->_scriptUrl = $_SERVER['SCRIPT_NAME']; |
||
790 | } elseif (isset($_SERVER['PHP_SELF']) && basename($_SERVER['PHP_SELF']) === $scriptName) { |
||
791 | $this->_scriptUrl = $_SERVER['PHP_SELF']; |
||
792 | } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $scriptName) { |
||
793 | $this->_scriptUrl = $_SERVER['ORIG_SCRIPT_NAME']; |
||
794 | } elseif (isset($_SERVER['PHP_SELF']) && ($pos = strpos($_SERVER['PHP_SELF'], '/' . $scriptName)) !== false) { |
||
795 | $this->_scriptUrl = substr($_SERVER['SCRIPT_NAME'], 0, $pos) . '/' . $scriptName; |
||
796 | } elseif (!empty($_SERVER['DOCUMENT_ROOT']) && strpos($scriptFile, $_SERVER['DOCUMENT_ROOT']) === 0) { |
||
797 | $this->_scriptUrl = str_replace('\\', '/', str_replace($_SERVER['DOCUMENT_ROOT'], '', $scriptFile)); |
||
798 | } else { |
||
799 | throw new InvalidConfigException('Unable to determine the entry script URL.'); |
||
800 | } |
||
801 | } |
||
802 | |||
803 | 290 | return $this->_scriptUrl; |
|
804 | } |
||
805 | |||
806 | /** |
||
807 | * Sets the relative URL for the application entry script. |
||
808 | * This setter is provided in case the entry script URL cannot be determined |
||
809 | * on certain Web servers. |
||
810 | * @param string $value the relative URL for the application entry script. |
||
811 | */ |
||
812 | 301 | public function setScriptUrl($value) |
|
816 | |||
817 | private $_scriptFile; |
||
|
|||
818 | |||
819 | /** |
||
820 | * Returns the entry script file path. |
||
821 | * The default implementation will simply return `$_SERVER['SCRIPT_FILENAME']`. |
||
822 | * @return string the entry script file path |
||
823 | * @throws InvalidConfigException |
||
824 | */ |
||
825 | 292 | public function getScriptFile() |
|
837 | |||
838 | /** |
||
839 | * Sets the entry script file path. |
||
840 | * The entry script file path normally can be obtained from `$_SERVER['SCRIPT_FILENAME']`. |
||
841 | * If your server configuration does not return the correct value, you may configure |
||
842 | * this property to make it right. |
||
843 | * @param string $value the entry script file path. |
||
844 | */ |
||
845 | 270 | public function setScriptFile($value) |
|
849 | |||
850 | private $_pathInfo; |
||
851 | |||
852 | /** |
||
853 | * Returns the path info of the currently requested URL. |
||
854 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||
855 | * The starting and ending slashes are both removed. |
||
856 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
857 | * Note, the returned path info is already URL-decoded. |
||
858 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
859 | */ |
||
860 | 18 | public function getPathInfo() |
|
868 | |||
869 | /** |
||
870 | * Sets the path info of the current request. |
||
871 | * This method is mainly provided for testing purpose. |
||
872 | * @param string $value the path info of the current request |
||
873 | */ |
||
874 | 19 | public function setPathInfo($value) |
|
878 | |||
879 | /** |
||
880 | * Resolves the path info part of the currently requested URL. |
||
881 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||
882 | * The starting slashes are both removed (ending slashes will be kept). |
||
883 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
884 | * Note, the returned path info is decoded. |
||
885 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
886 | */ |
||
887 | protected function resolvePathInfo() |
||
931 | |||
932 | /** |
||
933 | * Returns the currently requested absolute URL. |
||
934 | * This is a shortcut to the concatenation of [[hostInfo]] and [[url]]. |
||
935 | * @return string the currently requested absolute URL. |
||
936 | */ |
||
937 | public function getAbsoluteUrl() |
||
941 | |||
942 | private $_url; |
||
943 | |||
944 | /** |
||
945 | * Returns the currently requested relative URL. |
||
946 | * This refers to the portion of the URL that is after the [[hostInfo]] part. |
||
947 | * It includes the [[queryString]] part if any. |
||
948 | * @return string the currently requested relative URL. Note that the URI returned may be URL-encoded depending on the client. |
||
949 | * @throws InvalidConfigException if the URL cannot be determined due to unusual server configuration |
||
950 | */ |
||
951 | 11 | public function getUrl() |
|
959 | |||
960 | /** |
||
961 | * Sets the currently requested relative URL. |
||
962 | * The URI must refer to the portion that is after [[hostInfo]]. |
||
963 | * Note that the URI should be URL-encoded. |
||
964 | * @param string $value the request URI to be set |
||
965 | */ |
||
966 | 24 | public function setUrl($value) |
|
970 | |||
971 | /** |
||
972 | * Resolves the request URI portion for the currently requested URL. |
||
973 | * This refers to the portion that is after the [[hostInfo]] part. It includes the [[queryString]] part if any. |
||
974 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
975 | * @return string|bool the request URI portion for the currently requested URL. |
||
976 | * Note that the URI returned may be URL-encoded depending on the client. |
||
977 | * @throws InvalidConfigException if the request URI cannot be determined due to unusual server configuration |
||
978 | */ |
||
979 | 3 | protected function resolveRequestUri() |
|
999 | |||
1000 | /** |
||
1001 | * Returns part of the request URL that is after the question mark. |
||
1002 | * @return string part of the request URL that is after the question mark |
||
1003 | */ |
||
1004 | public function getQueryString() |
||
1008 | |||
1009 | /** |
||
1010 | * Return if the request is sent via secure channel (https). |
||
1011 | * @return bool if the request is sent via secure channel (https) |
||
1012 | */ |
||
1013 | 37 | public function getIsSecureConnection() |
|
1014 | { |
||
1015 | 37 | if (isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1)) { |
|
1016 | 2 | return true; |
|
1017 | } |
||
1018 | 35 | foreach ($this->secureProtocolHeaders as $header => $values) { |
|
1019 | 35 | if (($headerValue = $this->headers->get($header, null)) !== null) { |
|
1020 | 2 | foreach ($values as $value) { |
|
1021 | 2 | if (strcasecmp($headerValue, $value) === 0) { |
|
1022 | 35 | return true; |
|
1023 | } |
||
1024 | } |
||
1025 | } |
||
1030 | |||
1031 | /** |
||
1032 | * Returns the server name. |
||
1033 | * @return string server name, null if not available |
||
1034 | */ |
||
1035 | 1 | public function getServerName() |
|
1039 | |||
1040 | /** |
||
1041 | * Returns the server port number. |
||
1042 | * @return int|null server port number, null if not available |
||
1043 | */ |
||
1044 | 1 | public function getServerPort() |
|
1048 | |||
1049 | /** |
||
1050 | * Returns the URL referrer. |
||
1051 | * @return string|null URL referrer, null if not available |
||
1052 | */ |
||
1053 | public function getReferrer() |
||
1057 | |||
1058 | /** |
||
1059 | * Returns the URL origin of a CORS request. |
||
1060 | * |
||
1061 | * The return value is taken from the `Origin` [[getHeaders()|header]] sent by the browser. |
||
1062 | * |
||
1063 | * Note that the origin request header indicates where a fetch originates from. |
||
1064 | * It doesn't include any path information, but only the server name. |
||
1065 | * It is sent with a CORS requests, as well as with POST requests. |
||
1066 | * It is similar to the referer header, but, unlike this header, it doesn't disclose the whole path. |
||
1067 | * Please refer to <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin> for more information. |
||
1068 | * |
||
1069 | * @return string|null URL origin of a CORS request, `null` if not available. |
||
1070 | * @see getHeaders() |
||
1071 | * @since 2.0.13 |
||
1072 | */ |
||
1073 | 1 | public function getOrigin() |
|
1077 | |||
1078 | /** |
||
1079 | * Returns the user agent. |
||
1080 | * @return string|null user agent, null if not available |
||
1081 | */ |
||
1082 | public function getUserAgent() |
||
1086 | |||
1087 | /** |
||
1088 | * Returns the user IP address. |
||
1089 | * The IP is determined using headers and / or `$_SERVER` variables. |
||
1090 | * @return string|null user IP address, null if not available |
||
1091 | */ |
||
1092 | 52 | public function getUserIP() |
|
1102 | |||
1103 | /** |
||
1104 | * Returns the user host name. |
||
1105 | * The HOST is determined using headers and / or `$_SERVER` variables. |
||
1106 | * @return string|null user host name, null if not available |
||
1107 | */ |
||
1108 | public function getUserHost() |
||
1118 | |||
1119 | /** |
||
1120 | * Returns the IP on the other end of this connection. |
||
1121 | * This is always the next hop, any headers are ignored. |
||
1122 | * @return string|null remote IP address, `null` if not available. |
||
1123 | * @since 2.0.13 |
||
1124 | */ |
||
1125 | 67 | public function getRemoteIP() |
|
1129 | |||
1130 | /** |
||
1131 | * Returns the host name of the other end of this connection. |
||
1132 | * This is always the next hop, any headers are ignored. |
||
1133 | * @return string|null remote host name, `null` if not available |
||
1134 | * @see getUserHost() |
||
1135 | * @see getRemoteIP() |
||
1136 | * @since 2.0.13 |
||
1137 | */ |
||
1138 | public function getRemoteHost() |
||
1142 | |||
1143 | /** |
||
1144 | * @return string|null the username sent via HTTP authentication, `null` if the username is not given |
||
1145 | * @see getAuthCredentials() to get both username and password in one call |
||
1146 | */ |
||
1147 | 9 | public function getAuthUser() |
|
1151 | |||
1152 | /** |
||
1153 | * @return string|null the password sent via HTTP authentication, `null` if the password is not given |
||
1154 | * @see getAuthCredentials() to get both username and password in one call |
||
1155 | */ |
||
1156 | 9 | public function getAuthPassword() |
|
1160 | |||
1161 | /** |
||
1162 | * @return array that contains exactly two elements: |
||
1163 | * - 0: the username sent via HTTP authentication, `null` if the username is not given |
||
1164 | * - 1: the password sent via HTTP authentication, `null` if the password is not given |
||
1165 | * @see getAuthUser() to get only username |
||
1166 | * @see getAuthPassword() to get only password |
||
1167 | * @since 2.0.13 |
||
1168 | */ |
||
1169 | 29 | public function getAuthCredentials() |
|
1198 | |||
1199 | private $_port; |
||
1200 | |||
1201 | /** |
||
1202 | * Returns the port to use for insecure requests. |
||
1203 | * Defaults to 80, or the port specified by the server if the current |
||
1204 | * request is insecure. |
||
1205 | * @return int port number for insecure requests. |
||
1206 | * @see setPort() |
||
1207 | */ |
||
1208 | public function getPort() |
||
1216 | |||
1217 | /** |
||
1218 | * Sets the port to use for insecure requests. |
||
1219 | * This setter is provided in case a custom port is necessary for certain |
||
1220 | * server configurations. |
||
1221 | * @param int $value port number. |
||
1222 | */ |
||
1223 | public function setPort($value) |
||
1230 | |||
1231 | private $_securePort; |
||
1232 | |||
1233 | /** |
||
1234 | * Returns the port to use for secure requests. |
||
1235 | * Defaults to 443, or the port specified by the server if the current |
||
1236 | * request is secure. |
||
1237 | * @return int port number for secure requests. |
||
1238 | * @see setSecurePort() |
||
1239 | */ |
||
1240 | public function getSecurePort() |
||
1248 | |||
1249 | /** |
||
1250 | * Sets the port to use for secure requests. |
||
1251 | * This setter is provided in case a custom port is necessary for certain |
||
1252 | * server configurations. |
||
1253 | * @param int $value port number. |
||
1254 | */ |
||
1255 | public function setSecurePort($value) |
||
1262 | |||
1263 | private $_contentTypes; |
||
1264 | |||
1265 | /** |
||
1266 | * Returns the content types acceptable by the end user. |
||
1267 | * |
||
1268 | * This is determined by the `Accept` HTTP header. For example, |
||
1269 | * |
||
1270 | * ```php |
||
1271 | * $_SERVER['HTTP_ACCEPT'] = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
1272 | * $types = $request->getAcceptableContentTypes(); |
||
1273 | * print_r($types); |
||
1274 | * // displays: |
||
1275 | * // [ |
||
1276 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||
1277 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||
1278 | * // 'text/plain' => ['q' => 0.5], |
||
1279 | * // ] |
||
1280 | * ``` |
||
1281 | * |
||
1282 | * @return array the content types ordered by the quality score. Types with the highest scores |
||
1283 | * will be returned first. The array keys are the content types, while the array values |
||
1284 | * are the corresponding quality score and other parameters as given in the header. |
||
1285 | */ |
||
1286 | 2 | public function getAcceptableContentTypes() |
|
1298 | |||
1299 | /** |
||
1300 | * Sets the acceptable content types. |
||
1301 | * Please refer to [[getAcceptableContentTypes()]] on the format of the parameter. |
||
1302 | * @param array $value the content types that are acceptable by the end user. They should |
||
1303 | * be ordered by the preference level. |
||
1304 | * @see getAcceptableContentTypes() |
||
1305 | * @see parseAcceptHeader() |
||
1306 | */ |
||
1307 | public function setAcceptableContentTypes($value) |
||
1311 | |||
1312 | /** |
||
1313 | * Returns request content-type |
||
1314 | * The Content-Type header field indicates the MIME type of the data |
||
1315 | * contained in [[getRawBody()]] or, in the case of the HEAD method, the |
||
1316 | * media type that would have been sent had the request been a GET. |
||
1317 | * For the MIME-types the user expects in response, see [[acceptableContentTypes]]. |
||
1318 | * @return string request content-type. Null is returned if this information is not available. |
||
1319 | * @link https://tools.ietf.org/html/rfc2616#section-14.17 |
||
1320 | * HTTP 1.1 header field definitions |
||
1321 | */ |
||
1322 | public function getContentType() |
||
1331 | |||
1332 | private $_languages; |
||
1333 | |||
1334 | /** |
||
1335 | * Returns the languages acceptable by the end user. |
||
1336 | * This is determined by the `Accept-Language` HTTP header. |
||
1337 | * @return array the languages ordered by the preference level. The first element |
||
1338 | * represents the most preferred language. |
||
1339 | */ |
||
1340 | 1 | public function getAcceptableLanguages() |
|
1352 | |||
1353 | /** |
||
1354 | * @param array $value the languages that are acceptable by the end user. They should |
||
1355 | * be ordered by the preference level. |
||
1356 | */ |
||
1357 | 1 | public function setAcceptableLanguages($value) |
|
1361 | |||
1362 | /** |
||
1363 | * Parses the given `Accept` (or `Accept-Language`) header. |
||
1364 | * |
||
1365 | * This method will return the acceptable values with their quality scores and the corresponding parameters |
||
1366 | * as specified in the given `Accept` header. The array keys of the return value are the acceptable values, |
||
1367 | * while the array values consisting of the corresponding quality scores and parameters. The acceptable |
||
1368 | * values with the highest quality scores will be returned first. For example, |
||
1369 | * |
||
1370 | * ```php |
||
1371 | * $header = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
1372 | * $accepts = $request->parseAcceptHeader($header); |
||
1373 | * print_r($accepts); |
||
1374 | * // displays: |
||
1375 | * // [ |
||
1376 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||
1377 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||
1378 | * // 'text/plain' => ['q' => 0.5], |
||
1379 | * // ] |
||
1380 | * ``` |
||
1381 | * |
||
1382 | * @param string $header the header to be parsed |
||
1383 | * @return array the acceptable values ordered by their quality score. The values with the highest scores |
||
1384 | * will be returned first. |
||
1385 | */ |
||
1386 | 3 | public function parseAcceptHeader($header) |
|
1453 | |||
1454 | /** |
||
1455 | * Returns the user-preferred language that should be used by this application. |
||
1456 | * The language resolution is based on the user preferred languages and the languages |
||
1457 | * supported by the application. The method will try to find the best match. |
||
1458 | * @param array $languages a list of the languages supported by the application. If this is empty, the current |
||
1459 | * application language will be returned without further processing. |
||
1460 | * @return string the language that the application should use. |
||
1461 | */ |
||
1462 | 1 | public function getPreferredLanguage(array $languages = []) |
|
1484 | |||
1485 | /** |
||
1486 | * Gets the Etags. |
||
1487 | * |
||
1488 | * @return array The entity tags |
||
1489 | */ |
||
1490 | public function getETags() |
||
1498 | |||
1499 | /** |
||
1500 | * Returns the cookie collection. |
||
1501 | * |
||
1502 | * Through the returned cookie collection, you may access a cookie using the following syntax: |
||
1503 | * |
||
1504 | * ```php |
||
1505 | * $cookie = $request->cookies['name'] |
||
1506 | * if ($cookie !== null) { |
||
1507 | * $value = $cookie->value; |
||
1508 | * } |
||
1509 | * |
||
1510 | * // alternatively |
||
1511 | * $value = $request->cookies->getValue('name'); |
||
1512 | * ``` |
||
1513 | * |
||
1514 | * @return CookieCollection the cookie collection. |
||
1515 | */ |
||
1516 | 35 | public function getCookies() |
|
1526 | |||
1527 | /** |
||
1528 | * Converts `$_COOKIE` into an array of [[Cookie]]. |
||
1529 | * @return array the cookies obtained from request |
||
1530 | * @throws InvalidConfigException if [[cookieValidationKey]] is not set when [[enableCookieValidation]] is true |
||
1531 | */ |
||
1532 | 35 | protected function loadCookies() |
|
1570 | |||
1571 | private $_csrfToken; |
||
1572 | |||
1573 | /** |
||
1574 | * Returns the token used to perform CSRF validation. |
||
1575 | * |
||
1576 | * This token is generated in a way to prevent [BREACH attacks](http://breachattack.com/). It may be passed |
||
1577 | * along via a hidden field of an HTML form or an HTTP header value to support CSRF validation. |
||
1578 | * @param bool $regenerate whether to regenerate CSRF token. When this parameter is true, each time |
||
1579 | * this method is called, a new CSRF token will be generated and persisted (in session or cookie). |
||
1580 | * @return string the token used to perform CSRF validation. |
||
1581 | */ |
||
1582 | 39 | public function getCsrfToken($regenerate = false) |
|
1594 | |||
1595 | /** |
||
1596 | * Loads the CSRF token from cookie or session. |
||
1597 | * @return string the CSRF token loaded from cookie or session. Null is returned if the cookie or session |
||
1598 | * does not have CSRF token. |
||
1599 | */ |
||
1600 | 39 | protected function loadCsrfToken() |
|
1608 | |||
1609 | /** |
||
1610 | * Generates an unmasked random token used to perform CSRF validation. |
||
1611 | * @return string the random token for CSRF validation. |
||
1612 | */ |
||
1613 | 36 | protected function generateCsrfToken() |
|
1625 | |||
1626 | /** |
||
1627 | * @return string the CSRF token sent via [[CSRF_HEADER]] by browser. Null is returned if no such header is sent. |
||
1628 | */ |
||
1629 | 3 | public function getCsrfTokenFromHeader() |
|
1633 | |||
1634 | /** |
||
1635 | * Creates a cookie with a randomly generated CSRF token. |
||
1636 | * Initial values specified in [[csrfCookie]] will be applied to the generated cookie. |
||
1637 | * @param string $token the CSRF token |
||
1638 | * @return Cookie the generated cookie |
||
1639 | * @see enableCsrfValidation |
||
1640 | */ |
||
1641 | 34 | protected function createCsrfCookie($token) |
|
1650 | |||
1651 | /** |
||
1652 | * Performs the CSRF validation. |
||
1653 | * |
||
1654 | * This method will validate the user-provided CSRF token by comparing it with the one stored in cookie or session. |
||
1655 | * This method is mainly called in [[Controller::beforeAction()]]. |
||
1656 | * |
||
1657 | * Note that the method will NOT perform CSRF validation if [[enableCsrfValidation]] is false or the HTTP method |
||
1658 | * is among GET, HEAD or OPTIONS. |
||
1659 | * |
||
1660 | * @param string $clientSuppliedToken the user-provided CSRF token to be validated. If null, the token will be retrieved from |
||
1661 | * the [[csrfParam]] POST field or HTTP header. |
||
1662 | * This parameter is available since version 2.0.4. |
||
1663 | * @return bool whether CSRF token is valid. If [[enableCsrfValidation]] is false, this method will return true. |
||
1664 | */ |
||
1665 | 6 | public function validateCsrfToken($clientSuppliedToken = null) |
|
1682 | |||
1683 | /** |
||
1684 | * Validates CSRF token. |
||
1685 | * |
||
1686 | * @param string $clientSuppliedToken The masked client-supplied token. |
||
1687 | * @param string $trueToken The masked true token. |
||
1688 | * @return bool |
||
1689 | */ |
||
1690 | 4 | private function validateCsrfTokenInternal($clientSuppliedToken, $trueToken) |
|
1700 | } |
||
1701 |