blob: b8ad7e89c0a75e4df0db881d5293e1f3aebbefac [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev64e98aa2012-01-07 20:29:10 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev64e98aa2012-01-07 20:29:10 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Input Class
31 *
32 * Pre-processes global input data for security
33 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category Input
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/input.html
39 */
40class CI_Input {
Derek Allard2067d1a2008-11-13 22:59:24 +000041
David Behler9b5df592011-08-14 21:04:17 +020042 /**
43 * IP address of the current user
44 *
Andrey Andreev1887ec62012-10-27 16:22:07 +030045 * @var string
David Behler9b5df592011-08-14 21:04:17 +020046 */
Andrey Andreev1887ec62012-10-27 16:22:07 +030047 public $ip_address = FALSE;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030048
David Behler9b5df592011-08-14 21:04:17 +020049 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +030050 * Allow GET array flag
David Behler9b5df592011-08-14 21:04:17 +020051 *
Andrey Andreev1887ec62012-10-27 16:22:07 +030052 * If set to FALSE, then $_GET will be set to an empty array.
David Behler9b5df592011-08-14 21:04:17 +020053 *
Andrey Andreev1887ec62012-10-27 16:22:07 +030054 * @var bool
David Behler9b5df592011-08-14 21:04:17 +020055 */
Andrey Andreev1887ec62012-10-27 16:22:07 +030056 protected $_allow_get_array = TRUE;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030057
David Behler9b5df592011-08-14 21:04:17 +020058 /**
Andrey Andreevbfb635b2014-01-08 18:32:05 +020059 * Standardize new lines flag
David Behler9b5df592011-08-14 21:04:17 +020060 *
Andrey Andreev1887ec62012-10-27 16:22:07 +030061 * If set to TRUE, then newlines are standardized.
62 *
63 * @var bool
David Behler9b5df592011-08-14 21:04:17 +020064 */
Andrey Andreev1887ec62012-10-27 16:22:07 +030065 protected $_standardize_newlines = TRUE;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030066
David Behler9b5df592011-08-14 21:04:17 +020067 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +030068 * Enable XSS flag
69 *
70 * Determines whether the XSS filter is always active when
71 * GET, POST or COOKIE data is encountered.
72 * Set automatically based on config setting.
73 *
74 * @var bool
75 */
76 protected $_enable_xss = FALSE;
77
78 /**
79 * Enable CSRF flag
80 *
David Behler9b5df592011-08-14 21:04:17 +020081 * Enables a CSRF cookie token to be set.
Andrey Andreev1887ec62012-10-27 16:22:07 +030082 * Set automatically based on config setting.
David Behler9b5df592011-08-14 21:04:17 +020083 *
Andrey Andreev1887ec62012-10-27 16:22:07 +030084 * @var bool
David Behler9b5df592011-08-14 21:04:17 +020085 */
Andrey Andreev1887ec62012-10-27 16:22:07 +030086 protected $_enable_csrf = FALSE;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030087
David Behler9b5df592011-08-14 21:04:17 +020088 /**
89 * List of all HTTP request headers
90 *
91 * @var array
92 */
Andrey Andreev1887ec62012-10-27 16:22:07 +030093 protected $headers = array();
David Behler9b5df592011-08-14 21:04:17 +020094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 /**
Andrey Andreev303eef02012-11-06 14:55:48 +020096 * Input stream data
97 *
98 * Parsed from php://input at runtime
99 *
100 * @see CI_Input::input_stream()
101 * @var array
102 */
103 protected $_input_stream = NULL;
104
105 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300106 * Class constructor
Greg Akera9263282010-11-10 15:26:43 -0600107 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300108 * Determines whether to globally enable the XSS processing
109 * and whether to allow the $_GET array.
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300110 *
111 * @return void
Greg Akera9263282010-11-10 15:26:43 -0600112 */
113 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 {
Andrey Andreev13774972012-01-08 04:30:33 +0200115 log_message('debug', 'Input Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000116
Andrey Andreevbfb635b2014-01-08 18:32:05 +0200117 $this->_allow_get_array = (config_item('allow_get_array') === TRUE);
118 $this->_enable_xss = (config_item('global_xss_filtering') === TRUE);
119 $this->_enable_csrf = (config_item('csrf_protection') === TRUE);
fabianozenatti523cda32014-03-21 12:13:03 +0100120 $this->_standardize_newlines = (bool) config_item('standardize_newlines');
Derek Jones69fc4fc2010-03-02 13:36:31 -0600121
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200122 $this->security =& load_class('Security', 'core');
Derek Jones69fc4fc2010-03-02 13:36:31 -0600123
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500124 // Do we need the UTF-8 class?
Derek Jones69fc4fc2010-03-02 13:36:31 -0600125 if (UTF8_ENABLED === TRUE)
126 {
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200127 $this->uni =& load_class('Utf8', 'core');
Derek Jones69fc4fc2010-03-02 13:36:31 -0600128 }
129
130 // Sanitize global arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 $this->_sanitize_globals();
132 }
133
134 // --------------------------------------------------------------------
135
136 /**
Greg Akera9263282010-11-10 15:26:43 -0600137 * Fetch from array
138 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300139 * Internal method used to retrieve values from global arrays.
Greg Akera9263282010-11-10 15:26:43 -0600140 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300141 * @param array &$array $_GET, $_POST, $_COOKIE, $_SERVER, etc.
142 * @param string $index Index for item to be fetched from $array
143 * @param bool $xss_clean Whether to apply XSS filtering
144 * @return mixed
Greg Akera9263282010-11-10 15:26:43 -0600145 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200146 protected function _fetch_from_array(&$array, $index = NULL, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
Andrey Andreev7c60b122014-02-08 18:47:19 +0200148 // If $index is NULL, it means that the whole $array is requested
149 if ($index === NULL)
150 {
151 $output = array();
152 foreach (array_keys($array) as $key)
153 {
154 $output[$key] = $this->_fetch_from_array($array, $key, $xss_clean);
155 }
156
157 return $output;
158 }
159
Andrey Andreev80a16b12014-01-08 17:19:03 +0200160 is_bool($xss_clean) OR $xss_clean = $this->_enable_xss;
161
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530162 if (isset($array[$index]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530164 $value = $array[$index];
165 }
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530166 elseif (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) // Does the index contain array notation
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530167 {
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530168 $value = $array;
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530169 for ($i = 0; $i < $count; $i++)
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530170 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530171 $key = trim($matches[0][$i], '[]');
nisheeth-barthwal408cbb42013-03-26 19:06:40 +0530172 if ($key === '') // Empty notation will return the value as array
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530173 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530174 break;
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530175 }
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530176
177 if (isset($value[$key]))
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530178 {
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530179 $value = $value[$key];
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530180 }
181 else
182 {
183 return NULL;
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530184 }
185 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530187 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530189 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 }
191
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530192 return ($xss_clean === TRUE)
193 ? $this->security->xss_clean($value)
194 : $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 }
196
197 // --------------------------------------------------------------------
198
199 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400200 * Fetch an item from the GET array
201 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300202 * @param string $index Index for item to be fetched from $_GET
203 * @param bool $xss_clean Whether to apply XSS filtering
204 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400205 */
Andrey Andreev80a16b12014-01-08 17:19:03 +0200206 public function get($index = NULL, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530208 return $this->_fetch_from_array($_GET, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 }
210
211 // --------------------------------------------------------------------
212
213 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400214 * Fetch an item from the POST array
215 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300216 * @param string $index Index for item to be fetched from $_POST
217 * @param bool $xss_clean Whether to apply XSS filtering
218 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400219 */
Andrey Andreev80a16b12014-01-08 17:19:03 +0200220 public function post($index = NULL, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530222 return $this->_fetch_from_array($_POST, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
224
225 // --------------------------------------------------------------------
226
227 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300228 * Fetch an item from POST data with fallback to GET
Timothy Warren40403d22012-04-19 16:38:50 -0400229 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300230 * @param string $index Index for item to be fetched from $_POST or $_GET
231 * @param bool $xss_clean Whether to apply XSS filtering
232 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400233 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200234 public function post_get($index, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200236 return isset($_POST[$index])
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530237 ? $this->post($index, $xss_clean)
238 : $this->get($index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 }
240
241 // --------------------------------------------------------------------
242
243 /**
vlakoff441fd262013-08-11 20:36:41 +0200244 * Fetch an item from GET data with fallback to POST
245 *
246 * @param string $index Index for item to be fetched from $_GET or $_POST
247 * @param bool $xss_clean Whether to apply XSS filtering
248 * @return mixed
249 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200250 public function get_post($index, $xss_clean = NULL)
vlakoff441fd262013-08-11 20:36:41 +0200251 {
252 return isset($_GET[$index])
253 ? $this->get($index, $xss_clean)
254 : $this->post($index, $xss_clean);
255 }
256
257 // --------------------------------------------------------------------
258
259 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400260 * Fetch an item from the COOKIE array
261 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300262 * @param string $index Index for item to be fetched from $_COOKIE
263 * @param bool $xss_clean Whether to apply XSS filtering
264 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400265 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200266 public function cookie($index = NULL, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530268 return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 }
270
Andrey Andreev1887ec62012-10-27 16:22:07 +0300271 // --------------------------------------------------------------------
272
273 /**
274 * Fetch an item from the SERVER array
275 *
276 * @param string $index Index for item to be fetched from $_SERVER
277 * @param bool $xss_clean Whether to apply XSS filtering
278 * @return mixed
279 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200280 public function server($index, $xss_clean = NULL)
Andrey Andreev1887ec62012-10-27 16:22:07 +0300281 {
282 return $this->_fetch_from_array($_SERVER, $index, $xss_clean);
283 }
284
Derek Jones69fc4fc2010-03-02 13:36:31 -0600285 // ------------------------------------------------------------------------
286
287 /**
Andrey Andreev303eef02012-11-06 14:55:48 +0200288 * Fetch an item from the php://input stream
289 *
290 * Useful when you need to access PUT, DELETE or PATCH request data.
291 *
292 * @param string $index Index for item to be fetched
293 * @param bool $xss_clean Whether to apply XSS filtering
294 * @return mixed
295 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200296 public function input_stream($index = NULL, $xss_clean = NULL)
Andrey Andreev303eef02012-11-06 14:55:48 +0200297 {
298 // The input stream can only be read once, so we'll need to check
299 // if we have already done that first.
Andrey Andreev303eef02012-11-06 14:55:48 +0200300 if ( ! is_array($this->_input_stream))
301 {
Andrey Andreev7c60b122014-02-08 18:47:19 +0200302 parse_str(file_get_contents('php://input'), $this->_input_stream);
303 is_array($this->_input_stream) OR $this->_input_stream = array();
Andrey Andreev303eef02012-11-06 14:55:48 +0200304 }
305
306 return $this->_fetch_from_array($this->_input_stream, $index, $xss_clean);
307 }
308
309 // ------------------------------------------------------------------------
310
311 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400312 * Set cookie
313 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300314 * Accepts an arbitrary number of parameters (up to 7) or an associative
Timothy Warren40403d22012-04-19 16:38:50 -0400315 * array in the first parameter containing all the values.
316 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300317 * @param string|mixed[] $name Cookie name or an array containing parameters
318 * @param string $value Cookie value
319 * @param int $expire Cookie expiration time in seconds
320 * @param string $domain Cookie domain (e.g.: '.yourdomain.com')
321 * @param string $path Cookie path (default: '/')
322 * @param string $prefix Cookie name prefix
323 * @param bool $secure Whether to only transfer cookies via SSL
324 * @param bool $httponly Whether to only makes the cookie accessible via HTTP (no javascript)
Timothy Warren40403d22012-04-19 16:38:50 -0400325 * @return void
326 */
Andrey Andreev8f5420b2014-01-06 10:34:23 +0200327 public function set_cookie($name, $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600328 {
329 if (is_array($name))
330 {
tobiasbg9aa7dc92011-02-18 21:57:13 +0100331 // always leave 'name' in last place, as the loop will break otherwise, due to $$item
freewil4ad0fd82012-03-13 22:37:42 -0400332 foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name') as $item)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600333 {
334 if (isset($name[$item]))
335 {
336 $$item = $name[$item];
337 }
338 }
339 }
340
Alex Bilbieed944a32012-06-02 11:07:47 +0100341 if ($prefix === '' && config_item('cookie_prefix') !== '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600342 {
343 $prefix = config_item('cookie_prefix');
344 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300345
346 if ($domain == '' && config_item('cookie_domain') != '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600347 {
348 $domain = config_item('cookie_domain');
349 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300350
Alex Bilbieed944a32012-06-02 11:07:47 +0100351 if ($path === '/' && config_item('cookie_path') !== '/')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600352 {
353 $path = config_item('cookie_path');
354 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300355
Alex Bilbieed944a32012-06-02 11:07:47 +0100356 if ($secure === FALSE && config_item('cookie_secure') !== FALSE)
tobiasbg9aa7dc92011-02-18 21:57:13 +0100357 {
358 $secure = config_item('cookie_secure');
359 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300360
Alex Bilbieed944a32012-06-02 11:07:47 +0100361 if ($httponly === FALSE && config_item('cookie_httponly') !== FALSE)
freewil4ad0fd82012-03-13 22:37:42 -0400362 {
363 $httponly = config_item('cookie_httponly');
364 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600365
366 if ( ! is_numeric($expire))
367 {
368 $expire = time() - 86500;
369 }
370 else
371 {
Phil Sturgeonc8089152010-12-27 19:06:28 +0000372 $expire = ($expire > 0) ? time() + $expire : 0;
Derek Jones69fc4fc2010-03-02 13:36:31 -0600373 }
374
freewil4ad0fd82012-03-13 22:37:42 -0400375 setcookie($prefix.$name, $value, $expire, $path, $domain, $secure, $httponly);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600376 }
377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 // --------------------------------------------------------------------
379
380 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400381 * Fetch the IP Address
382 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300383 * Determines and validates the visitor's IP address.
384 *
385 * @return string IP address
Timothy Warren40403d22012-04-19 16:38:50 -0400386 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800387 public function ip_address()
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
389 if ($this->ip_address !== FALSE)
390 {
391 return $this->ip_address;
392 }
Barry Mienydd671972010-10-04 16:33:58 +0200393
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300394 $proxy_ips = config_item('proxy_ips');
Andrey Andreevea7a8662012-10-09 13:36:31 +0300395 if ( ! empty($proxy_ips) && ! is_array($proxy_ips))
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300396 {
397 $proxy_ips = explode(',', str_replace(' ', '', $proxy_ips));
398 }
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300399
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300400 $this->ip_address = $this->server('REMOTE_ADDR');
401
402 if ($proxy_ips)
403 {
404 foreach (array('HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'HTTP_X_CLIENT_IP', 'HTTP_X_CLUSTER_CLIENT_IP') as $header)
Jordan Pittman8960acf2012-07-23 09:05:49 -0300405 {
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300406 if (($spoof = $this->server($header)) !== NULL)
Jordan Pittman8960acf2012-07-23 09:05:49 -0300407 {
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300408 // Some proxies typically list the whole chain of IP
409 // addresses through which the client has reached us.
410 // e.g. client_ip, proxy_ip1, proxy_ip2, etc.
Andrey Andreeve24eed72012-11-02 23:33:45 +0200411 sscanf($spoof, '%[^,]', $spoof);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300412
413 if ( ! $this->valid_ip($spoof))
414 {
415 $spoof = NULL;
416 }
417 else
418 {
Jordan Pittmana5a71352012-07-20 19:36:43 -0300419 break;
420 }
421 }
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300422 }
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300423
Andrey Andreeve45ad2b2012-10-09 13:11:15 +0300424 if ($spoof)
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300425 {
Andrey Andreev9df35b42012-10-09 13:37:58 +0300426 for ($i = 0, $c = count($proxy_ips); $i < $c; $i++)
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300427 {
428 // Check if we have an IP address or a subnet
429 if (strpos($proxy_ips[$i], '/') === FALSE)
430 {
431 // An IP address (and not a subnet) is specified.
432 // We can compare right away.
433 if ($proxy_ips[$i] === $this->ip_address)
434 {
435 $this->ip_address = $spoof;
436 break;
437 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000438
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300439 continue;
440 }
441
442 // We have a subnet ... now the heavy lifting begins
443 isset($separator) OR $separator = $this->valid_ip($this->ip_address, 'ipv6') ? ':' : '.';
444
445 // If the proxy entry doesn't match the IP protocol - skip it
446 if (strpos($proxy_ips[$i], $separator) === FALSE)
447 {
448 continue;
449 }
450
451 // Convert the REMOTE_ADDR IP address to binary, if needed
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300452 if ( ! isset($ip, $sprintf))
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300453 {
454 if ($separator === ':')
455 {
456 // Make sure we're have the "full" IPv6 format
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300457 $ip = explode(':',
458 str_replace('::',
459 str_repeat(':', 9 - substr_count($this->ip_address, ':')),
460 $this->ip_address
461 )
462 );
463
464 for ($i = 0; $i < 8; $i++)
465 {
466 $ip[$i] = intval($ip[$i], 16);
467 }
468
469 $sprintf = '%016b%016b%016b%016b%016b%016b%016b%016b';
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300470 }
471 else
472 {
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300473 $ip = explode('.', $this->ip_address);
474 $sprintf = '%08b%08b%08b%08b';
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300475 }
476
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300477 $ip = vsprintf($sprintf, $ip);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300478 }
479
480 // Split the netmask length off the network address
Andrey Andreeve24eed72012-11-02 23:33:45 +0200481 sscanf($proxy_ips[$i], '%[^/]/%d', $netaddr, $masklen);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300482
483 // Again, an IPv6 address is most likely in a compressed form
484 if ($separator === ':')
485 {
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300486 $netaddr = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($netaddr, ':')), $netaddr));
487 for ($i = 0; $i < 8; $i++)
488 {
489 $netaddr[$i] = intval($netaddr[$i], 16);
490 }
491 }
492 else
493 {
494 $netaddr = explode('.', $netaddr);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300495 }
496
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300497 // Convert to binary and finally compare
498 if (strncmp($ip, vsprintf($sprintf, $netaddr), $masklen) === 0)
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300499 {
500 $this->ip_address = $spoof;
501 break;
502 }
503 }
504 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 }
506
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 if ( ! $this->valid_ip($this->ip_address))
508 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200509 return $this->ip_address = '0.0.0.0';
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 }
511
512 return $this->ip_address;
513 }
514
515 // --------------------------------------------------------------------
516
517 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400518 * Validate IP Address
519 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300520 * @param string $ip IP address
521 * @param string $which IP protocol: 'ipv4' or 'ipv6'
Timothy Warren40403d22012-04-19 16:38:50 -0400522 * @return bool
523 */
Andrey Andreev5a257182012-06-10 06:18:14 +0300524 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
Andrey Andreev5a257182012-06-10 06:18:14 +0300526 switch (strtolower($which))
527 {
528 case 'ipv4':
529 $which = FILTER_FLAG_IPV4;
530 break;
531 case 'ipv6':
532 $which = FILTER_FLAG_IPV6;
533 break;
534 default:
535 $which = NULL;
536 break;
537 }
538
539 return (bool) filter_var($ip, FILTER_VALIDATE_IP, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 }
541
542 // --------------------------------------------------------------------
543
544 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300545 * Fetch User Agent string
Timothy Warren40403d22012-04-19 16:38:50 -0400546 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300547 * @return string|null User Agent string or NULL if it doesn't exist
Timothy Warren40403d22012-04-19 16:38:50 -0400548 */
Andrey Andreev8850e372014-02-27 21:56:06 +0200549 public function user_agent($xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 {
Andrey Andreev8850e372014-02-27 21:56:06 +0200551 return $this->_fetch_from_array($_SERVER, 'HTTP_USER_AGENT', $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 }
553
554 // --------------------------------------------------------------------
555
556 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400557 * Sanitize Globals
558 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300559 * Internal method serving for the following purposes:
Timothy Warren40403d22012-04-19 16:38:50 -0400560 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300561 * - Unsets $_GET data (if query strings are not enabled)
562 * - Unsets all globals if register_globals is enabled
563 * - Cleans POST, COOKIE and SERVER data
564 * - Standardizes newline characters to PHP_EOL
Timothy Warren40403d22012-04-19 16:38:50 -0400565 *
566 * @return void
567 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200568 protected function _sanitize_globals()
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600570 // It would be "wrong" to unset any of these GLOBALS.
Timothy Warren40403d22012-04-19 16:38:50 -0400571 $protected = array(
572 '_SERVER',
573 '_GET',
574 '_POST',
575 '_FILES',
576 '_REQUEST',
577 '_SESSION',
578 '_ENV',
579 'GLOBALS',
580 'HTTP_RAW_POST_DATA',
581 'system_folder',
582 'application_folder',
583 'BM',
584 'EXT',
585 'CFG',
586 'URI',
587 'RTR',
Timothy Warren67cb3ee2012-04-19 16:41:52 -0400588 'OUT',
Timothy Warren40403d22012-04-19 16:38:50 -0400589 'IN'
590 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000591
Andrey Andreev1887ec62012-10-27 16:22:07 +0300592 // Unset globals for security.
Derek Jones69fc4fc2010-03-02 13:36:31 -0600593 // This is effectively the same as register_globals = off
Andrey Andreev1887ec62012-10-27 16:22:07 +0300594 // PHP 5.4 no longer has the register_globals functionality.
595 if ( ! is_php('5.4'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300597 foreach (array($_GET, $_POST, $_COOKIE) as $global)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600598 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300599 if (is_array($global))
Derek Jones69fc4fc2010-03-02 13:36:31 -0600600 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300601 foreach ($global as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600602 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300603 if ( ! in_array($key, $protected))
604 {
605 global $$key;
606 $$key = NULL;
607 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600608 }
609 }
Andrey Andreev1887ec62012-10-27 16:22:07 +0300610 elseif ( ! in_array($global, $protected))
611 {
612 global $$global;
613 $$global = NULL;
614 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300615 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 }
617
Derek Jones69fc4fc2010-03-02 13:36:31 -0600618 // Is $_GET data allowed? If not we'll set the $_GET to an empty array
Alex Bilbieed944a32012-06-02 11:07:47 +0100619 if ($this->_allow_get_array === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600621 $_GET = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200623 elseif (is_array($_GET) && count($_GET) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200625 foreach ($_GET as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200627 $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 }
629 }
630
Derek Jones69fc4fc2010-03-02 13:36:31 -0600631 // Clean $_POST Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200632 if (is_array($_POST) && count($_POST) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500634 foreach ($_POST as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600635 {
636 $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
637 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 }
639
Derek Jones69fc4fc2010-03-02 13:36:31 -0600640 // Clean $_COOKIE Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200641 if (is_array($_COOKIE) && count($_COOKIE) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600643 // Also get rid of specially treated cookies that might be set by a server
644 // or silly application, that are of no use to a CI application anyway
645 // but that when present will trip our 'Disallowed Key Characters' alarm
646 // http://www.ietf.org/rfc/rfc2109.txt
647 // note that the key names below are single quoted strings, and are not PHP variables
Andrey Andreev5ac428b2014-01-08 16:07:31 +0200648 unset(
649 $_COOKIE['$Version'],
650 $_COOKIE['$Path'],
651 $_COOKIE['$Domain']
652 );
Derek Jones69fc4fc2010-03-02 13:36:31 -0600653
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500654 foreach ($_COOKIE as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600655 {
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300656 if (($cookie_key = $this->_clean_input_keys($key)) !== FALSE)
657 {
658 $_COOKIE[$cookie_key] = $this->_clean_input_data($val);
659 }
660 else
661 {
662 unset($_COOKIE[$key]);
663 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600664 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 }
666
Derek Jones69fc4fc2010-03-02 13:36:31 -0600667 // Sanitize PHP_SELF
668 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
669
Derek Jones69fc4fc2010-03-02 13:36:31 -0600670 // CSRF Protection check
Andrey Andreevf964b162013-11-12 17:04:55 +0200671 if ($this->_enable_csrf === TRUE && ! is_cli())
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600673 $this->security->csrf_verify();
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000675
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300676 log_message('debug', 'Global POST, GET and COOKIE data sanitized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 }
678
679 // --------------------------------------------------------------------
680
681 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400682 * Clean Input Data
683 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300684 * Internal method that aids in escaping data and
685 * standardizing newline characters to PHP_EOL.
Timothy Warren40403d22012-04-19 16:38:50 -0400686 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300687 * @param string|string[] $str Input string(s)
Timothy Warren40403d22012-04-19 16:38:50 -0400688 * @return string
689 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200690 protected function _clean_input_data($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600692 if (is_array($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600694 $new_array = array();
Andrey Andreev1887ec62012-10-27 16:22:07 +0300695 foreach (array_keys($str) as $key)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600696 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300697 $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($str[$key]);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600698 }
699 return $new_array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 }
701
Andrey Andreevaf728622011-10-20 10:11:59 +0300702 /* We strip slashes if magic quotes is on to keep things consistent
703
704 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
705 it will probably not exist in future versions at all.
706 */
707 if ( ! is_php('5.4') && get_magic_quotes_gpc())
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600709 $str = stripslashes($str);
710 }
711
712 // Clean UTF-8 if supported
713 if (UTF8_ENABLED === TRUE)
714 {
715 $str = $this->uni->clean_string($str);
716 }
David Behler9b5df592011-08-14 21:04:17 +0200717
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400718 // Remove control characters
Andrey Andreev5ac428b2014-01-08 16:07:31 +0200719 $str = remove_invisible_characters($str, FALSE);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600720
Derek Jones69fc4fc2010-03-02 13:36:31 -0600721 // Standardize newlines if needed
Eric Robertsb75e13d2013-01-27 20:10:09 -0600722 if ($this->_standardize_newlines === TRUE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600723 {
Eric Robertsb75e13d2013-01-27 20:10:09 -0600724 return preg_replace('/(?:\r\n|[\r\n])/', PHP_EOL, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 }
726
727 return $str;
728 }
729
730 // --------------------------------------------------------------------
731
732 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400733 * Clean Keys
734 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300735 * Internal method that helps to prevent malicious users
Timothy Warren40403d22012-04-19 16:38:50 -0400736 * from trying to exploit keys we make sure that keys are
737 * only named with alpha-numeric text and a few other items.
738 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300739 * @param string $str Input string
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300740 * @param string $fatal Whether to terminate script exection
741 * or to return FALSE if an invalid
742 * key is encountered
743 * @return string|bool
Timothy Warren40403d22012-04-19 16:38:50 -0400744 */
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300745 protected function _clean_input_keys($str, $fatal = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 {
bigCat3c0846b2012-08-21 00:20:20 +0800747 if ( ! preg_match('/^[a-z0-9:_\/|-]+$/i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 {
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300749 if ($fatal === TRUE)
750 {
751 return FALSE;
752 }
753 else
754 {
755 set_status_header(503);
756 echo 'Disallowed Key Characters.';
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200757 exit(7); // EXIT_USER_INPUT
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300758 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 }
760
Derek Jones69fc4fc2010-03-02 13:36:31 -0600761 // Clean UTF-8 if supported
762 if (UTF8_ENABLED === TRUE)
763 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200764 return $this->uni->clean_string($str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600765 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000766
Derek Jones69fc4fc2010-03-02 13:36:31 -0600767 return $str;
768 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000769
Greg Akerec2f5712010-11-15 16:22:12 -0600770 // --------------------------------------------------------------------
771
772 /**
773 * Request Headers
774 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300775 * @param bool $xss_clean Whether to apply XSS filtering
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200776 * @return array
Greg Akerec2f5712010-11-15 16:22:12 -0600777 */
778 public function request_headers($xss_clean = FALSE)
779 {
CJ71cff1d2013-04-16 21:50:55 +0800780 // If header is already defined, return it immediately
781 if ( ! empty($this->headers))
782 {
783 return $this->headers;
784 }
785
Andrey Andreev1887ec62012-10-27 16:22:07 +0300786 // In Apache, you can simply call apache_request_headers()
Greg Akerec2f5712010-11-15 16:22:12 -0600787 if (function_exists('apache_request_headers'))
788 {
CJ71cff1d2013-04-16 21:50:55 +0800789 return $this->headers = apache_request_headers();
Greg Akerec2f5712010-11-15 16:22:12 -0600790 }
CJd195f222013-04-17 01:04:13 +0800791
CJ8347f912013-04-17 21:45:22 +0800792 $this->headers['Content-Type'] = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : @getenv('CONTENT_TYPE');
CJd195f222013-04-17 01:04:13 +0800793
794 foreach ($_SERVER as $key => $val)
Greg Akerec2f5712010-11-15 16:22:12 -0600795 {
CJd195f222013-04-17 01:04:13 +0800796 if (sscanf($key, 'HTTP_%s', $header) === 1)
Greg Akerec2f5712010-11-15 16:22:12 -0600797 {
CJd195f222013-04-17 01:04:13 +0800798 // take SOME_HEADER and turn it into Some-Header
799 $header = str_replace('_', ' ', strtolower($header));
800 $header = str_replace(' ', '-', ucwords($header));
Greg Akerec2f5712010-11-15 16:22:12 -0600801
CJd195f222013-04-17 01:04:13 +0800802 $this->headers[$header] = $this->_fetch_from_array($_SERVER, $key, $xss_clean);
CJ826990f2013-04-16 14:17:53 +0800803 }
Greg Akerec2f5712010-11-15 16:22:12 -0600804 }
David Behler9b5df592011-08-14 21:04:17 +0200805
Greg Akerec2f5712010-11-15 16:22:12 -0600806 return $this->headers;
807 }
808
809 // --------------------------------------------------------------------
810
811 /**
812 * Get Request Header
813 *
814 * Returns the value of a single member of the headers class member
815 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300816 * @param string $index Header name
817 * @param bool $xss_clean Whether to apply XSS filtering
818 * @return string|bool The requested header on success or FALSE on failure
Greg Akerec2f5712010-11-15 16:22:12 -0600819 */
820 public function get_request_header($index, $xss_clean = FALSE)
821 {
822 if (empty($this->headers))
823 {
824 $this->request_headers();
825 }
David Behler9b5df592011-08-14 21:04:17 +0200826
Greg Akerec2f5712010-11-15 16:22:12 -0600827 if ( ! isset($this->headers[$index]))
828 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100829 return NULL;
Greg Akerec2f5712010-11-15 16:22:12 -0600830 }
831
Andrey Andreev9448afb2012-02-08 19:49:19 +0200832 return ($xss_clean === TRUE)
833 ? $this->security->xss_clean($this->headers[$index])
834 : $this->headers[$index];
Greg Akerec2f5712010-11-15 16:22:12 -0600835 }
836
Greg Aker081ac9d2010-11-22 14:42:53 -0600837 // --------------------------------------------------------------------
Phil Sturgeonc3828712011-01-19 12:31:47 +0000838
Greg Aker081ac9d2010-11-22 14:42:53 -0600839 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300840 * Is AJAX request?
Greg Aker081ac9d2010-11-22 14:42:53 -0600841 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300842 * Test to see if a request contains the HTTP_X_REQUESTED_WITH header.
Greg Aker081ac9d2010-11-22 14:42:53 -0600843 *
Andrey Andreev9448afb2012-02-08 19:49:19 +0200844 * @return bool
Greg Aker081ac9d2010-11-22 14:42:53 -0600845 */
846 public function is_ajax_request()
847 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200848 return ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
Greg Aker081ac9d2010-11-22 14:42:53 -0600849 }
850
Phil Sturgeonc3828712011-01-19 12:31:47 +0000851 // --------------------------------------------------------------------
852
853 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300854 * Is CLI request?
Phil Sturgeonc3828712011-01-19 12:31:47 +0000855 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300856 * Test to see if a request was made from the command line.
Phil Sturgeonc3828712011-01-19 12:31:47 +0000857 *
Andrey Andreevf964b162013-11-12 17:04:55 +0200858 * @deprecated 3.0.0 Use is_cli() instead
859 * @return bool
Phil Sturgeonc3828712011-01-19 12:31:47 +0000860 */
861 public function is_cli_request()
862 {
Andrey Andreevf964b162013-11-12 17:04:55 +0200863 return is_cli();
Phil Sturgeonc3828712011-01-19 12:31:47 +0000864 }
865
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100866 // --------------------------------------------------------------------
867
868 /**
869 * Get Request Method
870 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300871 * Return the request method
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100872 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300873 * @param bool $upper Whether to return in upper or lower case
874 * (default: FALSE)
875 * @return string
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100876 */
Michiel Vugteveen704fb162012-03-07 20:42:33 +0100877 public function method($upper = FALSE)
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100878 {
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100879 return ($upper)
880 ? strtoupper($this->server('REQUEST_METHOD'))
881 : strtolower($this->server('REQUEST_METHOD'));
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100882 }
883
Derek Allard2067d1a2008-11-13 22:59:24 +0000884}
Derek Allard2067d1a2008-11-13 22:59:24 +0000885
886/* End of file Input.php */
fabianozenatti523cda32014-03-21 12:13:03 +0100887/* Location: ./system/core/Input.php */