PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/src/application/libraries/Zend/Controller/Request/Http.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 1066 lines | 724 code | 63 blank | 279 comment | 90 complexity | 6ebe9b51ebed66727613e56915298d97 MD5 | raw file
  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-2011 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 24008 2011-05-04 18:11:15Z ralph $
  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($raw = false)
  502. {
  503. if (null === $this->_baseUrl) {
  504. $this->setBaseUrl();
  505. }
  506. return (($raw == false) ? urldecode($this->_baseUrl) : $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(); // this actually calls setBaseUrl() & setRequestUri()
  560. $baseUrlRaw = $this->getBaseUrl(false);
  561. $baseUrlEncoded = urlencode($baseUrlRaw);
  562. if (null === ($requestUri = $this->getRequestUri())) {
  563. return $this;
  564. }
  565. // Remove the query string from REQUEST_URI
  566. if ($pos = strpos($requestUri, '?')) {
  567. $requestUri = substr($requestUri, 0, $pos);
  568. }
  569. if (!empty($baseUrl) || !empty($baseUrlRaw)) {
  570. if (strpos($requestUri, $baseUrl) === 0) {
  571. $pathInfo = substr($requestUri, strlen($baseUrl));
  572. } elseif (strpos($requestUri, $baseUrlRaw) === 0) {
  573. $pathInfo = substr($requestUri, strlen($baseUrlRaw));
  574. } elseif (strpos($requestUri, $baseUrlEncoded) === 0) {
  575. $pathInfo = substr($requestUri, strlen($baseUrlEncoded));
  576. } else {
  577. $pathInfo = $requestUri;
  578. }
  579. } else {
  580. $pathInfo = $requestUri;
  581. }
  582. }
  583. $this->_pathInfo = (string) $pathInfo;
  584. return $this;
  585. }
  586. /**
  587. * Returns everything between the BaseUrl and QueryString.
  588. * This value is calculated instead of reading PATH_INFO
  589. * directly from $_SERVER due to cross-platform differences.
  590. *
  591. * @return string
  592. */
  593. public function getPathInfo()
  594. {
  595. if (empty($this->_pathInfo)) {
  596. $this->setPathInfo();
  597. }
  598. return $this->_pathInfo;
  599. }
  600. /**
  601. * Set allowed parameter sources
  602. *
  603. * Can be empty array, or contain one or more of '_GET' or '_POST'.
  604. *
  605. * @param array $paramSoures
  606. * @return Zend_Controller_Request_Http
  607. */
  608. public function setParamSources(array $paramSources = array())
  609. {
  610. $this->_paramSources = $paramSources;
  611. return $this;
  612. }
  613. /**
  614. * Get list of allowed parameter sources
  615. *
  616. * @return array
  617. */
  618. public function getParamSources()
  619. {
  620. return $this->_paramSources;
  621. }
  622. /**
  623. * Set a userland parameter
  624. *
  625. * Uses $key to set a userland parameter. If $key is an alias, the actual
  626. * key will be retrieved and used to set the parameter.
  627. *
  628. * @param mixed $key
  629. * @param mixed $value
  630. * @return Zend_Controller_Request_Http
  631. */
  632. public function setParam($key, $value)
  633. {
  634. $key = (null !== ($alias = $this->getAlias($key))) ? $alias : $key;
  635. parent::setParam($key, $value);
  636. return $this;
  637. }
  638. /**
  639. * Retrieve a parameter
  640. *
  641. * Retrieves a parameter from the instance. Priority is in the order of
  642. * userland parameters (see {@link setParam()}), $_GET, $_POST. If a
  643. * parameter matching the $key is not found, null is returned.
  644. *
  645. * If the $key is an alias, the actual key aliased will be used.
  646. *
  647. * @param mixed $key
  648. * @param mixed $default Default value to use if key not found
  649. * @return mixed
  650. */
  651. public function getParam($key, $default = null)
  652. {
  653. $keyName = (null !== ($alias = $this->getAlias($key))) ? $alias : $key;
  654. $paramSources = $this->getParamSources();
  655. if (isset($this->_params[$keyName])) {
  656. return $this->_params[$keyName];
  657. } elseif (in_array('_GET', $paramSources) && (isset($_GET[$keyName]))) {
  658. return $_GET[$keyName];
  659. } elseif (in_array('_POST', $paramSources) && (isset($_POST[$keyName]))) {
  660. return $_POST[$keyName];
  661. }
  662. return $default;
  663. }
  664. /**
  665. * Retrieve an array of parameters
  666. *
  667. * Retrieves a merged array of parameters, with precedence of userland
  668. * params (see {@link setParam()}), $_GET, $_POST (i.e., values in the
  669. * userland params will take precedence over all others).
  670. *
  671. * @return array
  672. */
  673. public function getParams()
  674. {
  675. $return = $this->_params;
  676. $paramSources = $this->getParamSources();
  677. if (in_array('_GET', $paramSources)
  678. && isset($_GET)
  679. && is_array($_GET)
  680. ) {
  681. $return += $_GET;
  682. }
  683. if (in_array('_POST', $paramSources)
  684. && isset($_POST)
  685. && is_array($_POST)
  686. ) {
  687. $return += $_POST;
  688. }
  689. return $return;
  690. }
  691. /**
  692. * Set parameters
  693. *
  694. * Set one or more parameters. Parameters are set as userland parameters,
  695. * using the keys specified in the array.
  696. *
  697. * @param array $params
  698. * @return Zend_Controller_Request_Http
  699. */
  700. public function setParams(array $params)
  701. {
  702. foreach ($params as $key => $value) {
  703. $this->setParam($key, $value);
  704. }
  705. return $this;
  706. }
  707. /**
  708. * Set a key alias
  709. *
  710. * Set an alias used for key lookups. $name specifies the alias, $target
  711. * specifies the actual key to use.
  712. *
  713. * @param string $name
  714. * @param string $target
  715. * @return Zend_Controller_Request_Http
  716. */
  717. public function setAlias($name, $target)
  718. {
  719. $this->_aliases[$name] = $target;
  720. return $this;
  721. }
  722. /**
  723. * Retrieve an alias
  724. *
  725. * Retrieve the actual key represented by the alias $name.
  726. *
  727. * @param string $name
  728. * @return string|null Returns null when no alias exists
  729. */
  730. public function getAlias($name)
  731. {
  732. if (isset($this->_aliases[$name])) {
  733. return $this->_aliases[$name];
  734. }
  735. return null;
  736. }
  737. /**
  738. * Retrieve the list of all aliases
  739. *
  740. * @return array
  741. */
  742. public function getAliases()
  743. {
  744. return $this->_aliases;
  745. }
  746. /**
  747. * Return the method by which the request was made
  748. *
  749. * @return string
  750. */
  751. public function getMethod()
  752. {
  753. return $this->getServer('REQUEST_METHOD');
  754. }
  755. /**
  756. * Was the request made by POST?
  757. *
  758. * @return boolean
  759. */
  760. public function isPost()
  761. {
  762. if ('POST' == $this->getMethod()) {
  763. return true;
  764. }
  765. return false;
  766. }
  767. /**
  768. * Was the request made by GET?
  769. *
  770. * @return boolean
  771. */
  772. public function isGet()
  773. {
  774. if ('GET' == $this->getMethod()) {
  775. return true;
  776. }
  777. return false;
  778. }
  779. /**
  780. * Was the request made by PUT?
  781. *
  782. * @return boolean
  783. */
  784. public function isPut()
  785. {
  786. if ('PUT' == $this->getMethod()) {
  787. return true;
  788. }
  789. return false;
  790. }
  791. /**
  792. * Was the request made by DELETE?
  793. *
  794. * @return boolean
  795. */
  796. public function isDelete()
  797. {
  798. if ('DELETE' == $this->getMethod()) {
  799. return true;
  800. }
  801. return false;
  802. }
  803. /**
  804. * Was the request made by HEAD?
  805. *
  806. * @return boolean
  807. */
  808. public function isHead()
  809. {
  810. if ('HEAD' == $this->getMethod()) {
  811. return true;
  812. }
  813. return false;
  814. }
  815. /**
  816. * Was the request made by OPTIONS?
  817. *
  818. * @return boolean
  819. */
  820. public function isOptions()
  821. {
  822. if ('OPTIONS' == $this->getMethod()) {
  823. return true;
  824. }
  825. return false;
  826. }
  827. /**
  828. * Is the request a Javascript XMLHttpRequest?
  829. *
  830. * Should work with Prototype/Script.aculo.us, possibly others.
  831. *
  832. * @return boolean
  833. */
  834. public function isXmlHttpRequest()
  835. {
  836. return ($this->getHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
  837. }
  838. /**
  839. * Is this a Flash request?
  840. *
  841. * @return boolean
  842. */
  843. public function isFlashRequest()
  844. {
  845. $header = strtolower($this->getHeader('USER_AGENT'));
  846. return (strstr($header, ' flash')) ? true : false;
  847. }
  848. /**
  849. * Is https secure request
  850. *
  851. * @return boolean
  852. */
  853. public function isSecure()
  854. {
  855. return ($this->getScheme() === self::SCHEME_HTTPS);
  856. }
  857. /**
  858. * Return the raw body of the request, if present
  859. *
  860. * @return string|false Raw body, or false if not present
  861. */
  862. public function getRawBody()
  863. {
  864. if (null === $this->_rawBody) {
  865. $body = file_get_contents('php://input');
  866. if (strlen(trim($body)) > 0) {
  867. $this->_rawBody = $body;
  868. } else {
  869. $this->_rawBody = false;
  870. }
  871. }
  872. return $this->_rawBody;
  873. }
  874. /**
  875. * Return the value of the given HTTP header. Pass the header name as the
  876. * plain, HTTP-specified header name. Ex.: Ask for 'Accept' to get the
  877. * Accept header, 'Accept-Encoding' to get the Accept-Encoding header.
  878. *
  879. * @param string $header HTTP header name
  880. * @return string|false HTTP header value, or false if not found
  881. * @throws Zend_Controller_Request_Exception
  882. */
  883. public function getHeader($header)
  884. {
  885. if (empty($header)) {
  886. require_once 'Zend/Controller/Request/Exception.php';
  887. throw new Zend_Controller_Request_Exception('An HTTP header name is required');
  888. }
  889. // Try to get it from the $_SERVER array first
  890. $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
  891. if (isset($_SERVER[$temp])) {
  892. return $_SERVER[$temp];
  893. }
  894. // This seems to be the only way to get the Authorization header on
  895. // Apache
  896. if (function_exists('apache_request_headers')) {
  897. $headers = apache_request_headers();
  898. if (isset($headers[$header])) {
  899. return $headers[$header];
  900. }
  901. $header = strtolower($header);
  902. foreach ($headers as $key => $value) {
  903. if (strtolower($key) == $header) {
  904. return $value;
  905. }
  906. }
  907. }
  908. return false;
  909. }
  910. /**
  911. * Get the request URI scheme
  912. *
  913. * @return string
  914. */
  915. public function getScheme()
  916. {
  917. return ($this->getServer('HTTPS') == 'on') ? self::SCHEME_HTTPS : self::SCHEME_HTTP;
  918. }
  919. /**
  920. * Get the HTTP host.
  921. *
  922. * "Host" ":" host [ ":" port ] ; Section 3.2.2
  923. * Note the HTTP Host header is not the same as the URI host.
  924. * It includes the port while the URI host doesn't.
  925. *
  926. * @return string
  927. */
  928. public function getHttpHost()
  929. {
  930. $host = $this->getServer('HTTP_HOST');
  931. if (!empty($host)) {
  932. return $host;
  933. }
  934. $scheme = $this->getScheme();
  935. $name = $this->getServer('SERVER_NAME');
  936. $port = $this->getServer('SERVER_PORT');
  937. if(null === $name) {
  938. return '';
  939. }
  940. elseif (($scheme == self::SCHEME_HTTP && $port == 80) || ($scheme == self::SCHEME_HTTPS && $port == 443)) {
  941. return $name;
  942. } else {
  943. return $name . ':' . $port;
  944. }
  945. }
  946. /**
  947. * Get the client's IP addres
  948. *
  949. * @param boolean $checkProxy
  950. * @return string
  951. */
  952. public function getClientIp($checkProxy = true)
  953. {
  954. if ($checkProxy && $this->getServer('HTTP_CLIENT_IP') != null) {
  955. $ip = $this->getServer('HTTP_CLIENT_IP');
  956. } else if ($checkProxy && $this->getServer('HTTP_X_FORWARDED_FOR') != null) {
  957. $ip = $this->getServer('HTTP_X_FORWARDED_FOR');
  958. } else {
  959. $ip = $this->getServer('REMOTE_ADDR');
  960. }
  961. return $ip;
  962. }
  963. }