blob: 35ce5f12f9a75920e45779b64e2eeb9b9136cc61 [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400129 global $SEC;
130 $this->security =& $SEC;
Derek Jones69fc4fc2010-03-02 13:36:31 -0600131
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500132 // Do we need the UTF-8 class?
Derek Jones69fc4fc2010-03-02 13:36:31 -0600133 if (UTF8_ENABLED === TRUE)
134 {
135 global $UNI;
136 $this->uni =& $UNI;
137 }
138
139 // Sanitize global arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 $this->_sanitize_globals();
141 }
142
143 // --------------------------------------------------------------------
144
145 /**
Greg Akera9263282010-11-10 15:26:43 -0600146 * Fetch from array
147 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300148 * Internal method used to retrieve values from global arrays.
Greg Akera9263282010-11-10 15:26:43 -0600149 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300150 * @param array &$array $_GET, $_POST, $_COOKIE, $_SERVER, etc.
151 * @param string $index Index for item to be fetched from $array
152 * @param bool $xss_clean Whether to apply XSS filtering
153 * @return mixed
Greg Akera9263282010-11-10 15:26:43 -0600154 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200155 protected function _fetch_from_array(&$array, $index = NULL, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 {
Andrey Andreev7c60b122014-02-08 18:47:19 +0200157 // If $index is NULL, it means that the whole $array is requested
158 if ($index === NULL)
159 {
160 $output = array();
161 foreach (array_keys($array) as $key)
162 {
163 $output[$key] = $this->_fetch_from_array($array, $key, $xss_clean);
164 }
165
166 return $output;
167 }
168
Andrey Andreev80a16b12014-01-08 17:19:03 +0200169 is_bool($xss_clean) OR $xss_clean = $this->_enable_xss;
170
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530171 if (isset($array[$index]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 {
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530173 $value = $array[$index];
174 }
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530175 elseif (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) // Does the index contain array notation
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530176 {
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530177 $value = $array;
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530178 for ($i = 0; $i < $count; $i++)
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530179 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530180 $key = trim($matches[0][$i], '[]');
nisheeth-barthwal408cbb42013-03-26 19:06:40 +0530181 if ($key === '') // Empty notation will return the value as array
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530182 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530183 break;
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530184 }
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530185
186 if (isset($value[$key]))
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530187 {
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530188 $value = $value[$key];
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530189 }
190 else
191 {
192 return NULL;
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530193 }
194 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 }
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530196 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530198 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
200
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530201 return ($xss_clean === TRUE)
202 ? $this->security->xss_clean($value)
203 : $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 }
205
206 // --------------------------------------------------------------------
207
208 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400209 * Fetch an item from the GET array
210 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300211 * @param string $index Index for item to be fetched from $_GET
212 * @param bool $xss_clean Whether to apply XSS filtering
213 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400214 */
Andrey Andreev80a16b12014-01-08 17:19:03 +0200215 public function get($index = NULL, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 {
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530217 return $this->_fetch_from_array($_GET, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
219
220 // --------------------------------------------------------------------
221
222 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400223 * Fetch an item from the POST array
224 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300225 * @param string $index Index for item to be fetched from $_POST
226 * @param bool $xss_clean Whether to apply XSS filtering
227 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400228 */
Andrey Andreev80a16b12014-01-08 17:19:03 +0200229 public function post($index = NULL, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 {
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530231 return $this->_fetch_from_array($_POST, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 }
233
234 // --------------------------------------------------------------------
235
236 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300237 * Fetch an item from POST data with fallback to GET
Timothy Warren40403d22012-04-19 16:38:50 -0400238 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300239 * @param string $index Index for item to be fetched from $_POST or $_GET
240 * @param bool $xss_clean Whether to apply XSS filtering
241 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400242 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200243 public function post_get($index, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200245 return isset($_POST[$index])
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530246 ? $this->post($index, $xss_clean)
247 : $this->get($index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 }
249
250 // --------------------------------------------------------------------
251
252 /**
vlakoff441fd262013-08-11 20:36:41 +0200253 * Fetch an item from GET data with fallback to POST
254 *
255 * @param string $index Index for item to be fetched from $_GET or $_POST
256 * @param bool $xss_clean Whether to apply XSS filtering
257 * @return mixed
258 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200259 public function get_post($index, $xss_clean = NULL)
vlakoff441fd262013-08-11 20:36:41 +0200260 {
261 return isset($_GET[$index])
262 ? $this->get($index, $xss_clean)
263 : $this->post($index, $xss_clean);
264 }
265
266 // --------------------------------------------------------------------
267
268 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400269 * Fetch an item from the COOKIE array
270 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300271 * @param string $index Index for item to be fetched from $_COOKIE
272 * @param bool $xss_clean Whether to apply XSS filtering
273 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400274 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200275 public function cookie($index = NULL, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530277 return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 }
279
Andrey Andreev1887ec62012-10-27 16:22:07 +0300280 // --------------------------------------------------------------------
281
282 /**
283 * Fetch an item from the SERVER array
284 *
285 * @param string $index Index for item to be fetched from $_SERVER
286 * @param bool $xss_clean Whether to apply XSS filtering
287 * @return mixed
288 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200289 public function server($index, $xss_clean = NULL)
Andrey Andreev1887ec62012-10-27 16:22:07 +0300290 {
291 return $this->_fetch_from_array($_SERVER, $index, $xss_clean);
292 }
293
Derek Jones69fc4fc2010-03-02 13:36:31 -0600294 // ------------------------------------------------------------------------
295
296 /**
Andrey Andreev303eef02012-11-06 14:55:48 +0200297 * Fetch an item from the php://input stream
298 *
299 * Useful when you need to access PUT, DELETE or PATCH request data.
300 *
301 * @param string $index Index for item to be fetched
302 * @param bool $xss_clean Whether to apply XSS filtering
303 * @return mixed
304 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200305 public function input_stream($index = NULL, $xss_clean = NULL)
Andrey Andreev303eef02012-11-06 14:55:48 +0200306 {
307 // The input stream can only be read once, so we'll need to check
308 // if we have already done that first.
Andrey Andreev303eef02012-11-06 14:55:48 +0200309 if ( ! is_array($this->_input_stream))
310 {
Andrey Andreev7c60b122014-02-08 18:47:19 +0200311 parse_str(file_get_contents('php://input'), $this->_input_stream);
312 is_array($this->_input_stream) OR $this->_input_stream = array();
Andrey Andreev303eef02012-11-06 14:55:48 +0200313 }
314
315 return $this->_fetch_from_array($this->_input_stream, $index, $xss_clean);
316 }
317
318 // ------------------------------------------------------------------------
319
320 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400321 * Set cookie
322 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300323 * Accepts an arbitrary number of parameters (up to 7) or an associative
Timothy Warren40403d22012-04-19 16:38:50 -0400324 * array in the first parameter containing all the values.
325 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300326 * @param string|mixed[] $name Cookie name or an array containing parameters
327 * @param string $value Cookie value
328 * @param int $expire Cookie expiration time in seconds
329 * @param string $domain Cookie domain (e.g.: '.yourdomain.com')
330 * @param string $path Cookie path (default: '/')
331 * @param string $prefix Cookie name prefix
332 * @param bool $secure Whether to only transfer cookies via SSL
333 * @param bool $httponly Whether to only makes the cookie accessible via HTTP (no javascript)
Timothy Warren40403d22012-04-19 16:38:50 -0400334 * @return void
335 */
Andrey Andreev8f5420b2014-01-06 10:34:23 +0200336 public function set_cookie($name, $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600337 {
338 if (is_array($name))
339 {
tobiasbg9aa7dc92011-02-18 21:57:13 +0100340 // always leave 'name' in last place, as the loop will break otherwise, due to $$item
freewil4ad0fd82012-03-13 22:37:42 -0400341 foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name') as $item)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600342 {
343 if (isset($name[$item]))
344 {
345 $$item = $name[$item];
346 }
347 }
348 }
349
Alex Bilbieed944a32012-06-02 11:07:47 +0100350 if ($prefix === '' && config_item('cookie_prefix') !== '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600351 {
352 $prefix = config_item('cookie_prefix');
353 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300354
355 if ($domain == '' && config_item('cookie_domain') != '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600356 {
357 $domain = config_item('cookie_domain');
358 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300359
Alex Bilbieed944a32012-06-02 11:07:47 +0100360 if ($path === '/' && config_item('cookie_path') !== '/')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600361 {
362 $path = config_item('cookie_path');
363 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300364
Alex Bilbieed944a32012-06-02 11:07:47 +0100365 if ($secure === FALSE && config_item('cookie_secure') !== FALSE)
tobiasbg9aa7dc92011-02-18 21:57:13 +0100366 {
367 $secure = config_item('cookie_secure');
368 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300369
Alex Bilbieed944a32012-06-02 11:07:47 +0100370 if ($httponly === FALSE && config_item('cookie_httponly') !== FALSE)
freewil4ad0fd82012-03-13 22:37:42 -0400371 {
372 $httponly = config_item('cookie_httponly');
373 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600374
375 if ( ! is_numeric($expire))
376 {
377 $expire = time() - 86500;
378 }
379 else
380 {
Phil Sturgeonc8089152010-12-27 19:06:28 +0000381 $expire = ($expire > 0) ? time() + $expire : 0;
Derek Jones69fc4fc2010-03-02 13:36:31 -0600382 }
383
freewil4ad0fd82012-03-13 22:37:42 -0400384 setcookie($prefix.$name, $value, $expire, $path, $domain, $secure, $httponly);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600385 }
386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 // --------------------------------------------------------------------
388
389 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400390 * Fetch the IP Address
391 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300392 * Determines and validates the visitor's IP address.
393 *
394 * @return string IP address
Timothy Warren40403d22012-04-19 16:38:50 -0400395 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800396 public function ip_address()
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
398 if ($this->ip_address !== FALSE)
399 {
400 return $this->ip_address;
401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300403 $proxy_ips = config_item('proxy_ips');
Andrey Andreevea7a8662012-10-09 13:36:31 +0300404 if ( ! empty($proxy_ips) && ! is_array($proxy_ips))
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300405 {
406 $proxy_ips = explode(',', str_replace(' ', '', $proxy_ips));
407 }
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300408
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300409 $this->ip_address = $this->server('REMOTE_ADDR');
410
411 if ($proxy_ips)
412 {
413 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 -0300414 {
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300415 if (($spoof = $this->server($header)) !== NULL)
Jordan Pittman8960acf2012-07-23 09:05:49 -0300416 {
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300417 // Some proxies typically list the whole chain of IP
418 // addresses through which the client has reached us.
419 // e.g. client_ip, proxy_ip1, proxy_ip2, etc.
Andrey Andreeve24eed72012-11-02 23:33:45 +0200420 sscanf($spoof, '%[^,]', $spoof);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300421
422 if ( ! $this->valid_ip($spoof))
423 {
424 $spoof = NULL;
425 }
426 else
427 {
Jordan Pittmana5a71352012-07-20 19:36:43 -0300428 break;
429 }
430 }
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300431 }
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300432
Andrey Andreeve45ad2b2012-10-09 13:11:15 +0300433 if ($spoof)
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300434 {
Andrey Andreev9df35b42012-10-09 13:37:58 +0300435 for ($i = 0, $c = count($proxy_ips); $i < $c; $i++)
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300436 {
437 // Check if we have an IP address or a subnet
438 if (strpos($proxy_ips[$i], '/') === FALSE)
439 {
440 // An IP address (and not a subnet) is specified.
441 // We can compare right away.
442 if ($proxy_ips[$i] === $this->ip_address)
443 {
444 $this->ip_address = $spoof;
445 break;
446 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000447
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300448 continue;
449 }
450
451 // We have a subnet ... now the heavy lifting begins
452 isset($separator) OR $separator = $this->valid_ip($this->ip_address, 'ipv6') ? ':' : '.';
453
454 // If the proxy entry doesn't match the IP protocol - skip it
455 if (strpos($proxy_ips[$i], $separator) === FALSE)
456 {
457 continue;
458 }
459
460 // Convert the REMOTE_ADDR IP address to binary, if needed
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300461 if ( ! isset($ip, $sprintf))
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300462 {
463 if ($separator === ':')
464 {
465 // Make sure we're have the "full" IPv6 format
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300466 $ip = explode(':',
467 str_replace('::',
468 str_repeat(':', 9 - substr_count($this->ip_address, ':')),
469 $this->ip_address
470 )
471 );
472
473 for ($i = 0; $i < 8; $i++)
474 {
475 $ip[$i] = intval($ip[$i], 16);
476 }
477
478 $sprintf = '%016b%016b%016b%016b%016b%016b%016b%016b';
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300479 }
480 else
481 {
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300482 $ip = explode('.', $this->ip_address);
483 $sprintf = '%08b%08b%08b%08b';
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300484 }
485
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300486 $ip = vsprintf($sprintf, $ip);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300487 }
488
489 // Split the netmask length off the network address
Andrey Andreeve24eed72012-11-02 23:33:45 +0200490 sscanf($proxy_ips[$i], '%[^/]/%d', $netaddr, $masklen);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300491
492 // Again, an IPv6 address is most likely in a compressed form
493 if ($separator === ':')
494 {
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300495 $netaddr = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($netaddr, ':')), $netaddr));
496 for ($i = 0; $i < 8; $i++)
497 {
498 $netaddr[$i] = intval($netaddr[$i], 16);
499 }
500 }
501 else
502 {
503 $netaddr = explode('.', $netaddr);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300504 }
505
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300506 // Convert to binary and finally compare
507 if (strncmp($ip, vsprintf($sprintf, $netaddr), $masklen) === 0)
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300508 {
509 $this->ip_address = $spoof;
510 break;
511 }
512 }
513 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 }
515
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 if ( ! $this->valid_ip($this->ip_address))
517 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200518 return $this->ip_address = '0.0.0.0';
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 }
520
521 return $this->ip_address;
522 }
523
524 // --------------------------------------------------------------------
525
526 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400527 * Validate IP Address
528 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300529 * @param string $ip IP address
530 * @param string $which IP protocol: 'ipv4' or 'ipv6'
Timothy Warren40403d22012-04-19 16:38:50 -0400531 * @return bool
532 */
Andrey Andreev5a257182012-06-10 06:18:14 +0300533 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 {
Andrey Andreev5a257182012-06-10 06:18:14 +0300535 switch (strtolower($which))
536 {
537 case 'ipv4':
538 $which = FILTER_FLAG_IPV4;
539 break;
540 case 'ipv6':
541 $which = FILTER_FLAG_IPV6;
542 break;
543 default:
544 $which = NULL;
545 break;
546 }
547
548 return (bool) filter_var($ip, FILTER_VALIDATE_IP, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 }
550
551 // --------------------------------------------------------------------
552
553 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300554 * Fetch User Agent string
Timothy Warren40403d22012-04-19 16:38:50 -0400555 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300556 * @return string|null User Agent string or NULL if it doesn't exist
Timothy Warren40403d22012-04-19 16:38:50 -0400557 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800558 public function user_agent()
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 {
560 if ($this->user_agent !== FALSE)
561 {
562 return $this->user_agent;
563 }
564
Andrey Andreev1887ec62012-10-27 16:22:07 +0300565 return $this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 }
567
568 // --------------------------------------------------------------------
569
570 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400571 * Sanitize Globals
572 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300573 * Internal method serving for the following purposes:
Timothy Warren40403d22012-04-19 16:38:50 -0400574 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300575 * - Unsets $_GET data (if query strings are not enabled)
576 * - Unsets all globals if register_globals is enabled
577 * - Cleans POST, COOKIE and SERVER data
578 * - Standardizes newline characters to PHP_EOL
Timothy Warren40403d22012-04-19 16:38:50 -0400579 *
580 * @return void
581 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200582 protected function _sanitize_globals()
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600584 // It would be "wrong" to unset any of these GLOBALS.
Timothy Warren40403d22012-04-19 16:38:50 -0400585 $protected = array(
586 '_SERVER',
587 '_GET',
588 '_POST',
589 '_FILES',
590 '_REQUEST',
591 '_SESSION',
592 '_ENV',
593 'GLOBALS',
594 'HTTP_RAW_POST_DATA',
595 'system_folder',
596 'application_folder',
597 'BM',
598 'EXT',
599 'CFG',
600 'URI',
601 'RTR',
Timothy Warren67cb3ee2012-04-19 16:41:52 -0400602 'OUT',
Timothy Warren40403d22012-04-19 16:38:50 -0400603 'IN'
604 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000605
Andrey Andreev1887ec62012-10-27 16:22:07 +0300606 // Unset globals for security.
Derek Jones69fc4fc2010-03-02 13:36:31 -0600607 // This is effectively the same as register_globals = off
Andrey Andreev1887ec62012-10-27 16:22:07 +0300608 // PHP 5.4 no longer has the register_globals functionality.
609 if ( ! is_php('5.4'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300611 foreach (array($_GET, $_POST, $_COOKIE) as $global)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600612 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300613 if (is_array($global))
Derek Jones69fc4fc2010-03-02 13:36:31 -0600614 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300615 foreach ($global as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600616 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300617 if ( ! in_array($key, $protected))
618 {
619 global $$key;
620 $$key = NULL;
621 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600622 }
623 }
Andrey Andreev1887ec62012-10-27 16:22:07 +0300624 elseif ( ! in_array($global, $protected))
625 {
626 global $$global;
627 $$global = NULL;
628 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300629 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 }
631
Derek Jones69fc4fc2010-03-02 13:36:31 -0600632 // Is $_GET data allowed? If not we'll set the $_GET to an empty array
Alex Bilbieed944a32012-06-02 11:07:47 +0100633 if ($this->_allow_get_array === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600635 $_GET = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200637 elseif (is_array($_GET) && count($_GET) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200639 foreach ($_GET as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200641 $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 }
643 }
644
Derek Jones69fc4fc2010-03-02 13:36:31 -0600645 // Clean $_POST Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200646 if (is_array($_POST) && count($_POST) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500648 foreach ($_POST as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600649 {
650 $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
651 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 }
653
Derek Jones69fc4fc2010-03-02 13:36:31 -0600654 // Clean $_COOKIE Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200655 if (is_array($_COOKIE) && count($_COOKIE) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600657 // Also get rid of specially treated cookies that might be set by a server
658 // or silly application, that are of no use to a CI application anyway
659 // but that when present will trip our 'Disallowed Key Characters' alarm
660 // http://www.ietf.org/rfc/rfc2109.txt
661 // note that the key names below are single quoted strings, and are not PHP variables
Andrey Andreev5ac428b2014-01-08 16:07:31 +0200662 unset(
663 $_COOKIE['$Version'],
664 $_COOKIE['$Path'],
665 $_COOKIE['$Domain']
666 );
Derek Jones69fc4fc2010-03-02 13:36:31 -0600667
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500668 foreach ($_COOKIE as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600669 {
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300670 if (($cookie_key = $this->_clean_input_keys($key)) !== FALSE)
671 {
672 $_COOKIE[$cookie_key] = $this->_clean_input_data($val);
673 }
674 else
675 {
676 unset($_COOKIE[$key]);
677 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600678 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 }
680
Derek Jones69fc4fc2010-03-02 13:36:31 -0600681 // Sanitize PHP_SELF
682 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
683
Derek Jones69fc4fc2010-03-02 13:36:31 -0600684 // CSRF Protection check
Andrey Andreevf964b162013-11-12 17:04:55 +0200685 if ($this->_enable_csrf === TRUE && ! is_cli())
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600687 $this->security->csrf_verify();
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000689
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300690 log_message('debug', 'Global POST, GET and COOKIE data sanitized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 }
692
693 // --------------------------------------------------------------------
694
695 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400696 * Clean Input Data
697 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300698 * Internal method that aids in escaping data and
699 * standardizing newline characters to PHP_EOL.
Timothy Warren40403d22012-04-19 16:38:50 -0400700 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300701 * @param string|string[] $str Input string(s)
Timothy Warren40403d22012-04-19 16:38:50 -0400702 * @return string
703 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200704 protected function _clean_input_data($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600706 if (is_array($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600708 $new_array = array();
Andrey Andreev1887ec62012-10-27 16:22:07 +0300709 foreach (array_keys($str) as $key)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600710 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300711 $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($str[$key]);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600712 }
713 return $new_array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 }
715
Andrey Andreevaf728622011-10-20 10:11:59 +0300716 /* We strip slashes if magic quotes is on to keep things consistent
717
718 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
719 it will probably not exist in future versions at all.
720 */
721 if ( ! is_php('5.4') && get_magic_quotes_gpc())
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600723 $str = stripslashes($str);
724 }
725
726 // Clean UTF-8 if supported
727 if (UTF8_ENABLED === TRUE)
728 {
729 $str = $this->uni->clean_string($str);
730 }
David Behler9b5df592011-08-14 21:04:17 +0200731
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400732 // Remove control characters
Andrey Andreev5ac428b2014-01-08 16:07:31 +0200733 $str = remove_invisible_characters($str, FALSE);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600734
Derek Jones69fc4fc2010-03-02 13:36:31 -0600735 // Standardize newlines if needed
Eric Robertsb75e13d2013-01-27 20:10:09 -0600736 if ($this->_standardize_newlines === TRUE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600737 {
Eric Robertsb75e13d2013-01-27 20:10:09 -0600738 return preg_replace('/(?:\r\n|[\r\n])/', PHP_EOL, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 }
740
741 return $str;
742 }
743
744 // --------------------------------------------------------------------
745
746 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400747 * Clean Keys
748 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300749 * Internal method that helps to prevent malicious users
Timothy Warren40403d22012-04-19 16:38:50 -0400750 * from trying to exploit keys we make sure that keys are
751 * only named with alpha-numeric text and a few other items.
752 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300753 * @param string $str Input string
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300754 * @param string $fatal Whether to terminate script exection
755 * or to return FALSE if an invalid
756 * key is encountered
757 * @return string|bool
Timothy Warren40403d22012-04-19 16:38:50 -0400758 */
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300759 protected function _clean_input_keys($str, $fatal = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 {
bigCat3c0846b2012-08-21 00:20:20 +0800761 if ( ! preg_match('/^[a-z0-9:_\/|-]+$/i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 {
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300763 if ($fatal === TRUE)
764 {
765 return FALSE;
766 }
767 else
768 {
769 set_status_header(503);
770 echo 'Disallowed Key Characters.';
771 exit(EXIT_USER_INPUT);
772 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 }
774
Derek Jones69fc4fc2010-03-02 13:36:31 -0600775 // Clean UTF-8 if supported
776 if (UTF8_ENABLED === TRUE)
777 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200778 return $this->uni->clean_string($str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600779 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000780
Derek Jones69fc4fc2010-03-02 13:36:31 -0600781 return $str;
782 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000783
Greg Akerec2f5712010-11-15 16:22:12 -0600784 // --------------------------------------------------------------------
785
786 /**
787 * Request Headers
788 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300789 * @param bool $xss_clean Whether to apply XSS filtering
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200790 * @return array
Greg Akerec2f5712010-11-15 16:22:12 -0600791 */
792 public function request_headers($xss_clean = FALSE)
793 {
CJ71cff1d2013-04-16 21:50:55 +0800794 // If header is already defined, return it immediately
795 if ( ! empty($this->headers))
796 {
797 return $this->headers;
798 }
799
Andrey Andreev1887ec62012-10-27 16:22:07 +0300800 // In Apache, you can simply call apache_request_headers()
Greg Akerec2f5712010-11-15 16:22:12 -0600801 if (function_exists('apache_request_headers'))
802 {
CJ71cff1d2013-04-16 21:50:55 +0800803 return $this->headers = apache_request_headers();
Greg Akerec2f5712010-11-15 16:22:12 -0600804 }
CJd195f222013-04-17 01:04:13 +0800805
CJ8347f912013-04-17 21:45:22 +0800806 $this->headers['Content-Type'] = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : @getenv('CONTENT_TYPE');
CJd195f222013-04-17 01:04:13 +0800807
808 foreach ($_SERVER as $key => $val)
Greg Akerec2f5712010-11-15 16:22:12 -0600809 {
CJd195f222013-04-17 01:04:13 +0800810 if (sscanf($key, 'HTTP_%s', $header) === 1)
Greg Akerec2f5712010-11-15 16:22:12 -0600811 {
CJd195f222013-04-17 01:04:13 +0800812 // take SOME_HEADER and turn it into Some-Header
813 $header = str_replace('_', ' ', strtolower($header));
814 $header = str_replace(' ', '-', ucwords($header));
Greg Akerec2f5712010-11-15 16:22:12 -0600815
CJd195f222013-04-17 01:04:13 +0800816 $this->headers[$header] = $this->_fetch_from_array($_SERVER, $key, $xss_clean);
CJ826990f2013-04-16 14:17:53 +0800817 }
Greg Akerec2f5712010-11-15 16:22:12 -0600818 }
David Behler9b5df592011-08-14 21:04:17 +0200819
Greg Akerec2f5712010-11-15 16:22:12 -0600820 return $this->headers;
821 }
822
823 // --------------------------------------------------------------------
824
825 /**
826 * Get Request Header
827 *
828 * Returns the value of a single member of the headers class member
829 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300830 * @param string $index Header name
831 * @param bool $xss_clean Whether to apply XSS filtering
832 * @return string|bool The requested header on success or FALSE on failure
Greg Akerec2f5712010-11-15 16:22:12 -0600833 */
834 public function get_request_header($index, $xss_clean = FALSE)
835 {
836 if (empty($this->headers))
837 {
838 $this->request_headers();
839 }
David Behler9b5df592011-08-14 21:04:17 +0200840
Greg Akerec2f5712010-11-15 16:22:12 -0600841 if ( ! isset($this->headers[$index]))
842 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100843 return NULL;
Greg Akerec2f5712010-11-15 16:22:12 -0600844 }
845
Andrey Andreev9448afb2012-02-08 19:49:19 +0200846 return ($xss_clean === TRUE)
847 ? $this->security->xss_clean($this->headers[$index])
848 : $this->headers[$index];
Greg Akerec2f5712010-11-15 16:22:12 -0600849 }
850
Greg Aker081ac9d2010-11-22 14:42:53 -0600851 // --------------------------------------------------------------------
Phil Sturgeonc3828712011-01-19 12:31:47 +0000852
Greg Aker081ac9d2010-11-22 14:42:53 -0600853 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300854 * Is AJAX request?
Greg Aker081ac9d2010-11-22 14:42:53 -0600855 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300856 * Test to see if a request contains the HTTP_X_REQUESTED_WITH header.
Greg Aker081ac9d2010-11-22 14:42:53 -0600857 *
Andrey Andreev9448afb2012-02-08 19:49:19 +0200858 * @return bool
Greg Aker081ac9d2010-11-22 14:42:53 -0600859 */
860 public function is_ajax_request()
861 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200862 return ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
Greg Aker081ac9d2010-11-22 14:42:53 -0600863 }
864
Phil Sturgeonc3828712011-01-19 12:31:47 +0000865 // --------------------------------------------------------------------
866
867 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300868 * Is CLI request?
Phil Sturgeonc3828712011-01-19 12:31:47 +0000869 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300870 * Test to see if a request was made from the command line.
Phil Sturgeonc3828712011-01-19 12:31:47 +0000871 *
Andrey Andreevf964b162013-11-12 17:04:55 +0200872 * @deprecated 3.0.0 Use is_cli() instead
873 * @return bool
Phil Sturgeonc3828712011-01-19 12:31:47 +0000874 */
875 public function is_cli_request()
876 {
Andrey Andreevf964b162013-11-12 17:04:55 +0200877 return is_cli();
Phil Sturgeonc3828712011-01-19 12:31:47 +0000878 }
879
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100880 // --------------------------------------------------------------------
881
882 /**
883 * Get Request Method
884 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300885 * Return the request method
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100886 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300887 * @param bool $upper Whether to return in upper or lower case
888 * (default: FALSE)
889 * @return string
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100890 */
Michiel Vugteveen704fb162012-03-07 20:42:33 +0100891 public function method($upper = FALSE)
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100892 {
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100893 return ($upper)
894 ? strtoupper($this->server('REQUEST_METHOD'))
895 : strtolower($this->server('REQUEST_METHOD'));
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100896 }
897
Derek Allard2067d1a2008-11-13 22:59:24 +0000898}
Derek Allard2067d1a2008-11-13 22:59:24 +0000899
900/* End of file Input.php */
Timothy Warren40403d22012-04-19 16:38:50 -0400901/* Location: ./system/core/Input.php */