PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/fo/classes/HttpRequest.class.php

https://github.com/openstate/HNS.dev
PHP | 281 lines | 216 code | 49 blank | 16 comment | 48 complexity | ef0b9211ebc74ce5564887c4e68266d7 MD5 | raw file
  1. <?php
  2. require_once 'Request.abstract.php';
  3. require_once 'Destination.class.php';
  4. class HttpRequest extends Request {
  5. protected $requestUri;
  6. protected $pathInfo;
  7. protected $baseUrl;
  8. protected $documentRoot;
  9. protected $getVariables = array();
  10. protected $postVariables = array();
  11. protected $filesVariables = array();
  12. public function __construct($site, $requestUri = null) {
  13. $this->setRequestUri($requestUri);
  14. if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
  15. array_walk_recursive($_REQUEST, array($this, 'undoQuotes'));
  16. array_walk_recursive($_GET, array($this, 'undoQuotes'));
  17. array_walk_recursive($_POST, array($this, 'undoQuotes'));
  18. array_walk_recursive($_COOKIE, array($this, 'undoQuotes'));
  19. }
  20. $this->getVariables = $_GET;
  21. $_GET = array();
  22. $this->postVariables = $_POST;
  23. $_POST = array();
  24. $this->filesVariables = $_FILES;
  25. $_FILES = array();
  26. parent::__construct($site);
  27. }
  28. protected function undoQuotes(&$value, $key) {
  29. $value = stripslashes($value);
  30. }
  31. public function __clone() {
  32. parent::__clone();
  33. }
  34. public function getRequestUri() {
  35. if (null === $this->requestUri) {
  36. $this->setRequestUri();
  37. }
  38. return $this->requestUri;
  39. }
  40. public function setRequestUri($requestUri = null) {
  41. if ($requestUri === null) {
  42. if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // check this first so IIS will catch
  43. $requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
  44. } elseif (isset($_SERVER['REQUEST_URI'])) {
  45. $requestUri = $_SERVER['REQUEST_URI'];
  46. } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI
  47. $requestUri = $_SERVER['ORIG_PATH_INFO'];
  48. if (!empty($_SERVER['QUERY_STRING'])) {
  49. $requestUri .= '?' . $_SERVER['QUERY_STRING'];
  50. }
  51. } else {
  52. return $this;
  53. }
  54. } elseif (!is_string($requestUri)) {
  55. return $this;
  56. } else {
  57. // Set GET items, if available
  58. $this->getVariables = array();
  59. if (false !== ($pos = strpos($requestUri, '?'))) {
  60. // Get key => value pairs and set $_GET
  61. $query = substr($requestUri, $pos + 1);
  62. parse_str($query, $vars);
  63. $this->getVariables = $vars;
  64. }
  65. }
  66. $this->requestUri = urldecode($requestUri);
  67. return $this;
  68. }
  69. public function setBaseUrl($baseUrl = null) {
  70. if ((null !== $baseUrl) && !is_string($baseUrl)) {
  71. return $this;
  72. }
  73. if ($baseUrl === null) {
  74. $filename = basename($_SERVER['SCRIPT_FILENAME']);
  75. if (basename($_SERVER['SCRIPT_NAME']) === $filename) {
  76. $baseUrl = $_SERVER['SCRIPT_NAME'];
  77. } elseif (basename($_SERVER['PHP_SELF']) === $filename) {
  78. $baseUrl = $_SERVER['PHP_SELF'];
  79. } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $filename) {
  80. $baseUrl = $_SERVER['ORIG_SCRIPT_NAME']; // 1and1 shared hosting compatibility
  81. } else {
  82. // Backtrack up the script_filename to find the portion matching
  83. // php_self
  84. $path = $_SERVER['PHP_SELF'];
  85. $segs = explode('/', trim($_SERVER['SCRIPT_FILENAME'], '/'));
  86. $segs = array_reverse($segs);
  87. $index = 0;
  88. $last = count($segs);
  89. $baseUrl = '';
  90. do {
  91. $seg = $segs[$index];
  92. $baseUrl = '/' . $seg . $baseUrl;
  93. ++$index;
  94. } while (($last > $index) && (false !== ($pos = strpos($path, $baseUrl))) && (0 != $pos));
  95. }
  96. // Does the baseUrl have anything in common with the request_uri?
  97. $requestUri = $this->getRequestUri();
  98. if (0 === strpos($requestUri, $baseUrl)) {
  99. // full $baseUrl matches
  100. $this->baseUrl = $baseUrl;
  101. return $this;
  102. }
  103. if (0 === strpos($requestUri, dirname($baseUrl))) {
  104. // directory portion of $baseUrl matches
  105. $this->baseUrl = rtrim(dirname($baseUrl), '/');
  106. return $this;
  107. }
  108. if (!strpos($requestUri, basename($baseUrl))) {
  109. // no match whatsoever; set it blank
  110. $this->baseUrl = '';
  111. return $this;
  112. }
  113. // If using mod_rewrite or ISAPI_Rewrite strip the script filename
  114. // out of baseUrl. $pos !== 0 makes sure it is not matching a value
  115. // from PATH_INFO or QUERY_STRING
  116. if ((strlen($requestUri) >= strlen($baseUrl))
  117. && ((false !== ($pos = strpos($requestUri, $baseUrl))) && ($pos !== 0)))
  118. {
  119. $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl));
  120. }
  121. }
  122. $this->baseUrl = rtrim($baseUrl, '/');
  123. return $this;
  124. }
  125. public function getBaseUrl() {
  126. if (null === $this->baseUrl) {
  127. $this->setBaseUrl();
  128. }
  129. return $this->baseUrl;
  130. }
  131. public function setDocumentRoot($documentRoot = null) {
  132. if ($documentRoot === null) {
  133. $filename = basename($_SERVER['SCRIPT_FILENAME']);
  134. $baseUrl = $this->getBaseUrl();
  135. if (empty($baseUrl)) {
  136. $documentRoot = '';
  137. } elseif (basename($baseUrl) === $filename) {
  138. $documentRoot = dirname($baseUrl);
  139. } else {
  140. $documentRoot = $baseUrl;
  141. }
  142. }
  143. $this->documentRoot = realpath(trim($documentRoot, '/')).'/';
  144. return $this;
  145. }
  146. public function getDocumentRoot() {
  147. if (null === $this->documentRoot) {
  148. $this->setDocumentRoot();
  149. }
  150. return $this->documentRoot;
  151. }
  152. public function setPathInfo($pathInfo = null) {
  153. if ($pathInfo === null) {
  154. $baseUrl = $this->getBaseUrl();
  155. if (null === ($requestUri = $this->getRequestUri())) {
  156. return $this;
  157. }
  158. // Remove the query string from REQUEST_URI
  159. if ($pos = strpos($requestUri, '?')) {
  160. $requestUri = substr($requestUri, 0, $pos);
  161. }
  162. if ((null !== $baseUrl) && (false === ($pathInfo = substr($requestUri, strlen($baseUrl))))) {
  163. // If substr() returns false then PATH_INFO is set to an empty string
  164. $pathInfo = '';
  165. } elseif (null === $baseUrl) {
  166. $pathInfo = $requestUri;
  167. }
  168. }
  169. $this->pathInfo = (string) $pathInfo;
  170. return $this;
  171. }
  172. public function getPathInfo() {
  173. if (empty($this->pathInfo)) {
  174. $this->setPathInfo();
  175. }
  176. return $this->pathInfo;
  177. }
  178. public function getGET($name = null, $default = null) {
  179. if ($name === null) return $this->getVariables;
  180. if (isset($this->getVariables[$name])) {
  181. return $this->getVariables[$name];
  182. }
  183. return $default;
  184. }
  185. public function setGET($get) {
  186. $this->getVariables = $get;
  187. }
  188. public function getPOST($name = null, $default = null) {
  189. if ($name === null) return $this->postVariables;
  190. if (isset($this->postVariables[$name])) {
  191. return $this->postVariables[$name];
  192. }
  193. return $default;
  194. }
  195. public function setPOST($post) {
  196. $this->postVariables = $post;
  197. }
  198. public function getFILES($name = null, $default = null) {
  199. if ($name === null) return $this->filesVariables;
  200. if (isset($this->filesVariables[$name])) {
  201. return $this->filesVariables[$name];
  202. }
  203. return $default;
  204. }
  205. public function setFILES($files) {
  206. $this->filesVariables = $files;
  207. }
  208. public function getHeader($header) {
  209. if (empty($header))
  210. return false;
  211. // Try to get it from the $_SERVER array first
  212. $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
  213. if (!empty($_SERVER[$temp])) {
  214. return $_SERVER[$temp];
  215. }
  216. // This seems to be the only way to get the Authorization header on
  217. // Apache
  218. if (function_exists('apache_request_headers')) {
  219. $headers = apache_request_headers();
  220. if (!empty($headers[$header])) {
  221. return $headers[$header];
  222. }
  223. }
  224. return false;
  225. }
  226. public function isXmlHttpRequest() {
  227. return ($this->getHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
  228. }
  229. public function isFlashRequest() {
  230. return ($this->getHeader('USER_AGENT') == 'Shockwave Flash');
  231. }
  232. }