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() |
|
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() |
|
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 | 15 | 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 | 6 | public function getIsHead() |
|
269 | { |
||
270 | 6 | return $this->getMethod() === 'HEAD'; |
|
271 | } |
||
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 | 3 | public function getBodyParams() |
|
380 | { |
||
381 | 3 | if ($this->_bodyParams === null) { |
|
382 | 1 | if (isset($_POST[$this->methodParam])) { |
|
383 | 1 | $this->_bodyParams = $_POST; |
|
384 | 1 | unset($this->_bodyParams[$this->methodParam]); |
|
385 | 1 | return $this->_bodyParams; |
|
386 | } |
||
387 | |||
388 | $rawContentType = $this->getContentType(); |
||
389 | if (($pos = strpos($rawContentType, ';')) !== false) { |
||
390 | // e.g. application/json; charset=UTF-8 |
||
391 | $contentType = substr($rawContentType, 0, $pos); |
||
392 | } else { |
||
393 | $contentType = $rawContentType; |
||
394 | } |
||
395 | |||
396 | if (isset($this->parsers[$contentType])) { |
||
397 | $parser = Yii::createObject($this->parsers[$contentType]); |
||
398 | if (!($parser instanceof RequestParserInterface)) { |
||
399 | throw new InvalidConfigException("The '$contentType' request parser is invalid. It must implement the yii\\web\\RequestParserInterface."); |
||
400 | } |
||
401 | $this->_bodyParams = $parser->parse($this->getRawBody(), $rawContentType); |
||
402 | } elseif (isset($this->parsers['*'])) { |
||
403 | $parser = Yii::createObject($this->parsers['*']); |
||
404 | if (!($parser instanceof RequestParserInterface)) { |
||
405 | throw new InvalidConfigException("The fallback request parser is invalid. It must implement the yii\\web\\RequestParserInterface."); |
||
406 | } |
||
407 | $this->_bodyParams = $parser->parse($this->getRawBody(), $rawContentType); |
||
408 | } elseif ($this->getMethod() === 'POST') { |
||
409 | // PHP has already parsed the body so we have all params in $_POST |
||
410 | $this->_bodyParams = $_POST; |
||
411 | } else { |
||
412 | $this->_bodyParams = []; |
||
413 | mb_parse_str($this->getRawBody(), $this->_bodyParams); |
||
414 | } |
||
415 | } |
||
416 | |||
417 | 3 | return $this->_bodyParams; |
|
418 | } |
||
419 | |||
420 | /** |
||
421 | * Sets the request body parameters. |
||
422 | * @param array $values the request body parameters (name-value pairs) |
||
423 | * @see getBodyParam() |
||
424 | * @see getBodyParams() |
||
425 | */ |
||
426 | 2 | public function setBodyParams($values) |
|
430 | |||
431 | /** |
||
432 | * Returns the named request body parameter value. |
||
433 | * If the parameter does not exist, the second parameter passed to this method will be returned. |
||
434 | * @param string $name the parameter name |
||
435 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
436 | * @return mixed the parameter value |
||
437 | * @see getBodyParams() |
||
438 | * @see setBodyParams() |
||
439 | */ |
||
440 | 3 | public function getBodyParam($name, $defaultValue = null) |
|
446 | |||
447 | /** |
||
448 | * Returns POST parameter with a given name. If name isn't specified, returns an array of all POST parameters. |
||
449 | * |
||
450 | * @param string $name the parameter name |
||
451 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
452 | * @return array|mixed |
||
453 | */ |
||
454 | public function post($name = null, $defaultValue = null) |
||
462 | |||
463 | private $_queryParams; |
||
464 | |||
465 | /** |
||
466 | * Returns the request parameters given in the [[queryString]]. |
||
467 | * |
||
468 | * This method will return the contents of `$_GET` if params where not explicitly set. |
||
469 | * @return array the request GET parameter values. |
||
470 | * @see setQueryParams() |
||
471 | */ |
||
472 | 20 | public function getQueryParams() |
|
480 | |||
481 | /** |
||
482 | * Sets the request [[queryString]] parameters. |
||
483 | * @param array $values the request query parameters (name-value pairs) |
||
484 | * @see getQueryParam() |
||
485 | * @see getQueryParams() |
||
486 | */ |
||
487 | 4 | public function setQueryParams($values) |
|
491 | |||
492 | /** |
||
493 | * Returns GET parameter with a given name. If name isn't specified, returns an array of all GET parameters. |
||
494 | * |
||
495 | * @param string $name the parameter name |
||
496 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
497 | * @return array|mixed |
||
498 | */ |
||
499 | 11 | public function get($name = null, $defaultValue = null) |
|
507 | |||
508 | /** |
||
509 | * Returns the named GET parameter value. |
||
510 | * If the GET parameter does not exist, the second parameter passed to this method will be returned. |
||
511 | * @param string $name the GET parameter name. |
||
512 | * @param mixed $defaultValue the default parameter value if the GET parameter does not exist. |
||
513 | * @return mixed the GET parameter value |
||
514 | * @see getBodyParam() |
||
515 | */ |
||
516 | 12 | public function getQueryParam($name, $defaultValue = null) |
|
522 | |||
523 | private $_hostInfo; |
||
524 | |||
525 | /** |
||
526 | * Returns the schema and host part of the current request URL. |
||
527 | * The returned URL does not have an ending slash. |
||
528 | * By default this is determined based on the user request information. |
||
529 | * You may explicitly specify it by setting the [[setHostInfo()|hostInfo]] property. |
||
530 | * @return string schema and hostname part (with port number if needed) of the request URL (e.g. `http://www.yiiframework.com`), |
||
531 | * null if can't be obtained from `$_SERVER` and wasn't set. |
||
532 | * @see setHostInfo() |
||
533 | */ |
||
534 | 9 | public function getHostInfo() |
|
552 | |||
553 | /** |
||
554 | * Sets the schema and host part of the application URL. |
||
555 | * This setter is provided in case the schema and hostname cannot be determined |
||
556 | * on certain Web servers. |
||
557 | * @param string $value the schema and host part of the application URL. The trailing slashes will be removed. |
||
558 | */ |
||
559 | 22 | public function setHostInfo($value) |
|
563 | |||
564 | private $_baseUrl; |
||
565 | |||
566 | /** |
||
567 | * Returns the relative URL for the application. |
||
568 | * This is similar to [[scriptUrl]] except that it does not include the script file name, |
||
569 | * and the ending slashes are removed. |
||
570 | * @return string the relative URL for the application |
||
571 | * @see setScriptUrl() |
||
572 | */ |
||
573 | 113 | public function getBaseUrl() |
|
581 | |||
582 | /** |
||
583 | * Sets the relative URL for the application. |
||
584 | * By default the URL is determined based on the entry script URL. |
||
585 | * This setter is provided in case you want to change this behavior. |
||
586 | * @param string $value the relative URL for the application |
||
587 | */ |
||
588 | 1 | public function setBaseUrl($value) |
|
592 | |||
593 | private $_scriptUrl; |
||
594 | |||
595 | /** |
||
596 | * Returns the relative URL of the entry script. |
||
597 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
598 | * @return string the relative URL of the entry script. |
||
599 | * @throws InvalidConfigException if unable to determine the entry script URL |
||
600 | */ |
||
601 | 114 | public function getScriptUrl() |
|
623 | |||
624 | /** |
||
625 | * Sets the relative URL for the application entry script. |
||
626 | * This setter is provided in case the entry script URL cannot be determined |
||
627 | * on certain Web servers. |
||
628 | * @param string $value the relative URL for the application entry script. |
||
629 | */ |
||
630 | 118 | public function setScriptUrl($value) |
|
634 | |||
635 | private $_scriptFile; |
||
|
|||
636 | |||
637 | /** |
||
638 | * Returns the entry script file path. |
||
639 | * The default implementation will simply return `$_SERVER['SCRIPT_FILENAME']`. |
||
640 | * @return string the entry script file path |
||
641 | * @throws InvalidConfigException |
||
642 | */ |
||
643 | 115 | public function getScriptFile() |
|
653 | |||
654 | /** |
||
655 | * Sets the entry script file path. |
||
656 | * The entry script file path normally can be obtained from `$_SERVER['SCRIPT_FILENAME']`. |
||
657 | * If your server configuration does not return the correct value, you may configure |
||
658 | * this property to make it right. |
||
659 | * @param string $value the entry script file path. |
||
660 | */ |
||
661 | 97 | public function setScriptFile($value) |
|
665 | |||
666 | private $_pathInfo; |
||
667 | |||
668 | /** |
||
669 | * Returns the path info of the currently requested URL. |
||
670 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||
671 | * The starting and ending slashes are both removed. |
||
672 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
673 | * Note, the returned path info is already URL-decoded. |
||
674 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
675 | */ |
||
676 | 9 | public function getPathInfo() |
|
684 | |||
685 | /** |
||
686 | * Sets the path info of the current request. |
||
687 | * This method is mainly provided for testing purpose. |
||
688 | * @param string $value the path info of the current request |
||
689 | */ |
||
690 | 9 | public function setPathInfo($value) |
|
694 | |||
695 | /** |
||
696 | * Resolves the path info part of the currently requested URL. |
||
697 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||
698 | * The starting slashes are both removed (ending slashes will be kept). |
||
699 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
700 | * Note, the returned path info is decoded. |
||
701 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
702 | */ |
||
703 | protected function resolvePathInfo() |
||
747 | |||
748 | /** |
||
749 | * Returns the currently requested absolute URL. |
||
750 | * This is a shortcut to the concatenation of [[hostInfo]] and [[url]]. |
||
751 | * @return string the currently requested absolute URL. |
||
752 | */ |
||
753 | public function getAbsoluteUrl() |
||
757 | |||
758 | private $_url; |
||
759 | |||
760 | /** |
||
761 | * Returns the currently requested relative URL. |
||
762 | * This refers to the portion of the URL that is after the [[hostInfo]] part. |
||
763 | * It includes the [[queryString]] part if any. |
||
764 | * @return string the currently requested relative URL. Note that the URI returned is URL-encoded. |
||
765 | * @throws InvalidConfigException if the URL cannot be determined due to unusual server configuration |
||
766 | */ |
||
767 | 7 | public function getUrl() |
|
775 | |||
776 | /** |
||
777 | * Sets the currently requested relative URL. |
||
778 | * The URI must refer to the portion that is after [[hostInfo]]. |
||
779 | * Note that the URI should be URL-encoded. |
||
780 | * @param string $value the request URI to be set |
||
781 | */ |
||
782 | 16 | public function setUrl($value) |
|
786 | |||
787 | /** |
||
788 | * Resolves the request URI portion for the currently requested URL. |
||
789 | * This refers to the portion that is after the [[hostInfo]] part. It includes the [[queryString]] part if any. |
||
790 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
791 | * @return string|boolean the request URI portion for the currently requested URL. |
||
792 | * Note that the URI returned is URL-encoded. |
||
793 | * @throws InvalidConfigException if the request URI cannot be determined due to unusual server configuration |
||
794 | */ |
||
795 | protected function resolveRequestUri() |
||
815 | |||
816 | /** |
||
817 | * Returns part of the request URL that is after the question mark. |
||
818 | * @return string part of the request URL that is after the question mark |
||
819 | */ |
||
820 | public function getQueryString() |
||
824 | |||
825 | /** |
||
826 | * Return if the request is sent via secure channel (https). |
||
827 | * @return boolean if the request is sent via secure channel (https) |
||
828 | */ |
||
829 | 5 | public function getIsSecureConnection() |
|
834 | |||
835 | /** |
||
836 | * Returns the server name. |
||
837 | * @return string server name, null if not available |
||
838 | */ |
||
839 | 1 | public function getServerName() |
|
843 | |||
844 | /** |
||
845 | * Returns the server port number. |
||
846 | * @return integer|null server port number, null if not available |
||
847 | */ |
||
848 | 1 | public function getServerPort() |
|
852 | |||
853 | /** |
||
854 | * Returns the URL referrer. |
||
855 | * @return string|null URL referrer, null if not available |
||
856 | */ |
||
857 | public function getReferrer() |
||
861 | |||
862 | /** |
||
863 | * Returns the user agent. |
||
864 | * @return string|null user agent, null if not available |
||
865 | */ |
||
866 | public function getUserAgent() |
||
870 | |||
871 | /** |
||
872 | * Returns the user IP address. |
||
873 | * @return string|null user IP address, null if not available |
||
874 | */ |
||
875 | 14 | public function getUserIP() |
|
879 | |||
880 | /** |
||
881 | * Returns the user host name. |
||
882 | * @return string|null user host name, null if not available |
||
883 | */ |
||
884 | public function getUserHost() |
||
888 | |||
889 | /** |
||
890 | * @return string|null the username sent via HTTP authentication, null if the username is not given |
||
891 | */ |
||
892 | 10 | public function getAuthUser() |
|
896 | |||
897 | /** |
||
898 | * @return string|null the password sent via HTTP authentication, null if the password is not given |
||
899 | */ |
||
900 | 10 | public function getAuthPassword() |
|
904 | |||
905 | private $_port; |
||
906 | |||
907 | /** |
||
908 | * Returns the port to use for insecure requests. |
||
909 | * Defaults to 80, or the port specified by the server if the current |
||
910 | * request is insecure. |
||
911 | * @return integer port number for insecure requests. |
||
912 | * @see setPort() |
||
913 | */ |
||
914 | public function getPort() |
||
922 | |||
923 | /** |
||
924 | * Sets the port to use for insecure requests. |
||
925 | * This setter is provided in case a custom port is necessary for certain |
||
926 | * server configurations. |
||
927 | * @param integer $value port number. |
||
928 | */ |
||
929 | public function setPort($value) |
||
936 | |||
937 | private $_securePort; |
||
938 | |||
939 | /** |
||
940 | * Returns the port to use for secure requests. |
||
941 | * Defaults to 443, or the port specified by the server if the current |
||
942 | * request is secure. |
||
943 | * @return integer port number for secure requests. |
||
944 | * @see setSecurePort() |
||
945 | */ |
||
946 | public function getSecurePort() |
||
954 | |||
955 | /** |
||
956 | * Sets the port to use for secure requests. |
||
957 | * This setter is provided in case a custom port is necessary for certain |
||
958 | * server configurations. |
||
959 | * @param integer $value port number. |
||
960 | */ |
||
961 | public function setSecurePort($value) |
||
968 | |||
969 | private $_contentTypes; |
||
970 | |||
971 | /** |
||
972 | * Returns the content types acceptable by the end user. |
||
973 | * This is determined by the `Accept` HTTP header. For example, |
||
974 | * |
||
975 | * ```php |
||
976 | * $_SERVER['HTTP_ACCEPT'] = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
977 | * $types = $request->getAcceptableContentTypes(); |
||
978 | * print_r($types); |
||
979 | * // displays: |
||
980 | * // [ |
||
981 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||
982 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||
983 | * // 'text/plain' => ['q' => 0.5], |
||
984 | * // ] |
||
985 | * ``` |
||
986 | * |
||
987 | * @return array the content types ordered by the quality score. Types with the highest scores |
||
988 | * will be returned first. The array keys are the content types, while the array values |
||
989 | * are the corresponding quality score and other parameters as given in the header. |
||
990 | */ |
||
991 | 2 | public function getAcceptableContentTypes() |
|
1003 | |||
1004 | /** |
||
1005 | * Sets the acceptable content types. |
||
1006 | * Please refer to [[getAcceptableContentTypes()]] on the format of the parameter. |
||
1007 | * @param array $value the content types that are acceptable by the end user. They should |
||
1008 | * be ordered by the preference level. |
||
1009 | * @see getAcceptableContentTypes() |
||
1010 | * @see parseAcceptHeader() |
||
1011 | */ |
||
1012 | public function setAcceptableContentTypes($value) |
||
1016 | |||
1017 | /** |
||
1018 | * Returns request content-type |
||
1019 | * The Content-Type header field indicates the MIME type of the data |
||
1020 | * contained in [[getRawBody()]] or, in the case of the HEAD method, the |
||
1021 | * media type that would have been sent had the request been a GET. |
||
1022 | * For the MIME-types the user expects in response, see [[acceptableContentTypes]]. |
||
1023 | * @return string request content-type. Null is returned if this information is not available. |
||
1024 | * @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17 |
||
1025 | * HTTP 1.1 header field definitions |
||
1026 | */ |
||
1027 | public function getContentType() |
||
1038 | |||
1039 | private $_languages; |
||
1040 | |||
1041 | /** |
||
1042 | * Returns the languages acceptable by the end user. |
||
1043 | * This is determined by the `Accept-Language` HTTP header. |
||
1044 | * @return array the languages ordered by the preference level. The first element |
||
1045 | * represents the most preferred language. |
||
1046 | */ |
||
1047 | 1 | public function getAcceptableLanguages() |
|
1059 | |||
1060 | /** |
||
1061 | * @param array $value the languages that are acceptable by the end user. They should |
||
1062 | * be ordered by the preference level. |
||
1063 | */ |
||
1064 | 1 | public function setAcceptableLanguages($value) |
|
1068 | |||
1069 | /** |
||
1070 | * Parses the given `Accept` (or `Accept-Language`) header. |
||
1071 | * |
||
1072 | * This method will return the acceptable values with their quality scores and the corresponding parameters |
||
1073 | * as specified in the given `Accept` header. The array keys of the return value are the acceptable values, |
||
1074 | * while the array values consisting of the corresponding quality scores and parameters. The acceptable |
||
1075 | * values with the highest quality scores will be returned first. For example, |
||
1076 | * |
||
1077 | * ```php |
||
1078 | * $header = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
1079 | * $accepts = $request->parseAcceptHeader($header); |
||
1080 | * print_r($accepts); |
||
1081 | * // displays: |
||
1082 | * // [ |
||
1083 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||
1084 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||
1085 | * // 'text/plain' => ['q' => 0.5], |
||
1086 | * // ] |
||
1087 | * ``` |
||
1088 | * |
||
1089 | * @param string $header the header to be parsed |
||
1090 | * @return array the acceptable values ordered by their quality score. The values with the highest scores |
||
1091 | * will be returned first. |
||
1092 | */ |
||
1093 | 3 | public function parseAcceptHeader($header) |
|
1152 | |||
1153 | /** |
||
1154 | * Returns the user-preferred language that should be used by this application. |
||
1155 | * The language resolution is based on the user preferred languages and the languages |
||
1156 | * supported by the application. The method will try to find the best match. |
||
1157 | * @param array $languages a list of the languages supported by the application. If this is empty, the current |
||
1158 | * application language will be returned without further processing. |
||
1159 | * @return string the language that the application should use. |
||
1160 | */ |
||
1161 | 1 | public function getPreferredLanguage(array $languages = []) |
|
1182 | |||
1183 | /** |
||
1184 | * Gets the Etags. |
||
1185 | * |
||
1186 | * @return array The entity tags |
||
1187 | */ |
||
1188 | public function getETags() |
||
1196 | |||
1197 | /** |
||
1198 | * Returns the cookie collection. |
||
1199 | * Through the returned cookie collection, you may access a cookie using the following syntax: |
||
1200 | * |
||
1201 | * ```php |
||
1202 | * $cookie = $request->cookies['name'] |
||
1203 | * if ($cookie !== null) { |
||
1204 | * $value = $cookie->value; |
||
1205 | * } |
||
1206 | * |
||
1207 | * // alternatively |
||
1208 | * $value = $request->cookies->getValue('name'); |
||
1209 | * ``` |
||
1210 | * |
||
1211 | * @return CookieCollection the cookie collection. |
||
1212 | */ |
||
1213 | 24 | public function getCookies() |
|
1223 | |||
1224 | /** |
||
1225 | * Converts `$_COOKIE` into an array of [[Cookie]]. |
||
1226 | * @return array the cookies obtained from request |
||
1227 | * @throws InvalidConfigException if [[cookieValidationKey]] is not set when [[enableCookieValidation]] is true |
||
1228 | */ |
||
1229 | 24 | protected function loadCookies() |
|
1265 | |||
1266 | private $_csrfToken; |
||
1267 | |||
1268 | /** |
||
1269 | * Returns the token used to perform CSRF validation. |
||
1270 | * |
||
1271 | * This token is generated in a way to prevent [BREACH attacks](http://breachattack.com/). It may be passed |
||
1272 | * along via a hidden field of an HTML form or an HTTP header value to support CSRF validation. |
||
1273 | * @param boolean $regenerate whether to regenerate CSRF token. When this parameter is true, each time |
||
1274 | * this method is called, a new CSRF token will be generated and persisted (in session or cookie). |
||
1275 | * @return string the token used to perform CSRF validation. |
||
1276 | */ |
||
1277 | 27 | public function getCsrfToken($regenerate = false) |
|
1292 | |||
1293 | /** |
||
1294 | * Loads the CSRF token from cookie or session. |
||
1295 | * @return string the CSRF token loaded from cookie or session. Null is returned if the cookie or session |
||
1296 | * does not have CSRF token. |
||
1297 | */ |
||
1298 | 27 | protected function loadCsrfToken() |
|
1306 | |||
1307 | /** |
||
1308 | * Generates an unmasked random token used to perform CSRF validation. |
||
1309 | * @return string the random token for CSRF validation. |
||
1310 | */ |
||
1311 | 25 | protected function generateCsrfToken() |
|
1322 | |||
1323 | /** |
||
1324 | * Returns the XOR result of two strings. |
||
1325 | * If the two strings are of different lengths, the shorter one will be padded to the length of the longer one. |
||
1326 | * @param string $token1 |
||
1327 | * @param string $token2 |
||
1328 | * @return string the XOR result |
||
1329 | */ |
||
1330 | 27 | private function xorTokens($token1, $token2) |
|
1342 | |||
1343 | /** |
||
1344 | * @return string the CSRF token sent via [[CSRF_HEADER]] by browser. Null is returned if no such header is sent. |
||
1345 | */ |
||
1346 | 3 | public function getCsrfTokenFromHeader() |
|
1351 | |||
1352 | /** |
||
1353 | * Creates a cookie with a randomly generated CSRF token. |
||
1354 | * Initial values specified in [[csrfCookie]] will be applied to the generated cookie. |
||
1355 | * @param string $token the CSRF token |
||
1356 | * @return Cookie the generated cookie |
||
1357 | * @see enableCsrfValidation |
||
1358 | */ |
||
1359 | 24 | protected function createCsrfCookie($token) |
|
1366 | |||
1367 | /** |
||
1368 | * Performs the CSRF validation. |
||
1369 | * |
||
1370 | * This method will validate the user-provided CSRF token by comparing it with the one stored in cookie or session. |
||
1371 | * This method is mainly called in [[Controller::beforeAction()]]. |
||
1372 | * |
||
1373 | * Note that the method will NOT perform CSRF validation if [[enableCsrfValidation]] is false or the HTTP method |
||
1374 | * is among GET, HEAD or OPTIONS. |
||
1375 | * |
||
1376 | * @param string $token the user-provided CSRF token to be validated. If null, the token will be retrieved from |
||
1377 | * the [[csrfParam]] POST field or HTTP header. |
||
1378 | * This parameter is available since version 2.0.4. |
||
1379 | * @return boolean whether CSRF token is valid. If [[enableCsrfValidation]] is false, this method will return true. |
||
1380 | */ |
||
1381 | 3 | public function validateCsrfToken($token = null) |
|
1398 | |||
1399 | /** |
||
1400 | * Validates CSRF token |
||
1401 | * |
||
1402 | * @param string $token |
||
1403 | * @param string $trueToken |
||
1404 | * @return boolean |
||
1405 | */ |
||
1406 | 3 | private function validateCsrfTokenInternal($token, $trueToken) |
|
1423 | } |
||
1424 |