Http.php 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. $truncatedRequestUri = $requestUri;
  466. if (($pos = strpos($requestUri, '?')) !== false) {
  467. $truncatedRequestUri = substr($requestUri, 0, $pos);
  468. }
  469. $basename = basename($baseUrl);
  470. if (empty($basename) || !strpos($truncatedRequestUri, $basename)) {
  471. // no match whatsoever; set it blank
  472. $this->_baseUrl = '';
  473. return $this;
  474. }
  475. // If using mod_rewrite or ISAPI_Rewrite strip the script filename
  476. // out of baseUrl. $pos !== 0 makes sure it is not matching a value
  477. // from PATH_INFO or QUERY_STRING
  478. if ((strlen($requestUri) >= strlen($baseUrl))
  479. && ((false !== ($pos = strpos($requestUri, $baseUrl))) && ($pos !== 0)))
  480. {
  481. $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl));
  482. }
  483. }
  484. $this->_baseUrl = rtrim($baseUrl, '/');
  485. return $this;
  486. }
  487. /**
  488. * Everything in REQUEST_URI before PATH_INFO
  489. * <form action="<?=$baseUrl?>/news/submit" method="POST"/>
  490. *
  491. * @return string
  492. */
  493. public function getBaseUrl()
  494. {
  495. if (null === $this->_baseUrl) {
  496. $this->setBaseUrl();
  497. }
  498. return $this->_baseUrl;
  499. }
  500. /**
  501. * Set the base path for the URL
  502. *
  503. * @param string|null $basePath
  504. * @return Zend_Controller_Request_Http
  505. */
  506. public function setBasePath($basePath = null)
  507. {
  508. if ($basePath === null) {
  509. $filename = (isset($_SERVER['SCRIPT_FILENAME']))
  510. ? basename($_SERVER['SCRIPT_FILENAME'])
  511. : '';
  512. $baseUrl = $this->getBaseUrl();
  513. if (empty($baseUrl)) {
  514. $this->_basePath = '';
  515. return $this;
  516. }
  517. if (basename($baseUrl) === $filename) {
  518. $basePath = dirname($baseUrl);
  519. } else {
  520. $basePath = $baseUrl;
  521. }
  522. }
  523. if (substr(PHP_OS, 0, 3) === 'WIN') {
  524. $basePath = str_replace('\\', '/', $basePath);
  525. }
  526. $this->_basePath = rtrim($basePath, '/');
  527. return $this;
  528. }
  529. /**
  530. * Everything in REQUEST_URI before PATH_INFO not including the filename
  531. * <img src="<?=$basePath?>/images/zend.png"/>
  532. *
  533. * @return string
  534. */
  535. public function getBasePath()
  536. {
  537. if (null === $this->_basePath) {
  538. $this->setBasePath();
  539. }
  540. return $this->_basePath;
  541. }
  542. /**
  543. * Set the PATH_INFO string
  544. *
  545. * @param string|null $pathInfo
  546. * @return Zend_Controller_Request_Http
  547. */
  548. public function setPathInfo($pathInfo = null)
  549. {
  550. if ($pathInfo === null) {
  551. $baseUrl = $this->getBaseUrl();
  552. if (null === ($requestUri = $this->getRequestUri())) {
  553. return $this;
  554. }
  555. // Remove the query string from REQUEST_URI
  556. if ($pos = strpos($requestUri, '?')) {
  557. $requestUri = substr($requestUri, 0, $pos);
  558. }
  559. if ((null !== $baseUrl)
  560. && (false === ($pathInfo = substr($requestUri, strlen($baseUrl)))))
  561. {
  562. // If substr() returns false then PATH_INFO is set to an empty string
  563. $pathInfo = '';
  564. } elseif (null === $baseUrl) {
  565. $pathInfo = $requestUri;
  566. }
  567. }
  568. $this->_pathInfo = (string) $pathInfo;
  569. return $this;
  570. }
  571. /**
  572. * Returns everything between the BaseUrl and QueryString.
  573. * This value is calculated instead of reading PATH_INFO
  574. * directly from $_SERVER due to cross-platform differences.
  575. *
  576. * @return string
  577. */
  578. public function getPathInfo()
  579. {
  580. if (empty($this->_pathInfo)) {
  581. $this->setPathInfo();
  582. }
  583. return $this->_pathInfo;
  584. }
  585. /**
  586. * Set allowed parameter sources
  587. *
  588. * Can be empty array, or contain one or more of '_GET' or '_POST'.
  589. *
  590. * @param array $paramSoures
  591. * @return Zend_Controller_Request_Http
  592. */
  593. public function setParamSources(array $paramSources = array())
  594. {
  595. $this->_paramSources = $paramSources;
  596. return $this;
  597. }
  598. /**
  599. * Get list of allowed parameter sources
  600. *
  601. * @return array
  602. */
  603. public function getParamSources()
  604. {
  605. return $this->_paramSources;
  606. }
  607. /**
  608. * Set a userland parameter
  609. *
  610. * Uses $key to set a userland parameter. If $key is an alias, the actual
  611. * key will be retrieved and used to set the parameter.
  612. *
  613. * @param mixed $key
  614. * @param mixed $value
  615. * @return Zend_Controller_Request_Http
  616. */
  617. public function setParam($key, $value)
  618. {
  619. $key = (null !== ($alias = $this->getAlias($key))) ? $alias : $key;
  620. parent::setParam($key, $value);
  621. return $this;
  622. }
  623. /**
  624. * Retrieve a parameter
  625. *
  626. * Retrieves a parameter from the instance. Priority is in the order of
  627. * userland parameters (see {@link setParam()}), $_GET, $_POST. If a
  628. * parameter matching the $key is not found, null is returned.
  629. *
  630. * If the $key is an alias, the actual key aliased will be used.
  631. *
  632. * @param mixed $key
  633. * @param mixed $default Default value to use if key not found
  634. * @return mixed
  635. */
  636. public function getParam($key, $default = null)
  637. {
  638. $keyName = (null !== ($alias = $this->getAlias($key))) ? $alias : $key;
  639. $paramSources = $this->getParamSources();
  640. if (isset($this->_params[$keyName])) {
  641. return $this->_params[$keyName];
  642. } elseif (in_array('_GET', $paramSources) && (isset($_GET[$keyName]))) {
  643. return $_GET[$keyName];
  644. } elseif (in_array('_POST', $paramSources) && (isset($_POST[$keyName]))) {
  645. return $_POST[$keyName];
  646. }
  647. return $default;
  648. }
  649. /**
  650. * Retrieve an array of parameters
  651. *
  652. * Retrieves a merged array of parameters, with precedence of userland
  653. * params (see {@link setParam()}), $_GET, $POST (i.e., values in the
  654. * userland params will take precedence over all others).
  655. *
  656. * @return array
  657. */
  658. public function getParams()
  659. {
  660. $return = $this->_params;
  661. $paramSources = $this->getParamSources();
  662. if (in_array('_GET', $paramSources)
  663. && isset($_GET)
  664. && is_array($_GET)
  665. ) {
  666. $return += $_GET;
  667. }
  668. if (in_array('_POST', $paramSources)
  669. && isset($_POST)
  670. && is_array($_POST)
  671. ) {
  672. $return += $_POST;
  673. }
  674. return $return;
  675. }
  676. /**
  677. * Set parameters
  678. *
  679. * Set one or more parameters. Parameters are set as userland parameters,
  680. * using the keys specified in the array.
  681. *
  682. * @param array $params
  683. * @return Zend_Controller_Request_Http
  684. */
  685. public function setParams(array $params)
  686. {
  687. foreach ($params as $key => $value) {
  688. $this->setParam($key, $value);
  689. }
  690. return $this;
  691. }
  692. /**
  693. * Set a key alias
  694. *
  695. * Set an alias used for key lookups. $name specifies the alias, $target
  696. * specifies the actual key to use.
  697. *
  698. * @param string $name
  699. * @param string $target
  700. * @return Zend_Controller_Request_Http
  701. */
  702. public function setAlias($name, $target)
  703. {
  704. $this->_aliases[$name] = $target;
  705. return $this;
  706. }
  707. /**
  708. * Retrieve an alias
  709. *
  710. * Retrieve the actual key represented by the alias $name.
  711. *
  712. * @param string $name
  713. * @return string|null Returns null when no alias exists
  714. */
  715. public function getAlias($name)
  716. {
  717. if (isset($this->_aliases[$name])) {
  718. return $this->_aliases[$name];
  719. }
  720. return null;
  721. }
  722. /**
  723. * Retrieve the list of all aliases
  724. *
  725. * @return array
  726. */
  727. public function getAliases()
  728. {
  729. return $this->_aliases;
  730. }
  731. /**
  732. * Return the method by which the request was made
  733. *
  734. * @return string
  735. */
  736. public function getMethod()
  737. {
  738. return $this->getServer('REQUEST_METHOD');
  739. }
  740. /**
  741. * Was the request made by POST?
  742. *
  743. * @return boolean
  744. */
  745. public function isPost()
  746. {
  747. if ('POST' == $this->getMethod()) {
  748. return true;
  749. }
  750. return false;
  751. }
  752. /**
  753. * Was the request made by GET?
  754. *
  755. * @return boolean
  756. */
  757. public function isGet()
  758. {
  759. if ('GET' == $this->getMethod()) {
  760. return true;
  761. }
  762. return false;
  763. }
  764. /**
  765. * Was the request made by PUT?
  766. *
  767. * @return boolean
  768. */
  769. public function isPut()
  770. {
  771. if ('PUT' == $this->getMethod()) {
  772. return true;
  773. }
  774. return false;
  775. }
  776. /**
  777. * Was the request made by DELETE?
  778. *
  779. * @return boolean
  780. */
  781. public function isDelete()
  782. {
  783. if ('DELETE' == $this->getMethod()) {
  784. return true;
  785. }
  786. return false;
  787. }
  788. /**
  789. * Was the request made by HEAD?
  790. *
  791. * @return boolean
  792. */
  793. public function isHead()
  794. {
  795. if ('HEAD' == $this->getMethod()) {
  796. return true;
  797. }
  798. return false;
  799. }
  800. /**
  801. * Was the request made by OPTIONS?
  802. *
  803. * @return boolean
  804. */
  805. public function isOptions()
  806. {
  807. if ('OPTIONS' == $this->getMethod()) {
  808. return true;
  809. }
  810. return false;
  811. }
  812. /**
  813. * Is the request a Javascript XMLHttpRequest?
  814. *
  815. * Should work with Prototype/Script.aculo.us, possibly others.
  816. *
  817. * @return boolean
  818. */
  819. public function isXmlHttpRequest()
  820. {
  821. return ($this->getHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
  822. }
  823. /**
  824. * Is this a Flash request?
  825. *
  826. * @return bool
  827. */
  828. public function isFlashRequest()
  829. {
  830. $header = strtolower($this->getHeader('USER_AGENT'));
  831. return (strstr($header, ' flash')) ? true : false;
  832. }
  833. /**
  834. * Is https secure request
  835. *
  836. * @return boolean
  837. */
  838. public function isSecure()
  839. {
  840. return ($this->getScheme() === self::SCHEME_HTTPS);
  841. }
  842. /**
  843. * Return the raw body of the request, if present
  844. *
  845. * @return string|false Raw body, or false if not present
  846. */
  847. public function getRawBody()
  848. {
  849. if (null === $this->_rawBody) {
  850. $body = file_get_contents('php://input');
  851. if (strlen(trim($body)) > 0) {
  852. $this->_rawBody = $body;
  853. } else {
  854. $this->_rawBody = false;
  855. }
  856. }
  857. return $this->_rawBody;
  858. }
  859. /**
  860. * Return the value of the given HTTP header. Pass the header name as the
  861. * plain, HTTP-specified header name. Ex.: Ask for 'Accept' to get the
  862. * Accept header, 'Accept-Encoding' to get the Accept-Encoding header.
  863. *
  864. * @param string $header HTTP header name
  865. * @return string|false HTTP header value, or false if not found
  866. * @throws Zend_Controller_Request_Exception
  867. */
  868. public function getHeader($header)
  869. {
  870. if (empty($header)) {
  871. require_once 'Zend/Controller/Request/Exception.php';
  872. throw new Zend_Controller_Request_Exception('An HTTP header name is required');
  873. }
  874. // Try to get it from the $_SERVER array first
  875. $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
  876. if (!empty($_SERVER[$temp])) {
  877. return $_SERVER[$temp];
  878. }
  879. // This seems to be the only way to get the Authorization header on
  880. // Apache
  881. if (function_exists('apache_request_headers')) {
  882. $headers = apache_request_headers();
  883. if (!empty($headers[$header])) {
  884. return $headers[$header];
  885. }
  886. }
  887. return false;
  888. }
  889. /**
  890. * Get the request URI scheme
  891. *
  892. * @return string
  893. */
  894. public function getScheme()
  895. {
  896. return ($this->getServer('HTTPS') == 'on') ? self::SCHEME_HTTPS : self::SCHEME_HTTP;
  897. }
  898. /**
  899. * Get the HTTP host.
  900. *
  901. * "Host" ":" host [ ":" port ] ; Section 3.2.2
  902. * Note the HTTP Host header is not the same as the URI host.
  903. * It includes the port while the URI host doesn't.
  904. *
  905. * @return string
  906. */
  907. public function getHttpHost()
  908. {
  909. $host = $this->getServer('HTTP_HOST');
  910. if (!empty($host)) {
  911. return $host;
  912. }
  913. $scheme = $this->getScheme();
  914. $name = $this->getServer('SERVER_NAME');
  915. $port = $this->getServer('SERVER_PORT');
  916. if (($scheme == self::SCHEME_HTTP && $port == 80) || ($scheme == self::SCHEME_HTTPS && $port == 443)) {
  917. return $name;
  918. } else {
  919. return $name . ':' . $port;
  920. }
  921. }
  922. /**
  923. * Get the client's IP addres
  924. *
  925. * @return string
  926. */
  927. public function getClientIp($checkProxy = true)
  928. {
  929. if ($checkProxy && $this->getServer('HTTP_CLIENT_IP') != null) {
  930. $ip = $this->getServer('HTTP_CLIENT_IP');
  931. } else if ($checkProxy && $this->getServer('HTTP_X_FORWARDED_FOR') != null) {
  932. $ip = $this->getServer('HTTP_X_FORWARDED_FOR');
  933. } else {
  934. $ip = $this->getServer('REMOTE_ADDR');
  935. }
  936. return $ip;
  937. }
  938. }