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

/fuel/core/classes/input.php

https://bitbucket.org/arkross/venus
PHP | 397 lines | 192 code | 46 blank | 159 comment | 26 complexity | 38216360dcc62b9da6533e6a050cf26d 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 - 2011 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 $input All of the input (GET, POST, PUT, DELETE)
  31. */
  32. protected static $input = null;
  33. /**
  34. * @var $put_delete All of the put or delete vars
  35. */
  36. protected static $put_delete = null;
  37. /**
  38. * Detects and returns the current URI based on a number of different server
  39. * variables.
  40. *
  41. * @return string
  42. */
  43. public static function uri()
  44. {
  45. if (static::$detected_uri !== null)
  46. {
  47. return static::$detected_uri;
  48. }
  49. if (\Fuel::$is_cli)
  50. {
  51. if ($uri = \Cli::option('uri') !== null)
  52. {
  53. static::$detected_uri = $uri;
  54. }
  55. else
  56. {
  57. static::$detected_uri = \Cli::option(1);
  58. }
  59. return static::$detected_uri;
  60. }
  61. // We want to use PATH_INFO if we can.
  62. if ( ! empty($_SERVER['PATH_INFO']))
  63. {
  64. $uri = $_SERVER['PATH_INFO'];
  65. }
  66. // Only use ORIG_PATH_INFO if it contains the path
  67. elseif ( ! empty($_SERVER['ORIG_PATH_INFO']) and ($path = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['ORIG_PATH_INFO'])) != '')
  68. {
  69. $uri = $path;
  70. }
  71. else
  72. {
  73. // Fall back to parsing the REQUEST URI
  74. if (isset($_SERVER['REQUEST_URI']))
  75. {
  76. $uri = $_SERVER['REQUEST_URI'];
  77. }
  78. else
  79. {
  80. throw new \FuelException('Unable to detect the URI.');
  81. }
  82. // Remove the base URL from the URI
  83. $base_url = parse_url(\Config::get('base_url'), PHP_URL_PATH);
  84. if ($uri != '' and strncmp($uri, $base_url, strlen($base_url)) === 0)
  85. {
  86. $uri = substr($uri, strlen($base_url));
  87. }
  88. // If we are using an index file (not mod_rewrite) then remove it
  89. $index_file = \Config::get('index_file');
  90. if ($index_file and strncmp($uri, $index_file, strlen($index_file)) === 0)
  91. {
  92. $uri = substr($uri, strlen($index_file));
  93. }
  94. // When index.php? is used and the config is set wrong, lets just
  95. // be nice and help them out.
  96. if ($index_file and strncmp($uri, '?/', 2) === 0)
  97. {
  98. $uri = substr($uri, 1);
  99. }
  100. // Lets split the URI up in case it contains a ?. This would
  101. // indicate the server requires 'index.php?' and that mod_rewrite
  102. // is not being used.
  103. preg_match('#(.*?)\?(.*)#i', $uri, $matches);
  104. // If there are matches then lets set set everything correctly
  105. if ( ! empty($matches))
  106. {
  107. $uri = $matches[1];
  108. $_SERVER['QUERY_STRING'] = $matches[2];
  109. parse_str($matches[2], $_GET);
  110. }
  111. }
  112. // Strip the defined url suffix from the uri if needed
  113. $ext = \Config::get('url_suffix');
  114. strrchr($uri, '.') === $ext and $uri = substr($uri,0,-strlen($ext));
  115. // Do some final clean up of the uri
  116. static::$detected_uri = \Security::clean_uri($uri, true);
  117. return static::$detected_uri;
  118. }
  119. /**
  120. * Get the public ip address of the user.
  121. *
  122. * @return string
  123. */
  124. public static function ip($default = '0.0.0.0')
  125. {
  126. if (static::server('REMOTE_ADDR') !== null)
  127. {
  128. return static::server('REMOTE_ADDR');
  129. }
  130. else
  131. {
  132. // detection failed, return the default
  133. return \Fuel::value($default);
  134. }
  135. }
  136. /**
  137. * Get the real ip address of the user. Even if they are using a proxy.
  138. *
  139. * @return string the real ip address of the user
  140. */
  141. public static function real_ip($default = '0.0.0.0')
  142. {
  143. if (static::server('HTTP_X_CLUSTER_CLIENT_IP') !== null)
  144. {
  145. return static::server('HTTP_X_CLUSTER_CLIENT_IP');
  146. }
  147. if (static::server('HTTP_X_FORWARDED_FOR') !== null)
  148. {
  149. return static::server('HTTP_X_FORWARDED_FOR');
  150. }
  151. if (static::server('HTTP_CLIENT_IP') !== null)
  152. {
  153. return static::server('HTTP_CLIENT_IP');
  154. }
  155. if (static::server('REMOTE_ADDR') !== null)
  156. {
  157. return static::server('REMOTE_ADDR');
  158. }
  159. // detection failed, return the default
  160. return \Fuel::value($default);
  161. }
  162. /**
  163. * Return's the protocol that the request was made with
  164. *
  165. * @return string
  166. */
  167. public static function protocol()
  168. {
  169. if ((static::server('HTTPS') !== null and static::server('HTTPS') != 'off')
  170. or (static::server('HTTPS') === null and static::server('SERVER_PORT') == 443))
  171. {
  172. return 'https';
  173. }
  174. return 'http';
  175. }
  176. /**
  177. * Return's whether this is an AJAX request or not
  178. *
  179. * @return bool
  180. */
  181. public static function is_ajax()
  182. {
  183. return (static::server('HTTP_X_REQUESTED_WITH') !== null) and strtolower(static::server('HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest';
  184. }
  185. /**
  186. * Return's the referrer
  187. *
  188. * @return string
  189. */
  190. public static function referrer($default = '')
  191. {
  192. return static::server('HTTP_REFERER', $default);
  193. }
  194. /**
  195. * Return's the input method used (GET, POST, DELETE, etc.)
  196. *
  197. * @return string
  198. */
  199. public static function method($default = 'GET')
  200. {
  201. return static::server('REQUEST_METHOD', $default);
  202. }
  203. /**
  204. * Return's the user agent
  205. *
  206. * @return string
  207. */
  208. public static function user_agent($default = '')
  209. {
  210. return static::server('HTTP_USER_AGENT', $default);
  211. }
  212. /**
  213. * Returns all of the GET, POST, PUT and DELETE variables.
  214. *
  215. * @return array
  216. */
  217. public static function all()
  218. {
  219. if (is_null(static::$input))
  220. {
  221. static::hydrate();
  222. }
  223. return static::$input;
  224. }
  225. /**
  226. * Gets the specified GET variable.
  227. *
  228. * @param string $index The index to get
  229. * @param string $default The default value
  230. * @return void
  231. */
  232. public static function get($index = null, $default = null)
  233. {
  234. return (is_null($index) and func_num_args() === 0) ? $_GET : \Arr::get($_GET, $index, $default);
  235. }
  236. /**
  237. * Fetch an item from the POST array
  238. *
  239. * @param string The index key
  240. * @param mixed The default value
  241. * @return string|array
  242. */
  243. public static function post($index = null, $default = null)
  244. {
  245. return (is_null($index) and func_num_args() === 0) ? $_POST : \Arr::get($_POST, $index, $default);
  246. }
  247. /**
  248. * Fetch an item from the php://input for put arguments
  249. *
  250. * @param string The index key
  251. * @param mixed The default value
  252. * @return string|array
  253. */
  254. public static function put($index = null, $default = null)
  255. {
  256. if (is_null(static::$put_delete))
  257. {
  258. static::hydrate();
  259. }
  260. return (is_null($index) and func_num_args() === 0) ? static::$put_delete : \Arr::get(static::$put_delete, $index, $default);
  261. }
  262. /**
  263. * Fetch an item from the php://input for delete arguments
  264. *
  265. * @param string The index key
  266. * @param mixed The default value
  267. * @return string|array
  268. */
  269. public static function delete($index = null, $default = null)
  270. {
  271. if (is_null(static::$put_delete))
  272. {
  273. static::hydrate();
  274. }
  275. return (is_null($index) and func_num_args() === 0) ? static::$put_delete : \Arr::get(static::$put_delete, $index, $default);
  276. }
  277. /**
  278. * Fetch an item from the FILE array
  279. *
  280. * @param string The index key
  281. * @param mixed The default value
  282. * @return string|array
  283. */
  284. public static function file($index = null, $default = null)
  285. {
  286. return (is_null($index) and func_num_args() === 0) ? $_FILES : \Arr::get($_FILES, $index, $default);
  287. }
  288. /**
  289. * Fetch an item from either the GET, POST, PUT or DELETE array
  290. *
  291. * @param string The index key
  292. * @param mixed The default value
  293. * @return string|array
  294. */
  295. public static function param($index = null, $default = null)
  296. {
  297. if (is_null(static::$input))
  298. {
  299. static::hydrate();
  300. }
  301. return \Arr::get(static::$input, $index, $default);
  302. }
  303. /**
  304. * Fetch an item from either the GET array or the POST
  305. *
  306. * @param string The index key
  307. * @param mixed The default value
  308. * @return string|array
  309. * @deprecated until 1.2
  310. */
  311. public static function get_post($index = null, $default = null)
  312. {
  313. return static::param($index, $default);
  314. }
  315. /**
  316. * Fetch an item from the COOKIE array
  317. *
  318. * @param string The index key
  319. * @param mixed The default value
  320. * @return string|array
  321. */
  322. public static function cookie($index = null, $default = null)
  323. {
  324. return (is_null($index) and func_num_args() === 0) ? $_COOKIE : \Arr::get($_COOKIE, $index, $default);
  325. }
  326. /**
  327. * Fetch an item from the SERVER array
  328. *
  329. * @param string The index key
  330. * @param mixed The default value
  331. * @return string|array
  332. */
  333. public static function server($index = null, $default = null)
  334. {
  335. return (is_null($index) and func_num_args() === 0) ? $_SERVER : \Arr::get($_SERVER, strtoupper($index), $default);
  336. }
  337. /**
  338. * Hydrates the input array
  339. *
  340. * @return void
  341. */
  342. protected static function hydrate()
  343. {
  344. static::$input = array_merge($_GET, $_POST);
  345. if (\Input::method() == 'PUT' or \Input::method() == 'DELETE')
  346. {
  347. parse_str(file_get_contents('php://input'), static::$put_delete);
  348. static::$input = array_merge(static::$input, static::$put_delete);
  349. }
  350. }
  351. }