blob: 81555df9a084b656d60465bb000e85e65a4f4f16 [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 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev64e98aa2012-01-07 20:29:10 +02008 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, British Columbia Institute of Technology
Andrey Andreev64e98aa2012-01-07 20:29:10 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Input Class
42 *
43 * Pre-processes global input data for security
44 *
45 * @package CodeIgniter
46 * @subpackage Libraries
47 * @category Input
Derek Jonesf4a4bd82011-10-20 12:18:42 -050048 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000049 * @link http://codeigniter.com/user_guide/libraries/input.html
50 */
51class CI_Input {
Derek Allard2067d1a2008-11-13 22:59:24 +000052
David Behler9b5df592011-08-14 21:04:17 +020053 /**
54 * IP address of the current user
55 *
Andrey Andreev1887ec62012-10-27 16:22:07 +030056 * @var string
David Behler9b5df592011-08-14 21:04:17 +020057 */
Andrey Andreev1887ec62012-10-27 16:22:07 +030058 public $ip_address = FALSE;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030059
David Behler9b5df592011-08-14 21:04:17 +020060 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +030061 * Allow GET array flag
David Behler9b5df592011-08-14 21:04:17 +020062 *
Andrey Andreev1887ec62012-10-27 16:22:07 +030063 * If set to FALSE, then $_GET will be set to an empty array.
David Behler9b5df592011-08-14 21:04:17 +020064 *
Andrey Andreev1887ec62012-10-27 16:22:07 +030065 * @var bool
David Behler9b5df592011-08-14 21:04:17 +020066 */
Andrey Andreev1887ec62012-10-27 16:22:07 +030067 protected $_allow_get_array = TRUE;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030068
David Behler9b5df592011-08-14 21:04:17 +020069 /**
Andrey Andreevbfb635b2014-01-08 18:32:05 +020070 * Standardize new lines flag
David Behler9b5df592011-08-14 21:04:17 +020071 *
Andrey Andreev1887ec62012-10-27 16:22:07 +030072 * If set to TRUE, then newlines are standardized.
73 *
74 * @var bool
David Behler9b5df592011-08-14 21:04:17 +020075 */
Andrey Andreevefc08e92014-04-15 14:32:52 +030076 protected $_standardize_newlines;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030077
David Behler9b5df592011-08-14 21:04:17 +020078 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +030079 * Enable XSS flag
80 *
81 * Determines whether the XSS filter is always active when
82 * GET, POST or COOKIE data is encountered.
83 * Set automatically based on config setting.
84 *
85 * @var bool
86 */
87 protected $_enable_xss = FALSE;
88
89 /**
90 * Enable CSRF flag
91 *
David Behler9b5df592011-08-14 21:04:17 +020092 * Enables a CSRF cookie token to be set.
Andrey Andreev1887ec62012-10-27 16:22:07 +030093 * Set automatically based on config setting.
David Behler9b5df592011-08-14 21:04:17 +020094 *
Andrey Andreev1887ec62012-10-27 16:22:07 +030095 * @var bool
David Behler9b5df592011-08-14 21:04:17 +020096 */
Andrey Andreev1887ec62012-10-27 16:22:07 +030097 protected $_enable_csrf = FALSE;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030098
David Behler9b5df592011-08-14 21:04:17 +020099 /**
100 * List of all HTTP request headers
101 *
102 * @var array
103 */
Andrey Andreev1887ec62012-10-27 16:22:07 +0300104 protected $headers = array();
David Behler9b5df592011-08-14 21:04:17 +0200105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 /**
Andrey Andreev303eef02012-11-06 14:55:48 +0200107 * Input stream data
108 *
109 * Parsed from php://input at runtime
110 *
111 * @see CI_Input::input_stream()
112 * @var array
113 */
114 protected $_input_stream = NULL;
115
116 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300117 * Class constructor
Greg Akera9263282010-11-10 15:26:43 -0600118 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300119 * Determines whether to globally enable the XSS processing
120 * and whether to allow the $_GET array.
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300121 *
122 * @return void
Greg Akera9263282010-11-10 15:26:43 -0600123 */
124 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 {
Andrey Andreev13774972012-01-08 04:30:33 +0200126 log_message('debug', 'Input Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000127
Andrey Andreevbfb635b2014-01-08 18:32:05 +0200128 $this->_allow_get_array = (config_item('allow_get_array') === TRUE);
129 $this->_enable_xss = (config_item('global_xss_filtering') === TRUE);
130 $this->_enable_csrf = (config_item('csrf_protection') === TRUE);
fabianozenatti523cda32014-03-21 12:13:03 +0100131 $this->_standardize_newlines = (bool) config_item('standardize_newlines');
Derek Jones69fc4fc2010-03-02 13:36:31 -0600132
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200133 $this->security =& load_class('Security', 'core');
Derek Jones69fc4fc2010-03-02 13:36:31 -0600134
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500135 // Do we need the UTF-8 class?
Derek Jones69fc4fc2010-03-02 13:36:31 -0600136 if (UTF8_ENABLED === TRUE)
137 {
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200138 $this->uni =& load_class('Utf8', 'core');
Derek Jones69fc4fc2010-03-02 13:36:31 -0600139 }
140
141 // Sanitize global arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 $this->_sanitize_globals();
143 }
144
145 // --------------------------------------------------------------------
146
147 /**
Greg Akera9263282010-11-10 15:26:43 -0600148 * Fetch from array
149 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300150 * Internal method used to retrieve values from global arrays.
Greg Akera9263282010-11-10 15:26:43 -0600151 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300152 * @param array &$array $_GET, $_POST, $_COOKIE, $_SERVER, etc.
153 * @param string $index Index for item to be fetched from $array
154 * @param bool $xss_clean Whether to apply XSS filtering
155 * @return mixed
Greg Akera9263282010-11-10 15:26:43 -0600156 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200157 protected function _fetch_from_array(&$array, $index = NULL, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
Andrey Andreev7c60b122014-02-08 18:47:19 +0200159 // If $index is NULL, it means that the whole $array is requested
160 if ($index === NULL)
161 {
162 $output = array();
163 foreach (array_keys($array) as $key)
164 {
165 $output[$key] = $this->_fetch_from_array($array, $key, $xss_clean);
166 }
167
168 return $output;
169 }
170
Andrey Andreev80a16b12014-01-08 17:19:03 +0200171 is_bool($xss_clean) OR $xss_clean = $this->_enable_xss;
172
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530173 if (isset($array[$index]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530175 $value = $array[$index];
176 }
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530177 elseif (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) // Does the index contain array notation
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530178 {
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530179 $value = $array;
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530180 for ($i = 0; $i < $count; $i++)
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530181 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530182 $key = trim($matches[0][$i], '[]');
nisheeth-barthwal408cbb42013-03-26 19:06:40 +0530183 if ($key === '') // Empty notation will return the value as array
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530184 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530185 break;
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530186 }
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530187
188 if (isset($value[$key]))
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530189 {
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530190 $value = $value[$key];
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530191 }
192 else
193 {
194 return NULL;
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530195 }
196 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530198 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530200 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 }
202
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530203 return ($xss_clean === TRUE)
204 ? $this->security->xss_clean($value)
205 : $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 }
207
208 // --------------------------------------------------------------------
209
210 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400211 * Fetch an item from the GET array
212 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300213 * @param string $index Index for item to be fetched from $_GET
214 * @param bool $xss_clean Whether to apply XSS filtering
215 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400216 */
Andrey Andreev80a16b12014-01-08 17:19:03 +0200217 public function get($index = NULL, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 {
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 */
Andrey Andreev80a16b12014-01-08 17:19:03 +0200231 public function post($index = NULL, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530233 return $this->_fetch_from_array($_POST, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 }
235
236 // --------------------------------------------------------------------
237
238 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300239 * Fetch an item from POST data with fallback to GET
Timothy Warren40403d22012-04-19 16:38:50 -0400240 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300241 * @param string $index Index for item to be fetched from $_POST or $_GET
242 * @param bool $xss_clean Whether to apply XSS filtering
243 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400244 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200245 public function post_get($index, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200247 return isset($_POST[$index])
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530248 ? $this->post($index, $xss_clean)
249 : $this->get($index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 }
251
252 // --------------------------------------------------------------------
253
254 /**
vlakoff441fd262013-08-11 20:36:41 +0200255 * Fetch an item from GET data with fallback to POST
256 *
257 * @param string $index Index for item to be fetched from $_GET or $_POST
258 * @param bool $xss_clean Whether to apply XSS filtering
259 * @return mixed
260 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200261 public function get_post($index, $xss_clean = NULL)
vlakoff441fd262013-08-11 20:36:41 +0200262 {
263 return isset($_GET[$index])
264 ? $this->get($index, $xss_clean)
265 : $this->post($index, $xss_clean);
266 }
267
268 // --------------------------------------------------------------------
269
270 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400271 * Fetch an item from the COOKIE array
272 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300273 * @param string $index Index for item to be fetched from $_COOKIE
274 * @param bool $xss_clean Whether to apply XSS filtering
275 * @return mixed
Timothy Warren40403d22012-04-19 16:38:50 -0400276 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200277 public function cookie($index = NULL, $xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
nisheeth-barthwala5bcfb12013-03-23 10:53:51 +0530279 return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
281
Andrey Andreev1887ec62012-10-27 16:22:07 +0300282 // --------------------------------------------------------------------
283
284 /**
285 * Fetch an item from the SERVER array
286 *
287 * @param string $index Index for item to be fetched from $_SERVER
288 * @param bool $xss_clean Whether to apply XSS filtering
289 * @return mixed
290 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200291 public function server($index, $xss_clean = NULL)
Andrey Andreev1887ec62012-10-27 16:22:07 +0300292 {
293 return $this->_fetch_from_array($_SERVER, $index, $xss_clean);
294 }
295
Derek Jones69fc4fc2010-03-02 13:36:31 -0600296 // ------------------------------------------------------------------------
297
298 /**
Andrey Andreev303eef02012-11-06 14:55:48 +0200299 * Fetch an item from the php://input stream
300 *
301 * Useful when you need to access PUT, DELETE or PATCH request data.
302 *
303 * @param string $index Index for item to be fetched
304 * @param bool $xss_clean Whether to apply XSS filtering
305 * @return mixed
306 */
Andrey Andreev7c60b122014-02-08 18:47:19 +0200307 public function input_stream($index = NULL, $xss_clean = NULL)
Andrey Andreev303eef02012-11-06 14:55:48 +0200308 {
Andrey Andreevdc134a82014-05-08 10:00:55 +0300309 // Prior to PHP 5.6, the input stream can only be read once,
310 // so we'll need to check if we have already done that first.
Andrey Andreev303eef02012-11-06 14:55:48 +0200311 if ( ! is_array($this->_input_stream))
312 {
Andrey Andreev7c60b122014-02-08 18:47:19 +0200313 parse_str(file_get_contents('php://input'), $this->_input_stream);
314 is_array($this->_input_stream) OR $this->_input_stream = array();
Andrey Andreev303eef02012-11-06 14:55:48 +0200315 }
316
317 return $this->_fetch_from_array($this->_input_stream, $index, $xss_clean);
318 }
319
320 // ------------------------------------------------------------------------
321
322 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400323 * Set cookie
324 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300325 * Accepts an arbitrary number of parameters (up to 7) or an associative
Timothy Warren40403d22012-04-19 16:38:50 -0400326 * array in the first parameter containing all the values.
327 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300328 * @param string|mixed[] $name Cookie name or an array containing parameters
329 * @param string $value Cookie value
330 * @param int $expire Cookie expiration time in seconds
331 * @param string $domain Cookie domain (e.g.: '.yourdomain.com')
332 * @param string $path Cookie path (default: '/')
333 * @param string $prefix Cookie name prefix
334 * @param bool $secure Whether to only transfer cookies via SSL
335 * @param bool $httponly Whether to only makes the cookie accessible via HTTP (no javascript)
Timothy Warren40403d22012-04-19 16:38:50 -0400336 * @return void
337 */
Andrey Andreev8f5420b2014-01-06 10:34:23 +0200338 public function set_cookie($name, $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600339 {
340 if (is_array($name))
341 {
tobiasbg9aa7dc92011-02-18 21:57:13 +0100342 // always leave 'name' in last place, as the loop will break otherwise, due to $$item
freewil4ad0fd82012-03-13 22:37:42 -0400343 foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name') as $item)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600344 {
345 if (isset($name[$item]))
346 {
347 $$item = $name[$item];
348 }
349 }
350 }
351
Alex Bilbieed944a32012-06-02 11:07:47 +0100352 if ($prefix === '' && config_item('cookie_prefix') !== '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600353 {
354 $prefix = config_item('cookie_prefix');
355 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300356
357 if ($domain == '' && config_item('cookie_domain') != '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600358 {
359 $domain = config_item('cookie_domain');
360 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300361
Alex Bilbieed944a32012-06-02 11:07:47 +0100362 if ($path === '/' && config_item('cookie_path') !== '/')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600363 {
364 $path = config_item('cookie_path');
365 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300366
Andrey Andreevd444d442014-10-06 00:00:08 +0300367 if ($secure === FALSE && config_item('cookie_secure') === TRUE)
tobiasbg9aa7dc92011-02-18 21:57:13 +0100368 {
369 $secure = config_item('cookie_secure');
370 }
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300371
Alex Bilbieed944a32012-06-02 11:07:47 +0100372 if ($httponly === FALSE && config_item('cookie_httponly') !== FALSE)
freewil4ad0fd82012-03-13 22:37:42 -0400373 {
374 $httponly = config_item('cookie_httponly');
375 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600376
377 if ( ! is_numeric($expire))
378 {
379 $expire = time() - 86500;
380 }
381 else
382 {
Phil Sturgeonc8089152010-12-27 19:06:28 +0000383 $expire = ($expire > 0) ? time() + $expire : 0;
Derek Jones69fc4fc2010-03-02 13:36:31 -0600384 }
385
freewil4ad0fd82012-03-13 22:37:42 -0400386 setcookie($prefix.$name, $value, $expire, $path, $domain, $secure, $httponly);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600387 }
388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 // --------------------------------------------------------------------
390
391 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400392 * Fetch the IP Address
393 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300394 * Determines and validates the visitor's IP address.
395 *
396 * @return string IP address
Timothy Warren40403d22012-04-19 16:38:50 -0400397 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800398 public function ip_address()
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
400 if ($this->ip_address !== FALSE)
401 {
402 return $this->ip_address;
403 }
Barry Mienydd671972010-10-04 16:33:58 +0200404
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300405 $proxy_ips = config_item('proxy_ips');
Andrey Andreevea7a8662012-10-09 13:36:31 +0300406 if ( ! empty($proxy_ips) && ! is_array($proxy_ips))
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300407 {
408 $proxy_ips = explode(',', str_replace(' ', '', $proxy_ips));
409 }
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300410
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300411 $this->ip_address = $this->server('REMOTE_ADDR');
412
413 if ($proxy_ips)
414 {
415 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 -0300416 {
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300417 if (($spoof = $this->server($header)) !== NULL)
Jordan Pittman8960acf2012-07-23 09:05:49 -0300418 {
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300419 // Some proxies typically list the whole chain of IP
420 // addresses through which the client has reached us.
421 // e.g. client_ip, proxy_ip1, proxy_ip2, etc.
Andrey Andreeve24eed72012-11-02 23:33:45 +0200422 sscanf($spoof, '%[^,]', $spoof);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300423
424 if ( ! $this->valid_ip($spoof))
425 {
426 $spoof = NULL;
427 }
428 else
429 {
Jordan Pittmana5a71352012-07-20 19:36:43 -0300430 break;
431 }
432 }
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300433 }
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300434
Andrey Andreeve45ad2b2012-10-09 13:11:15 +0300435 if ($spoof)
Andrey Andreev5b92ae12012-10-04 13:05:03 +0300436 {
Andrey Andreev9df35b42012-10-09 13:37:58 +0300437 for ($i = 0, $c = count($proxy_ips); $i < $c; $i++)
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300438 {
439 // Check if we have an IP address or a subnet
440 if (strpos($proxy_ips[$i], '/') === FALSE)
441 {
442 // An IP address (and not a subnet) is specified.
443 // We can compare right away.
444 if ($proxy_ips[$i] === $this->ip_address)
445 {
446 $this->ip_address = $spoof;
447 break;
448 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000449
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300450 continue;
451 }
452
453 // We have a subnet ... now the heavy lifting begins
454 isset($separator) OR $separator = $this->valid_ip($this->ip_address, 'ipv6') ? ':' : '.';
455
456 // If the proxy entry doesn't match the IP protocol - skip it
457 if (strpos($proxy_ips[$i], $separator) === FALSE)
458 {
459 continue;
460 }
461
462 // Convert the REMOTE_ADDR IP address to binary, if needed
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300463 if ( ! isset($ip, $sprintf))
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300464 {
465 if ($separator === ':')
466 {
467 // Make sure we're have the "full" IPv6 format
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300468 $ip = explode(':',
469 str_replace('::',
470 str_repeat(':', 9 - substr_count($this->ip_address, ':')),
471 $this->ip_address
472 )
473 );
474
475 for ($i = 0; $i < 8; $i++)
476 {
477 $ip[$i] = intval($ip[$i], 16);
478 }
479
480 $sprintf = '%016b%016b%016b%016b%016b%016b%016b%016b';
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300481 }
482 else
483 {
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300484 $ip = explode('.', $this->ip_address);
485 $sprintf = '%08b%08b%08b%08b';
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300486 }
487
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300488 $ip = vsprintf($sprintf, $ip);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300489 }
490
491 // Split the netmask length off the network address
Andrey Andreeve24eed72012-11-02 23:33:45 +0200492 sscanf($proxy_ips[$i], '%[^/]/%d', $netaddr, $masklen);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300493
494 // Again, an IPv6 address is most likely in a compressed form
495 if ($separator === ':')
496 {
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300497 $netaddr = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($netaddr, ':')), $netaddr));
498 for ($i = 0; $i < 8; $i++)
499 {
500 $netaddr[$i] = intval($netaddr[$i], 16);
501 }
502 }
503 else
504 {
505 $netaddr = explode('.', $netaddr);
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300506 }
507
Andrey Andreev82d2cf12012-10-13 12:38:42 +0300508 // Convert to binary and finally compare
509 if (strncmp($ip, vsprintf($sprintf, $netaddr), $masklen) === 0)
Andrey Andreev9ac557f2012-10-06 20:27:57 +0300510 {
511 $this->ip_address = $spoof;
512 break;
513 }
514 }
515 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 }
517
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 if ( ! $this->valid_ip($this->ip_address))
519 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200520 return $this->ip_address = '0.0.0.0';
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 }
522
523 return $this->ip_address;
524 }
525
526 // --------------------------------------------------------------------
527
528 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400529 * Validate IP Address
530 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300531 * @param string $ip IP address
532 * @param string $which IP protocol: 'ipv4' or 'ipv6'
Timothy Warren40403d22012-04-19 16:38:50 -0400533 * @return bool
534 */
Andrey Andreev5a257182012-06-10 06:18:14 +0300535 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 {
Andrey Andreev5a257182012-06-10 06:18:14 +0300537 switch (strtolower($which))
538 {
539 case 'ipv4':
540 $which = FILTER_FLAG_IPV4;
541 break;
542 case 'ipv6':
543 $which = FILTER_FLAG_IPV6;
544 break;
545 default:
546 $which = NULL;
547 break;
548 }
549
550 return (bool) filter_var($ip, FILTER_VALIDATE_IP, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
552
553 // --------------------------------------------------------------------
554
555 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300556 * Fetch User Agent string
Timothy Warren40403d22012-04-19 16:38:50 -0400557 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300558 * @return string|null User Agent string or NULL if it doesn't exist
Timothy Warren40403d22012-04-19 16:38:50 -0400559 */
Andrey Andreev8850e372014-02-27 21:56:06 +0200560 public function user_agent($xss_clean = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 {
Andrey Andreev8850e372014-02-27 21:56:06 +0200562 return $this->_fetch_from_array($_SERVER, 'HTTP_USER_AGENT', $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 }
564
565 // --------------------------------------------------------------------
566
567 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400568 * Sanitize Globals
569 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300570 * Internal method serving for the following purposes:
Timothy Warren40403d22012-04-19 16:38:50 -0400571 *
Andrey Andreevb78a8c72014-04-15 17:21:16 +0300572 * - Unsets $_GET data, if query strings are not enabled
Andrey Andreev1887ec62012-10-27 16:22:07 +0300573 * - Cleans POST, COOKIE and SERVER data
574 * - Standardizes newline characters to PHP_EOL
Timothy Warren40403d22012-04-19 16:38:50 -0400575 *
576 * @return void
577 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200578 protected function _sanitize_globals()
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600580 // Is $_GET data allowed? If not we'll set the $_GET to an empty array
Alex Bilbieed944a32012-06-02 11:07:47 +0100581 if ($this->_allow_get_array === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600583 $_GET = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200585 elseif (is_array($_GET) && count($_GET) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200587 foreach ($_GET as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200589 $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 }
591 }
592
Derek Jones69fc4fc2010-03-02 13:36:31 -0600593 // Clean $_POST Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200594 if (is_array($_POST) && count($_POST) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500596 foreach ($_POST as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600597 {
598 $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
599 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 }
601
Derek Jones69fc4fc2010-03-02 13:36:31 -0600602 // Clean $_COOKIE Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200603 if (is_array($_COOKIE) && count($_COOKIE) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600605 // Also get rid of specially treated cookies that might be set by a server
606 // or silly application, that are of no use to a CI application anyway
607 // but that when present will trip our 'Disallowed Key Characters' alarm
608 // http://www.ietf.org/rfc/rfc2109.txt
609 // note that the key names below are single quoted strings, and are not PHP variables
Andrey Andreev5ac428b2014-01-08 16:07:31 +0200610 unset(
611 $_COOKIE['$Version'],
612 $_COOKIE['$Path'],
613 $_COOKIE['$Domain']
614 );
Derek Jones69fc4fc2010-03-02 13:36:31 -0600615
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500616 foreach ($_COOKIE as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600617 {
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300618 if (($cookie_key = $this->_clean_input_keys($key)) !== FALSE)
619 {
620 $_COOKIE[$cookie_key] = $this->_clean_input_data($val);
621 }
622 else
623 {
624 unset($_COOKIE[$key]);
625 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600626 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 }
628
Derek Jones69fc4fc2010-03-02 13:36:31 -0600629 // Sanitize PHP_SELF
630 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
631
Derek Jones69fc4fc2010-03-02 13:36:31 -0600632 // CSRF Protection check
Andrey Andreevf964b162013-11-12 17:04:55 +0200633 if ($this->_enable_csrf === TRUE && ! is_cli())
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600635 $this->security->csrf_verify();
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000637
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300638 log_message('debug', 'Global POST, GET and COOKIE data sanitized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 }
640
641 // --------------------------------------------------------------------
642
643 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400644 * Clean Input Data
645 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300646 * Internal method that aids in escaping data and
647 * standardizing newline characters to PHP_EOL.
Timothy Warren40403d22012-04-19 16:38:50 -0400648 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300649 * @param string|string[] $str Input string(s)
Timothy Warren40403d22012-04-19 16:38:50 -0400650 * @return string
651 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200652 protected function _clean_input_data($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600654 if (is_array($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600656 $new_array = array();
Andrey Andreev1887ec62012-10-27 16:22:07 +0300657 foreach (array_keys($str) as $key)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600658 {
Andrey Andreev1887ec62012-10-27 16:22:07 +0300659 $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($str[$key]);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600660 }
661 return $new_array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 }
663
Andrey Andreevaf728622011-10-20 10:11:59 +0300664 /* We strip slashes if magic quotes is on to keep things consistent
665
666 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
667 it will probably not exist in future versions at all.
668 */
669 if ( ! is_php('5.4') && get_magic_quotes_gpc())
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600671 $str = stripslashes($str);
672 }
673
674 // Clean UTF-8 if supported
675 if (UTF8_ENABLED === TRUE)
676 {
677 $str = $this->uni->clean_string($str);
678 }
David Behler9b5df592011-08-14 21:04:17 +0200679
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400680 // Remove control characters
Andrey Andreev5ac428b2014-01-08 16:07:31 +0200681 $str = remove_invisible_characters($str, FALSE);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600682
Derek Jones69fc4fc2010-03-02 13:36:31 -0600683 // Standardize newlines if needed
Eric Robertsb75e13d2013-01-27 20:10:09 -0600684 if ($this->_standardize_newlines === TRUE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600685 {
Eric Robertsb75e13d2013-01-27 20:10:09 -0600686 return preg_replace('/(?:\r\n|[\r\n])/', PHP_EOL, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 }
688
689 return $str;
690 }
691
692 // --------------------------------------------------------------------
693
694 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400695 * Clean Keys
696 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300697 * Internal method that helps to prevent malicious users
Timothy Warren40403d22012-04-19 16:38:50 -0400698 * from trying to exploit keys we make sure that keys are
699 * only named with alpha-numeric text and a few other items.
700 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300701 * @param string $str Input string
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300702 * @param string $fatal Whether to terminate script exection
703 * or to return FALSE if an invalid
704 * key is encountered
705 * @return string|bool
Timothy Warren40403d22012-04-19 16:38:50 -0400706 */
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300707 protected function _clean_input_keys($str, $fatal = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 {
bigCat3c0846b2012-08-21 00:20:20 +0800709 if ( ! preg_match('/^[a-z0-9:_\/|-]+$/i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 {
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300711 if ($fatal === TRUE)
712 {
713 return FALSE;
714 }
715 else
716 {
717 set_status_header(503);
718 echo 'Disallowed Key Characters.';
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200719 exit(7); // EXIT_USER_INPUT
Andrey Andreevfd0aabb2013-09-23 13:18:20 +0300720 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 }
722
Derek Jones69fc4fc2010-03-02 13:36:31 -0600723 // Clean UTF-8 if supported
724 if (UTF8_ENABLED === TRUE)
725 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200726 return $this->uni->clean_string($str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600727 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000728
Derek Jones69fc4fc2010-03-02 13:36:31 -0600729 return $str;
730 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000731
Greg Akerec2f5712010-11-15 16:22:12 -0600732 // --------------------------------------------------------------------
733
734 /**
735 * Request Headers
736 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300737 * @param bool $xss_clean Whether to apply XSS filtering
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200738 * @return array
Greg Akerec2f5712010-11-15 16:22:12 -0600739 */
740 public function request_headers($xss_clean = FALSE)
741 {
CJ71cff1d2013-04-16 21:50:55 +0800742 // If header is already defined, return it immediately
743 if ( ! empty($this->headers))
744 {
745 return $this->headers;
746 }
747
Andrey Andreev1887ec62012-10-27 16:22:07 +0300748 // In Apache, you can simply call apache_request_headers()
Greg Akerec2f5712010-11-15 16:22:12 -0600749 if (function_exists('apache_request_headers'))
750 {
CJ71cff1d2013-04-16 21:50:55 +0800751 return $this->headers = apache_request_headers();
Greg Akerec2f5712010-11-15 16:22:12 -0600752 }
CJd195f222013-04-17 01:04:13 +0800753
CJ8347f912013-04-17 21:45:22 +0800754 $this->headers['Content-Type'] = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : @getenv('CONTENT_TYPE');
CJd195f222013-04-17 01:04:13 +0800755
756 foreach ($_SERVER as $key => $val)
Greg Akerec2f5712010-11-15 16:22:12 -0600757 {
CJd195f222013-04-17 01:04:13 +0800758 if (sscanf($key, 'HTTP_%s', $header) === 1)
Greg Akerec2f5712010-11-15 16:22:12 -0600759 {
CJd195f222013-04-17 01:04:13 +0800760 // take SOME_HEADER and turn it into Some-Header
761 $header = str_replace('_', ' ', strtolower($header));
762 $header = str_replace(' ', '-', ucwords($header));
Greg Akerec2f5712010-11-15 16:22:12 -0600763
CJd195f222013-04-17 01:04:13 +0800764 $this->headers[$header] = $this->_fetch_from_array($_SERVER, $key, $xss_clean);
CJ826990f2013-04-16 14:17:53 +0800765 }
Greg Akerec2f5712010-11-15 16:22:12 -0600766 }
David Behler9b5df592011-08-14 21:04:17 +0200767
Greg Akerec2f5712010-11-15 16:22:12 -0600768 return $this->headers;
769 }
770
771 // --------------------------------------------------------------------
772
773 /**
774 * Get Request Header
775 *
776 * Returns the value of a single member of the headers class member
777 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300778 * @param string $index Header name
779 * @param bool $xss_clean Whether to apply XSS filtering
Adriano Rosaf112e4a2014-10-03 13:15:46 -0300780 * @return string|null The requested header on success or NULL on failure
Greg Akerec2f5712010-11-15 16:22:12 -0600781 */
782 public function get_request_header($index, $xss_clean = FALSE)
783 {
784 if (empty($this->headers))
785 {
786 $this->request_headers();
787 }
David Behler9b5df592011-08-14 21:04:17 +0200788
Greg Akerec2f5712010-11-15 16:22:12 -0600789 if ( ! isset($this->headers[$index]))
790 {
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100791 return NULL;
Greg Akerec2f5712010-11-15 16:22:12 -0600792 }
793
Andrey Andreev9448afb2012-02-08 19:49:19 +0200794 return ($xss_clean === TRUE)
795 ? $this->security->xss_clean($this->headers[$index])
796 : $this->headers[$index];
Greg Akerec2f5712010-11-15 16:22:12 -0600797 }
798
Greg Aker081ac9d2010-11-22 14:42:53 -0600799 // --------------------------------------------------------------------
Phil Sturgeonc3828712011-01-19 12:31:47 +0000800
Greg Aker081ac9d2010-11-22 14:42:53 -0600801 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300802 * Is AJAX request?
Greg Aker081ac9d2010-11-22 14:42:53 -0600803 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300804 * Test to see if a request contains the HTTP_X_REQUESTED_WITH header.
Greg Aker081ac9d2010-11-22 14:42:53 -0600805 *
Andrey Andreev9448afb2012-02-08 19:49:19 +0200806 * @return bool
Greg Aker081ac9d2010-11-22 14:42:53 -0600807 */
808 public function is_ajax_request()
809 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200810 return ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
Greg Aker081ac9d2010-11-22 14:42:53 -0600811 }
812
Phil Sturgeonc3828712011-01-19 12:31:47 +0000813 // --------------------------------------------------------------------
814
815 /**
Andrey Andreev1887ec62012-10-27 16:22:07 +0300816 * Is CLI request?
Phil Sturgeonc3828712011-01-19 12:31:47 +0000817 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300818 * Test to see if a request was made from the command line.
Phil Sturgeonc3828712011-01-19 12:31:47 +0000819 *
Andrey Andreevf964b162013-11-12 17:04:55 +0200820 * @deprecated 3.0.0 Use is_cli() instead
821 * @return bool
Phil Sturgeonc3828712011-01-19 12:31:47 +0000822 */
823 public function is_cli_request()
824 {
Andrey Andreevf964b162013-11-12 17:04:55 +0200825 return is_cli();
Phil Sturgeonc3828712011-01-19 12:31:47 +0000826 }
827
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100828 // --------------------------------------------------------------------
829
830 /**
831 * Get Request Method
832 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300833 * Return the request method
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100834 *
Andrey Andreev1887ec62012-10-27 16:22:07 +0300835 * @param bool $upper Whether to return in upper or lower case
836 * (default: FALSE)
837 * @return string
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100838 */
Michiel Vugteveen704fb162012-03-07 20:42:33 +0100839 public function method($upper = FALSE)
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100840 {
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100841 return ($upper)
842 ? strtoupper($this->server('REQUEST_METHOD'))
843 : strtolower($this->server('REQUEST_METHOD'));
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100844 }
845
Derek Allard2067d1a2008-11-13 22:59:24 +0000846}
Derek Allard2067d1a2008-11-13 22:59:24 +0000847
848/* End of file Input.php */
fabianozenatti28eca652014-03-21 14:54:25 +0100849/* Location: ./system/core/Input.php */