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 | * |
||
659 | * Since 2.1.0 body params also include result of [[getUploadedFiles()]]. |
||
660 | * |
||
661 | * @return array the request parameters given in the request body. |
||
662 | * @throws InvalidConfigException if a registered parser does not implement the [[RequestParserInterface]]. |
||
663 | * @throws UnsupportedMediaTypeHttpException if unable to parse raw body. |
||
664 | * @see getMethod() |
||
665 | * @see getBodyParam() |
||
666 | * @see setBodyParams() |
||
667 | */ |
||
668 | 11 | public function getBodyParams() |
|
716 | |||
717 | /** |
||
718 | * Sets the request body parameters. |
||
719 | * @param array $values the request body parameters (name-value pairs) |
||
720 | * @see getBodyParam() |
||
721 | * @see getBodyParams() |
||
722 | */ |
||
723 | 3 | public function setBodyParams($values) |
|
727 | |||
728 | /** |
||
729 | * Returns the named request body parameter value. |
||
730 | * If the parameter does not exist, the second parameter passed to this method will be returned. |
||
731 | * @param string $name the parameter name |
||
732 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
733 | * @return mixed the parameter value |
||
734 | * @see getBodyParams() |
||
735 | * @see setBodyParams() |
||
736 | */ |
||
737 | 4 | public function getBodyParam($name, $defaultValue = null) |
|
743 | |||
744 | /** |
||
745 | * Returns POST parameter with a given name. If name isn't specified, returns an array of all POST parameters. |
||
746 | * |
||
747 | * @param string $name the parameter name |
||
748 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
749 | * @return array|mixed |
||
750 | */ |
||
751 | public function post($name = null, $defaultValue = null) |
||
759 | |||
760 | private $_queryParams; |
||
761 | |||
762 | /** |
||
763 | * Returns the request parameters given in the [[queryString]]. |
||
764 | * |
||
765 | * This method will return the contents of `$_GET` if params where not explicitly set. |
||
766 | * @return array the request GET parameter values. |
||
767 | * @see setQueryParams() |
||
768 | */ |
||
769 | 29 | public function getQueryParams() |
|
777 | |||
778 | /** |
||
779 | * Sets the request [[queryString]] parameters. |
||
780 | * @param array $values the request query parameters (name-value pairs) |
||
781 | * @see getQueryParam() |
||
782 | * @see getQueryParams() |
||
783 | */ |
||
784 | 8 | public function setQueryParams($values) |
|
788 | |||
789 | /** |
||
790 | * Returns GET parameter with a given name. If name isn't specified, returns an array of all GET parameters. |
||
791 | * |
||
792 | * @param string $name the parameter name |
||
793 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
794 | * @return array|mixed |
||
795 | */ |
||
796 | 15 | public function get($name = null, $defaultValue = null) |
|
804 | |||
805 | /** |
||
806 | * Returns the named GET parameter value. |
||
807 | * If the GET parameter does not exist, the second parameter passed to this method will be returned. |
||
808 | * @param string $name the GET parameter name. |
||
809 | * @param mixed $defaultValue the default parameter value if the GET parameter does not exist. |
||
810 | * @return mixed the GET parameter value |
||
811 | * @see getBodyParam() |
||
812 | */ |
||
813 | 20 | public function getQueryParam($name, $defaultValue = null) |
|
819 | |||
820 | private $_hostInfo; |
||
821 | private $_hostName; |
||
822 | |||
823 | /** |
||
824 | * Returns the schema and host part of the current request URL. |
||
825 | * |
||
826 | * The returned URL does not have an ending slash. |
||
827 | * |
||
828 | * By default this value is based on the user request information. This method will |
||
829 | * return the value of `$_SERVER['HTTP_HOST']` if it is available or `$_SERVER['SERVER_NAME']` if not. |
||
830 | * You may want to check out the [PHP documentation](http://php.net/manual/en/reserved.variables.server.php) |
||
831 | * for more information on these variables. |
||
832 | * |
||
833 | * You may explicitly specify it by setting the [[setHostInfo()|hostInfo]] property. |
||
834 | * |
||
835 | * > Warning: Dependent on the server configuration this information may not be |
||
836 | * > reliable and [may be faked by the user sending the HTTP request](https://www.acunetix.com/vulnerabilities/web/host-header-attack). |
||
837 | * > If the webserver is configured to serve the same site independent of the value of |
||
838 | * > the `Host` header, this value is not reliable. In such situations you should either |
||
839 | * > fix your webserver configuration or explicitly set the value by setting the [[setHostInfo()|hostInfo]] property. |
||
840 | * > If you don't have access to the server configuration, you can setup [[\yii\filters\HostControl]] filter at |
||
841 | * > application level in order to protect against such kind of attack. |
||
842 | * |
||
843 | * @property string|null schema and hostname part (with port number if needed) of the request URL |
||
844 | * (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set. |
||
845 | * See [[getHostInfo()]] for security related notes on this property. |
||
846 | * @return string|null schema and hostname part (with port number if needed) of the request URL |
||
847 | * (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set. |
||
848 | * @see setHostInfo() |
||
849 | */ |
||
850 | 24 | public function getHostInfo() |
|
868 | |||
869 | /** |
||
870 | * Sets the schema and host part of the application URL. |
||
871 | * This setter is provided in case the schema and hostname cannot be determined |
||
872 | * on certain Web servers. |
||
873 | * @param string|null $value the schema and host part of the application URL. The trailing slashes will be removed. |
||
874 | * @see getHostInfo() for security related notes on this property. |
||
875 | */ |
||
876 | 57 | public function setHostInfo($value) |
|
881 | |||
882 | /** |
||
883 | * Returns the host part of the current request URL. |
||
884 | * Value is calculated from current [[getHostInfo()|hostInfo]] property. |
||
885 | * |
||
886 | * > Warning: The content of this value may not be reliable, dependent on the server |
||
887 | * > configuration. Please refer to [[getHostInfo()]] for more information. |
||
888 | * |
||
889 | * @return string|null hostname part of the request URL (e.g. `www.yiiframework.com`) |
||
890 | * @see getHostInfo() |
||
891 | * @since 2.0.10 |
||
892 | */ |
||
893 | 11 | public function getHostName() |
|
901 | |||
902 | private $_baseUrl; |
||
903 | |||
904 | /** |
||
905 | * Returns the relative URL for the application. |
||
906 | * This is similar to [[scriptUrl]] except that it does not include the script file name, |
||
907 | * and the ending slashes are removed. |
||
908 | * @return string the relative URL for the application |
||
909 | * @see setScriptUrl() |
||
910 | */ |
||
911 | 257 | public function getBaseUrl() |
|
919 | |||
920 | /** |
||
921 | * Sets the relative URL for the application. |
||
922 | * By default the URL is determined based on the entry script URL. |
||
923 | * This setter is provided in case you want to change this behavior. |
||
924 | * @param string $value the relative URL for the application |
||
925 | */ |
||
926 | 1 | public function setBaseUrl($value) |
|
930 | |||
931 | private $_scriptUrl; |
||
932 | |||
933 | /** |
||
934 | * Returns the relative URL of the entry script. |
||
935 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
936 | * @return string the relative URL of the entry script. |
||
937 | * @throws InvalidConfigException if unable to determine the entry script URL |
||
938 | */ |
||
939 | 258 | public function getScriptUrl() |
|
961 | |||
962 | /** |
||
963 | * Sets the relative URL for the application entry script. |
||
964 | * This setter is provided in case the entry script URL cannot be determined |
||
965 | * on certain Web servers. |
||
966 | * @param string $value the relative URL for the application entry script. |
||
967 | */ |
||
968 | 268 | public function setScriptUrl($value) |
|
972 | |||
973 | private $_scriptFile; |
||
974 | |||
975 | /** |
||
976 | * Returns the entry script file path. |
||
977 | * The default implementation will simply return `$_SERVER['SCRIPT_FILENAME']`. |
||
978 | * @return string the entry script file path |
||
979 | * @throws InvalidConfigException |
||
980 | */ |
||
981 | 259 | public function getScriptFile() |
|
993 | |||
994 | /** |
||
995 | * Sets the entry script file path. |
||
996 | * The entry script file path normally can be obtained from `$_SERVER['SCRIPT_FILENAME']`. |
||
997 | * If your server configuration does not return the correct value, you may configure |
||
998 | * this property to make it right. |
||
999 | * @param string $value the entry script file path. |
||
1000 | */ |
||
1001 | 237 | public function setScriptFile($value) |
|
1005 | |||
1006 | private $_pathInfo; |
||
1007 | |||
1008 | /** |
||
1009 | * Returns the path info of the currently requested URL. |
||
1010 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||
1011 | * The starting and ending slashes are both removed. |
||
1012 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
1013 | * Note, the returned path info is already URL-decoded. |
||
1014 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
1015 | */ |
||
1016 | 18 | public function getPathInfo() |
|
1024 | |||
1025 | /** |
||
1026 | * Sets the path info of the current request. |
||
1027 | * This method is mainly provided for testing purpose. |
||
1028 | * @param string $value the path info of the current request |
||
1029 | */ |
||
1030 | 19 | public function setPathInfo($value) |
|
1034 | |||
1035 | /** |
||
1036 | * Resolves the path info part of the currently requested URL. |
||
1037 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||
1038 | * The starting slashes are both removed (ending slashes will be kept). |
||
1039 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
1040 | * Note, the returned path info is decoded. |
||
1041 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
1042 | */ |
||
1043 | protected function resolvePathInfo() |
||
1087 | |||
1088 | /** |
||
1089 | * Returns the currently requested absolute URL. |
||
1090 | * This is a shortcut to the concatenation of [[hostInfo]] and [[url]]. |
||
1091 | * @return string the currently requested absolute URL. |
||
1092 | */ |
||
1093 | public function getAbsoluteUrl() |
||
1097 | |||
1098 | private $_url; |
||
1099 | |||
1100 | /** |
||
1101 | * Returns the currently requested relative URL. |
||
1102 | * This refers to the portion of the URL that is after the [[hostInfo]] part. |
||
1103 | * It includes the [[queryString]] part if any. |
||
1104 | * @return string the currently requested relative URL. Note that the URI returned may be URL-encoded depending on the client. |
||
1105 | * @throws InvalidConfigException if the URL cannot be determined due to unusual server configuration |
||
1106 | */ |
||
1107 | 11 | public function getUrl() |
|
1115 | |||
1116 | /** |
||
1117 | * Sets the currently requested relative URL. |
||
1118 | * The URI must refer to the portion that is after [[hostInfo]]. |
||
1119 | * Note that the URI should be URL-encoded. |
||
1120 | * @param string $value the request URI to be set |
||
1121 | */ |
||
1122 | 24 | public function setUrl($value) |
|
1126 | |||
1127 | /** |
||
1128 | * Resolves the request URI portion for the currently requested URL. |
||
1129 | * This refers to the portion that is after the [[hostInfo]] part. It includes the [[queryString]] part if any. |
||
1130 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
1131 | * @return string|bool the request URI portion for the currently requested URL. |
||
1132 | * Note that the URI returned may be URL-encoded depending on the client. |
||
1133 | * @throws InvalidConfigException if the request URI cannot be determined due to unusual server configuration |
||
1134 | */ |
||
1135 | 3 | protected function resolveRequestUri() |
|
1155 | |||
1156 | /** |
||
1157 | * Returns part of the request URL that is after the question mark. |
||
1158 | * @return string part of the request URL that is after the question mark |
||
1159 | */ |
||
1160 | public function getQueryString() |
||
1164 | |||
1165 | /** |
||
1166 | * Return if the request is sent via secure channel (https). |
||
1167 | * @return bool if the request is sent via secure channel (https) |
||
1168 | */ |
||
1169 | 37 | public function getIsSecureConnection() |
|
1186 | |||
1187 | /** |
||
1188 | * Returns the server name. |
||
1189 | * @return string server name, null if not available |
||
1190 | */ |
||
1191 | 1 | public function getServerName() |
|
1195 | |||
1196 | /** |
||
1197 | * Returns the server port number. |
||
1198 | * @return int|null server port number, null if not available |
||
1199 | */ |
||
1200 | 1 | public function getServerPort() |
|
1204 | |||
1205 | /** |
||
1206 | * Returns the URL referrer. |
||
1207 | * @return string|null URL referrer, null if not available |
||
1208 | */ |
||
1209 | public function getReferrer() |
||
1216 | |||
1217 | /** |
||
1218 | * Returns the URL origin of a CORS request. |
||
1219 | * |
||
1220 | * The return value is taken from the `Origin` [[getHeaders()|header]] sent by the browser. |
||
1221 | * |
||
1222 | * Note that the origin request header indicates where a fetch originates from. |
||
1223 | * It doesn't include any path information, but only the server name. |
||
1224 | * It is sent with a CORS requests, as well as with POST requests. |
||
1225 | * It is similar to the referer header, but, unlike this header, it doesn't disclose the whole path. |
||
1226 | * Please refer to <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin> for more information. |
||
1227 | * |
||
1228 | * @return string|null URL origin of a CORS request, `null` if not available. |
||
1229 | * @see getHeaders() |
||
1230 | * @since 2.0.13 |
||
1231 | */ |
||
1232 | 1 | public function getOrigin() |
|
1236 | |||
1237 | /** |
||
1238 | * Returns the user agent. |
||
1239 | * @return string|null user agent, null if not available |
||
1240 | */ |
||
1241 | public function getUserAgent() |
||
1248 | |||
1249 | /** |
||
1250 | * Returns the user IP address. |
||
1251 | * The IP is determined using headers and / or `$_SERVER` variables. |
||
1252 | * @return string|null user IP address, null if not available |
||
1253 | */ |
||
1254 | 32 | public function getUserIP() |
|
1264 | |||
1265 | /** |
||
1266 | * Returns the user host name. |
||
1267 | * The HOST is determined using headers and / or `$_SERVER` variables. |
||
1268 | * @return string|null user host name, null if not available |
||
1269 | */ |
||
1270 | public function getUserHost() |
||
1280 | |||
1281 | /** |
||
1282 | * Returns the IP on the other end of this connection. |
||
1283 | * This is always the next hop, any headers are ignored. |
||
1284 | * @return string|null remote IP address, `null` if not available. |
||
1285 | * @since 2.0.13 |
||
1286 | */ |
||
1287 | 47 | public function getRemoteIP() |
|
1291 | |||
1292 | /** |
||
1293 | * Returns the host name of the other end of this connection. |
||
1294 | * This is always the next hop, any headers are ignored. |
||
1295 | * @return string|null remote host name, `null` if not available |
||
1296 | * @see getUserHost() |
||
1297 | * @see getRemoteIP() |
||
1298 | * @since 2.0.13 |
||
1299 | */ |
||
1300 | public function getRemoteHost() |
||
1304 | |||
1305 | /** |
||
1306 | * @return string|null the username sent via HTTP authentication, null if the username is not given |
||
1307 | */ |
||
1308 | 10 | public function getAuthUser() |
|
1312 | |||
1313 | /** |
||
1314 | * @return string|null the password sent via HTTP authentication, null if the password is not given |
||
1315 | */ |
||
1316 | 10 | public function getAuthPassword() |
|
1320 | |||
1321 | private $_port; |
||
1322 | |||
1323 | /** |
||
1324 | * Returns the port to use for insecure requests. |
||
1325 | * Defaults to 80, or the port specified by the server if the current |
||
1326 | * request is insecure. |
||
1327 | * @return int port number for insecure requests. |
||
1328 | * @see setPort() |
||
1329 | */ |
||
1330 | public function getPort() |
||
1338 | |||
1339 | /** |
||
1340 | * Sets the port to use for insecure requests. |
||
1341 | * This setter is provided in case a custom port is necessary for certain |
||
1342 | * server configurations. |
||
1343 | * @param int $value port number. |
||
1344 | */ |
||
1345 | public function setPort($value) |
||
1352 | |||
1353 | private $_securePort; |
||
1354 | |||
1355 | /** |
||
1356 | * Returns the port to use for secure requests. |
||
1357 | * Defaults to 443, or the port specified by the server if the current |
||
1358 | * request is secure. |
||
1359 | * @return int port number for secure requests. |
||
1360 | * @see setSecurePort() |
||
1361 | */ |
||
1362 | public function getSecurePort() |
||
1370 | |||
1371 | /** |
||
1372 | * Sets the port to use for secure requests. |
||
1373 | * This setter is provided in case a custom port is necessary for certain |
||
1374 | * server configurations. |
||
1375 | * @param int $value port number. |
||
1376 | */ |
||
1377 | public function setSecurePort($value) |
||
1384 | |||
1385 | private $_contentTypes; |
||
1386 | |||
1387 | /** |
||
1388 | * Returns the content types acceptable by the end user. |
||
1389 | * |
||
1390 | * This is determined by the `Accept` HTTP header. For example, |
||
1391 | * |
||
1392 | * ```php |
||
1393 | * $_SERVER['HTTP_ACCEPT'] = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
1394 | * $types = $request->getAcceptableContentTypes(); |
||
1395 | * print_r($types); |
||
1396 | * // displays: |
||
1397 | * // [ |
||
1398 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||
1399 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||
1400 | * // 'text/plain' => ['q' => 0.5], |
||
1401 | * // ] |
||
1402 | * ``` |
||
1403 | * |
||
1404 | * @return array the content types ordered by the quality score. Types with the highest scores |
||
1405 | * will be returned first. The array keys are the content types, while the array values |
||
1406 | * are the corresponding quality score and other parameters as given in the header. |
||
1407 | */ |
||
1408 | 3 | public function getAcceptableContentTypes() |
|
1420 | |||
1421 | /** |
||
1422 | * Sets the acceptable content types. |
||
1423 | * Please refer to [[getAcceptableContentTypes()]] on the format of the parameter. |
||
1424 | * @param array $value the content types that are acceptable by the end user. They should |
||
1425 | * be ordered by the preference level. |
||
1426 | * @see getAcceptableContentTypes() |
||
1427 | * @see parseAcceptHeader() |
||
1428 | */ |
||
1429 | 1 | public function setAcceptableContentTypes($value) |
|
1433 | |||
1434 | /** |
||
1435 | * Returns request content-type |
||
1436 | * The Content-Type header field indicates the MIME type of the data |
||
1437 | * contained in [[getBody()]] or, in the case of the HEAD method, the |
||
1438 | * media type that would have been sent had the request been a GET. |
||
1439 | * For the MIME-types the user expects in response, see [[acceptableContentTypes]]. |
||
1440 | * @return string request content-type. Empty string is returned if this information is not available. |
||
1441 | * @link https://tools.ietf.org/html/rfc2616#section-14.17 |
||
1442 | * HTTP 1.1 header field definitions |
||
1443 | */ |
||
1444 | 12 | public function getContentType() |
|
1448 | |||
1449 | private $_languages; |
||
1450 | |||
1451 | /** |
||
1452 | * Returns the languages acceptable by the end user. |
||
1453 | * This is determined by the `Accept-Language` HTTP header. |
||
1454 | * @return array the languages ordered by the preference level. The first element |
||
1455 | * represents the most preferred language. |
||
1456 | */ |
||
1457 | 1 | public function getAcceptableLanguages() |
|
1469 | |||
1470 | /** |
||
1471 | * @param array $value the languages that are acceptable by the end user. They should |
||
1472 | * be ordered by the preference level. |
||
1473 | */ |
||
1474 | 1 | public function setAcceptableLanguages($value) |
|
1478 | |||
1479 | /** |
||
1480 | * Parses the given `Accept` (or `Accept-Language`) header. |
||
1481 | * |
||
1482 | * This method will return the acceptable values with their quality scores and the corresponding parameters |
||
1483 | * as specified in the given `Accept` header. The array keys of the return value are the acceptable values, |
||
1484 | * while the array values consisting of the corresponding quality scores and parameters. The acceptable |
||
1485 | * values with the highest quality scores will be returned first. For example, |
||
1486 | * |
||
1487 | * ```php |
||
1488 | * $header = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
1489 | * $accepts = $request->parseAcceptHeader($header); |
||
1490 | * print_r($accepts); |
||
1491 | * // displays: |
||
1492 | * // [ |
||
1493 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||
1494 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||
1495 | * // 'text/plain' => ['q' => 0.5], |
||
1496 | * // ] |
||
1497 | * ``` |
||
1498 | * |
||
1499 | * @param string $header the header to be parsed |
||
1500 | * @return array the acceptable values ordered by their quality score. The values with the highest scores |
||
1501 | * will be returned first. |
||
1502 | */ |
||
1503 | 3 | public function parseAcceptHeader($header) |
|
1570 | |||
1571 | /** |
||
1572 | * Returns the user-preferred language that should be used by this application. |
||
1573 | * The language resolution is based on the user preferred languages and the languages |
||
1574 | * supported by the application. The method will try to find the best match. |
||
1575 | * @param array $languages a list of the languages supported by the application. If this is empty, the current |
||
1576 | * application language will be returned without further processing. |
||
1577 | * @return string the language that the application should use. |
||
1578 | */ |
||
1579 | 1 | public function getPreferredLanguage(array $languages = []) |
|
1601 | |||
1602 | /** |
||
1603 | * Gets the Etags. |
||
1604 | * |
||
1605 | * @return array The entity tags |
||
1606 | */ |
||
1607 | public function getETags() |
||
1615 | |||
1616 | /** |
||
1617 | * Returns the cookie collection. |
||
1618 | * |
||
1619 | * Through the returned cookie collection, you may access a cookie using the following syntax: |
||
1620 | * |
||
1621 | * ```php |
||
1622 | * $cookie = $request->cookies['name'] |
||
1623 | * if ($cookie !== null) { |
||
1624 | * $value = $cookie->value; |
||
1625 | * } |
||
1626 | * |
||
1627 | * // alternatively |
||
1628 | * $value = $request->cookies->getValue('name'); |
||
1629 | * ``` |
||
1630 | * |
||
1631 | * @return CookieCollection the cookie collection. |
||
1632 | */ |
||
1633 | 33 | public function getCookies() |
|
1643 | |||
1644 | /** |
||
1645 | * Converts `$_COOKIE` into an array of [[Cookie]]. |
||
1646 | * @return array the cookies obtained from request |
||
1647 | * @throws InvalidConfigException if [[cookieValidationKey]] is not set when [[enableCookieValidation]] is true |
||
1648 | */ |
||
1649 | 33 | protected function loadCookies() |
|
1685 | |||
1686 | /** |
||
1687 | * Returns uploaded files for this request. |
||
1688 | * Uploaded files are returned in format according to [PSR-7 Uploaded Files specs](http://www.php-fig.org/psr/psr-7/#16-uploaded-files). |
||
1689 | * @return array uploaded files. |
||
1690 | * @since 2.1.0 |
||
1691 | */ |
||
1692 | 12 | public function getUploadedFiles() |
|
1702 | |||
1703 | /** |
||
1704 | * Sets uploaded files for this request. |
||
1705 | * Data structure for the uploaded files should follow [PSR-7 Uploaded Files specs](http://www.php-fig.org/psr/psr-7/#16-uploaded-files). |
||
1706 | * @param array|null $uploadedFiles uploaded files. |
||
1707 | * @since 2.1.0 |
||
1708 | */ |
||
1709 | 6 | public function setUploadedFiles($uploadedFiles) |
|
1713 | |||
1714 | /** |
||
1715 | * Initializes default uploaded files data structure parsing super-global $_FILES. |
||
1716 | * @see http://www.php-fig.org/psr/psr-7/#16-uploaded-files |
||
1717 | * @return array uploaded files. |
||
1718 | * @since 2.1.0 |
||
1719 | */ |
||
1720 | 6 | protected function defaultUploadedFiles() |
|
1730 | |||
1731 | /** |
||
1732 | * Populates uploaded files array from $_FILE data structure recursively. |
||
1733 | * @param array $files uploaded files array to be populated. |
||
1734 | * @param mixed $names file names provided by PHP |
||
1735 | * @param mixed $tempNames temporary file names provided by PHP |
||
1736 | * @param mixed $types file types provided by PHP |
||
1737 | * @param mixed $sizes file sizes provided by PHP |
||
1738 | * @param mixed $errors uploading issues provided by PHP |
||
1739 | * @since 2.1.0 |
||
1740 | */ |
||
1741 | 3 | private function populateUploadedFileRecursive(&$files, $names, $tempNames, $types, $sizes, $errors) |
|
1759 | |||
1760 | /** |
||
1761 | * Returns an uploaded file according to the given name. |
||
1762 | * Name can be either a string HTML form input name, e.g. 'Item[file]' or array path, e.g. `['Item', 'file']`. |
||
1763 | * Note: this method returns `null` in case given name matches multiple files. |
||
1764 | * @param string|array $name HTML form input name or array path. |
||
1765 | * @return UploadedFileInterface|null uploaded file instance, `null` - if not found. |
||
1766 | * @since 2.1.0 |
||
1767 | */ |
||
1768 | 1 | public function getUploadedFileByName($name) |
|
1776 | |||
1777 | /** |
||
1778 | * Returns the list of uploaded file instances according to the given name. |
||
1779 | * Name can be either a string HTML form input name, e.g. 'Item[file]' or array path, e.g. `['Item', 'file']`. |
||
1780 | * Note: this method does NOT preserve uploaded files structure - it returns instances in single-level array (list), |
||
1781 | * even if they are set by nested keys. |
||
1782 | * @param string|array $name HTML form input name or array path. |
||
1783 | * @return UploadedFileInterface[] list of uploaded file instances. |
||
1784 | * @since 2.1.0 |
||
1785 | */ |
||
1786 | 1 | public function getUploadedFilesByName($name) |
|
1797 | |||
1798 | /** |
||
1799 | * Finds the uploaded file or set of uploaded files inside [[$uploadedFiles]] according to given name. |
||
1800 | * Name can be either a string HTML form input name, e.g. 'Item[file]' or array path, e.g. `['Item', 'file']`. |
||
1801 | * @param string|array $name HTML form input name or array path. |
||
1802 | * @return UploadedFileInterface|array|null |
||
1803 | * @since 2.1.0 |
||
1804 | */ |
||
1805 | 2 | private function findUploadedFiles($name) |
|
1812 | |||
1813 | /** |
||
1814 | * Reduces complex uploaded files structure to the single-level array (list). |
||
1815 | * @param array $uploadedFiles raw set of the uploaded files. |
||
1816 | * @return UploadedFileInterface[] list of uploaded files. |
||
1817 | * @since 2.1.0 |
||
1818 | */ |
||
1819 | private function reduceUploadedFiles($uploadedFiles) |
||
1830 | |||
1831 | private $_csrfToken; |
||
1832 | |||
1833 | /** |
||
1834 | * Returns the token used to perform CSRF validation. |
||
1835 | * |
||
1836 | * This token is generated in a way to prevent [BREACH attacks](http://breachattack.com/). It may be passed |
||
1837 | * along via a hidden field of an HTML form or an HTTP header value to support CSRF validation. |
||
1838 | * @param bool $regenerate whether to regenerate CSRF token. When this parameter is true, each time |
||
1839 | * this method is called, a new CSRF token will be generated and persisted (in session or cookie). |
||
1840 | * @return string the token used to perform CSRF validation. |
||
1841 | */ |
||
1842 | 37 | public function getCsrfToken($regenerate = false) |
|
1853 | |||
1854 | /** |
||
1855 | * Loads the CSRF token from cookie or session. |
||
1856 | * @return string the CSRF token loaded from cookie or session. Null is returned if the cookie or session |
||
1857 | * does not have CSRF token. |
||
1858 | */ |
||
1859 | 37 | protected function loadCsrfToken() |
|
1867 | |||
1868 | /** |
||
1869 | * Generates an unmasked random token used to perform CSRF validation. |
||
1870 | * @return string the random token for CSRF validation. |
||
1871 | */ |
||
1872 | 35 | protected function generateCsrfToken() |
|
1884 | |||
1885 | /** |
||
1886 | * @return string the CSRF token sent via [[CSRF_HEADER]] by browser. Null is returned if no such header is sent. |
||
1887 | */ |
||
1888 | 3 | public function getCsrfTokenFromHeader() |
|
1892 | |||
1893 | /** |
||
1894 | * Creates a cookie with a randomly generated CSRF token. |
||
1895 | * Initial values specified in [[csrfCookie]] will be applied to the generated cookie. |
||
1896 | * @param string $token the CSRF token |
||
1897 | * @return Cookie the generated cookie |
||
1898 | * @see enableCsrfValidation |
||
1899 | */ |
||
1900 | 33 | protected function createCsrfCookie($token) |
|
1907 | |||
1908 | /** |
||
1909 | * Performs the CSRF validation. |
||
1910 | * |
||
1911 | * This method will validate the user-provided CSRF token by comparing it with the one stored in cookie or session. |
||
1912 | * This method is mainly called in [[Controller::beforeAction()]]. |
||
1913 | * |
||
1914 | * Note that the method will NOT perform CSRF validation if [[enableCsrfValidation]] is false or the HTTP method |
||
1915 | * is among GET, HEAD or OPTIONS. |
||
1916 | * |
||
1917 | * @param string $clientSuppliedToken the user-provided CSRF token to be validated. If null, the token will be retrieved from |
||
1918 | * the [[csrfParam]] POST field or HTTP header. |
||
1919 | * This parameter is available since version 2.0.4. |
||
1920 | * @return bool whether CSRF token is valid. If [[enableCsrfValidation]] is false, this method will return true. |
||
1921 | */ |
||
1922 | 5 | public function validateCsrfToken($clientSuppliedToken = null) |
|
1939 | |||
1940 | /** |
||
1941 | * Validates CSRF token. |
||
1942 | * |
||
1943 | * @param string $clientSuppliedToken The masked client-supplied token. |
||
1944 | * @param string $trueToken The masked true token. |
||
1945 | * @return bool |
||
1946 | */ |
||
1947 | 3 | private function validateCsrfTokenInternal($clientSuppliedToken, $trueToken) |
|
1957 | |||
1958 | /** |
||
1959 | * {@inheritdoc} |
||
1960 | */ |
||
1961 | 2 | public function __clone() |
|
1971 | } |
||
1972 |
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.