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 |
||
97 | class Request extends \yii\base\Request implements RequestInterface |
||
98 | { |
||
99 | use MessageTrait; |
||
100 | |||
101 | /** |
||
102 | * The name of the HTTP header for sending CSRF token. |
||
103 | */ |
||
104 | const CSRF_HEADER = 'X-CSRF-Token'; |
||
105 | /** |
||
106 | * The length of the CSRF token mask. |
||
107 | * @deprecated 2.0.12 The mask length is now equal to the token length. |
||
108 | */ |
||
109 | const CSRF_MASK_LENGTH = 8; |
||
110 | |||
111 | /** |
||
112 | * @var bool whether to enable CSRF (Cross-Site Request Forgery) validation. Defaults to true. |
||
113 | * When CSRF validation is enabled, forms submitted to an Yii Web application must be originated |
||
114 | * from the same application. If not, a 400 HTTP exception will be raised. |
||
115 | * |
||
116 | * Note, this feature requires that the user client accepts cookie. Also, to use this feature, |
||
117 | * forms submitted via POST method must contain a hidden input whose name is specified by [[csrfParam]]. |
||
118 | * You may use [[\yii\helpers\Html::beginForm()]] to generate his hidden input. |
||
119 | * |
||
120 | * In JavaScript, you may get the values of [[csrfParam]] and [[csrfToken]] via `yii.getCsrfParam()` and |
||
121 | * `yii.getCsrfToken()`, respectively. The [[\yii\web\YiiAsset]] asset must be registered. |
||
122 | * You also need to include CSRF meta tags in your pages by using [[\yii\helpers\Html::csrfMetaTags()]]. |
||
123 | * |
||
124 | * @see Controller::enableCsrfValidation |
||
125 | * @see http://en.wikipedia.org/wiki/Cross-site_request_forgery |
||
126 | */ |
||
127 | public $enableCsrfValidation = true; |
||
128 | /** |
||
129 | * @var string the name of the token used to prevent CSRF. Defaults to '_csrf'. |
||
130 | * This property is used only when [[enableCsrfValidation]] is true. |
||
131 | */ |
||
132 | public $csrfParam = '_csrf'; |
||
133 | /** |
||
134 | * @var array the configuration for creating the CSRF [[Cookie|cookie]]. This property is used only when |
||
135 | * both [[enableCsrfValidation]] and [[enableCsrfCookie]] are true. |
||
136 | */ |
||
137 | public $csrfCookie = ['httpOnly' => true]; |
||
138 | /** |
||
139 | * @var bool whether to use cookie to persist CSRF token. If false, CSRF token will be stored |
||
140 | * in session under the name of [[csrfParam]]. Note that while storing CSRF tokens in session increases |
||
141 | * security, it requires starting a session for every page, which will degrade your site performance. |
||
142 | */ |
||
143 | public $enableCsrfCookie = true; |
||
144 | /** |
||
145 | * @var bool whether cookies should be validated to ensure they are not tampered. Defaults to true. |
||
146 | */ |
||
147 | public $enableCookieValidation = true; |
||
148 | /** |
||
149 | * @var string a secret key used for cookie validation. This property must be set if [[enableCookieValidation]] is true. |
||
150 | */ |
||
151 | public $cookieValidationKey; |
||
152 | /** |
||
153 | * @var string the name of the POST parameter that is used to indicate if a request is a PUT, PATCH or DELETE |
||
154 | * request tunneled through POST. Defaults to '_method'. |
||
155 | * @see getMethod() |
||
156 | * @see getBodyParams() |
||
157 | */ |
||
158 | public $methodParam = '_method'; |
||
159 | /** |
||
160 | * @var array the parsers for converting the raw HTTP request body into [[bodyParams]]. |
||
161 | * The array keys are the request `Content-Types`, and the array values are the |
||
162 | * corresponding configurations for [[Yii::createObject|creating the parser objects]]. |
||
163 | * A parser must implement the [[RequestParserInterface]]. |
||
164 | * |
||
165 | * To enable parsing for JSON requests you can use the [[JsonParser]] class like in the following example: |
||
166 | * |
||
167 | * ``` |
||
168 | * [ |
||
169 | * 'application/json' => \yii\web\JsonParser::class, |
||
170 | * ] |
||
171 | * ``` |
||
172 | * |
||
173 | * To register a parser for parsing all request types you can use `'*'` as the array key. |
||
174 | * This one will be used as a fallback in case no other types match. |
||
175 | * |
||
176 | * @see getBodyParams() |
||
177 | */ |
||
178 | public $parsers = []; |
||
179 | |||
180 | /** |
||
181 | * @var CookieCollection Collection of request cookies. |
||
182 | */ |
||
183 | private $_cookies; |
||
184 | /** |
||
185 | * @var string the HTTP method of the request. |
||
186 | */ |
||
187 | private $_method; |
||
188 | /** |
||
189 | * @var UriInterface the URI instance associated with request. |
||
190 | */ |
||
191 | private $_uri; |
||
192 | /** |
||
193 | * @var mixed the message's request target. |
||
194 | */ |
||
195 | private $_requestTarget; |
||
196 | |||
197 | |||
198 | /** |
||
199 | * Resolves the current request into a route and the associated parameters. |
||
200 | * @return array the first element is the route, and the second is the associated parameters. |
||
201 | * @throws NotFoundHttpException if the request cannot be resolved. |
||
202 | */ |
||
203 | 1 | public function resolve() |
|
218 | |||
219 | /** |
||
220 | * Returns default message's headers, which should be present once [[headerCollection]] is instantiated. |
||
221 | * @return string[][] an associative array of the message's headers. |
||
222 | */ |
||
223 | 11 | protected function defaultHeaders() |
|
245 | |||
246 | /** |
||
247 | * {@inheritdoc} |
||
248 | * @since 2.1.0 |
||
249 | */ |
||
250 | public function getRequestTarget() |
||
257 | |||
258 | /** |
||
259 | * Specifies the message's request target |
||
260 | * @param mixed $requestTarget the message's request target. |
||
261 | * @since 2.1.0 |
||
262 | */ |
||
263 | public function setRequestTarget($requestTarget) |
||
267 | |||
268 | /** |
||
269 | * {@inheritdoc} |
||
270 | * @since 2.1.0 |
||
271 | */ |
||
272 | public function withRequestTarget($requestTarget) |
||
282 | |||
283 | /** |
||
284 | * {@inheritdoc} |
||
285 | */ |
||
286 | 21 | public function getMethod() |
|
301 | |||
302 | /** |
||
303 | * Specifies request HTTP method. |
||
304 | * @param string $method case-sensitive HTTP method. |
||
305 | * @since 2.1.0 |
||
306 | */ |
||
307 | 6 | public function setMethod($method) |
|
311 | |||
312 | /** |
||
313 | * {@inheritdoc} |
||
314 | * @since 2.1.0 |
||
315 | */ |
||
316 | public function withMethod($method) |
||
317 | { |
||
318 | if ($this->getMethod() === $method) { |
||
319 | return $this; |
||
320 | } |
||
321 | |||
322 | $newInstance = clone $this; |
||
323 | $newInstance->setMethod($method); |
||
324 | return $newInstance; |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | * @since 2.1.0 |
||
330 | */ |
||
331 | public function getUri() |
||
346 | |||
347 | /** |
||
348 | * Specifies the URI instance. |
||
349 | * @param UriInterface|\Closure|array $uri URI instance or its DI compatible configuration. |
||
350 | * @since 2.1.0 |
||
351 | */ |
||
352 | public function setUri($uri) |
||
356 | |||
357 | /** |
||
358 | * {@inheritdoc} |
||
359 | * @since 2.1.0 |
||
360 | */ |
||
361 | public function withUri(UriInterface $uri, $preserveHost = false) |
||
375 | |||
376 | /** |
||
377 | * Returns whether this is a GET request. |
||
378 | * @return bool whether this is a GET request. |
||
379 | */ |
||
380 | 2 | public function getIsGet() |
|
384 | |||
385 | /** |
||
386 | * Returns whether this is an OPTIONS request. |
||
387 | * @return bool whether this is a OPTIONS request. |
||
388 | */ |
||
389 | public function getIsOptions() |
||
393 | |||
394 | /** |
||
395 | * Returns whether this is a HEAD request. |
||
396 | * @return bool whether this is a HEAD request. |
||
397 | */ |
||
398 | 9 | public function getIsHead() |
|
402 | |||
403 | /** |
||
404 | * Returns whether this is a POST request. |
||
405 | * @return bool whether this is a POST request. |
||
406 | */ |
||
407 | public function getIsPost() |
||
411 | |||
412 | /** |
||
413 | * Returns whether this is a DELETE request. |
||
414 | * @return bool whether this is a DELETE request. |
||
415 | */ |
||
416 | public function getIsDelete() |
||
420 | |||
421 | /** |
||
422 | * Returns whether this is a PUT request. |
||
423 | * @return bool whether this is a PUT request. |
||
424 | */ |
||
425 | public function getIsPut() |
||
429 | |||
430 | /** |
||
431 | * Returns whether this is a PATCH request. |
||
432 | * @return bool whether this is a PATCH request. |
||
433 | */ |
||
434 | public function getIsPatch() |
||
438 | |||
439 | /** |
||
440 | * Returns whether this is an AJAX (XMLHttpRequest) request. |
||
441 | * |
||
442 | * Note that jQuery doesn't set the header in case of cross domain |
||
443 | * requests: https://stackoverflow.com/questions/8163703/cross-domain-ajax-doesnt-send-x-requested-with-header |
||
444 | * |
||
445 | * @return bool whether this is an AJAX (XMLHttpRequest) request. |
||
446 | */ |
||
447 | 10 | public function getIsAjax() |
|
451 | |||
452 | /** |
||
453 | * Returns whether this is a PJAX request |
||
454 | * @return bool whether this is a PJAX request |
||
455 | */ |
||
456 | 1 | public function getIsPjax() |
|
460 | |||
461 | /** |
||
462 | * Returns whether this is an Adobe Flash or Flex request. |
||
463 | * @return bool whether this is an Adobe Flash or Adobe Flex request. |
||
464 | */ |
||
465 | public function getIsFlash() |
||
470 | |||
471 | /** |
||
472 | * Returns default message body to be used in case it is not explicitly set. |
||
473 | * @return StreamInterface default body instance. |
||
474 | */ |
||
475 | protected function defaultBody() |
||
482 | |||
483 | private $_rawBody; |
||
484 | |||
485 | /** |
||
486 | * Returns the raw HTTP request body. |
||
487 | * @return string the request body |
||
488 | */ |
||
489 | public function getRawBody() |
||
497 | |||
498 | /** |
||
499 | * Sets the raw HTTP request body, this method is mainly used by test scripts to simulate raw HTTP requests. |
||
500 | * @param string $rawBody the request body |
||
501 | */ |
||
502 | public function setRawBody($rawBody) |
||
506 | |||
507 | private $_bodyParams; |
||
508 | |||
509 | /** |
||
510 | * Returns the request parameters given in the request body. |
||
511 | * |
||
512 | * Request parameters are determined using the parsers configured in [[parsers]] property. |
||
513 | * If no parsers are configured for the current [[contentType]] it uses the PHP function `mb_parse_str()` |
||
514 | * to parse the [[rawBody|request body]]. |
||
515 | * @return array the request parameters given in the request body. |
||
516 | * @throws \yii\base\InvalidConfigException if a registered parser does not implement the [[RequestParserInterface]]. |
||
517 | * @see getMethod() |
||
518 | * @see getBodyParam() |
||
519 | * @see setBodyParams() |
||
520 | */ |
||
521 | 3 | public function getBodyParams() |
|
561 | |||
562 | /** |
||
563 | * Sets the request body parameters. |
||
564 | * @param array $values the request body parameters (name-value pairs) |
||
565 | * @see getBodyParam() |
||
566 | * @see getBodyParams() |
||
567 | */ |
||
568 | 2 | public function setBodyParams($values) |
|
572 | |||
573 | /** |
||
574 | * Returns the named request body parameter value. |
||
575 | * If the parameter does not exist, the second parameter passed to this method will be returned. |
||
576 | * @param string $name the parameter name |
||
577 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
578 | * @return mixed the parameter value |
||
579 | * @see getBodyParams() |
||
580 | * @see setBodyParams() |
||
581 | */ |
||
582 | 3 | public function getBodyParam($name, $defaultValue = null) |
|
588 | |||
589 | /** |
||
590 | * Returns POST parameter with a given name. If name isn't specified, returns an array of all POST parameters. |
||
591 | * |
||
592 | * @param string $name the parameter name |
||
593 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
594 | * @return array|mixed |
||
595 | */ |
||
596 | public function post($name = null, $defaultValue = null) |
||
604 | |||
605 | private $_queryParams; |
||
606 | |||
607 | /** |
||
608 | * Returns the request parameters given in the [[queryString]]. |
||
609 | * |
||
610 | * This method will return the contents of `$_GET` if params where not explicitly set. |
||
611 | * @return array the request GET parameter values. |
||
612 | * @see setQueryParams() |
||
613 | */ |
||
614 | 29 | public function getQueryParams() |
|
622 | |||
623 | /** |
||
624 | * Sets the request [[queryString]] parameters. |
||
625 | * @param array $values the request query parameters (name-value pairs) |
||
626 | * @see getQueryParam() |
||
627 | * @see getQueryParams() |
||
628 | */ |
||
629 | 8 | public function setQueryParams($values) |
|
633 | |||
634 | /** |
||
635 | * Returns GET parameter with a given name. If name isn't specified, returns an array of all GET parameters. |
||
636 | * |
||
637 | * @param string $name the parameter name |
||
638 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
639 | * @return array|mixed |
||
640 | */ |
||
641 | 15 | public function get($name = null, $defaultValue = null) |
|
649 | |||
650 | /** |
||
651 | * Returns the named GET parameter value. |
||
652 | * If the GET parameter does not exist, the second parameter passed to this method will be returned. |
||
653 | * @param string $name the GET parameter name. |
||
654 | * @param mixed $defaultValue the default parameter value if the GET parameter does not exist. |
||
655 | * @return mixed the GET parameter value |
||
656 | * @see getBodyParam() |
||
657 | */ |
||
658 | 20 | public function getQueryParam($name, $defaultValue = null) |
|
664 | |||
665 | private $_hostInfo; |
||
666 | private $_hostName; |
||
667 | |||
668 | /** |
||
669 | * Returns the schema and host part of the current request URL. |
||
670 | * |
||
671 | * The returned URL does not have an ending slash. |
||
672 | * |
||
673 | * By default this value is based on the user request information. This method will |
||
674 | * return the value of `$_SERVER['HTTP_HOST']` if it is available or `$_SERVER['SERVER_NAME']` if not. |
||
675 | * You may want to check out the [PHP documentation](http://php.net/manual/en/reserved.variables.server.php) |
||
676 | * for more information on these variables. |
||
677 | * |
||
678 | * You may explicitly specify it by setting the [[setHostInfo()|hostInfo]] property. |
||
679 | * |
||
680 | * > Warning: Dependent on the server configuration this information may not be |
||
681 | * > reliable and [may be faked by the user sending the HTTP request](https://www.acunetix.com/vulnerabilities/web/host-header-attack). |
||
682 | * > If the webserver is configured to serve the same site independent of the value of |
||
683 | * > the `Host` header, this value is not reliable. In such situations you should either |
||
684 | * > fix your webserver configuration or explicitly set the value by setting the [[setHostInfo()|hostInfo]] property. |
||
685 | * > If you don't have access to the server configuration, you can setup [[\yii\filters\HostControl]] filter at |
||
686 | * > application level in order to protect against such kind of attack. |
||
687 | * |
||
688 | * @property string|null schema and hostname part (with port number if needed) of the request URL |
||
689 | * (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set. |
||
690 | * See [[getHostInfo()]] for security related notes on this property. |
||
691 | * @return string|null schema and hostname part (with port number if needed) of the request URL |
||
692 | * (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set. |
||
693 | * @see setHostInfo() |
||
694 | */ |
||
695 | 24 | public function getHostInfo() |
|
713 | |||
714 | /** |
||
715 | * Sets the schema and host part of the application URL. |
||
716 | * This setter is provided in case the schema and hostname cannot be determined |
||
717 | * on certain Web servers. |
||
718 | * @param string|null $value the schema and host part of the application URL. The trailing slashes will be removed. |
||
719 | * @see getHostInfo() for security related notes on this property. |
||
720 | */ |
||
721 | 57 | public function setHostInfo($value) |
|
726 | |||
727 | /** |
||
728 | * Returns the host part of the current request URL. |
||
729 | * Value is calculated from current [[getHostInfo()|hostInfo]] property. |
||
730 | * |
||
731 | * > Warning: The content of this value may not be reliable, dependent on the server |
||
732 | * > configuration. Please refer to [[getHostInfo()]] for more information. |
||
733 | * |
||
734 | * @return string|null hostname part of the request URL (e.g. `www.yiiframework.com`) |
||
735 | * @see getHostInfo() |
||
736 | * @since 2.0.10 |
||
737 | */ |
||
738 | 11 | public function getHostName() |
|
746 | |||
747 | private $_baseUrl; |
||
748 | |||
749 | /** |
||
750 | * Returns the relative URL for the application. |
||
751 | * This is similar to [[scriptUrl]] except that it does not include the script file name, |
||
752 | * and the ending slashes are removed. |
||
753 | * @return string the relative URL for the application |
||
754 | * @see setScriptUrl() |
||
755 | */ |
||
756 | 253 | public function getBaseUrl() |
|
764 | |||
765 | /** |
||
766 | * Sets the relative URL for the application. |
||
767 | * By default the URL is determined based on the entry script URL. |
||
768 | * This setter is provided in case you want to change this behavior. |
||
769 | * @param string $value the relative URL for the application |
||
770 | */ |
||
771 | 1 | public function setBaseUrl($value) |
|
775 | |||
776 | private $_scriptUrl; |
||
777 | |||
778 | /** |
||
779 | * Returns the relative URL of the entry script. |
||
780 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
781 | * @return string the relative URL of the entry script. |
||
782 | * @throws InvalidConfigException if unable to determine the entry script URL |
||
783 | */ |
||
784 | 254 | public function getScriptUrl() |
|
785 | { |
||
786 | 254 | if ($this->_scriptUrl === null) { |
|
787 | 2 | $scriptFile = $this->getScriptFile(); |
|
788 | 1 | $scriptName = basename($scriptFile); |
|
789 | 1 | if (isset($_SERVER['SCRIPT_NAME']) && basename($_SERVER['SCRIPT_NAME']) === $scriptName) { |
|
790 | 1 | $this->_scriptUrl = $_SERVER['SCRIPT_NAME']; |
|
791 | } elseif (isset($_SERVER['PHP_SELF']) && basename($_SERVER['PHP_SELF']) === $scriptName) { |
||
792 | $this->_scriptUrl = $_SERVER['PHP_SELF']; |
||
793 | } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $scriptName) { |
||
794 | $this->_scriptUrl = $_SERVER['ORIG_SCRIPT_NAME']; |
||
795 | } elseif (isset($_SERVER['PHP_SELF']) && ($pos = strpos($_SERVER['PHP_SELF'], '/' . $scriptName)) !== false) { |
||
796 | $this->_scriptUrl = substr($_SERVER['SCRIPT_NAME'], 0, $pos) . '/' . $scriptName; |
||
797 | } elseif (!empty($_SERVER['DOCUMENT_ROOT']) && strpos($scriptFile, $_SERVER['DOCUMENT_ROOT']) === 0) { |
||
798 | $this->_scriptUrl = str_replace('\\', '/', str_replace($_SERVER['DOCUMENT_ROOT'], '', $scriptFile)); |
||
799 | } else { |
||
800 | throw new InvalidConfigException('Unable to determine the entry script URL.'); |
||
801 | } |
||
802 | } |
||
803 | |||
804 | 253 | return $this->_scriptUrl; |
|
805 | } |
||
806 | |||
807 | /** |
||
808 | * Sets the relative URL for the application entry script. |
||
809 | * This setter is provided in case the entry script URL cannot be determined |
||
810 | * on certain Web servers. |
||
811 | * @param string $value the relative URL for the application entry script. |
||
812 | */ |
||
813 | 264 | public function setScriptUrl($value) |
|
817 | |||
818 | private $_scriptFile; |
||
819 | |||
820 | /** |
||
821 | * Returns the entry script file path. |
||
822 | * The default implementation will simply return `$_SERVER['SCRIPT_FILENAME']`. |
||
823 | * @return string the entry script file path |
||
824 | * @throws InvalidConfigException |
||
825 | */ |
||
826 | 255 | public function getScriptFile() |
|
838 | |||
839 | /** |
||
840 | * Sets the entry script file path. |
||
841 | * The entry script file path normally can be obtained from `$_SERVER['SCRIPT_FILENAME']`. |
||
842 | * If your server configuration does not return the correct value, you may configure |
||
843 | * this property to make it right. |
||
844 | * @param string $value the entry script file path. |
||
845 | */ |
||
846 | 233 | public function setScriptFile($value) |
|
850 | |||
851 | private $_pathInfo; |
||
852 | |||
853 | /** |
||
854 | * Returns the path info of the currently requested URL. |
||
855 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||
856 | * The starting and ending slashes are both removed. |
||
857 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
858 | * Note, the returned path info is already URL-decoded. |
||
859 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
860 | */ |
||
861 | 18 | public function getPathInfo() |
|
869 | |||
870 | /** |
||
871 | * Sets the path info of the current request. |
||
872 | * This method is mainly provided for testing purpose. |
||
873 | * @param string $value the path info of the current request |
||
874 | */ |
||
875 | 19 | public function setPathInfo($value) |
|
879 | |||
880 | /** |
||
881 | * Resolves the path info part of the currently requested URL. |
||
882 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||
883 | * The starting slashes are both removed (ending slashes will be kept). |
||
884 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
885 | * Note, the returned path info is decoded. |
||
886 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
887 | */ |
||
888 | protected function resolvePathInfo() |
||
932 | |||
933 | /** |
||
934 | * Returns the currently requested absolute URL. |
||
935 | * This is a shortcut to the concatenation of [[hostInfo]] and [[url]]. |
||
936 | * @return string the currently requested absolute URL. |
||
937 | */ |
||
938 | public function getAbsoluteUrl() |
||
942 | |||
943 | private $_url; |
||
944 | |||
945 | /** |
||
946 | * Returns the currently requested relative URL. |
||
947 | * This refers to the portion of the URL that is after the [[hostInfo]] part. |
||
948 | * It includes the [[queryString]] part if any. |
||
949 | * @return string the currently requested relative URL. Note that the URI returned may be URL-encoded depending on the client. |
||
950 | * @throws InvalidConfigException if the URL cannot be determined due to unusual server configuration |
||
951 | */ |
||
952 | 11 | public function getUrl() |
|
960 | |||
961 | /** |
||
962 | * Sets the currently requested relative URL. |
||
963 | * The URI must refer to the portion that is after [[hostInfo]]. |
||
964 | * Note that the URI should be URL-encoded. |
||
965 | * @param string $value the request URI to be set |
||
966 | */ |
||
967 | 24 | public function setUrl($value) |
|
971 | |||
972 | /** |
||
973 | * Resolves the request URI portion for the currently requested URL. |
||
974 | * This refers to the portion that is after the [[hostInfo]] part. It includes the [[queryString]] part if any. |
||
975 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
976 | * @return string|bool the request URI portion for the currently requested URL. |
||
977 | * Note that the URI returned may be URL-encoded depending on the client. |
||
978 | * @throws InvalidConfigException if the request URI cannot be determined due to unusual server configuration |
||
979 | */ |
||
980 | 3 | protected function resolveRequestUri() |
|
1000 | |||
1001 | /** |
||
1002 | * Returns part of the request URL that is after the question mark. |
||
1003 | * @return string part of the request URL that is after the question mark |
||
1004 | */ |
||
1005 | public function getQueryString() |
||
1009 | |||
1010 | /** |
||
1011 | * Return if the request is sent via secure channel (https). |
||
1012 | * @return bool if the request is sent via secure channel (https) |
||
1013 | */ |
||
1014 | 20 | public function getIsSecureConnection() |
|
1019 | |||
1020 | /** |
||
1021 | * Returns the server name. |
||
1022 | * @return string server name, null if not available |
||
1023 | */ |
||
1024 | 1 | public function getServerName() |
|
1028 | |||
1029 | /** |
||
1030 | * Returns the server port number. |
||
1031 | * @return int|null server port number, null if not available |
||
1032 | */ |
||
1033 | 1 | public function getServerPort() |
|
1037 | |||
1038 | /** |
||
1039 | * Returns the URL referrer. |
||
1040 | * @return string|null URL referrer, null if not available |
||
1041 | */ |
||
1042 | public function getReferrer() |
||
1046 | |||
1047 | /** |
||
1048 | * Returns the URL origin of a CORS request. |
||
1049 | * |
||
1050 | * The return value is taken from the `Origin` [[getHeaders()|header]] sent by the browser. |
||
1051 | * |
||
1052 | * Note that the origin request header indicates where a fetch originates from. |
||
1053 | * It doesn't include any path information, but only the server name. |
||
1054 | * It is sent with a CORS requests, as well as with POST requests. |
||
1055 | * It is similar to the referer header, but, unlike this header, it doesn't disclose the whole path. |
||
1056 | * Please refer to <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin> for more information. |
||
1057 | * |
||
1058 | * @return string|null URL origin of a CORS request, `null` if not available. |
||
1059 | * @see getHeaders() |
||
1060 | * @since 2.0.13 |
||
1061 | */ |
||
1062 | 1 | public function getOrigin() |
|
1066 | |||
1067 | /** |
||
1068 | * Returns the user agent. |
||
1069 | * @return string|null user agent, null if not available |
||
1070 | */ |
||
1071 | public function getUserAgent() |
||
1075 | |||
1076 | /** |
||
1077 | * Returns the user IP address. |
||
1078 | * @return string|null user IP address, null if not available |
||
1079 | */ |
||
1080 | 27 | public function getUserIP() |
|
1084 | |||
1085 | /** |
||
1086 | * Returns the user host name. |
||
1087 | * @return string|null user host name, null if not available |
||
1088 | */ |
||
1089 | public function getUserHost() |
||
1093 | |||
1094 | /** |
||
1095 | * @return string|null the username sent via HTTP authentication, null if the username is not given |
||
1096 | */ |
||
1097 | 10 | public function getAuthUser() |
|
1101 | |||
1102 | /** |
||
1103 | * @return string|null the password sent via HTTP authentication, null if the password is not given |
||
1104 | */ |
||
1105 | 10 | public function getAuthPassword() |
|
1109 | |||
1110 | private $_port; |
||
1111 | |||
1112 | /** |
||
1113 | * Returns the port to use for insecure requests. |
||
1114 | * Defaults to 80, or the port specified by the server if the current |
||
1115 | * request is insecure. |
||
1116 | * @return int port number for insecure requests. |
||
1117 | * @see setPort() |
||
1118 | */ |
||
1119 | public function getPort() |
||
1127 | |||
1128 | /** |
||
1129 | * Sets the port to use for insecure requests. |
||
1130 | * This setter is provided in case a custom port is necessary for certain |
||
1131 | * server configurations. |
||
1132 | * @param int $value port number. |
||
1133 | */ |
||
1134 | public function setPort($value) |
||
1141 | |||
1142 | private $_securePort; |
||
1143 | |||
1144 | /** |
||
1145 | * Returns the port to use for secure requests. |
||
1146 | * Defaults to 443, or the port specified by the server if the current |
||
1147 | * request is secure. |
||
1148 | * @return int port number for secure requests. |
||
1149 | * @see setSecurePort() |
||
1150 | */ |
||
1151 | public function getSecurePort() |
||
1159 | |||
1160 | /** |
||
1161 | * Sets the port to use for secure requests. |
||
1162 | * This setter is provided in case a custom port is necessary for certain |
||
1163 | * server configurations. |
||
1164 | * @param int $value port number. |
||
1165 | */ |
||
1166 | public function setSecurePort($value) |
||
1173 | |||
1174 | private $_contentTypes; |
||
1175 | |||
1176 | /** |
||
1177 | * Returns the content types acceptable by the end user. |
||
1178 | * This is determined by the `Accept` HTTP header. For example, |
||
1179 | * |
||
1180 | * ```php |
||
1181 | * $_SERVER['HTTP_ACCEPT'] = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
1182 | * $types = $request->getAcceptableContentTypes(); |
||
1183 | * print_r($types); |
||
1184 | * // displays: |
||
1185 | * // [ |
||
1186 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||
1187 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||
1188 | * // 'text/plain' => ['q' => 0.5], |
||
1189 | * // ] |
||
1190 | * ``` |
||
1191 | * |
||
1192 | * @return array the content types ordered by the quality score. Types with the highest scores |
||
1193 | * will be returned first. The array keys are the content types, while the array values |
||
1194 | * are the corresponding quality score and other parameters as given in the header. |
||
1195 | */ |
||
1196 | 3 | public function getAcceptableContentTypes() |
|
1208 | |||
1209 | /** |
||
1210 | * Sets the acceptable content types. |
||
1211 | * Please refer to [[getAcceptableContentTypes()]] on the format of the parameter. |
||
1212 | * @param array $value the content types that are acceptable by the end user. They should |
||
1213 | * be ordered by the preference level. |
||
1214 | * @see getAcceptableContentTypes() |
||
1215 | * @see parseAcceptHeader() |
||
1216 | */ |
||
1217 | 1 | public function setAcceptableContentTypes($value) |
|
1218 | { |
||
1219 | 1 | $this->_contentTypes = $value; |
|
1220 | 1 | } |
|
1221 | |||
1222 | /** |
||
1223 | * Returns request content-type |
||
1224 | * The Content-Type header field indicates the MIME type of the data |
||
1225 | * contained in [[getRawBody()]] or, in the case of the HEAD method, the |
||
1226 | * media type that would have been sent had the request been a GET. |
||
1227 | * For the MIME-types the user expects in response, see [[acceptableContentTypes]]. |
||
1228 | * @return string request content-type. Null is returned if this information is not available. |
||
1229 | * @link https://tools.ietf.org/html/rfc2616#section-14.17 |
||
1230 | * HTTP 1.1 header field definitions |
||
1231 | */ |
||
1232 | 1 | public function getContentType() |
|
1245 | |||
1246 | private $_languages; |
||
1247 | |||
1248 | /** |
||
1249 | * Returns the languages acceptable by the end user. |
||
1250 | * This is determined by the `Accept-Language` HTTP header. |
||
1251 | * @return array the languages ordered by the preference level. The first element |
||
1252 | * represents the most preferred language. |
||
1253 | */ |
||
1254 | 1 | public function getAcceptableLanguages() |
|
1266 | |||
1267 | /** |
||
1268 | * @param array $value the languages that are acceptable by the end user. They should |
||
1269 | * be ordered by the preference level. |
||
1270 | */ |
||
1271 | 1 | public function setAcceptableLanguages($value) |
|
1275 | |||
1276 | /** |
||
1277 | * Parses the given `Accept` (or `Accept-Language`) header. |
||
1278 | * |
||
1279 | * This method will return the acceptable values with their quality scores and the corresponding parameters |
||
1280 | * as specified in the given `Accept` header. The array keys of the return value are the acceptable values, |
||
1281 | * while the array values consisting of the corresponding quality scores and parameters. The acceptable |
||
1282 | * values with the highest quality scores will be returned first. For example, |
||
1283 | * |
||
1284 | * ```php |
||
1285 | * $header = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
1286 | * $accepts = $request->parseAcceptHeader($header); |
||
1287 | * print_r($accepts); |
||
1288 | * // displays: |
||
1289 | * // [ |
||
1290 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||
1291 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||
1292 | * // 'text/plain' => ['q' => 0.5], |
||
1293 | * // ] |
||
1294 | * ``` |
||
1295 | * |
||
1296 | * @param string $header the header to be parsed |
||
1297 | * @return array the acceptable values ordered by their quality score. The values with the highest scores |
||
1298 | * will be returned first. |
||
1299 | */ |
||
1300 | 3 | public function parseAcceptHeader($header) |
|
1367 | |||
1368 | /** |
||
1369 | * Returns the user-preferred language that should be used by this application. |
||
1370 | * The language resolution is based on the user preferred languages and the languages |
||
1371 | * supported by the application. The method will try to find the best match. |
||
1372 | * @param array $languages a list of the languages supported by the application. If this is empty, the current |
||
1373 | * application language will be returned without further processing. |
||
1374 | * @return string the language that the application should use. |
||
1375 | */ |
||
1376 | 1 | public function getPreferredLanguage(array $languages = []) |
|
1398 | |||
1399 | /** |
||
1400 | * Gets the Etags. |
||
1401 | * |
||
1402 | * @return array The entity tags |
||
1403 | */ |
||
1404 | public function getETags() |
||
1412 | |||
1413 | /** |
||
1414 | * Returns the cookie collection. |
||
1415 | * Through the returned cookie collection, you may access a cookie using the following syntax: |
||
1416 | * |
||
1417 | * ```php |
||
1418 | * $cookie = $request->cookies['name'] |
||
1419 | * if ($cookie !== null) { |
||
1420 | * $value = $cookie->value; |
||
1421 | * } |
||
1422 | * |
||
1423 | * // alternatively |
||
1424 | * $value = $request->cookies->getValue('name'); |
||
1425 | * ``` |
||
1426 | * |
||
1427 | * @return CookieCollection the cookie collection. |
||
1428 | */ |
||
1429 | 32 | public function getCookies() |
|
1439 | |||
1440 | /** |
||
1441 | * Converts `$_COOKIE` into an array of [[Cookie]]. |
||
1442 | * @return array the cookies obtained from request |
||
1443 | * @throws InvalidConfigException if [[cookieValidationKey]] is not set when [[enableCookieValidation]] is true |
||
1444 | */ |
||
1445 | 32 | protected function loadCookies() |
|
1481 | |||
1482 | private $_csrfToken; |
||
1483 | |||
1484 | /** |
||
1485 | * Returns the token used to perform CSRF validation. |
||
1486 | * |
||
1487 | * This token is generated in a way to prevent [BREACH attacks](http://breachattack.com/). It may be passed |
||
1488 | * along via a hidden field of an HTML form or an HTTP header value to support CSRF validation. |
||
1489 | * @param bool $regenerate whether to regenerate CSRF token. When this parameter is true, each time |
||
1490 | * this method is called, a new CSRF token will be generated and persisted (in session or cookie). |
||
1491 | * @return string the token used to perform CSRF validation. |
||
1492 | */ |
||
1493 | 35 | public function getCsrfToken($regenerate = false) |
|
1504 | |||
1505 | /** |
||
1506 | * Loads the CSRF token from cookie or session. |
||
1507 | * @return string the CSRF token loaded from cookie or session. Null is returned if the cookie or session |
||
1508 | * does not have CSRF token. |
||
1509 | */ |
||
1510 | 35 | protected function loadCsrfToken() |
|
1517 | |||
1518 | /** |
||
1519 | * Generates an unmasked random token used to perform CSRF validation. |
||
1520 | * @return string the random token for CSRF validation. |
||
1521 | */ |
||
1522 | 34 | protected function generateCsrfToken() |
|
1533 | |||
1534 | /** |
||
1535 | * @return string the CSRF token sent via [[CSRF_HEADER]] by browser. Null is returned if no such header is sent. |
||
1536 | */ |
||
1537 | 3 | public function getCsrfTokenFromHeader() |
|
1541 | |||
1542 | /** |
||
1543 | * Creates a cookie with a randomly generated CSRF token. |
||
1544 | * Initial values specified in [[csrfCookie]] will be applied to the generated cookie. |
||
1545 | * @param string $token the CSRF token |
||
1546 | * @return Cookie the generated cookie |
||
1547 | * @see enableCsrfValidation |
||
1548 | */ |
||
1549 | 32 | protected function createCsrfCookie($token) |
|
1556 | |||
1557 | /** |
||
1558 | * Performs the CSRF validation. |
||
1559 | * |
||
1560 | * This method will validate the user-provided CSRF token by comparing it with the one stored in cookie or session. |
||
1561 | * This method is mainly called in [[Controller::beforeAction()]]. |
||
1562 | * |
||
1563 | * Note that the method will NOT perform CSRF validation if [[enableCsrfValidation]] is false or the HTTP method |
||
1564 | * is among GET, HEAD or OPTIONS. |
||
1565 | * |
||
1566 | * @param string $clientSuppliedToken the user-provided CSRF token to be validated. If null, the token will be retrieved from |
||
1567 | * the [[csrfParam]] POST field or HTTP header. |
||
1568 | * This parameter is available since version 2.0.4. |
||
1569 | * @return bool whether CSRF token is valid. If [[enableCsrfValidation]] is false, this method will return true. |
||
1570 | */ |
||
1571 | 5 | public function validateCsrfToken($clientSuppliedToken = null) |
|
1589 | |||
1590 | /** |
||
1591 | * Validates CSRF token |
||
1592 | * |
||
1593 | * @param string $clientSuppliedToken The masked client-supplied token. |
||
1594 | * @param string $trueToken The masked true token. |
||
1595 | * @return bool |
||
1596 | */ |
||
1597 | 3 | private function validateCsrfTokenInternal($clientSuppliedToken, $trueToken) |
|
1607 | |||
1608 | /** |
||
1609 | * {@inheritdoc} |
||
1610 | */ |
||
1611 | 1 | public function __clone() |
|
1621 | } |
||
1622 |
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.