Http.php 30 KB

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