Http.php 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /** Zend_Controller_Request_Abstract */
  22. require_once 'Zend/Controller/Request/Abstract.php';
  23. /** 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 (isset($_SERVER['REQUEST_URI'])) {
  363. $requestUri = $_SERVER['REQUEST_URI'];
  364. // Http proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path
  365. $schemeAndHttpHost = $this->getScheme() . '://' . $this->getHttpHost();
  366. if (strpos($requestUri, $schemeAndHttpHost) === 0) {
  367. $requestUri = substr($requestUri, strlen($schemeAndHttpHost));
  368. }
  369. } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI
  370. $requestUri = $_SERVER['ORIG_PATH_INFO'];
  371. if (!empty($_SERVER['QUERY_STRING'])) {
  372. $requestUri .= '?' . $_SERVER['QUERY_STRING'];
  373. }
  374. } else {
  375. return $this;
  376. }
  377. } elseif (!is_string($requestUri)) {
  378. return $this;
  379. } else {
  380. // Set GET items, if available
  381. if (false !== ($pos = strpos($requestUri, '?'))) {
  382. // Get key => value pairs and set $_GET
  383. $query = substr($requestUri, $pos + 1);
  384. parse_str($query, $vars);
  385. $this->setQuery($vars);
  386. }
  387. }
  388. $this->_requestUri = $requestUri;
  389. return $this;
  390. }
  391. /**
  392. * Returns the REQUEST_URI taking into account
  393. * platform differences between Apache and IIS
  394. *
  395. * @return string
  396. */
  397. public function getRequestUri()
  398. {
  399. if (empty($this->_requestUri)) {
  400. $this->setRequestUri();
  401. }
  402. return $this->_requestUri;
  403. }
  404. /**
  405. * Set the base URL of the request; i.e., the segment leading to the script name
  406. *
  407. * E.g.:
  408. * - /admin
  409. * - /myapp
  410. * - /subdir/index.php
  411. *
  412. * Do not use the full URI when providing the base. The following are
  413. * examples of what not to use:
  414. * - http://example.com/admin (should be just /admin)
  415. * - http://example.com/subdir/index.php (should be just /subdir/index.php)
  416. *
  417. * If no $baseUrl is provided, attempts to determine the base URL from the
  418. * environment, using SCRIPT_FILENAME, SCRIPT_NAME, PHP_SELF, and
  419. * ORIG_SCRIPT_NAME in its determination.
  420. *
  421. * @param mixed $baseUrl
  422. * @return Zend_Controller_Request_Http
  423. */
  424. public function setBaseUrl($baseUrl = null)
  425. {
  426. if ((null !== $baseUrl) && !is_string($baseUrl)) {
  427. return $this;
  428. }
  429. if ($baseUrl === null) {
  430. $filename = (isset($_SERVER['SCRIPT_FILENAME'])) ? basename($_SERVER['SCRIPT_FILENAME']) : '';
  431. if (isset($_SERVER['SCRIPT_NAME']) && basename($_SERVER['SCRIPT_NAME']) === $filename) {
  432. $baseUrl = $_SERVER['SCRIPT_NAME'];
  433. } elseif (isset($_SERVER['PHP_SELF']) && basename($_SERVER['PHP_SELF']) === $filename) {
  434. $baseUrl = $_SERVER['PHP_SELF'];
  435. } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $filename) {
  436. $baseUrl = $_SERVER['ORIG_SCRIPT_NAME']; // 1and1 shared hosting compatibility
  437. } else {
  438. // Backtrack up the script_filename to find the portion matching
  439. // php_self
  440. $path = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : '';
  441. $file = isset($_SERVER['SCRIPT_FILENAME']) ? $_SERVER['SCRIPT_FILENAME'] : '';
  442. $segs = explode('/', trim($file, '/'));
  443. $segs = array_reverse($segs);
  444. $index = 0;
  445. $last = count($segs);
  446. $baseUrl = '';
  447. do {
  448. $seg = $segs[$index];
  449. $baseUrl = '/' . $seg . $baseUrl;
  450. ++$index;
  451. } while (($last > $index) && (false !== ($pos = strpos($path, $baseUrl))) && (0 != $pos));
  452. }
  453. // Does the baseUrl have anything in common with the request_uri?
  454. $requestUri = $this->getRequestUri();
  455. if (0 === strpos($requestUri, $baseUrl)) {
  456. // full $baseUrl matches
  457. $this->_baseUrl = $baseUrl;
  458. return $this;
  459. }
  460. if (0 === strpos($requestUri, dirname($baseUrl))) {
  461. // directory portion of $baseUrl matches
  462. $this->_baseUrl = rtrim(dirname($baseUrl), '/');
  463. return $this;
  464. }
  465. $basename = basename($baseUrl);
  466. if (empty($basename) || !strpos($requestUri, $basename)) {
  467. // no match whatsoever; set it blank
  468. $this->_baseUrl = '';
  469. return $this;
  470. }
  471. // If using mod_rewrite or ISAPI_Rewrite strip the script filename
  472. // out of baseUrl. $pos !== 0 makes sure it is not matching a value
  473. // from PATH_INFO or QUERY_STRING
  474. if ((strlen($requestUri) >= strlen($baseUrl))
  475. && ((false !== ($pos = strpos($requestUri, $baseUrl))) && ($pos !== 0)))
  476. {
  477. $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl));
  478. }
  479. }
  480. $this->_baseUrl = rtrim($baseUrl, '/');
  481. return $this;
  482. }
  483. /**
  484. * Everything in REQUEST_URI before PATH_INFO
  485. * <form action="<?=$baseUrl?>/news/submit" method="POST"/>
  486. *
  487. * @return string
  488. */
  489. public function getBaseUrl()
  490. {
  491. if (null === $this->_baseUrl) {
  492. $this->setBaseUrl();
  493. }
  494. return $this->_baseUrl;
  495. }
  496. /**
  497. * Set the base path for the URL
  498. *
  499. * @param string|null $basePath
  500. * @return Zend_Controller_Request_Http
  501. */
  502. public function setBasePath($basePath = null)
  503. {
  504. if ($basePath === null) {
  505. $filename = (isset($_SERVER['SCRIPT_FILENAME']))
  506. ? basename($_SERVER['SCRIPT_FILENAME'])
  507. : '';
  508. $baseUrl = $this->getBaseUrl();
  509. if (empty($baseUrl)) {
  510. $this->_basePath = '';
  511. return $this;
  512. }
  513. if (basename($baseUrl) === $filename) {
  514. $basePath = dirname($baseUrl);
  515. } else {
  516. $basePath = $baseUrl;
  517. }
  518. }
  519. if (substr(PHP_OS, 0, 3) === 'WIN') {
  520. $basePath = str_replace('\\', '/', $basePath);
  521. }
  522. $this->_basePath = rtrim($basePath, '/');
  523. return $this;
  524. }
  525. /**
  526. * Everything in REQUEST_URI before PATH_INFO not including the filename
  527. * <img src="<?=$basePath?>/images/zend.png"/>
  528. *
  529. * @return string
  530. */
  531. public function getBasePath()
  532. {
  533. if (null === $this->_basePath) {
  534. $this->setBasePath();
  535. }
  536. return $this->_basePath;
  537. }
  538. /**
  539. * Set the PATH_INFO string
  540. *
  541. * @param string|null $pathInfo
  542. * @return Zend_Controller_Request_Http
  543. */
  544. public function setPathInfo($pathInfo = null)
  545. {
  546. if ($pathInfo === null) {
  547. $baseUrl = $this->getBaseUrl();
  548. if (null === ($requestUri = $this->getRequestUri())) {
  549. return $this;
  550. }
  551. // Remove the query string from REQUEST_URI
  552. if ($pos = strpos($requestUri, '?')) {
  553. $requestUri = substr($requestUri, 0, $pos);
  554. }
  555. if ((null !== $baseUrl)
  556. && (false === ($pathInfo = substr($requestUri, strlen($baseUrl)))))
  557. {
  558. // If substr() returns false then PATH_INFO is set to an empty string
  559. $pathInfo = '';
  560. } elseif (null === $baseUrl) {
  561. $pathInfo = $requestUri;
  562. }
  563. }
  564. $this->_pathInfo = (string) $pathInfo;
  565. return $this;
  566. }
  567. /**
  568. * Returns everything between the BaseUrl and QueryString.
  569. * This value is calculated instead of reading PATH_INFO
  570. * directly from $_SERVER due to cross-platform differences.
  571. *
  572. * @return string
  573. */
  574. public function getPathInfo()
  575. {
  576. if (empty($this->_pathInfo)) {
  577. $this->setPathInfo();
  578. }
  579. return $this->_pathInfo;
  580. }
  581. /**
  582. * Set allowed parameter sources
  583. *
  584. * Can be empty array, or contain one or more of '_GET' or '_POST'.
  585. *
  586. * @param array $paramSoures
  587. * @return Zend_Controller_Request_Http
  588. */
  589. public function setParamSources(array $paramSources = array())
  590. {
  591. $this->_paramSources = $paramSources;
  592. return $this;
  593. }
  594. /**
  595. * Get list of allowed parameter sources
  596. *
  597. * @return array
  598. */
  599. public function getParamSources()
  600. {
  601. return $this->_paramSources;
  602. }
  603. /**
  604. * Set a userland parameter
  605. *
  606. * Uses $key to set a userland parameter. If $key is an alias, the actual
  607. * key will be retrieved and used to set the parameter.
  608. *
  609. * @param mixed $key
  610. * @param mixed $value
  611. * @return Zend_Controller_Request_Http
  612. */
  613. public function setParam($key, $value)
  614. {
  615. $key = (null !== ($alias = $this->getAlias($key))) ? $alias : $key;
  616. parent::setParam($key, $value);
  617. return $this;
  618. }
  619. /**
  620. * Retrieve a parameter
  621. *
  622. * Retrieves a parameter from the instance. Priority is in the order of
  623. * userland parameters (see {@link setParam()}), $_GET, $_POST. If a
  624. * parameter matching the $key is not found, null is returned.
  625. *
  626. * If the $key is an alias, the actual key aliased will be used.
  627. *
  628. * @param mixed $key
  629. * @param mixed $default Default value to use if key not found
  630. * @return mixed
  631. */
  632. public function getParam($key, $default = null)
  633. {
  634. $keyName = (null !== ($alias = $this->getAlias($key))) ? $alias : $key;
  635. $paramSources = $this->getParamSources();
  636. if (isset($this->_params[$keyName])) {
  637. return $this->_params[$keyName];
  638. } elseif (in_array('_GET', $paramSources) && (isset($_GET[$keyName]))) {
  639. return $_GET[$keyName];
  640. } elseif (in_array('_POST', $paramSources) && (isset($_POST[$keyName]))) {
  641. return $_POST[$keyName];
  642. }
  643. return $default;
  644. }
  645. /**
  646. * Retrieve an array of parameters
  647. *
  648. * Retrieves a merged array of parameters, with precedence of userland
  649. * params (see {@link setParam()}), $_GET, $POST (i.e., values in the
  650. * userland params will take precedence over all others).
  651. *
  652. * @return array
  653. */
  654. public function getParams()
  655. {
  656. $return = $this->_params;
  657. if (isset($_GET) && is_array($_GET)) {
  658. $return += $_GET;
  659. }
  660. if (isset($_POST) && is_array($_POST)) {
  661. $return += $_POST;
  662. }
  663. return $return;
  664. }
  665. /**
  666. * Set parameters
  667. *
  668. * Set one or more parameters. Parameters are set as userland parameters,
  669. * using the keys specified in the array.
  670. *
  671. * @param array $params
  672. * @return Zend_Controller_Request_Http
  673. */
  674. public function setParams(array $params)
  675. {
  676. foreach ($params as $key => $value) {
  677. $this->setParam($key, $value);
  678. }
  679. return $this;
  680. }
  681. /**
  682. * Set a key alias
  683. *
  684. * Set an alias used for key lookups. $name specifies the alias, $target
  685. * specifies the actual key to use.
  686. *
  687. * @param string $name
  688. * @param string $target
  689. * @return Zend_Controller_Request_Http
  690. */
  691. public function setAlias($name, $target)
  692. {
  693. $this->_aliases[$name] = $target;
  694. return $this;
  695. }
  696. /**
  697. * Retrieve an alias
  698. *
  699. * Retrieve the actual key represented by the alias $name.
  700. *
  701. * @param string $name
  702. * @return string|null Returns null when no alias exists
  703. */
  704. public function getAlias($name)
  705. {
  706. if (isset($this->_aliases[$name])) {
  707. return $this->_aliases[$name];
  708. }
  709. return null;
  710. }
  711. /**
  712. * Retrieve the list of all aliases
  713. *
  714. * @return array
  715. */
  716. public function getAliases()
  717. {
  718. return $this->_aliases;
  719. }
  720. /**
  721. * Return the method by which the request was made
  722. *
  723. * @return string
  724. */
  725. public function getMethod()
  726. {
  727. return $this->getServer('REQUEST_METHOD');
  728. }
  729. /**
  730. * Was the request made by POST?
  731. *
  732. * @return boolean
  733. */
  734. public function isPost()
  735. {
  736. if ('POST' == $this->getMethod()) {
  737. return true;
  738. }
  739. return false;
  740. }
  741. /**
  742. * Was the request made by GET?
  743. *
  744. * @return boolean
  745. */
  746. public function isGet()
  747. {
  748. if ('GET' == $this->getMethod()) {
  749. return true;
  750. }
  751. return false;
  752. }
  753. /**
  754. * Was the request made by PUT?
  755. *
  756. * @return boolean
  757. */
  758. public function isPut()
  759. {
  760. if ('PUT' == $this->getMethod()) {
  761. return true;
  762. }
  763. return false;
  764. }
  765. /**
  766. * Was the request made by DELETE?
  767. *
  768. * @return boolean
  769. */
  770. public function isDelete()
  771. {
  772. if ('DELETE' == $this->getMethod()) {
  773. return true;
  774. }
  775. return false;
  776. }
  777. /**
  778. * Was the request made by HEAD?
  779. *
  780. * @return boolean
  781. */
  782. public function isHead()
  783. {
  784. if ('HEAD' == $this->getMethod()) {
  785. return true;
  786. }
  787. return false;
  788. }
  789. /**
  790. * Was the request made by OPTIONS?
  791. *
  792. * @return boolean
  793. */
  794. public function isOptions()
  795. {
  796. if ('OPTIONS' == $this->getMethod()) {
  797. return true;
  798. }
  799. return false;
  800. }
  801. /**
  802. * Is the request a Javascript XMLHttpRequest?
  803. *
  804. * Should work with Prototype/Script.aculo.us, possibly others.
  805. *
  806. * @return boolean
  807. */
  808. public function isXmlHttpRequest()
  809. {
  810. return ($this->getHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
  811. }
  812. /**
  813. * Is this a Flash request?
  814. *
  815. * @return bool
  816. */
  817. public function isFlashRequest()
  818. {
  819. $header = strtolower($this->getHeader('USER_AGENT'));
  820. return (strstr($header, ' flash')) ? true : false;
  821. }
  822. /**
  823. * Is https secure request
  824. *
  825. * @return boolean
  826. */
  827. public function isSecure()
  828. {
  829. return ($this->getScheme() === self::SCHEME_HTTPS);
  830. }
  831. /**
  832. * Return the raw body of the request, if present
  833. *
  834. * @return string|false Raw body, or false if not present
  835. */
  836. public function getRawBody()
  837. {
  838. if (null === $this->_rawBody) {
  839. $body = file_get_contents('php://input');
  840. if (strlen(trim($body)) > 0) {
  841. $this->_rawBody = $body;
  842. } else {
  843. $this->_rawBody = false;
  844. }
  845. }
  846. return $this->_rawBody;
  847. }
  848. /**
  849. * Return the value of the given HTTP header. Pass the header name as the
  850. * plain, HTTP-specified header name. Ex.: Ask for 'Accept' to get the
  851. * Accept header, 'Accept-Encoding' to get the Accept-Encoding header.
  852. *
  853. * @param string $header HTTP header name
  854. * @return string|false HTTP header value, or false if not found
  855. * @throws Zend_Controller_Request_Exception
  856. */
  857. public function getHeader($header)
  858. {
  859. if (empty($header)) {
  860. require_once 'Zend/Controller/Request/Exception.php';
  861. throw new Zend_Controller_Request_Exception('An HTTP header name is required');
  862. }
  863. // Try to get it from the $_SERVER array first
  864. $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
  865. if (!empty($_SERVER[$temp])) {
  866. return $_SERVER[$temp];
  867. }
  868. // This seems to be the only way to get the Authorization header on
  869. // Apache
  870. if (function_exists('apache_request_headers')) {
  871. $headers = apache_request_headers();
  872. if (!empty($headers[$header])) {
  873. return $headers[$header];
  874. }
  875. }
  876. return false;
  877. }
  878. /**
  879. * Get the request URI scheme
  880. *
  881. * @return string
  882. */
  883. public function getScheme()
  884. {
  885. return ($this->getServer('HTTPS') == 'on') ? self::SCHEME_HTTPS : self::SCHEME_HTTP;
  886. }
  887. /**
  888. * Get the HTTP host.
  889. *
  890. * "Host" ":" host [ ":" port ] ; Section 3.2.2
  891. * Note the HTTP Host header is not the same as the URI host.
  892. * It includes the port while the URI host doesn't.
  893. *
  894. * @return string
  895. */
  896. public function getHttpHost()
  897. {
  898. $host = $this->getServer('HTTP_HOST');
  899. if (!empty($host)) {
  900. return $host;
  901. }
  902. $scheme = $this->getScheme();
  903. $name = $this->getServer('SERVER_NAME');
  904. $port = $this->getServer('SERVER_PORT');
  905. if (($scheme == self::SCHEME_HTTP && $port == 80) || ($scheme == self::SCHEME_HTTPS && $port == 443)) {
  906. return $name;
  907. } else {
  908. return $name . ':' . $port;
  909. }
  910. }
  911. /**
  912. * Get the client's IP addres
  913. *
  914. * @return string
  915. */
  916. public function getClientIp($checkProxy = true)
  917. {
  918. if ($checkProxy && $this->getServer('HTTP_CLIENT_IP') != null) {
  919. $ip = $this->getServer('HTTP_CLIENT_IP');
  920. } else if ($checkProxy && $this->getServer('HTTP_X_FORWARDED_FOR') != null) {
  921. $ip = $this->getServer('HTTP_X_FORWARDED_FOR');
  922. } else {
  923. $ip = $this->getServer('REMOTE_ADDR');
  924. }
  925. return $ip;
  926. }
  927. }