blob: 620e50f6311cebac9fe4fb76be62ca59f973b4d9 [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 /**
vlakoffc941d852013-08-06 14:44:40 +020050 * User agent string
David Behler9b5df592011-08-14 21:04:17 +020051 *
Andrey Andreev1887ec62012-10-27 16:22:07 +030052 * @var string
David Behler9b5df592011-08-14 21:04:17 +020053 */
Andrey Andreev1887ec62012-10-27 16:22:07 +030054 public $user_agent = FALSE;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030055
David Behler9b5df592011-08-14 21:04:17 +020056 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +030057 * Allow GET array flag
David Behler9b5df592011-08-14 21:04:17 +020058 *
Andrey Andreev1887ec62012-10-27 16:22:07 +030059 * If set to FALSE, then $_GET will be set to an empty array.
David Behler9b5df592011-08-14 21:04:17 +020060 *
Andrey Andreev1887ec62012-10-27 16:22:07 +030061 * @var bool
David Behler9b5df592011-08-14 21:04:17 +020062 */
Andrey Andreev1887ec62012-10-27 16:22:07 +030063 protected $_allow_get_array = TRUE;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030064
David Behler9b5df592011-08-14 21:04:17 +020065 /**
Andrey Andreevbfb635b2014-01-08 18:32:05 +020066 * Standardize new lines flag
David Behler9b5df592011-08-14 21:04:17 +020067 *
Andrey Andreev1887ec62012-10-27 16:22:07 +030068 * If set to TRUE, then newlines are standardized.
69 *
70 * @var bool
David Behler9b5df592011-08-14 21:04:17 +020071 */
Andrey Andreev1887ec62012-10-27 16:22:07 +030072 protected $_standardize_newlines = TRUE;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030073
David Behler9b5df592011-08-14 21:04:17 +020074 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +030075 * Enable XSS flag
76 *
77 * Determines whether the XSS filter is always active when
78 * GET, POST or COOKIE data is encountered.
79 * Set automatically based on config setting.
80 *
81 * @var bool
82 */
83 protected $_enable_xss = FALSE;
84
85 /**
86 * Enable CSRF flag
87 *
David Behler9b5df592011-08-14 21:04:17 +020088 * Enables a CSRF cookie token to be set.
Andrey Andreev1887ec62012-10-27 16:22:07 +030089 * Set automatically based on config setting.
David Behler9b5df592011-08-14 21:04:17 +020090 *
Andrey Andreev1887ec62012-10-27 16:22:07 +030091 * @var bool
David Behler9b5df592011-08-14 21:04:17 +020092 */
Andrey Andreev1887ec62012-10-27 16:22:07 +030093 protected $_enable_csrf = FALSE;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030094
David Behler9b5df592011-08-14 21:04:17 +020095 /**
96 * List of all HTTP request headers
97 *
98 * @var array
99 */
Andrey Andreev1887ec62012-10-27 16:22:07 +0300100 protected $headers = array();
David Behler9b5df592011-08-14 21:04:17 +0200101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 /**
Andrey Andreev303eef02012-11-06 14:55:48 +0200103 * Input stream data
104 *
105 * Parsed from php://input at runtime
106 *
107 * @see CI_Input::input_stream()
108 * @var array
109 */
110 protected $_input_stream = NULL;
111
112 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300113 * Class constructor
Greg Akera9263282010-11-10 15:26:43 -0600114 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300115 * Determines whether to globally enable the XSS processing
116 * and whether to allow the $_GET array.
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300117 *
118 * @return void
Greg Akera9263282010-11-10 15:26:43 -0600119 */
120 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 {
Andrey Andreev13774972012-01-08 04:30:33 +0200122 log_message('debug', 'Input Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000123
Andrey Andreevbfb635b2014-01-08 18:32:05 +0200124 $this->_allow_get_array = (config_item('allow_get_array') === TRUE);
125 $this->_enable_xss = (config_item('global_xss_filtering') === TRUE);
126 $this->_enable_csrf = (config_item('csrf_protection') === TRUE);
127 $this->_sandardize_newlines = (bool) config_item('standardize_newlines');
Derek Jones69fc4fc2010-03-02 13:36:31 -0600128
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200129 $this->security =& load_class('Security', 'core');
Derek Jones69fc4fc2010-03-02 13:36:31 -0600130
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500131 // Do we need the UTF-8 class?
Derek Jones69fc4fc2010-03-02 13:36:31 -0600132 if (UTF8_ENABLED === TRUE)
133 {
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200134 $this->uni =& load_class('Utf8', 'core');
Derek Jones69fc4fc2010-03-02 13:36:31 -0600135 }
136
137 // Sanitize global arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 $this->_sanitize_globals();
139 }
140
141 // --------------------------------------------------------------------
142
143 /**
Greg Akera9263282010-11-10 15:26:43 -0600144 * Fetch from array
145 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300146 * Internal method used to retrieve values from global arrays.
Greg Akera9263282010-11-10 15:26:43 -0600147 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300148 * @param array &$array $_GET, $_POST, $_COOKIE, $_SERVER, etc.
149 * @param string $index Index for item to be fetched from $array
150 * @param bool $xss_clean Whether to apply XSS filtering
151 * @return mixed
Greg Akera9263282010-11-10 15:26:43 -0600152 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200153 protected function _fetch_from_array(&$array, $index = NULL, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 {
Andrey Andreev7c60b122014-02-08 18:47:19 +0200155 // If $index is NULL, it means that the whole $array is requested
156 if ($index === NULL)
157 {
158 $output = array();
159 foreach (array_keys($array) as $key)
160 {
161 $output[$key] = $this->_fetch_from_array($array, $key, $xss_clean);
162 }
163
164 return $output;
165 }
166
Andrey Andreev80a16b12014-01-08 17:19:03 +0200167 is_bool($xss_clean) OR $xss_clean = $this->_enable_xss;
168
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530169 if (isset($array[$index]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530171 $value = $array[$index];
172 }
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530173 elseif (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) // Does the index contain array notation
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530174 {
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530175 $value = $array;
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530176 for ($i = 0; $i < $count; $i++)
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530177 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530178 $key = trim($matches[0][$i], '[]');
nisheeth-barthwal408cbb42013-03-26 19:06:40 +0530179 if ($key === '') // Empty notation will return the value as array
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530180 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530181 break;
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530182 }
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530183
184 if (isset($value[$key]))
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530185 {
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530186 $value = $value[$key];
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530187 }
188 else
189 {
190 return NULL;
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530191 }
192 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 }
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530194 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530196 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
198
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530199 return ($xss_clean === TRUE)
200 ? $this->security->xss_clean($value)
201 : $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 }
203
204 // --------------------------------------------------------------------
205
206 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400207 * Fetch an item from the GET array
208 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300209 * @param string $index Index for item to be fetched from $_GET
210 * @param bool $xss_clean Whether to apply XSS filtering
211 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400212 */
Andrey Andreev80a16b12014-01-08 17:19:03 +0200213 public function get($index = NULL, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 {
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530215 return $this->_fetch_from_array($_GET, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 }
217
218 // --------------------------------------------------------------------
219
220 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400221 * Fetch an item from the POST array
222 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300223 * @param string $index Index for item to be fetched from $_POST
224 * @param bool $xss_clean Whether to apply XSS filtering
225 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400226 */
Andrey Andreev80a16b12014-01-08 17:19:03 +0200227 public function post($index = NULL, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530229 return $this->_fetch_from_array($_POST, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
231
232 // --------------------------------------------------------------------
233
234 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300235 * Fetch an item from POST data with fallback to GET
Timothy Warren40403d22012-04-19 16:38:50 -0400236 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300237 * @param string $index Index for item to be fetched from $_POST or $_GET
238 * @param bool $xss_clean Whether to apply XSS filtering
239 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400240 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200241 public function post_get($index, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200243 return isset($_POST[$index])
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530244 ? $this->post($index, $xss_clean)
245 : $this->get($index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
247
248 // --------------------------------------------------------------------
249
250 /**
vlakoff441fd262013-08-11 20:36:41 +0200251 * Fetch an item from GET data with fallback to POST
252 *
253 * @param string $index Index for item to be fetched from $_GET or $_POST
254 * @param bool $xss_clean Whether to apply XSS filtering
255 * @return mixed
256 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200257 public function get_post($index, $xss_clean = NULL)
vlakoff441fd262013-08-11 20:36:41 +0200258 {
259 return isset($_GET[$index])
260 ? $this->get($index, $xss_clean)
261 : $this->post($index, $xss_clean);
262 }
263
264 // --------------------------------------------------------------------
265
266 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400267 * Fetch an item from the COOKIE array
268 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300269 * @param string $index Index for item to be fetched from $_COOKIE
270 * @param bool $xss_clean Whether to apply XSS filtering
271 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400272 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200273 public function cookie($index = NULL, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 {
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530275 return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 }
277
Andrey Andreev1887ec62012-10-27 16:22:07 +0300278 // --------------------------------------------------------------------
279
280 /**
281 * Fetch an item from the SERVER array
282 *
283 * @param string $index Index for item to be fetched from $_SERVER
284 * @param bool $xss_clean Whether to apply XSS filtering
285 * @return mixed
286 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200287 public function server($index, $xss_clean = NULL)
Andrey Andreev1887ec62012-10-27 16:22:07 +0300288 {
289 return $this->_fetch_from_array($_SERVER, $index, $xss_clean);
290 }
291
Derek Jones69fc4fc2010-03-02 13:36:31 -0600292 // ------------------------------------------------------------------------
293
294 /**
Andrey Andreev303eef02012-11-06 14:55:48 +0200295 * Fetch an item from the php://input stream
296 *
297 * Useful when you need to access PUT, DELETE or PATCH request data.
298 *
299 * @param string $index Index for item to be fetched
300 * @param bool $xss_clean Whether to apply XSS filtering
301 * @return mixed
302 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200303 public function input_stream($index = NULL, $xss_clean = NULL)
Andrey Andreev303eef02012-11-06 14:55:48 +0200304 {
305 // The input stream can only be read once, so we'll need to check
306 // if we have already done that first.
Andrey Andreev303eef02012-11-06 14:55:48 +0200307 if ( ! is_array($this->_input_stream))
308 {
Andrey Andreev7c60b122014-02-08 18:47:19 +0200309 parse_str(file_get_contents('php://input'), $this->_input_stream);
310 is_array($this->_input_stream) OR $this->_input_stream = array();
Andrey Andreev303eef02012-11-06 14:55:48 +0200311 }
312
313 return $this->_fetch_from_array($this->_input_stream, $index, $xss_clean);
314 }
315
316 // ------------------------------------------------------------------------
317
318 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400319 * Set cookie
320 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300321 * Accepts an arbitrary number of parameters (up to 7) or an associative
Timothy Warren40403d22012-04-19 16:38:50 -0400322 * array in the first parameter containing all the values.
323 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300324 * @param string|mixed[] $name Cookie name or an array containing parameters
325 * @param string $value Cookie value
326 * @param int $expire Cookie expiration time in seconds
327 * @param string $domain Cookie domain (e.g.: '.yourdomain.com')
328 * @param string $path Cookie path (default: '/')
329 * @param string $prefix Cookie name prefix
330 * @param bool $secure Whether to only transfer cookies via SSL
331 * @param bool $httponly Whether to only makes the cookie accessible via HTTP (no javascript)
Timothy Warren40403d22012-04-19 16:38:50 -0400332 * @return void
333 */
Andrey Andreev8f5420b2014-01-06 10:34:23 +0200334 public function set_cookie($name, $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600335 {
336 if (is_array($name))
337 {
tobiasbg9aa7dc92011-02-18 21:57:13 +0100338 // always leave 'name' in last place, as the loop will break otherwise, due to $$item
freewil4ad0fd82012-03-13 22:37:42 -0400339 foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name') as $item)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600340 {
341 if (isset($name[$item]))
342 {
343 $$item = $name[$item];
344 }
345 }
346 }
347
Alex Bilbieed944a32012-06-02 11:07:47 +0100348 if ($prefix === '' && config_item('cookie_prefix') !== '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600349 {
350 $prefix = config_item('cookie_prefix');
351 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300352
353 if ($domain == '' && config_item('cookie_domain') != '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600354 {
355 $domain = config_item('cookie_domain');
356 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300357
Alex Bilbieed944a32012-06-02 11:07:47 +0100358 if ($path === '/' && config_item('cookie_path') !== '/')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600359 {
360 $path = config_item('cookie_path');
361 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300362
Alex Bilbieed944a32012-06-02 11:07:47 +0100363 if ($secure === FALSE && config_item('cookie_secure') !== FALSE)
tobiasbg9aa7dc92011-02-18 21:57:13 +0100364 {
365 $secure = config_item('cookie_secure');
366 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300367
Alex Bilbieed944a32012-06-02 11:07:47 +0100368 if ($httponly === FALSE && config_item('cookie_httponly') !== FALSE)
freewil4ad0fd82012-03-13 22:37:42 -0400369 {
370 $httponly = config_item('cookie_httponly');
371 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600372
373 if ( ! is_numeric($expire))
374 {
375 $expire = time() - 86500;
376 }
377 else
378 {
Phil Sturgeonc8089152010-12-27 19:06:28 +0000379 $expire = ($expire > 0) ? time() + $expire : 0;
Derek Jones69fc4fc2010-03-02 13:36:31 -0600380 }
381
freewil4ad0fd82012-03-13 22:37:42 -0400382 setcookie($prefix.$name, $value, $expire, $path, $domain, $secure, $httponly);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600383 }
384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 // --------------------------------------------------------------------
386
387 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400388 * Fetch the IP Address
389 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300390 * Determines and validates the visitor's IP address.
391 *
392 * @return string IP address
Timothy Warren40403d22012-04-19 16:38:50 -0400393 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800394 public function ip_address()
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
396 if ($this->ip_address !== FALSE)
397 {
398 return $this->ip_address;
399 }
Barry Mienydd671972010-10-04 16:33:58 +0200400
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300401 $proxy_ips = config_item('proxy_ips');
Andrey Andreevea7a8662012-10-09 13:36:31 +0300402 if ( ! empty($proxy_ips) && ! is_array($proxy_ips))
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300403 {
404 $proxy_ips = explode(',', str_replace(' ', '', $proxy_ips));
405 }
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300406
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300407 $this->ip_address = $this->server('REMOTE_ADDR');
408
409 if ($proxy_ips)
410 {
411 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 -0300412 {
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300413 if (($spoof = $this->server($header)) !== NULL)
Jordan Pittman8960acf2012-07-23 09:05:49 -0300414 {
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300415 // Some proxies typically list the whole chain of IP
416 // addresses through which the client has reached us.
417 // e.g. client_ip, proxy_ip1, proxy_ip2, etc.
Andrey Andreeve24eed72012-11-02 23:33:45 +0200418 sscanf($spoof, '%[^,]', $spoof);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300419
420 if ( ! $this->valid_ip($spoof))
421 {
422 $spoof = NULL;
423 }
424 else
425 {
Jordan Pittmana5a71352012-07-20 19:36:43 -0300426 break;
427 }
428 }
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300429 }
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300430
Andrey Andreeve45ad2b2012-10-09 13:11:15 +0300431 if ($spoof)
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300432 {
Andrey Andreev9df35b42012-10-09 13:37:58 +0300433 for ($i = 0, $c = count($proxy_ips); $i < $c; $i++)
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300434 {
435 // Check if we have an IP address or a subnet
436 if (strpos($proxy_ips[$i], '/') === FALSE)
437 {
438 // An IP address (and not a subnet) is specified.
439 // We can compare right away.
440 if ($proxy_ips[$i] === $this->ip_address)
441 {
442 $this->ip_address = $spoof;
443 break;
444 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000445
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300446 continue;
447 }
448
449 // We have a subnet ... now the heavy lifting begins
450 isset($separator) OR $separator = $this->valid_ip($this->ip_address, 'ipv6') ? ':' : '.';
451
452 // If the proxy entry doesn't match the IP protocol - skip it
453 if (strpos($proxy_ips[$i], $separator) === FALSE)
454 {
455 continue;
456 }
457
458 // Convert the REMOTE_ADDR IP address to binary, if needed
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300459 if ( ! isset($ip, $sprintf))
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300460 {
461 if ($separator === ':')
462 {
463 // Make sure we're have the "full" IPv6 format
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300464 $ip = explode(':',
465 str_replace('::',
466 str_repeat(':', 9 - substr_count($this->ip_address, ':')),
467 $this->ip_address
468 )
469 );
470
471 for ($i = 0; $i < 8; $i++)
472 {
473 $ip[$i] = intval($ip[$i], 16);
474 }
475
476 $sprintf = '%016b%016b%016b%016b%016b%016b%016b%016b';
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300477 }
478 else
479 {
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300480 $ip = explode('.', $this->ip_address);
481 $sprintf = '%08b%08b%08b%08b';
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300482 }
483
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300484 $ip = vsprintf($sprintf, $ip);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300485 }
486
487 // Split the netmask length off the network address
Andrey Andreeve24eed72012-11-02 23:33:45 +0200488 sscanf($proxy_ips[$i], '%[^/]/%d', $netaddr, $masklen);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300489
490 // Again, an IPv6 address is most likely in a compressed form
491 if ($separator === ':')
492 {
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300493 $netaddr = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($netaddr, ':')), $netaddr));
494 for ($i = 0; $i < 8; $i++)
495 {
496 $netaddr[$i] = intval($netaddr[$i], 16);
497 }
498 }
499 else
500 {
501 $netaddr = explode('.', $netaddr);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300502 }
503
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300504 // Convert to binary and finally compare
505 if (strncmp($ip, vsprintf($sprintf, $netaddr), $masklen) === 0)
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300506 {
507 $this->ip_address = $spoof;
508 break;
509 }
510 }
511 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 }
513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 if ( ! $this->valid_ip($this->ip_address))
515 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200516 return $this->ip_address = '0.0.0.0';
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 }
518
519 return $this->ip_address;
520 }
521
522 // --------------------------------------------------------------------
523
524 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400525 * Validate IP Address
526 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300527 * @param string $ip IP address
528 * @param string $which IP protocol: 'ipv4' or 'ipv6'
Timothy Warren40403d22012-04-19 16:38:50 -0400529 * @return bool
530 */
Andrey Andreev5a257182012-06-10 06:18:14 +0300531 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 {
Andrey Andreev5a257182012-06-10 06:18:14 +0300533 switch (strtolower($which))
534 {
535 case 'ipv4':
536 $which = FILTER_FLAG_IPV4;
537 break;
538 case 'ipv6':
539 $which = FILTER_FLAG_IPV6;
540 break;
541 default:
542 $which = NULL;
543 break;
544 }
545
546 return (bool) filter_var($ip, FILTER_VALIDATE_IP, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 }
548
549 // --------------------------------------------------------------------
550
551 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300552 * Fetch User Agent string
Timothy Warren40403d22012-04-19 16:38:50 -0400553 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300554 * @return string|null User Agent string or NULL if it doesn't exist
Timothy Warren40403d22012-04-19 16:38:50 -0400555 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800556 public function user_agent()
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 {
558 if ($this->user_agent !== FALSE)
559 {
560 return $this->user_agent;
561 }
562
Andrey Andreev1887ec62012-10-27 16:22:07 +0300563 return $this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 }
565
566 // --------------------------------------------------------------------
567
568 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400569 * Sanitize Globals
570 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300571 * Internal method serving for the following purposes:
Timothy Warren40403d22012-04-19 16:38:50 -0400572 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300573 * - Unsets $_GET data (if query strings are not enabled)
574 * - Unsets all globals if register_globals is enabled
575 * - Cleans POST, COOKIE and SERVER data
576 * - Standardizes newline characters to PHP_EOL
Timothy Warren40403d22012-04-19 16:38:50 -0400577 *
578 * @return void
579 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200580 protected function _sanitize_globals()
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600582 // It would be "wrong" to unset any of these GLOBALS.
Timothy Warren40403d22012-04-19 16:38:50 -0400583 $protected = array(
584 '_SERVER',
585 '_GET',
586 '_POST',
587 '_FILES',
588 '_REQUEST',
589 '_SESSION',
590 '_ENV',
591 'GLOBALS',
592 'HTTP_RAW_POST_DATA',
593 'system_folder',
594 'application_folder',
595 'BM',
596 'EXT',
597 'CFG',
598 'URI',
599 'RTR',
Timothy Warren67cb3ee2012-04-19 16:41:52 -0400600 'OUT',
Timothy Warren40403d22012-04-19 16:38:50 -0400601 'IN'
602 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000603
Andrey Andreev1887ec62012-10-27 16:22:07 +0300604 // Unset globals for security.
Derek Jones69fc4fc2010-03-02 13:36:31 -0600605 // This is effectively the same as register_globals = off
Andrey Andreev1887ec62012-10-27 16:22:07 +0300606 // PHP 5.4 no longer has the register_globals functionality.
607 if ( ! is_php('5.4'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300609 foreach (array($_GET, $_POST, $_COOKIE) as $global)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600610 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300611 if (is_array($global))
Derek Jones69fc4fc2010-03-02 13:36:31 -0600612 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300613 foreach ($global as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600614 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300615 if ( ! in_array($key, $protected))
616 {
617 global $$key;
618 $$key = NULL;
619 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600620 }
621 }
Andrey Andreev1887ec62012-10-27 16:22:07 +0300622 elseif ( ! in_array($global, $protected))
623 {
624 global $$global;
625 $$global = NULL;
626 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300627 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 }
629
Derek Jones69fc4fc2010-03-02 13:36:31 -0600630 // Is $_GET data allowed? If not we'll set the $_GET to an empty array
Alex Bilbieed944a32012-06-02 11:07:47 +0100631 if ($this->_allow_get_array === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600633 $_GET = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200635 elseif (is_array($_GET) && count($_GET) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200637 foreach ($_GET as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200639 $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 }
641 }
642
Derek Jones69fc4fc2010-03-02 13:36:31 -0600643 // Clean $_POST Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200644 if (is_array($_POST) && count($_POST) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500646 foreach ($_POST as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600647 {
648 $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
649 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 }
651
Derek Jones69fc4fc2010-03-02 13:36:31 -0600652 // Clean $_COOKIE Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200653 if (is_array($_COOKIE) && count($_COOKIE) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600655 // Also get rid of specially treated cookies that might be set by a server
656 // or silly application, that are of no use to a CI application anyway
657 // but that when present will trip our 'Disallowed Key Characters' alarm
658 // http://www.ietf.org/rfc/rfc2109.txt
659 // note that the key names below are single quoted strings, and are not PHP variables
Andrey Andreev5ac428b2014-01-08 16:07:31 +0200660 unset(
661 $_COOKIE['$Version'],
662 $_COOKIE['$Path'],
663 $_COOKIE['$Domain']
664 );
Derek Jones69fc4fc2010-03-02 13:36:31 -0600665
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500666 foreach ($_COOKIE as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600667 {
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300668 if (($cookie_key = $this->_clean_input_keys($key)) !== FALSE)
669 {
670 $_COOKIE[$cookie_key] = $this->_clean_input_data($val);
671 }
672 else
673 {
674 unset($_COOKIE[$key]);
675 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600676 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 }
678
Derek Jones69fc4fc2010-03-02 13:36:31 -0600679 // Sanitize PHP_SELF
680 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
681
Derek Jones69fc4fc2010-03-02 13:36:31 -0600682 // CSRF Protection check
Andrey Andreevf964b162013-11-12 17:04:55 +0200683 if ($this->_enable_csrf === TRUE && ! is_cli())
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600685 $this->security->csrf_verify();
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000687
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300688 log_message('debug', 'Global POST, GET and COOKIE data sanitized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 }
690
691 // --------------------------------------------------------------------
692
693 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400694 * Clean Input Data
695 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300696 * Internal method that aids in escaping data and
697 * standardizing newline characters to PHP_EOL.
Timothy Warren40403d22012-04-19 16:38:50 -0400698 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300699 * @param string|string[] $str Input string(s)
Timothy Warren40403d22012-04-19 16:38:50 -0400700 * @return string
701 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200702 protected function _clean_input_data($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600704 if (is_array($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600706 $new_array = array();
Andrey Andreev1887ec62012-10-27 16:22:07 +0300707 foreach (array_keys($str) as $key)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600708 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300709 $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($str[$key]);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600710 }
711 return $new_array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 }
713
Andrey Andreevaf728622011-10-20 10:11:59 +0300714 /* We strip slashes if magic quotes is on to keep things consistent
715
716 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
717 it will probably not exist in future versions at all.
718 */
719 if ( ! is_php('5.4') && get_magic_quotes_gpc())
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600721 $str = stripslashes($str);
722 }
723
724 // Clean UTF-8 if supported
725 if (UTF8_ENABLED === TRUE)
726 {
727 $str = $this->uni->clean_string($str);
728 }
David Behler9b5df592011-08-14 21:04:17 +0200729
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400730 // Remove control characters
Andrey Andreev5ac428b2014-01-08 16:07:31 +0200731 $str = remove_invisible_characters($str, FALSE);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600732
Derek Jones69fc4fc2010-03-02 13:36:31 -0600733 // Standardize newlines if needed
Eric Robertsb75e13d2013-01-27 20:10:09 -0600734 if ($this->_standardize_newlines === TRUE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600735 {
Eric Robertsb75e13d2013-01-27 20:10:09 -0600736 return preg_replace('/(?:\r\n|[\r\n])/', PHP_EOL, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 }
738
739 return $str;
740 }
741
742 // --------------------------------------------------------------------
743
744 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400745 * Clean Keys
746 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300747 * Internal method that helps to prevent malicious users
Timothy Warren40403d22012-04-19 16:38:50 -0400748 * from trying to exploit keys we make sure that keys are
749 * only named with alpha-numeric text and a few other items.
750 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300751 * @param string $str Input string
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300752 * @param string $fatal Whether to terminate script exection
753 * or to return FALSE if an invalid
754 * key is encountered
755 * @return string|bool
Timothy Warren40403d22012-04-19 16:38:50 -0400756 */
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300757 protected function _clean_input_keys($str, $fatal = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 {
bigCat3c0846b2012-08-21 00:20:20 +0800759 if ( ! preg_match('/^[a-z0-9:_\/|-]+$/i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 {
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300761 if ($fatal === TRUE)
762 {
763 return FALSE;
764 }
765 else
766 {
767 set_status_header(503);
768 echo 'Disallowed Key Characters.';
769 exit(EXIT_USER_INPUT);
770 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 }
772
Derek Jones69fc4fc2010-03-02 13:36:31 -0600773 // Clean UTF-8 if supported
774 if (UTF8_ENABLED === TRUE)
775 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200776 return $this->uni->clean_string($str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600777 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000778
Derek Jones69fc4fc2010-03-02 13:36:31 -0600779 return $str;
780 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000781
Greg Akerec2f5712010-11-15 16:22:12 -0600782 // --------------------------------------------------------------------
783
784 /**
785 * Request Headers
786 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300787 * @param bool $xss_clean Whether to apply XSS filtering
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200788 * @return array
Greg Akerec2f5712010-11-15 16:22:12 -0600789 */
790 public function request_headers($xss_clean = FALSE)
791 {
CJ71cff1d2013-04-16 21:50:55 +0800792 // If header is already defined, return it immediately
793 if ( ! empty($this->headers))
794 {
795 return $this->headers;
796 }
797
Andrey Andreev1887ec62012-10-27 16:22:07 +0300798 // In Apache, you can simply call apache_request_headers()
Greg Akerec2f5712010-11-15 16:22:12 -0600799 if (function_exists('apache_request_headers'))
800 {
CJ71cff1d2013-04-16 21:50:55 +0800801 return $this->headers = apache_request_headers();
Greg Akerec2f5712010-11-15 16:22:12 -0600802 }
CJd195f222013-04-17 01:04:13 +0800803
CJ8347f912013-04-17 21:45:22 +0800804 $this->headers['Content-Type'] = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : @getenv('CONTENT_TYPE');
CJd195f222013-04-17 01:04:13 +0800805
806 foreach ($_SERVER as $key => $val)
Greg Akerec2f5712010-11-15 16:22:12 -0600807 {
CJd195f222013-04-17 01:04:13 +0800808 if (sscanf($key, 'HTTP_%s', $header) === 1)
Greg Akerec2f5712010-11-15 16:22:12 -0600809 {
CJd195f222013-04-17 01:04:13 +0800810 // take SOME_HEADER and turn it into Some-Header
811 $header = str_replace('_', ' ', strtolower($header));
812 $header = str_replace(' ', '-', ucwords($header));
Greg Akerec2f5712010-11-15 16:22:12 -0600813
CJd195f222013-04-17 01:04:13 +0800814 $this->headers[$header] = $this->_fetch_from_array($_SERVER, $key, $xss_clean);
CJ826990f2013-04-16 14:17:53 +0800815 }
Greg Akerec2f5712010-11-15 16:22:12 -0600816 }
David Behler9b5df592011-08-14 21:04:17 +0200817
Greg Akerec2f5712010-11-15 16:22:12 -0600818 return $this->headers;
819 }
820
821 // --------------------------------------------------------------------
822
823 /**
824 * Get Request Header
825 *
826 * Returns the value of a single member of the headers class member
827 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300828 * @param string $index Header name
829 * @param bool $xss_clean Whether to apply XSS filtering
830 * @return string|bool The requested header on success or FALSE on failure
Greg Akerec2f5712010-11-15 16:22:12 -0600831 */
832 public function get_request_header($index, $xss_clean = FALSE)
833 {
834 if (empty($this->headers))
835 {
836 $this->request_headers();
837 }
David Behler9b5df592011-08-14 21:04:17 +0200838
Greg Akerec2f5712010-11-15 16:22:12 -0600839 if ( ! isset($this->headers[$index]))
840 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100841 return NULL;
Greg Akerec2f5712010-11-15 16:22:12 -0600842 }
843
Andrey Andreev9448afb2012-02-08 19:49:19 +0200844 return ($xss_clean === TRUE)
845 ? $this->security->xss_clean($this->headers[$index])
846 : $this->headers[$index];
Greg Akerec2f5712010-11-15 16:22:12 -0600847 }
848
Greg Aker081ac9d2010-11-22 14:42:53 -0600849 // --------------------------------------------------------------------
Phil Sturgeonc3828712011-01-19 12:31:47 +0000850
Greg Aker081ac9d2010-11-22 14:42:53 -0600851 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300852 * Is AJAX request?
Greg Aker081ac9d2010-11-22 14:42:53 -0600853 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300854 * Test to see if a request contains the HTTP_X_REQUESTED_WITH header.
Greg Aker081ac9d2010-11-22 14:42:53 -0600855 *
Andrey Andreev9448afb2012-02-08 19:49:19 +0200856 * @return bool
Greg Aker081ac9d2010-11-22 14:42:53 -0600857 */
858 public function is_ajax_request()
859 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200860 return ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
Greg Aker081ac9d2010-11-22 14:42:53 -0600861 }
862
Phil Sturgeonc3828712011-01-19 12:31:47 +0000863 // --------------------------------------------------------------------
864
865 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300866 * Is CLI request?
Phil Sturgeonc3828712011-01-19 12:31:47 +0000867 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300868 * Test to see if a request was made from the command line.
Phil Sturgeonc3828712011-01-19 12:31:47 +0000869 *
Andrey Andreevf964b162013-11-12 17:04:55 +0200870 * @deprecated 3.0.0 Use is_cli() instead
871 * @return bool
Phil Sturgeonc3828712011-01-19 12:31:47 +0000872 */
873 public function is_cli_request()
874 {
Andrey Andreevf964b162013-11-12 17:04:55 +0200875 return is_cli();
Phil Sturgeonc3828712011-01-19 12:31:47 +0000876 }
877
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100878 // --------------------------------------------------------------------
879
880 /**
881 * Get Request Method
882 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300883 * Return the request method
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100884 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300885 * @param bool $upper Whether to return in upper or lower case
886 * (default: FALSE)
887 * @return string
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100888 */
Michiel Vugteveen704fb162012-03-07 20:42:33 +0100889 public function method($upper = FALSE)
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100890 {
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100891 return ($upper)
892 ? strtoupper($this->server('REQUEST_METHOD'))
893 : strtolower($this->server('REQUEST_METHOD'));
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100894 }
895
Derek Allard2067d1a2008-11-13 22:59:24 +0000896}
Derek Allard2067d1a2008-11-13 22:59:24 +0000897
898/* End of file Input.php */
Timothy Warren40403d22012-04-19 16:38:50 -0400899/* Location: ./system/core/Input.php */