summaryrefslogtreecommitdiffstats
path: root/system/core/Input.php
diff options
context:
space:
mode:
authorIgnasimg <ignasimg@gmail.com>2015-02-06 09:21:07 +0100
committerIgnasimg <ignasimg@gmail.com>2015-02-06 09:21:07 +0100
commitf9fbf1187516363a48fe2fe7bc33d00ae11f134f (patch)
tree502a0bc947565a87c08481150e288b3d11b5d3bf /system/core/Input.php
parentdb00b627b83cdba8564fb7e6fbf619e6182f4aa8 (diff)
Update Input.php
Added support for json input stream. (Not tested)
Diffstat (limited to 'system/core/Input.php')
-rw-r--r--system/core/Input.php55
1 files changed, 46 insertions, 9 deletions
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
@@ -104,6 +104,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
*
* Parsed from php://input at runtime
@@ -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
@@ -299,6 +307,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
*
* Useful when you need to access PUT, DELETE or PATCH request data.
@@ -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);
+ }
// ------------------------------------------------------------------------