Http.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace Qii\Request;
  3. final class Http extends Base
  4. {
  5. /**
  6. * __construct
  7. *
  8. * @param string $request_uri
  9. * @param string $base_uri
  10. */
  11. public function __construct($request_uri = null, $base_uri = null)
  12. {
  13. if (isset($_SERVER['REQUEST_METHOD'])) {
  14. $this->method = $_SERVER['REQUEST_METHOD'];
  15. } else {
  16. if (!strncasecmp(PHP_SAPI, 'cli', 3)) {
  17. $this->method = 'Cli';
  18. } else {
  19. $this->method = 'Unknown';
  20. }
  21. }
  22. if (empty($request_uri)) {
  23. do {
  24. // #ifdef PHP_WIN32
  25. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  26. /* check this first so IIS will catch */
  27. if ($request_uri = $this->getServer('HTTP_X_REWRITE_URL')) {
  28. break;
  29. }
  30. /* IIS7 with URL Rewrite: make sure we get the unencoded url (double slash problem) */
  31. if ($rewrited = (boolean)$this->getServer('IIS_WasUrlRewritten')) {
  32. $unencode = $this->getServer('UNENCODED_URL');
  33. if ($unencode && is_string($unencode)) {
  34. $request_uri = $unencode;
  35. }
  36. break;
  37. }
  38. // #endif
  39. }
  40. if ($request_uri = $this->getServer('PATH_INFO')) {
  41. break;
  42. }
  43. if ($request_uri = $this->getServer('REQUEST_URI')) {
  44. /* Http proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path */
  45. if (strstr($request_uri, 'http') == $request_uri) {
  46. $url_info = parse_url($request_uri);
  47. if ($url_info && isset($url_info['path'])) {
  48. $request_uri = $url_info['path'];
  49. }
  50. } else {
  51. if ($pos = strstr($request_uri, '?')) {
  52. $request_uri = substr($request_uri, 0, $pos - 1);
  53. }
  54. }
  55. break;
  56. }
  57. if ($request_uri = $this->getServer('ORIG_PATH_INFO')) {
  58. /* intended do nothing */
  59. /*
  60. if ($query = $this->getServer('QUERY_STRING')) {
  61. }
  62. */
  63. break;
  64. }
  65. } while (0);
  66. }
  67. if ($request_uri && is_string($request_uri)) {
  68. $request_uri = str_replace('//', '/', $request_uri);
  69. $this->uri = $request_uri;
  70. // request_set_base_uri
  71. $this->_setbaseUri($base_uri, $request_uri);
  72. }
  73. $this->params = array();
  74. parent::__construct();
  75. }
  76. /**
  77. * getQuery
  78. *
  79. * @param string $name
  80. * @param mixed $default
  81. * @return mixed
  82. */
  83. public function getQuery($name = null, $default = null)
  84. {
  85. if (is_null($name)) {
  86. return $_GET;
  87. } elseif (isset($_GET[$name])) {
  88. return $_GET[$name];
  89. }
  90. return $default;
  91. }
  92. /**
  93. * getRequest
  94. *
  95. * @param string $name
  96. * @param mixed $default
  97. * @return mixed
  98. */
  99. public function getRequest($name = null, $default = null)
  100. {
  101. if (is_null($name)) {
  102. return $_REQUEST;
  103. } elseif (isset($_REQUEST[$name])) {
  104. return $_REQUEST[$name];
  105. }
  106. return $default;
  107. }
  108. /**
  109. * getPost
  110. *
  111. * @param string $name
  112. * @param mixed $default
  113. * @return mixed
  114. */
  115. public function getPost($name = null, $default = null)
  116. {
  117. if (is_null($name)) {
  118. return $_POST;
  119. } elseif (isset($_POST[$name])) {
  120. return $_POST[$name];
  121. }
  122. return $default;
  123. }
  124. /**
  125. * getCookie
  126. *
  127. * @param string $name
  128. * @param mixed $default
  129. * @return mixed
  130. */
  131. public function getCookie($name = null, $default = null)
  132. {
  133. if (is_null($name)) {
  134. return $_COOKIE;
  135. } elseif (isset($_COOKIE[$name])) {
  136. return $_COOKIE[$name];
  137. }
  138. return $default;
  139. }
  140. /**
  141. * getFiles
  142. *
  143. * @param string $name
  144. * @param mixed $default
  145. * @return mixed
  146. */
  147. public function getFiles($name = null, $default = null)
  148. {
  149. if (is_null($name)) {
  150. return $_FILES;
  151. } elseif (isset($_FILES[$name])) {
  152. return $_FILES[$name];
  153. }
  154. return $default;
  155. }
  156. /**
  157. * get [params -> post -> get -> cookie -> server]
  158. *
  159. * @param string $name
  160. * @param mixed $default
  161. * @return mixed
  162. */
  163. public function get($name, $default = null)
  164. {
  165. if (isset($this->params[$name])) {
  166. return $this->params[$name];
  167. } elseif (isset($_POST[$name])) {
  168. return $_POST[$name];
  169. } elseif (isset($_GET[$name])) {
  170. return $_GET[$name];
  171. } elseif (isset($_COOKIE[$name])) {
  172. return $_COOKIE[$name];
  173. } elseif (isset($_SERVER[$name])) {
  174. return $_SERVER[$name];
  175. }
  176. return $default;
  177. }
  178. /**
  179. * isXmlHttpRequest
  180. *
  181. * @param void
  182. * @return boolean
  183. */
  184. public function isXmlHttpRequest()
  185. {
  186. $header = isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? $_SERVER['HTTP_X_REQUESTED_WITH'] : '';
  187. if (is_string($header) && strncasecmp('XMLHttpRequest', $header, 14) == 0) {
  188. return true;
  189. }
  190. return false;
  191. }
  192. /**
  193. * __clone
  194. *
  195. * @param void
  196. */
  197. private function __clone()
  198. {
  199. }
  200. }