From f9fbf1187516363a48fe2fe7bc33d00ae11f134f Mon Sep 17 00:00:00 2001 From: Ignasimg Date: Fri, 6 Feb 2015 09:21:07 +0100 Subject: Update Input.php Added support for json input stream. (Not tested) --- system/core/Input.php | 55 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 9 deletions(-) (limited to 'system/core') diff --git a/system/core/Input.php b/system/core/Input.php index 72425c1c1..3024fca78 100644 --- a/system/core/Input.php +++ b/system/core/Input.php @@ -103,6 +103,14 @@ class CI_Input { */ protected $headers = array(); + /** + * Raw input stream data + * + * @see CI_Input::input_stream() + * @var array + */ + protected $_raw_input_stream = NULL; + /** * Input stream data * @@ -111,7 +119,7 @@ class CI_Input { * @see CI_Input::input_stream() * @var array */ - protected $_input_stream = NULL; + protected $_input_stream = NULL; // Kept for backward compatible. /** * Class constructor @@ -298,6 +306,25 @@ class CI_Input { // ------------------------------------------------------------------------ + /** + * Fetch raw data from php://input stream + * + * Useful when data is not an array and might contain = and & symbols. + */ + public function raw_input_stream() + { + // Prior to PHP 5.6, the input stream can only be read once, + // so we'll need to check if we have already done that first. + if (is_null($this->_raw_input_stream)) + { + $this->_raw_input_stream = file_get_contents('php://input'); + } + + return $this->_raw_input_stream; + } + + // ------------------------------------------------------------------------ + /** * Fetch an item from the php://input stream * @@ -309,16 +336,26 @@ class CI_Input { */ public function input_stream($index = NULL, $xss_clean = NULL) { - // Prior to PHP 5.6, the input stream can only be read once, - // so we'll need to check if we have already done that first. - if ( ! is_array($this->_input_stream)) - { - parse_str(file_get_contents('php://input'), $this->_input_stream); - is_array($this->_input_stream) OR $this->_input_stream = array(); - } - + parse_str($this->raw_input_stream(), $this->_input_stream); return $this->_fetch_from_array($this->_input_stream, $index, $xss_clean); } + + // ------------------------------------------------------------------------ + + /** + * Fetch an item from the php://input stream + * + * Useful when you need to access input that's been send as raw json data' + * + * @param string $index Index for item to be fetched + * @param bool $xss_clean Whether to apply XSS filtering + * @return mixed + */ + public function json_input_stream($index = NULL, $xss_clean = NULL) + { + $json_input_stream = json_decode($this->raw_input_stream(), true); + return $this->_fetch_from_array($json_input_stream, $index, $xss_clean); + } // ------------------------------------------------------------------------ -- cgit v1.2.3-24-g4f1b