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 |
||
| 98 | class Request extends \yii\base\Request implements RequestInterface |
||
| 99 | { |
||
| 100 | use MessageTrait; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * The name of the HTTP header for sending CSRF token. |
||
| 104 | */ |
||
| 105 | const CSRF_HEADER = 'X-CSRF-Token'; |
||
| 106 | /** |
||
| 107 | * The length of the CSRF token mask. |
||
| 108 | * @deprecated 2.0.12 The mask length is now equal to the token length. |
||
| 109 | */ |
||
| 110 | const CSRF_MASK_LENGTH = 8; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var bool whether to enable CSRF (Cross-Site Request Forgery) validation. Defaults to true. |
||
| 114 | * When CSRF validation is enabled, forms submitted to an Yii Web application must be originated |
||
| 115 | * from the same application. If not, a 400 HTTP exception will be raised. |
||
| 116 | * |
||
| 117 | * Note, this feature requires that the user client accepts cookie. Also, to use this feature, |
||
| 118 | * forms submitted via POST method must contain a hidden input whose name is specified by [[csrfParam]]. |
||
| 119 | * You may use [[\yii\helpers\Html::beginForm()]] to generate his hidden input. |
||
| 120 | * |
||
| 121 | * In JavaScript, you may get the values of [[csrfParam]] and [[csrfToken]] via `yii.getCsrfParam()` and |
||
| 122 | * `yii.getCsrfToken()`, respectively. The [[\yii\web\YiiAsset]] asset must be registered. |
||
| 123 | * You also need to include CSRF meta tags in your pages by using [[\yii\helpers\Html::csrfMetaTags()]]. |
||
| 124 | * |
||
| 125 | * @see Controller::enableCsrfValidation |
||
| 126 | * @see http://en.wikipedia.org/wiki/Cross-site_request_forgery |
||
| 127 | */ |
||
| 128 | public $enableCsrfValidation = true; |
||
| 129 | /** |
||
| 130 | * @var string the name of the token used to prevent CSRF. Defaults to '_csrf'. |
||
| 131 | * This property is used only when [[enableCsrfValidation]] is true. |
||
| 132 | */ |
||
| 133 | public $csrfParam = '_csrf'; |
||
| 134 | /** |
||
| 135 | * @var array the configuration for creating the CSRF [[Cookie|cookie]]. This property is used only when |
||
| 136 | * both [[enableCsrfValidation]] and [[enableCsrfCookie]] are true. |
||
| 137 | */ |
||
| 138 | public $csrfCookie = ['httpOnly' => true]; |
||
| 139 | /** |
||
| 140 | * @var bool whether to use cookie to persist CSRF token. If false, CSRF token will be stored |
||
| 141 | * in session under the name of [[csrfParam]]. Note that while storing CSRF tokens in session increases |
||
| 142 | * security, it requires starting a session for every page, which will degrade your site performance. |
||
| 143 | */ |
||
| 144 | public $enableCsrfCookie = true; |
||
| 145 | /** |
||
| 146 | * @var bool whether cookies should be validated to ensure they are not tampered. Defaults to true. |
||
| 147 | */ |
||
| 148 | public $enableCookieValidation = true; |
||
| 149 | /** |
||
| 150 | * @var string a secret key used for cookie validation. This property must be set if [[enableCookieValidation]] is true. |
||
| 151 | */ |
||
| 152 | public $cookieValidationKey; |
||
| 153 | /** |
||
| 154 | * @var string the name of the POST parameter that is used to indicate if a request is a PUT, PATCH or DELETE |
||
| 155 | * request tunneled through POST. Defaults to '_method'. |
||
| 156 | * @see getMethod() |
||
| 157 | * @see getBodyParams() |
||
| 158 | */ |
||
| 159 | public $methodParam = '_method'; |
||
| 160 | /** |
||
| 161 | * @var array the parsers for converting the raw HTTP request body into [[bodyParams]]. |
||
| 162 | * The array keys are the request `Content-Types`, and the array values are the |
||
| 163 | * corresponding configurations for [[Yii::createObject|creating the parser objects]]. |
||
| 164 | * A parser must implement the [[RequestParserInterface]]. |
||
| 165 | * |
||
| 166 | * To enable parsing for JSON requests you can use the [[JsonParser]] class like in the following example: |
||
| 167 | * |
||
| 168 | * ``` |
||
| 169 | * [ |
||
| 170 | * 'application/json' => \yii\web\JsonParser::class, |
||
| 171 | * ] |
||
| 172 | * ``` |
||
| 173 | * |
||
| 174 | * To register a parser for parsing all request types you can use `'*'` as the array key. |
||
| 175 | * This one will be used as a fallback in case no other types match. |
||
| 176 | * |
||
| 177 | * @see getBodyParams() |
||
| 178 | */ |
||
| 179 | public $parsers = []; |
||
| 180 | /** |
||
| 181 | * @var array the configuration for trusted security related headers. |
||
| 182 | * |
||
| 183 | * An array key is a regular expression for matching a client (ususally a proxy) by |
||
| 184 | * host name or ip address. |
||
| 185 | * |
||
| 186 | * An array value is a list of headers to trust. These will be matched against |
||
| 187 | * [[secureHeaders]] to determine which headers are allowed to be sent by a specified host. |
||
| 188 | * The case of the header names must be the same as specified in [[secureHeaders]]. |
||
| 189 | * |
||
| 190 | * To specify a host for which you trust all headers listed in [[secureHeaders]] you can just specify |
||
| 191 | * the regular expression as the array value. |
||
| 192 | * |
||
| 193 | * For example, to trust all headers listed in [[secureHeaders]] |
||
| 194 | * from domains ending in '.trusted.com' use the following: |
||
| 195 | * |
||
| 196 | * ```php |
||
| 197 | * [ |
||
| 198 | * '/\.trusted\.com$/', |
||
| 199 | * ] |
||
| 200 | * ``` |
||
| 201 | * |
||
| 202 | * To trust just the `x-forwarded-for` header from domains ending in `.partial.com` use: |
||
| 203 | * |
||
| 204 | * ``` |
||
| 205 | * [ |
||
| 206 | * '/\.partial\.com$/' => ['X-Forwarded-For'] |
||
| 207 | * ] |
||
| 208 | * ``` |
||
| 209 | * |
||
| 210 | * Default is to trust all headers except those listed in [[secureHeaders]] from all hosts. |
||
| 211 | * Matches are tried in order and searching is stopped when a host or IP matches. |
||
| 212 | * @see $secureHeaders |
||
| 213 | * @since 2.0.13 |
||
| 214 | */ |
||
| 215 | public $trustedHosts = []; |
||
| 216 | /** |
||
| 217 | * @var array lists of headers that are, by default, subject to the trusted host configuration. |
||
| 218 | * These headers will be filtered unless explicitly allowed in [[$trustedHosts]]. |
||
| 219 | * The match of header names is case-insensitive. |
||
| 220 | * @see https://en.wikipedia.org/wiki/List_of_HTTP_header_fields |
||
| 221 | * @see $trustedHosts |
||
| 222 | * @since 2.0.13 |
||
| 223 | */ |
||
| 224 | public $secureHeaders = [ |
||
| 225 | 'X-Forwarded-For', |
||
| 226 | 'X-Forwarded-Host', |
||
| 227 | 'X-Forwarded-Proto', |
||
| 228 | 'Front-End-Https', |
||
| 229 | 'X-Rewrite-Url', |
||
| 230 | ]; |
||
| 231 | /** |
||
| 232 | * @var string[] List of headers where proxies store the real client IP. |
||
| 233 | * It's not advisable to put insecure headers here. |
||
| 234 | * The match of header names is case-insensitive. |
||
| 235 | * @see $trustedHosts |
||
| 236 | * @see $secureHeaders |
||
| 237 | * @since 2.0.13 |
||
| 238 | */ |
||
| 239 | public $ipHeaders = [ |
||
| 240 | 'X-Forwarded-For', |
||
| 241 | ]; |
||
| 242 | /** |
||
| 243 | * @var array list of headers to check for determining whether the connection is made via HTTPS. |
||
| 244 | * The array keys are header names and the array value is a list of header values that indicate a secure connection. |
||
| 245 | * The match of header names and values is case-insensitive. |
||
| 246 | * It's not advisable to put insecure headers here. |
||
| 247 | * @see $trustedHosts |
||
| 248 | * @see $secureHeaders |
||
| 249 | * @since 2.0.13 |
||
| 250 | */ |
||
| 251 | public $secureProtocolHeaders = [ |
||
| 252 | 'X-Forwarded-Proto' => ['https'], |
||
| 253 | 'Front-End-Https' => ['on'], |
||
| 254 | ]; |
||
| 255 | /** |
||
| 256 | * @var CookieCollection Collection of request cookies. |
||
| 257 | */ |
||
| 258 | private $_cookies; |
||
| 259 | /** |
||
| 260 | * @var string the HTTP method of the request. |
||
| 261 | */ |
||
| 262 | private $_method; |
||
| 263 | /** |
||
| 264 | * @var UriInterface the URI instance associated with request. |
||
| 265 | */ |
||
| 266 | private $_uri; |
||
| 267 | /** |
||
| 268 | * @var mixed the message's request target. |
||
| 269 | */ |
||
| 270 | private $_requestTarget; |
||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * Resolves the current request into a route and the associated parameters. |
||
| 275 | * @return array the first element is the route, and the second is the associated parameters. |
||
| 276 | * @throws NotFoundHttpException if the request cannot be resolved. |
||
| 277 | */ |
||
| 278 | 1 | public function resolve() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Filters headers according to the [[trustedHosts]]. |
||
| 297 | * @param array $inputHeaders |
||
| 298 | * @since 2.0.13 |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | 103 | protected function filterHeaders(array $inputHeaders) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Returns default message's headers, which should be present once [[headerCollection]] is instantiated. |
||
| 332 | * @return string[][] an associative array of the message's headers. |
||
| 333 | */ |
||
| 334 | 103 | protected function defaultHeaders() |
|
| 353 | |||
| 354 | /** |
||
| 355 | * {@inheritdoc} |
||
| 356 | * @since 2.1.0 |
||
| 357 | */ |
||
| 358 | public function getRequestTarget() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Specifies the message's request target |
||
| 368 | * @param mixed $requestTarget the message's request target. |
||
| 369 | * @since 2.1.0 |
||
| 370 | */ |
||
| 371 | public function setRequestTarget($requestTarget) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * {@inheritdoc} |
||
| 378 | * @since 2.1.0 |
||
| 379 | */ |
||
| 380 | public function withRequestTarget($requestTarget) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * {@inheritdoc} |
||
| 393 | */ |
||
| 394 | 24 | public function getMethod() |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Specifies request HTTP method. |
||
| 412 | * @param string $method case-sensitive HTTP method. |
||
| 413 | * @since 2.1.0 |
||
| 414 | */ |
||
| 415 | 6 | public function setMethod($method) |
|
| 419 | |||
| 420 | /** |
||
| 421 | * {@inheritdoc} |
||
| 422 | * @since 2.1.0 |
||
| 423 | */ |
||
| 424 | public function withMethod($method) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * {@inheritdoc} |
||
| 437 | * @since 2.1.0 |
||
| 438 | */ |
||
| 439 | public function getUri() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Specifies the URI instance. |
||
| 457 | * @param UriInterface|\Closure|array $uri URI instance or its DI compatible configuration. |
||
| 458 | * @since 2.1.0 |
||
| 459 | */ |
||
| 460 | public function setUri($uri) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * {@inheritdoc} |
||
| 467 | * @since 2.1.0 |
||
| 468 | */ |
||
| 469 | public function withUri(UriInterface $uri, $preserveHost = false) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Returns whether this is a GET request. |
||
| 486 | * @return bool whether this is a GET request. |
||
| 487 | */ |
||
| 488 | 2 | public function getIsGet() |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Returns whether this is an OPTIONS request. |
||
| 495 | * @return bool whether this is a OPTIONS request. |
||
| 496 | */ |
||
| 497 | public function getIsOptions() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Returns whether this is a HEAD request. |
||
| 504 | * @return bool whether this is a HEAD request. |
||
| 505 | */ |
||
| 506 | 9 | public function getIsHead() |
|
| 510 | |||
| 511 | /** |
||
| 512 | * Returns whether this is a POST request. |
||
| 513 | * @return bool whether this is a POST request. |
||
| 514 | */ |
||
| 515 | public function getIsPost() |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Returns whether this is a DELETE request. |
||
| 522 | * @return bool whether this is a DELETE request. |
||
| 523 | */ |
||
| 524 | public function getIsDelete() |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Returns whether this is a PUT request. |
||
| 531 | * @return bool whether this is a PUT request. |
||
| 532 | */ |
||
| 533 | public function getIsPut() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Returns whether this is a PATCH request. |
||
| 540 | * @return bool whether this is a PATCH request. |
||
| 541 | */ |
||
| 542 | public function getIsPatch() |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Returns whether this is an AJAX (XMLHttpRequest) request. |
||
| 549 | * |
||
| 550 | * Note that jQuery doesn't set the header in case of cross domain |
||
| 551 | * requests: https://stackoverflow.com/questions/8163703/cross-domain-ajax-doesnt-send-x-requested-with-header |
||
| 552 | * |
||
| 553 | * @return bool whether this is an AJAX (XMLHttpRequest) request. |
||
| 554 | */ |
||
| 555 | 14 | public function getIsAjax() |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Returns whether this is a PJAX request. |
||
| 562 | * @return bool whether this is a PJAX request |
||
| 563 | */ |
||
| 564 | 3 | public function getIsPjax() |
|
| 568 | |||
| 569 | /** |
||
| 570 | * Returns whether this is an Adobe Flash or Flex request. |
||
| 571 | * @return bool whether this is an Adobe Flash or Adobe Flex request. |
||
| 572 | */ |
||
| 573 | public function getIsFlash() |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Returns default message body to be used in case it is not explicitly set. |
||
| 584 | * @return StreamInterface default body instance. |
||
| 585 | */ |
||
| 586 | 1 | protected function defaultBody() |
|
| 593 | |||
| 594 | private $_rawBody; |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Returns the raw HTTP request body. |
||
| 598 | * @return string the request body |
||
| 599 | */ |
||
| 600 | 1 | public function getRawBody() |
|
| 608 | |||
| 609 | /** |
||
| 610 | * Sets the raw HTTP request body, this method is mainly used by test scripts to simulate raw HTTP requests. |
||
| 611 | * @param string $rawBody the request body |
||
| 612 | */ |
||
| 613 | public function setRawBody($rawBody) |
||
| 617 | |||
| 618 | private $_bodyParams; |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Returns the request parameters given in the request body. |
||
| 622 | * |
||
| 623 | * Request parameters are determined using the parsers configured in [[parsers]] property. |
||
| 624 | * If no parsers are configured for the current [[contentType]] it uses the PHP function `mb_parse_str()` |
||
| 625 | * to parse the [[rawBody|request body]]. |
||
| 626 | * @return array the request parameters given in the request body. |
||
| 627 | * @throws \yii\base\InvalidConfigException if a registered parser does not implement the [[RequestParserInterface]]. |
||
| 628 | * @see getMethod() |
||
| 629 | * @see getBodyParam() |
||
| 630 | * @see setBodyParams() |
||
| 631 | */ |
||
| 632 | 4 | public function getBodyParams() |
|
| 672 | |||
| 673 | /** |
||
| 674 | * Sets the request body parameters. |
||
| 675 | * @param array $values the request body parameters (name-value pairs) |
||
| 676 | * @see getBodyParam() |
||
| 677 | * @see getBodyParams() |
||
| 678 | */ |
||
| 679 | 3 | public function setBodyParams($values) |
|
| 683 | |||
| 684 | /** |
||
| 685 | * Returns the named request body parameter value. |
||
| 686 | * If the parameter does not exist, the second parameter passed to this method will be returned. |
||
| 687 | * @param string $name the parameter name |
||
| 688 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
| 689 | * @return mixed the parameter value |
||
| 690 | * @see getBodyParams() |
||
| 691 | * @see setBodyParams() |
||
| 692 | */ |
||
| 693 | 4 | public function getBodyParam($name, $defaultValue = null) |
|
| 699 | |||
| 700 | /** |
||
| 701 | * Returns POST parameter with a given name. If name isn't specified, returns an array of all POST parameters. |
||
| 702 | * |
||
| 703 | * @param string $name the parameter name |
||
| 704 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
| 705 | * @return array|mixed |
||
| 706 | */ |
||
| 707 | public function post($name = null, $defaultValue = null) |
||
| 715 | |||
| 716 | private $_queryParams; |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Returns the request parameters given in the [[queryString]]. |
||
| 720 | * |
||
| 721 | * This method will return the contents of `$_GET` if params where not explicitly set. |
||
| 722 | * @return array the request GET parameter values. |
||
| 723 | * @see setQueryParams() |
||
| 724 | */ |
||
| 725 | 29 | public function getQueryParams() |
|
| 733 | |||
| 734 | /** |
||
| 735 | * Sets the request [[queryString]] parameters. |
||
| 736 | * @param array $values the request query parameters (name-value pairs) |
||
| 737 | * @see getQueryParam() |
||
| 738 | * @see getQueryParams() |
||
| 739 | */ |
||
| 740 | 8 | public function setQueryParams($values) |
|
| 744 | |||
| 745 | /** |
||
| 746 | * Returns GET parameter with a given name. If name isn't specified, returns an array of all GET parameters. |
||
| 747 | * |
||
| 748 | * @param string $name the parameter name |
||
| 749 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
| 750 | * @return array|mixed |
||
| 751 | */ |
||
| 752 | 15 | public function get($name = null, $defaultValue = null) |
|
| 760 | |||
| 761 | /** |
||
| 762 | * Returns the named GET parameter value. |
||
| 763 | * If the GET parameter does not exist, the second parameter passed to this method will be returned. |
||
| 764 | * @param string $name the GET parameter name. |
||
| 765 | * @param mixed $defaultValue the default parameter value if the GET parameter does not exist. |
||
| 766 | * @return mixed the GET parameter value |
||
| 767 | * @see getBodyParam() |
||
| 768 | */ |
||
| 769 | 20 | public function getQueryParam($name, $defaultValue = null) |
|
| 775 | |||
| 776 | private $_hostInfo; |
||
| 777 | private $_hostName; |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Returns the schema and host part of the current request URL. |
||
| 781 | * |
||
| 782 | * The returned URL does not have an ending slash. |
||
| 783 | * |
||
| 784 | * By default this value is based on the user request information. This method will |
||
| 785 | * return the value of `$_SERVER['HTTP_HOST']` if it is available or `$_SERVER['SERVER_NAME']` if not. |
||
| 786 | * You may want to check out the [PHP documentation](http://php.net/manual/en/reserved.variables.server.php) |
||
| 787 | * for more information on these variables. |
||
| 788 | * |
||
| 789 | * You may explicitly specify it by setting the [[setHostInfo()|hostInfo]] property. |
||
| 790 | * |
||
| 791 | * > Warning: Dependent on the server configuration this information may not be |
||
| 792 | * > reliable and [may be faked by the user sending the HTTP request](https://www.acunetix.com/vulnerabilities/web/host-header-attack). |
||
| 793 | * > If the webserver is configured to serve the same site independent of the value of |
||
| 794 | * > the `Host` header, this value is not reliable. In such situations you should either |
||
| 795 | * > fix your webserver configuration or explicitly set the value by setting the [[setHostInfo()|hostInfo]] property. |
||
| 796 | * > If you don't have access to the server configuration, you can setup [[\yii\filters\HostControl]] filter at |
||
| 797 | * > application level in order to protect against such kind of attack. |
||
| 798 | * |
||
| 799 | * @property string|null schema and hostname part (with port number if needed) of the request URL |
||
| 800 | * (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set. |
||
| 801 | * See [[getHostInfo()]] for security related notes on this property. |
||
| 802 | * @return string|null schema and hostname part (with port number if needed) of the request URL |
||
| 803 | * (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set. |
||
| 804 | * @see setHostInfo() |
||
| 805 | */ |
||
| 806 | 24 | public function getHostInfo() |
|
| 824 | |||
| 825 | /** |
||
| 826 | * Sets the schema and host part of the application URL. |
||
| 827 | * This setter is provided in case the schema and hostname cannot be determined |
||
| 828 | * on certain Web servers. |
||
| 829 | * @param string|null $value the schema and host part of the application URL. The trailing slashes will be removed. |
||
| 830 | * @see getHostInfo() for security related notes on this property. |
||
| 831 | */ |
||
| 832 | 57 | public function setHostInfo($value) |
|
| 837 | |||
| 838 | /** |
||
| 839 | * Returns the host part of the current request URL. |
||
| 840 | * Value is calculated from current [[getHostInfo()|hostInfo]] property. |
||
| 841 | * |
||
| 842 | * > Warning: The content of this value may not be reliable, dependent on the server |
||
| 843 | * > configuration. Please refer to [[getHostInfo()]] for more information. |
||
| 844 | * |
||
| 845 | * @return string|null hostname part of the request URL (e.g. `www.yiiframework.com`) |
||
| 846 | * @see getHostInfo() |
||
| 847 | * @since 2.0.10 |
||
| 848 | */ |
||
| 849 | 11 | public function getHostName() |
|
| 857 | |||
| 858 | private $_baseUrl; |
||
| 859 | |||
| 860 | /** |
||
| 861 | * Returns the relative URL for the application. |
||
| 862 | * This is similar to [[scriptUrl]] except that it does not include the script file name, |
||
| 863 | * and the ending slashes are removed. |
||
| 864 | * @return string the relative URL for the application |
||
| 865 | * @see setScriptUrl() |
||
| 866 | */ |
||
| 867 | 257 | public function getBaseUrl() |
|
| 875 | |||
| 876 | /** |
||
| 877 | * Sets the relative URL for the application. |
||
| 878 | * By default the URL is determined based on the entry script URL. |
||
| 879 | * This setter is provided in case you want to change this behavior. |
||
| 880 | * @param string $value the relative URL for the application |
||
| 881 | */ |
||
| 882 | 1 | public function setBaseUrl($value) |
|
| 886 | |||
| 887 | private $_scriptUrl; |
||
| 888 | |||
| 889 | /** |
||
| 890 | * Returns the relative URL of the entry script. |
||
| 891 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
| 892 | * @return string the relative URL of the entry script. |
||
| 893 | * @throws InvalidConfigException if unable to determine the entry script URL |
||
| 894 | */ |
||
| 895 | 258 | public function getScriptUrl() |
|
| 917 | |||
| 918 | /** |
||
| 919 | * Sets the relative URL for the application entry script. |
||
| 920 | * This setter is provided in case the entry script URL cannot be determined |
||
| 921 | * on certain Web servers. |
||
| 922 | * @param string $value the relative URL for the application entry script. |
||
| 923 | */ |
||
| 924 | 268 | public function setScriptUrl($value) |
|
| 928 | |||
| 929 | private $_scriptFile; |
||
| 930 | |||
| 931 | /** |
||
| 932 | * Returns the entry script file path. |
||
| 933 | * The default implementation will simply return `$_SERVER['SCRIPT_FILENAME']`. |
||
| 934 | * @return string the entry script file path |
||
| 935 | * @throws InvalidConfigException |
||
| 936 | */ |
||
| 937 | 259 | public function getScriptFile() |
|
| 949 | |||
| 950 | /** |
||
| 951 | * Sets the entry script file path. |
||
| 952 | * The entry script file path normally can be obtained from `$_SERVER['SCRIPT_FILENAME']`. |
||
| 953 | * If your server configuration does not return the correct value, you may configure |
||
| 954 | * this property to make it right. |
||
| 955 | * @param string $value the entry script file path. |
||
| 956 | */ |
||
| 957 | 237 | public function setScriptFile($value) |
|
| 961 | |||
| 962 | private $_pathInfo; |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Returns the path info of the currently requested URL. |
||
| 966 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||
| 967 | * The starting and ending slashes are both removed. |
||
| 968 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
| 969 | * Note, the returned path info is already URL-decoded. |
||
| 970 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
| 971 | */ |
||
| 972 | 18 | public function getPathInfo() |
|
| 980 | |||
| 981 | /** |
||
| 982 | * Sets the path info of the current request. |
||
| 983 | * This method is mainly provided for testing purpose. |
||
| 984 | * @param string $value the path info of the current request |
||
| 985 | */ |
||
| 986 | 19 | public function setPathInfo($value) |
|
| 990 | |||
| 991 | /** |
||
| 992 | * Resolves the path info part of the currently requested URL. |
||
| 993 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||
| 994 | * The starting slashes are both removed (ending slashes will be kept). |
||
| 995 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
| 996 | * Note, the returned path info is decoded. |
||
| 997 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
| 998 | */ |
||
| 999 | protected function resolvePathInfo() |
||
| 1043 | |||
| 1044 | /** |
||
| 1045 | * Returns the currently requested absolute URL. |
||
| 1046 | * This is a shortcut to the concatenation of [[hostInfo]] and [[url]]. |
||
| 1047 | * @return string the currently requested absolute URL. |
||
| 1048 | */ |
||
| 1049 | public function getAbsoluteUrl() |
||
| 1053 | |||
| 1054 | private $_url; |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * Returns the currently requested relative URL. |
||
| 1058 | * This refers to the portion of the URL that is after the [[hostInfo]] part. |
||
| 1059 | * It includes the [[queryString]] part if any. |
||
| 1060 | * @return string the currently requested relative URL. Note that the URI returned may be URL-encoded depending on the client. |
||
| 1061 | * @throws InvalidConfigException if the URL cannot be determined due to unusual server configuration |
||
| 1062 | */ |
||
| 1063 | 11 | public function getUrl() |
|
| 1071 | |||
| 1072 | /** |
||
| 1073 | * Sets the currently requested relative URL. |
||
| 1074 | * The URI must refer to the portion that is after [[hostInfo]]. |
||
| 1075 | * Note that the URI should be URL-encoded. |
||
| 1076 | * @param string $value the request URI to be set |
||
| 1077 | */ |
||
| 1078 | 24 | public function setUrl($value) |
|
| 1082 | |||
| 1083 | /** |
||
| 1084 | * Resolves the request URI portion for the currently requested URL. |
||
| 1085 | * This refers to the portion that is after the [[hostInfo]] part. It includes the [[queryString]] part if any. |
||
| 1086 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
| 1087 | * @return string|bool the request URI portion for the currently requested URL. |
||
| 1088 | * Note that the URI returned may be URL-encoded depending on the client. |
||
| 1089 | * @throws InvalidConfigException if the request URI cannot be determined due to unusual server configuration |
||
| 1090 | */ |
||
| 1091 | 3 | protected function resolveRequestUri() |
|
| 1111 | |||
| 1112 | /** |
||
| 1113 | * Returns part of the request URL that is after the question mark. |
||
| 1114 | * @return string part of the request URL that is after the question mark |
||
| 1115 | */ |
||
| 1116 | public function getQueryString() |
||
| 1120 | |||
| 1121 | /** |
||
| 1122 | * Return if the request is sent via secure channel (https). |
||
| 1123 | * @return bool if the request is sent via secure channel (https) |
||
| 1124 | */ |
||
| 1125 | 37 | public function getIsSecureConnection() |
|
| 1130 | |||
| 1131 | /** |
||
| 1132 | * Returns the server name. |
||
| 1133 | * @return string server name, null if not available |
||
| 1134 | */ |
||
| 1135 | 1 | public function getServerName() |
|
| 1139 | |||
| 1140 | /** |
||
| 1141 | * Returns the server port number. |
||
| 1142 | * @return int|null server port number, null if not available |
||
| 1143 | */ |
||
| 1144 | 1 | public function getServerPort() |
|
| 1148 | |||
| 1149 | /** |
||
| 1150 | * Returns the URL referrer. |
||
| 1151 | * @return string|null URL referrer, null if not available |
||
| 1152 | */ |
||
| 1153 | public function getReferrer() |
||
| 1160 | |||
| 1161 | /** |
||
| 1162 | * Returns the URL origin of a CORS request. |
||
| 1163 | * |
||
| 1164 | * The return value is taken from the `Origin` [[getHeaders()|header]] sent by the browser. |
||
| 1165 | * |
||
| 1166 | * Note that the origin request header indicates where a fetch originates from. |
||
| 1167 | * It doesn't include any path information, but only the server name. |
||
| 1168 | * It is sent with a CORS requests, as well as with POST requests. |
||
| 1169 | * It is similar to the referer header, but, unlike this header, it doesn't disclose the whole path. |
||
| 1170 | * Please refer to <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin> for more information. |
||
| 1171 | * |
||
| 1172 | * @return string|null URL origin of a CORS request, `null` if not available. |
||
| 1173 | * @see getHeaders() |
||
| 1174 | * @since 2.0.13 |
||
| 1175 | */ |
||
| 1176 | 1 | public function getOrigin() |
|
| 1180 | |||
| 1181 | /** |
||
| 1182 | * Returns the user agent. |
||
| 1183 | * @return string|null user agent, null if not available |
||
| 1184 | */ |
||
| 1185 | public function getUserAgent() |
||
| 1192 | |||
| 1193 | /** |
||
| 1194 | * Returns the user IP address. |
||
| 1195 | * The IP is determined using headers and / or `$_SERVER` variables. |
||
| 1196 | * @return string|null user IP address, null if not available |
||
| 1197 | */ |
||
| 1198 | 32 | public function getUserIP() |
|
| 1208 | |||
| 1209 | /** |
||
| 1210 | * Returns the user host name. |
||
| 1211 | * The HOST is determined using headers and / or `$_SERVER` variables. |
||
| 1212 | * @return string|null user host name, null if not available |
||
| 1213 | */ |
||
| 1214 | public function getUserHost() |
||
| 1224 | |||
| 1225 | /** |
||
| 1226 | * Returns the IP on the other end of this connection. |
||
| 1227 | * This is always the next hop, any headers are ignored. |
||
| 1228 | * @return string|null remote IP address, `null` if not available. |
||
| 1229 | * @since 2.0.13 |
||
| 1230 | */ |
||
| 1231 | 47 | public function getRemoteIP() |
|
| 1235 | |||
| 1236 | /** |
||
| 1237 | * Returns the host name of the other end of this connection. |
||
| 1238 | * This is always the next hop, any headers are ignored. |
||
| 1239 | * @return string|null remote host name, `null` if not available |
||
| 1240 | * @see getUserHost() |
||
| 1241 | * @see getRemoteIP() |
||
| 1242 | * @since 2.0.13 |
||
| 1243 | */ |
||
| 1244 | 19 | public function getRemoteHost() |
|
| 1248 | |||
| 1249 | /** |
||
| 1250 | * @return string|null the username sent via HTTP authentication, null if the username is not given |
||
| 1251 | */ |
||
| 1252 | 10 | public function getAuthUser() |
|
| 1256 | |||
| 1257 | /** |
||
| 1258 | * @return string|null the password sent via HTTP authentication, null if the password is not given |
||
| 1259 | */ |
||
| 1260 | 10 | public function getAuthPassword() |
|
| 1264 | |||
| 1265 | private $_port; |
||
| 1266 | |||
| 1267 | /** |
||
| 1268 | * Returns the port to use for insecure requests. |
||
| 1269 | * Defaults to 80, or the port specified by the server if the current |
||
| 1270 | * request is insecure. |
||
| 1271 | * @return int port number for insecure requests. |
||
| 1272 | * @see setPort() |
||
| 1273 | */ |
||
| 1274 | public function getPort() |
||
| 1282 | |||
| 1283 | /** |
||
| 1284 | * Sets the port to use for insecure requests. |
||
| 1285 | * This setter is provided in case a custom port is necessary for certain |
||
| 1286 | * server configurations. |
||
| 1287 | * @param int $value port number. |
||
| 1288 | */ |
||
| 1289 | public function setPort($value) |
||
| 1296 | |||
| 1297 | private $_securePort; |
||
| 1298 | |||
| 1299 | /** |
||
| 1300 | * Returns the port to use for secure requests. |
||
| 1301 | * Defaults to 443, or the port specified by the server if the current |
||
| 1302 | * request is secure. |
||
| 1303 | * @return int port number for secure requests. |
||
| 1304 | * @see setSecurePort() |
||
| 1305 | */ |
||
| 1306 | public function getSecurePort() |
||
| 1314 | |||
| 1315 | /** |
||
| 1316 | * Sets the port to use for secure requests. |
||
| 1317 | * This setter is provided in case a custom port is necessary for certain |
||
| 1318 | * server configurations. |
||
| 1319 | * @param int $value port number. |
||
| 1320 | */ |
||
| 1321 | public function setSecurePort($value) |
||
| 1328 | |||
| 1329 | private $_contentTypes; |
||
| 1330 | |||
| 1331 | /** |
||
| 1332 | * Returns the content types acceptable by the end user. |
||
| 1333 | * |
||
| 1334 | * This is determined by the `Accept` HTTP header. For example, |
||
| 1335 | * |
||
| 1336 | * ```php |
||
| 1337 | * $_SERVER['HTTP_ACCEPT'] = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
| 1338 | * $types = $request->getAcceptableContentTypes(); |
||
| 1339 | * print_r($types); |
||
| 1340 | * // displays: |
||
| 1341 | * // [ |
||
| 1342 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||
| 1343 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||
| 1344 | * // 'text/plain' => ['q' => 0.5], |
||
| 1345 | * // ] |
||
| 1346 | * ``` |
||
| 1347 | * |
||
| 1348 | * @return array the content types ordered by the quality score. Types with the highest scores |
||
| 1349 | * will be returned first. The array keys are the content types, while the array values |
||
| 1350 | * are the corresponding quality score and other parameters as given in the header. |
||
| 1351 | */ |
||
| 1352 | 3 | public function getAcceptableContentTypes() |
|
| 1364 | |||
| 1365 | /** |
||
| 1366 | * Sets the acceptable content types. |
||
| 1367 | * Please refer to [[getAcceptableContentTypes()]] on the format of the parameter. |
||
| 1368 | * @param array $value the content types that are acceptable by the end user. They should |
||
| 1369 | * be ordered by the preference level. |
||
| 1370 | * @see getAcceptableContentTypes() |
||
| 1371 | * @see parseAcceptHeader() |
||
| 1372 | */ |
||
| 1373 | 1 | public function setAcceptableContentTypes($value) |
|
| 1377 | |||
| 1378 | /** |
||
| 1379 | * Returns request content-type |
||
| 1380 | * The Content-Type header field indicates the MIME type of the data |
||
| 1381 | * contained in [[getRawBody()]] or, in the case of the HEAD method, the |
||
| 1382 | * media type that would have been sent had the request been a GET. |
||
| 1383 | * For the MIME-types the user expects in response, see [[acceptableContentTypes]]. |
||
| 1384 | * @return string request content-type. Empty string is returned if this information is not available. |
||
| 1385 | * @link https://tools.ietf.org/html/rfc2616#section-14.17 |
||
| 1386 | * HTTP 1.1 header field definitions |
||
| 1387 | */ |
||
| 1388 | 2 | public function getContentType() |
|
| 1392 | |||
| 1393 | private $_languages; |
||
| 1394 | |||
| 1395 | /** |
||
| 1396 | * Returns the languages acceptable by the end user. |
||
| 1397 | * This is determined by the `Accept-Language` HTTP header. |
||
| 1398 | * @return array the languages ordered by the preference level. The first element |
||
| 1399 | * represents the most preferred language. |
||
| 1400 | */ |
||
| 1401 | 1 | public function getAcceptableLanguages() |
|
| 1413 | |||
| 1414 | /** |
||
| 1415 | * @param array $value the languages that are acceptable by the end user. They should |
||
| 1416 | * be ordered by the preference level. |
||
| 1417 | */ |
||
| 1418 | 1 | public function setAcceptableLanguages($value) |
|
| 1422 | |||
| 1423 | /** |
||
| 1424 | * Parses the given `Accept` (or `Accept-Language`) header. |
||
| 1425 | * |
||
| 1426 | * This method will return the acceptable values with their quality scores and the corresponding parameters |
||
| 1427 | * as specified in the given `Accept` header. The array keys of the return value are the acceptable values, |
||
| 1428 | * while the array values consisting of the corresponding quality scores and parameters. The acceptable |
||
| 1429 | * values with the highest quality scores will be returned first. For example, |
||
| 1430 | * |
||
| 1431 | * ```php |
||
| 1432 | * $header = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
| 1433 | * $accepts = $request->parseAcceptHeader($header); |
||
| 1434 | * print_r($accepts); |
||
| 1435 | * // displays: |
||
| 1436 | * // [ |
||
| 1437 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||
| 1438 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||
| 1439 | * // 'text/plain' => ['q' => 0.5], |
||
| 1440 | * // ] |
||
| 1441 | * ``` |
||
| 1442 | * |
||
| 1443 | * @param string $header the header to be parsed |
||
| 1444 | * @return array the acceptable values ordered by their quality score. The values with the highest scores |
||
| 1445 | * will be returned first. |
||
| 1446 | */ |
||
| 1447 | 3 | public function parseAcceptHeader($header) |
|
| 1514 | |||
| 1515 | /** |
||
| 1516 | * Returns the user-preferred language that should be used by this application. |
||
| 1517 | * The language resolution is based on the user preferred languages and the languages |
||
| 1518 | * supported by the application. The method will try to find the best match. |
||
| 1519 | * @param array $languages a list of the languages supported by the application. If this is empty, the current |
||
| 1520 | * application language will be returned without further processing. |
||
| 1521 | * @return string the language that the application should use. |
||
| 1522 | */ |
||
| 1523 | 1 | public function getPreferredLanguage(array $languages = []) |
|
| 1545 | |||
| 1546 | /** |
||
| 1547 | * Gets the Etags. |
||
| 1548 | * |
||
| 1549 | * @return array The entity tags |
||
| 1550 | */ |
||
| 1551 | public function getETags() |
||
| 1559 | |||
| 1560 | /** |
||
| 1561 | * Returns the cookie collection. |
||
| 1562 | * |
||
| 1563 | * Through the returned cookie collection, you may access a cookie using the following syntax: |
||
| 1564 | * |
||
| 1565 | * ```php |
||
| 1566 | * $cookie = $request->cookies['name'] |
||
| 1567 | * if ($cookie !== null) { |
||
| 1568 | * $value = $cookie->value; |
||
| 1569 | * } |
||
| 1570 | * |
||
| 1571 | * // alternatively |
||
| 1572 | * $value = $request->cookies->getValue('name'); |
||
| 1573 | * ``` |
||
| 1574 | * |
||
| 1575 | * @return CookieCollection the cookie collection. |
||
| 1576 | */ |
||
| 1577 | 33 | public function getCookies() |
|
| 1587 | |||
| 1588 | /** |
||
| 1589 | * Converts `$_COOKIE` into an array of [[Cookie]]. |
||
| 1590 | * @return array the cookies obtained from request |
||
| 1591 | * @throws InvalidConfigException if [[cookieValidationKey]] is not set when [[enableCookieValidation]] is true |
||
| 1592 | */ |
||
| 1593 | 33 | protected function loadCookies() |
|
| 1629 | |||
| 1630 | private $_csrfToken; |
||
| 1631 | |||
| 1632 | /** |
||
| 1633 | * Returns the token used to perform CSRF validation. |
||
| 1634 | * |
||
| 1635 | * This token is generated in a way to prevent [BREACH attacks](http://breachattack.com/). It may be passed |
||
| 1636 | * along via a hidden field of an HTML form or an HTTP header value to support CSRF validation. |
||
| 1637 | * @param bool $regenerate whether to regenerate CSRF token. When this parameter is true, each time |
||
| 1638 | * this method is called, a new CSRF token will be generated and persisted (in session or cookie). |
||
| 1639 | * @return string the token used to perform CSRF validation. |
||
| 1640 | */ |
||
| 1641 | 37 | public function getCsrfToken($regenerate = false) |
|
| 1652 | |||
| 1653 | /** |
||
| 1654 | * Loads the CSRF token from cookie or session. |
||
| 1655 | * @return string the CSRF token loaded from cookie or session. Null is returned if the cookie or session |
||
| 1656 | * does not have CSRF token. |
||
| 1657 | */ |
||
| 1658 | 37 | protected function loadCsrfToken() |
|
| 1666 | |||
| 1667 | /** |
||
| 1668 | * Generates an unmasked random token used to perform CSRF validation. |
||
| 1669 | * @return string the random token for CSRF validation. |
||
| 1670 | */ |
||
| 1671 | 35 | protected function generateCsrfToken() |
|
| 1683 | |||
| 1684 | /** |
||
| 1685 | * @return string the CSRF token sent via [[CSRF_HEADER]] by browser. Null is returned if no such header is sent. |
||
| 1686 | */ |
||
| 1687 | 3 | public function getCsrfTokenFromHeader() |
|
| 1691 | |||
| 1692 | /** |
||
| 1693 | * Creates a cookie with a randomly generated CSRF token. |
||
| 1694 | * Initial values specified in [[csrfCookie]] will be applied to the generated cookie. |
||
| 1695 | * @param string $token the CSRF token |
||
| 1696 | * @return Cookie the generated cookie |
||
| 1697 | * @see enableCsrfValidation |
||
| 1698 | */ |
||
| 1699 | 33 | protected function createCsrfCookie($token) |
|
| 1706 | |||
| 1707 | /** |
||
| 1708 | * Performs the CSRF validation. |
||
| 1709 | * |
||
| 1710 | * This method will validate the user-provided CSRF token by comparing it with the one stored in cookie or session. |
||
| 1711 | * This method is mainly called in [[Controller::beforeAction()]]. |
||
| 1712 | * |
||
| 1713 | * Note that the method will NOT perform CSRF validation if [[enableCsrfValidation]] is false or the HTTP method |
||
| 1714 | * is among GET, HEAD or OPTIONS. |
||
| 1715 | * |
||
| 1716 | * @param string $clientSuppliedToken the user-provided CSRF token to be validated. If null, the token will be retrieved from |
||
| 1717 | * the [[csrfParam]] POST field or HTTP header. |
||
| 1718 | * This parameter is available since version 2.0.4. |
||
| 1719 | * @return bool whether CSRF token is valid. If [[enableCsrfValidation]] is false, this method will return true. |
||
| 1720 | */ |
||
| 1721 | 5 | public function validateCsrfToken($clientSuppliedToken = null) |
|
| 1739 | |||
| 1740 | /** |
||
| 1741 | * Validates CSRF token. |
||
| 1742 | * |
||
| 1743 | * @param string $clientSuppliedToken The masked client-supplied token. |
||
| 1744 | * @param string $trueToken The masked true token. |
||
| 1745 | * @return bool |
||
| 1746 | */ |
||
| 1747 | 3 | private function validateCsrfTokenInternal($clientSuppliedToken, $trueToken) |
|
| 1757 | |||
| 1758 | /** |
||
| 1759 | * {@inheritdoc} |
||
| 1760 | */ |
||
| 1761 | 1 | public function __clone() |
|
| 1771 | } |
||
| 1772 |
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.