blob: ccb70daec37c9b7cfb1c44e9c55a141895034126 [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 Andreev80a16b12014-01-08 17:19:03 +0200155 protected function _fetch_from_array(&$array, $index = '', $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 {
Andrey Andreev80a16b12014-01-08 17:19:03 +0200157 is_bool($xss_clean) OR $xss_clean = $this->_enable_xss;
158
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530159 if (isset($array[$index]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530161 $value = $array[$index];
162 }
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530163 elseif (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) // Does the index contain array notation
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530164 {
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530165 $value = $array;
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530166 for ($i = 0; $i < $count; $i++)
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530167 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530168 $key = trim($matches[0][$i], '[]');
nisheeth-barthwal408cbb42013-03-26 19:06:40 +0530169 if ($key === '') // Empty notation will return the value as array
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530170 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530171 break;
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530172 }
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530173
174 if (isset($value[$key]))
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530175 {
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530176 $value = $value[$key];
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530177 }
178 else
179 {
180 return NULL;
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530181 }
182 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530184 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530186 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
188
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530189 return ($xss_clean === TRUE)
190 ? $this->security->xss_clean($value)
191 : $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 }
193
194 // --------------------------------------------------------------------
195
196 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400197 * Fetch an item from the GET array
198 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300199 * @param string $index Index for item to be fetched from $_GET
200 * @param bool $xss_clean Whether to apply XSS filtering
201 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400202 */
Andrey Andreev80a16b12014-01-08 17:19:03 +0200203 public function get($index = NULL, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
Andrey Andreev80a16b12014-01-08 17:19:03 +0200205 is_bool($xss_clean) OR $xss_clean = $this->_enable_xss;
206
Phil Sturgeon44f21052011-02-15 21:39:25 +0000207 // Check if a field has been provided
Andrey Andreev77bd21b2012-11-20 16:34:15 +0200208 if ($index === NULL)
vascopjff1cfa12011-02-13 21:30:19 +0000209 {
Andrey Andreev77bd21b2012-11-20 16:34:15 +0200210 if (empty($_GET))
211 {
212 return array();
213 }
214
Phil Sturgeon44f21052011-02-15 21:39:25 +0000215 $get = array();
vascopjff1cfa12011-02-13 21:30:19 +0000216
217 // loop through the full _GET array
Phil Sturgeon44f21052011-02-15 21:39:25 +0000218 foreach (array_keys($_GET) as $key)
vascopjff1cfa12011-02-13 21:30:19 +0000219 {
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530220 $get[$key] = $this->_fetch_from_array($_GET, $key, $xss_clean);
vascopjff1cfa12011-02-13 21:30:19 +0000221 }
Phil Sturgeon44f21052011-02-15 21:39:25 +0000222 return $get;
vascopjff1cfa12011-02-13 21:30:19 +0000223 }
224
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530225 return $this->_fetch_from_array($_GET, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
227
228 // --------------------------------------------------------------------
229
230 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400231 * Fetch an item from the POST array
232 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300233 * @param string $index Index for item to be fetched from $_POST
234 * @param bool $xss_clean Whether to apply XSS filtering
235 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400236 */
Andrey Andreev80a16b12014-01-08 17:19:03 +0200237 public function post($index = NULL, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
Andrey Andreev80a16b12014-01-08 17:19:03 +0200239 is_bool($xss_clean) OR $xss_clean = $this->_enable_xss;
240
Phil Sturgeon44f21052011-02-15 21:39:25 +0000241 // Check if a field has been provided
Andrey Andreev77bd21b2012-11-20 16:34:15 +0200242 if ($index === NULL)
vascopj0ba58b82011-02-06 14:20:21 +0000243 {
Andrey Andreev77bd21b2012-11-20 16:34:15 +0200244 if (empty($_POST))
245 {
246 return array();
247 }
248
Phil Sturgeon44f21052011-02-15 21:39:25 +0000249 $post = array();
vascopj0ba58b82011-02-06 14:20:21 +0000250
Phil Sturgeon44f21052011-02-15 21:39:25 +0000251 // Loop through the full _POST array and return it
252 foreach (array_keys($_POST) as $key)
vascopj0ba58b82011-02-06 14:20:21 +0000253 {
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530254 $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
vascopj0ba58b82011-02-06 14:20:21 +0000255 }
Phil Sturgeon44f21052011-02-15 21:39:25 +0000256 return $post;
vascopj0ba58b82011-02-06 14:20:21 +0000257 }
David Behler9b5df592011-08-14 21:04:17 +0200258
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530259 return $this->_fetch_from_array($_POST, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
261
262 // --------------------------------------------------------------------
263
264 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300265 * Fetch an item from POST data with fallback to GET
Timothy Warren40403d22012-04-19 16:38:50 -0400266 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300267 * @param string $index Index for item to be fetched from $_POST or $_GET
268 * @param bool $xss_clean Whether to apply XSS filtering
269 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400270 */
Andrey Andreev80a16b12014-01-08 17:19:03 +0200271 public function post_get($index = '', $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
Andrey Andreev80a16b12014-01-08 17:19:03 +0200273 is_bool($xss_clean) OR $xss_clean = $this->_enable_xss;
274
Andrey Andreev9448afb2012-02-08 19:49:19 +0200275 return isset($_POST[$index])
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530276 ? $this->post($index, $xss_clean)
277 : $this->get($index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 }
279
280 // --------------------------------------------------------------------
281
282 /**
vlakoff441fd262013-08-11 20:36:41 +0200283 * Fetch an item from GET data with fallback to POST
284 *
285 * @param string $index Index for item to be fetched from $_GET or $_POST
286 * @param bool $xss_clean Whether to apply XSS filtering
287 * @return mixed
288 */
Andrey Andreev80a16b12014-01-08 17:19:03 +0200289 public function get_post($index = '', $xss_clean = NULL)
vlakoff441fd262013-08-11 20:36:41 +0200290 {
Andrey Andreev80a16b12014-01-08 17:19:03 +0200291 is_bool($xss_clean) OR $xss_clean = $this->_enable_xss;
292
vlakoff441fd262013-08-11 20:36:41 +0200293 return isset($_GET[$index])
294 ? $this->get($index, $xss_clean)
295 : $this->post($index, $xss_clean);
296 }
297
298 // --------------------------------------------------------------------
299
300 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400301 * Fetch an item from the COOKIE array
302 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300303 * @param string $index Index for item to be fetched from $_COOKIE
304 * @param bool $xss_clean Whether to apply XSS filtering
305 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400306 */
Andrey Andreev80a16b12014-01-08 17:19:03 +0200307 public function cookie($index = '', $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
Andrey Andreev80a16b12014-01-08 17:19:03 +0200309 is_bool($xss_clean) OR $xss_clean = $this->_enable_xss;
310
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530311 return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 }
313
Andrey Andreev1887ec62012-10-27 16:22:07 +0300314 // --------------------------------------------------------------------
315
316 /**
317 * Fetch an item from the SERVER array
318 *
319 * @param string $index Index for item to be fetched from $_SERVER
320 * @param bool $xss_clean Whether to apply XSS filtering
321 * @return mixed
322 */
Andrey Andreev80a16b12014-01-08 17:19:03 +0200323 public function server($index = '', $xss_clean = NULL)
Andrey Andreev1887ec62012-10-27 16:22:07 +0300324 {
Andrey Andreev80a16b12014-01-08 17:19:03 +0200325 is_bool($xss_clean) OR $xss_clean = $this->_enable_xss;
326
Andrey Andreev1887ec62012-10-27 16:22:07 +0300327 return $this->_fetch_from_array($_SERVER, $index, $xss_clean);
328 }
329
Derek Jones69fc4fc2010-03-02 13:36:31 -0600330 // ------------------------------------------------------------------------
331
332 /**
Andrey Andreev303eef02012-11-06 14:55:48 +0200333 * Fetch an item from the php://input stream
334 *
335 * Useful when you need to access PUT, DELETE or PATCH request data.
336 *
337 * @param string $index Index for item to be fetched
338 * @param bool $xss_clean Whether to apply XSS filtering
339 * @return mixed
340 */
Andrey Andreev80a16b12014-01-08 17:19:03 +0200341 public function input_stream($index = '', $xss_clean = NULL)
Andrey Andreev303eef02012-11-06 14:55:48 +0200342 {
Andrey Andreev80a16b12014-01-08 17:19:03 +0200343 is_bool($xss_clean) OR $xss_clean = $this->_enable_xss;
344
Andrey Andreev303eef02012-11-06 14:55:48 +0200345 // The input stream can only be read once, so we'll need to check
346 // if we have already done that first.
347 if (is_array($this->_input_stream))
348 {
349 return $this->_fetch_from_array($this->_input_stream, $index, $xss_clean);
350 }
351
352 // Parse the input stream in our cache var
353 parse_str(file_get_contents('php://input'), $this->_input_stream);
354 if ( ! is_array($this->_input_stream))
355 {
356 $this->_input_stream = array();
357 return NULL;
358 }
359
360 return $this->_fetch_from_array($this->_input_stream, $index, $xss_clean);
361 }
362
363 // ------------------------------------------------------------------------
364
365 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400366 * Set cookie
367 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300368 * Accepts an arbitrary number of parameters (up to 7) or an associative
Timothy Warren40403d22012-04-19 16:38:50 -0400369 * array in the first parameter containing all the values.
370 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300371 * @param string|mixed[] $name Cookie name or an array containing parameters
372 * @param string $value Cookie value
373 * @param int $expire Cookie expiration time in seconds
374 * @param string $domain Cookie domain (e.g.: '.yourdomain.com')
375 * @param string $path Cookie path (default: '/')
376 * @param string $prefix Cookie name prefix
377 * @param bool $secure Whether to only transfer cookies via SSL
378 * @param bool $httponly Whether to only makes the cookie accessible via HTTP (no javascript)
Timothy Warren40403d22012-04-19 16:38:50 -0400379 * @return void
380 */
Andrey Andreev8f5420b2014-01-06 10:34:23 +0200381 public function set_cookie($name, $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600382 {
383 if (is_array($name))
384 {
tobiasbg9aa7dc92011-02-18 21:57:13 +0100385 // always leave 'name' in last place, as the loop will break otherwise, due to $$item
freewil4ad0fd82012-03-13 22:37:42 -0400386 foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name') as $item)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600387 {
388 if (isset($name[$item]))
389 {
390 $$item = $name[$item];
391 }
392 }
393 }
394
Alex Bilbieed944a32012-06-02 11:07:47 +0100395 if ($prefix === '' && config_item('cookie_prefix') !== '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600396 {
397 $prefix = config_item('cookie_prefix');
398 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300399
400 if ($domain == '' && config_item('cookie_domain') != '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600401 {
402 $domain = config_item('cookie_domain');
403 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300404
Alex Bilbieed944a32012-06-02 11:07:47 +0100405 if ($path === '/' && config_item('cookie_path') !== '/')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600406 {
407 $path = config_item('cookie_path');
408 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300409
Alex Bilbieed944a32012-06-02 11:07:47 +0100410 if ($secure === FALSE && config_item('cookie_secure') !== FALSE)
tobiasbg9aa7dc92011-02-18 21:57:13 +0100411 {
412 $secure = config_item('cookie_secure');
413 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300414
Alex Bilbieed944a32012-06-02 11:07:47 +0100415 if ($httponly === FALSE && config_item('cookie_httponly') !== FALSE)
freewil4ad0fd82012-03-13 22:37:42 -0400416 {
417 $httponly = config_item('cookie_httponly');
418 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600419
420 if ( ! is_numeric($expire))
421 {
422 $expire = time() - 86500;
423 }
424 else
425 {
Phil Sturgeonc8089152010-12-27 19:06:28 +0000426 $expire = ($expire > 0) ? time() + $expire : 0;
Derek Jones69fc4fc2010-03-02 13:36:31 -0600427 }
428
freewil4ad0fd82012-03-13 22:37:42 -0400429 setcookie($prefix.$name, $value, $expire, $path, $domain, $secure, $httponly);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600430 }
431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 // --------------------------------------------------------------------
433
434 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400435 * Fetch the IP Address
436 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300437 * Determines and validates the visitor's IP address.
438 *
439 * @return string IP address
Timothy Warren40403d22012-04-19 16:38:50 -0400440 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800441 public function ip_address()
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
443 if ($this->ip_address !== FALSE)
444 {
445 return $this->ip_address;
446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300448 $proxy_ips = config_item('proxy_ips');
Andrey Andreevea7a8662012-10-09 13:36:31 +0300449 if ( ! empty($proxy_ips) && ! is_array($proxy_ips))
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300450 {
451 $proxy_ips = explode(',', str_replace(' ', '', $proxy_ips));
452 }
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300453
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300454 $this->ip_address = $this->server('REMOTE_ADDR');
455
456 if ($proxy_ips)
457 {
458 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 -0300459 {
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300460 if (($spoof = $this->server($header)) !== NULL)
Jordan Pittman8960acf2012-07-23 09:05:49 -0300461 {
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300462 // Some proxies typically list the whole chain of IP
463 // addresses through which the client has reached us.
464 // e.g. client_ip, proxy_ip1, proxy_ip2, etc.
Andrey Andreeve24eed72012-11-02 23:33:45 +0200465 sscanf($spoof, '%[^,]', $spoof);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300466
467 if ( ! $this->valid_ip($spoof))
468 {
469 $spoof = NULL;
470 }
471 else
472 {
Jordan Pittmana5a71352012-07-20 19:36:43 -0300473 break;
474 }
475 }
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300476 }
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300477
Andrey Andreeve45ad2b2012-10-09 13:11:15 +0300478 if ($spoof)
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300479 {
Andrey Andreev9df35b42012-10-09 13:37:58 +0300480 for ($i = 0, $c = count($proxy_ips); $i < $c; $i++)
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300481 {
482 // Check if we have an IP address or a subnet
483 if (strpos($proxy_ips[$i], '/') === FALSE)
484 {
485 // An IP address (and not a subnet) is specified.
486 // We can compare right away.
487 if ($proxy_ips[$i] === $this->ip_address)
488 {
489 $this->ip_address = $spoof;
490 break;
491 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000492
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300493 continue;
494 }
495
496 // We have a subnet ... now the heavy lifting begins
497 isset($separator) OR $separator = $this->valid_ip($this->ip_address, 'ipv6') ? ':' : '.';
498
499 // If the proxy entry doesn't match the IP protocol - skip it
500 if (strpos($proxy_ips[$i], $separator) === FALSE)
501 {
502 continue;
503 }
504
505 // Convert the REMOTE_ADDR IP address to binary, if needed
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300506 if ( ! isset($ip, $sprintf))
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300507 {
508 if ($separator === ':')
509 {
510 // Make sure we're have the "full" IPv6 format
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300511 $ip = explode(':',
512 str_replace('::',
513 str_repeat(':', 9 - substr_count($this->ip_address, ':')),
514 $this->ip_address
515 )
516 );
517
518 for ($i = 0; $i < 8; $i++)
519 {
520 $ip[$i] = intval($ip[$i], 16);
521 }
522
523 $sprintf = '%016b%016b%016b%016b%016b%016b%016b%016b';
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300524 }
525 else
526 {
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300527 $ip = explode('.', $this->ip_address);
528 $sprintf = '%08b%08b%08b%08b';
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300529 }
530
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300531 $ip = vsprintf($sprintf, $ip);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300532 }
533
534 // Split the netmask length off the network address
Andrey Andreeve24eed72012-11-02 23:33:45 +0200535 sscanf($proxy_ips[$i], '%[^/]/%d', $netaddr, $masklen);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300536
537 // Again, an IPv6 address is most likely in a compressed form
538 if ($separator === ':')
539 {
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300540 $netaddr = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($netaddr, ':')), $netaddr));
541 for ($i = 0; $i < 8; $i++)
542 {
543 $netaddr[$i] = intval($netaddr[$i], 16);
544 }
545 }
546 else
547 {
548 $netaddr = explode('.', $netaddr);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300549 }
550
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300551 // Convert to binary and finally compare
552 if (strncmp($ip, vsprintf($sprintf, $netaddr), $masklen) === 0)
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300553 {
554 $this->ip_address = $spoof;
555 break;
556 }
557 }
558 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 }
560
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 if ( ! $this->valid_ip($this->ip_address))
562 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200563 return $this->ip_address = '0.0.0.0';
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 }
565
566 return $this->ip_address;
567 }
568
569 // --------------------------------------------------------------------
570
571 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400572 * Validate IP Address
573 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300574 * @param string $ip IP address
575 * @param string $which IP protocol: 'ipv4' or 'ipv6'
Timothy Warren40403d22012-04-19 16:38:50 -0400576 * @return bool
577 */
Andrey Andreev5a257182012-06-10 06:18:14 +0300578 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 {
Andrey Andreev5a257182012-06-10 06:18:14 +0300580 switch (strtolower($which))
581 {
582 case 'ipv4':
583 $which = FILTER_FLAG_IPV4;
584 break;
585 case 'ipv6':
586 $which = FILTER_FLAG_IPV6;
587 break;
588 default:
589 $which = NULL;
590 break;
591 }
592
593 return (bool) filter_var($ip, FILTER_VALIDATE_IP, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 }
595
596 // --------------------------------------------------------------------
597
598 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300599 * Fetch User Agent string
Timothy Warren40403d22012-04-19 16:38:50 -0400600 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300601 * @return string|null User Agent string or NULL if it doesn't exist
Timothy Warren40403d22012-04-19 16:38:50 -0400602 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800603 public function user_agent()
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 {
605 if ($this->user_agent !== FALSE)
606 {
607 return $this->user_agent;
608 }
609
Andrey Andreev1887ec62012-10-27 16:22:07 +0300610 return $this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 }
612
613 // --------------------------------------------------------------------
614
615 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400616 * Sanitize Globals
617 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300618 * Internal method serving for the following purposes:
Timothy Warren40403d22012-04-19 16:38:50 -0400619 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300620 * - Unsets $_GET data (if query strings are not enabled)
621 * - Unsets all globals if register_globals is enabled
622 * - Cleans POST, COOKIE and SERVER data
623 * - Standardizes newline characters to PHP_EOL
Timothy Warren40403d22012-04-19 16:38:50 -0400624 *
625 * @return void
626 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200627 protected function _sanitize_globals()
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600629 // It would be "wrong" to unset any of these GLOBALS.
Timothy Warren40403d22012-04-19 16:38:50 -0400630 $protected = array(
631 '_SERVER',
632 '_GET',
633 '_POST',
634 '_FILES',
635 '_REQUEST',
636 '_SESSION',
637 '_ENV',
638 'GLOBALS',
639 'HTTP_RAW_POST_DATA',
640 'system_folder',
641 'application_folder',
642 'BM',
643 'EXT',
644 'CFG',
645 'URI',
646 'RTR',
Timothy Warren67cb3ee2012-04-19 16:41:52 -0400647 'OUT',
Timothy Warren40403d22012-04-19 16:38:50 -0400648 'IN'
649 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000650
Andrey Andreev1887ec62012-10-27 16:22:07 +0300651 // Unset globals for security.
Derek Jones69fc4fc2010-03-02 13:36:31 -0600652 // This is effectively the same as register_globals = off
Andrey Andreev1887ec62012-10-27 16:22:07 +0300653 // PHP 5.4 no longer has the register_globals functionality.
654 if ( ! is_php('5.4'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300656 foreach (array($_GET, $_POST, $_COOKIE) as $global)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600657 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300658 if (is_array($global))
Derek Jones69fc4fc2010-03-02 13:36:31 -0600659 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300660 foreach ($global as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600661 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300662 if ( ! in_array($key, $protected))
663 {
664 global $$key;
665 $$key = NULL;
666 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600667 }
668 }
Andrey Andreev1887ec62012-10-27 16:22:07 +0300669 elseif ( ! in_array($global, $protected))
670 {
671 global $$global;
672 $$global = NULL;
673 }
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300674 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 }
676
Derek Jones69fc4fc2010-03-02 13:36:31 -0600677 // Is $_GET data allowed? If not we'll set the $_GET to an empty array
Alex Bilbieed944a32012-06-02 11:07:47 +0100678 if ($this->_allow_get_array === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600680 $_GET = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200682 elseif (is_array($_GET) && count($_GET) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200684 foreach ($_GET as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200686 $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 }
688 }
689
Derek Jones69fc4fc2010-03-02 13:36:31 -0600690 // Clean $_POST Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200691 if (is_array($_POST) && count($_POST) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500693 foreach ($_POST as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600694 {
695 $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
696 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 }
698
Derek Jones69fc4fc2010-03-02 13:36:31 -0600699 // Clean $_COOKIE Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200700 if (is_array($_COOKIE) && count($_COOKIE) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600702 // Also get rid of specially treated cookies that might be set by a server
703 // or silly application, that are of no use to a CI application anyway
704 // but that when present will trip our 'Disallowed Key Characters' alarm
705 // http://www.ietf.org/rfc/rfc2109.txt
706 // note that the key names below are single quoted strings, and are not PHP variables
Andrey Andreev5ac428b2014-01-08 16:07:31 +0200707 unset(
708 $_COOKIE['$Version'],
709 $_COOKIE['$Path'],
710 $_COOKIE['$Domain']
711 );
Derek Jones69fc4fc2010-03-02 13:36:31 -0600712
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500713 foreach ($_COOKIE as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600714 {
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300715 if (($cookie_key = $this->_clean_input_keys($key)) !== FALSE)
716 {
717 $_COOKIE[$cookie_key] = $this->_clean_input_data($val);
718 }
719 else
720 {
721 unset($_COOKIE[$key]);
722 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600723 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 }
725
Derek Jones69fc4fc2010-03-02 13:36:31 -0600726 // Sanitize PHP_SELF
727 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
728
Derek Jones69fc4fc2010-03-02 13:36:31 -0600729 // CSRF Protection check
Andrey Andreevf964b162013-11-12 17:04:55 +0200730 if ($this->_enable_csrf === TRUE && ! is_cli())
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600732 $this->security->csrf_verify();
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000734
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300735 log_message('debug', 'Global POST, GET and COOKIE data sanitized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 }
737
738 // --------------------------------------------------------------------
739
740 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400741 * Clean Input Data
742 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300743 * Internal method that aids in escaping data and
744 * standardizing newline characters to PHP_EOL.
Timothy Warren40403d22012-04-19 16:38:50 -0400745 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300746 * @param string|string[] $str Input string(s)
Timothy Warren40403d22012-04-19 16:38:50 -0400747 * @return string
748 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200749 protected function _clean_input_data($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600751 if (is_array($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600753 $new_array = array();
Andrey Andreev1887ec62012-10-27 16:22:07 +0300754 foreach (array_keys($str) as $key)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600755 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300756 $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($str[$key]);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600757 }
758 return $new_array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 }
760
Andrey Andreevaf728622011-10-20 10:11:59 +0300761 /* We strip slashes if magic quotes is on to keep things consistent
762
763 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
764 it will probably not exist in future versions at all.
765 */
766 if ( ! is_php('5.4') && get_magic_quotes_gpc())
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600768 $str = stripslashes($str);
769 }
770
771 // Clean UTF-8 if supported
772 if (UTF8_ENABLED === TRUE)
773 {
774 $str = $this->uni->clean_string($str);
775 }
David Behler9b5df592011-08-14 21:04:17 +0200776
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400777 // Remove control characters
Andrey Andreev5ac428b2014-01-08 16:07:31 +0200778 $str = remove_invisible_characters($str, FALSE);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600779
Derek Jones69fc4fc2010-03-02 13:36:31 -0600780 // Standardize newlines if needed
Eric Robertsb75e13d2013-01-27 20:10:09 -0600781 if ($this->_standardize_newlines === TRUE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600782 {
Eric Robertsb75e13d2013-01-27 20:10:09 -0600783 return preg_replace('/(?:\r\n|[\r\n])/', PHP_EOL, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 }
785
786 return $str;
787 }
788
789 // --------------------------------------------------------------------
790
791 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400792 * Clean Keys
793 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300794 * Internal method that helps to prevent malicious users
Timothy Warren40403d22012-04-19 16:38:50 -0400795 * from trying to exploit keys we make sure that keys are
796 * only named with alpha-numeric text and a few other items.
797 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300798 * @param string $str Input string
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300799 * @param string $fatal Whether to terminate script exection
800 * or to return FALSE if an invalid
801 * key is encountered
802 * @return string|bool
Timothy Warren40403d22012-04-19 16:38:50 -0400803 */
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300804 protected function _clean_input_keys($str, $fatal = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 {
bigCat3c0846b2012-08-21 00:20:20 +0800806 if ( ! preg_match('/^[a-z0-9:_\/|-]+$/i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 {
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300808 if ($fatal === TRUE)
809 {
810 return FALSE;
811 }
812 else
813 {
814 set_status_header(503);
815 echo 'Disallowed Key Characters.';
816 exit(EXIT_USER_INPUT);
817 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 }
819
Derek Jones69fc4fc2010-03-02 13:36:31 -0600820 // Clean UTF-8 if supported
821 if (UTF8_ENABLED === TRUE)
822 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200823 return $this->uni->clean_string($str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600824 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000825
Derek Jones69fc4fc2010-03-02 13:36:31 -0600826 return $str;
827 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000828
Greg Akerec2f5712010-11-15 16:22:12 -0600829 // --------------------------------------------------------------------
830
831 /**
832 * Request Headers
833 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300834 * @param bool $xss_clean Whether to apply XSS filtering
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200835 * @return array
Greg Akerec2f5712010-11-15 16:22:12 -0600836 */
837 public function request_headers($xss_clean = FALSE)
838 {
CJ71cff1d2013-04-16 21:50:55 +0800839 // If header is already defined, return it immediately
840 if ( ! empty($this->headers))
841 {
842 return $this->headers;
843 }
844
Andrey Andreev1887ec62012-10-27 16:22:07 +0300845 // In Apache, you can simply call apache_request_headers()
Greg Akerec2f5712010-11-15 16:22:12 -0600846 if (function_exists('apache_request_headers'))
847 {
CJ71cff1d2013-04-16 21:50:55 +0800848 return $this->headers = apache_request_headers();
Greg Akerec2f5712010-11-15 16:22:12 -0600849 }
CJd195f222013-04-17 01:04:13 +0800850
CJ8347f912013-04-17 21:45:22 +0800851 $this->headers['Content-Type'] = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : @getenv('CONTENT_TYPE');
CJd195f222013-04-17 01:04:13 +0800852
853 foreach ($_SERVER as $key => $val)
Greg Akerec2f5712010-11-15 16:22:12 -0600854 {
CJd195f222013-04-17 01:04:13 +0800855 if (sscanf($key, 'HTTP_%s', $header) === 1)
Greg Akerec2f5712010-11-15 16:22:12 -0600856 {
CJd195f222013-04-17 01:04:13 +0800857 // take SOME_HEADER and turn it into Some-Header
858 $header = str_replace('_', ' ', strtolower($header));
859 $header = str_replace(' ', '-', ucwords($header));
Greg Akerec2f5712010-11-15 16:22:12 -0600860
CJd195f222013-04-17 01:04:13 +0800861 $this->headers[$header] = $this->_fetch_from_array($_SERVER, $key, $xss_clean);
CJ826990f2013-04-16 14:17:53 +0800862 }
Greg Akerec2f5712010-11-15 16:22:12 -0600863 }
David Behler9b5df592011-08-14 21:04:17 +0200864
Greg Akerec2f5712010-11-15 16:22:12 -0600865 return $this->headers;
866 }
867
868 // --------------------------------------------------------------------
869
870 /**
871 * Get Request Header
872 *
873 * Returns the value of a single member of the headers class member
874 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300875 * @param string $index Header name
876 * @param bool $xss_clean Whether to apply XSS filtering
877 * @return string|bool The requested header on success or FALSE on failure
Greg Akerec2f5712010-11-15 16:22:12 -0600878 */
879 public function get_request_header($index, $xss_clean = FALSE)
880 {
881 if (empty($this->headers))
882 {
883 $this->request_headers();
884 }
David Behler9b5df592011-08-14 21:04:17 +0200885
Greg Akerec2f5712010-11-15 16:22:12 -0600886 if ( ! isset($this->headers[$index]))
887 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100888 return NULL;
Greg Akerec2f5712010-11-15 16:22:12 -0600889 }
890
Andrey Andreev9448afb2012-02-08 19:49:19 +0200891 return ($xss_clean === TRUE)
892 ? $this->security->xss_clean($this->headers[$index])
893 : $this->headers[$index];
Greg Akerec2f5712010-11-15 16:22:12 -0600894 }
895
Greg Aker081ac9d2010-11-22 14:42:53 -0600896 // --------------------------------------------------------------------
Phil Sturgeonc3828712011-01-19 12:31:47 +0000897
Greg Aker081ac9d2010-11-22 14:42:53 -0600898 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300899 * Is AJAX request?
Greg Aker081ac9d2010-11-22 14:42:53 -0600900 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300901 * Test to see if a request contains the HTTP_X_REQUESTED_WITH header.
Greg Aker081ac9d2010-11-22 14:42:53 -0600902 *
Andrey Andreev9448afb2012-02-08 19:49:19 +0200903 * @return bool
Greg Aker081ac9d2010-11-22 14:42:53 -0600904 */
905 public function is_ajax_request()
906 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200907 return ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
Greg Aker081ac9d2010-11-22 14:42:53 -0600908 }
909
Phil Sturgeonc3828712011-01-19 12:31:47 +0000910 // --------------------------------------------------------------------
911
912 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300913 * Is CLI request?
Phil Sturgeonc3828712011-01-19 12:31:47 +0000914 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300915 * Test to see if a request was made from the command line.
Phil Sturgeonc3828712011-01-19 12:31:47 +0000916 *
Andrey Andreevf964b162013-11-12 17:04:55 +0200917 * @deprecated 3.0.0 Use is_cli() instead
918 * @return bool
Phil Sturgeonc3828712011-01-19 12:31:47 +0000919 */
920 public function is_cli_request()
921 {
Andrey Andreevf964b162013-11-12 17:04:55 +0200922 return is_cli();
Phil Sturgeonc3828712011-01-19 12:31:47 +0000923 }
924
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100925 // --------------------------------------------------------------------
926
927 /**
928 * Get Request Method
929 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300930 * Return the request method
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100931 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300932 * @param bool $upper Whether to return in upper or lower case
933 * (default: FALSE)
934 * @return string
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100935 */
Michiel Vugteveen704fb162012-03-07 20:42:33 +0100936 public function method($upper = FALSE)
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100937 {
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100938 return ($upper)
939 ? strtoupper($this->server('REQUEST_METHOD'))
940 : strtolower($this->server('REQUEST_METHOD'));
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100941 }
942
Derek Allard2067d1a2008-11-13 22:59:24 +0000943}
Derek Allard2067d1a2008-11-13 22:59:24 +0000944
945/* End of file Input.php */
Timothy Warren40403d22012-04-19 16:38:50 -0400946/* Location: ./system/core/Input.php */