You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
4.9 KiB

7 years ago
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. class WB_Input extends CI_Input
  4. {
  5. /**
  6. * Fetch from array
  7. *
  8. * Internal method used to retrieve values from global arrays.
  9. *
  10. * @param array &$array $_GET, $_POST, $_COOKIE, $_SERVER, etc.
  11. * @param mixed $index Index for item to be fetched from $array
  12. * @param bool $xss_clean Whether to apply XSS filtering
  13. * @return mixed
  14. */
  15. protected function _fetch_from_array(&$array, $index = NULL, $xss_clean = NULL, $default_value =NULL)
  16. {
  17. is_bool($xss_clean) OR $xss_clean = $this->_enable_xss;
  18. // If $index is NULL, it means that the whole $array is requested
  19. isset($index) OR $index = array_keys($array);
  20. // allow fetching multiple keys at once
  21. if (is_array($index))
  22. {
  23. $output = array();
  24. foreach ($index as $key)
  25. {
  26. $output[$key] = $this->_fetch_from_array($array, $key, $xss_clean);
  27. }
  28. return $output;
  29. }
  30. if (isset($array[$index]))
  31. {
  32. $value = $array[$index];
  33. }
  34. elseif (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) // Does the index contain array notation
  35. {
  36. $value = $array;
  37. for ($i = 0; $i < $count; $i++)
  38. {
  39. $key = trim($matches[0][$i], '[]');
  40. if ($key === '') // Empty notation will return the value as array
  41. {
  42. break;
  43. }
  44. if (isset($value[$key]))
  45. {
  46. $value = $value[$key];
  47. }
  48. else
  49. {
  50. return $default_value;
  51. }
  52. }
  53. }
  54. else
  55. {
  56. return $default_value;
  57. }
  58. return ($xss_clean === TRUE)
  59. ? $this->security->xss_clean($value)
  60. : $value;
  61. }
  62. // --------------------------------------------------------------------
  63. /**
  64. * Fetch an item from the GET array
  65. *
  66. * @param mixed $index Index for item to be fetched from $_GET
  67. * @param bool $xss_clean Whether to apply XSS filtering
  68. * @return mixed
  69. */
  70. public function get($index = NULL, $xss_clean = NULL, $default_value = NULL)
  71. {
  72. return $this->_fetch_from_array($_GET, $index, $xss_clean, $default_value);
  73. }
  74. // --------------------------------------------------------------------
  75. /**
  76. * Fetch an item from the POST array
  77. *
  78. * @param mixed $index Index for item to be fetched from $_POST
  79. * @param bool $xss_clean Whether to apply XSS filtering
  80. * @return mixed
  81. */
  82. public function post($index = NULL, $xss_clean = NULL, $default_value = NULL)
  83. {
  84. return $this->_fetch_from_array($_POST, $index, $xss_clean, $default_value);
  85. }
  86. // --------------------------------------------------------------------
  87. /**
  88. * Fetch an item from POST data with fallback to GET
  89. *
  90. * @param string $index Index for item to be fetched from $_POST or $_GET
  91. * @param bool $xss_clean Whether to apply XSS filtering
  92. * @return mixed
  93. */
  94. public function post_get($index, $xss_clean = NULL, $default_value = NULL)
  95. {
  96. return isset($_POST[$index])
  97. ? $this->post($index, $xss_clean, $default_value)
  98. : $this->get($index, $xss_clean, $default_value);
  99. }
  100. // --------------------------------------------------------------------
  101. /**
  102. * Fetch an item from GET data with fallback to POST
  103. *
  104. * @param string $index Index for item to be fetched from $_GET or $_POST
  105. * @param bool $xss_clean Whether to apply XSS filtering
  106. * @return mixed
  107. */
  108. public function get_post($index, $xss_clean = NULL, $default_value = NULL)
  109. {
  110. return isset($_GET[$index])
  111. ? $this->get($index, $xss_clean, $default_value)
  112. : $this->post($index, $xss_clean, $default_value);
  113. }
  114. // --------------------------------------------------------------------
  115. /**
  116. * Fetch an item from the COOKIE array
  117. *
  118. * @param mixed $index Index for item to be fetched from $_COOKIE
  119. * @param bool $xss_clean Whether to apply XSS filtering
  120. * @return mixed
  121. */
  122. public function cookie($index = NULL, $xss_clean = NULL, $default_value = NULL)
  123. {
  124. return $this->_fetch_from_array($_COOKIE, $index, $xss_clean, $default_value);
  125. }
  126. // --------------------------------------------------------------------
  127. /**
  128. * Fetch an item from the SERVER array
  129. *
  130. * @param mixed $index Index for item to be fetched from $_SERVER
  131. * @param bool $xss_clean Whether to apply XSS filtering
  132. * @return mixed
  133. */
  134. public function server($index, $xss_clean = NULL, $default_value = NULL)
  135. {
  136. return $this->_fetch_from_array($_SERVER, $index, $xss_clean, $default_value);
  137. }
  138. }