Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 6 revisions

Additional/overwritten methods for the Input library.

Add the methods you find useful [code] class MY_Input extends CI_Input {

   function MY_Input()
   {
      parent::CI_Input();
   }

   /**
   * Sanitize Globals
   *
   * removed sanitizing of post and cookie values
   */
    function _sanitize_globals()
{
    // Would kind of be "wrong" to unset any of these GLOBALS
    $protected = array('_SERVER', '_GET', '_POST', '_FILES', '_REQUEST', '_SESSION', '_ENV', 'GLOBALS', 'HTTP_RAW_POST_DATA',
                        'system_folder', 'application_folder', 'BM', 'EXT', 'CFG', 'URI', 'RTR', 'OUT', 'IN');
    
    // Unset globals for security. 
    // This is effectively the same as register_globals = off
    foreach (array($_GET, $_POST, $_COOKIE, $_SERVER, $_FILES, $_ENV, (isset($_SESSION) && is_array($_SESSION)) ? $_SESSION : array()) as $global)
    {
        if ( ! is_array($global))
        {
            if ( ! in_array($global, $protected))
            {
                unset($GLOBALS[$global]);
            }
        }
        else
        {
            foreach ($global as $key => $val)
            {
                if ( ! in_array($key, $protected))
                {
                    unset($GLOBALS[$key]);
                }
                
                if (is_array($val))
                {
                    foreach($val as $k => $v)
                    {
                        if ( ! in_array($k, $protected))
                        {
                            unset($GLOBALS[$k]);
                        }
                    }
                }
            }    
        }
    }

    // Is $_GET data allowed? If not we'll set the $_GET to an empty array
    if ($this->allow_get_array == FALSE)
    {
        $_GET = array();
    }
    else
    {
        if (is_array($_GET) AND count($_GET) > 0)
        {
            foreach($_GET as $key => $val)
            {
                $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
            }
        }
    }
    
}

    /**
     * Fetch an item from the POST array
     *
     * added a default as second argument to define a value if needed
     * example : $this->input->post('fieldname','default value');
     * set $xss_clean to NULL for clarity and added an extra condition for the 
     * xss cleaning to overwrite the xss cleaning setting in the config file.
     */
    function post($index = '', $default = FALSE, $xss_clean = NULL)
{        
    if ( ! isset($_POST[$index]))
    {
        return $default;
    }

    if ($xss_clean === TRUE || ($this->use_xss_clean === TRUE && $xss_clean !== FALSE))
    {
        if (is_array($_POST[$index]))
        {
            foreach($_POST[$index] as $key => $val)
            {                    
                $_POST[$index][$key] = $this->xss_clean($val);
            }
        }
        else
        {
            return $this->xss_clean($_POST[$index]);
        }
    }

    return $_POST[$index];
} 

    /**
     * Fetch an item from the COOKIE array
     *
     * see post method
     */
    function cookie($index = '', $default = FALSE, $xss_clean = FALSE)
{
    if ( ! isset($_COOKIE[$index]))
    {
        return $default;
    }

    if ($xss_clean === TRUE || ($this->use_xss_clean === TRUE && $xss_clean !== FALSE))
    {
        if (is_array($_COOKIE[$index]))
        {
            $cookie = array();
            foreach($_COOKIE[$index] as $key => $val)
            {
                $cookie[$key] = $this->xss_clean($val);
            }
    
            return $cookie;
        }
        else
        {
            return $this->xss_clean($_COOKIE[$index]);
        }
    }
    else
    {
        return $_COOKIE[$index];
    }
}

}
[/code]

Clone this wiki locally