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 |
||
82 | class Request extends \yii\base\Request |
||
83 | { |
||
84 | /** |
||
85 | * The name of the HTTP header for sending CSRF token. |
||
86 | */ |
||
87 | const CSRF_HEADER = 'X-CSRF-Token'; |
||
88 | /** |
||
89 | * The length of the CSRF token mask. |
||
90 | */ |
||
91 | const CSRF_MASK_LENGTH = 8; |
||
92 | |||
93 | /** |
||
94 | * @var boolean whether to enable CSRF (Cross-Site Request Forgery) validation. Defaults to true. |
||
95 | * When CSRF validation is enabled, forms submitted to an Yii Web application must be originated |
||
96 | * from the same application. If not, a 400 HTTP exception will be raised. |
||
97 | * |
||
98 | * Note, this feature requires that the user client accepts cookie. Also, to use this feature, |
||
99 | * forms submitted via POST method must contain a hidden input whose name is specified by [[csrfParam]]. |
||
100 | * You may use [[\yii\helpers\Html::beginForm()]] to generate his hidden input. |
||
101 | * |
||
102 | * In JavaScript, you may get the values of [[csrfParam]] and [[csrfToken]] via `yii.getCsrfParam()` and |
||
103 | * `yii.getCsrfToken()`, respectively. The [[\yii\web\YiiAsset]] asset must be registered. |
||
104 | * You also need to include CSRF meta tags in your pages by using [[\yii\helpers\Html::csrfMetaTags()]]. |
||
105 | * |
||
106 | * @see Controller::enableCsrfValidation |
||
107 | * @see http://en.wikipedia.org/wiki/Cross-site_request_forgery |
||
108 | */ |
||
109 | public $enableCsrfValidation = true; |
||
110 | /** |
||
111 | * @var string the name of the token used to prevent CSRF. Defaults to '_csrf'. |
||
112 | * This property is used only when [[enableCsrfValidation]] is true. |
||
113 | */ |
||
114 | public $csrfParam = '_csrf'; |
||
115 | /** |
||
116 | * @var array the configuration for creating the CSRF [[Cookie|cookie]]. This property is used only when |
||
117 | * both [[enableCsrfValidation]] and [[enableCsrfCookie]] are true. |
||
118 | */ |
||
119 | public $csrfCookie = ['httpOnly' => true]; |
||
120 | /** |
||
121 | * @var boolean whether to use cookie to persist CSRF token. If false, CSRF token will be stored |
||
122 | * in session under the name of [[csrfParam]]. Note that while storing CSRF tokens in session increases |
||
123 | * security, it requires starting a session for every page, which will degrade your site performance. |
||
124 | */ |
||
125 | public $enableCsrfCookie = true; |
||
126 | /** |
||
127 | * @var boolean whether cookies should be validated to ensure they are not tampered. Defaults to true. |
||
128 | */ |
||
129 | public $enableCookieValidation = true; |
||
130 | /** |
||
131 | * @var string a secret key used for cookie validation. This property must be set if [[enableCookieValidation]] is true. |
||
132 | */ |
||
133 | public $cookieValidationKey; |
||
134 | /** |
||
135 | * @var string the name of the POST parameter that is used to indicate if a request is a PUT, PATCH or DELETE |
||
136 | * request tunneled through POST. Defaults to '_method'. |
||
137 | * @see getMethod() |
||
138 | * @see getBodyParams() |
||
139 | */ |
||
140 | public $methodParam = '_method'; |
||
141 | /** |
||
142 | * @var array the parsers for converting the raw HTTP request body into [[bodyParams]]. |
||
143 | * The array keys are the request `Content-Types`, and the array values are the |
||
144 | * corresponding configurations for [[Yii::createObject|creating the parser objects]]. |
||
145 | * A parser must implement the [[RequestParserInterface]]. |
||
146 | * |
||
147 | * To enable parsing for JSON requests you can use the [[JsonParser]] class like in the following example: |
||
148 | * |
||
149 | * ``` |
||
150 | * [ |
||
151 | * 'application/json' => \yii\web\JsonParser::class, |
||
152 | * ] |
||
153 | * ``` |
||
154 | * |
||
155 | * To register a parser for parsing all request types you can use `'*'` as the array key. |
||
156 | * This one will be used as a fallback in case no other types match. |
||
157 | * |
||
158 | * @see getBodyParams() |
||
159 | */ |
||
160 | public $parsers = []; |
||
161 | |||
162 | /** |
||
163 | * @var CookieCollection Collection of request cookies. |
||
164 | */ |
||
165 | private $_cookies; |
||
166 | /** |
||
167 | * @var HeaderCollection Collection of request headers. |
||
168 | */ |
||
169 | private $_headers; |
||
170 | |||
171 | |||
172 | /** |
||
173 | * Resolves the current request into a route and the associated parameters. |
||
174 | * @return array the first element is the route, and the second is the associated parameters. |
||
175 | * @throws NotFoundHttpException if the request cannot be resolved. |
||
176 | */ |
||
177 | 1 | public function resolve() |
|
178 | { |
||
179 | 1 | $result = Yii::$app->getUrlManager()->parseRequest($this); |
|
180 | 1 | if ($result !== false) { |
|
181 | 1 | list($route, $params) = $result; |
|
182 | 1 | if ($this->_queryParams === null) { |
|
183 | 1 | $_GET = $params + $_GET; // preserve numeric keys |
|
184 | 1 | } else { |
|
185 | 1 | $this->_queryParams = $params + $this->_queryParams; |
|
186 | } |
||
187 | 1 | return [$route, $this->getQueryParams()]; |
|
188 | } else { |
||
189 | throw new NotFoundHttpException(Yii::t('yii', 'Page not found.')); |
||
190 | } |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Returns the header collection. |
||
195 | * The header collection contains incoming HTTP headers. |
||
196 | * @return HeaderCollection the header collection |
||
197 | */ |
||
198 | 6 | public function getHeaders() |
|
199 | { |
||
200 | 6 | if ($this->_headers === null) { |
|
201 | 6 | $this->_headers = new HeaderCollection; |
|
202 | 6 | if (function_exists('getallheaders')) { |
|
203 | $headers = getallheaders(); |
||
204 | 6 | } elseif (function_exists('http_get_request_headers')) { |
|
205 | $headers = http_get_request_headers(); |
||
206 | } else { |
||
207 | 6 | foreach ($_SERVER as $name => $value) { |
|
208 | 6 | if (strncmp($name, 'HTTP_', 5) === 0) { |
|
209 | 1 | $name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))); |
|
210 | 1 | $this->_headers->add($name, $value); |
|
211 | 1 | } |
|
212 | 6 | } |
|
213 | |||
214 | 6 | return $this->_headers; |
|
215 | } |
||
216 | foreach ($headers as $name => $value) { |
||
217 | $this->_headers->add($name, $value); |
||
218 | } |
||
219 | } |
||
220 | |||
221 | 6 | return $this->_headers; |
|
222 | } |
||
223 | |||
224 | /** |
||
225 | * Returns the method of the current request (e.g. GET, POST, HEAD, PUT, PATCH, DELETE). |
||
226 | * @return string request method, such as GET, POST, HEAD, PUT, PATCH, DELETE. |
||
227 | * The value returned is turned into upper case. |
||
228 | */ |
||
229 | 9 | public function getMethod() |
|
245 | |||
246 | /** |
||
247 | * Returns whether this is a GET request. |
||
248 | * @return boolean whether this is a GET request. |
||
249 | */ |
||
250 | 2 | public function getIsGet() |
|
254 | |||
255 | /** |
||
256 | * Returns whether this is an OPTIONS request. |
||
257 | * @return boolean whether this is a OPTIONS request. |
||
258 | */ |
||
259 | public function getIsOptions() |
||
263 | |||
264 | /** |
||
265 | * Returns whether this is a HEAD request. |
||
266 | * @return boolean whether this is a HEAD request. |
||
267 | */ |
||
268 | public function getIsHead() |
||
272 | |||
273 | /** |
||
274 | * Returns whether this is a POST request. |
||
275 | * @return boolean whether this is a POST request. |
||
276 | */ |
||
277 | public function getIsPost() |
||
281 | |||
282 | /** |
||
283 | * Returns whether this is a DELETE request. |
||
284 | * @return boolean whether this is a DELETE request. |
||
285 | */ |
||
286 | public function getIsDelete() |
||
290 | |||
291 | /** |
||
292 | * Returns whether this is a PUT request. |
||
293 | * @return boolean whether this is a PUT request. |
||
294 | */ |
||
295 | public function getIsPut() |
||
299 | |||
300 | /** |
||
301 | * Returns whether this is a PATCH request. |
||
302 | * @return boolean whether this is a PATCH request. |
||
303 | */ |
||
304 | public function getIsPatch() |
||
308 | |||
309 | /** |
||
310 | * Returns whether this is an AJAX (XMLHttpRequest) request. |
||
311 | * |
||
312 | * Note that jQuery doesn't set the header in case of cross domain |
||
313 | * requests: https://stackoverflow.com/questions/8163703/cross-domain-ajax-doesnt-send-x-requested-with-header |
||
314 | * |
||
315 | * @return boolean whether this is an AJAX (XMLHttpRequest) request. |
||
316 | */ |
||
317 | 2 | public function getIsAjax() |
|
321 | |||
322 | /** |
||
323 | * Returns whether this is a PJAX request |
||
324 | * @return boolean whether this is a PJAX request |
||
325 | */ |
||
326 | 1 | public function getIsPjax() |
|
330 | |||
331 | /** |
||
332 | * Returns whether this is an Adobe Flash or Flex request. |
||
333 | * @return boolean whether this is an Adobe Flash or Adobe Flex request. |
||
334 | */ |
||
335 | public function getIsFlash() |
||
340 | |||
341 | private $_rawBody; |
||
342 | |||
343 | /** |
||
344 | * Returns the raw HTTP request body. |
||
345 | * @return string the request body |
||
346 | */ |
||
347 | public function getRawBody() |
||
355 | |||
356 | /** |
||
357 | * Sets the raw HTTP request body, this method is mainly used by test scripts to simulate raw HTTP requests. |
||
358 | * @param string $rawBody the request body |
||
359 | */ |
||
360 | public function setRawBody($rawBody) |
||
364 | |||
365 | private $_bodyParams; |
||
366 | |||
367 | /** |
||
368 | * Returns the request parameters given in the request body. |
||
369 | * |
||
370 | * Request parameters are determined using the parsers configured in [[parsers]] property. |
||
371 | * If no parsers are configured for the current [[contentType]] it uses the PHP function `mb_parse_str()` |
||
372 | * to parse the [[rawBody|request body]]. |
||
373 | * @return array the request parameters given in the request body. |
||
374 | * @throws \yii\base\InvalidConfigException if a registered parser does not implement the [[RequestParserInterface]]. |
||
375 | * @see getMethod() |
||
376 | * @see getBodyParam() |
||
377 | * @see setBodyParams() |
||
378 | */ |
||
379 | public function getBodyParams() |
||
417 | |||
418 | /** |
||
419 | * Sets the request body parameters. |
||
420 | * @param array $values the request body parameters (name-value pairs) |
||
421 | * @see getBodyParam() |
||
422 | * @see getBodyParams() |
||
423 | */ |
||
424 | public function setBodyParams($values) |
||
428 | |||
429 | /** |
||
430 | * Returns the named request body parameter value. |
||
431 | * If the parameter does not exist, the second parameter passed to this method will be returned. |
||
432 | * @param string $name the parameter name |
||
433 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
434 | * @return mixed the parameter value |
||
435 | * @see getBodyParams() |
||
436 | * @see setBodyParams() |
||
437 | */ |
||
438 | public function getBodyParam($name, $defaultValue = null) |
||
444 | |||
445 | /** |
||
446 | * Returns POST parameter with a given name. If name isn't specified, returns an array of all POST parameters. |
||
447 | * |
||
448 | * @param string $name the parameter name |
||
449 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
450 | * @return array|mixed |
||
451 | */ |
||
452 | public function post($name = null, $defaultValue = null) |
||
460 | |||
461 | private $_queryParams; |
||
462 | |||
463 | /** |
||
464 | * Returns the request parameters given in the [[queryString]]. |
||
465 | * |
||
466 | * This method will return the contents of `$_GET` if params where not explicitly set. |
||
467 | * @return array the request GET parameter values. |
||
468 | * @see setQueryParams() |
||
469 | */ |
||
470 | 13 | public function getQueryParams() |
|
478 | |||
479 | /** |
||
480 | * Sets the request [[queryString]] parameters. |
||
481 | * @param array $values the request query parameters (name-value pairs) |
||
482 | * @see getQueryParam() |
||
483 | * @see getQueryParams() |
||
484 | */ |
||
485 | 2 | public function setQueryParams($values) |
|
489 | |||
490 | /** |
||
491 | * Returns GET parameter with a given name. If name isn't specified, returns an array of all GET parameters. |
||
492 | * |
||
493 | * @param string $name the parameter name |
||
494 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
495 | * @return array|mixed |
||
496 | */ |
||
497 | 5 | public function get($name = null, $defaultValue = null) |
|
505 | |||
506 | /** |
||
507 | * Returns the named GET parameter value. |
||
508 | * If the GET parameter does not exist, the second parameter passed to this method will be returned. |
||
509 | * @param string $name the GET parameter name. |
||
510 | * @param mixed $defaultValue the default parameter value if the GET parameter does not exist. |
||
511 | * @return mixed the GET parameter value |
||
512 | * @see getBodyParam() |
||
513 | */ |
||
514 | 6 | public function getQueryParam($name, $defaultValue = null) |
|
520 | |||
521 | private $_hostInfo; |
||
522 | |||
523 | /** |
||
524 | * Returns the schema and host part of the current request URL. |
||
525 | * The returned URL does not have an ending slash. |
||
526 | * By default this is determined based on the user request information. |
||
527 | * You may explicitly specify it by setting the [[setHostInfo()|hostInfo]] property. |
||
528 | * @return string schema and hostname part (with port number if needed) of the request URL (e.g. `http://www.yiiframework.com`), |
||
529 | * null if can't be obtained from `$_SERVER` and wasn't set. |
||
530 | * @see setHostInfo() |
||
531 | */ |
||
532 | 4 | public function getHostInfo() |
|
550 | |||
551 | /** |
||
552 | * Sets the schema and host part of the application URL. |
||
553 | * This setter is provided in case the schema and hostname cannot be determined |
||
554 | * on certain Web servers. |
||
555 | * @param string $value the schema and host part of the application URL. The trailing slashes will be removed. |
||
556 | */ |
||
557 | 18 | public function setHostInfo($value) |
|
561 | |||
562 | private $_baseUrl; |
||
563 | |||
564 | /** |
||
565 | * Returns the relative URL for the application. |
||
566 | * This is similar to [[scriptUrl]] except that it does not include the script file name, |
||
567 | * and the ending slashes are removed. |
||
568 | * @return string the relative URL for the application |
||
569 | * @see setScriptUrl() |
||
570 | */ |
||
571 | 96 | public function getBaseUrl() |
|
579 | |||
580 | /** |
||
581 | * Sets the relative URL for the application. |
||
582 | * By default the URL is determined based on the entry script URL. |
||
583 | * This setter is provided in case you want to change this behavior. |
||
584 | * @param string $value the relative URL for the application |
||
585 | */ |
||
586 | 1 | public function setBaseUrl($value) |
|
590 | |||
591 | private $_scriptUrl; |
||
592 | |||
593 | /** |
||
594 | * Returns the relative URL of the entry script. |
||
595 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
596 | * @return string the relative URL of the entry script. |
||
597 | * @throws InvalidConfigException if unable to determine the entry script URL |
||
598 | */ |
||
599 | 97 | public function getScriptUrl() |
|
621 | |||
622 | /** |
||
623 | * Sets the relative URL for the application entry script. |
||
624 | * This setter is provided in case the entry script URL cannot be determined |
||
625 | * on certain Web servers. |
||
626 | * @param string $value the relative URL for the application entry script. |
||
627 | */ |
||
628 | 101 | public function setScriptUrl($value) |
|
632 | |||
633 | private $_scriptFile; |
||
|
|||
634 | |||
635 | /** |
||
636 | * Returns the entry script file path. |
||
637 | * The default implementation will simply return `$_SERVER['SCRIPT_FILENAME']`. |
||
638 | * @return string the entry script file path |
||
639 | * @throws InvalidConfigException |
||
640 | */ |
||
641 | 98 | public function getScriptFile() |
|
651 | |||
652 | /** |
||
653 | * Sets the entry script file path. |
||
654 | * The entry script file path normally can be obtained from `$_SERVER['SCRIPT_FILENAME']`. |
||
655 | * If your server configuration does not return the correct value, you may configure |
||
656 | * this property to make it right. |
||
657 | * @param string $value the entry script file path. |
||
658 | */ |
||
659 | 88 | public function setScriptFile($value) |
|
663 | |||
664 | private $_pathInfo; |
||
665 | |||
666 | /** |
||
667 | * Returns the path info of the currently requested URL. |
||
668 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||
669 | * The starting and ending slashes are both removed. |
||
670 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
671 | * Note, the returned path info is already URL-decoded. |
||
672 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
673 | */ |
||
674 | 7 | public function getPathInfo() |
|
682 | |||
683 | /** |
||
684 | * Sets the path info of the current request. |
||
685 | * This method is mainly provided for testing purpose. |
||
686 | * @param string $value the path info of the current request |
||
687 | */ |
||
688 | 7 | public function setPathInfo($value) |
|
692 | |||
693 | /** |
||
694 | * Resolves the path info part of the currently requested URL. |
||
695 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||
696 | * The starting slashes are both removed (ending slashes will be kept). |
||
697 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
698 | * Note, the returned path info is decoded. |
||
699 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
700 | */ |
||
701 | protected function resolvePathInfo() |
||
745 | |||
746 | /** |
||
747 | * Returns the currently requested absolute URL. |
||
748 | * This is a shortcut to the concatenation of [[hostInfo]] and [[url]]. |
||
749 | * @return string the currently requested absolute URL. |
||
750 | */ |
||
751 | public function getAbsoluteUrl() |
||
755 | |||
756 | private $_url; |
||
757 | |||
758 | /** |
||
759 | * Returns the currently requested relative URL. |
||
760 | * This refers to the portion of the URL that is after the [[hostInfo]] part. |
||
761 | * It includes the [[queryString]] part if any. |
||
762 | * @return string the currently requested relative URL. Note that the URI returned is URL-encoded. |
||
763 | * @throws InvalidConfigException if the URL cannot be determined due to unusual server configuration |
||
764 | */ |
||
765 | 7 | public function getUrl() |
|
773 | |||
774 | /** |
||
775 | * Sets the currently requested relative URL. |
||
776 | * The URI must refer to the portion that is after [[hostInfo]]. |
||
777 | * Note that the URI should be URL-encoded. |
||
778 | * @param string $value the request URI to be set |
||
779 | */ |
||
780 | 15 | public function setUrl($value) |
|
784 | |||
785 | /** |
||
786 | * Resolves the request URI portion for the currently requested URL. |
||
787 | * This refers to the portion that is after the [[hostInfo]] part. It includes the [[queryString]] part if any. |
||
788 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
789 | * @return string|boolean the request URI portion for the currently requested URL. |
||
790 | * Note that the URI returned is URL-encoded. |
||
791 | * @throws InvalidConfigException if the request URI cannot be determined due to unusual server configuration |
||
792 | */ |
||
793 | protected function resolveRequestUri() |
||
813 | |||
814 | /** |
||
815 | * Returns part of the request URL that is after the question mark. |
||
816 | * @return string part of the request URL that is after the question mark |
||
817 | */ |
||
818 | public function getQueryString() |
||
822 | |||
823 | /** |
||
824 | * Return if the request is sent via secure channel (https). |
||
825 | * @return boolean if the request is sent via secure channel (https) |
||
826 | */ |
||
827 | 2 | public function getIsSecureConnection() |
|
832 | |||
833 | /** |
||
834 | * Returns the server name. |
||
835 | * @return string server name, null if not available |
||
836 | */ |
||
837 | 1 | public function getServerName() |
|
841 | |||
842 | /** |
||
843 | * Returns the server port number. |
||
844 | * @return integer|null server port number, null if not available |
||
845 | */ |
||
846 | 1 | public function getServerPort() |
|
850 | |||
851 | /** |
||
852 | * Returns the URL referrer. |
||
853 | * @return string|null URL referrer, null if not available |
||
854 | */ |
||
855 | public function getReferrer() |
||
859 | |||
860 | /** |
||
861 | * Returns the user agent. |
||
862 | * @return string|null user agent, null if not available |
||
863 | */ |
||
864 | public function getUserAgent() |
||
868 | |||
869 | /** |
||
870 | * Returns the user IP address. |
||
871 | * @return string|null user IP address, null if not available |
||
872 | */ |
||
873 | 13 | public function getUserIP() |
|
877 | |||
878 | /** |
||
879 | * Returns the user host name. |
||
880 | * @return string|null user host name, null if not available |
||
881 | */ |
||
882 | public function getUserHost() |
||
886 | |||
887 | /** |
||
888 | * @return string|null the username sent via HTTP authentication, null if the username is not given |
||
889 | */ |
||
890 | 10 | public function getAuthUser() |
|
894 | |||
895 | /** |
||
896 | * @return string|null the password sent via HTTP authentication, null if the password is not given |
||
897 | */ |
||
898 | 10 | public function getAuthPassword() |
|
902 | |||
903 | private $_port; |
||
904 | |||
905 | /** |
||
906 | * Returns the port to use for insecure requests. |
||
907 | * Defaults to 80, or the port specified by the server if the current |
||
908 | * request is insecure. |
||
909 | * @return integer port number for insecure requests. |
||
910 | * @see setPort() |
||
911 | */ |
||
912 | public function getPort() |
||
920 | |||
921 | /** |
||
922 | * Sets the port to use for insecure requests. |
||
923 | * This setter is provided in case a custom port is necessary for certain |
||
924 | * server configurations. |
||
925 | * @param integer $value port number. |
||
926 | */ |
||
927 | public function setPort($value) |
||
934 | |||
935 | private $_securePort; |
||
936 | |||
937 | /** |
||
938 | * Returns the port to use for secure requests. |
||
939 | * Defaults to 443, or the port specified by the server if the current |
||
940 | * request is secure. |
||
941 | * @return integer port number for secure requests. |
||
942 | * @see setSecurePort() |
||
943 | */ |
||
944 | public function getSecurePort() |
||
952 | |||
953 | /** |
||
954 | * Sets the port to use for secure requests. |
||
955 | * This setter is provided in case a custom port is necessary for certain |
||
956 | * server configurations. |
||
957 | * @param integer $value port number. |
||
958 | */ |
||
959 | public function setSecurePort($value) |
||
966 | |||
967 | private $_contentTypes; |
||
968 | |||
969 | /** |
||
970 | * Returns the content types acceptable by the end user. |
||
971 | * This is determined by the `Accept` HTTP header. For example, |
||
972 | * |
||
973 | * ```php |
||
974 | * $_SERVER['HTTP_ACCEPT'] = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
975 | * $types = $request->getAcceptableContentTypes(); |
||
976 | * print_r($types); |
||
977 | * // displays: |
||
978 | * // [ |
||
979 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||
980 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||
981 | * // 'text/plain' => ['q' => 0.5], |
||
982 | * // ] |
||
983 | * ``` |
||
984 | * |
||
985 | * @return array the content types ordered by the quality score. Types with the highest scores |
||
986 | * will be returned first. The array keys are the content types, while the array values |
||
987 | * are the corresponding quality score and other parameters as given in the header. |
||
988 | */ |
||
989 | 2 | public function getAcceptableContentTypes() |
|
1001 | |||
1002 | /** |
||
1003 | * Sets the acceptable content types. |
||
1004 | * Please refer to [[getAcceptableContentTypes()]] on the format of the parameter. |
||
1005 | * @param array $value the content types that are acceptable by the end user. They should |
||
1006 | * be ordered by the preference level. |
||
1007 | * @see getAcceptableContentTypes() |
||
1008 | * @see parseAcceptHeader() |
||
1009 | */ |
||
1010 | public function setAcceptableContentTypes($value) |
||
1014 | |||
1015 | /** |
||
1016 | * Returns request content-type |
||
1017 | * The Content-Type header field indicates the MIME type of the data |
||
1018 | * contained in [[getRawBody()]] or, in the case of the HEAD method, the |
||
1019 | * media type that would have been sent had the request been a GET. |
||
1020 | * For the MIME-types the user expects in response, see [[acceptableContentTypes]]. |
||
1021 | * @return string request content-type. Null is returned if this information is not available. |
||
1022 | * @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17 |
||
1023 | * HTTP 1.1 header field definitions |
||
1024 | */ |
||
1025 | public function getContentType() |
||
1036 | |||
1037 | private $_languages; |
||
1038 | |||
1039 | /** |
||
1040 | * Returns the languages acceptable by the end user. |
||
1041 | * This is determined by the `Accept-Language` HTTP header. |
||
1042 | * @return array the languages ordered by the preference level. The first element |
||
1043 | * represents the most preferred language. |
||
1044 | */ |
||
1045 | 1 | public function getAcceptableLanguages() |
|
1057 | |||
1058 | /** |
||
1059 | * @param array $value the languages that are acceptable by the end user. They should |
||
1060 | * be ordered by the preference level. |
||
1061 | */ |
||
1062 | 1 | public function setAcceptableLanguages($value) |
|
1066 | |||
1067 | /** |
||
1068 | * Parses the given `Accept` (or `Accept-Language`) header. |
||
1069 | * |
||
1070 | * This method will return the acceptable values with their quality scores and the corresponding parameters |
||
1071 | * as specified in the given `Accept` header. The array keys of the return value are the acceptable values, |
||
1072 | * while the array values consisting of the corresponding quality scores and parameters. The acceptable |
||
1073 | * values with the highest quality scores will be returned first. For example, |
||
1074 | * |
||
1075 | * ```php |
||
1076 | * $header = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
1077 | * $accepts = $request->parseAcceptHeader($header); |
||
1078 | * print_r($accepts); |
||
1079 | * // displays: |
||
1080 | * // [ |
||
1081 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||
1082 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||
1083 | * // 'text/plain' => ['q' => 0.5], |
||
1084 | * // ] |
||
1085 | * ``` |
||
1086 | * |
||
1087 | * @param string $header the header to be parsed |
||
1088 | * @return array the acceptable values ordered by their quality score. The values with the highest scores |
||
1089 | * will be returned first. |
||
1090 | */ |
||
1091 | 3 | public function parseAcceptHeader($header) |
|
1150 | |||
1151 | /** |
||
1152 | * Returns the user-preferred language that should be used by this application. |
||
1153 | * The language resolution is based on the user preferred languages and the languages |
||
1154 | * supported by the application. The method will try to find the best match. |
||
1155 | * @param array $languages a list of the languages supported by the application. If this is empty, the current |
||
1156 | * application language will be returned without further processing. |
||
1157 | * @return string the language that the application should use. |
||
1158 | */ |
||
1159 | 1 | public function getPreferredLanguage(array $languages = []) |
|
1180 | |||
1181 | /** |
||
1182 | * Gets the Etags. |
||
1183 | * |
||
1184 | * @return array The entity tags |
||
1185 | */ |
||
1186 | public function getETags() |
||
1194 | |||
1195 | /** |
||
1196 | * Returns the cookie collection. |
||
1197 | * Through the returned cookie collection, you may access a cookie using the following syntax: |
||
1198 | * |
||
1199 | * ```php |
||
1200 | * $cookie = $request->cookies['name'] |
||
1201 | * if ($cookie !== null) { |
||
1202 | * $value = $cookie->value; |
||
1203 | * } |
||
1204 | * |
||
1205 | * // alternatively |
||
1206 | * $value = $request->cookies->getValue('name'); |
||
1207 | * ``` |
||
1208 | * |
||
1209 | * @return CookieCollection the cookie collection. |
||
1210 | */ |
||
1211 | 21 | public function getCookies() |
|
1221 | |||
1222 | /** |
||
1223 | * Converts `$_COOKIE` into an array of [[Cookie]]. |
||
1224 | * @return array the cookies obtained from request |
||
1225 | * @throws InvalidConfigException if [[cookieValidationKey]] is not set when [[enableCookieValidation]] is true |
||
1226 | */ |
||
1227 | 21 | protected function loadCookies() |
|
1263 | |||
1264 | private $_csrfToken; |
||
1265 | |||
1266 | /** |
||
1267 | * Returns the token used to perform CSRF validation. |
||
1268 | * |
||
1269 | * This token is generated in a way to prevent [BREACH attacks](http://breachattack.com/). It may be passed |
||
1270 | * along via a hidden field of an HTML form or an HTTP header value to support CSRF validation. |
||
1271 | * @param boolean $regenerate whether to regenerate CSRF token. When this parameter is true, each time |
||
1272 | * this method is called, a new CSRF token will be generated and persisted (in session or cookie). |
||
1273 | * @return string the token used to perform CSRF validation. |
||
1274 | */ |
||
1275 | 22 | public function getCsrfToken($regenerate = false) |
|
1290 | |||
1291 | /** |
||
1292 | * Loads the CSRF token from cookie or session. |
||
1293 | * @return string the CSRF token loaded from cookie or session. Null is returned if the cookie or session |
||
1294 | * does not have CSRF token. |
||
1295 | */ |
||
1296 | 22 | protected function loadCsrfToken() |
|
1304 | |||
1305 | /** |
||
1306 | * Generates an unmasked random token used to perform CSRF validation. |
||
1307 | * @return string the random token for CSRF validation. |
||
1308 | */ |
||
1309 | 22 | protected function generateCsrfToken() |
|
1320 | |||
1321 | /** |
||
1322 | * Returns the XOR result of two strings. |
||
1323 | * If the two strings are of different lengths, the shorter one will be padded to the length of the longer one. |
||
1324 | * @param string $token1 |
||
1325 | * @param string $token2 |
||
1326 | * @return string the XOR result |
||
1327 | */ |
||
1328 | 22 | private function xorTokens($token1, $token2) |
|
1340 | |||
1341 | /** |
||
1342 | * @return string the CSRF token sent via [[CSRF_HEADER]] by browser. Null is returned if no such header is sent. |
||
1343 | */ |
||
1344 | public function getCsrfTokenFromHeader() |
||
1349 | |||
1350 | /** |
||
1351 | * Creates a cookie with a randomly generated CSRF token. |
||
1352 | * Initial values specified in [[csrfCookie]] will be applied to the generated cookie. |
||
1353 | * @param string $token the CSRF token |
||
1354 | * @return Cookie the generated cookie |
||
1355 | * @see enableCsrfValidation |
||
1356 | */ |
||
1357 | 21 | protected function createCsrfCookie($token) |
|
1364 | |||
1365 | /** |
||
1366 | * Performs the CSRF validation. |
||
1367 | * |
||
1368 | * This method will validate the user-provided CSRF token by comparing it with the one stored in cookie or session. |
||
1369 | * This method is mainly called in [[Controller::beforeAction()]]. |
||
1370 | * |
||
1371 | * Note that the method will NOT perform CSRF validation if [[enableCsrfValidation]] is false or the HTTP method |
||
1372 | * is among GET, HEAD or OPTIONS. |
||
1373 | * |
||
1374 | * @param string $token the user-provided CSRF token to be validated. If null, the token will be retrieved from |
||
1375 | * the [[csrfParam]] POST field or HTTP header. |
||
1376 | * This parameter is available since version 2.0.4. |
||
1377 | * @return boolean whether CSRF token is valid. If [[enableCsrfValidation]] is false, this method will return true. |
||
1378 | */ |
||
1379 | 1 | public function validateCsrfToken($token = null) |
|
1396 | |||
1397 | /** |
||
1398 | * Validates CSRF token |
||
1399 | * |
||
1400 | * @param string $token |
||
1401 | * @param string $trueToken |
||
1402 | * @return boolean |
||
1403 | */ |
||
1404 | private function validateCsrfTokenInternal($token, $trueToken) |
||
1417 | } |
||
1418 |