PageRenderTime 58ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Controller/Request/Http.php

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 1064 lines | 715 code | 63 blank | 286 comment | 89 complexity | fdb043566ac6ae20255af138e510c8a4 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Http.php 23414 2010-11-20 10:56:11Z bittarman $
  20. */
  21. /** @see Zend_Controller_Request_Abstract */
  22. #require_once 'Zend/Controller/Request/Abstract.php';
  23. /** @see Zend_Uri */
  24. #require_once 'Zend/Uri.php';
  25. /**
  26. * Zend_Controller_Request_Http
  27. *
  28. * HTTP request object for use with Zend_Controller family.
  29. *
  30. * @uses Zend_Controller_Request_Abstract
  31. * @package Zend_Controller
  32. * @subpackage Request
  33. */
  34. class Zend_Controller_Request_Http extends Zend_Controller_Request_Abstract
  35. {
  36. /**
  37. * Scheme for http
  38. *
  39. */
  40. const SCHEME_HTTP = 'http';
  41. /**
  42. * Scheme for https
  43. *
  44. */
  45. const SCHEME_HTTPS = 'https';
  46. /**
  47. * Allowed parameter sources
  48. * @var array
  49. */
  50. protected $_paramSources = array('_GET', '_POST');
  51. /**
  52. * REQUEST_URI
  53. * @var string;
  54. */
  55. protected $_requestUri;
  56. /**
  57. * Base URL of request
  58. * @var string
  59. */
  60. protected $_baseUrl = null;
  61. /**
  62. * Base path of request
  63. * @var string
  64. */
  65. protected $_basePath = null;
  66. /**
  67. * PATH_INFO
  68. * @var string
  69. */
  70. protected $_pathInfo = '';
  71. /**
  72. * Instance parameters
  73. * @var array
  74. */
  75. protected $_params = array();
  76. /**
  77. * Raw request body
  78. * @var string|false
  79. */
  80. protected $_rawBody;
  81. /**
  82. * Alias keys for request parameters
  83. * @var array
  84. */
  85. protected $_aliases = array();
  86. /**
  87. * Constructor
  88. *
  89. * If a $uri is passed, the object will attempt to populate itself using
  90. * that information.
  91. *
  92. * @param string|Zend_Uri $uri
  93. * @return void
  94. * @throws Zend_Controller_Request_Exception when invalid URI passed
  95. */
  96. public function __construct($uri = null)
  97. {
  98. if (null !== $uri) {
  99. if (!$uri instanceof Zend_Uri) {
  100. $uri = Zend_Uri::factory($uri);
  101. }
  102. if ($uri->valid()) {
  103. $path = $uri->getPath();
  104. $query = $uri->getQuery();
  105. if (!empty($query)) {
  106. $path .= '?' . $query;
  107. }
  108. $this->setRequestUri($path);
  109. } else {
  110. #require_once 'Zend/Controller/Request/Exception.php';
  111. throw new Zend_Controller_Request_Exception('Invalid URI provided to constructor');
  112. }
  113. } else {
  114. $this->setRequestUri();
  115. }
  116. }
  117. /**
  118. * Access values contained in the superglobals as public members
  119. * Order of precedence: 1. GET, 2. POST, 3. COOKIE, 4. SERVER, 5. ENV
  120. *
  121. * @see http://msdn.microsoft.com/en-us/library/system.web.httprequest.item.aspx
  122. * @param string $key
  123. * @return mixed
  124. */
  125. public function __get($key)
  126. {
  127. switch (true) {
  128. case isset($this->_params[$key]):
  129. return $this->_params[$key];
  130. case isset($_GET[$key]):
  131. return $_GET[$key];
  132. case isset($_POST[$key]):
  133. return $_POST[$key];
  134. case isset($_COOKIE[$key]):
  135. return $_COOKIE[$key];
  136. case ($key == 'REQUEST_URI'):
  137. return $this->getRequestUri();
  138. case ($key == 'PATH_INFO'):
  139. return $this->getPathInfo();
  140. case isset($_SERVER[$key]):
  141. return $_SERVER[$key];
  142. case isset($_ENV[$key]):
  143. return $_ENV[$key];
  144. default:
  145. return null;
  146. }
  147. }
  148. /**
  149. * Alias to __get
  150. *
  151. * @param string $key
  152. * @return mixed
  153. */
  154. public function get($key)
  155. {
  156. return $this->__get($key);
  157. }
  158. /**
  159. * Set values
  160. *
  161. * In order to follow {@link __get()}, which operates on a number of
  162. * superglobals, setting values through overloading is not allowed and will
  163. * raise an exception. Use setParam() instead.
  164. *
  165. * @param string $key
  166. * @param mixed $value
  167. * @return void
  168. * @throws Zend_Controller_Request_Exception
  169. */
  170. public function __set($key, $value)
  171. {
  172. #require_once 'Zend/Controller/Request/Exception.php';
  173. throw new Zend_Controller_Request_Exception('Setting values in superglobals not allowed; please use setParam()');
  174. }
  175. /**
  176. * Alias to __set()
  177. *
  178. * @param string $key
  179. * @param mixed $value
  180. * @return void
  181. */
  182. public function set($key, $value)
  183. {
  184. return $this->__set($key, $value);
  185. }
  186. /**
  187. * Check to see if a property is set
  188. *
  189. * @param string $key
  190. * @return boolean
  191. */
  192. public function __isset($key)
  193. {
  194. switch (true) {
  195. case isset($this->_params[$key]):
  196. return true;
  197. case isset($_GET[$key]):
  198. return true;
  199. case isset($_POST[$key]):
  200. return true;
  201. case isset($_COOKIE[$key]):
  202. return true;
  203. case isset($_SERVER[$key]):
  204. return true;
  205. case isset($_ENV[$key]):
  206. return true;
  207. default:
  208. return false;
  209. }
  210. }
  211. /**
  212. * Alias to __isset()
  213. *
  214. * @param string $key
  215. * @return boolean
  216. */
  217. public function has($key)
  218. {
  219. return $this->__isset($key);
  220. }
  221. /**
  222. * Set GET values
  223. *
  224. * @param string|array $spec
  225. * @param null|mixed $value
  226. * @return Zend_Controller_Request_Http
  227. */
  228. public function setQuery($spec, $value = null)
  229. {
  230. if ((null === $value) && !is_array($spec)) {
  231. #require_once 'Zend/Controller/Exception.php';
  232. throw new Zend_Controller_Exception('Invalid value passed to setQuery(); must be either array of values or key/value pair');
  233. }
  234. if ((null === $value) && is_array($spec)) {
  235. foreach ($spec as $key => $value) {
  236. $this->setQuery($key, $value);
  237. }
  238. return $this;
  239. }
  240. $_GET[(string) $spec] = $value;
  241. return $this;
  242. }
  243. /**
  244. * Retrieve a member of the $_GET superglobal
  245. *
  246. * If no $key is passed, returns the entire $_GET array.
  247. *
  248. * @todo How to retrieve from nested arrays
  249. * @param string $key
  250. * @param mixed $default Default value to use if key not found
  251. * @return mixed Returns null if key does not exist
  252. */
  253. public function getQuery($key = null, $default = null)
  254. {
  255. if (null === $key) {
  256. return $_GET;
  257. }
  258. return (isset($_GET[$key])) ? $_GET[$key] : $default;
  259. }
  260. /**
  261. * Set POST values
  262. *
  263. * @param string|array $spec
  264. * @param null|mixed $value
  265. * @return Zend_Controller_Request_Http
  266. */
  267. public function setPost($spec, $value = null)
  268. {
  269. if ((null === $value) && !is_array($spec)) {
  270. #require_once 'Zend/Controller/Exception.php';
  271. throw new Zend_Controller_Exception('Invalid value passed to setPost(); must be either array of values or key/value pair');
  272. }
  273. if ((null === $value) && is_array($spec)) {
  274. foreach ($spec as $key => $value) {
  275. $this->setPost($key, $value);
  276. }
  277. return $this;
  278. }
  279. $_POST[(string) $spec] = $value;
  280. return $this;
  281. }
  282. /**
  283. * Retrieve a member of the $_POST superglobal
  284. *
  285. * If no $key is passed, returns the entire $_POST array.
  286. *
  287. * @todo How to retrieve from nested arrays
  288. * @param string $key
  289. * @param mixed $default Default value to use if key not found
  290. * @return mixed Returns null if key does not exist
  291. */
  292. public function getPost($key = null, $default = null)
  293. {
  294. if (null === $key) {
  295. return $_POST;
  296. }
  297. return (isset($_POST[$key])) ? $_POST[$key] : $default;
  298. }
  299. /**
  300. * Retrieve a member of the $_COOKIE superglobal
  301. *
  302. * If no $key is passed, returns the entire $_COOKIE array.
  303. *
  304. * @todo How to retrieve from nested arrays
  305. * @param string $key
  306. * @param mixed $default Default value to use if key not found
  307. * @return mixed Returns null if key does not exist
  308. */
  309. public function getCookie($key = null, $default = null)
  310. {
  311. if (null === $key) {
  312. return $_COOKIE;
  313. }
  314. return (isset($_COOKIE[$key])) ? $_COOKIE[$key] : $default;
  315. }
  316. /**
  317. * Retrieve a member of the $_SERVER superglobal
  318. *
  319. * If no $key is passed, returns the entire $_SERVER array.
  320. *
  321. * @param string $key
  322. * @param mixed $default Default value to use if key not found
  323. * @return mixed Returns null if key does not exist
  324. */
  325. public function getServer($key = null, $default = null)
  326. {
  327. if (null === $key) {
  328. return $_SERVER;
  329. }
  330. return (isset($_SERVER[$key])) ? $_SERVER[$key] : $default;
  331. }
  332. /**
  333. * Retrieve a member of the $_ENV superglobal
  334. *
  335. * If no $key is passed, returns the entire $_ENV array.
  336. *
  337. * @param string $key
  338. * @param mixed $default Default value to use if key not found
  339. * @return mixed Returns null if key does not exist
  340. */
  341. public function getEnv($key = null, $default = null)
  342. {
  343. if (null === $key) {
  344. return $_ENV;
  345. }
  346. return (isset($_ENV[$key])) ? $_ENV[$key] : $default;
  347. }
  348. /**
  349. * Set the REQUEST_URI on which the instance operates
  350. *
  351. * If no request URI is passed, uses the value in $_SERVER['REQUEST_URI'],
  352. * $_SERVER['HTTP_X_REWRITE_URL'], or $_SERVER['ORIG_PATH_INFO'] + $_SERVER['QUERY_STRING'].
  353. *
  354. * @param string $requestUri
  355. * @return Zend_Controller_Request_Http
  356. */
  357. public function setRequestUri($requestUri = null)
  358. {
  359. if ($requestUri === null) {
  360. if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // check this first so IIS will catch
  361. $requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
  362. } elseif (
  363. // IIS7 with URL Rewrite: make sure we get the unencoded url (double slash problem)
  364. isset($_SERVER['IIS_WasUrlRewritten'])
  365. && $_SERVER['IIS_WasUrlRewritten'] == '1'
  366. && isset($_SERVER['UNENCODED_URL'])
  367. && $_SERVER['UNENCODED_URL'] != ''
  368. ) {
  369. $requestUri = $_SERVER['UNENCODED_URL'];
  370. } elseif (isset($_SERVER['REQUEST_URI'])) {
  371. $requestUri = $_SERVER['REQUEST_URI'];
  372. // Http proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path
  373. $schemeAndHttpHost = $this->getScheme() . '://' . $this->getHttpHost();
  374. if (strpos($requestUri, $schemeAndHttpHost) === 0) {
  375. $requestUri = substr($requestUri, strlen($schemeAndHttpHost));
  376. }
  377. } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI
  378. $requestUri = $_SERVER['ORIG_PATH_INFO'];
  379. if (!empty($_SERVER['QUERY_STRING'])) {
  380. $requestUri .= '?' . $_SERVER['QUERY_STRING'];
  381. }
  382. } else {
  383. return $this;
  384. }
  385. } elseif (!is_string($requestUri)) {
  386. return $this;
  387. } else {
  388. // Set GET items, if available
  389. if (false !== ($pos = strpos($requestUri, '?'))) {
  390. // Get key => value pairs and set $_GET
  391. $query = substr($requestUri, $pos + 1);
  392. parse_str($query, $vars);
  393. $this->setQuery($vars);
  394. }
  395. }
  396. $this->_requestUri = $requestUri;
  397. return $this;
  398. }
  399. /**
  400. * Returns the REQUEST_URI taking into account
  401. * platform differences between Apache and IIS
  402. *
  403. * @return string
  404. */
  405. public function getRequestUri()
  406. {
  407. if (empty($this->_requestUri)) {
  408. $this->setRequestUri();
  409. }
  410. return $this->_requestUri;
  411. }
  412. /**
  413. * Set the base URL of the request; i.e., the segment leading to the script name
  414. *
  415. * E.g.:
  416. * - /admin
  417. * - /myapp
  418. * - /subdir/index.php
  419. *
  420. * Do not use the full URI when providing the base. The following are
  421. * examples of what not to use:
  422. * - http://example.com/admin (should be just /admin)
  423. * - http://example.com/subdir/index.php (should be just /subdir/index.php)
  424. *
  425. * If no $baseUrl is provided, attempts to determine the base URL from the
  426. * environment, using SCRIPT_FILENAME, SCRIPT_NAME, PHP_SELF, and
  427. * ORIG_SCRIPT_NAME in its determination.
  428. *
  429. * @param mixed $baseUrl
  430. * @return Zend_Controller_Request_Http
  431. */
  432. public function setBaseUrl($baseUrl = null)
  433. {
  434. if ((null !== $baseUrl) && !is_string($baseUrl)) {
  435. return $this;
  436. }
  437. if ($baseUrl === null) {
  438. $filename = (isset($_SERVER['SCRIPT_FILENAME'])) ? basename($_SERVER['SCRIPT_FILENAME']) : '';
  439. if (isset($_SERVER['SCRIPT_NAME']) && basename($_SERVER['SCRIPT_NAME']) === $filename) {
  440. $baseUrl = $_SERVER['SCRIPT_NAME'];
  441. } elseif (isset($_SERVER['PHP_SELF']) && basename($_SERVER['PHP_SELF']) === $filename) {
  442. $baseUrl = $_SERVER['PHP_SELF'];
  443. } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $filename) {
  444. $baseUrl = $_SERVER['ORIG_SCRIPT_NAME']; // 1and1 shared hosting compatibility
  445. } else {
  446. // Backtrack up the script_filename to find the portion matching
  447. // php_self
  448. $path = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : '';
  449. $file = isset($_SERVER['SCRIPT_FILENAME']) ? $_SERVER['SCRIPT_FILENAME'] : '';
  450. $segs = explode('/', trim($file, '/'));
  451. $segs = array_reverse($segs);
  452. $index = 0;
  453. $last = count($segs);
  454. $baseUrl = '';
  455. do {
  456. $seg = $segs[$index];
  457. $baseUrl = '/' . $seg . $baseUrl;
  458. ++$index;
  459. } while (($last > $index) && (false !== ($pos = strpos($path, $baseUrl))) && (0 != $pos));
  460. }
  461. // Does the baseUrl have anything in common with the request_uri?
  462. $requestUri = $this->getRequestUri();
  463. if (0 === strpos($requestUri, $baseUrl)) {
  464. // full $baseUrl matches
  465. $this->_baseUrl = $baseUrl;
  466. return $this;
  467. }
  468. if (0 === strpos($requestUri, dirname($baseUrl))) {
  469. // directory portion of $baseUrl matches
  470. $this->_baseUrl = rtrim(dirname($baseUrl), '/');
  471. return $this;
  472. }
  473. $truncatedRequestUri = $requestUri;
  474. if (($pos = strpos($requestUri, '?')) !== false) {
  475. $truncatedRequestUri = substr($requestUri, 0, $pos);
  476. }
  477. $basename = basename($baseUrl);
  478. if (empty($basename) || !strpos($truncatedRequestUri, $basename)) {
  479. // no match whatsoever; set it blank
  480. $this->_baseUrl = '';
  481. return $this;
  482. }
  483. // If using mod_rewrite or ISAPI_Rewrite strip the script filename
  484. // out of baseUrl. $pos !== 0 makes sure it is not matching a value
  485. // from PATH_INFO or QUERY_STRING
  486. if ((strlen($requestUri) >= strlen($baseUrl))
  487. && ((false !== ($pos = strpos($requestUri, $baseUrl))) && ($pos !== 0)))
  488. {
  489. $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl));
  490. }
  491. }
  492. $this->_baseUrl = rtrim($baseUrl, '/');
  493. return $this;
  494. }
  495. /**
  496. * Everything in REQUEST_URI before PATH_INFO
  497. * <form action="<?=$baseUrl?>/news/submit" method="POST"/>
  498. *
  499. * @return string
  500. */
  501. public function getBaseUrl()
  502. {
  503. if (null === $this->_baseUrl) {
  504. $this->setBaseUrl();
  505. }
  506. return urldecode($this->_baseUrl);
  507. }
  508. /**
  509. * Set the base path for the URL
  510. *
  511. * @param string|null $basePath
  512. * @return Zend_Controller_Request_Http
  513. */
  514. public function setBasePath($basePath = null)
  515. {
  516. if ($basePath === null) {
  517. $filename = (isset($_SERVER['SCRIPT_FILENAME']))
  518. ? basename($_SERVER['SCRIPT_FILENAME'])
  519. : '';
  520. $baseUrl = $this->getBaseUrl();
  521. if (empty($baseUrl)) {
  522. $this->_basePath = '';
  523. return $this;
  524. }
  525. if (basename($baseUrl) === $filename) {
  526. $basePath = dirname($baseUrl);
  527. } else {
  528. $basePath = $baseUrl;
  529. }
  530. }
  531. if (substr(PHP_OS, 0, 3) === 'WIN') {
  532. $basePath = str_replace('\\', '/', $basePath);
  533. }
  534. $this->_basePath = rtrim($basePath, '/');
  535. return $this;
  536. }
  537. /**
  538. * Everything in REQUEST_URI before PATH_INFO not including the filename
  539. * <img src="<?=$basePath?>/images/zend.png"/>
  540. *
  541. * @return string
  542. */
  543. public function getBasePath()
  544. {
  545. if (null === $this->_basePath) {
  546. $this->setBasePath();
  547. }
  548. return $this->_basePath;
  549. }
  550. /**
  551. * Set the PATH_INFO string
  552. *
  553. * @param string|null $pathInfo
  554. * @return Zend_Controller_Request_Http
  555. */
  556. public function setPathInfo($pathInfo = null)
  557. {
  558. if ($pathInfo === null) {
  559. $baseUrl = $this->getBaseUrl();
  560. if (null === ($requestUri = $this->getRequestUri())) {
  561. return $this;
  562. }
  563. // Remove the query string from REQUEST_URI
  564. if ($pos = strpos($requestUri, '?')) {
  565. $requestUri = substr($requestUri, 0, $pos);
  566. }
  567. $requestUri = urldecode($requestUri);
  568. if (null !== $baseUrl
  569. && ((!empty($baseUrl) && 0 === strpos($requestUri, $baseUrl))
  570. || empty($baseUrl))
  571. && false === ($pathInfo = substr($requestUri, strlen($baseUrl)))
  572. ){
  573. // If substr() returns false then PATH_INFO is set to an empty string
  574. $pathInfo = '';
  575. } elseif (null === $baseUrl
  576. || (!empty($baseUrl) && false === strpos($requestUri, $baseUrl))
  577. ) {
  578. $pathInfo = $requestUri;
  579. }
  580. }
  581. $this->_pathInfo = (string) $pathInfo;
  582. return $this;
  583. }
  584. /**
  585. * Returns everything between the BaseUrl and QueryString.
  586. * This value is calculated instead of reading PATH_INFO
  587. * directly from $_SERVER due to cross-platform differences.
  588. *
  589. * @return string
  590. */
  591. public function getPathInfo()
  592. {
  593. if (empty($this->_pathInfo)) {
  594. $this->setPathInfo();
  595. }
  596. return $this->_pathInfo;
  597. }
  598. /**
  599. * Set allowed parameter sources
  600. *
  601. * Can be empty array, or contain one or more of '_GET' or '_POST'.
  602. *
  603. * @param array $paramSoures
  604. * @return Zend_Controller_Request_Http
  605. */
  606. public function setParamSources(array $paramSources = array())
  607. {
  608. $this->_paramSources = $paramSources;
  609. return $this;
  610. }
  611. /**
  612. * Get list of allowed parameter sources
  613. *
  614. * @return array
  615. */
  616. public function getParamSources()
  617. {
  618. return $this->_paramSources;
  619. }
  620. /**
  621. * Set a userland parameter
  622. *
  623. * Uses $key to set a userland parameter. If $key is an alias, the actual
  624. * key will be retrieved and used to set the parameter.
  625. *
  626. * @param mixed $key
  627. * @param mixed $value
  628. * @return Zend_Controller_Request_Http
  629. */
  630. public function setParam($key, $value)
  631. {
  632. $key = (null !== ($alias = $this->getAlias($key))) ? $alias : $key;
  633. parent::setParam($key, $value);
  634. return $this;
  635. }
  636. /**
  637. * Retrieve a parameter
  638. *
  639. * Retrieves a parameter from the instance. Priority is in the order of
  640. * userland parameters (see {@link setParam()}), $_GET, $_POST. If a
  641. * parameter matching the $key is not found, null is returned.
  642. *
  643. * If the $key is an alias, the actual key aliased will be used.
  644. *
  645. * @param mixed $key
  646. * @param mixed $default Default value to use if key not found
  647. * @return mixed
  648. */
  649. public function getParam($key, $default = null)
  650. {
  651. $keyName = (null !== ($alias = $this->getAlias($key))) ? $alias : $key;
  652. $paramSources = $this->getParamSources();
  653. if (isset($this->_params[$keyName])) {
  654. return $this->_params[$keyName];
  655. } elseif (in_array('_GET', $paramSources) && (isset($_GET[$keyName]))) {
  656. return $_GET[$keyName];
  657. } elseif (in_array('_POST', $paramSources) && (isset($_POST[$keyName]))) {
  658. return $_POST[$keyName];
  659. }
  660. return $default;
  661. }
  662. /**
  663. * Retrieve an array of parameters
  664. *
  665. * Retrieves a merged array of parameters, with precedence of userland
  666. * params (see {@link setParam()}), $_GET, $_POST (i.e., values in the
  667. * userland params will take precedence over all others).
  668. *
  669. * @return array
  670. */
  671. public function getParams()
  672. {
  673. $return = $this->_params;
  674. $paramSources = $this->getParamSources();
  675. if (in_array('_GET', $paramSources)
  676. && isset($_GET)
  677. && is_array($_GET)
  678. ) {
  679. $return += $_GET;
  680. }
  681. if (in_array('_POST', $paramSources)
  682. && isset($_POST)
  683. && is_array($_POST)
  684. ) {
  685. $return += $_POST;
  686. }
  687. return $return;
  688. }
  689. /**
  690. * Set parameters
  691. *
  692. * Set one or more parameters. Parameters are set as userland parameters,
  693. * using the keys specified in the array.
  694. *
  695. * @param array $params
  696. * @return Zend_Controller_Request_Http
  697. */
  698. public function setParams(array $params)
  699. {
  700. foreach ($params as $key => $value) {
  701. $this->setParam($key, $value);
  702. }
  703. return $this;
  704. }
  705. /**
  706. * Set a key alias
  707. *
  708. * Set an alias used for key lookups. $name specifies the alias, $target
  709. * specifies the actual key to use.
  710. *
  711. * @param string $name
  712. * @param string $target
  713. * @return Zend_Controller_Request_Http
  714. */
  715. public function setAlias($name, $target)
  716. {
  717. $this->_aliases[$name] = $target;
  718. return $this;
  719. }
  720. /**
  721. * Retrieve an alias
  722. *
  723. * Retrieve the actual key represented by the alias $name.
  724. *
  725. * @param string $name
  726. * @return string|null Returns null when no alias exists
  727. */
  728. public function getAlias($name)
  729. {
  730. if (isset($this->_aliases[$name])) {
  731. return $this->_aliases[$name];
  732. }
  733. return null;
  734. }
  735. /**
  736. * Retrieve the list of all aliases
  737. *
  738. * @return array
  739. */
  740. public function getAliases()
  741. {
  742. return $this->_aliases;
  743. }
  744. /**
  745. * Return the method by which the request was made
  746. *
  747. * @return string
  748. */
  749. public function getMethod()
  750. {
  751. return $this->getServer('REQUEST_METHOD');
  752. }
  753. /**
  754. * Was the request made by POST?
  755. *
  756. * @return boolean
  757. */
  758. public function isPost()
  759. {
  760. if ('POST' == $this->getMethod()) {
  761. return true;
  762. }
  763. return false;
  764. }
  765. /**
  766. * Was the request made by GET?
  767. *
  768. * @return boolean
  769. */
  770. public function isGet()
  771. {
  772. if ('GET' == $this->getMethod()) {
  773. return true;
  774. }
  775. return false;
  776. }
  777. /**
  778. * Was the request made by PUT?
  779. *
  780. * @return boolean
  781. */
  782. public function isPut()
  783. {
  784. if ('PUT' == $this->getMethod()) {
  785. return true;
  786. }
  787. return false;
  788. }
  789. /**
  790. * Was the request made by DELETE?
  791. *
  792. * @return boolean
  793. */
  794. public function isDelete()
  795. {
  796. if ('DELETE' == $this->getMethod()) {
  797. return true;
  798. }
  799. return false;
  800. }
  801. /**
  802. * Was the request made by HEAD?
  803. *
  804. * @return boolean
  805. */
  806. public function isHead()
  807. {
  808. if ('HEAD' == $this->getMethod()) {
  809. return true;
  810. }
  811. return false;
  812. }
  813. /**
  814. * Was the request made by OPTIONS?
  815. *
  816. * @return boolean
  817. */
  818. public function isOptions()
  819. {
  820. if ('OPTIONS' == $this->getMethod()) {
  821. return true;
  822. }
  823. return false;
  824. }
  825. /**
  826. * Is the request a Javascript XMLHttpRequest?
  827. *
  828. * Should work with Prototype/Script.aculo.us, possibly others.
  829. *
  830. * @return boolean
  831. */
  832. public function isXmlHttpRequest()
  833. {
  834. return ($this->getHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
  835. }
  836. /**
  837. * Is this a Flash request?
  838. *
  839. * @return boolean
  840. */
  841. public function isFlashRequest()
  842. {
  843. $header = strtolower($this->getHeader('USER_AGENT'));
  844. return (strstr($header, ' flash')) ? true : false;
  845. }
  846. /**
  847. * Is https secure request
  848. *
  849. * @return boolean
  850. */
  851. public function isSecure()
  852. {
  853. return ($this->getScheme() === self::SCHEME_HTTPS);
  854. }
  855. /**
  856. * Return the raw body of the request, if present
  857. *
  858. * @return string|false Raw body, or false if not present
  859. */
  860. public function getRawBody()
  861. {
  862. if (null === $this->_rawBody) {
  863. $body = file_get_contents('php://input');
  864. if (strlen(trim($body)) > 0) {
  865. $this->_rawBody = $body;
  866. } else {
  867. $this->_rawBody = false;
  868. }
  869. }
  870. return $this->_rawBody;
  871. }
  872. /**
  873. * Return the value of the given HTTP header. Pass the header name as the
  874. * plain, HTTP-specified header name. Ex.: Ask for 'Accept' to get the
  875. * Accept header, 'Accept-Encoding' to get the Accept-Encoding header.
  876. *
  877. * @param string $header HTTP header name
  878. * @return string|false HTTP header value, or false if not found
  879. * @throws Zend_Controller_Request_Exception
  880. */
  881. public function getHeader($header)
  882. {
  883. if (empty($header)) {
  884. #require_once 'Zend/Controller/Request/Exception.php';
  885. throw new Zend_Controller_Request_Exception('An HTTP header name is required');
  886. }
  887. // Try to get it from the $_SERVER array first
  888. $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
  889. if (isset($_SERVER[$temp])) {
  890. return $_SERVER[$temp];
  891. }
  892. // This seems to be the only way to get the Authorization header on
  893. // Apache
  894. if (function_exists('apache_request_headers')) {
  895. $headers = apache_request_headers();
  896. if (isset($headers[$header])) {
  897. return $headers[$header];
  898. }
  899. $header = strtolower($header);
  900. foreach ($headers as $key => $value) {
  901. if (strtolower($key) == $header) {
  902. return $value;
  903. }
  904. }
  905. }
  906. return false;
  907. }
  908. /**
  909. * Get the request URI scheme
  910. *
  911. * @return string
  912. */
  913. public function getScheme()
  914. {
  915. return ($this->getServer('HTTPS') == 'on') ? self::SCHEME_HTTPS : self::SCHEME_HTTP;
  916. }
  917. /**
  918. * Get the HTTP host.
  919. *
  920. * "Host" ":" host [ ":" port ] ; Section 3.2.2
  921. * Note the HTTP Host header is not the same as the URI host.
  922. * It includes the port while the URI host doesn't.
  923. *
  924. * @return string
  925. */
  926. public function getHttpHost()
  927. {
  928. $host = $this->getServer('HTTP_HOST');
  929. if (!empty($host)) {
  930. return $host;
  931. }
  932. $scheme = $this->getScheme();
  933. $name = $this->getServer('SERVER_NAME');
  934. $port = $this->getServer('SERVER_PORT');
  935. if(null === $name) {
  936. return '';
  937. }
  938. elseif (($scheme == self::SCHEME_HTTP && $port == 80) || ($scheme == self::SCHEME_HTTPS && $port == 443)) {
  939. return $name;
  940. } else {
  941. return $name . ':' . $port;
  942. }
  943. }
  944. /**
  945. * Get the client's IP addres
  946. *
  947. * @param boolean $checkProxy
  948. * @return string
  949. */
  950. public function getClientIp($checkProxy = true)
  951. {
  952. if ($checkProxy && $this->getServer('HTTP_CLIENT_IP') != null) {
  953. $ip = $this->getServer('HTTP_CLIENT_IP');
  954. } else if ($checkProxy && $this->getServer('HTTP_X_FORWARDED_FOR') != null) {
  955. $ip = $this->getServer('HTTP_X_FORWARDED_FOR');
  956. } else {
  957. $ip = $this->getServer('REMOTE_ADDR');
  958. }
  959. return $ip;
  960. }
  961. }