Permalink
Browse files

Added CI_Input::input_stream()

Helps in reading php://input stream data by caching it when accessed for the first time.

(supersedes PR #1684)
  • Loading branch information...
1 parent 55a8c62 commit 303eef056b7317a1e4f06feb26fdb452a59c3a51 @narfbg narfbg committed Nov 6, 2012
Showing with 103 additions and 32 deletions.
  1. +41 −0 system/core/Input.php
  2. +1 −0 user_guide_src/source/changelog.rst
  3. +61 −32 user_guide_src/source/libraries/input.rst
View
@@ -100,6 +100,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
*
* Determines whether to globally enable the XSS processing
@@ -257,6 +267,37 @@ public function server($index = '', $xss_clean = FALSE)
// ------------------------------------------------------------------------
/**
+ * 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
*
* Accepts an arbitrary number of parameters (up to 7) or an associative
@@ -259,6 +259,7 @@ Release Date: Not Released
- :doc:`Input Library <libraries/input>` changes include:
- Added ``method()`` to retrieve ``$_SERVER['REQUEST_METHOD']``.
- Added support for arrays and network addresses (e.g. 192.168.1.1/24) for use with the *proxy_ips* setting.
+ - Added method ``input_stream()`` to aid in using **php://input** stream data such as one passed via PUT, DELETE and PATCH requests.
- 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.
@@ -5,16 +5,15 @@ Input Class
The Input Class serves two purposes:
#. It pre-processes global input data for security.
-#. It provides some helper functions for fetching input data and
- pre-processing it.
+#. It provides some helper methods for fetching input data and pre-processing it.
.. note:: This class is initialized automatically by the system so there
is no need to do it manually.
Security Filtering
==================
-The security filtering function is called automatically when a new
+The security filtering method is called automatically when a new
:doc:`controller <../general/controllers>` is invoked. It does the
following:
@@ -47,15 +46,15 @@ Using POST, GET, COOKIE, or SERVER Data
CodeIgniter comes with four helper methods that let you fetch POST, GET,
COOKIE or SERVER items. The main advantage of using the provided
-functions rather than fetching an item directly ($_POST['something'])
+methods rather than fetching an item directly (``$_POST['something']``)
is that the methods will check to see if the item is set and return
NULL if not. This lets you conveniently use data without
having to test whether an item exists first. In other words, normally
you might do something like this::
$something = isset($_POST['something']) ? $_POST['something'] : NULL;
-With CodeIgniter's built in functions you can simply do this::
+With CodeIgniter's built in methods you can simply do this::
$something = $this->input->post('something');
@@ -74,7 +73,7 @@ looking for::
$this->input->post('some_data');
-The function returns NULL if the item you are attempting to retrieve
+The method returns NULL if the item you are attempting to retrieve
does not exist.
The second optional parameter lets you run the data through the XSS
@@ -89,7 +88,7 @@ To return an array of all POST items call without any parameters.
To return all POST items and pass them through the XSS filter set the
first parameter NULL while setting the second parameter to boolean;
-The function returns NULL if there are no items in the POST.
+The method returns NULL if there are no items in the POST.
::
@@ -99,8 +98,8 @@ The function returns NULL if there are no items in the POST.
$this->input->get()
===================
-This function is identical to the post function, only it fetches get
-data::
+This method is identical to the post method, only it fetches get data
+::
$this->input->get('some_data', TRUE);
@@ -109,7 +108,7 @@ To return an array of all GET items call without any parameters.
To return all GET items and pass them through the XSS filter set the
first parameter NULL while setting the second parameter to boolean;
-The function returns NULL if there are no items in the GET.
+The method returns NULL if there are no items in the GET.
::
@@ -118,18 +117,18 @@ The function returns NULL if there are no items in the GET.
$this->input->get_post()
-=========================
+========================
-This function will search through both the post and get streams for
+This method will search through both the post and get streams for
data, looking first in post, and then in get::
$this->input->get_post('some_data', TRUE);
$this->input->cookie()
======================
-This function is identical to the post function, only it fetches cookie
-data::
+This method is identical to the post method, only it fetches cookie data
+::
$this->input->cookie('some_cookie');
$this->input->cookie('some_cookie, TRUE); // with XSS filter
@@ -138,16 +137,43 @@ data::
$this->input->server()
======================
-This function is identical to the above functions, only it fetches
+This method is identical to the above methods, only it fetches server
server data::
$this->input->server('some_data');
+Using the php://input stream
+============================
+
+If you want to utilize the PUT, DELETE, PATCH or other exotic request
+methods, they can only be accessed via a special input stream, that
+can only be read once. This isn't as easy as just reading from e.g.
+the ``$_POST`` array, because it will always exist and you can try
+and access multiple variables without caring that you might only have
+one shot at all of the POST data.
+
+CodeIgniter will take care of that for you, and you can access data
+from the **php://input** stream at any time, just by calling the
+``input_stream()`` method::
+
+ $this->input->input_stream('key');
+
+Similar to the methods above, if the requested data is not found, it
+will return NULL and you can also decide whether to run the data
+through ``xss_clean()`` by passing a boolean value as the second
+parameter::
+
+ $this->input->input_stream('key', TRUE); // XSS Clean
+ $this->input->input_stream('key', FALSE); // No XSS filter
+
+.. note:: You can utilize method() in order to know if you're reading
+ PUT, DELETE or PATCH data.
+
$this->input->set_cookie()
-===========================
+==========================
Sets a cookie containing the values you specify. There are two ways to
-pass information to this function so that a cookie can be set: Array
+pass information to this method so that a cookie can be set: Array
Method, and Discrete Parameters:
Array Method
@@ -182,7 +208,7 @@ For site-wide cookies regardless of how your site is requested, add your
URL to the **domain** starting with a period, like this:
.your-domain.com
-The path is usually not needed since the function sets a root path.
+The path is usually not needed since the method sets a root path.
The prefix is only needed if you need to avoid name collisions with
other identically named cookies for your server.
@@ -198,22 +224,25 @@ parameters::
$this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure);
+
$this->input->ip_address()
-===========================
+==========================
Returns the IP address for the current user. If the IP address is not
-valid, the function will return an IP of: 0.0.0.0
+valid, the method will return an IP of: 0.0.0.0
::
echo $this->input->ip_address();
$this->input->valid_ip($ip)
-============================
+===========================
Takes an IP address as input and returns TRUE or FALSE (boolean) if it
-is valid or not. Note: The $this->input->ip_address() function above
-validates the IP automatically.
+is valid or not.
+
+.. note:: The $this->input->ip_address() method above automatically
+ validates the IP address.
::
@@ -230,7 +259,7 @@ Accepts an optional second string parameter of 'ipv4' or 'ipv6' to specify
an IP format. The default checks for both formats.
$this->input->user_agent()
-===========================
+==========================
Returns the user agent (web browser) being used by the current user.
Returns FALSE if it's not available.
@@ -243,7 +272,7 @@ See the :doc:`User Agent Class <user_agent>` for methods which extract
information from the user agent string.
$this->input->request_headers()
-================================
+===============================
Useful if running in a non-Apache environment where
`apache_request_headers() <http://php.net/apache_request_headers>`_
@@ -253,8 +282,8 @@ will not be supported. Returns an array of headers.
$headers = $this->input->request_headers();
-$this->input->get_request_header();
-=====================================
+$this->input->get_request_header()
+==================================
Returns a single member of the request headers array.
@@ -263,13 +292,13 @@ Returns a single member of the request headers array.
$this->input->get_request_header('some-header', TRUE);
$this->input->is_ajax_request()
-=================================
+===============================
Checks to see if the HTTP_X_REQUESTED_WITH server header has been
set, and returns a boolean response.
$this->input->is_cli_request()
-================================
+==============================
Checks to see if the STDIN constant is set, which is a failsafe way to
see if PHP is being run on the command line.
@@ -278,13 +307,13 @@ see if PHP is being run on the command line.
$this->input->is_cli_request()
-$this->input->method();
-=====================================
+$this->input->method()
+======================
Returns the $_SERVER['REQUEST_METHOD'], optional set uppercase or lowercase (default lowercase).
::
echo $this->input->method(TRUE); // Outputs: POST
echo $this->input->method(FALSE); // Outputs: post
- echo $this->input->method(); // Outputs: post
+ echo $this->input->method(); // Outputs: post

0 comments on commit 303eef0

Please sign in to comment.