PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/classes/XLite/Core/Request.php

https://github.com/ckdimka/core
PHP | 343 lines | 118 code | 33 blank | 192 comment | 24 complexity | ba44f6ed0a272ad8fb0a304d29fa28e6 MD5 | raw file
  1. <?php
  2. // vim: set ts=4 sw=4 sts=4 et:
  3. /**
  4. * LiteCommerce
  5. *
  6. * NOTICE OF LICENSE
  7. *
  8. * This source file is subject to the Open Software License (OSL 3.0)
  9. * that is bundled with this package in the file LICENSE.txt.
  10. * It is also available through the world-wide-web at this URL:
  11. * http://opensource.org/licenses/osl-3.0.php
  12. * If you did not receive a copy of the license and are unable to
  13. * obtain it through the world-wide-web, please send an email
  14. * to licensing@litecommerce.com so we can send you a copy immediately.
  15. *
  16. * PHP version 5.3.0
  17. *
  18. * @category LiteCommerce
  19. * @author Creative Development LLC <info@cdev.ru>
  20. * @copyright Copyright (c) 2011 Creative Development LLC <info@cdev.ru>. All rights reserved
  21. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @link http://www.litecommerce.com/
  23. * @see ____file_see____
  24. * @since 1.0.0
  25. */
  26. namespace XLite\Core;
  27. /**
  28. * Request
  29. *
  30. * @see ____class_see____
  31. * @since 1.0.0
  32. */
  33. class Request extends \XLite\Base\Singleton
  34. {
  35. const METHOD_CLI = 'cli';
  36. /**
  37. * Cureent request method
  38. *
  39. * @var string
  40. * @see ____var_see____
  41. * @since 1.0.0
  42. */
  43. protected $requestMethod = null;
  44. /**
  45. * Request data
  46. *
  47. * @var array
  48. * @see ____var_see____
  49. * @since 1.0.0
  50. */
  51. protected $data = array();
  52. /**
  53. * Map request data
  54. *
  55. * @param array $data Custom data OPTIONAL
  56. *
  57. * @return void
  58. * @see ____func_see____
  59. * @since 1.0.0
  60. */
  61. public function mapRequest(array $data = array())
  62. {
  63. if (empty($data)) {
  64. if ($this->isCLI()) {
  65. for ($i = 1; count($_SERVER['argv']) > $i; $i++) {
  66. $pair = explode('=', $_SERVER['argv'][$i], 2);
  67. $data[preg_replace('/^-+/Ss', '', $pair[0])] = isset($pair[1]) ? trim($pair[1]) : true;
  68. }
  69. } else {
  70. $data = $_REQUEST;
  71. }
  72. }
  73. $this->data = array_replace_recursive($this->data, $this->prepare($data));
  74. }
  75. /**
  76. * Return all data
  77. *
  78. * @return array
  79. * @see ____func_see____
  80. * @since 1.0.0
  81. */
  82. public function getData()
  83. {
  84. return $this->data;
  85. }
  86. /**
  87. * Return current request method
  88. *
  89. * @return string
  90. * @see ____func_see____
  91. * @since 1.0.0
  92. */
  93. public function getRequestMethod()
  94. {
  95. return $this->requestMethod;
  96. }
  97. /**
  98. * Set request method
  99. *
  100. * @param string $method New request method
  101. *
  102. * @return void
  103. * @see ____func_see____
  104. * @since 1.0.0
  105. */
  106. public function setRequestMethod($method)
  107. {
  108. $this->requestMethod = $method;
  109. }
  110. /**
  111. * Check if current request method is "GET"
  112. *
  113. * @return boolean
  114. * @see ____func_see____
  115. * @since 1.0.0
  116. */
  117. public function isGet()
  118. {
  119. return 'GET' === $this->requestMethod;
  120. }
  121. /**
  122. * Check if current request method is "POST"
  123. *
  124. * @return boolean
  125. * @see ____func_see____
  126. * @since 1.0.0
  127. */
  128. public function isPost()
  129. {
  130. return 'POST' === $this->requestMethod;
  131. }
  132. /**
  133. * Check - is AJAX request or not
  134. *
  135. * @return boolean
  136. * @see ____func_see____
  137. * @since 1.0.0
  138. */
  139. public function isAJAX()
  140. {
  141. return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
  142. }
  143. /**
  144. * Check - is secure connection or not
  145. *
  146. * @return boolean
  147. * @see ____func_see____
  148. * @since 1.0.0
  149. */
  150. public function isHTTPS()
  151. {
  152. return (isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS'] == 'on') || $_SERVER['HTTPS'] == '1'))
  153. || (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443')
  154. || (
  155. isset($_SERVER['REMOTE_ADDR'])
  156. && \XLite::getInstance()->getOptions(array('host_details', 'remote_addr')) == $_SERVER['REMOTE_ADDR']
  157. );
  158. }
  159. /**
  160. * Check - is command line interface or not
  161. *
  162. * @return boolean
  163. * @see ____func_see____
  164. * @since 1.0.0
  165. */
  166. public function isCLI()
  167. {
  168. return 'cli' == PHP_SAPI;
  169. }
  170. /**
  171. * Getter
  172. *
  173. * @param string $name Property name
  174. *
  175. * @return mixed
  176. * @see ____func_see____
  177. * @since 1.0.0
  178. */
  179. public function __get($name)
  180. {
  181. return isset($this->data[$name]) ? $this->data[$name] : null;
  182. }
  183. /**
  184. * Setter
  185. *
  186. * @param string $name Property name
  187. * @param mixed $value Property value
  188. *
  189. * @return void
  190. * @see ____func_see____
  191. * @since 1.0.0
  192. */
  193. public function __set($name, $value)
  194. {
  195. $this->data[$name] = $this->prepare($value);
  196. }
  197. /**
  198. * Check property accessability
  199. *
  200. * @param string $name Property name
  201. *
  202. * @return boolean
  203. * @see ____func_see____
  204. * @since 1.0.0
  205. */
  206. public function __isset($name)
  207. {
  208. return isset($this->data[$name]);
  209. }
  210. /**
  211. * Constructor
  212. *
  213. * @return void
  214. * @see ____func_see____
  215. * @since 1.0.0
  216. */
  217. protected function __construct()
  218. {
  219. $this->requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : self::METHOD_CLI;
  220. $this->mapRequest();
  221. }
  222. /**
  223. * Unescape single value
  224. *
  225. * @param string $value Value to sanitize
  226. *
  227. * @return string
  228. * @see ____func_see____
  229. * @since 1.0.0
  230. */
  231. protected function doUnescapeSingle($value)
  232. {
  233. return stripslashes($value);
  234. }
  235. /**
  236. * Remove automatically added escaping
  237. *
  238. * @param mixed $data Data to sanitize
  239. *
  240. * @return mixed
  241. * @see ____func_see____
  242. * @since 1.0.0
  243. */
  244. protected function doUnescape($data)
  245. {
  246. return is_array($data)
  247. ? array_map(array($this, __FUNCTION__), $data)
  248. : $this->doUnescapeSingle($data);
  249. }
  250. /**
  251. * Normalize request data
  252. *
  253. * @param mixed $request Request data
  254. *
  255. * @return mixed
  256. * @see ____func_see____
  257. * @since 1.0.0
  258. */
  259. protected function normalizeRequestData($request)
  260. {
  261. if (ini_get('magic_quotes_gpc')) {
  262. $request = $this->doUnescape($request);
  263. }
  264. return $request;
  265. }
  266. /**
  267. * Wrapper for sanitize()
  268. *
  269. * @param mixed $data Data to sanitize
  270. *
  271. * @return mixed
  272. * @see ____func_see____
  273. * @since 1.0.0
  274. */
  275. protected function prepare($data)
  276. {
  277. if (is_array($data)) {
  278. if (isset($data['target']) && !$this->checkControlArgument($data['target'], 'Target')) {
  279. $data['target'] = \XLite::TARGET_404;
  280. $data['action'] = null;
  281. }
  282. if (isset($data['action']) && !$this->checkControlArgument($data['action'], 'Action')) {
  283. unset($data['action']);
  284. }
  285. }
  286. return $this->normalizeRequestData($data);
  287. }
  288. /**
  289. * Check control argument (like target)
  290. *
  291. * @param mixed $value Argument value
  292. * @param string $name Argument name
  293. *
  294. * @return boolean
  295. * @see ____func_see____
  296. * @since 1.0.0
  297. */
  298. protected function checkControlArgument($value, $name)
  299. {
  300. $result = true;
  301. if (!is_string($value)) {
  302. \XLite\Logger::getInstance()->log($name . ' has a wrong type');
  303. $result = false;
  304. } elseif (!preg_match('/^[a-z0-9_]*$/Ssi', $value)) {
  305. \XLite\Logger::getInstance()->log($name . ' has a wrong format');
  306. $result = false;
  307. }
  308. return $result;
  309. }
  310. }