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 |
||
105 | class Request extends \yii\base\Request implements RequestInterface |
||
106 | { |
||
107 | use MessageTrait; |
||
108 | |||
109 | /** |
||
110 | * The name of the HTTP header for sending CSRF token. |
||
111 | */ |
||
112 | const CSRF_HEADER = 'X-CSRF-Token'; |
||
113 | /** |
||
114 | * The length of the CSRF token mask. |
||
115 | * @deprecated 2.0.12 The mask length is now equal to the token length. |
||
116 | */ |
||
117 | const CSRF_MASK_LENGTH = 8; |
||
118 | |||
119 | /** |
||
120 | * @var bool whether to enable CSRF (Cross-Site Request Forgery) validation. Defaults to true. |
||
121 | * When CSRF validation is enabled, forms submitted to an Yii Web application must be originated |
||
122 | * from the same application. If not, a 400 HTTP exception will be raised. |
||
123 | * |
||
124 | * Note, this feature requires that the user client accepts cookie. Also, to use this feature, |
||
125 | * forms submitted via POST method must contain a hidden input whose name is specified by [[csrfParam]]. |
||
126 | * You may use [[\yii\helpers\Html::beginForm()]] to generate his hidden input. |
||
127 | * |
||
128 | * In JavaScript, you may get the values of [[csrfParam]] and [[csrfToken]] via `yii.getCsrfParam()` and |
||
129 | * `yii.getCsrfToken()`, respectively. The [[\yii\web\YiiAsset]] asset must be registered. |
||
130 | * You also need to include CSRF meta tags in your pages by using [[\yii\helpers\Html::csrfMetaTags()]]. |
||
131 | * |
||
132 | * @see Controller::enableCsrfValidation |
||
133 | * @see http://en.wikipedia.org/wiki/Cross-site_request_forgery |
||
134 | */ |
||
135 | public $enableCsrfValidation = true; |
||
136 | /** |
||
137 | * @var string the name of the token used to prevent CSRF. Defaults to '_csrf'. |
||
138 | * This property is used only when [[enableCsrfValidation]] is true. |
||
139 | */ |
||
140 | public $csrfParam = '_csrf'; |
||
141 | /** |
||
142 | * @var array the configuration for creating the CSRF [[Cookie|cookie]]. This property is used only when |
||
143 | * both [[enableCsrfValidation]] and [[enableCsrfCookie]] are true. |
||
144 | */ |
||
145 | public $csrfCookie = ['httpOnly' => true]; |
||
146 | /** |
||
147 | * @var bool whether to use cookie to persist CSRF token. If false, CSRF token will be stored |
||
148 | * in session under the name of [[csrfParam]]. Note that while storing CSRF tokens in session increases |
||
149 | * security, it requires starting a session for every page, which will degrade your site performance. |
||
150 | */ |
||
151 | public $enableCsrfCookie = true; |
||
152 | /** |
||
153 | * @var bool whether cookies should be validated to ensure they are not tampered. Defaults to true. |
||
154 | */ |
||
155 | public $enableCookieValidation = true; |
||
156 | /** |
||
157 | * @var string a secret key used for cookie validation. This property must be set if [[enableCookieValidation]] is true. |
||
158 | */ |
||
159 | public $cookieValidationKey; |
||
160 | /** |
||
161 | * @var string the name of the POST parameter that is used to indicate if a request is a PUT, PATCH or DELETE |
||
162 | * request tunneled through POST. Defaults to '_method'. |
||
163 | * @see getMethod() |
||
164 | * @see getBodyParams() |
||
165 | */ |
||
166 | public $methodParam = '_method'; |
||
167 | /** |
||
168 | * @var array the parsers for converting the raw HTTP request body into [[bodyParams]]. |
||
169 | * The array keys are the request `Content-Types`, and the array values are the |
||
170 | * corresponding configurations for [[Yii::createObject|creating the parser objects]]. |
||
171 | * A parser must implement the [[RequestParserInterface]]. |
||
172 | * |
||
173 | * To enable parsing for JSON requests you can use the [[JsonParser]] class like in the following example: |
||
174 | * |
||
175 | * ``` |
||
176 | * [ |
||
177 | * 'application/json' => \yii\web\JsonParser::class, |
||
178 | * ] |
||
179 | * ``` |
||
180 | * |
||
181 | * To register a parser for parsing all request types you can use `'*'` as the array key. |
||
182 | * This one will be used as a fallback in case no other types match. |
||
183 | * |
||
184 | * @see getBodyParams() |
||
185 | */ |
||
186 | public $parsers = []; |
||
187 | /** |
||
188 | * @var string name of the class to be used for uploaded file instantiation. |
||
189 | * This class should implement [[UploadedFileInterface]]. |
||
190 | * @since 2.1.0 |
||
191 | */ |
||
192 | public $uploadedFileClass = UploadedFile::class; |
||
193 | /** |
||
194 | * @var array the configuration for trusted security related headers. |
||
195 | * |
||
196 | * An array key is an IPv4 or IPv6 IP address in CIDR notation for matching a client. |
||
197 | * |
||
198 | * An array value is a list of headers to trust. These will be matched against |
||
199 | * [[secureHeaders]] to determine which headers are allowed to be sent by a specified host. |
||
200 | * The case of the header names must be the same as specified in [[secureHeaders]]. |
||
201 | * |
||
202 | * For example, to trust all headers listed in [[secureHeaders]] for IP addresses |
||
203 | * in range `192.168.0.0-192.168.0.254` write the following: |
||
204 | * |
||
205 | * ```php |
||
206 | * [ |
||
207 | * '192.168.0.0/24', |
||
208 | * ] |
||
209 | * ``` |
||
210 | * |
||
211 | * To trust just the `X-Forwarded-For` header from `10.0.0.1`, use: |
||
212 | * |
||
213 | * ``` |
||
214 | * [ |
||
215 | * '10.0.0.1' => ['X-Forwarded-For'] |
||
216 | * ] |
||
217 | * ``` |
||
218 | * |
||
219 | * Default is to trust all headers except those listed in [[secureHeaders]] from all hosts. |
||
220 | * Matches are tried in order and searching is stopped when IP matches. |
||
221 | * |
||
222 | * > Info: Matching is performed using [[IpValidator]]. |
||
223 | * See [[IpValidator::::setRanges()|IpValidator::setRanges()]] |
||
224 | * and [[IpValidator::networks]] for advanced matching. |
||
225 | * |
||
226 | * @see $secureHeaders |
||
227 | * @since 2.0.13 |
||
228 | */ |
||
229 | public $trustedHosts = []; |
||
230 | /** |
||
231 | * @var array lists of headers that are, by default, subject to the trusted host configuration. |
||
232 | * These headers will be filtered unless explicitly allowed in [[trustedHosts]]. |
||
233 | * The match of header names is case-insensitive. |
||
234 | * @see https://en.wikipedia.org/wiki/List_of_HTTP_header_fields |
||
235 | * @see $trustedHosts |
||
236 | * @since 2.0.13 |
||
237 | */ |
||
238 | public $secureHeaders = [ |
||
239 | 'X-Forwarded-For', |
||
240 | 'X-Forwarded-Host', |
||
241 | 'X-Forwarded-Proto', |
||
242 | 'Front-End-Https', |
||
243 | 'X-Rewrite-Url', |
||
244 | ]; |
||
245 | /** |
||
246 | * @var string[] List of headers where proxies store the real client IP. |
||
247 | * It's not advisable to put insecure headers here. |
||
248 | * The match of header names is case-insensitive. |
||
249 | * @see $trustedHosts |
||
250 | * @see $secureHeaders |
||
251 | * @since 2.0.13 |
||
252 | */ |
||
253 | public $ipHeaders = [ |
||
254 | 'X-Forwarded-For', |
||
255 | ]; |
||
256 | /** |
||
257 | * @var array list of headers to check for determining whether the connection is made via HTTPS. |
||
258 | * The array keys are header names and the array value is a list of header values that indicate a secure connection. |
||
259 | * The match of header names and values is case-insensitive. |
||
260 | * It's not advisable to put insecure headers here. |
||
261 | * @see $trustedHosts |
||
262 | * @see $secureHeaders |
||
263 | * @since 2.0.13 |
||
264 | */ |
||
265 | public $secureProtocolHeaders = [ |
||
266 | 'X-Forwarded-Proto' => ['https'], |
||
267 | 'Front-End-Https' => ['on'], |
||
268 | ]; |
||
269 | |||
270 | /** |
||
271 | * @var CookieCollection Collection of request cookies. |
||
272 | */ |
||
273 | private $_cookies; |
||
274 | /** |
||
275 | * @var string the HTTP method of the request. |
||
276 | */ |
||
277 | private $_method; |
||
278 | /** |
||
279 | * @var UriInterface the URI instance associated with request. |
||
280 | */ |
||
281 | private $_uri; |
||
282 | /** |
||
283 | * @var mixed the message's request target. |
||
284 | */ |
||
285 | private $_requestTarget; |
||
286 | /** |
||
287 | * @var array uploaded files. |
||
288 | * @since 2.1.0 |
||
289 | */ |
||
290 | private $_uploadedFiles; |
||
291 | |||
292 | |||
293 | /** |
||
294 | * Resolves the current request into a route and the associated parameters. |
||
295 | * @return array the first element is the route, and the second is the associated parameters. |
||
296 | * @throws NotFoundHttpException if the request cannot be resolved. |
||
297 | */ |
||
298 | 1 | public function resolve() |
|
314 | |||
315 | /** |
||
316 | * Filters headers according to the [[trustedHosts]]. |
||
317 | * @param array $rawHeaders |
||
318 | * @return array filtered headers |
||
319 | * @since 2.0.13 |
||
320 | */ |
||
321 | 113 | protected function filterHeaders($rawHeaders) |
|
354 | |||
355 | /** |
||
356 | * Creates instance of [[IpValidator]]. |
||
357 | * You can override this method to adjust validator or implement different matching strategy. |
||
358 | * |
||
359 | * @return IpValidator |
||
360 | * @since 2.0.13 |
||
361 | */ |
||
362 | 19 | protected function getIpValidator() |
|
366 | |||
367 | /** |
||
368 | * Returns default message's headers, which should be present once [[headerCollection]] is instantiated. |
||
369 | * @return string[][] an associative array of the message's headers. |
||
370 | */ |
||
371 | 113 | protected function defaultHeaders() |
|
389 | |||
390 | /** |
||
391 | * {@inheritdoc} |
||
392 | * @since 2.1.0 |
||
393 | */ |
||
394 | public function getRequestTarget() |
||
401 | |||
402 | /** |
||
403 | * Specifies the message's request target |
||
404 | * @param mixed $requestTarget the message's request target. |
||
405 | * @since 2.1.0 |
||
406 | */ |
||
407 | public function setRequestTarget($requestTarget) |
||
411 | |||
412 | /** |
||
413 | * {@inheritdoc} |
||
414 | * @since 2.1.0 |
||
415 | */ |
||
416 | public function withRequestTarget($requestTarget) |
||
426 | |||
427 | /** |
||
428 | * {@inheritdoc} |
||
429 | */ |
||
430 | 28 | public function getMethod() |
|
445 | |||
446 | /** |
||
447 | * Specifies request HTTP method. |
||
448 | * @param string $method case-sensitive HTTP method. |
||
449 | * @since 2.1.0 |
||
450 | */ |
||
451 | 11 | public function setMethod($method) |
|
455 | |||
456 | /** |
||
457 | * {@inheritdoc} |
||
458 | * @since 2.1.0 |
||
459 | */ |
||
460 | 1 | public function withMethod($method) |
|
470 | |||
471 | /** |
||
472 | * {@inheritdoc} |
||
473 | * @since 2.1.0 |
||
474 | */ |
||
475 | public function getUri() |
||
490 | |||
491 | /** |
||
492 | * Specifies the URI instance. |
||
493 | * @param UriInterface|\Closure|array $uri URI instance or its DI compatible configuration. |
||
494 | * @since 2.1.0 |
||
495 | */ |
||
496 | public function setUri($uri) |
||
500 | |||
501 | /** |
||
502 | * {@inheritdoc} |
||
503 | * @since 2.1.0 |
||
504 | */ |
||
505 | public function withUri(UriInterface $uri, $preserveHost = false) |
||
519 | |||
520 | /** |
||
521 | * Returns whether this is a GET request. |
||
522 | * @return bool whether this is a GET request. |
||
523 | */ |
||
524 | 2 | public function getIsGet() |
|
528 | |||
529 | /** |
||
530 | * Returns whether this is an OPTIONS request. |
||
531 | * @return bool whether this is a OPTIONS request. |
||
532 | */ |
||
533 | public function getIsOptions() |
||
537 | |||
538 | /** |
||
539 | * Returns whether this is a HEAD request. |
||
540 | * @return bool whether this is a HEAD request. |
||
541 | */ |
||
542 | 9 | public function getIsHead() |
|
546 | |||
547 | /** |
||
548 | * Returns whether this is a POST request. |
||
549 | * @return bool whether this is a POST request. |
||
550 | */ |
||
551 | public function getIsPost() |
||
555 | |||
556 | /** |
||
557 | * Returns whether this is a DELETE request. |
||
558 | * @return bool whether this is a DELETE request. |
||
559 | */ |
||
560 | public function getIsDelete() |
||
564 | |||
565 | /** |
||
566 | * Returns whether this is a PUT request. |
||
567 | * @return bool whether this is a PUT request. |
||
568 | */ |
||
569 | public function getIsPut() |
||
573 | |||
574 | /** |
||
575 | * Returns whether this is a PATCH request. |
||
576 | * @return bool whether this is a PATCH request. |
||
577 | */ |
||
578 | public function getIsPatch() |
||
582 | |||
583 | /** |
||
584 | * Returns whether this is an AJAX (XMLHttpRequest) request. |
||
585 | * |
||
586 | * Note that jQuery doesn't set the header in case of cross domain |
||
587 | * requests: https://stackoverflow.com/questions/8163703/cross-domain-ajax-doesnt-send-x-requested-with-header |
||
588 | * |
||
589 | * @return bool whether this is an AJAX (XMLHttpRequest) request. |
||
590 | */ |
||
591 | 14 | public function getIsAjax() |
|
595 | |||
596 | /** |
||
597 | * Returns whether this is a PJAX request |
||
598 | * @return bool whether this is a PJAX request |
||
599 | */ |
||
600 | 3 | public function getIsPjax() |
|
604 | |||
605 | /** |
||
606 | * Returns whether this is an Adobe Flash or Flex request. |
||
607 | * @return bool whether this is an Adobe Flash or Adobe Flex request. |
||
608 | */ |
||
609 | public function getIsFlash() |
||
617 | |||
618 | /** |
||
619 | * Returns default message body to be used in case it is not explicitly set. |
||
620 | * @return StreamInterface default body instance. |
||
621 | */ |
||
622 | protected function defaultBody() |
||
629 | |||
630 | /** |
||
631 | * Returns the raw HTTP request body. |
||
632 | * @return string the request body |
||
633 | */ |
||
634 | public function getRawBody() |
||
638 | |||
639 | /** |
||
640 | * Sets the raw HTTP request body, this method is mainly used by test scripts to simulate raw HTTP requests. |
||
641 | * @param string $rawBody the request body |
||
642 | */ |
||
643 | 6 | public function setRawBody($rawBody) |
|
649 | |||
650 | private $_bodyParams; |
||
651 | |||
652 | /** |
||
653 | * Returns the request parameters given in the request body. |
||
654 | * |
||
655 | * Request parameters are determined using the parsers configured in [[parsers]] property. |
||
656 | * If no parsers are configured for the current [[contentType]] it uses the PHP function `mb_parse_str()` |
||
657 | * to parse the [[rawBody|request body]]. |
||
658 | * @return array the request parameters given in the request body. |
||
659 | * @throws InvalidConfigException if a registered parser does not implement the [[RequestParserInterface]]. |
||
660 | * @throws UnsupportedMediaTypeHttpException if unable to parse raw body. |
||
661 | * @see getMethod() |
||
662 | * @see getBodyParam() |
||
663 | * @see setBodyParams() |
||
664 | */ |
||
665 | 11 | public function getBodyParams() |
|
709 | |||
710 | /** |
||
711 | * Sets the request body parameters. |
||
712 | * @param array $values the request body parameters (name-value pairs) |
||
713 | * @see getBodyParam() |
||
714 | * @see getBodyParams() |
||
715 | */ |
||
716 | 3 | public function setBodyParams($values) |
|
720 | |||
721 | /** |
||
722 | * Returns the named request body parameter value. |
||
723 | * If the parameter does not exist, the second parameter passed to this method will be returned. |
||
724 | * @param string $name the parameter name |
||
725 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
726 | * @return mixed the parameter value |
||
727 | * @see getBodyParams() |
||
728 | * @see setBodyParams() |
||
729 | */ |
||
730 | 4 | public function getBodyParam($name, $defaultValue = null) |
|
736 | |||
737 | /** |
||
738 | * Returns POST parameter with a given name. If name isn't specified, returns an array of all POST parameters. |
||
739 | * |
||
740 | * @param string $name the parameter name |
||
741 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
742 | * @return array|mixed |
||
743 | */ |
||
744 | public function post($name = null, $defaultValue = null) |
||
752 | |||
753 | private $_queryParams; |
||
754 | |||
755 | /** |
||
756 | * Returns the request parameters given in the [[queryString]]. |
||
757 | * |
||
758 | * This method will return the contents of `$_GET` if params where not explicitly set. |
||
759 | * @return array the request GET parameter values. |
||
760 | * @see setQueryParams() |
||
761 | */ |
||
762 | 29 | public function getQueryParams() |
|
770 | |||
771 | /** |
||
772 | * Sets the request [[queryString]] parameters. |
||
773 | * @param array $values the request query parameters (name-value pairs) |
||
774 | * @see getQueryParam() |
||
775 | * @see getQueryParams() |
||
776 | */ |
||
777 | 8 | public function setQueryParams($values) |
|
781 | |||
782 | /** |
||
783 | * Returns GET parameter with a given name. If name isn't specified, returns an array of all GET parameters. |
||
784 | * |
||
785 | * @param string $name the parameter name |
||
786 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
787 | * @return array|mixed |
||
788 | */ |
||
789 | 15 | public function get($name = null, $defaultValue = null) |
|
797 | |||
798 | /** |
||
799 | * Returns the named GET parameter value. |
||
800 | * If the GET parameter does not exist, the second parameter passed to this method will be returned. |
||
801 | * @param string $name the GET parameter name. |
||
802 | * @param mixed $defaultValue the default parameter value if the GET parameter does not exist. |
||
803 | * @return mixed the GET parameter value |
||
804 | * @see getBodyParam() |
||
805 | */ |
||
806 | 20 | public function getQueryParam($name, $defaultValue = null) |
|
812 | |||
813 | private $_hostInfo; |
||
814 | private $_hostName; |
||
815 | |||
816 | /** |
||
817 | * Returns the schema and host part of the current request URL. |
||
818 | * |
||
819 | * The returned URL does not have an ending slash. |
||
820 | * |
||
821 | * By default this value is based on the user request information. This method will |
||
822 | * return the value of `$_SERVER['HTTP_HOST']` if it is available or `$_SERVER['SERVER_NAME']` if not. |
||
823 | * You may want to check out the [PHP documentation](http://php.net/manual/en/reserved.variables.server.php) |
||
824 | * for more information on these variables. |
||
825 | * |
||
826 | * You may explicitly specify it by setting the [[setHostInfo()|hostInfo]] property. |
||
827 | * |
||
828 | * > Warning: Dependent on the server configuration this information may not be |
||
829 | * > reliable and [may be faked by the user sending the HTTP request](https://www.acunetix.com/vulnerabilities/web/host-header-attack). |
||
830 | * > If the webserver is configured to serve the same site independent of the value of |
||
831 | * > the `Host` header, this value is not reliable. In such situations you should either |
||
832 | * > fix your webserver configuration or explicitly set the value by setting the [[setHostInfo()|hostInfo]] property. |
||
833 | * > If you don't have access to the server configuration, you can setup [[\yii\filters\HostControl]] filter at |
||
834 | * > application level in order to protect against such kind of attack. |
||
835 | * |
||
836 | * @property string|null schema and hostname part (with port number if needed) of the request URL |
||
837 | * (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set. |
||
838 | * See [[getHostInfo()]] for security related notes on this property. |
||
839 | * @return string|null schema and hostname part (with port number if needed) of the request URL |
||
840 | * (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set. |
||
841 | * @see setHostInfo() |
||
842 | */ |
||
843 | 24 | public function getHostInfo() |
|
861 | |||
862 | /** |
||
863 | * Sets the schema and host part of the application URL. |
||
864 | * This setter is provided in case the schema and hostname cannot be determined |
||
865 | * on certain Web servers. |
||
866 | * @param string|null $value the schema and host part of the application URL. The trailing slashes will be removed. |
||
867 | * @see getHostInfo() for security related notes on this property. |
||
868 | */ |
||
869 | 57 | public function setHostInfo($value) |
|
874 | |||
875 | /** |
||
876 | * Returns the host part of the current request URL. |
||
877 | * Value is calculated from current [[getHostInfo()|hostInfo]] property. |
||
878 | * |
||
879 | * > Warning: The content of this value may not be reliable, dependent on the server |
||
880 | * > configuration. Please refer to [[getHostInfo()]] for more information. |
||
881 | * |
||
882 | * @return string|null hostname part of the request URL (e.g. `www.yiiframework.com`) |
||
883 | * @see getHostInfo() |
||
884 | * @since 2.0.10 |
||
885 | */ |
||
886 | 11 | public function getHostName() |
|
894 | |||
895 | private $_baseUrl; |
||
896 | |||
897 | /** |
||
898 | * Returns the relative URL for the application. |
||
899 | * This is similar to [[scriptUrl]] except that it does not include the script file name, |
||
900 | * and the ending slashes are removed. |
||
901 | * @return string the relative URL for the application |
||
902 | * @see setScriptUrl() |
||
903 | */ |
||
904 | 257 | public function getBaseUrl() |
|
912 | |||
913 | /** |
||
914 | * Sets the relative URL for the application. |
||
915 | * By default the URL is determined based on the entry script URL. |
||
916 | * This setter is provided in case you want to change this behavior. |
||
917 | * @param string $value the relative URL for the application |
||
918 | */ |
||
919 | 1 | public function setBaseUrl($value) |
|
923 | |||
924 | private $_scriptUrl; |
||
925 | |||
926 | /** |
||
927 | * Returns the relative URL of the entry script. |
||
928 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
929 | * @return string the relative URL of the entry script. |
||
930 | * @throws InvalidConfigException if unable to determine the entry script URL |
||
931 | */ |
||
932 | 258 | public function getScriptUrl() |
|
954 | |||
955 | /** |
||
956 | * Sets the relative URL for the application entry script. |
||
957 | * This setter is provided in case the entry script URL cannot be determined |
||
958 | * on certain Web servers. |
||
959 | * @param string $value the relative URL for the application entry script. |
||
960 | */ |
||
961 | 268 | public function setScriptUrl($value) |
|
965 | |||
966 | private $_scriptFile; |
||
967 | |||
968 | /** |
||
969 | * Returns the entry script file path. |
||
970 | * The default implementation will simply return `$_SERVER['SCRIPT_FILENAME']`. |
||
971 | * @return string the entry script file path |
||
972 | * @throws InvalidConfigException |
||
973 | */ |
||
974 | 259 | public function getScriptFile() |
|
986 | |||
987 | /** |
||
988 | * Sets the entry script file path. |
||
989 | * The entry script file path normally can be obtained from `$_SERVER['SCRIPT_FILENAME']`. |
||
990 | * If your server configuration does not return the correct value, you may configure |
||
991 | * this property to make it right. |
||
992 | * @param string $value the entry script file path. |
||
993 | */ |
||
994 | 237 | public function setScriptFile($value) |
|
998 | |||
999 | private $_pathInfo; |
||
1000 | |||
1001 | /** |
||
1002 | * Returns the path info of the currently requested URL. |
||
1003 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||
1004 | * The starting and ending slashes are both removed. |
||
1005 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
1006 | * Note, the returned path info is already URL-decoded. |
||
1007 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
1008 | */ |
||
1009 | 18 | public function getPathInfo() |
|
1017 | |||
1018 | /** |
||
1019 | * Sets the path info of the current request. |
||
1020 | * This method is mainly provided for testing purpose. |
||
1021 | * @param string $value the path info of the current request |
||
1022 | */ |
||
1023 | 19 | public function setPathInfo($value) |
|
1027 | |||
1028 | /** |
||
1029 | * Resolves the path info part of the currently requested URL. |
||
1030 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||
1031 | * The starting slashes are both removed (ending slashes will be kept). |
||
1032 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
1033 | * Note, the returned path info is decoded. |
||
1034 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
1035 | */ |
||
1036 | protected function resolvePathInfo() |
||
1080 | |||
1081 | /** |
||
1082 | * Returns the currently requested absolute URL. |
||
1083 | * This is a shortcut to the concatenation of [[hostInfo]] and [[url]]. |
||
1084 | * @return string the currently requested absolute URL. |
||
1085 | */ |
||
1086 | public function getAbsoluteUrl() |
||
1090 | |||
1091 | private $_url; |
||
1092 | |||
1093 | /** |
||
1094 | * Returns the currently requested relative URL. |
||
1095 | * This refers to the portion of the URL that is after the [[hostInfo]] part. |
||
1096 | * It includes the [[queryString]] part if any. |
||
1097 | * @return string the currently requested relative URL. Note that the URI returned may be URL-encoded depending on the client. |
||
1098 | * @throws InvalidConfigException if the URL cannot be determined due to unusual server configuration |
||
1099 | */ |
||
1100 | 11 | public function getUrl() |
|
1108 | |||
1109 | /** |
||
1110 | * Sets the currently requested relative URL. |
||
1111 | * The URI must refer to the portion that is after [[hostInfo]]. |
||
1112 | * Note that the URI should be URL-encoded. |
||
1113 | * @param string $value the request URI to be set |
||
1114 | */ |
||
1115 | 24 | public function setUrl($value) |
|
1119 | |||
1120 | /** |
||
1121 | * Resolves the request URI portion for the currently requested URL. |
||
1122 | * This refers to the portion that is after the [[hostInfo]] part. It includes the [[queryString]] part if any. |
||
1123 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
1124 | * @return string|bool the request URI portion for the currently requested URL. |
||
1125 | * Note that the URI returned may be URL-encoded depending on the client. |
||
1126 | * @throws InvalidConfigException if the request URI cannot be determined due to unusual server configuration |
||
1127 | */ |
||
1128 | 3 | protected function resolveRequestUri() |
|
1148 | |||
1149 | /** |
||
1150 | * Returns part of the request URL that is after the question mark. |
||
1151 | * @return string part of the request URL that is after the question mark |
||
1152 | */ |
||
1153 | public function getQueryString() |
||
1157 | |||
1158 | /** |
||
1159 | * Return if the request is sent via secure channel (https). |
||
1160 | * @return bool if the request is sent via secure channel (https) |
||
1161 | */ |
||
1162 | 37 | public function getIsSecureConnection() |
|
1179 | |||
1180 | /** |
||
1181 | * Returns the server name. |
||
1182 | * @return string server name, null if not available |
||
1183 | */ |
||
1184 | 1 | public function getServerName() |
|
1188 | |||
1189 | /** |
||
1190 | * Returns the server port number. |
||
1191 | * @return int|null server port number, null if not available |
||
1192 | */ |
||
1193 | 1 | public function getServerPort() |
|
1197 | |||
1198 | /** |
||
1199 | * Returns the URL referrer. |
||
1200 | * @return string|null URL referrer, null if not available |
||
1201 | */ |
||
1202 | public function getReferrer() |
||
1209 | |||
1210 | /** |
||
1211 | * Returns the URL origin of a CORS request. |
||
1212 | * |
||
1213 | * The return value is taken from the `Origin` [[getHeaders()|header]] sent by the browser. |
||
1214 | * |
||
1215 | * Note that the origin request header indicates where a fetch originates from. |
||
1216 | * It doesn't include any path information, but only the server name. |
||
1217 | * It is sent with a CORS requests, as well as with POST requests. |
||
1218 | * It is similar to the referer header, but, unlike this header, it doesn't disclose the whole path. |
||
1219 | * Please refer to <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin> for more information. |
||
1220 | * |
||
1221 | * @return string|null URL origin of a CORS request, `null` if not available. |
||
1222 | * @see getHeaders() |
||
1223 | * @since 2.0.13 |
||
1224 | */ |
||
1225 | 1 | public function getOrigin() |
|
1229 | |||
1230 | /** |
||
1231 | * Returns the user agent. |
||
1232 | * @return string|null user agent, null if not available |
||
1233 | */ |
||
1234 | public function getUserAgent() |
||
1241 | |||
1242 | /** |
||
1243 | * Returns the user IP address. |
||
1244 | * The IP is determined using headers and / or `$_SERVER` variables. |
||
1245 | * @return string|null user IP address, null if not available |
||
1246 | */ |
||
1247 | 32 | public function getUserIP() |
|
1257 | |||
1258 | /** |
||
1259 | * Returns the user host name. |
||
1260 | * The HOST is determined using headers and / or `$_SERVER` variables. |
||
1261 | * @return string|null user host name, null if not available |
||
1262 | */ |
||
1263 | public function getUserHost() |
||
1273 | |||
1274 | /** |
||
1275 | * Returns the IP on the other end of this connection. |
||
1276 | * This is always the next hop, any headers are ignored. |
||
1277 | * @return string|null remote IP address, `null` if not available. |
||
1278 | * @since 2.0.13 |
||
1279 | */ |
||
1280 | 47 | public function getRemoteIP() |
|
1284 | |||
1285 | /** |
||
1286 | * Returns the host name of the other end of this connection. |
||
1287 | * This is always the next hop, any headers are ignored. |
||
1288 | * @return string|null remote host name, `null` if not available |
||
1289 | * @see getUserHost() |
||
1290 | * @see getRemoteIP() |
||
1291 | * @since 2.0.13 |
||
1292 | */ |
||
1293 | public function getRemoteHost() |
||
1297 | |||
1298 | /** |
||
1299 | * @return string|null the username sent via HTTP authentication, null if the username is not given |
||
1300 | */ |
||
1301 | 10 | public function getAuthUser() |
|
1305 | |||
1306 | /** |
||
1307 | * @return string|null the password sent via HTTP authentication, null if the password is not given |
||
1308 | */ |
||
1309 | 10 | public function getAuthPassword() |
|
1313 | |||
1314 | private $_port; |
||
1315 | |||
1316 | /** |
||
1317 | * Returns the port to use for insecure requests. |
||
1318 | * Defaults to 80, or the port specified by the server if the current |
||
1319 | * request is insecure. |
||
1320 | * @return int port number for insecure requests. |
||
1321 | * @see setPort() |
||
1322 | */ |
||
1323 | public function getPort() |
||
1331 | |||
1332 | /** |
||
1333 | * Sets the port to use for insecure requests. |
||
1334 | * This setter is provided in case a custom port is necessary for certain |
||
1335 | * server configurations. |
||
1336 | * @param int $value port number. |
||
1337 | */ |
||
1338 | public function setPort($value) |
||
1345 | |||
1346 | private $_securePort; |
||
1347 | |||
1348 | /** |
||
1349 | * Returns the port to use for secure requests. |
||
1350 | * Defaults to 443, or the port specified by the server if the current |
||
1351 | * request is secure. |
||
1352 | * @return int port number for secure requests. |
||
1353 | * @see setSecurePort() |
||
1354 | */ |
||
1355 | public function getSecurePort() |
||
1363 | |||
1364 | /** |
||
1365 | * Sets the port to use for secure requests. |
||
1366 | * This setter is provided in case a custom port is necessary for certain |
||
1367 | * server configurations. |
||
1368 | * @param int $value port number. |
||
1369 | */ |
||
1370 | public function setSecurePort($value) |
||
1377 | |||
1378 | private $_contentTypes; |
||
1379 | |||
1380 | /** |
||
1381 | * Returns the content types acceptable by the end user. |
||
1382 | * |
||
1383 | * This is determined by the `Accept` HTTP header. For example, |
||
1384 | * |
||
1385 | * ```php |
||
1386 | * $_SERVER['HTTP_ACCEPT'] = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
1387 | * $types = $request->getAcceptableContentTypes(); |
||
1388 | * print_r($types); |
||
1389 | * // displays: |
||
1390 | * // [ |
||
1391 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||
1392 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||
1393 | * // 'text/plain' => ['q' => 0.5], |
||
1394 | * // ] |
||
1395 | * ``` |
||
1396 | * |
||
1397 | * @return array the content types ordered by the quality score. Types with the highest scores |
||
1398 | * will be returned first. The array keys are the content types, while the array values |
||
1399 | * are the corresponding quality score and other parameters as given in the header. |
||
1400 | */ |
||
1401 | 3 | public function getAcceptableContentTypes() |
|
1413 | |||
1414 | /** |
||
1415 | * Sets the acceptable content types. |
||
1416 | * Please refer to [[getAcceptableContentTypes()]] on the format of the parameter. |
||
1417 | * @param array $value the content types that are acceptable by the end user. They should |
||
1418 | * be ordered by the preference level. |
||
1419 | * @see getAcceptableContentTypes() |
||
1420 | * @see parseAcceptHeader() |
||
1421 | */ |
||
1422 | 1 | public function setAcceptableContentTypes($value) |
|
1426 | |||
1427 | /** |
||
1428 | * Returns request content-type |
||
1429 | * The Content-Type header field indicates the MIME type of the data |
||
1430 | * contained in [[getBody()]] or, in the case of the HEAD method, the |
||
1431 | * media type that would have been sent had the request been a GET. |
||
1432 | * For the MIME-types the user expects in response, see [[acceptableContentTypes]]. |
||
1433 | * @return string request content-type. Empty string is returned if this information is not available. |
||
1434 | * @link https://tools.ietf.org/html/rfc2616#section-14.17 |
||
1435 | * HTTP 1.1 header field definitions |
||
1436 | */ |
||
1437 | 12 | public function getContentType() |
|
1441 | |||
1442 | private $_languages; |
||
1443 | |||
1444 | /** |
||
1445 | * Returns the languages acceptable by the end user. |
||
1446 | * This is determined by the `Accept-Language` HTTP header. |
||
1447 | * @return array the languages ordered by the preference level. The first element |
||
1448 | * represents the most preferred language. |
||
1449 | */ |
||
1450 | 1 | public function getAcceptableLanguages() |
|
1462 | |||
1463 | /** |
||
1464 | * @param array $value the languages that are acceptable by the end user. They should |
||
1465 | * be ordered by the preference level. |
||
1466 | */ |
||
1467 | 1 | public function setAcceptableLanguages($value) |
|
1471 | |||
1472 | /** |
||
1473 | * Parses the given `Accept` (or `Accept-Language`) header. |
||
1474 | * |
||
1475 | * This method will return the acceptable values with their quality scores and the corresponding parameters |
||
1476 | * as specified in the given `Accept` header. The array keys of the return value are the acceptable values, |
||
1477 | * while the array values consisting of the corresponding quality scores and parameters. The acceptable |
||
1478 | * values with the highest quality scores will be returned first. For example, |
||
1479 | * |
||
1480 | * ```php |
||
1481 | * $header = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
1482 | * $accepts = $request->parseAcceptHeader($header); |
||
1483 | * print_r($accepts); |
||
1484 | * // displays: |
||
1485 | * // [ |
||
1486 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||
1487 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||
1488 | * // 'text/plain' => ['q' => 0.5], |
||
1489 | * // ] |
||
1490 | * ``` |
||
1491 | * |
||
1492 | * @param string $header the header to be parsed |
||
1493 | * @return array the acceptable values ordered by their quality score. The values with the highest scores |
||
1494 | * will be returned first. |
||
1495 | */ |
||
1496 | 3 | public function parseAcceptHeader($header) |
|
1563 | |||
1564 | /** |
||
1565 | * Returns the user-preferred language that should be used by this application. |
||
1566 | * The language resolution is based on the user preferred languages and the languages |
||
1567 | * supported by the application. The method will try to find the best match. |
||
1568 | * @param array $languages a list of the languages supported by the application. If this is empty, the current |
||
1569 | * application language will be returned without further processing. |
||
1570 | * @return string the language that the application should use. |
||
1571 | */ |
||
1572 | 1 | public function getPreferredLanguage(array $languages = []) |
|
1594 | |||
1595 | /** |
||
1596 | * Gets the Etags. |
||
1597 | * |
||
1598 | * @return array The entity tags |
||
1599 | */ |
||
1600 | public function getETags() |
||
1608 | |||
1609 | /** |
||
1610 | * Returns the cookie collection. |
||
1611 | * |
||
1612 | * Through the returned cookie collection, you may access a cookie using the following syntax: |
||
1613 | * |
||
1614 | * ```php |
||
1615 | * $cookie = $request->cookies['name'] |
||
1616 | * if ($cookie !== null) { |
||
1617 | * $value = $cookie->value; |
||
1618 | * } |
||
1619 | * |
||
1620 | * // alternatively |
||
1621 | * $value = $request->cookies->getValue('name'); |
||
1622 | * ``` |
||
1623 | * |
||
1624 | * @return CookieCollection the cookie collection. |
||
1625 | */ |
||
1626 | 33 | public function getCookies() |
|
1636 | |||
1637 | /** |
||
1638 | * Converts `$_COOKIE` into an array of [[Cookie]]. |
||
1639 | * @return array the cookies obtained from request |
||
1640 | * @throws InvalidConfigException if [[cookieValidationKey]] is not set when [[enableCookieValidation]] is true |
||
1641 | */ |
||
1642 | 33 | protected function loadCookies() |
|
1678 | |||
1679 | /** |
||
1680 | * Returns uploaded files for this request. |
||
1681 | * Uploaded files are returned in format according to [PSR-7 Uploaded Files specs](http://www.php-fig.org/psr/psr-7/#16-uploaded-files). |
||
1682 | * @return array uploaded files. |
||
1683 | * @since 2.1.0 |
||
1684 | */ |
||
1685 | 11 | public function getUploadedFiles() |
|
1695 | |||
1696 | /** |
||
1697 | * Sets uploaded files for this request. |
||
1698 | * Data structure for the uploaded files should follow [PSR-7 Uploaded Files specs](http://www.php-fig.org/psr/psr-7/#16-uploaded-files). |
||
1699 | * @param array|null $uploadedFiles uploaded files. |
||
1700 | * @since 2.1.0 |
||
1701 | */ |
||
1702 | 6 | public function setUploadedFiles($uploadedFiles) |
|
1706 | |||
1707 | /** |
||
1708 | * Initializes default uploaded files data structure parsing super-global $_FILES. |
||
1709 | * @see http://www.php-fig.org/psr/psr-7/#16-uploaded-files |
||
1710 | * @return array uploaded files. |
||
1711 | * @since 2.1.0 |
||
1712 | */ |
||
1713 | 5 | protected function defaultUploadedFiles() |
|
1723 | |||
1724 | /** |
||
1725 | * Populates uploaded files array from $_FILE data structure recursively. |
||
1726 | * @param array $files uploaded files array to be populated. |
||
1727 | * @param mixed $names file names provided by PHP |
||
1728 | * @param mixed $tempNames temporary file names provided by PHP |
||
1729 | * @param mixed $types file types provided by PHP |
||
1730 | * @param mixed $sizes file sizes provided by PHP |
||
1731 | * @param mixed $errors uploading issues provided by PHP |
||
1732 | * @since 2.1.0 |
||
1733 | */ |
||
1734 | 3 | private function populateUploadedFileRecursive(&$files, $names, $tempNames, $types, $sizes, $errors) |
|
1752 | |||
1753 | /** |
||
1754 | * Returns an uploaded file according to the given name. |
||
1755 | * Name can be either a string HTML form input name, e.g. 'Item[file]' or array path, e.g. `['Item', 'file']`. |
||
1756 | * Note: this method returns `null` in case given name matches multiple files. |
||
1757 | * @param string|array $name HTML form input name or array path. |
||
1758 | * @return UploadedFileInterface|null uploaded file instance, `null` - if not found. |
||
1759 | * @since 2.1.0 |
||
1760 | */ |
||
1761 | 1 | public function getUploadedFileByName($name) |
|
1769 | |||
1770 | /** |
||
1771 | * Returns the list of uploaded file instances according to the given name. |
||
1772 | * Name can be either a string HTML form input name, e.g. 'Item[file]' or array path, e.g. `['Item', 'file']`. |
||
1773 | * Note: this method does NOT preserve uploaded files structure - it returns instances in single-level array (list), |
||
1774 | * even if they are set by nested keys. |
||
1775 | * @param string|array $name HTML form input name or array path. |
||
1776 | * @return UploadedFileInterface[] list of uploaded file instances. |
||
1777 | * @since 2.1.0 |
||
1778 | */ |
||
1779 | 1 | public function getUploadedFilesByName($name) |
|
1790 | |||
1791 | /** |
||
1792 | * Finds the uploaded file or set of uploaded files inside [[$uploadedFiles]] according to given name. |
||
1793 | * Name can be either a string HTML form input name, e.g. 'Item[file]' or array path, e.g. `['Item', 'file']`. |
||
1794 | * @param string|array $name HTML form input name or array path. |
||
1795 | * @return UploadedFileInterface|array|null |
||
1796 | * @since 2.1.0 |
||
1797 | */ |
||
1798 | 2 | private function findUploadedFiles($name) |
|
1805 | |||
1806 | /** |
||
1807 | * Reduces complex uploaded files structure to the single-level array (list). |
||
1808 | * @param array $uploadedFiles raw set of the uploaded files. |
||
1809 | * @return UploadedFileInterface[] list of uploaded files. |
||
1810 | * @since 2.1.0 |
||
1811 | */ |
||
1812 | private function reduceUploadedFiles($uploadedFiles) |
||
1823 | |||
1824 | private $_csrfToken; |
||
1825 | |||
1826 | /** |
||
1827 | * Returns the token used to perform CSRF validation. |
||
1828 | * |
||
1829 | * This token is generated in a way to prevent [BREACH attacks](http://breachattack.com/). It may be passed |
||
1830 | * along via a hidden field of an HTML form or an HTTP header value to support CSRF validation. |
||
1831 | * @param bool $regenerate whether to regenerate CSRF token. When this parameter is true, each time |
||
1832 | * this method is called, a new CSRF token will be generated and persisted (in session or cookie). |
||
1833 | * @return string the token used to perform CSRF validation. |
||
1834 | */ |
||
1835 | 37 | public function getCsrfToken($regenerate = false) |
|
1846 | |||
1847 | /** |
||
1848 | * Loads the CSRF token from cookie or session. |
||
1849 | * @return string the CSRF token loaded from cookie or session. Null is returned if the cookie or session |
||
1850 | * does not have CSRF token. |
||
1851 | */ |
||
1852 | 37 | protected function loadCsrfToken() |
|
1860 | |||
1861 | /** |
||
1862 | * Generates an unmasked random token used to perform CSRF validation. |
||
1863 | * @return string the random token for CSRF validation. |
||
1864 | */ |
||
1865 | 35 | protected function generateCsrfToken() |
|
1877 | |||
1878 | /** |
||
1879 | * @return string the CSRF token sent via [[CSRF_HEADER]] by browser. Null is returned if no such header is sent. |
||
1880 | */ |
||
1881 | 3 | public function getCsrfTokenFromHeader() |
|
1885 | |||
1886 | /** |
||
1887 | * Creates a cookie with a randomly generated CSRF token. |
||
1888 | * Initial values specified in [[csrfCookie]] will be applied to the generated cookie. |
||
1889 | * @param string $token the CSRF token |
||
1890 | * @return Cookie the generated cookie |
||
1891 | * @see enableCsrfValidation |
||
1892 | */ |
||
1893 | 33 | protected function createCsrfCookie($token) |
|
1900 | |||
1901 | /** |
||
1902 | * Performs the CSRF validation. |
||
1903 | * |
||
1904 | * This method will validate the user-provided CSRF token by comparing it with the one stored in cookie or session. |
||
1905 | * This method is mainly called in [[Controller::beforeAction()]]. |
||
1906 | * |
||
1907 | * Note that the method will NOT perform CSRF validation if [[enableCsrfValidation]] is false or the HTTP method |
||
1908 | * is among GET, HEAD or OPTIONS. |
||
1909 | * |
||
1910 | * @param string $clientSuppliedToken the user-provided CSRF token to be validated. If null, the token will be retrieved from |
||
1911 | * the [[csrfParam]] POST field or HTTP header. |
||
1912 | * This parameter is available since version 2.0.4. |
||
1913 | * @return bool whether CSRF token is valid. If [[enableCsrfValidation]] is false, this method will return true. |
||
1914 | */ |
||
1915 | 5 | public function validateCsrfToken($clientSuppliedToken = null) |
|
1932 | |||
1933 | /** |
||
1934 | * Validates CSRF token. |
||
1935 | * |
||
1936 | * @param string $clientSuppliedToken The masked client-supplied token. |
||
1937 | * @param string $trueToken The masked true token. |
||
1938 | * @return bool |
||
1939 | */ |
||
1940 | 3 | private function validateCsrfTokenInternal($clientSuppliedToken, $trueToken) |
|
1950 | |||
1951 | /** |
||
1952 | * {@inheritdoc} |
||
1953 | */ |
||
1954 | 2 | public function __clone() |
|
1964 | } |
||
1965 |
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.