PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/core/classes/input.php

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