PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/category_tool/fuel/core/classes/input.php

https://github.com/connvoi/dev
PHP | 449 lines | 219 code | 53 blank | 177 comment | 27 complexity | 07af14e6f2de718939eb3477be2c616d MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2012 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * Input class
  15. *
  16. * The input class allows you to access HTTP parameters, load server variables
  17. * and user agent details.
  18. *
  19. * @package Fuel
  20. * @category Core
  21. * @link http://docs.fuelphp.com/classes/input.html
  22. */
  23. class Input
  24. {
  25. /**
  26. * @var $detected_uri The URI that was detected automatically
  27. */
  28. protected static $detected_uri = null;
  29. /**
  30. * @var $detected_ext The URI extension that was detected automatically
  31. */
  32. protected static $detected_ext = null;
  33. /**
  34. * @var $input All of the input (GET, POST, PUT, DELETE)
  35. */
  36. protected static $input = null;
  37. /**
  38. * @var $put_delete All of the put or delete vars
  39. */
  40. protected static $put_delete = null;
  41. /**
  42. * @var $content parsed request body (xml/json)
  43. */
  44. protected static $content = null;
  45. /**
  46. * Get the request body interpreted as JSON.
  47. *
  48. * @return array parsed request body content.
  49. */
  50. public static function json($index = null, $default = null)
  51. {
  52. if (static::$content === null)
  53. {
  54. static::hydrate_raw_input('json');
  55. }
  56. return (func_num_args() === 0) ? static::$content : \Arr::get(static::$content, $index, $default);
  57. }
  58. /**
  59. * Get the request body interpreted as XML.
  60. *
  61. * @return array parsed request body content.
  62. */
  63. public static function xml($index = null, $default = null)
  64. {
  65. if (static::$content === null)
  66. {
  67. static::hydrate_raw_input('xml');
  68. }
  69. return (func_num_args() === 0) ? static::$content : \Arr::get(static::$content, $index, $default);
  70. }
  71. /**
  72. * Hydration from raw request (xml/json requests)
  73. *
  74. * @param string $type input type
  75. */
  76. protected static function hydrate_raw_input($type)
  77. {
  78. $content = \Format::forge(file_get_contents('php://input'), $type)->to_array();
  79. is_array($content) and static::$content = \Security::clean($content);
  80. }
  81. /**
  82. * Detects and returns the current URI based on a number of different server
  83. * variables.
  84. *
  85. * @return string
  86. */
  87. public static function uri()
  88. {
  89. if (static::$detected_uri !== null)
  90. {
  91. return static::$detected_uri;
  92. }
  93. if (\Fuel::$is_cli)
  94. {
  95. if ($uri = \Cli::option('uri') !== null)
  96. {
  97. static::$detected_uri = $uri;
  98. }
  99. else
  100. {
  101. static::$detected_uri = \Cli::option(1);
  102. }
  103. return static::$detected_uri;
  104. }
  105. // We want to use PATH_INFO if we can.
  106. if ( ! empty($_SERVER['PATH_INFO']))
  107. {
  108. $uri = $_SERVER['PATH_INFO'];
  109. }
  110. // Only use ORIG_PATH_INFO if it contains the path
  111. elseif ( ! empty($_SERVER['ORIG_PATH_INFO']) and ($path = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['ORIG_PATH_INFO'])) != '')
  112. {
  113. $uri = $path;
  114. }
  115. else
  116. {
  117. // Fall back to parsing the REQUEST URI
  118. if (isset($_SERVER['REQUEST_URI']))
  119. {
  120. $uri = $_SERVER['REQUEST_URI'];
  121. }
  122. else
  123. {
  124. throw new \FuelException('Unable to detect the URI.');
  125. }
  126. // Remove the base URL from the URI
  127. $base_url = parse_url(\Config::get('base_url'), PHP_URL_PATH);
  128. if ($uri != '' and strncmp($uri, $base_url, strlen($base_url)) === 0)
  129. {
  130. $uri = substr($uri, strlen($base_url));
  131. }
  132. // If we are using an index file (not mod_rewrite) then remove it
  133. $index_file = \Config::get('index_file');
  134. if ($index_file and strncmp($uri, $index_file, strlen($index_file)) === 0)
  135. {
  136. $uri = substr($uri, strlen($index_file));
  137. }
  138. // When index.php? is used and the config is set wrong, lets just
  139. // be nice and help them out.
  140. if ($index_file and strncmp($uri, '?/', 2) === 0)
  141. {
  142. $uri = substr($uri, 1);
  143. }
  144. // Lets split the URI up in case it contains a ?. This would
  145. // indicate the server requires 'index.php?' and that mod_rewrite
  146. // is not being used.
  147. preg_match('#(.*?)\?(.*)#i', $uri, $matches);
  148. // If there are matches then lets set set everything correctly
  149. if ( ! empty($matches))
  150. {
  151. $uri = $matches[1];
  152. $_SERVER['QUERY_STRING'] = $matches[2];
  153. parse_str($matches[2], $_GET);
  154. }
  155. }
  156. // Strip the defined url suffix from the uri if needed
  157. $uri_info = pathinfo($uri);
  158. if ( ! empty($uri_info['extension']))
  159. {
  160. static::$detected_ext = $uri_info['extension'];
  161. $uri = $uri_info['dirname'].'/'.$uri_info['filename'];
  162. }
  163. // Do some final clean up of the uri
  164. static::$detected_uri = \Security::clean_uri($uri, true);
  165. return static::$detected_uri;
  166. }
  167. /**
  168. * Detects and returns the current URI extension
  169. *
  170. * @return string
  171. */
  172. public static function extension()
  173. {
  174. static::$detected_ext === null and static::uri();
  175. return static::$detected_ext;
  176. }
  177. /**
  178. * Get the public ip address of the user.
  179. *
  180. * @return string
  181. */
  182. public static function ip($default = '0.0.0.0')
  183. {
  184. return static::server('REMOTE_ADDR', $default);
  185. }
  186. /**
  187. * Get the real ip address of the user. Even if they are using a proxy.
  188. *
  189. * @param string the default to return on failure
  190. * @param bool exclude private and reserved IPs
  191. * @return string the real ip address of the user
  192. */
  193. public static function real_ip($default = '0.0.0.0', $exclude_reserved = false)
  194. {
  195. $server_keys = array('HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'REMOTE_ADDR');
  196. foreach ($server_keys as $key)
  197. {
  198. if ( ! static::server($key))
  199. {
  200. continue;
  201. }
  202. $ips = explode(',', static::server($key));
  203. array_walk($ips, function (&$ip) {
  204. $ip = trim($ip);
  205. });
  206. if ($exclude_reserved)
  207. {
  208. $ips = array_filter($ips, function($ip) {
  209. return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
  210. });
  211. }
  212. if ($ips)
  213. {
  214. return reset($ips);
  215. }
  216. }
  217. return \Fuel::value($default);
  218. }
  219. /**
  220. * Return's the protocol that the request was made with
  221. *
  222. * @return string
  223. */
  224. public static function protocol()
  225. {
  226. if ((static::server('HTTPS') !== null and static::server('HTTPS') != 'off')
  227. or (static::server('HTTPS') === null and static::server('SERVER_PORT') == 443))
  228. {
  229. return 'https';
  230. }
  231. return 'http';
  232. }
  233. /**
  234. * Return's whether this is an AJAX request or not
  235. *
  236. * @return bool
  237. */
  238. public static function is_ajax()
  239. {
  240. return (static::server('HTTP_X_REQUESTED_WITH') !== null) and strtolower(static::server('HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest';
  241. }
  242. /**
  243. * Return's the referrer
  244. *
  245. * @return string
  246. */
  247. public static function referrer($default = '')
  248. {
  249. return static::server('HTTP_REFERER', $default);
  250. }
  251. /**
  252. * Return's the input method used (GET, POST, DELETE, etc.)
  253. *
  254. * @return string
  255. */
  256. public static function method($default = 'GET')
  257. {
  258. return static::server('HTTP_X_HTTP_METHOD_OVERRIDE', static::server('REQUEST_METHOD', $default));
  259. }
  260. /**
  261. * Return's the user agent
  262. *
  263. * @return string
  264. */
  265. public static function user_agent($default = '')
  266. {
  267. return static::server('HTTP_USER_AGENT', $default);
  268. }
  269. /**
  270. * Returns all of the GET, POST, PUT and DELETE variables.
  271. *
  272. * @return array
  273. */
  274. public static function all()
  275. {
  276. if (static::$input === null)
  277. {
  278. static::hydrate();
  279. }
  280. return static::$input;
  281. }
  282. /**
  283. * Gets the specified GET variable.
  284. *
  285. * @param string $index The index to get
  286. * @param string $default The default value
  287. * @return string|array
  288. */
  289. public static function get($index = null, $default = null)
  290. {
  291. return (func_num_args() === 0) ? $_GET : \Arr::get($_GET, $index, $default);
  292. }
  293. /**
  294. * Fetch an item from the POST array
  295. *
  296. * @param string The index key
  297. * @param mixed The default value
  298. * @return string|array
  299. */
  300. public static function post($index = null, $default = null)
  301. {
  302. return (func_num_args() === 0) ? $_POST : \Arr::get($_POST, $index, $default);
  303. }
  304. /**
  305. * Fetch an item from the php://input for put arguments
  306. *
  307. * @param string The index key
  308. * @param mixed The default value
  309. * @return string|array
  310. */
  311. public static function put($index = null, $default = null)
  312. {
  313. if (static::$put_delete === null)
  314. {
  315. static::hydrate();
  316. }
  317. return (func_num_args() === 0) ? static::$put_delete : \Arr::get(static::$put_delete, $index, $default);
  318. }
  319. /**
  320. * Fetch an item from the php://input for delete arguments
  321. *
  322. * @param string The index key
  323. * @param mixed The default value
  324. * @return string|array
  325. */
  326. public static function delete($index = null, $default = null)
  327. {
  328. if (static::$put_delete === null)
  329. {
  330. static::hydrate();
  331. }
  332. return (is_null($index) and func_num_args() === 0) ? static::$put_delete : \Arr::get(static::$put_delete, $index, $default);
  333. }
  334. /**
  335. * Fetch an item from the FILE array
  336. *
  337. * @param string The index key
  338. * @param mixed The default value
  339. * @return string|array
  340. */
  341. public static function file($index = null, $default = null)
  342. {
  343. return (func_num_args() === 0) ? $_FILES : \Arr::get($_FILES, $index, $default);
  344. }
  345. /**
  346. * Fetch an item from either the GET, POST, PUT or DELETE array
  347. *
  348. * @param string The index key
  349. * @param mixed The default value
  350. * @return string|array
  351. */
  352. public static function param($index = null, $default = null)
  353. {
  354. if (static::$input === null)
  355. {
  356. static::hydrate();
  357. }
  358. return \Arr::get(static::$input, $index, $default);
  359. }
  360. /**
  361. * Fetch an item from the COOKIE array
  362. *
  363. * @param string The index key
  364. * @param mixed The default value
  365. * @return string|array
  366. */
  367. public static function cookie($index = null, $default = null)
  368. {
  369. return (func_num_args() === 0) ? $_COOKIE : \Arr::get($_COOKIE, $index, $default);
  370. }
  371. /**
  372. * Fetch an item from the SERVER array
  373. *
  374. * @param string The index key
  375. * @param mixed The default value
  376. * @return string|array
  377. */
  378. public static function server($index = null, $default = null)
  379. {
  380. return (func_num_args() === 0) ? $_SERVER : \Arr::get($_SERVER, strtoupper($index), $default);
  381. }
  382. /**
  383. * Hydrates the input array
  384. *
  385. * @return void
  386. */
  387. protected static function hydrate()
  388. {
  389. static::$input = array_merge($_GET, $_POST);
  390. if (\Input::method() == 'PUT' or \Input::method() == 'DELETE')
  391. {
  392. parse_str(file_get_contents('php://input'), static::$put_delete);
  393. static::$input = array_merge(static::$input, static::$put_delete);
  394. }
  395. }
  396. }