blob: d707fe25cc731fb0c7e5783bf3667670091a5811 [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 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +030050 * User agent strin
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 Andreev1887ec62012-10-27 16:22:07 +030066 * Standartize 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
Phil Sturgeonc8089152010-12-27 19:06:28 +0000124 $this->_allow_get_array = (config_item('allow_get_array') === TRUE);
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200125 $this->_enable_xss = (config_item('global_xss_filtering') === TRUE);
126 $this->_enable_csrf = (config_item('csrf_protection') === TRUE);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600127
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400128 global $SEC;
129 $this->security =& $SEC;
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 {
134 global $UNI;
135 $this->uni =& $UNI;
136 }
137
138 // Sanitize global arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 $this->_sanitize_globals();
140 }
141
142 // --------------------------------------------------------------------
143
144 /**
Greg Akera9263282010-11-10 15:26:43 -0600145 * Fetch from array
146 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300147 * Internal method used to retrieve values from global arrays.
Greg Akera9263282010-11-10 15:26:43 -0600148 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300149 * @param array &$array $_GET, $_POST, $_COOKIE, $_SERVER, etc.
150 * @param string $index Index for item to be fetched from $array
151 * @param bool $xss_clean Whether to apply XSS filtering
152 * @return mixed
Greg Akera9263282010-11-10 15:26:43 -0600153 */
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530154 protected function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 {
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530156 if (isset($array[$index]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530158 $value = $array[$index];
159 }
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530160 elseif (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) // Does the index contain array notation
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530161 {
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530162 $container = $array;
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530163 for ($i = 0; $i < $count; $i++)
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530164 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530165 $key = trim($matches[0][$i], '[]');
166 if($key === '') // The array notation will return the value as array
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530167 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530168 break;
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530169 }
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530170 if (isset($container[$key]))
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530171 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530172 $value = $container = $container[$key];
173 }
174 else
175 {
176 return NULL;
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530177 }
178 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530180 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530182 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
184
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530185 return ($xss_clean === TRUE)
186 ? $this->security->xss_clean($value)
187 : $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
189
190 // --------------------------------------------------------------------
191
192 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400193 * Fetch an item from the GET array
194 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300195 * @param string $index Index for item to be fetched from $_GET
196 * @param bool $xss_clean Whether to apply XSS filtering
197 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400198 */
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530199 public function get($index = NULL, $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000201 // Check if a field has been provided
Andrey Andreev77bd21b2012-11-20 16:34:15 +0200202 if ($index === NULL)
vascopjff1cfa12011-02-13 21:30:19 +0000203 {
Andrey Andreev77bd21b2012-11-20 16:34:15 +0200204 if (empty($_GET))
205 {
206 return array();
207 }
208
Phil Sturgeon44f21052011-02-15 21:39:25 +0000209 $get = array();
vascopjff1cfa12011-02-13 21:30:19 +0000210
211 // loop through the full _GET array
Phil Sturgeon44f21052011-02-15 21:39:25 +0000212 foreach (array_keys($_GET) as $key)
vascopjff1cfa12011-02-13 21:30:19 +0000213 {
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530214 $get[$key] = $this->_fetch_from_array($_GET, $key, $xss_clean);
vascopjff1cfa12011-02-13 21:30:19 +0000215 }
Phil Sturgeon44f21052011-02-15 21:39:25 +0000216 return $get;
vascopjff1cfa12011-02-13 21:30:19 +0000217 }
218
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530219 return $this->_fetch_from_array($_GET, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 }
221
222 // --------------------------------------------------------------------
223
224 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400225 * Fetch an item from the POST array
226 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300227 * @param string $index Index for item to be fetched from $_POST
228 * @param bool $xss_clean Whether to apply XSS filtering
229 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400230 */
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530231 public function post($index = NULL, $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000233 // Check if a field has been provided
Andrey Andreev77bd21b2012-11-20 16:34:15 +0200234 if ($index === NULL)
vascopj0ba58b82011-02-06 14:20:21 +0000235 {
Andrey Andreev77bd21b2012-11-20 16:34:15 +0200236 if (empty($_POST))
237 {
238 return array();
239 }
240
Phil Sturgeon44f21052011-02-15 21:39:25 +0000241 $post = array();
vascopj0ba58b82011-02-06 14:20:21 +0000242
Phil Sturgeon44f21052011-02-15 21:39:25 +0000243 // Loop through the full _POST array and return it
244 foreach (array_keys($_POST) as $key)
vascopj0ba58b82011-02-06 14:20:21 +0000245 {
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530246 $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
vascopj0ba58b82011-02-06 14:20:21 +0000247 }
Phil Sturgeon44f21052011-02-15 21:39:25 +0000248 return $post;
vascopj0ba58b82011-02-06 14:20:21 +0000249 }
David Behler9b5df592011-08-14 21:04:17 +0200250
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530251 return $this->_fetch_from_array($_POST, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 }
253
254 // --------------------------------------------------------------------
255
256 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300257 * Fetch an item from POST data with fallback to GET
Timothy Warren40403d22012-04-19 16:38:50 -0400258 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300259 * @param string $index Index for item to be fetched from $_POST or $_GET
260 * @param bool $xss_clean Whether to apply XSS filtering
261 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400262 */
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530263 public function get_post($index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200265 return isset($_POST[$index])
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530266 ? $this->post($index, $xss_clean)
267 : $this->get($index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 }
269
270 // --------------------------------------------------------------------
271
272 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400273 * Fetch an item from the COOKIE array
274 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300275 * @param string $index Index for item to be fetched from $_COOKIE
276 * @param bool $xss_clean Whether to apply XSS filtering
277 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400278 */
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530279 public function cookie($index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530281 return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 }
283
Andrey Andreev1887ec62012-10-27 16:22:07 +0300284 // --------------------------------------------------------------------
285
286 /**
287 * Fetch an item from the SERVER array
288 *
289 * @param string $index Index for item to be fetched from $_SERVER
290 * @param bool $xss_clean Whether to apply XSS filtering
291 * @return mixed
292 */
293 public function server($index = '', $xss_clean = FALSE)
294 {
295 return $this->_fetch_from_array($_SERVER, $index, $xss_clean);
296 }
297
Derek Jones69fc4fc2010-03-02 13:36:31 -0600298 // ------------------------------------------------------------------------
299
300 /**
Andrey Andreev303eef02012-11-06 14:55:48 +0200301 * Fetch an item from the php://input stream
302 *
303 * Useful when you need to access PUT, DELETE or PATCH request data.
304 *
305 * @param string $index Index for item to be fetched
306 * @param bool $xss_clean Whether to apply XSS filtering
307 * @return mixed
308 */
309 public function input_stream($index = '', $xss_clean = FALSE)
310 {
311 // The input stream can only be read once, so we'll need to check
312 // if we have already done that first.
313 if (is_array($this->_input_stream))
314 {
315 return $this->_fetch_from_array($this->_input_stream, $index, $xss_clean);
316 }
317
318 // Parse the input stream in our cache var
319 parse_str(file_get_contents('php://input'), $this->_input_stream);
320 if ( ! is_array($this->_input_stream))
321 {
322 $this->_input_stream = array();
323 return NULL;
324 }
325
326 return $this->_fetch_from_array($this->_input_stream, $index, $xss_clean);
327 }
328
329 // ------------------------------------------------------------------------
330
331 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400332 * Set cookie
333 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300334 * Accepts an arbitrary number of parameters (up to 7) or an associative
Timothy Warren40403d22012-04-19 16:38:50 -0400335 * array in the first parameter containing all the values.
336 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300337 * @param string|mixed[] $name Cookie name or an array containing parameters
338 * @param string $value Cookie value
339 * @param int $expire Cookie expiration time in seconds
340 * @param string $domain Cookie domain (e.g.: '.yourdomain.com')
341 * @param string $path Cookie path (default: '/')
342 * @param string $prefix Cookie name prefix
343 * @param bool $secure Whether to only transfer cookies via SSL
344 * @param bool $httponly Whether to only makes the cookie accessible via HTTP (no javascript)
Timothy Warren40403d22012-04-19 16:38:50 -0400345 * @return void
346 */
freewil4ad0fd82012-03-13 22:37:42 -0400347 public function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600348 {
349 if (is_array($name))
350 {
tobiasbg9aa7dc92011-02-18 21:57:13 +0100351 // always leave 'name' in last place, as the loop will break otherwise, due to $$item
freewil4ad0fd82012-03-13 22:37:42 -0400352 foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name') as $item)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600353 {
354 if (isset($name[$item]))
355 {
356 $$item = $name[$item];
357 }
358 }
359 }
360
Alex Bilbieed944a32012-06-02 11:07:47 +0100361 if ($prefix === '' && config_item('cookie_prefix') !== '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600362 {
363 $prefix = config_item('cookie_prefix');
364 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300365
366 if ($domain == '' && config_item('cookie_domain') != '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600367 {
368 $domain = config_item('cookie_domain');
369 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300370
Alex Bilbieed944a32012-06-02 11:07:47 +0100371 if ($path === '/' && config_item('cookie_path') !== '/')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600372 {
373 $path = config_item('cookie_path');
374 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300375
Alex Bilbieed944a32012-06-02 11:07:47 +0100376 if ($secure === FALSE && config_item('cookie_secure') !== FALSE)
tobiasbg9aa7dc92011-02-18 21:57:13 +0100377 {
378 $secure = config_item('cookie_secure');
379 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300380
Alex Bilbieed944a32012-06-02 11:07:47 +0100381 if ($httponly === FALSE && config_item('cookie_httponly') !== FALSE)
freewil4ad0fd82012-03-13 22:37:42 -0400382 {
383 $httponly = config_item('cookie_httponly');
384 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600385
386 if ( ! is_numeric($expire))
387 {
388 $expire = time() - 86500;
389 }
390 else
391 {
Phil Sturgeonc8089152010-12-27 19:06:28 +0000392 $expire = ($expire > 0) ? time() + $expire : 0;
Derek Jones69fc4fc2010-03-02 13:36:31 -0600393 }
394
freewil4ad0fd82012-03-13 22:37:42 -0400395 setcookie($prefix.$name, $value, $expire, $path, $domain, $secure, $httponly);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600396 }
397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 // --------------------------------------------------------------------
399
400 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400401 * Fetch the IP Address
402 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300403 * Determines and validates the visitor's IP address.
404 *
405 * @return string IP address
Timothy Warren40403d22012-04-19 16:38:50 -0400406 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800407 public function ip_address()
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
409 if ($this->ip_address !== FALSE)
410 {
411 return $this->ip_address;
412 }
Barry Mienydd671972010-10-04 16:33:58 +0200413
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300414 $proxy_ips = config_item('proxy_ips');
Andrey Andreevea7a8662012-10-09 13:36:31 +0300415 if ( ! empty($proxy_ips) && ! is_array($proxy_ips))
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300416 {
417 $proxy_ips = explode(',', str_replace(' ', '', $proxy_ips));
418 }
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300419
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300420 $this->ip_address = $this->server('REMOTE_ADDR');
421
422 if ($proxy_ips)
423 {
424 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 -0300425 {
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300426 if (($spoof = $this->server($header)) !== NULL)
Jordan Pittman8960acf2012-07-23 09:05:49 -0300427 {
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300428 // Some proxies typically list the whole chain of IP
429 // addresses through which the client has reached us.
430 // e.g. client_ip, proxy_ip1, proxy_ip2, etc.
Andrey Andreeve24eed72012-11-02 23:33:45 +0200431 sscanf($spoof, '%[^,]', $spoof);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300432
433 if ( ! $this->valid_ip($spoof))
434 {
435 $spoof = NULL;
436 }
437 else
438 {
Jordan Pittmana5a71352012-07-20 19:36:43 -0300439 break;
440 }
441 }
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300442 }
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300443
Andrey Andreeve45ad2b2012-10-09 13:11:15 +0300444 if ($spoof)
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300445 {
Andrey Andreev9df35b42012-10-09 13:37:58 +0300446 for ($i = 0, $c = count($proxy_ips); $i < $c; $i++)
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300447 {
448 // Check if we have an IP address or a subnet
449 if (strpos($proxy_ips[$i], '/') === FALSE)
450 {
451 // An IP address (and not a subnet) is specified.
452 // We can compare right away.
453 if ($proxy_ips[$i] === $this->ip_address)
454 {
455 $this->ip_address = $spoof;
456 break;
457 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000458
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300459 continue;
460 }
461
462 // We have a subnet ... now the heavy lifting begins
463 isset($separator) OR $separator = $this->valid_ip($this->ip_address, 'ipv6') ? ':' : '.';
464
465 // If the proxy entry doesn't match the IP protocol - skip it
466 if (strpos($proxy_ips[$i], $separator) === FALSE)
467 {
468 continue;
469 }
470
471 // Convert the REMOTE_ADDR IP address to binary, if needed
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300472 if ( ! isset($ip, $sprintf))
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300473 {
474 if ($separator === ':')
475 {
476 // Make sure we're have the "full" IPv6 format
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300477 $ip = explode(':',
478 str_replace('::',
479 str_repeat(':', 9 - substr_count($this->ip_address, ':')),
480 $this->ip_address
481 )
482 );
483
484 for ($i = 0; $i < 8; $i++)
485 {
486 $ip[$i] = intval($ip[$i], 16);
487 }
488
489 $sprintf = '%016b%016b%016b%016b%016b%016b%016b%016b';
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300490 }
491 else
492 {
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300493 $ip = explode('.', $this->ip_address);
494 $sprintf = '%08b%08b%08b%08b';
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300495 }
496
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300497 $ip = vsprintf($sprintf, $ip);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300498 }
499
500 // Split the netmask length off the network address
Andrey Andreeve24eed72012-11-02 23:33:45 +0200501 sscanf($proxy_ips[$i], '%[^/]/%d', $netaddr, $masklen);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300502
503 // Again, an IPv6 address is most likely in a compressed form
504 if ($separator === ':')
505 {
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300506 $netaddr = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($netaddr, ':')), $netaddr));
507 for ($i = 0; $i < 8; $i++)
508 {
509 $netaddr[$i] = intval($netaddr[$i], 16);
510 }
511 }
512 else
513 {
514 $netaddr = explode('.', $netaddr);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300515 }
516
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300517 // Convert to binary and finally compare
518 if (strncmp($ip, vsprintf($sprintf, $netaddr), $masklen) === 0)
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300519 {
520 $this->ip_address = $spoof;
521 break;
522 }
523 }
524 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 }
526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 if ( ! $this->valid_ip($this->ip_address))
528 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200529 return $this->ip_address = '0.0.0.0';
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 }
531
532 return $this->ip_address;
533 }
534
535 // --------------------------------------------------------------------
536
537 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400538 * Validate IP Address
539 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300540 * @param string $ip IP address
541 * @param string $which IP protocol: 'ipv4' or 'ipv6'
Timothy Warren40403d22012-04-19 16:38:50 -0400542 * @return bool
543 */
Andrey Andreev5a257182012-06-10 06:18:14 +0300544 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 {
Andrey Andreev5a257182012-06-10 06:18:14 +0300546 switch (strtolower($which))
547 {
548 case 'ipv4':
549 $which = FILTER_FLAG_IPV4;
550 break;
551 case 'ipv6':
552 $which = FILTER_FLAG_IPV6;
553 break;
554 default:
555 $which = NULL;
556 break;
557 }
558
559 return (bool) filter_var($ip, FILTER_VALIDATE_IP, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 }
561
562 // --------------------------------------------------------------------
563
564 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300565 * Fetch User Agent string
Timothy Warren40403d22012-04-19 16:38:50 -0400566 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300567 * @return string|null User Agent string or NULL if it doesn't exist
Timothy Warren40403d22012-04-19 16:38:50 -0400568 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800569 public function user_agent()
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 {
571 if ($this->user_agent !== FALSE)
572 {
573 return $this->user_agent;
574 }
575
Andrey Andreev1887ec62012-10-27 16:22:07 +0300576 return $this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 }
578
579 // --------------------------------------------------------------------
580
581 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400582 * Sanitize Globals
583 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300584 * Internal method serving for the following purposes:
Timothy Warren40403d22012-04-19 16:38:50 -0400585 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300586 * - Unsets $_GET data (if query strings are not enabled)
587 * - Unsets all globals if register_globals is enabled
588 * - Cleans POST, COOKIE and SERVER data
589 * - Standardizes newline characters to PHP_EOL
Timothy Warren40403d22012-04-19 16:38:50 -0400590 *
591 * @return void
592 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200593 protected function _sanitize_globals()
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600595 // It would be "wrong" to unset any of these GLOBALS.
Timothy Warren40403d22012-04-19 16:38:50 -0400596 $protected = array(
597 '_SERVER',
598 '_GET',
599 '_POST',
600 '_FILES',
601 '_REQUEST',
602 '_SESSION',
603 '_ENV',
604 'GLOBALS',
605 'HTTP_RAW_POST_DATA',
606 'system_folder',
607 'application_folder',
608 'BM',
609 'EXT',
610 'CFG',
611 'URI',
612 'RTR',
Timothy Warren67cb3ee2012-04-19 16:41:52 -0400613 'OUT',
Timothy Warren40403d22012-04-19 16:38:50 -0400614 'IN'
615 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000616
Andrey Andreev1887ec62012-10-27 16:22:07 +0300617 // Unset globals for security.
Derek Jones69fc4fc2010-03-02 13:36:31 -0600618 // This is effectively the same as register_globals = off
Andrey Andreev1887ec62012-10-27 16:22:07 +0300619 // PHP 5.4 no longer has the register_globals functionality.
620 if ( ! is_php('5.4'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300622 foreach (array($_GET, $_POST, $_COOKIE) as $global)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600623 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300624 if (is_array($global))
Derek Jones69fc4fc2010-03-02 13:36:31 -0600625 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300626 foreach ($global as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600627 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300628 if ( ! in_array($key, $protected))
629 {
630 global $$key;
631 $$key = NULL;
632 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600633 }
634 }
Andrey Andreev1887ec62012-10-27 16:22:07 +0300635 elseif ( ! in_array($global, $protected))
636 {
637 global $$global;
638 $$global = NULL;
639 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300640 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 }
642
Derek Jones69fc4fc2010-03-02 13:36:31 -0600643 // Is $_GET data allowed? If not we'll set the $_GET to an empty array
Alex Bilbieed944a32012-06-02 11:07:47 +0100644 if ($this->_allow_get_array === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600646 $_GET = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200648 elseif (is_array($_GET) && count($_GET) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200650 foreach ($_GET as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200652 $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 }
654 }
655
Derek Jones69fc4fc2010-03-02 13:36:31 -0600656 // Clean $_POST Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200657 if (is_array($_POST) && count($_POST) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500659 foreach ($_POST as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600660 {
661 $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
662 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 }
664
Derek Jones69fc4fc2010-03-02 13:36:31 -0600665 // Clean $_COOKIE Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200666 if (is_array($_COOKIE) && count($_COOKIE) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600668 // Also get rid of specially treated cookies that might be set by a server
669 // or silly application, that are of no use to a CI application anyway
670 // but that when present will trip our 'Disallowed Key Characters' alarm
671 // http://www.ietf.org/rfc/rfc2109.txt
672 // note that the key names below are single quoted strings, and are not PHP variables
673 unset($_COOKIE['$Version']);
674 unset($_COOKIE['$Path']);
675 unset($_COOKIE['$Domain']);
676
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500677 foreach ($_COOKIE as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600678 {
679 $_COOKIE[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
680 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 }
682
Derek Jones69fc4fc2010-03-02 13:36:31 -0600683 // Sanitize PHP_SELF
684 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
685
Derek Jones69fc4fc2010-03-02 13:36:31 -0600686 // CSRF Protection check
Andrey Andreeve45ad2b2012-10-09 13:11:15 +0300687 if ($this->_enable_csrf === TRUE && ! $this->is_cli_request())
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600689 $this->security->csrf_verify();
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000691
Andrey Andreev90cfe142012-01-08 04:46:42 +0200692 log_message('debug', 'Global POST and COOKIE data sanitized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 }
694
695 // --------------------------------------------------------------------
696
697 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400698 * Clean Input Data
699 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300700 * Internal method that aids in escaping data and
701 * standardizing newline characters to PHP_EOL.
Timothy Warren40403d22012-04-19 16:38:50 -0400702 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300703 * @param string|string[] $str Input string(s)
Timothy Warren40403d22012-04-19 16:38:50 -0400704 * @return string
705 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200706 protected function _clean_input_data($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600708 if (is_array($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600710 $new_array = array();
Andrey Andreev1887ec62012-10-27 16:22:07 +0300711 foreach (array_keys($str) as $key)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600712 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300713 $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($str[$key]);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600714 }
715 return $new_array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 }
717
Andrey Andreevaf728622011-10-20 10:11:59 +0300718 /* We strip slashes if magic quotes is on to keep things consistent
719
720 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
721 it will probably not exist in future versions at all.
722 */
723 if ( ! is_php('5.4') && get_magic_quotes_gpc())
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600725 $str = stripslashes($str);
726 }
727
728 // Clean UTF-8 if supported
729 if (UTF8_ENABLED === TRUE)
730 {
731 $str = $this->uni->clean_string($str);
732 }
David Behler9b5df592011-08-14 21:04:17 +0200733
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400734 // Remove control characters
735 $str = remove_invisible_characters($str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600736
737 // Should we filter the input data?
738 if ($this->_enable_xss === TRUE)
739 {
740 $str = $this->security->xss_clean($str);
741 }
742
743 // Standardize newlines if needed
Eric Robertsb75e13d2013-01-27 20:10:09 -0600744 if ($this->_standardize_newlines === TRUE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600745 {
Eric Robertsb75e13d2013-01-27 20:10:09 -0600746 return preg_replace('/(?:\r\n|[\r\n])/', PHP_EOL, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 }
748
749 return $str;
750 }
751
752 // --------------------------------------------------------------------
753
754 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400755 * Clean Keys
756 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300757 * Internal method that helps to prevent malicious users
Timothy Warren40403d22012-04-19 16:38:50 -0400758 * from trying to exploit keys we make sure that keys are
759 * only named with alpha-numeric text and a few other items.
760 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300761 * @param string $str Input string
Timothy Warren40403d22012-04-19 16:38:50 -0400762 * @return string
763 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200764 protected function _clean_input_keys($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 {
bigCat3c0846b2012-08-21 00:20:20 +0800766 if ( ! preg_match('/^[a-z0-9:_\/|-]+$/i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 {
Kevin Cuppd63e4012012-02-05 14:14:32 -0500768 set_status_header(503);
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700769 echo 'Disallowed Key Characters.';
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -0700770 exit(EXIT_USER_INPUT);
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 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300792 // In Apache, you can simply call apache_request_headers()
Greg Akerec2f5712010-11-15 16:22:12 -0600793 if (function_exists('apache_request_headers'))
794 {
795 $headers = apache_request_headers();
796 }
797 else
798 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200799 $headers['Content-Type'] = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : @getenv('CONTENT_TYPE');
Greg Akerec2f5712010-11-15 16:22:12 -0600800
801 foreach ($_SERVER as $key => $val)
802 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200803 if (sscanf($key, 'HTTP_%s', $header) === 1)
Greg Akerec2f5712010-11-15 16:22:12 -0600804 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200805 $headers[$header] = $this->_fetch_from_array($_SERVER, $key, $xss_clean);
Greg Akerec2f5712010-11-15 16:22:12 -0600806 }
807 }
808 }
809
810 // take SOME_HEADER and turn it into Some-Header
811 foreach ($headers as $key => $val)
812 {
Daniel Hunsakerc82b57b2012-12-31 09:22:07 -0700813 $key = str_replace(array('_', '-'), ' ', strtolower($key));
Greg Akerec2f5712010-11-15 16:22:12 -0600814 $key = str_replace(' ', '-', ucwords($key));
David Behler9b5df592011-08-14 21:04:17 +0200815
Greg Akerec2f5712010-11-15 16:22:12 -0600816 $this->headers[$key] = $val;
817 }
David Behler9b5df592011-08-14 21:04:17 +0200818
Greg Akerec2f5712010-11-15 16:22:12 -0600819 return $this->headers;
820 }
821
822 // --------------------------------------------------------------------
823
824 /**
825 * Get Request Header
826 *
827 * Returns the value of a single member of the headers class member
828 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300829 * @param string $index Header name
830 * @param bool $xss_clean Whether to apply XSS filtering
831 * @return string|bool The requested header on success or FALSE on failure
Greg Akerec2f5712010-11-15 16:22:12 -0600832 */
833 public function get_request_header($index, $xss_clean = FALSE)
834 {
835 if (empty($this->headers))
836 {
837 $this->request_headers();
838 }
David Behler9b5df592011-08-14 21:04:17 +0200839
Greg Akerec2f5712010-11-15 16:22:12 -0600840 if ( ! isset($this->headers[$index]))
841 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100842 return NULL;
Greg Akerec2f5712010-11-15 16:22:12 -0600843 }
844
Andrey Andreev9448afb2012-02-08 19:49:19 +0200845 return ($xss_clean === TRUE)
846 ? $this->security->xss_clean($this->headers[$index])
847 : $this->headers[$index];
Greg Akerec2f5712010-11-15 16:22:12 -0600848 }
849
Greg Aker081ac9d2010-11-22 14:42:53 -0600850 // --------------------------------------------------------------------
Phil Sturgeonc3828712011-01-19 12:31:47 +0000851
Greg Aker081ac9d2010-11-22 14:42:53 -0600852 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300853 * Is AJAX request?
Greg Aker081ac9d2010-11-22 14:42:53 -0600854 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300855 * Test to see if a request contains the HTTP_X_REQUESTED_WITH header.
Greg Aker081ac9d2010-11-22 14:42:53 -0600856 *
Andrey Andreev9448afb2012-02-08 19:49:19 +0200857 * @return bool
Greg Aker081ac9d2010-11-22 14:42:53 -0600858 */
859 public function is_ajax_request()
860 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200861 return ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
Greg Aker081ac9d2010-11-22 14:42:53 -0600862 }
863
Phil Sturgeonc3828712011-01-19 12:31:47 +0000864 // --------------------------------------------------------------------
865
866 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300867 * Is CLI request?
Phil Sturgeonc3828712011-01-19 12:31:47 +0000868 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300869 * Test to see if a request was made from the command line.
Phil Sturgeonc3828712011-01-19 12:31:47 +0000870 *
Andrey Andreev9448afb2012-02-08 19:49:19 +0200871 * @return bool
Phil Sturgeonc3828712011-01-19 12:31:47 +0000872 */
873 public function is_cli_request()
874 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200875 return (php_sapi_name() === 'cli' OR defined('STDIN'));
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 */