From a7447d205296eeead94617f4b66707e336547b51 Mon Sep 17 00:00:00 2001 From: nisheeth-barthwal Date: Thu, 21 Mar 2013 15:48:10 +0530 Subject: Added array notation for keys in Input library --- system/core/Input.php | 74 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 16 deletions(-) (limited to 'system/core') 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); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From a5bcfb1d291d42521b0dc420b1b501c36710277d Mon Sep 17 00:00:00 2001 From: nisheeth-barthwal Date: Sat, 23 Mar 2013 10:53:51 +0530 Subject: Removed $recurse parameter in lieu of auto parsing. Changed "provision" entry. --- system/core/Input.php | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) (limited to 'system/core') diff --git a/system/core/Input.php b/system/core/Input.php index ffe7b4d27..7424a003a 100644 --- a/system/core/Input.php +++ b/system/core/Input.php @@ -149,10 +149,9 @@ 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, $recurse = FALSE) + protected function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE) { $value = NULL; @@ -160,9 +159,8 @@ class CI_Input { { $value = $array[$index]; } - else if($recurse) + else if(preg_match('/\[[^]]*\]$/', $index)) // Does the index contain array notation { - // We couldn't find the $field as a simple key, so try the nested notation $key = $index; $container = $array; @@ -211,10 +209,9 @@ 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, $recurse = FALSE) + public function get($index = NULL, $xss_clean = FALSE) { // Check if a field has been provided if ($index === NULL) @@ -229,12 +226,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, $recurse); + $get[$key] = $this->_fetch_from_array($_GET, $key, $xss_clean); } return $get; } - return $this->_fetch_from_array($_GET, $index, $xss_clean, $recurse); + return $this->_fetch_from_array($_GET, $index, $xss_clean); } // -------------------------------------------------------------------- @@ -244,10 +241,9 @@ 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, $recurse = FALSE) + public function post($index = NULL, $xss_clean = FALSE) { // Check if a field has been provided if ($index === NULL) @@ -262,12 +258,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, $recurse); + $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean); } return $post; } - return $this->_fetch_from_array($_POST, $index, $xss_clean, $recurse); + return $this->_fetch_from_array($_POST, $index, $xss_clean); } // -------------------------------------------------------------------- @@ -277,14 +273,13 @@ 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, $recurse = FALSE) + public function get_post($index = '', $xss_clean = FALSE) { return isset($_POST[$index]) - ? $this->post($index, $xss_clean, $recurse) - : $this->get($index, $xss_clean, $recurse); + ? $this->post($index, $xss_clean) + : $this->get($index, $xss_clean); } // -------------------------------------------------------------------- @@ -294,12 +289,11 @@ 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, $recurse = FALSE) + public function cookie($index = '', $xss_clean = FALSE) { - return $this->_fetch_from_array($_COOKIE, $index, $xss_clean, $recurse); + return $this->_fetch_from_array($_COOKIE, $index, $xss_clean); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 7b1a2f1f40d940dde34e47b36808a84e0353af56 Mon Sep 17 00:00:00 2001 From: nisheeth-barthwal Date: Mon, 25 Mar 2013 11:29:53 +0530 Subject: Changed "else if" to "elseif" --- system/core/Input.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/core') diff --git a/system/core/Input.php b/system/core/Input.php index 7424a003a..6ee132005 100644 --- a/system/core/Input.php +++ b/system/core/Input.php @@ -159,7 +159,7 @@ class CI_Input { { $value = $array[$index]; } - else if(preg_match('/\[[^]]*\]$/', $index)) // Does the index contain array notation + elseif(preg_match('/\[[^]]*\]$/', $index)) // Does the index contain array notation { $key = $index; $container = $array; -- cgit v1.2.3-24-g4f1b From 77236e055234cbbc9f6ca6be472c70077a1f5856 Mon Sep 17 00:00:00 2001 From: nisheeth-barthwal Date: Mon, 25 Mar 2013 23:42:36 +0530 Subject: Simplified notation parsing and other cosmetic fixes --- system/core/Input.php | 47 ++++++++++++++++------------------------------- 1 file changed, 16 insertions(+), 31 deletions(-) (limited to 'system/core') diff --git a/system/core/Input.php b/system/core/Input.php index 6ee132005..d707fe25c 100644 --- a/system/core/Input.php +++ b/system/core/Input.php @@ -153,53 +153,38 @@ class CI_Input { */ protected function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE) { - $value = NULL; - if (isset($array[$index])) { $value = $array[$index]; } - elseif(preg_match('/\[[^]]*\]$/', $index)) // Does the index contain array notation + elseif (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) // Does the index contain array 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)) + for ($i = 0; $i < $count; $i++) { - sscanf($index, '%[^[][', $key); - for ($i = 0, $c = count($matches[0]); $i < $c; $i++) + $key = trim($matches[0][$i], '[]'); + if($key === '') // The array notation will return the value as array { - 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; - } + break; } - - // Check if the deepest container has the field - if(isset($container[$key])) + if (isset($container[$key])) + { + $value = $container = $container[$key]; + } + else { - $value = $container[$key]; + return NULL; } } } - - if ($xss_clean === TRUE) + else { - return $this->security->xss_clean($value); + return NULL; } - return $value; + return ($xss_clean === TRUE) + ? $this->security->xss_clean($value) + : $value; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 47ea5a8b99e17e9513be57d0af92f9e2637569b2 Mon Sep 17 00:00:00 2001 From: nisheeth-barthwal Date: Tue, 26 Mar 2013 18:57:28 +0530 Subject: Code fixes in line with suggestions --- system/core/Input.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'system/core') diff --git a/system/core/Input.php b/system/core/Input.php index d707fe25c..1e21886ff 100644 --- a/system/core/Input.php +++ b/system/core/Input.php @@ -157,19 +157,20 @@ class CI_Input { { $value = $array[$index]; } - elseif (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) // Does the index contain array notation + elseif (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) // Does the index contain array notation { - $container = $array; + $value = $array; for ($i = 0; $i < $count; $i++) { $key = trim($matches[0][$i], '[]'); - if($key === '') // The array notation will return the value as array + if($key === '') // Empty notation will return the value as array { break; } - if (isset($container[$key])) + + if (isset($value[$key])) { - $value = $container = $container[$key]; + $value = $value[$key]; } else { -- cgit v1.2.3-24-g4f1b From 408cbb4f3582ac64bb534a6539370992071d5950 Mon Sep 17 00:00:00 2001 From: nisheeth-barthwal Date: Tue, 26 Mar 2013 19:06:40 +0530 Subject: Code style fix --- system/core/Input.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/core') diff --git a/system/core/Input.php b/system/core/Input.php index 1e21886ff..6690b7f2e 100644 --- a/system/core/Input.php +++ b/system/core/Input.php @@ -163,7 +163,7 @@ class CI_Input { for ($i = 0; $i < $count; $i++) { $key = trim($matches[0][$i], '[]'); - if($key === '') // Empty notation will return the value as array + if ($key === '') // Empty notation will return the value as array { break; } -- cgit v1.2.3-24-g4f1b