From 5e227994581f2325b4d10e90da6dadb113a4cde1 Mon Sep 17 00:00:00 2001 From: nisheeth-barthwal Date: Thu, 21 Feb 2013 13:09:29 +0530 Subject: Fix for issue #2236 --- system/helpers/form_helper.php | 33 ++++++++++++++++++++++++++++----- system/libraries/Form_validation.php | 15 +++++++++++++++ user_guide_src/source/changelog.rst | 4 +++- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index d6e3e85fa..200da8ac5 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -642,14 +642,37 @@ 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 isset($_POST[$field]) - ? form_prep($_POST[$field], $is_textarea) - : form_prep($default, $is_textarea); + return form_prep($OBJ->set_value($field, $default), $is_textarea); + } + + // We couldn't find the $field in validator, so try in $_POST array + $index = $field; + $container = $_POST; + + // Test if the $field is an array name, and try to obtain the final index + if (preg_match_all('/\[(.*?)\]/', $field, $matches)) + { + sscanf($field, '%[^[][', $index); + for ($i = 0, $c = count($matches[0]); $i < $c; $i++) + { + if (isset($container[$index]) && $matches[1][$i] !== '') + { + $container = $container[$index]; + $index = $matches[1][$i]; + } + else + { + $container = array(); + break; + } + } } - return form_prep($OBJ->set_value($field, $default), $is_textarea); + return isset($container[$index]) + ? form_prep($container[$index], $is_textarea) + : form_prep($default, $is_textarea); } } 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 @@ -835,6 +835,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 * diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index a5f560564..2c2da6aaa 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -244,6 +244,7 @@ Release Date: Not Released - Added support for named parameters in error messages. - :doc:`Language ` line keys must now be prefixed with **form_validation_**. - Added rule **alpha_numeric_spaces**. + - Added method ``has_rule()`` to determine if a rule exists. - Added support for setting :doc:`Table ` class defaults in a config file. - :doc:`Caching Library ` changes include: - Added Wincache driver. @@ -463,7 +464,7 @@ Bug fixes for 3.0 - Fixed a bug (#1255) - :doc:`User Agent Library ` method ``is_referral()`` only checked if ``$_SERVER['HTTP_REFERER']`` exists. - Fixed a bug (#1146) - :doc:`Download Helper ` function ``force_download()`` incorrectly sent *Cache-Control* directives *pre-check* and *post-check* to Internet Explorer. - Fixed a bug (#1811) - :doc:`URI Library ` didn't properly cache segments for ``uri_to_assoc()`` and ``ruri_to_assoc()``. -- Fixed a bug (#1506) - :doc:`Form Helpers ` set empty *name* attributes. +- Fixed a bug (#1506) - :doc:`Form Helper ` set empty *name* attributes. - Fixed a bug (#59) - :doc:`Query Builder ` method ``count_all_results()`` ignored the DISTINCT clause. - Fixed a bug (#1624) - :doc:`Form Validation Library ` rule **matches** didn't property handle array field names. - Fixed a bug (#1630) - :doc:`Form Helper ` function ``set_value()`` didn't escape HTML entities. @@ -488,6 +489,7 @@ Bug fixes for 3.0 - Fixed a bug (#2211) - :doc:`Migration Library ` extensions couldn't execute ``CI_Migration::__construct()``. - Fixed a bug (#2255) where ``smtp_timeout`` was not being applied to read and writes for the socket. - Fixed a bug (#2239) of missing subject when using ``bcc_batch_mode``. +- Fixed a bug (#2236) - :doc:`Form Helper ` incorrectly determined the value to return in method ``set_value()``. Version 2.1.3 ============= -- cgit v1.2.3-24-g4f1b From 7d10006a0001ff0e7d82ec994b1e9cbb63683ffc Mon Sep 17 00:00:00 2001 From: nisheeth-barthwal Date: Mon, 4 Mar 2013 17:20:12 +0530 Subject: Fixed #2289 --- system/libraries/Email.php | 2 +- user_guide_src/source/changelog.rst | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/system/libraries/Email.php b/system/libraries/Email.php index daa38484b..728b637a3 100644 --- a/system/libraries/Email.php +++ b/system/libraries/Email.php @@ -2017,7 +2017,7 @@ class CI_Email { $reply = $this->_get_smtp_data(); - if (strpos($reply, '503') !== 0) // Already authenticated + if (strpos($reply, '503') === 0) // Already authenticated { return TRUE; } diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 07dae1fdc..d805c8f10 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -488,6 +488,7 @@ Bug fixes for 3.0 - Fixed a bug (#2239) - :doc:`Email Library ` improperly handled the Subject when used with ``bcc_batch_mode`` resulting in E_WARNING messages and an empty Subject. - Fixed a bug (#2234) - :doc:`Query Builder ` didn't reset JOIN cache for write-type queries. - Fixed a bug (#2298) - :doc:`Database Results ` method `next_row()` kept returning the last row, allowing for infinite loops. +- Fixed a bug (#2289) - :doc:`Email Library ` method `_smtp_authenticate()` returned prematurely from authentication due to opposite condition check. Version 2.1.3 ============= -- cgit v1.2.3-24-g4f1b 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 ++++++++++++++++++++++++++++-------- system/helpers/form_helper.php | 44 ++++++++++++++++++--- system/libraries/Form_validation.php | 15 ++++++++ user_guide_src/source/changelog.rst | 2 + 4 files changed, 114 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 @@ -835,6 +835,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 * diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index a9c420af1..33fc8fa9e 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -304,6 +304,7 @@ Release Date: Not Released - Changed method ``valid_ip()`` to use PHP's native ``filter_var()`` function. - Changed internal method ``_sanitize_globals()`` to skip enforcing reversal of *register_globals* in PHP 5.4+, where this functionality no longer exists. - Changed methods ``get()``, ``post()``, ``get_post()``, ``cookie()``, ``server()``, ``user_agent()`` to return NULL instead of FALSE when no value is found. + - Added provision for using array notation for keys. - :doc:`Common functions ` changes include: - Added function :php:func:`get_mimes()` to return the *application/config/mimes.php* array. - Added support for HTTP code 303 ("See Other") in :php:func:`set_status_header()`. @@ -489,6 +490,7 @@ Bug fixes for 3.0 - Fixed a bug (#2234) - :doc:`Query Builder ` didn't reset JOIN cache for write-type queries. - Fixed a bug (#2298) - :doc:`Database Results ` method `next_row()` kept returning the last row, allowing for infinite loops. - Fixed a bug (#2289) - :doc:`Email Library ` method `_smtp_authenticate()` returned prematurely from authentication due to opposite condition check. +- Fixed a bug (#2236) - :doc:`Form Helper ` function ``set_value()`` didn't parse array notation for keys if the rule was not present in the :doc:`Form Validation Library `. Version 2.1.3 ============= -- cgit v1.2.3-24-g4f1b From a945d078d769f00410d10d10b9fdac3ea2e55b45 Mon Sep 17 00:00:00 2001 From: nisheeth-barthwal Date: Fri, 22 Mar 2013 00:49:11 +0530 Subject: Removed entry for erroneous bugfix --- user_guide_src/source/changelog.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 33fc8fa9e..bc74973e0 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -489,7 +489,6 @@ Bug fixes for 3.0 - Fixed a bug (#2239) - :doc:`Email Library ` improperly handled the Subject when used with ``bcc_batch_mode`` resulting in E_WARNING messages and an empty Subject. - Fixed a bug (#2234) - :doc:`Query Builder ` didn't reset JOIN cache for write-type queries. - Fixed a bug (#2298) - :doc:`Database Results ` method `next_row()` kept returning the last row, allowing for infinite loops. -- Fixed a bug (#2289) - :doc:`Email Library ` method `_smtp_authenticate()` returned prematurely from authentication due to opposite condition check. - Fixed a bug (#2236) - :doc:`Form Helper ` function ``set_value()`` didn't parse array notation for keys if the rule was not present in the :doc:`Form Validation Library `. Version 2.1.3 -- 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 +++++++++++++------------------- system/helpers/form_helper.php | 2 +- user_guide_src/source/changelog.rst | 2 +- 3 files changed, 15 insertions(+), 21 deletions(-) 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); } // -------------------------------------------------------------------- diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index d2c22b05c..2238af92a 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -647,7 +647,7 @@ if ( ! function_exists('set_value')) return form_prep($OBJ->set_value($field, $default), $is_textarea); } - if (FALSE !== ($OBJ =& _get_input_object()) && ($value = $OBJ->post($field, FALSE, TRUE))) + if (FALSE !== ($OBJ =& _get_input_object()) && ($value = $OBJ->post($field, FALSE))) { return form_prep($value, $is_textarea); } diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index bc74973e0..6ef08c1a9 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -304,7 +304,7 @@ Release Date: Not Released - Changed method ``valid_ip()`` to use PHP's native ``filter_var()`` function. - Changed internal method ``_sanitize_globals()`` to skip enforcing reversal of *register_globals* in PHP 5.4+, where this functionality no longer exists. - Changed methods ``get()``, ``post()``, ``get_post()``, ``cookie()``, ``server()``, ``user_agent()`` to return NULL instead of FALSE when no value is found. - - Added provision for using array notation for keys. + - Changed method ``_fetch_from_array()`` to parse array notation in field name. - :doc:`Common functions ` changes include: - Added function :php:func:`get_mimes()` to return the *application/config/mimes.php* array. - Added support for HTTP code 303 ("See Other") in :php:func:`set_status_header()`. -- 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(-) 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 ++++++++++++++---------------------------- system/helpers/form_helper.php | 34 ++---------------------------- 2 files changed, 18 insertions(+), 63 deletions(-) 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; } // -------------------------------------------------------------------- diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index 2238af92a..443a06a2d 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -647,7 +647,8 @@ if ( ! function_exists('set_value')) return form_prep($OBJ->set_value($field, $default), $is_textarea); } - if (FALSE !== ($OBJ =& _get_input_object()) && ($value = $OBJ->post($field, FALSE))) + $CI =& get_instance(); + if (NULL !== ($value = $CI->input->post($field, FALSE))) { return form_prep($value, $is_textarea); } @@ -1007,36 +1008,5 @@ 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 -- 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 ++++++----- system/helpers/form_helper.php | 15 +++++---------- 2 files changed, 11 insertions(+), 15 deletions(-) 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 { diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index 443a06a2d..e2c0cc4c5 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -642,18 +642,13 @@ if ( ! function_exists('set_value')) */ function set_value($field = '', $default = '', $is_textarea = FALSE) { - if (FALSE !== ($OBJ =& _get_validation_object()) && $OBJ->has_rule($field)) - { - return form_prep($OBJ->set_value($field, $default), $is_textarea); - } - $CI =& get_instance(); - if (NULL !== ($value = $CI->input->post($field, FALSE))) - { - return form_prep($value, $is_textarea); - } - return form_prep($default, $is_textarea); + $value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field)) + ? $CI->form_validation->set_value($field, $default) + : $CI->input->post($field, FALSE); + + return form_prep($value === NULL ? $default : $value, $is_textarea); } } -- 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(-) 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