/Ip/Request.php

https://gitlab.com/x33n/ImpressPages · PHP · 301 lines · 156 code · 43 blank · 102 comment · 30 complexity · 8396033e0362b4d6aadadc96c2621af5 MD5 · raw file

  1. <?php
  2. /**
  3. * @package ImpressPages
  4. *
  5. *
  6. */
  7. namespace Ip;
  8. /**
  9. * Get current HTTP request information
  10. *
  11. */
  12. class Request
  13. {
  14. protected $_SERVER = array();
  15. protected $_POST = array();
  16. protected $_GET = array();
  17. protected $_REQUEST = array();
  18. protected $_COOKIE = array();
  19. /**
  20. * @var \Ip\controller
  21. */
  22. protected $controller = null;
  23. protected $routePath = null;
  24. public function __construct()
  25. {
  26. $server = $_SERVER;
  27. $server['REDIRECT_QUERY_STRING'] = '';
  28. $server['REDIRECT_URL'] = '';
  29. $server['QUERY_STRING'] = '';
  30. $server['REQUEST_URI'] = parse_url(ipConfig()->baseUrl(), PHP_URL_PATH); // default uri points to root
  31. $this->setServer($server);
  32. }
  33. /**
  34. * Set post variables
  35. * @param $post
  36. */
  37. public function setPost($post)
  38. {
  39. $this->_POST = $post;
  40. $this->_REQUEST = array_merge($this->_REQUEST, $post);
  41. }
  42. /**
  43. * Set server data
  44. * @param $server
  45. */
  46. public function setServer($server)
  47. {
  48. $this->_SERVER = $server;
  49. }
  50. /**
  51. * Set GET query
  52. * @param $query
  53. */
  54. public function setQuery($query)
  55. {
  56. $this->_GET = $query;
  57. $this->_REQUEST = array_merge($this->_REQUEST, $query);
  58. }
  59. /**
  60. * Set request data
  61. * @param $request
  62. */
  63. public function setRequest($request)
  64. {
  65. $this->_REQUEST = $request;
  66. }
  67. /**
  68. * Check if HTTP request data is provided using GET method
  69. *
  70. * @return bool Returns true for GET method
  71. */
  72. public function isGet()
  73. {
  74. return $this->getMethod() == 'GET';
  75. }
  76. /**
  77. * Check if HTTP request data is provided using POST method
  78. *
  79. * @return bool Returns true for POST method
  80. */
  81. public function isPost()
  82. {
  83. return $this->getMethod() == 'POST';
  84. }
  85. public function isAjax()
  86. {
  87. return strtolower($this->getServer('HTTP_X_REQUESTED_WITH')) == 'xmlhttprequest';
  88. }
  89. /**
  90. * Require to provide HTTP request data using POST method
  91. *
  92. * @throws \Ip\Exception is thrown if POST method was not used.
  93. */
  94. public function mustBePost()
  95. {
  96. if (!$this->isPost()) {
  97. throw new \Ip\Exception('POST method required.');
  98. }
  99. }
  100. /**
  101. * Check if HTTPS protocol is used
  102. * @return bool Returns true for HTTPS request
  103. */
  104. public function isHttps()
  105. {
  106. return (isset($this->_SERVER["HTTPS"]) && strtolower($this->_SERVER["HTTPS"]) == "on");
  107. }
  108. /**
  109. * Get request method, such as 'GET', 'HEAD', 'POST', or 'PUT'
  110. *
  111. * @return string Request method
  112. */
  113. public function getMethod()
  114. {
  115. return $this->_SERVER['REQUEST_METHOD'];
  116. }
  117. /**
  118. * Return GET query parameter if $name is passed. Returns all query parameters if name == null.
  119. *
  120. * @param string $name query parameter name
  121. * @param mixed $default default value if no GET parameter exists
  122. * @return mixed GET query variable (all query variables if $name == null)
  123. */
  124. public function getQuery($name = null, $default = null)
  125. {
  126. return $this->getParam($name, $this->_GET, $default);
  127. }
  128. /**
  129. * Returns POST parameter if $name is passed. Returns all query parameters if name == null.
  130. *
  131. * @param string $name query parameter name
  132. * @param mixed $default default value if no GET parameter exists
  133. * @return mixed GET query variable (all query variables if $name == null)
  134. */
  135. public function getPost($name = null, $default = null)
  136. {
  137. return $this->getParam($name, $this->_POST, $default);
  138. }
  139. /**
  140. * Return request parameter if $name is passed. Returns all request parameters if $name == null.
  141. *
  142. * @param string $name query parameter name
  143. * @param mixed $default default value if no GET parameter exists
  144. * @return mixed GET query variable (all query variables if $name == null)
  145. */
  146. public function getRequest($name = null, $default = null)
  147. {
  148. return $this->getParam($name, $this->_REQUEST, $default);
  149. }
  150. /**
  151. * Return parameters, such as headers, paths, and script locations, provided in $_SERVER array
  152. *
  153. * @param string $name parameter name
  154. * @param string $default default value returned when a server parameter is null
  155. * @return mixed
  156. */
  157. public function getServer($name = null, $default = null)
  158. {
  159. return $this->getParam($name, $this->_SERVER, $default);
  160. }
  161. protected function getParam($name, $values, $default)
  162. {
  163. if ($name === null) {
  164. return $values;
  165. }
  166. if (!array_key_exists($name, $values)) {
  167. return $default;
  168. }
  169. return $values[$name];
  170. }
  171. /**
  172. * Get current page URL
  173. *
  174. * @return string URL address
  175. */
  176. public function getUrl()
  177. {
  178. $pageURL = 'http';
  179. if (isset($this->_SERVER["HTTPS"]) && strtolower($this->_SERVER["HTTPS"]) == "on") {
  180. $pageURL .= "s";
  181. }
  182. $pageURL .= '://';
  183. if ($this->_SERVER["SERVER_PORT"] != "80" && $this->_SERVER["SERVER_PORT"] != "443") {
  184. $pageURL .= $this->_SERVER["SERVER_NAME"] . ":" . $this->_SERVER["SERVER_PORT"] . $this->_SERVER["REQUEST_URI"];
  185. } else {
  186. $pageURL .= $this->_SERVER["SERVER_NAME"] . $this->_SERVER["REQUEST_URI"];
  187. }
  188. return $pageURL;
  189. }
  190. /**
  191. * Gets relative path from base URL
  192. *
  193. * @return string Path after BASE_URL
  194. */
  195. public function getRelativePath()
  196. {
  197. $basePath = parse_url(ipConfig()->baseUrl(), PHP_URL_PATH);
  198. $requestPath = parse_url($this->_SERVER["REQUEST_URI"], PHP_URL_PATH);
  199. if (strpos($requestPath, $basePath) !== 0) {
  200. if ($requestPath == rtrim($basePath, '/')) {
  201. return '';
  202. }
  203. // TODO log error
  204. return $requestPath;
  205. }
  206. $relativePath = substr($requestPath, strlen($basePath));
  207. if (strpos($relativePath, 'index.php') === 0) { // remove index.php if needed
  208. $relativePath = substr($relativePath, 9);
  209. }
  210. return $relativePath ? ltrim(urldecode($relativePath), '/') : '';
  211. }
  212. /**
  213. * @private
  214. * for internal ImpressPages uses only
  215. */
  216. public function _setRoutePath($routePath)
  217. {
  218. $this->routePath = $routePath;
  219. }
  220. public function getRoutePath()
  221. {
  222. return $this->routePath;
  223. }
  224. /**
  225. * @ignore
  226. */
  227. public function fixMagicQuotes()
  228. {
  229. if (!get_magic_quotes_gpc()) {
  230. return;
  231. }
  232. $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
  233. while (list($key, $val) = each($process)) {
  234. foreach ($val as $k => $v) {
  235. unset($process[$key][$k]);
  236. if (is_array($v)) {
  237. $process[$key][stripslashes($k)] = $v;
  238. $process[] = & $process[$key][stripslashes($k)];
  239. } else {
  240. $process[$key][stripslashes($k)] = stripslashes($v);
  241. }
  242. }
  243. }
  244. unset($process);
  245. }
  246. /**
  247. * @ignore
  248. * @return bool true if current url is pointing to website root or one of the languages
  249. */
  250. public function _isWebsiteRoot()
  251. {
  252. $relativePath = $this->getRelativePath();
  253. if (!$relativePath || (empty($relativePath[0]) || $relativePath[0] == '?' || $relativePath == 'index.php')) {
  254. return true;
  255. }
  256. return false;
  257. }
  258. }