blob: 384e30d43ffa9eefa099e53d3c970203989f523d [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 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-barthwal47ea5a82013-03-26 18:57:28 +0530160 elseif (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) // Does the index contain array notation
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530161 {
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530162 $value = $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], '[]');
nisheeth-barthwal408cbb42013-03-26 19:06:40 +0530166 if ($key === '') // Empty 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-barthwal47ea5a82013-03-26 18:57:28 +0530170
171 if (isset($value[$key]))
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530172 {
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530173 $value = $value[$key];
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530174 }
175 else
176 {
177 return NULL;
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530178 }
179 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 }
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530181 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530183 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
185
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530186 return ($xss_clean === TRUE)
187 ? $this->security->xss_clean($value)
188 : $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 }
190
191 // --------------------------------------------------------------------
192
193 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400194 * Fetch an item from the GET array
195 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300196 * @param string $index Index for item to be fetched from $_GET
197 * @param bool $xss_clean Whether to apply XSS filtering
198 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400199 */
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530200 public function get($index = NULL, $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000202 // Check if a field has been provided
Andrey Andreev77bd21b2012-11-20 16:34:15 +0200203 if ($index === NULL)
vascopjff1cfa12011-02-13 21:30:19 +0000204 {
Andrey Andreev77bd21b2012-11-20 16:34:15 +0200205 if (empty($_GET))
206 {
207 return array();
208 }
209
Phil Sturgeon44f21052011-02-15 21:39:25 +0000210 $get = array();
vascopjff1cfa12011-02-13 21:30:19 +0000211
212 // loop through the full _GET array
Phil Sturgeon44f21052011-02-15 21:39:25 +0000213 foreach (array_keys($_GET) as $key)
vascopjff1cfa12011-02-13 21:30:19 +0000214 {
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530215 $get[$key] = $this->_fetch_from_array($_GET, $key, $xss_clean);
vascopjff1cfa12011-02-13 21:30:19 +0000216 }
Phil Sturgeon44f21052011-02-15 21:39:25 +0000217 return $get;
vascopjff1cfa12011-02-13 21:30:19 +0000218 }
219
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530220 return $this->_fetch_from_array($_GET, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
222
223 // --------------------------------------------------------------------
224
225 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400226 * Fetch an item from the POST array
227 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300228 * @param string $index Index for item to be fetched from $_POST
229 * @param bool $xss_clean Whether to apply XSS filtering
230 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400231 */
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530232 public function post($index = NULL, $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000234 // Check if a field has been provided
Andrey Andreev77bd21b2012-11-20 16:34:15 +0200235 if ($index === NULL)
vascopj0ba58b82011-02-06 14:20:21 +0000236 {
Andrey Andreev77bd21b2012-11-20 16:34:15 +0200237 if (empty($_POST))
238 {
239 return array();
240 }
241
Phil Sturgeon44f21052011-02-15 21:39:25 +0000242 $post = array();
vascopj0ba58b82011-02-06 14:20:21 +0000243
Phil Sturgeon44f21052011-02-15 21:39:25 +0000244 // Loop through the full _POST array and return it
245 foreach (array_keys($_POST) as $key)
vascopj0ba58b82011-02-06 14:20:21 +0000246 {
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530247 $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
vascopj0ba58b82011-02-06 14:20:21 +0000248 }
Phil Sturgeon44f21052011-02-15 21:39:25 +0000249 return $post;
vascopj0ba58b82011-02-06 14:20:21 +0000250 }
David Behler9b5df592011-08-14 21:04:17 +0200251
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530252 return $this->_fetch_from_array($_POST, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 }
254
255 // --------------------------------------------------------------------
256
257 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300258 * Fetch an item from POST data with fallback to GET
Timothy Warren40403d22012-04-19 16:38:50 -0400259 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300260 * @param string $index Index for item to be fetched from $_POST or $_GET
261 * @param bool $xss_clean Whether to apply XSS filtering
262 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400263 */
vlakoff441fd262013-08-11 20:36:41 +0200264 public function post_get($index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200266 return isset($_POST[$index])
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530267 ? $this->post($index, $xss_clean)
268 : $this->get($index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 }
270
271 // --------------------------------------------------------------------
272
273 /**
vlakoff441fd262013-08-11 20:36:41 +0200274 * Fetch an item from GET data with fallback to POST
275 *
276 * @param string $index Index for item to be fetched from $_GET or $_POST
277 * @param bool $xss_clean Whether to apply XSS filtering
278 * @return mixed
279 */
280 public function get_post($index = '', $xss_clean = FALSE)
281 {
282 return isset($_GET[$index])
283 ? $this->get($index, $xss_clean)
284 : $this->post($index, $xss_clean);
285 }
286
287 // --------------------------------------------------------------------
288
289 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400290 * Fetch an item from the COOKIE array
291 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300292 * @param string $index Index for item to be fetched from $_COOKIE
293 * @param bool $xss_clean Whether to apply XSS filtering
294 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400295 */
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530296 public function cookie($index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 {
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530298 return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
300
Andrey Andreev1887ec62012-10-27 16:22:07 +0300301 // --------------------------------------------------------------------
302
303 /**
304 * Fetch an item from the SERVER array
305 *
306 * @param string $index Index for item to be fetched from $_SERVER
307 * @param bool $xss_clean Whether to apply XSS filtering
308 * @return mixed
309 */
310 public function server($index = '', $xss_clean = FALSE)
311 {
312 return $this->_fetch_from_array($_SERVER, $index, $xss_clean);
313 }
314
Derek Jones69fc4fc2010-03-02 13:36:31 -0600315 // ------------------------------------------------------------------------
316
317 /**
Andrey Andreev303eef02012-11-06 14:55:48 +0200318 * Fetch an item from the php://input stream
319 *
320 * Useful when you need to access PUT, DELETE or PATCH request data.
321 *
322 * @param string $index Index for item to be fetched
323 * @param bool $xss_clean Whether to apply XSS filtering
324 * @return mixed
325 */
326 public function input_stream($index = '', $xss_clean = FALSE)
327 {
328 // The input stream can only be read once, so we'll need to check
329 // if we have already done that first.
330 if (is_array($this->_input_stream))
331 {
332 return $this->_fetch_from_array($this->_input_stream, $index, $xss_clean);
333 }
334
335 // Parse the input stream in our cache var
336 parse_str(file_get_contents('php://input'), $this->_input_stream);
337 if ( ! is_array($this->_input_stream))
338 {
339 $this->_input_stream = array();
340 return NULL;
341 }
342
343 return $this->_fetch_from_array($this->_input_stream, $index, $xss_clean);
344 }
345
346 // ------------------------------------------------------------------------
347
348 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400349 * Set cookie
350 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300351 * Accepts an arbitrary number of parameters (up to 7) or an associative
Timothy Warren40403d22012-04-19 16:38:50 -0400352 * array in the first parameter containing all the values.
353 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300354 * @param string|mixed[] $name Cookie name or an array containing parameters
355 * @param string $value Cookie value
356 * @param int $expire Cookie expiration time in seconds
357 * @param string $domain Cookie domain (e.g.: '.yourdomain.com')
358 * @param string $path Cookie path (default: '/')
359 * @param string $prefix Cookie name prefix
360 * @param bool $secure Whether to only transfer cookies via SSL
361 * @param bool $httponly Whether to only makes the cookie accessible via HTTP (no javascript)
Timothy Warren40403d22012-04-19 16:38:50 -0400362 * @return void
363 */
freewil4ad0fd82012-03-13 22:37:42 -0400364 public function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600365 {
366 if (is_array($name))
367 {
tobiasbg9aa7dc92011-02-18 21:57:13 +0100368 // always leave 'name' in last place, as the loop will break otherwise, due to $$item
freewil4ad0fd82012-03-13 22:37:42 -0400369 foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name') as $item)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600370 {
371 if (isset($name[$item]))
372 {
373 $$item = $name[$item];
374 }
375 }
376 }
377
Alex Bilbieed944a32012-06-02 11:07:47 +0100378 if ($prefix === '' && config_item('cookie_prefix') !== '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600379 {
380 $prefix = config_item('cookie_prefix');
381 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300382
383 if ($domain == '' && config_item('cookie_domain') != '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600384 {
385 $domain = config_item('cookie_domain');
386 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300387
Alex Bilbieed944a32012-06-02 11:07:47 +0100388 if ($path === '/' && config_item('cookie_path') !== '/')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600389 {
390 $path = config_item('cookie_path');
391 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300392
Alex Bilbieed944a32012-06-02 11:07:47 +0100393 if ($secure === FALSE && config_item('cookie_secure') !== FALSE)
tobiasbg9aa7dc92011-02-18 21:57:13 +0100394 {
395 $secure = config_item('cookie_secure');
396 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300397
Alex Bilbieed944a32012-06-02 11:07:47 +0100398 if ($httponly === FALSE && config_item('cookie_httponly') !== FALSE)
freewil4ad0fd82012-03-13 22:37:42 -0400399 {
400 $httponly = config_item('cookie_httponly');
401 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600402
403 if ( ! is_numeric($expire))
404 {
405 $expire = time() - 86500;
406 }
407 else
408 {
Phil Sturgeonc8089152010-12-27 19:06:28 +0000409 $expire = ($expire > 0) ? time() + $expire : 0;
Derek Jones69fc4fc2010-03-02 13:36:31 -0600410 }
411
freewil4ad0fd82012-03-13 22:37:42 -0400412 setcookie($prefix.$name, $value, $expire, $path, $domain, $secure, $httponly);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600413 }
414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 // --------------------------------------------------------------------
416
417 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400418 * Fetch the IP Address
419 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300420 * Determines and validates the visitor's IP address.
421 *
422 * @return string IP address
Timothy Warren40403d22012-04-19 16:38:50 -0400423 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800424 public function ip_address()
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 {
426 if ($this->ip_address !== FALSE)
427 {
428 return $this->ip_address;
429 }
Barry Mienydd671972010-10-04 16:33:58 +0200430
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300431 $proxy_ips = config_item('proxy_ips');
Andrey Andreevea7a8662012-10-09 13:36:31 +0300432 if ( ! empty($proxy_ips) && ! is_array($proxy_ips))
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300433 {
434 $proxy_ips = explode(',', str_replace(' ', '', $proxy_ips));
435 }
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300436
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300437 $this->ip_address = $this->server('REMOTE_ADDR');
438
439 if ($proxy_ips)
440 {
441 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 -0300442 {
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300443 if (($spoof = $this->server($header)) !== NULL)
Jordan Pittman8960acf2012-07-23 09:05:49 -0300444 {
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300445 // Some proxies typically list the whole chain of IP
446 // addresses through which the client has reached us.
447 // e.g. client_ip, proxy_ip1, proxy_ip2, etc.
Andrey Andreeve24eed72012-11-02 23:33:45 +0200448 sscanf($spoof, '%[^,]', $spoof);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300449
450 if ( ! $this->valid_ip($spoof))
451 {
452 $spoof = NULL;
453 }
454 else
455 {
Jordan Pittmana5a71352012-07-20 19:36:43 -0300456 break;
457 }
458 }
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300459 }
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300460
Andrey Andreeve45ad2b2012-10-09 13:11:15 +0300461 if ($spoof)
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300462 {
Andrey Andreev9df35b42012-10-09 13:37:58 +0300463 for ($i = 0, $c = count($proxy_ips); $i < $c; $i++)
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300464 {
465 // Check if we have an IP address or a subnet
466 if (strpos($proxy_ips[$i], '/') === FALSE)
467 {
468 // An IP address (and not a subnet) is specified.
469 // We can compare right away.
470 if ($proxy_ips[$i] === $this->ip_address)
471 {
472 $this->ip_address = $spoof;
473 break;
474 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000475
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300476 continue;
477 }
478
479 // We have a subnet ... now the heavy lifting begins
480 isset($separator) OR $separator = $this->valid_ip($this->ip_address, 'ipv6') ? ':' : '.';
481
482 // If the proxy entry doesn't match the IP protocol - skip it
483 if (strpos($proxy_ips[$i], $separator) === FALSE)
484 {
485 continue;
486 }
487
488 // Convert the REMOTE_ADDR IP address to binary, if needed
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300489 if ( ! isset($ip, $sprintf))
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300490 {
491 if ($separator === ':')
492 {
493 // Make sure we're have the "full" IPv6 format
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300494 $ip = explode(':',
495 str_replace('::',
496 str_repeat(':', 9 - substr_count($this->ip_address, ':')),
497 $this->ip_address
498 )
499 );
500
501 for ($i = 0; $i < 8; $i++)
502 {
503 $ip[$i] = intval($ip[$i], 16);
504 }
505
506 $sprintf = '%016b%016b%016b%016b%016b%016b%016b%016b';
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300507 }
508 else
509 {
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300510 $ip = explode('.', $this->ip_address);
511 $sprintf = '%08b%08b%08b%08b';
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300512 }
513
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300514 $ip = vsprintf($sprintf, $ip);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300515 }
516
517 // Split the netmask length off the network address
Andrey Andreeve24eed72012-11-02 23:33:45 +0200518 sscanf($proxy_ips[$i], '%[^/]/%d', $netaddr, $masklen);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300519
520 // Again, an IPv6 address is most likely in a compressed form
521 if ($separator === ':')
522 {
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300523 $netaddr = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($netaddr, ':')), $netaddr));
524 for ($i = 0; $i < 8; $i++)
525 {
526 $netaddr[$i] = intval($netaddr[$i], 16);
527 }
528 }
529 else
530 {
531 $netaddr = explode('.', $netaddr);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300532 }
533
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300534 // Convert to binary and finally compare
535 if (strncmp($ip, vsprintf($sprintf, $netaddr), $masklen) === 0)
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300536 {
537 $this->ip_address = $spoof;
538 break;
539 }
540 }
541 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 }
543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 if ( ! $this->valid_ip($this->ip_address))
545 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200546 return $this->ip_address = '0.0.0.0';
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 }
548
549 return $this->ip_address;
550 }
551
552 // --------------------------------------------------------------------
553
554 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400555 * Validate IP Address
556 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300557 * @param string $ip IP address
558 * @param string $which IP protocol: 'ipv4' or 'ipv6'
Timothy Warren40403d22012-04-19 16:38:50 -0400559 * @return bool
560 */
Andrey Andreev5a257182012-06-10 06:18:14 +0300561 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
Andrey Andreev5a257182012-06-10 06:18:14 +0300563 switch (strtolower($which))
564 {
565 case 'ipv4':
566 $which = FILTER_FLAG_IPV4;
567 break;
568 case 'ipv6':
569 $which = FILTER_FLAG_IPV6;
570 break;
571 default:
572 $which = NULL;
573 break;
574 }
575
576 return (bool) filter_var($ip, FILTER_VALIDATE_IP, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 }
578
579 // --------------------------------------------------------------------
580
581 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300582 * Fetch User Agent string
Timothy Warren40403d22012-04-19 16:38:50 -0400583 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300584 * @return string|null User Agent string or NULL if it doesn't exist
Timothy Warren40403d22012-04-19 16:38:50 -0400585 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800586 public function user_agent()
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
588 if ($this->user_agent !== FALSE)
589 {
590 return $this->user_agent;
591 }
592
Andrey Andreev1887ec62012-10-27 16:22:07 +0300593 return $this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 }
595
596 // --------------------------------------------------------------------
597
598 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400599 * Sanitize Globals
600 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300601 * Internal method serving for the following purposes:
Timothy Warren40403d22012-04-19 16:38:50 -0400602 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300603 * - Unsets $_GET data (if query strings are not enabled)
604 * - Unsets all globals if register_globals is enabled
605 * - Cleans POST, COOKIE and SERVER data
606 * - Standardizes newline characters to PHP_EOL
Timothy Warren40403d22012-04-19 16:38:50 -0400607 *
608 * @return void
609 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200610 protected function _sanitize_globals()
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600612 // It would be "wrong" to unset any of these GLOBALS.
Timothy Warren40403d22012-04-19 16:38:50 -0400613 $protected = array(
614 '_SERVER',
615 '_GET',
616 '_POST',
617 '_FILES',
618 '_REQUEST',
619 '_SESSION',
620 '_ENV',
621 'GLOBALS',
622 'HTTP_RAW_POST_DATA',
623 'system_folder',
624 'application_folder',
625 'BM',
626 'EXT',
627 'CFG',
628 'URI',
629 'RTR',
Timothy Warren67cb3ee2012-04-19 16:41:52 -0400630 'OUT',
Timothy Warren40403d22012-04-19 16:38:50 -0400631 'IN'
632 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000633
Andrey Andreev1887ec62012-10-27 16:22:07 +0300634 // Unset globals for security.
Derek Jones69fc4fc2010-03-02 13:36:31 -0600635 // This is effectively the same as register_globals = off
Andrey Andreev1887ec62012-10-27 16:22:07 +0300636 // PHP 5.4 no longer has the register_globals functionality.
637 if ( ! is_php('5.4'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300639 foreach (array($_GET, $_POST, $_COOKIE) as $global)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600640 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300641 if (is_array($global))
Derek Jones69fc4fc2010-03-02 13:36:31 -0600642 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300643 foreach ($global as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600644 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300645 if ( ! in_array($key, $protected))
646 {
647 global $$key;
648 $$key = NULL;
649 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600650 }
651 }
Andrey Andreev1887ec62012-10-27 16:22:07 +0300652 elseif ( ! in_array($global, $protected))
653 {
654 global $$global;
655 $$global = NULL;
656 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300657 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 }
659
Derek Jones69fc4fc2010-03-02 13:36:31 -0600660 // Is $_GET data allowed? If not we'll set the $_GET to an empty array
Alex Bilbieed944a32012-06-02 11:07:47 +0100661 if ($this->_allow_get_array === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600663 $_GET = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200665 elseif (is_array($_GET) && count($_GET) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200667 foreach ($_GET as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200669 $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 }
671 }
672
Derek Jones69fc4fc2010-03-02 13:36:31 -0600673 // Clean $_POST Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200674 if (is_array($_POST) && count($_POST) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500676 foreach ($_POST as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600677 {
678 $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
679 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 }
681
Derek Jones69fc4fc2010-03-02 13:36:31 -0600682 // Clean $_COOKIE Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200683 if (is_array($_COOKIE) && count($_COOKIE) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600685 // Also get rid of specially treated cookies that might be set by a server
686 // or silly application, that are of no use to a CI application anyway
687 // but that when present will trip our 'Disallowed Key Characters' alarm
688 // http://www.ietf.org/rfc/rfc2109.txt
689 // note that the key names below are single quoted strings, and are not PHP variables
690 unset($_COOKIE['$Version']);
691 unset($_COOKIE['$Path']);
692 unset($_COOKIE['$Domain']);
693
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500694 foreach ($_COOKIE as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600695 {
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300696 if (($cookie_key = $this->_clean_input_keys($key)) !== FALSE)
697 {
698 $_COOKIE[$cookie_key] = $this->_clean_input_data($val);
699 }
700 else
701 {
702 unset($_COOKIE[$key]);
703 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600704 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 }
706
Derek Jones69fc4fc2010-03-02 13:36:31 -0600707 // Sanitize PHP_SELF
708 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
709
Derek Jones69fc4fc2010-03-02 13:36:31 -0600710 // CSRF Protection check
Andrey Andreevf964b162013-11-12 17:04:55 +0200711 if ($this->_enable_csrf === TRUE && ! is_cli())
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600713 $this->security->csrf_verify();
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000715
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300716 log_message('debug', 'Global POST, GET and COOKIE data sanitized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 }
718
719 // --------------------------------------------------------------------
720
721 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400722 * Clean Input Data
723 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300724 * Internal method that aids in escaping data and
725 * standardizing newline characters to PHP_EOL.
Timothy Warren40403d22012-04-19 16:38:50 -0400726 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300727 * @param string|string[] $str Input string(s)
Timothy Warren40403d22012-04-19 16:38:50 -0400728 * @return string
729 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200730 protected function _clean_input_data($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600732 if (is_array($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600734 $new_array = array();
Andrey Andreev1887ec62012-10-27 16:22:07 +0300735 foreach (array_keys($str) as $key)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600736 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300737 $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($str[$key]);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600738 }
739 return $new_array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 }
741
Andrey Andreevaf728622011-10-20 10:11:59 +0300742 /* We strip slashes if magic quotes is on to keep things consistent
743
744 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
745 it will probably not exist in future versions at all.
746 */
747 if ( ! is_php('5.4') && get_magic_quotes_gpc())
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600749 $str = stripslashes($str);
750 }
751
752 // Clean UTF-8 if supported
753 if (UTF8_ENABLED === TRUE)
754 {
755 $str = $this->uni->clean_string($str);
756 }
David Behler9b5df592011-08-14 21:04:17 +0200757
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400758 // Remove control characters
759 $str = remove_invisible_characters($str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600760
761 // Should we filter the input data?
762 if ($this->_enable_xss === TRUE)
763 {
764 $str = $this->security->xss_clean($str);
765 }
766
767 // Standardize newlines if needed
Eric Robertsb75e13d2013-01-27 20:10:09 -0600768 if ($this->_standardize_newlines === TRUE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600769 {
Eric Robertsb75e13d2013-01-27 20:10:09 -0600770 return preg_replace('/(?:\r\n|[\r\n])/', PHP_EOL, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 }
772
773 return $str;
774 }
775
776 // --------------------------------------------------------------------
777
778 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400779 * Clean Keys
780 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300781 * Internal method that helps to prevent malicious users
Timothy Warren40403d22012-04-19 16:38:50 -0400782 * from trying to exploit keys we make sure that keys are
783 * only named with alpha-numeric text and a few other items.
784 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300785 * @param string $str Input string
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300786 * @param string $fatal Whether to terminate script exection
787 * or to return FALSE if an invalid
788 * key is encountered
789 * @return string|bool
Timothy Warren40403d22012-04-19 16:38:50 -0400790 */
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300791 protected function _clean_input_keys($str, $fatal = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 {
bigCat3c0846b2012-08-21 00:20:20 +0800793 if ( ! preg_match('/^[a-z0-9:_\/|-]+$/i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 {
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300795 if ($fatal === TRUE)
796 {
797 return FALSE;
798 }
799 else
800 {
801 set_status_header(503);
802 echo 'Disallowed Key Characters.';
803 exit(EXIT_USER_INPUT);
804 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 }
806
Derek Jones69fc4fc2010-03-02 13:36:31 -0600807 // Clean UTF-8 if supported
808 if (UTF8_ENABLED === TRUE)
809 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200810 return $this->uni->clean_string($str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600811 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000812
Derek Jones69fc4fc2010-03-02 13:36:31 -0600813 return $str;
814 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000815
Greg Akerec2f5712010-11-15 16:22:12 -0600816 // --------------------------------------------------------------------
817
818 /**
819 * Request Headers
820 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300821 * @param bool $xss_clean Whether to apply XSS filtering
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200822 * @return array
Greg Akerec2f5712010-11-15 16:22:12 -0600823 */
824 public function request_headers($xss_clean = FALSE)
825 {
CJ71cff1d2013-04-16 21:50:55 +0800826 // If header is already defined, return it immediately
827 if ( ! empty($this->headers))
828 {
829 return $this->headers;
830 }
831
Andrey Andreev1887ec62012-10-27 16:22:07 +0300832 // In Apache, you can simply call apache_request_headers()
Greg Akerec2f5712010-11-15 16:22:12 -0600833 if (function_exists('apache_request_headers'))
834 {
CJ71cff1d2013-04-16 21:50:55 +0800835 return $this->headers = apache_request_headers();
Greg Akerec2f5712010-11-15 16:22:12 -0600836 }
CJd195f222013-04-17 01:04:13 +0800837
CJ8347f912013-04-17 21:45:22 +0800838 $this->headers['Content-Type'] = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : @getenv('CONTENT_TYPE');
CJd195f222013-04-17 01:04:13 +0800839
840 foreach ($_SERVER as $key => $val)
Greg Akerec2f5712010-11-15 16:22:12 -0600841 {
CJd195f222013-04-17 01:04:13 +0800842 if (sscanf($key, 'HTTP_%s', $header) === 1)
Greg Akerec2f5712010-11-15 16:22:12 -0600843 {
CJd195f222013-04-17 01:04:13 +0800844 // take SOME_HEADER and turn it into Some-Header
845 $header = str_replace('_', ' ', strtolower($header));
846 $header = str_replace(' ', '-', ucwords($header));
Greg Akerec2f5712010-11-15 16:22:12 -0600847
CJd195f222013-04-17 01:04:13 +0800848 $this->headers[$header] = $this->_fetch_from_array($_SERVER, $key, $xss_clean);
CJ826990f2013-04-16 14:17:53 +0800849 }
Greg Akerec2f5712010-11-15 16:22:12 -0600850 }
David Behler9b5df592011-08-14 21:04:17 +0200851
Greg Akerec2f5712010-11-15 16:22:12 -0600852 return $this->headers;
853 }
854
855 // --------------------------------------------------------------------
856
857 /**
858 * Get Request Header
859 *
860 * Returns the value of a single member of the headers class member
861 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300862 * @param string $index Header name
863 * @param bool $xss_clean Whether to apply XSS filtering
864 * @return string|bool The requested header on success or FALSE on failure
Greg Akerec2f5712010-11-15 16:22:12 -0600865 */
866 public function get_request_header($index, $xss_clean = FALSE)
867 {
868 if (empty($this->headers))
869 {
870 $this->request_headers();
871 }
David Behler9b5df592011-08-14 21:04:17 +0200872
Greg Akerec2f5712010-11-15 16:22:12 -0600873 if ( ! isset($this->headers[$index]))
874 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100875 return NULL;
Greg Akerec2f5712010-11-15 16:22:12 -0600876 }
877
Andrey Andreev9448afb2012-02-08 19:49:19 +0200878 return ($xss_clean === TRUE)
879 ? $this->security->xss_clean($this->headers[$index])
880 : $this->headers[$index];
Greg Akerec2f5712010-11-15 16:22:12 -0600881 }
882
Greg Aker081ac9d2010-11-22 14:42:53 -0600883 // --------------------------------------------------------------------
Phil Sturgeonc3828712011-01-19 12:31:47 +0000884
Greg Aker081ac9d2010-11-22 14:42:53 -0600885 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300886 * Is AJAX request?
Greg Aker081ac9d2010-11-22 14:42:53 -0600887 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300888 * Test to see if a request contains the HTTP_X_REQUESTED_WITH header.
Greg Aker081ac9d2010-11-22 14:42:53 -0600889 *
Andrey Andreev9448afb2012-02-08 19:49:19 +0200890 * @return bool
Greg Aker081ac9d2010-11-22 14:42:53 -0600891 */
892 public function is_ajax_request()
893 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200894 return ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
Greg Aker081ac9d2010-11-22 14:42:53 -0600895 }
896
Phil Sturgeonc3828712011-01-19 12:31:47 +0000897 // --------------------------------------------------------------------
898
899 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300900 * Is CLI request?
Phil Sturgeonc3828712011-01-19 12:31:47 +0000901 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300902 * Test to see if a request was made from the command line.
Phil Sturgeonc3828712011-01-19 12:31:47 +0000903 *
Andrey Andreevf964b162013-11-12 17:04:55 +0200904 * @deprecated 3.0.0 Use is_cli() instead
905 * @return bool
Phil Sturgeonc3828712011-01-19 12:31:47 +0000906 */
907 public function is_cli_request()
908 {
Andrey Andreevf964b162013-11-12 17:04:55 +0200909 return is_cli();
Phil Sturgeonc3828712011-01-19 12:31:47 +0000910 }
911
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100912 // --------------------------------------------------------------------
913
914 /**
915 * Get Request Method
916 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300917 * Return the request method
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100918 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300919 * @param bool $upper Whether to return in upper or lower case
920 * (default: FALSE)
921 * @return string
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100922 */
Michiel Vugteveen704fb162012-03-07 20:42:33 +0100923 public function method($upper = FALSE)
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100924 {
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100925 return ($upper)
926 ? strtoupper($this->server('REQUEST_METHOD'))
927 : strtolower($this->server('REQUEST_METHOD'));
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100928 }
929
Derek Allard2067d1a2008-11-13 22:59:24 +0000930}
Derek Allard2067d1a2008-11-13 22:59:24 +0000931
932/* End of file Input.php */
Timothy Warren40403d22012-04-19 16:38:50 -0400933/* Location: ./system/core/Input.php */