/framework/web/CHttpRequest.php
PHP | 1191 lines | 782 code | 54 blank | 355 comment | 73 complexity | 266e1f1243cab8d6ea6ba8e5560fd500 MD5 | raw file
- <?php
- /**
- * CHttpRequest and CCookieCollection class file.
- *
- * @author Qiang Xue <qiang.xue@gmail.com>
- * @link http://www.yiiframework.com/
- * @copyright Copyright © 2008-2011 Yii Software LLC
- * @license http://www.yiiframework.com/license/
- */
- /**
- * CHttpRequest encapsulates the $_SERVER variable and resolves its inconsistency among different Web servers.
- *
- * CHttpRequest also manages the cookies sent from and sent to the user.
- * By setting {@link enableCookieValidation} to true,
- * cookies sent from the user will be validated to see if they are tampered.
- * The property {@link getCookies cookies} returns the collection of cookies.
- * For more details, see {@link CCookieCollection}.
- *
- * CHttpRequest is a default application component loaded by {@link CWebApplication}. It can be
- * accessed via {@link CWebApplication::getRequest()}.
- *
- * @property string $url Part of the request URL after the host info.
- * @property string $hostInfo Schema and hostname part (with port number if needed) of the request URL (e.g. http://www.yiiframework.com).
- * @property string $baseUrl The relative URL for the application.
- * @property string $scriptUrl The relative URL of the entry script.
- * @property string $pathInfo Part of the request URL that is after the entry script and before the question mark.
- * Note, the returned pathinfo is decoded starting from 1.1.4.
- * Prior to 1.1.4, whether it is decoded or not depends on the server configuration
- * (in most cases it is not decoded).
- * @property string $requestUri The request URI portion for the currently requested URL.
- * @property string $queryString Part of the request URL that is after the question mark.
- * @property boolean $isSecureConnection If the request is sent via secure channel (https).
- * @property string $requestType Request type, such as GET, POST, HEAD, PUT, DELETE.
- * @property boolean $isPostRequest Whether this is a POST request.
- * @property boolean $isDeleteRequest Whether this is a DELETE request.
- * @property boolean $isPutRequest Whether this is a PUT request.
- * @property boolean $isAjaxRequest Whether this is an AJAX (XMLHttpRequest) request.
- * @property boolean $isFlashRequest Whether this is an Adobe Flash or Adobe Flex request.
- * @property string $serverName Server name.
- * @property integer $serverPort Server port number.
- * @property string $urlReferrer URL referrer, null if not present.
- * @property string $userAgent User agent, null if not present.
- * @property string $userHostAddress User IP address.
- * @property string $userHost User host name, null if cannot be determined.
- * @property string $scriptFile Entry script file path (processed w/ realpath()).
- * @property array $browser User browser capabilities.
- * @property string $acceptTypes User browser accept types, null if not present.
- * @property integer $port Port number for insecure requests.
- * @property integer $securePort Port number for secure requests.
- * @property CCookieCollection|CHttpCookie[] $cookies The cookie collection.
- * @property string $preferredLanguage The user preferred language.
- * @property array $preferredLanguages An array of all user accepted languages in order of preference.
- * @property string $csrfToken The random token for CSRF validation.
- *
- * @author Qiang Xue <qiang.xue@gmail.com>
- * @package system.web
- * @since 1.0
- */
- class CHttpRequest extends CApplicationComponent
- {
- /**
- * @var boolean whether cookies should be validated to ensure they are not tampered. Defaults to false.
- */
- public $enableCookieValidation=false;
- /**
- * @var boolean whether to enable CSRF (Cross-Site Request Forgery) validation. Defaults to false.
- * By setting this property to true, forms submitted to an Yii Web application must be originated
- * from the same application. If not, a 400 HTTP exception will be raised.
- * Note, this feature requires that the user client accepts cookie.
- * You also need to use {@link CHtml::form} or {@link CHtml::statefulForm} to generate
- * the needed HTML forms in your pages.
- * @see http://seclab.stanford.edu/websec/csrf/csrf.pdf
- */
- public $enableCsrfValidation=false;
- /**
- * @var string the name of the token used to prevent CSRF. Defaults to 'YII_CSRF_TOKEN'.
- * This property is effectively only when {@link enableCsrfValidation} is true.
- */
- public $csrfTokenName='YII_CSRF_TOKEN';
- /**
- * @var array the property values (in name-value pairs) used to initialize the CSRF cookie.
- * Any property of {@link CHttpCookie} may be initialized.
- * This property is effective only when {@link enableCsrfValidation} is true.
- */
- public $csrfCookie;
- private $_requestUri;
- private $_pathInfo;
- private $_scriptFile;
- private $_scriptUrl;
- private $_hostInfo;
- private $_baseUrl;
- private $_cookies;
- private $_preferredLanguages;
- private $_csrfToken;
- private $_restParams;
- /**
- * Initializes the application component.
- * This method overrides the parent implementation by preprocessing
- * the user request data.
- */
- public function init()
- {
- parent::init();
- $this->normalizeRequest();
- }
- /**
- * Normalizes the request data.
- * This method strips off slashes in request data if get_magic_quotes_gpc() returns true.
- * It also performs CSRF validation if {@link enableCsrfValidation} is true.
- */
- protected function normalizeRequest()
- {
- // normalize request
- if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
- {
- if(isset($_GET))
- $_GET=$this->stripSlashes($_GET);
- if(isset($_POST))
- $_POST=$this->stripSlashes($_POST);
- if(isset($_REQUEST))
- $_REQUEST=$this->stripSlashes($_REQUEST);
- if(isset($_COOKIE))
- $_COOKIE=$this->stripSlashes($_COOKIE);
- }
- if($this->enableCsrfValidation)
- Yii::app()->attachEventHandler('onBeginRequest',array($this,'validateCsrfToken'));
- }
- /**
- * Strips slashes from input data.
- * This method is applied when magic quotes is enabled.
- * @param mixed $data input data to be processed
- * @return mixed processed data
- */
- public function stripSlashes(&$data)
- {
- return is_array($data)?array_map(array($this,'stripSlashes'),$data):stripslashes($data);
- }
- /**
- * Returns the named GET or POST parameter value.
- * If the GET or POST parameter does not exist, the second parameter to this method will be returned.
- * If both GET and POST contains such a named parameter, the GET parameter takes precedence.
- * @param string $name the GET parameter name
- * @param mixed $defaultValue the default parameter value if the GET parameter does not exist.
- * @return mixed the GET parameter value
- * @see getQuery
- * @see getPost
- */
- public function getParam($name,$defaultValue=null)
- {
- return isset($_GET[$name]) ? $_GET[$name] : (isset($_POST[$name]) ? $_POST[$name] : $defaultValue);
- }
- /**
- * Returns the named GET parameter value.
- * If the GET parameter does not exist, the second parameter to this method will be returned.
- * @param string $name the GET parameter name
- * @param mixed $defaultValue the default parameter value if the GET parameter does not exist.
- * @return mixed the GET parameter value
- * @see getPost
- * @see getParam
- */
- public function getQuery($name,$defaultValue=null)
- {
- return isset($_GET[$name]) ? $_GET[$name] : $defaultValue;
- }
- /**
- * Returns the named POST parameter value.
- * If the POST parameter does not exist, the second parameter to this method will be returned.
- * @param string $name the POST parameter name
- * @param mixed $defaultValue the default parameter value if the POST parameter does not exist.
- * @return mixed the POST parameter value
- * @see getParam
- * @see getQuery
- */
- public function getPost($name,$defaultValue=null)
- {
- return isset($_POST[$name]) ? $_POST[$name] : $defaultValue;
- }
- /**
- * Returns the named DELETE parameter value.
- * If the DELETE parameter does not exist or if the current request is not a DELETE request,
- * the second parameter to this method will be returned.
- * If the DELETE request was tunneled through POST via _method parameter, the POST parameter
- * will be returned instead (available since version 1.1.11).
- * @param string $name the DELETE parameter name
- * @param mixed $defaultValue the default parameter value if the DELETE parameter does not exist.
- * @return mixed the DELETE parameter value
- * @since 1.1.7
- */
- public function getDelete($name,$defaultValue=null)
- {
- if($this->getIsDeleteViaPostRequest())
- return $this->getPost($name, $defaultValue);
- if($this->getIsDeleteRequest())
- {
- $this->getRestParams();
- return isset($this->_restParams[$name]) ? $this->_restParams[$name] : $defaultValue;
- }
- else
- return $defaultValue;
- }
- /**
- * Returns the named PUT parameter value.
- * If the PUT parameter does not exist or if the current request is not a PUT request,
- * the second parameter to this method will be returned.
- * If the PUT request was tunneled through POST via _method parameter, the POST parameter
- * will be returned instead (available since version 1.1.11).
- * @param string $name the PUT parameter name
- * @param mixed $defaultValue the default parameter value if the PUT parameter does not exist.
- * @return mixed the PUT parameter value
- * @since 1.1.7
- */
- public function getPut($name,$defaultValue=null)
- {
- if($this->getIsPutViaPostRequest())
- return $this->getPost($name, $defaultValue);
- if($this->getIsPutRequest())
- {
- $this->getRestParams();
- return isset($this->_restParams[$name]) ? $this->_restParams[$name] : $defaultValue;
- }
- else
- return $defaultValue;
- }
- /**
- * Returns request parameters. Typically PUT or DELETE.
- * @return array the request parameters
- * @since 1.1.7
- * @since 1.1.13 method became public
- */
- public function getRestParams()
- {
-