From 303eef056b7317a1e4f06feb26fdb452a59c3a51 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 6 Nov 2012 14:55:48 +0200 Subject: Added CI_Input::input_stream() Helps in reading php://input stream data by caching it when accessed for the first time. (supersedes PR #1684) --- system/core/Input.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'system') diff --git a/system/core/Input.php b/system/core/Input.php index c0158df99..adc5f7ac0 100644 --- a/system/core/Input.php +++ b/system/core/Input.php @@ -99,6 +99,16 @@ class CI_Input { */ protected $headers = array(); + /** + * Input stream data + * + * Parsed from php://input at runtime + * + * @see CI_Input::input_stream() + * @var array + */ + protected $_input_stream = NULL; + /** * Class constructor * @@ -256,6 +266,37 @@ class CI_Input { // ------------------------------------------------------------------------ + /** + * Fetch an item from the php://input stream + * + * Useful when you need to access PUT, DELETE or PATCH request data. + * + * @param string $index Index for item to be fetched + * @param bool $xss_clean Whether to apply XSS filtering + * @return mixed + */ + public function input_stream($index = '', $xss_clean = FALSE) + { + // 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)) + { + return $this->_fetch_from_array($this->_input_stream, $index, $xss_clean); + } + + // Parse the input stream in our cache var + parse_str(file_get_contents('php://input'), $this->_input_stream); + if ( ! is_array($this->_input_stream)) + { + $this->_input_stream = array(); + return NULL; + } + + return $this->_fetch_from_array($this->_input_stream, $index, $xss_clean); + } + + // ------------------------------------------------------------------------ + /** * Set cookie * -- cgit v1.2.3-24-g4f1b