summaryrefslogtreecommitdiffstats
path: root/system
diff options
context:
space:
mode:
authornisheeth-barthwal <nisheeth.barthwal@nbaztec.co.in>2013-03-21 11:18:10 +0100
committernisheeth-barthwal <nisheeth.barthwal@nbaztec.co.in>2013-03-21 11:18:10 +0100
commita7447d205296eeead94617f4b66707e336547b51 (patch)
treeef73cd6e834cc732bf706aad2b8a6a60988983f5 /system
parent76f42d9dcd42bf90c407f151d04ef207c8deebdf (diff)
Added array notation for keys in Input library
Diffstat (limited to 'system')
-rw-r--r--system/core/Input.php74
-rw-r--r--system/helpers/form_helper.php44
-rw-r--r--system/libraries/Form_validation.php15
3 files changed, 112 insertions, 21 deletions
diff --git a/system/core/Input.php b/system/core/Input.php
index 8d491e055..ffe7b4d27 100644
--- a/system/core/Input.php
+++ b/system/core/Input.php
@@ -149,21 +149,59 @@ class CI_Input {
* @param array &$array $_GET, $_POST, $_COOKIE, $_SERVER, etc.
* @param string $index Index for item to be fetched from $array
* @param bool $xss_clean Whether to apply XSS filtering
+ * @param bool $recurse Whether to recurse into arrays via nested keys
* @return mixed
*/
- protected function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE)
+ protected function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE, $recurse = FALSE)
{
- if ( ! isset($array[$index]))
+ $value = NULL;
+
+ if (isset($array[$index]))
{
- return NULL;
+ $value = $array[$index];
+ }
+ else if($recurse)
+ {
+ // We couldn't find the $field as a simple key, so try the nested notation
+ $key = $index;
+ $container = $array;
+
+ // Test if the $index is an array name, and try to obtain the final index
+ if (preg_match_all('/\[(.*?)\]/', $index, $matches))
+ {
+ sscanf($index, '%[^[][', $key);
+ for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
+ {
+ if($matches[1][$i] === '') // The array notation will return the value as array
+ {
+ break;
+ }
+ if (isset($container[$key]))
+ {
+ $container = $container[$key];
+ $key = $matches[1][$i];
+ }
+ else
+ {
+ $container = array();
+ break;
+ }
+ }
+
+ // Check if the deepest container has the field
+ if(isset($container[$key]))
+ {
+ $value = $container[$key];
+ }
+ }
}
if ($xss_clean === TRUE)
{
- return $this->security->xss_clean($array[$index]);
+ return $this->security->xss_clean($value);
}
- return $array[$index];
+ return $value;
}
// --------------------------------------------------------------------
@@ -173,9 +211,10 @@ class CI_Input {
*
* @param string $index Index for item to be fetched from $_GET
* @param bool $xss_clean Whether to apply XSS filtering
+ * @param bool $recurse Whether to recurse into arrays via nested keys
* @return mixed
*/
- public function get($index = NULL, $xss_clean = FALSE)
+ public function get($index = NULL, $xss_clean = FALSE, $recurse = FALSE)
{
// Check if a field has been provided
if ($index === NULL)
@@ -190,12 +229,12 @@ class CI_Input {
// loop through the full _GET array
foreach (array_keys($_GET) as $key)
{
- $get[$key] = $this->_fetch_from_array($_GET, $key, $xss_clean);
+ $get[$key] = $this->_fetch_from_array($_GET, $key, $xss_clean, $recurse);
}
return $get;
}
- return $this->_fetch_from_array($_GET, $index, $xss_clean);
+ return $this->_fetch_from_array($_GET, $index, $xss_clean, $recurse);
}
// --------------------------------------------------------------------
@@ -205,9 +244,10 @@ class CI_Input {
*
* @param string $index Index for item to be fetched from $_POST
* @param bool $xss_clean Whether to apply XSS filtering
+ * @param bool $recurse Whether to recurse into arrays via nested keys
* @return mixed
*/
- public function post($index = NULL, $xss_clean = FALSE)
+ public function post($index = NULL, $xss_clean = FALSE, $recurse = FALSE)
{
// Check if a field has been provided
if ($index === NULL)
@@ -222,12 +262,12 @@ class CI_Input {
// Loop through the full _POST array and return it
foreach (array_keys($_POST) as $key)
{
- $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
+ $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean, $recurse);
}
return $post;
}
- return $this->_fetch_from_array($_POST, $index, $xss_clean);
+ return $this->_fetch_from_array($_POST, $index, $xss_clean, $recurse);
}
// --------------------------------------------------------------------
@@ -237,13 +277,14 @@ class CI_Input {
*
* @param string $index Index for item to be fetched from $_POST or $_GET
* @param bool $xss_clean Whether to apply XSS filtering
+ * @param bool $recurse Whether to recurse into arrays via nested keys
* @return mixed
*/
- public function get_post($index = '', $xss_clean = FALSE)
+ public function get_post($index = '', $xss_clean = FALSE, $recurse = FALSE)
{
return isset($_POST[$index])
- ? $this->post($index, $xss_clean)
- : $this->get($index, $xss_clean);
+ ? $this->post($index, $xss_clean, $recurse)
+ : $this->get($index, $xss_clean, $recurse);
}
// --------------------------------------------------------------------
@@ -253,11 +294,12 @@ class CI_Input {
*
* @param string $index Index for item to be fetched from $_COOKIE
* @param bool $xss_clean Whether to apply XSS filtering
+ * @param bool $recurse Whether to recurse into arrays via nested keys
* @return mixed
*/
- public function cookie($index = '', $xss_clean = FALSE)
+ public function cookie($index = '', $xss_clean = FALSE, $recurse = FALSE)
{
- return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
+ return $this->_fetch_from_array($_COOKIE, $index, $xss_clean, $recurse);
}
// --------------------------------------------------------------------
diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php
index 692909c79..d2c22b05c 100644
--- a/system/helpers/form_helper.php
+++ b/system/helpers/form_helper.php
@@ -642,14 +642,17 @@ if ( ! function_exists('set_value'))
*/
function set_value($field = '', $default = '', $is_textarea = FALSE)
{
- if (FALSE === ($OBJ =& _get_validation_object()))
+ if (FALSE !== ($OBJ =& _get_validation_object()) && $OBJ->has_rule($field))
+ {
+ return form_prep($OBJ->set_value($field, $default), $is_textarea);
+ }
+
+ if (FALSE !== ($OBJ =& _get_input_object()) && ($value = $OBJ->post($field, FALSE, TRUE)))
{
- return isset($_POST[$field])
- ? form_prep($_POST[$field], $is_textarea)
- : form_prep($default, $is_textarea);
+ return form_prep($value, $is_textarea);
}
- return form_prep($OBJ->set_value($field, $default), $is_textarea);
+ return form_prep($default, $is_textarea);
}
}
@@ -1004,5 +1007,36 @@ if ( ! function_exists('_get_validation_object'))
}
}
+// ------------------------------------------------------------------------
+
+if ( ! function_exists('_get_input_object'))
+{
+ /**
+ * Input Object
+ *
+ * Fetches the input object
+ *
+ * @return mixed
+ */
+ function &_get_input_object()
+ {
+ $CI =& get_instance();
+
+ // We set this as a variable since we're returning by reference.
+ $return = FALSE;
+
+ if ( ! isset($CI->input) OR ! is_object($CI->input))
+ {
+ return $return;
+ }
+ else
+ {
+ $return = $CI->input;
+ }
+
+ return $return;
+ }
+}
+
/* End of file form_helper.php */
/* Location: ./system/helpers/form_helper.php */ \ No newline at end of file
diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php
index 172e799f6..1ed50844c 100644
--- a/system/libraries/Form_validation.php
+++ b/system/libraries/Form_validation.php
@@ -836,6 +836,21 @@ class CI_Form_validation {
// --------------------------------------------------------------------
/**
+ * Checks if the rule is present within the validator
+ *
+ * Permits you to check if a rule is present within the validator
+ *
+ * @param string the field name
+ * @return bool
+ */
+ public function has_rule($field)
+ {
+ return isset($this->_field_data[$field]);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Get the value from a form
*
* Permits you to repopulate a form field with the value it was submitted