PageRenderTime 33ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/helpers/core/input.php

https://github.com/megabr/obullo
PHP | 520 lines | 300 code | 54 blank | 166 comment | 46 complexity | f05b18c5ec192febc9333b2848ab2e0c MD5 | raw file
  1. <?php
  2. defined('BASE') or exit('Access Denied!');
  3. /**
  4. * Obullo Framework (c) 2009.
  5. *
  6. * PHP5 MVC Based Minimalist Software.
  7. *
  8. * @package obullo
  9. * @author obullo.com
  10. * @copyright Ersin Guvenc (c) 2010.
  11. * @since Version 1.0
  12. * @filesource
  13. * @license
  14. */
  15. Class InputException extends CommonException {}
  16. // ------------------------------------------------------------------------
  17. if( ! isset($_ob->input)) // Helper Constructor
  18. {
  19. $_ob = base_register('Storage');
  20. $_ob->input = new stdClass();
  21. $_ob->input->use_xss_clean = FALSE;
  22. $_ob->input->ip_address = FALSE;
  23. $_ob->input->user_agent = FALSE;
  24. $_ob->input->allow_get_array = FALSE;
  25. $_config = core_register('Config');
  26. $_ob->input->use_xss_clean = ($_config->item('global_xss_filtering') === TRUE) ? TRUE : FALSE;
  27. $_ob->input->allow_get_array = ($_config->item('enable_query_strings') === TRUE) ? TRUE : FALSE;
  28. log_me('debug', "Input Helper Initialized");
  29. }
  30. /**
  31. * Sanitize Globals
  32. * This function does the following:
  33. * Unsets $_GET data (if query strings are not enabled)
  34. * Unsets all globals if register_globals is enabled.
  35. *
  36. * Standardizes newline characters to \n
  37. *
  38. * @access private
  39. * @return void
  40. */
  41. if( ! function_exists('_sanitize_globals') )
  42. {
  43. function _sanitize_globals()
  44. {
  45. $_ob = base_register('Storage');
  46. // Would kind of be "wrong" to unset any of these GLOBALS
  47. $protected = array('_SERVER', '_GET', '_POST', '_FILES', '_REQUEST', '_SESSION', '_ENV', '_controller',
  48. 'GLOBALS', 'HTTP_RAW_POST_DATA');
  49. // Unset globals for security.
  50. // This is effectively the same as register_globals = off
  51. foreach (array($_GET, $_POST, $_COOKIE, $_SERVER, $_FILES, $_ENV, (isset($_SESSION) && is_array($_SESSION)) ? $_SESSION : array()) as $global)
  52. {
  53. if ( ! is_array($global))
  54. {
  55. if ( ! in_array($global, $protected))
  56. {
  57. unset($GLOBALS[$global]);
  58. }
  59. }
  60. else
  61. {
  62. foreach ($global as $key => $val)
  63. {
  64. if ( ! in_array($key, $protected))
  65. {
  66. unset($GLOBALS[$key]);
  67. }
  68. if (is_array($val))
  69. {
  70. foreach($val as $k => $v)
  71. {
  72. if ( ! in_array($k, $protected))
  73. {
  74. unset($GLOBALS[$k]);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. }
  81. // Is $_GET data allowed? If not we'll set the $_GET to an empty array
  82. if ($_ob->input->allow_get_array == FALSE)
  83. {
  84. $_GET = array();
  85. }
  86. else
  87. {
  88. $_GET = _clean_input_data($_GET);
  89. }
  90. // Clean $_POST Data
  91. $_POST = _clean_input_data($_POST);
  92. // Clean $_COOKIE Data
  93. // Also get rid of specially treated cookies that might be set by a server
  94. // or silly application, that are of no use to a OB application anyway
  95. // but that when present will trip our 'Disallowed Key Characters' alarm
  96. // http://www.ietf.org/rfc/rfc2109.txt
  97. // note that the key names below are single quoted strings, and are not PHP variables
  98. unset($_COOKIE['$Version']);
  99. unset($_COOKIE['$Path']);
  100. unset($_COOKIE['$Domain']);
  101. $_COOKIE = _clean_input_data($_COOKIE);
  102. log_me('debug', "Global POST and COOKIE data sanitized");
  103. }
  104. }
  105. // ------------------------------------------------------------------------
  106. /**
  107. * Clean Input Data
  108. *
  109. * This is a helper function. It escapes data and
  110. * standardizes newline characters to \n
  111. *
  112. * @access private
  113. * @param string
  114. * @return string
  115. */
  116. if( ! function_exists('_clean_input_data') )
  117. {
  118. function _clean_input_data($str)
  119. {
  120. $_ob = base_register('Storage');
  121. if (is_array($str))
  122. {
  123. $new_array = array();
  124. foreach ($str as $key => $val)
  125. {
  126. $new_array[_clean_input_keys($key)] = _clean_input_data($val);
  127. }
  128. return $new_array;
  129. }
  130. // We strip slashes if magic quotes is on to keep things consistent
  131. if (get_magic_quotes_gpc())
  132. {
  133. $str = stripslashes($str);
  134. }
  135. // Should we filter the input data?
  136. if ($_ob->input->use_xss_clean === TRUE)
  137. {
  138. loader::helper('ob/security');
  139. $str = xss_clean($str);
  140. }
  141. // Standardize newlines
  142. if (strpos($str, "\r") !== FALSE)
  143. {
  144. $str = str_replace(array("\r\n", "\r"), "\n", $str);
  145. }
  146. return $str;
  147. }
  148. }
  149. // ------------------------------------------------------------------------
  150. /**
  151. * Clean Keys
  152. *
  153. * This is a helper function. To prevent malicious users
  154. * from trying to exploit keys we make sure that keys are
  155. * only named with alpha-numeric text and a few other items.
  156. *
  157. * @access private
  158. * @param string
  159. * @return string
  160. */
  161. if( ! function_exists('_clean_input_keys') )
  162. {
  163. function _clean_input_keys($str)
  164. {
  165. if ( ! preg_match("/^[a-z0-9:_\/-]+$/i", $str))
  166. {
  167. exit('Disallowed Key Characters.');
  168. }
  169. return $str;
  170. }
  171. }
  172. // --------------------------------------------------------------------
  173. /**
  174. * Fetch from array
  175. *
  176. * This is a helper function to retrieve values from global arrays
  177. *
  178. * @access public
  179. * @param array
  180. * @param string
  181. * @param bool
  182. * @return string
  183. */
  184. if( ! function_exists('_fetch_from_array') )
  185. {
  186. function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE)
  187. {
  188. if ( ! isset($array[$index]))
  189. {
  190. return FALSE;
  191. }
  192. if ($xss_clean === TRUE)
  193. {
  194. loader::helper('ob/security');
  195. return xss_clean($array[$index]);
  196. }
  197. return $array[$index];
  198. }
  199. }
  200. // --------------------------------------------------------------------
  201. /**
  202. * Fetch an item from the GET array
  203. *
  204. * @access public
  205. * @param string
  206. * @param bool
  207. * @return string
  208. */
  209. if( ! function_exists('i_get') )
  210. {
  211. function i_get($index = '', $xss_clean = FALSE)
  212. {
  213. return _fetch_from_array($_GET, $index, $xss_clean);
  214. }
  215. }
  216. // --------------------------------------------------------------------
  217. /**
  218. * Fetch an item from the POST array
  219. *
  220. * @access public
  221. * @param string
  222. * @param bool
  223. * @return string
  224. */
  225. if( ! function_exists('i_post') )
  226. {
  227. function i_post($index = '', $xss_clean = FALSE)
  228. {
  229. return _fetch_from_array($_POST, $index, $xss_clean);
  230. }
  231. }
  232. // --------------------------------------------------------------------
  233. /**
  234. * Fetch an item from the REQUEST array
  235. *
  236. * @access public
  237. * @param string
  238. * @param bool
  239. * @return string
  240. */
  241. if( ! function_exists('i_request') )
  242. {
  243. function i_request($index = '', $xss_clean = FALSE)
  244. {
  245. return _fetch_from_array($_REQUEST, $index, $xss_clean);
  246. }
  247. }
  248. // --------------------------------------------------------------------
  249. /**
  250. * Fetch an item from either the GET array or the POST
  251. *
  252. * @access public
  253. * @param string The index key
  254. * @param bool XSS cleaning
  255. * @return string
  256. */
  257. if( ! function_exists('i_get_post') )
  258. {
  259. function i_get_post($index = '', $xss_clean = FALSE)
  260. {
  261. if ( ! isset($_POST[$index]) )
  262. {
  263. return i_get($index, $xss_clean);
  264. }
  265. else
  266. {
  267. return i_post($index, $xss_clean);
  268. }
  269. }
  270. }
  271. // --------------------------------------------------------------------
  272. /**
  273. * Fetch an item from the COOKIE array
  274. *
  275. * @access public
  276. * @param string
  277. * @param bool
  278. * @return string
  279. */
  280. if( ! function_exists('i_cookie') )
  281. {
  282. function i_cookie($index = '', $xss_clean = FALSE)
  283. {
  284. return _fetch_from_array($_COOKIE, $index, $xss_clean);
  285. }
  286. }
  287. // --------------------------------------------------------------------
  288. /**
  289. * Fetch an item from the SERVER array
  290. *
  291. * @access public
  292. * @param string
  293. * @param bool
  294. * @return string
  295. */
  296. if( ! function_exists('i_server') )
  297. {
  298. function i_server($index = '', $xss_clean = FALSE)
  299. {
  300. return _fetch_from_array($_SERVER, $index, $xss_clean);
  301. }
  302. }
  303. // --------------------------------------------------------------------
  304. /**
  305. * Fetch the IP Address
  306. *
  307. * @access public
  308. * @return string
  309. */
  310. if( ! function_exists('i_ip_address') )
  311. {
  312. function i_ip_address()
  313. {
  314. $_ob = base_register('Storage');
  315. if ($_ob->input->ip_address !== FALSE)
  316. {
  317. return $_ob->input->ip_address;
  318. }
  319. if (config_item('proxy_ips') != '' && i_server('HTTP_X_FORWARDED_FOR') && i_server('REMOTE_ADDR'))
  320. {
  321. $proxies = preg_split('/[\s,]/', config_item('proxy_ips'), -1, PREG_SPLIT_NO_EMPTY);
  322. $proxies = is_array($proxies) ? $proxies : array($proxies);
  323. $_ob->input->ip_address = in_array($_SERVER['REMOTE_ADDR'], $proxies) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
  324. }
  325. elseif (i_server('REMOTE_ADDR') AND i_server('HTTP_CLIENT_IP'))
  326. {
  327. $_ob->input->ip_address = $_SERVER['HTTP_CLIENT_IP'];
  328. }
  329. elseif (i_server('REMOTE_ADDR'))
  330. {
  331. $_ob->input->ip_address = $_SERVER['REMOTE_ADDR'];
  332. }
  333. elseif (i_server('HTTP_CLIENT_IP'))
  334. {
  335. $_ob->input->ip_address = $_SERVER['HTTP_CLIENT_IP'];
  336. }
  337. elseif (i_server('HTTP_X_FORWARDED_FOR'))
  338. {
  339. $_ob->input->ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
  340. }
  341. if ($_ob->input->ip_address === FALSE)
  342. {
  343. $_ob->input->ip_address = '0.0.0.0';
  344. return $_ob->input->ip_address;
  345. }
  346. if (strstr($_ob->input->ip_address, ','))
  347. {
  348. $x = explode(',', $_ob->input->ip_address);
  349. $_ob->input->ip_address = trim(end($x));
  350. }
  351. if ( ! i_valid_ip($_ob->input->ip_address))
  352. {
  353. $_ob->input->ip_address = '0.0.0.0';
  354. }
  355. return $_ob->input->ip_address;
  356. }
  357. }
  358. // --------------------------------------------------------------------
  359. /**
  360. * Validate IP Address
  361. *
  362. * Updated version suggested by Geert De Deckere
  363. *
  364. * @access public
  365. * @param string
  366. * @return string
  367. */
  368. if( ! function_exists('i_valid_ip') )
  369. {
  370. function i_valid_ip($ip)
  371. {
  372. $ip_segments = explode('.', $ip);
  373. // Always 4 segments needed
  374. if (count($ip_segments) != 4)
  375. {
  376. return FALSE;
  377. }
  378. // IP can not start with 0
  379. if ($ip_segments[0][0] == '0')
  380. {
  381. return FALSE;
  382. }
  383. // Check each segment
  384. foreach ($ip_segments as $segment)
  385. {
  386. // IP segments must be digits and can not be
  387. // longer than 3 digits or greater then 255
  388. if ($segment == '' OR preg_match("/[^0-9]/", $segment) OR $segment > 255 OR strlen($segment) > 3)
  389. {
  390. return FALSE;
  391. }
  392. }
  393. return TRUE;
  394. }
  395. }
  396. // --------------------------------------------------------------------
  397. /**
  398. * User Agent
  399. *
  400. * @access public
  401. * @return string
  402. */
  403. if( ! function_exists('i_user_agent') )
  404. {
  405. function i_user_agent()
  406. {
  407. $_ob = base_register('Storage');
  408. if ($_ob->input->user_agent !== FALSE)
  409. {
  410. return $_ob->input->user_agent;
  411. }
  412. $_ob->input->user_agent = ( ! isset($_SERVER['HTTP_USER_AGENT'])) ? FALSE : $_SERVER['HTTP_USER_AGENT'];
  413. return $_ob->input->user_agent;
  414. }
  415. }
  416. // --------------------------------------------------------------------
  417. /**
  418. * Filename Security
  419. *
  420. * @access public
  421. * @param string
  422. * @return string
  423. */
  424. if( ! function_exists('i_filename_security') )
  425. {
  426. function i_filename_security($str)
  427. {
  428. $bad = array(
  429. "../",
  430. "./",
  431. "<!--",
  432. "-->",
  433. "<",
  434. ">",
  435. "'",
  436. '"',
  437. '&',
  438. '$',
  439. '#',
  440. '{',
  441. '}',
  442. '[',
  443. ']',
  444. '=',
  445. ';',
  446. '?',
  447. "%20",
  448. "%22",
  449. "%3c", // <
  450. "%253c", // <
  451. "%3e", // >
  452. "%0e", // >
  453. "%28", // (
  454. "%29", // )
  455. "%2528", // (
  456. "%26", // &
  457. "%24", // $
  458. "%3f", // ?
  459. "%3b", // ;
  460. "%3d" // =
  461. );
  462. return stripslashes(str_replace($bad, '', $str));
  463. }
  464. }
  465. /* End of file input.php */
  466. /* Location: ./obullo/helpers/core/input.php */