Http.php 27 KB

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