File indexing completed on 2024-12-22 05:36:33

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Controller
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /** @see Zend_Controller_Request_Abstract */
0023 // require_once 'Zend/Controller/Request/Abstract.php';
0024 
0025 /** @see Zend_Uri */
0026 // require_once 'Zend/Uri.php';
0027 
0028 /**
0029  * Zend_Controller_Request_Http
0030  *
0031  * HTTP request object for use with Zend_Controller family.
0032  *
0033  * @uses Zend_Controller_Request_Abstract
0034  * @package Zend_Controller
0035  * @subpackage Request
0036  */
0037 class Zend_Controller_Request_Http extends Zend_Controller_Request_Abstract
0038 {
0039     /**
0040      * Scheme for http
0041      *
0042      */
0043     const SCHEME_HTTP  = 'http';
0044 
0045     /**
0046      * Scheme for https
0047      *
0048      */
0049     const SCHEME_HTTPS = 'https';
0050 
0051     /**
0052      * Allowed parameter sources
0053      * @var array
0054      */
0055     protected $_paramSources = array('_GET', '_POST');
0056 
0057     /**
0058      * REQUEST_URI
0059      * @var string;
0060      */
0061     protected $_requestUri;
0062 
0063     /**
0064      * Base URL of request
0065      * @var string
0066      */
0067     protected $_baseUrl = null;
0068 
0069     /**
0070      * Base path of request
0071      * @var string
0072      */
0073     protected $_basePath = null;
0074 
0075     /**
0076      * PATH_INFO
0077      * @var string
0078      */
0079     protected $_pathInfo = '';
0080 
0081     /**
0082      * Instance parameters
0083      * @var array
0084      */
0085     protected $_params = array();
0086 
0087     /**
0088      * Raw request body
0089      * @var string|false
0090      */
0091     protected $_rawBody;
0092 
0093     /**
0094      * Alias keys for request parameters
0095      * @var array
0096      */
0097     protected $_aliases = array();
0098 
0099     /**
0100      * Constructor
0101      *
0102      * If a $uri is passed, the object will attempt to populate itself using
0103      * that information.
0104      *
0105      * @param string|Zend_Uri $uri
0106      * @return void
0107      * @throws Zend_Controller_Request_Exception when invalid URI passed
0108      */
0109     public function __construct($uri = null)
0110     {
0111         if (null !== $uri) {
0112             if (!$uri instanceof Zend_Uri) {
0113                 $uri = Zend_Uri::factory($uri);
0114             }
0115             if ($uri->valid()) {
0116                 $path  = $uri->getPath();
0117                 $query = $uri->getQuery();
0118                 if (!empty($query)) {
0119                     $path .= '?' . $query;
0120                 }
0121 
0122                 $this->setRequestUri($path);
0123             } else {
0124                 // require_once 'Zend/Controller/Request/Exception.php';
0125                 throw new Zend_Controller_Request_Exception('Invalid URI provided to constructor');
0126             }
0127         } else {
0128             $this->setRequestUri();
0129         }
0130     }
0131 
0132     /**
0133      * Access values contained in the superglobals as public members
0134      * Order of precedence: 1. GET, 2. POST, 3. COOKIE, 4. SERVER, 5. ENV
0135      *
0136      * @see http://msdn.microsoft.com/en-us/library/system.web.httprequest.item.aspx
0137      * @param string $key
0138      * @return mixed
0139      */
0140     public function __get($key)
0141     {
0142         switch (true) {
0143             case isset($this->_params[$key]):
0144                 return $this->_params[$key];
0145             case isset($_GET[$key]):
0146                 return $_GET[$key];
0147             case isset($_POST[$key]):
0148                 return $_POST[$key];
0149             case isset($_COOKIE[$key]):
0150                 return $_COOKIE[$key];
0151             case ($key == 'REQUEST_URI'):
0152                 return $this->getRequestUri();
0153             case ($key == 'PATH_INFO'):
0154                 return $this->getPathInfo();
0155             case isset($_SERVER[$key]):
0156                 return $_SERVER[$key];
0157             case isset($_ENV[$key]):
0158                 return $_ENV[$key];
0159             default:
0160                 return null;
0161         }
0162     }
0163 
0164     /**
0165      * Alias to __get
0166      *
0167      * @param string $key
0168      * @return mixed
0169      */
0170     public function get($key)
0171     {
0172         return $this->__get($key);
0173     }
0174 
0175     /**
0176      * Set values
0177      *
0178      * In order to follow {@link __get()}, which operates on a number of
0179      * superglobals, setting values through overloading is not allowed and will
0180      * raise an exception. Use setParam() instead.
0181      *
0182      * @param string $key
0183      * @param mixed $value
0184      * @return void
0185      * @throws Zend_Controller_Request_Exception
0186      */
0187     public function __set($key, $value)
0188     {
0189         // require_once 'Zend/Controller/Request/Exception.php';
0190         throw new Zend_Controller_Request_Exception('Setting values in superglobals not allowed; please use setParam()');
0191     }
0192 
0193     /**
0194      * Alias to __set()
0195      *
0196      * @param string $key
0197      * @param mixed $value
0198      * @return void
0199      */
0200     public function set($key, $value)
0201     {
0202         return $this->__set($key, $value);
0203     }
0204 
0205     /**
0206      * Check to see if a property is set
0207      *
0208      * @param string $key
0209      * @return boolean
0210      */
0211     public function __isset($key)
0212     {
0213         switch (true) {
0214             case isset($this->_params[$key]):
0215                 return true;
0216             case isset($_GET[$key]):
0217                 return true;
0218             case isset($_POST[$key]):
0219                 return true;
0220             case isset($_COOKIE[$key]):
0221                 return true;
0222             case isset($_SERVER[$key]):
0223                 return true;
0224             case isset($_ENV[$key]):
0225                 return true;
0226             default:
0227                 return false;
0228         }
0229     }
0230 
0231     /**
0232      * Alias to __isset()
0233      *
0234      * @param string $key
0235      * @return boolean
0236      */
0237     public function has($key)
0238     {
0239         return $this->__isset($key);
0240     }
0241 
0242     /**
0243      * Set GET values
0244      *
0245      * @param  string|array $spec
0246      * @param  null|mixed $value
0247      * @return Zend_Controller_Request_Http
0248      */
0249     public function setQuery($spec, $value = null)
0250     {
0251         if ((null === $value) && !is_array($spec)) {
0252             // require_once 'Zend/Controller/Exception.php';
0253             throw new Zend_Controller_Exception('Invalid value passed to setQuery(); must be either array of values or key/value pair');
0254         }
0255         if ((null === $value) && is_array($spec)) {
0256             foreach ($spec as $key => $value) {
0257                 $this->setQuery($key, $value);
0258             }
0259             return $this;
0260         }
0261         $_GET[(string) $spec] = $value;
0262         return $this;
0263     }
0264 
0265     /**
0266      * Retrieve a member of the $_GET superglobal
0267      *
0268      * If no $key is passed, returns the entire $_GET array.
0269      *
0270      * @todo How to retrieve from nested arrays
0271      * @param string $key
0272      * @param mixed $default Default value to use if key not found
0273      * @return mixed Returns null if key does not exist
0274      */
0275     public function getQuery($key = null, $default = null)
0276     {
0277         if (null === $key) {
0278             return $_GET;
0279         }
0280 
0281         return (isset($_GET[$key])) ? $_GET[$key] : $default;
0282     }
0283 
0284     /**
0285      * Set POST values
0286      *
0287      * @param  string|array $spec
0288      * @param  null|mixed $value
0289      * @return Zend_Controller_Request_Http
0290      */
0291     public function setPost($spec, $value = null)
0292     {
0293         if ((null === $value) && !is_array($spec)) {
0294             // require_once 'Zend/Controller/Exception.php';
0295             throw new Zend_Controller_Exception('Invalid value passed to setPost(); must be either array of values or key/value pair');
0296         }
0297         if ((null === $value) && is_array($spec)) {
0298             foreach ($spec as $key => $value) {
0299                 $this->setPost($key, $value);
0300             }
0301             return $this;
0302         }
0303         $_POST[(string) $spec] = $value;
0304         return $this;
0305     }
0306 
0307     /**
0308      * Retrieve a member of the $_POST superglobal
0309      *
0310      * If no $key is passed, returns the entire $_POST array.
0311      *
0312      * @todo How to retrieve from nested arrays
0313      * @param string $key
0314      * @param mixed $default Default value to use if key not found
0315      * @return mixed Returns null if key does not exist
0316      */
0317     public function getPost($key = null, $default = null)
0318     {
0319         if (null === $key) {
0320             return $_POST;
0321         }
0322 
0323         return (isset($_POST[$key])) ? $_POST[$key] : $default;
0324     }
0325 
0326     /**
0327      * Retrieve a member of the $_COOKIE superglobal
0328      *
0329      * If no $key is passed, returns the entire $_COOKIE array.
0330      *
0331      * @todo How to retrieve from nested arrays
0332      * @param string $key
0333      * @param mixed $default Default value to use if key not found
0334      * @return mixed Returns null if key does not exist
0335      */
0336     public function getCookie($key = null, $default = null)
0337     {
0338         if (null === $key) {
0339             return $_COOKIE;
0340         }
0341 
0342         return (isset($_COOKIE[$key])) ? $_COOKIE[$key] : $default;
0343     }
0344 
0345     /**
0346      * Retrieve a member of the $_SERVER superglobal
0347      *
0348      * If no $key is passed, returns the entire $_SERVER array.
0349      *
0350      * @param string $key
0351      * @param mixed $default Default value to use if key not found
0352      * @return mixed Returns null if key does not exist
0353      */
0354     public function getServer($key = null, $default = null)
0355     {
0356         if (null === $key) {
0357             return $_SERVER;
0358         }
0359 
0360         return (isset($_SERVER[$key])) ? $_SERVER[$key] : $default;
0361     }
0362 
0363     /**
0364      * Retrieve a member of the $_ENV superglobal
0365      *
0366      * If no $key is passed, returns the entire $_ENV array.
0367      *
0368      * @param string $key
0369      * @param mixed $default Default value to use if key not found
0370      * @return mixed Returns null if key does not exist
0371      */
0372     public function getEnv($key = null, $default = null)
0373     {
0374         if (null === $key) {
0375             return $_ENV;
0376         }
0377 
0378         return (isset($_ENV[$key])) ? $_ENV[$key] : $default;
0379     }
0380 
0381     /**
0382      * Set the REQUEST_URI on which the instance operates
0383      *
0384      * If no request URI is passed, uses the value in $_SERVER['REQUEST_URI'],
0385      * $_SERVER['HTTP_X_REWRITE_URL'], or $_SERVER['ORIG_PATH_INFO'] + $_SERVER['QUERY_STRING'].
0386      *
0387      * @param string $requestUri
0388      * @return Zend_Controller_Request_Http
0389      */
0390     public function setRequestUri($requestUri = null)
0391     {
0392         if ($requestUri === null) {
0393             if (isset($_SERVER['HTTP_X_ORIGINAL_URL'])) { 
0394                 // IIS with Microsoft Rewrite Module
0395                 $requestUri = $_SERVER['HTTP_X_ORIGINAL_URL'];
0396             } elseif (isset($_SERVER['HTTP_X_REWRITE_URL'])) { 
0397                 // IIS with ISAPI_Rewrite
0398                 $requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
0399             } elseif (
0400                 // IIS7 with URL Rewrite: make sure we get the unencoded url (double slash problem)
0401                 isset($_SERVER['IIS_WasUrlRewritten'])
0402                 && $_SERVER['IIS_WasUrlRewritten'] == '1'
0403                 && isset($_SERVER['UNENCODED_URL'])
0404                 && $_SERVER['UNENCODED_URL'] != ''
0405                 ) {
0406                 $requestUri = $_SERVER['UNENCODED_URL'];
0407             } elseif (isset($_SERVER['REQUEST_URI'])) {
0408                 $requestUri = $_SERVER['REQUEST_URI'];
0409                 // Http proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path
0410                 $schemeAndHttpHost = $this->getScheme() . '://' . $this->getHttpHost();
0411                 if (strpos($requestUri, $schemeAndHttpHost) === 0) {
0412                     $requestUri = substr($requestUri, strlen($schemeAndHttpHost));
0413                 }
0414             } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI
0415                 $requestUri = $_SERVER['ORIG_PATH_INFO'];
0416                 if (!empty($_SERVER['QUERY_STRING'])) {
0417                     $requestUri .= '?' . $_SERVER['QUERY_STRING'];
0418                 }
0419             } else {
0420                 return $this;
0421             }
0422         } elseif (!is_string($requestUri)) {
0423             return $this;
0424         } else {
0425             // Set GET items, if available
0426             if (false !== ($pos = strpos($requestUri, '?'))) {
0427                 // Get key => value pairs and set $_GET
0428                 $query = substr($requestUri, $pos + 1);
0429                 parse_str($query, $vars);
0430                 $this->setQuery($vars);
0431             }
0432         }
0433 
0434         $this->_requestUri = $requestUri;
0435         return $this;
0436     }
0437 
0438     /**
0439      * Returns the REQUEST_URI taking into account
0440      * platform differences between Apache and IIS
0441      *
0442      * @return string
0443      */
0444     public function getRequestUri()
0445     {
0446         if (empty($this->_requestUri)) {
0447             $this->setRequestUri();
0448         }
0449 
0450         return $this->_requestUri;
0451     }
0452 
0453     /**
0454      * Set the base URL of the request; i.e., the segment leading to the script name
0455      *
0456      * E.g.:
0457      * - /admin
0458      * - /myapp
0459      * - /subdir/index.php
0460      *
0461      * Do not use the full URI when providing the base. The following are
0462      * examples of what not to use:
0463      * - http://example.com/admin (should be just /admin)
0464      * - http://example.com/subdir/index.php (should be just /subdir/index.php)
0465      *
0466      * If no $baseUrl is provided, attempts to determine the base URL from the
0467      * environment, using SCRIPT_FILENAME, SCRIPT_NAME, PHP_SELF, and
0468      * ORIG_SCRIPT_NAME in its determination.
0469      *
0470      * @param mixed $baseUrl
0471      * @return Zend_Controller_Request_Http
0472      */
0473     public function setBaseUrl($baseUrl = null)
0474     {
0475         if ((null !== $baseUrl) && !is_string($baseUrl)) {
0476             return $this;
0477         }
0478 
0479         if ($baseUrl === null) {
0480             $filename = (isset($_SERVER['SCRIPT_FILENAME'])) ? basename($_SERVER['SCRIPT_FILENAME']) : '';
0481 
0482             if (isset($_SERVER['SCRIPT_NAME']) && basename($_SERVER['SCRIPT_NAME']) === $filename) {
0483                 $baseUrl = $_SERVER['SCRIPT_NAME'];
0484             } elseif (isset($_SERVER['PHP_SELF']) && basename($_SERVER['PHP_SELF']) === $filename) {
0485                 $baseUrl = $_SERVER['PHP_SELF'];
0486             } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $filename) {
0487                 $baseUrl = $_SERVER['ORIG_SCRIPT_NAME']; // 1and1 shared hosting compatibility
0488             } else {
0489                 // Backtrack up the script_filename to find the portion matching
0490                 // php_self
0491                 $path    = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : '';
0492                 $file    = isset($_SERVER['SCRIPT_FILENAME']) ? $_SERVER['SCRIPT_FILENAME'] : '';
0493                 $segs    = explode('/', trim($file, '/'));
0494                 $segs    = array_reverse($segs);
0495                 $index   = 0;
0496                 $last    = count($segs);
0497                 $baseUrl = '';
0498                 do {
0499                     $seg     = $segs[$index];
0500                     $baseUrl = '/' . $seg . $baseUrl;
0501                     ++$index;
0502                 } while (($last > $index) && (false !== ($pos = strpos($path, $baseUrl))) && (0 != $pos));
0503             }
0504 
0505             // Does the baseUrl have anything in common with the request_uri?
0506             $requestUri = $this->getRequestUri();
0507 
0508             if (0 === strpos($requestUri, $baseUrl)) {
0509                 // full $baseUrl matches
0510                 $this->_baseUrl = $baseUrl;
0511                 return $this;
0512             }
0513 
0514             if (0 === strpos($requestUri, dirname($baseUrl))) {
0515                 // directory portion of $baseUrl matches
0516                 $this->_baseUrl = rtrim(dirname($baseUrl), '/');
0517                 return $this;
0518             }
0519 
0520             $truncatedRequestUri = $requestUri;
0521             if (($pos = strpos($requestUri, '?')) !== false) {
0522                 $truncatedRequestUri = substr($requestUri, 0, $pos);
0523             }
0524 
0525             $basename = basename($baseUrl);
0526             if (empty($basename) || !strpos($truncatedRequestUri, $basename)) {
0527                 // no match whatsoever; set it blank
0528                 $this->_baseUrl = '';
0529                 return $this;
0530             }
0531 
0532             // If using mod_rewrite or ISAPI_Rewrite strip the script filename
0533             // out of baseUrl. $pos !== 0 makes sure it is not matching a value
0534             // from PATH_INFO or QUERY_STRING
0535             if ((strlen($requestUri) >= strlen($baseUrl))
0536                 && ((false !== ($pos = strpos($requestUri, $baseUrl))) && ($pos !== 0)))
0537             {
0538                 $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl));
0539             }
0540         }
0541 
0542         $this->_baseUrl = rtrim($baseUrl, '/');
0543         return $this;
0544     }
0545 
0546     /**
0547      * Everything in REQUEST_URI before PATH_INFO
0548      * <form action="<?=$baseUrl?>/news/submit" method="POST"/>
0549      *
0550      * @return string
0551      */
0552     public function getBaseUrl($raw = false)
0553     {
0554         if (null === $this->_baseUrl) {
0555             $this->setBaseUrl();
0556         }
0557 
0558         return (($raw == false) ? urldecode($this->_baseUrl) : $this->_baseUrl);
0559     }
0560 
0561     /**
0562      * Set the base path for the URL
0563      *
0564      * @param string|null $basePath
0565      * @return Zend_Controller_Request_Http
0566      */
0567     public function setBasePath($basePath = null)
0568     {
0569         if ($basePath === null) {
0570             $filename = (isset($_SERVER['SCRIPT_FILENAME']))
0571                       ? basename($_SERVER['SCRIPT_FILENAME'])
0572                       : '';
0573 
0574             $baseUrl = $this->getBaseUrl();
0575             if (empty($baseUrl)) {
0576                 $this->_basePath = '';
0577                 return $this;
0578             }
0579 
0580             if (basename($baseUrl) === $filename) {
0581                 $basePath = dirname($baseUrl);
0582             } else {
0583                 $basePath = $baseUrl;
0584             }
0585         }
0586 
0587         if (substr(PHP_OS, 0, 3) === 'WIN') {
0588             $basePath = str_replace('\\', '/', $basePath);
0589         }
0590 
0591         $this->_basePath = rtrim($basePath, '/');
0592         return $this;
0593     }
0594 
0595     /**
0596      * Everything in REQUEST_URI before PATH_INFO not including the filename
0597      * <img src="<?=$basePath?>/images/zend.png"/>
0598      *
0599      * @return string
0600      */
0601     public function getBasePath()
0602     {
0603         if (null === $this->_basePath) {
0604             $this->setBasePath();
0605         }
0606 
0607         return $this->_basePath;
0608     }
0609 
0610     /**
0611      * Set the PATH_INFO string
0612      *
0613      * @param string|null $pathInfo
0614      * @return Zend_Controller_Request_Http
0615      */
0616     public function setPathInfo($pathInfo = null)
0617     {
0618         if ($pathInfo === null) {
0619             $baseUrl = $this->getBaseUrl(); // this actually calls setBaseUrl() & setRequestUri()
0620             $baseUrlRaw = $this->getBaseUrl(false);
0621             $baseUrlEncoded = urlencode($baseUrlRaw);
0622         
0623             if (null === ($requestUri = $this->getRequestUri())) {
0624                 return $this;
0625             }
0626         
0627             // Remove the query string from REQUEST_URI
0628             if ($pos = strpos($requestUri, '?')) {
0629                 $requestUri = substr($requestUri, 0, $pos);
0630             }
0631             
0632             if (!empty($baseUrl) || !empty($baseUrlRaw)) {
0633                 if (strpos($requestUri, $baseUrl) === 0) {
0634                     $pathInfo = substr($requestUri, strlen($baseUrl));
0635                 } elseif (strpos($requestUri, $baseUrlRaw) === 0) {
0636                     $pathInfo = substr($requestUri, strlen($baseUrlRaw));
0637                 } elseif (strpos($requestUri, $baseUrlEncoded) === 0) {
0638                     $pathInfo = substr($requestUri, strlen($baseUrlEncoded));
0639                 } else {
0640                     $pathInfo = $requestUri;
0641                 }
0642             } else {
0643                 $pathInfo = $requestUri;
0644             }
0645         
0646         }
0647 
0648         $this->_pathInfo = (string) $pathInfo;
0649         return $this;
0650     }
0651 
0652     /**
0653      * Returns everything between the BaseUrl and QueryString.
0654      * This value is calculated instead of reading PATH_INFO
0655      * directly from $_SERVER due to cross-platform differences.
0656      *
0657      * @return string
0658      */
0659     public function getPathInfo()
0660     {
0661         if (empty($this->_pathInfo)) {
0662             $this->setPathInfo();
0663         }
0664 
0665         return $this->_pathInfo;
0666     }
0667 
0668     /**
0669      * Set allowed parameter sources
0670      *
0671      * Can be empty array, or contain one or more of '_GET' or '_POST'.
0672      *
0673      * @param  array $paramSoures
0674      * @return Zend_Controller_Request_Http
0675      */
0676     public function setParamSources(array $paramSources = array())
0677     {
0678         $this->_paramSources = $paramSources;
0679         return $this;
0680     }
0681 
0682     /**
0683      * Get list of allowed parameter sources
0684      *
0685      * @return array
0686      */
0687     public function getParamSources()
0688     {
0689         return $this->_paramSources;
0690     }
0691 
0692     /**
0693      * Set a userland parameter
0694      *
0695      * Uses $key to set a userland parameter. If $key is an alias, the actual
0696      * key will be retrieved and used to set the parameter.
0697      *
0698      * @param mixed $key
0699      * @param mixed $value
0700      * @return Zend_Controller_Request_Http
0701      */
0702     public function setParam($key, $value)
0703     {
0704         $key = (null !== ($alias = $this->getAlias($key))) ? $alias : $key;
0705         parent::setParam($key, $value);
0706         return $this;
0707     }
0708 
0709     /**
0710      * Retrieve a parameter
0711      *
0712      * Retrieves a parameter from the instance. Priority is in the order of
0713      * userland parameters (see {@link setParam()}), $_GET, $_POST. If a
0714      * parameter matching the $key is not found, null is returned.
0715      *
0716      * If the $key is an alias, the actual key aliased will be used.
0717      *
0718      * @param mixed $key
0719      * @param mixed $default Default value to use if key not found
0720      * @return mixed
0721      */
0722     public function getParam($key, $default = null)
0723     {
0724         $keyName = (null !== ($alias = $this->getAlias($key))) ? $alias : $key;
0725 
0726         $paramSources = $this->getParamSources();
0727         if (isset($this->_params[$keyName])) {
0728             return $this->_params[$keyName];
0729         } elseif (in_array('_GET', $paramSources) && (isset($_GET[$keyName]))) {
0730             return $_GET[$keyName];
0731         } elseif (in_array('_POST', $paramSources) && (isset($_POST[$keyName]))) {
0732             return $_POST[$keyName];
0733         }
0734 
0735         return $default;
0736     }
0737 
0738     /**
0739      * Retrieve an array of parameters
0740      *
0741      * Retrieves a merged array of parameters, with precedence of userland
0742      * params (see {@link setParam()}), $_GET, $_POST (i.e., values in the
0743      * userland params will take precedence over all others).
0744      *
0745      * @return array
0746      */
0747     public function getParams()
0748     {
0749         $return       = $this->_params;
0750         $paramSources = $this->getParamSources();
0751         if (in_array('_GET', $paramSources)
0752             && isset($_GET)
0753             && is_array($_GET)
0754         ) {
0755             $return += $_GET;
0756         }
0757         if (in_array('_POST', $paramSources)
0758             && isset($_POST)
0759             && is_array($_POST)
0760         ) {
0761             $return += $_POST;
0762         }
0763         return $return;
0764     }
0765 
0766     /**
0767      * Set parameters
0768      *
0769      * Set one or more parameters. Parameters are set as userland parameters,
0770      * using the keys specified in the array.
0771      *
0772      * @param array $params
0773      * @return Zend_Controller_Request_Http
0774      */
0775     public function setParams(array $params)
0776     {
0777         foreach ($params as $key => $value) {
0778             $this->setParam($key, $value);
0779         }
0780         return $this;
0781     }
0782 
0783     /**
0784      * Set a key alias
0785      *
0786      * Set an alias used for key lookups. $name specifies the alias, $target
0787      * specifies the actual key to use.
0788      *
0789      * @param string $name
0790      * @param string $target
0791      * @return Zend_Controller_Request_Http
0792      */
0793     public function setAlias($name, $target)
0794     {
0795         $this->_aliases[$name] = $target;
0796         return $this;
0797     }
0798 
0799     /**
0800      * Retrieve an alias
0801      *
0802      * Retrieve the actual key represented by the alias $name.
0803      *
0804      * @param string $name
0805      * @return string|null Returns null when no alias exists
0806      */
0807     public function getAlias($name)
0808     {
0809         if (isset($this->_aliases[$name])) {
0810             return $this->_aliases[$name];
0811         }
0812 
0813         return null;
0814     }
0815 
0816     /**
0817      * Retrieve the list of all aliases
0818      *
0819      * @return array
0820      */
0821     public function getAliases()
0822     {
0823         return $this->_aliases;
0824     }
0825 
0826     /**
0827      * Return the method by which the request was made
0828      *
0829      * @return string
0830      */
0831     public function getMethod()
0832     {
0833         return $this->getServer('REQUEST_METHOD');
0834     }
0835 
0836     /**
0837      * Was the request made by POST?
0838      *
0839      * @return boolean
0840      */
0841     public function isPost()
0842     {
0843         if ('POST' == $this->getMethod()) {
0844             return true;
0845         }
0846 
0847         return false;
0848     }
0849 
0850     /**
0851      * Was the request made by GET?
0852      *
0853      * @return boolean
0854      */
0855     public function isGet()
0856     {
0857         if ('GET' == $this->getMethod()) {
0858             return true;
0859         }
0860 
0861         return false;
0862     }
0863 
0864     /**
0865      * Was the request made by PUT?
0866      *
0867      * @return boolean
0868      */
0869     public function isPut()
0870     {
0871         if ('PUT' == $this->getMethod()) {
0872             return true;
0873         }
0874 
0875         return false;
0876     }
0877 
0878     /**
0879      * Was the request made by DELETE?
0880      *
0881      * @return boolean
0882      */
0883     public function isDelete()
0884     {
0885         if ('DELETE' == $this->getMethod()) {
0886             return true;
0887         }
0888 
0889         return false;
0890     }
0891 
0892     /**
0893      * Was the request made by HEAD?
0894      *
0895      * @return boolean
0896      */
0897     public function isHead()
0898     {
0899         if ('HEAD' == $this->getMethod()) {
0900             return true;
0901         }
0902 
0903         return false;
0904     }
0905 
0906     /**
0907      * Was the request made by OPTIONS?
0908      *
0909      * @return boolean
0910      */
0911     public function isOptions()
0912     {
0913         if ('OPTIONS' == $this->getMethod()) {
0914             return true;
0915         }
0916 
0917         return false;
0918     }
0919 
0920     /**
0921      * Was the request made by PATCH?
0922      *
0923      * @return boolean
0924      */
0925     public function isPatch()
0926     {
0927         if ('PATCH' == $this->getMethod()) {
0928             return true;
0929         }
0930 
0931         return false;
0932     }
0933 
0934     /**
0935      * Is the request a Javascript XMLHttpRequest?
0936      *
0937      * Should work with Prototype/Script.aculo.us, possibly others.
0938      *
0939      * @return boolean
0940      */
0941     public function isXmlHttpRequest()
0942     {
0943         return ($this->getHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
0944     }
0945 
0946     /**
0947      * Is this a Flash request?
0948      *
0949      * @return boolean
0950      */
0951     public function isFlashRequest()
0952     {
0953         $header = strtolower($this->getHeader('USER_AGENT'));
0954         return (strstr($header, ' flash')) ? true : false;
0955     }
0956 
0957     /**
0958      * Is https secure request
0959      *
0960      * @return boolean
0961      */
0962     public function isSecure()
0963     {
0964         return ($this->getScheme() === self::SCHEME_HTTPS);
0965     }
0966 
0967     /**
0968      * Return the raw body of the request, if present
0969      *
0970      * @return string|false Raw body, or false if not present
0971      */
0972     public function getRawBody()
0973     {
0974         if (null === $this->_rawBody) {
0975             $body = file_get_contents('php://input');
0976 
0977             if (strlen(trim($body)) > 0) {
0978                 $this->_rawBody = $body;
0979             } else {
0980                 $this->_rawBody = false;
0981             }
0982         }
0983         return $this->_rawBody;
0984     }
0985 
0986     /**
0987      * Return the value of the given HTTP header. Pass the header name as the
0988      * plain, HTTP-specified header name. Ex.: Ask for 'Accept' to get the
0989      * Accept header, 'Accept-Encoding' to get the Accept-Encoding header.
0990      *
0991      * @param string $header HTTP header name
0992      * @return string|false HTTP header value, or false if not found
0993      * @throws Zend_Controller_Request_Exception
0994      */
0995     public function getHeader($header)
0996     {
0997         if (empty($header)) {
0998             // require_once 'Zend/Controller/Request/Exception.php';
0999             throw new Zend_Controller_Request_Exception('An HTTP header name is required');
1000         }
1001 
1002         // Try to get it from the $_SERVER array first
1003         $temp = strtoupper(str_replace('-', '_', $header));
1004         if (isset($_SERVER['HTTP_' . $temp])) {
1005             return $_SERVER['HTTP_' . $temp];
1006         }
1007 
1008         /*
1009          * Try to get it from the $_SERVER array on POST request or CGI environment
1010          * @see https://www.ietf.org/rfc/rfc3875 (4.1.2. and 4.1.3.)
1011          */
1012         if (isset($_SERVER[$temp])
1013             && in_array($temp, array('CONTENT_TYPE', 'CONTENT_LENGTH'))
1014         ) {
1015             return $_SERVER[$temp];
1016         }
1017 
1018         // This seems to be the only way to get the Authorization header on
1019         // Apache
1020         if (function_exists('apache_request_headers')) {
1021             $headers = apache_request_headers();
1022             if (isset($headers[$header])) {
1023                 return $headers[$header];
1024             }
1025             $header = strtolower($header);
1026             foreach ($headers as $key => $value) {
1027                 if (strtolower($key) == $header) {
1028                     return $value;
1029                 }
1030             }
1031         }
1032 
1033         return false;
1034     }
1035 
1036     /**
1037      * Get the request URI scheme
1038      *
1039      * @return string
1040      */
1041     public function getScheme()
1042     {
1043         return ($this->getServer('HTTPS') == 'on') ? self::SCHEME_HTTPS : self::SCHEME_HTTP;
1044     }
1045 
1046     /**
1047      * Get the HTTP host.
1048      *
1049      * "Host" ":" host [ ":" port ] ; Section 3.2.2
1050      * Note the HTTP Host header is not the same as the URI host.
1051      * It includes the port while the URI host doesn't.
1052      *
1053      * @return string
1054      */
1055     public function getHttpHost()
1056     {
1057         $host = $this->getServer('HTTP_HOST');
1058         if (!empty($host)) {
1059             return $host;
1060         }
1061 
1062         $scheme = $this->getScheme();
1063         $name   = $this->getServer('SERVER_NAME');
1064         $port   = $this->getServer('SERVER_PORT');
1065 
1066         if(null === $name) {
1067             return '';
1068         }
1069         elseif (($scheme == self::SCHEME_HTTP && $port == 80) || ($scheme == self::SCHEME_HTTPS && $port == 443)) {
1070             return $name;
1071         } else {
1072             return $name . ':' . $port;
1073         }
1074     }
1075 
1076     /**
1077      * Get the client's IP addres
1078      *
1079      * @param  boolean $checkProxy
1080      * @return string
1081      */
1082     public function getClientIp($checkProxy = true)
1083     {
1084         if ($checkProxy && $this->getServer('HTTP_CLIENT_IP') != null) {
1085             $ip = $this->getServer('HTTP_CLIENT_IP');
1086         } else if ($checkProxy && $this->getServer('HTTP_X_FORWARDED_FOR') != null) {
1087             $ip = $this->getServer('HTTP_X_FORWARDED_FOR');
1088         } else {
1089             $ip = $this->getServer('REMOTE_ADDR');
1090         }
1091 
1092         return $ip;
1093     }
1094 }