Andrey Andreev | 64e98aa | 2012-01-07 20:29:10 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 64e98aa | 2012-01-07 20:29:10 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 64e98aa | 2012-01-07 20:29:10 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * Input Class |
| 30 | * |
| 31 | * Pre-processes global input data for security |
| 32 | * |
| 33 | * @package CodeIgniter |
| 34 | * @subpackage Libraries |
| 35 | * @category Input |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 36 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | * @link http://codeigniter.com/user_guide/libraries/input.html |
| 38 | */ |
| 39 | class CI_Input { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 41 | /** |
| 42 | * IP address of the current user |
| 43 | * |
| 44 | * @var string |
| 45 | */ |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 46 | public $ip_address = FALSE; |
| 47 | |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 48 | /** |
| 49 | * user agent (web browser) being used by the current user |
| 50 | * |
| 51 | * @var string |
| 52 | */ |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 53 | public $user_agent = FALSE; |
| 54 | |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 55 | /** |
| 56 | * If FALSE, then $_GET will be set to an empty array |
| 57 | * |
| 58 | * @var bool |
| 59 | */ |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 60 | protected $_allow_get_array = TRUE; |
| 61 | |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 62 | /** |
| 63 | * If TRUE, then newlines are standardized |
| 64 | * |
| 65 | * @var bool |
| 66 | */ |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 67 | protected $_standardize_newlines = TRUE; |
| 68 | |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 69 | /** |
| 70 | * Determines whether the XSS filter is always active when GET, POST or COOKIE data is encountered |
| 71 | * Set automatically based on config setting |
| 72 | * |
| 73 | * @var bool |
| 74 | */ |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 75 | protected $_enable_xss = FALSE; |
| 76 | |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 77 | /** |
| 78 | * Enables a CSRF cookie token to be set. |
| 79 | * Set automatically based on config setting |
| 80 | * |
| 81 | * @var bool |
| 82 | */ |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 83 | protected $_enable_csrf = FALSE; |
| 84 | |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 85 | /** |
| 86 | * List of all HTTP request headers |
| 87 | * |
| 88 | * @var array |
| 89 | */ |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 90 | protected $headers = array(); |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 91 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 92 | /** |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 93 | * Constructor |
| 94 | * |
| 95 | * Sets whether to globally enable the XSS processing |
| 96 | * and whether to allow the $_GET array |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 97 | */ |
| 98 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 99 | { |
Andrey Andreev | 1377497 | 2012-01-08 04:30:33 +0200 | [diff] [blame] | 100 | log_message('debug', 'Input Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 101 | |
Phil Sturgeon | c808915 | 2010-12-27 19:06:28 +0000 | [diff] [blame] | 102 | $this->_allow_get_array = (config_item('allow_get_array') === TRUE); |
Andrey Andreev | 64e98aa | 2012-01-07 20:29:10 +0200 | [diff] [blame] | 103 | $this->_enable_xss = (config_item('global_xss_filtering') === TRUE); |
| 104 | $this->_enable_csrf = (config_item('csrf_protection') === TRUE); |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 105 | |
Pascal Kriete | 14a0ac6 | 2011-04-05 14:55:56 -0400 | [diff] [blame] | 106 | global $SEC; |
| 107 | $this->security =& $SEC; |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 108 | |
Pascal Kriete | aaec1e4 | 2011-01-20 00:01:21 -0500 | [diff] [blame] | 109 | // Do we need the UTF-8 class? |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 110 | if (UTF8_ENABLED === TRUE) |
| 111 | { |
| 112 | global $UNI; |
| 113 | $this->uni =& $UNI; |
| 114 | } |
| 115 | |
| 116 | // Sanitize global arrays |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | $this->_sanitize_globals(); |
| 118 | } |
| 119 | |
| 120 | // -------------------------------------------------------------------- |
| 121 | |
| 122 | /** |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 123 | * Fetch from array |
| 124 | * |
| 125 | * This is a helper function to retrieve values from global arrays |
| 126 | * |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 127 | * @param array |
| 128 | * @param string |
| 129 | * @param bool |
| 130 | * @return string |
| 131 | */ |
Bo-Yi Wu | 4721379 | 2011-09-13 22:44:07 +0800 | [diff] [blame] | 132 | protected function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 133 | { |
| 134 | if ( ! isset($array[$index])) |
| 135 | { |
| 136 | return FALSE; |
| 137 | } |
| 138 | |
| 139 | if ($xss_clean === TRUE) |
| 140 | { |
Pascal Kriete | 14a0ac6 | 2011-04-05 14:55:56 -0400 | [diff] [blame] | 141 | return $this->security->xss_clean($array[$index]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | return $array[$index]; |
| 145 | } |
| 146 | |
| 147 | // -------------------------------------------------------------------- |
| 148 | |
| 149 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 150 | * Fetch an item from the GET array |
| 151 | * |
| 152 | * @param string |
| 153 | * @param bool |
| 154 | * @return string |
| 155 | */ |
Bo-Yi Wu | 4db872f | 2011-09-12 10:52:37 +0800 | [diff] [blame] | 156 | public function get($index = NULL, $xss_clean = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | { |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 158 | // Check if a field has been provided |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 159 | if ($index === NULL && ! empty($_GET)) |
vascopj | ff1cfa1 | 2011-02-13 21:30:19 +0000 | [diff] [blame] | 160 | { |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 161 | $get = array(); |
vascopj | ff1cfa1 | 2011-02-13 21:30:19 +0000 | [diff] [blame] | 162 | |
| 163 | // loop through the full _GET array |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 164 | foreach (array_keys($_GET) as $key) |
vascopj | ff1cfa1 | 2011-02-13 21:30:19 +0000 | [diff] [blame] | 165 | { |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 166 | $get[$key] = $this->_fetch_from_array($_GET, $key, $xss_clean); |
vascopj | ff1cfa1 | 2011-02-13 21:30:19 +0000 | [diff] [blame] | 167 | } |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 168 | return $get; |
vascopj | ff1cfa1 | 2011-02-13 21:30:19 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | return $this->_fetch_from_array($_GET, $index, $xss_clean); |
| 172 | } |
| 173 | |
| 174 | // -------------------------------------------------------------------- |
| 175 | |
| 176 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 177 | * Fetch an item from the POST array |
| 178 | * |
| 179 | * @param string |
| 180 | * @param bool |
| 181 | * @return string |
| 182 | */ |
Bo-Yi Wu | 4db872f | 2011-09-12 10:52:37 +0800 | [diff] [blame] | 183 | public function post($index = NULL, $xss_clean = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | { |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 185 | // Check if a field has been provided |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 186 | if ($index === NULL && ! empty($_POST)) |
vascopj | 0ba58b8 | 2011-02-06 14:20:21 +0000 | [diff] [blame] | 187 | { |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 188 | $post = array(); |
vascopj | 0ba58b8 | 2011-02-06 14:20:21 +0000 | [diff] [blame] | 189 | |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 190 | // Loop through the full _POST array and return it |
| 191 | foreach (array_keys($_POST) as $key) |
vascopj | 0ba58b8 | 2011-02-06 14:20:21 +0000 | [diff] [blame] | 192 | { |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 193 | $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean); |
vascopj | 0ba58b8 | 2011-02-06 14:20:21 +0000 | [diff] [blame] | 194 | } |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 195 | return $post; |
vascopj | 0ba58b8 | 2011-02-06 14:20:21 +0000 | [diff] [blame] | 196 | } |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 197 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | return $this->_fetch_from_array($_POST, $index, $xss_clean); |
| 199 | } |
| 200 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 201 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | // -------------------------------------------------------------------- |
| 203 | |
| 204 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 205 | * Fetch an item from either the GET array or the POST |
| 206 | * |
| 207 | * @param string The index key |
| 208 | * @param bool XSS cleaning |
| 209 | * @return string |
| 210 | */ |
Bo-Yi Wu | 4db872f | 2011-09-12 10:52:37 +0800 | [diff] [blame] | 211 | public function get_post($index = '', $xss_clean = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | { |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 213 | return isset($_POST[$index]) |
| 214 | ? $this->post($index, $xss_clean) |
| 215 | : $this->get($index, $xss_clean); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // -------------------------------------------------------------------- |
| 219 | |
| 220 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 221 | * Fetch an item from the COOKIE array |
| 222 | * |
| 223 | * @param string |
| 224 | * @param bool |
| 225 | * @return string |
| 226 | */ |
Bo-Yi Wu | 4db872f | 2011-09-12 10:52:37 +0800 | [diff] [blame] | 227 | public function cookie($index = '', $xss_clean = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | { |
| 229 | return $this->_fetch_from_array($_COOKIE, $index, $xss_clean); |
| 230 | } |
| 231 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 232 | // ------------------------------------------------------------------------ |
| 233 | |
| 234 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 235 | * Set cookie |
| 236 | * |
| 237 | * Accepts seven parameters, or you can submit an associative |
| 238 | * array in the first parameter containing all the values. |
| 239 | * |
| 240 | * @param mixed |
| 241 | * @param string the value of the cookie |
| 242 | * @param string the number of seconds until expiration |
| 243 | * @param string the cookie domain. Usually: .yourdomain.com |
| 244 | * @param string the cookie path |
| 245 | * @param string the cookie prefix |
| 246 | * @param bool true makes the cookie secure |
| 247 | * @param bool true makes the cookie accessible via http(s) only (no javascript) |
| 248 | * @return void |
| 249 | */ |
freewil | 4ad0fd8 | 2012-03-13 22:37:42 -0400 | [diff] [blame] | 250 | public function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE) |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 251 | { |
| 252 | if (is_array($name)) |
| 253 | { |
tobiasbg | 9aa7dc9 | 2011-02-18 21:57:13 +0100 | [diff] [blame] | 254 | // always leave 'name' in last place, as the loop will break otherwise, due to $$item |
freewil | 4ad0fd8 | 2012-03-13 22:37:42 -0400 | [diff] [blame] | 255 | foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name') as $item) |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 256 | { |
| 257 | if (isset($name[$item])) |
| 258 | { |
| 259 | $$item = $name[$item]; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 264 | if ($prefix == '' && config_item('cookie_prefix') != '') |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 265 | { |
| 266 | $prefix = config_item('cookie_prefix'); |
| 267 | } |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 268 | if ($domain == '' && config_item('cookie_domain') != '') |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 269 | { |
| 270 | $domain = config_item('cookie_domain'); |
| 271 | } |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 272 | if ($path == '/' && config_item('cookie_path') !== '/') |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 273 | { |
| 274 | $path = config_item('cookie_path'); |
| 275 | } |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 276 | if ($secure == FALSE && config_item('cookie_secure') != FALSE) |
tobiasbg | 9aa7dc9 | 2011-02-18 21:57:13 +0100 | [diff] [blame] | 277 | { |
| 278 | $secure = config_item('cookie_secure'); |
| 279 | } |
freewil | 4ad0fd8 | 2012-03-13 22:37:42 -0400 | [diff] [blame] | 280 | if ($httponly == FALSE && config_item('cookie_httponly') != FALSE) |
| 281 | { |
| 282 | $httponly = config_item('cookie_httponly'); |
| 283 | } |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 284 | |
| 285 | if ( ! is_numeric($expire)) |
| 286 | { |
| 287 | $expire = time() - 86500; |
| 288 | } |
| 289 | else |
| 290 | { |
Phil Sturgeon | c808915 | 2010-12-27 19:06:28 +0000 | [diff] [blame] | 291 | $expire = ($expire > 0) ? time() + $expire : 0; |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 292 | } |
| 293 | |
freewil | 4ad0fd8 | 2012-03-13 22:37:42 -0400 | [diff] [blame] | 294 | setcookie($prefix.$name, $value, $expire, $path, $domain, $secure, $httponly); |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 295 | } |
| 296 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 297 | // -------------------------------------------------------------------- |
| 298 | |
| 299 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 300 | * Fetch an item from the SERVER array |
| 301 | * |
| 302 | * @param string |
| 303 | * @param bool |
| 304 | * @return string |
| 305 | */ |
Bo-Yi Wu | 4db872f | 2011-09-12 10:52:37 +0800 | [diff] [blame] | 306 | public function server($index = '', $xss_clean = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 307 | { |
| 308 | return $this->_fetch_from_array($_SERVER, $index, $xss_clean); |
| 309 | } |
| 310 | |
| 311 | // -------------------------------------------------------------------- |
| 312 | |
| 313 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 314 | * Fetch the IP Address |
| 315 | * |
| 316 | * @return string |
| 317 | */ |
Bo-Yi Wu | 4db872f | 2011-09-12 10:52:37 +0800 | [diff] [blame] | 318 | public function ip_address() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | { |
| 320 | if ($this->ip_address !== FALSE) |
| 321 | { |
| 322 | return $this->ip_address; |
| 323 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 324 | |
Derek Jones | 42b2e17 | 2009-02-05 16:59:45 +0000 | [diff] [blame] | 325 | if (config_item('proxy_ips') != '' && $this->server('HTTP_X_FORWARDED_FOR') && $this->server('REMOTE_ADDR')) |
Derek Jones | c597228 | 2009-02-04 21:40:20 +0000 | [diff] [blame] | 326 | { |
Derek Jones | 42b2e17 | 2009-02-05 16:59:45 +0000 | [diff] [blame] | 327 | $proxies = preg_split('/[\s,]/', config_item('proxy_ips'), -1, PREG_SPLIT_NO_EMPTY); |
Derek Jones | c597228 | 2009-02-04 21:40:20 +0000 | [diff] [blame] | 328 | $proxies = is_array($proxies) ? $proxies : array($proxies); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 329 | |
Derek Jones | c597228 | 2009-02-04 21:40:20 +0000 | [diff] [blame] | 330 | $this->ip_address = in_array($_SERVER['REMOTE_ADDR'], $proxies) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; |
| 331 | } |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 332 | elseif ( ! $this->server('HTTP_CLIENT_IP') && $this->server('REMOTE_ADDR')) |
John Bellone | 52c10b6 | 2011-08-21 11:41:32 -0400 | [diff] [blame] | 333 | { |
| 334 | $this->ip_address = $_SERVER['REMOTE_ADDR']; |
| 335 | } |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 336 | elseif ($this->server('REMOTE_ADDR') && $this->server('HTTP_CLIENT_IP')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 337 | { |
| 338 | $this->ip_address = $_SERVER['HTTP_CLIENT_IP']; |
| 339 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 340 | elseif ($this->server('HTTP_CLIENT_IP')) |
| 341 | { |
| 342 | $this->ip_address = $_SERVER['HTTP_CLIENT_IP']; |
| 343 | } |
| 344 | elseif ($this->server('HTTP_X_FORWARDED_FOR')) |
| 345 | { |
| 346 | $this->ip_address = $_SERVER['HTTP_X_FORWARDED_FOR']; |
| 347 | } |
| 348 | |
| 349 | if ($this->ip_address === FALSE) |
| 350 | { |
Andrey Andreev | 64e98aa | 2012-01-07 20:29:10 +0200 | [diff] [blame] | 351 | return $this->ip_address = '0.0.0.0'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Robin Sowell | 76b369e | 2010-03-19 11:15:28 -0400 | [diff] [blame] | 354 | if (strpos($this->ip_address, ',') !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | { |
| 356 | $x = explode(',', $this->ip_address); |
Derek Jones | c597228 | 2009-02-04 21:40:20 +0000 | [diff] [blame] | 357 | $this->ip_address = trim(end($x)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | if ( ! $this->valid_ip($this->ip_address)) |
| 361 | { |
Andrey Andreev | 64e98aa | 2012-01-07 20:29:10 +0200 | [diff] [blame] | 362 | return $this->ip_address = '0.0.0.0'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | return $this->ip_address; |
| 366 | } |
| 367 | |
| 368 | // -------------------------------------------------------------------- |
| 369 | |
| 370 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 371 | * Validate IP Address |
| 372 | * |
| 373 | * Updated version suggested by Geert De Deckere |
| 374 | * |
| 375 | * @param string |
| 376 | * @return bool |
| 377 | */ |
Bo-Yi Wu | 4db872f | 2011-09-12 10:52:37 +0800 | [diff] [blame] | 378 | public function valid_ip($ip) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 379 | { |
Andrey Andreev | 1ae6516 | 2012-03-10 16:11:34 +0200 | [diff] [blame] | 380 | return (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | // -------------------------------------------------------------------- |
| 384 | |
| 385 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 386 | * User Agent |
| 387 | * |
| 388 | * @return string |
| 389 | */ |
Bo-Yi Wu | 4db872f | 2011-09-12 10:52:37 +0800 | [diff] [blame] | 390 | public function user_agent() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 391 | { |
| 392 | if ($this->user_agent !== FALSE) |
| 393 | { |
| 394 | return $this->user_agent; |
| 395 | } |
| 396 | |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 397 | return $this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | // -------------------------------------------------------------------- |
| 401 | |
| 402 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 403 | * Sanitize Globals |
| 404 | * |
| 405 | * This function does the following: |
| 406 | * |
| 407 | * - Unsets $_GET data (if query strings are not enabled) |
| 408 | * - Unsets all globals if register_globals is enabled |
| 409 | * - Standardizes newline characters to \n |
| 410 | * |
| 411 | * @return void |
| 412 | */ |
Andrey Andreev | 90cfe14 | 2012-01-08 04:46:42 +0200 | [diff] [blame] | 413 | protected function _sanitize_globals() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 414 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 415 | // It would be "wrong" to unset any of these GLOBALS. |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 416 | $protected = array( |
| 417 | '_SERVER', |
| 418 | '_GET', |
| 419 | '_POST', |
| 420 | '_FILES', |
| 421 | '_REQUEST', |
| 422 | '_SESSION', |
| 423 | '_ENV', |
| 424 | 'GLOBALS', |
| 425 | 'HTTP_RAW_POST_DATA', |
| 426 | 'system_folder', |
| 427 | 'application_folder', |
| 428 | 'BM', |
| 429 | 'EXT', |
| 430 | 'CFG', |
| 431 | 'URI', |
| 432 | 'RTR', |
| 433 | 'OUT' |
| 434 | 'IN' |
| 435 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 437 | // Unset globals for securiy. |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 438 | // This is effectively the same as register_globals = off |
| 439 | foreach (array($_GET, $_POST, $_COOKIE) as $global) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 440 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 441 | if ( ! is_array($global)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 442 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 443 | if ( ! in_array($global, $protected)) |
| 444 | { |
| 445 | global $$global; |
| 446 | $$global = NULL; |
| 447 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | } |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 449 | else |
| 450 | { |
| 451 | foreach ($global as $key => $val) |
| 452 | { |
| 453 | if ( ! in_array($key, $protected)) |
| 454 | { |
| 455 | global $$key; |
| 456 | $$key = NULL; |
| 457 | } |
| 458 | } |
| 459 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 462 | // Is $_GET data allowed? If not we'll set the $_GET to an empty array |
| 463 | if ($this->_allow_get_array == FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 464 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 465 | $_GET = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 466 | } |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 467 | elseif (is_array($_GET) && count($_GET) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 468 | { |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 469 | foreach ($_GET as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | { |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 471 | $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 475 | // Clean $_POST Data |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 476 | if (is_array($_POST) && count($_POST) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 477 | { |
Pascal Kriete | 5d5895f | 2011-02-14 13:27:07 -0500 | [diff] [blame] | 478 | foreach ($_POST as $key => $val) |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 479 | { |
| 480 | $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); |
| 481 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 484 | // Clean $_COOKIE Data |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 485 | if (is_array($_COOKIE) && count($_COOKIE) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 486 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 487 | // Also get rid of specially treated cookies that might be set by a server |
| 488 | // or silly application, that are of no use to a CI application anyway |
| 489 | // but that when present will trip our 'Disallowed Key Characters' alarm |
| 490 | // http://www.ietf.org/rfc/rfc2109.txt |
| 491 | // note that the key names below are single quoted strings, and are not PHP variables |
| 492 | unset($_COOKIE['$Version']); |
| 493 | unset($_COOKIE['$Path']); |
| 494 | unset($_COOKIE['$Domain']); |
| 495 | |
Pascal Kriete | 5d5895f | 2011-02-14 13:27:07 -0500 | [diff] [blame] | 496 | foreach ($_COOKIE as $key => $val) |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 497 | { |
| 498 | $_COOKIE[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); |
| 499 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 502 | // Sanitize PHP_SELF |
| 503 | $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']); |
| 504 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 505 | // CSRF Protection check |
| 506 | if ($this->_enable_csrf == TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 507 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 508 | $this->security->csrf_verify(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 509 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 510 | |
Andrey Andreev | 90cfe14 | 2012-01-08 04:46:42 +0200 | [diff] [blame] | 511 | log_message('debug', 'Global POST and COOKIE data sanitized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | // -------------------------------------------------------------------- |
| 515 | |
| 516 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 517 | * Clean Input Data |
| 518 | * |
| 519 | * This is a helper function. It escapes data and |
| 520 | * standardizes newline characters to \n |
| 521 | * |
| 522 | * @param string |
| 523 | * @return string |
| 524 | */ |
Andrey Andreev | 90cfe14 | 2012-01-08 04:46:42 +0200 | [diff] [blame] | 525 | protected function _clean_input_data($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 526 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 527 | if (is_array($str)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 528 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 529 | $new_array = array(); |
| 530 | foreach ($str as $key => $val) |
| 531 | { |
| 532 | $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); |
| 533 | } |
| 534 | return $new_array; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Andrey Andreev | af72862 | 2011-10-20 10:11:59 +0300 | [diff] [blame] | 537 | /* We strip slashes if magic quotes is on to keep things consistent |
| 538 | |
| 539 | NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and |
| 540 | it will probably not exist in future versions at all. |
| 541 | */ |
| 542 | if ( ! is_php('5.4') && get_magic_quotes_gpc()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 543 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 544 | $str = stripslashes($str); |
| 545 | } |
| 546 | |
| 547 | // Clean UTF-8 if supported |
| 548 | if (UTF8_ENABLED === TRUE) |
| 549 | { |
| 550 | $str = $this->uni->clean_string($str); |
| 551 | } |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 552 | |
Pascal Kriete | 14a0ac6 | 2011-04-05 14:55:56 -0400 | [diff] [blame] | 553 | // Remove control characters |
| 554 | $str = remove_invisible_characters($str); |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 555 | |
| 556 | // Should we filter the input data? |
| 557 | if ($this->_enable_xss === TRUE) |
| 558 | { |
| 559 | $str = $this->security->xss_clean($str); |
| 560 | } |
| 561 | |
| 562 | // Standardize newlines if needed |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 563 | if ($this->_standardize_newlines == TRUE && strpos($str, "\r") !== FALSE) |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 564 | { |
Andrey Andreev | 64e98aa | 2012-01-07 20:29:10 +0200 | [diff] [blame] | 565 | return str_replace(array("\r\n", "\r", "\r\n\n"), PHP_EOL, $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | return $str; |
| 569 | } |
| 570 | |
| 571 | // -------------------------------------------------------------------- |
| 572 | |
| 573 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 574 | * Clean Keys |
| 575 | * |
| 576 | * This is a helper function. To prevent malicious users |
| 577 | * from trying to exploit keys we make sure that keys are |
| 578 | * only named with alpha-numeric text and a few other items. |
| 579 | * |
| 580 | * @param string |
| 581 | * @return string |
| 582 | */ |
Andrey Andreev | 90cfe14 | 2012-01-08 04:46:42 +0200 | [diff] [blame] | 583 | protected function _clean_input_keys($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 584 | { |
Andrey Andreev | 64e98aa | 2012-01-07 20:29:10 +0200 | [diff] [blame] | 585 | if ( ! preg_match('/^[a-z0-9:_\/-]+$/i', $str)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 586 | { |
Kevin Cupp | d63e401 | 2012-02-05 14:14:32 -0500 | [diff] [blame] | 587 | set_status_header(503); |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 588 | exit('Disallowed Key Characters.'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 591 | // Clean UTF-8 if supported |
| 592 | if (UTF8_ENABLED === TRUE) |
| 593 | { |
Andrey Andreev | 64e98aa | 2012-01-07 20:29:10 +0200 | [diff] [blame] | 594 | return $this->uni->clean_string($str); |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 595 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 596 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 597 | return $str; |
| 598 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 599 | |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 600 | // -------------------------------------------------------------------- |
| 601 | |
| 602 | /** |
| 603 | * Request Headers |
| 604 | * |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 605 | * In Apache, you can simply call apache_request_headers(), however for |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 606 | * people running other webservers the function is undefined. |
| 607 | * |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 608 | * @param bool XSS cleaning |
Andrey Andreev | 64e98aa | 2012-01-07 20:29:10 +0200 | [diff] [blame] | 609 | * @return array |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 610 | */ |
| 611 | public function request_headers($xss_clean = FALSE) |
| 612 | { |
| 613 | // Look at Apache go! |
| 614 | if (function_exists('apache_request_headers')) |
| 615 | { |
| 616 | $headers = apache_request_headers(); |
| 617 | } |
| 618 | else |
| 619 | { |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 620 | $headers['Content-Type'] = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : @getenv('CONTENT_TYPE'); |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 621 | |
| 622 | foreach ($_SERVER as $key => $val) |
| 623 | { |
Andrey Andreev | 64e98aa | 2012-01-07 20:29:10 +0200 | [diff] [blame] | 624 | if (strpos($key, 'HTTP_') === 0) |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 625 | { |
| 626 | $headers[substr($key, 5)] = $this->_fetch_from_array($_SERVER, $key, $xss_clean); |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | // take SOME_HEADER and turn it into Some-Header |
| 632 | foreach ($headers as $key => $val) |
| 633 | { |
| 634 | $key = str_replace('_', ' ', strtolower($key)); |
| 635 | $key = str_replace(' ', '-', ucwords($key)); |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 636 | |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 637 | $this->headers[$key] = $val; |
| 638 | } |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 639 | |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 640 | return $this->headers; |
| 641 | } |
| 642 | |
| 643 | // -------------------------------------------------------------------- |
| 644 | |
| 645 | /** |
| 646 | * Get Request Header |
| 647 | * |
| 648 | * Returns the value of a single member of the headers class member |
| 649 | * |
Andrey Andreev | 773e117 | 2012-02-08 23:02:19 +0200 | [diff] [blame] | 650 | * @param string array key for $this->headers |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 651 | * @param bool XSS Clean or not |
Andrey Andreev | 773e117 | 2012-02-08 23:02:19 +0200 | [diff] [blame] | 652 | * @return mixed FALSE on failure, string on success |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 653 | */ |
| 654 | public function get_request_header($index, $xss_clean = FALSE) |
| 655 | { |
| 656 | if (empty($this->headers)) |
| 657 | { |
| 658 | $this->request_headers(); |
| 659 | } |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 660 | |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 661 | if ( ! isset($this->headers[$index])) |
| 662 | { |
| 663 | return FALSE; |
| 664 | } |
| 665 | |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 666 | return ($xss_clean === TRUE) |
| 667 | ? $this->security->xss_clean($this->headers[$index]) |
| 668 | : $this->headers[$index]; |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 669 | } |
| 670 | |
Greg Aker | 081ac9d | 2010-11-22 14:42:53 -0600 | [diff] [blame] | 671 | // -------------------------------------------------------------------- |
Phil Sturgeon | c382871 | 2011-01-19 12:31:47 +0000 | [diff] [blame] | 672 | |
Greg Aker | 081ac9d | 2010-11-22 14:42:53 -0600 | [diff] [blame] | 673 | /** |
| 674 | * Is ajax Request? |
| 675 | * |
| 676 | * Test to see if a request contains the HTTP_X_REQUESTED_WITH header |
| 677 | * |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 678 | * @return bool |
Greg Aker | 081ac9d | 2010-11-22 14:42:53 -0600 | [diff] [blame] | 679 | */ |
| 680 | public function is_ajax_request() |
| 681 | { |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 682 | return ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'); |
Greg Aker | 081ac9d | 2010-11-22 14:42:53 -0600 | [diff] [blame] | 683 | } |
| 684 | |
Phil Sturgeon | c382871 | 2011-01-19 12:31:47 +0000 | [diff] [blame] | 685 | // -------------------------------------------------------------------- |
| 686 | |
| 687 | /** |
| 688 | * Is cli Request? |
| 689 | * |
| 690 | * Test to see if a request was made from the command line |
| 691 | * |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 692 | * @return bool |
Phil Sturgeon | c382871 | 2011-01-19 12:31:47 +0000 | [diff] [blame] | 693 | */ |
| 694 | public function is_cli_request() |
| 695 | { |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 696 | return (php_sapi_name() === 'cli' OR defined('STDIN')); |
Phil Sturgeon | c382871 | 2011-01-19 12:31:47 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 699 | // -------------------------------------------------------------------- |
| 700 | |
| 701 | /** |
| 702 | * Get Request Method |
| 703 | * |
Michiel Vugteveen | dc900df | 2012-03-07 20:41:37 +0100 | [diff] [blame] | 704 | * Return the Request Method |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 705 | * |
Michiel Vugteveen | dc900df | 2012-03-07 20:41:37 +0100 | [diff] [blame] | 706 | * @param bool uppercase or lowercase |
Michiel Vugteveen | 7c8841f | 2012-03-07 20:49:06 +0100 | [diff] [blame] | 707 | * @return bool |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 708 | */ |
Michiel Vugteveen | 704fb16 | 2012-03-07 20:42:33 +0100 | [diff] [blame] | 709 | public function method($upper = FALSE) |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 710 | { |
Michiel Vugteveen | dc900df | 2012-03-07 20:41:37 +0100 | [diff] [blame] | 711 | return ($upper) |
| 712 | ? strtoupper($this->server('REQUEST_METHOD')) |
| 713 | : strtolower($this->server('REQUEST_METHOD')); |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 714 | } |
| 715 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 716 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 717 | |
| 718 | /* End of file Input.php */ |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame^] | 719 | /* Location: ./system/core/Input.php */ |