Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 64e98aa | 2012-01-07 20:29:10 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 64e98aa | 2012-01-07 20:29:10 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Andrey Andreev | 80500af | 2013-01-01 08:16:53 +0200 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Input Class |
| 31 | * |
| 32 | * Pre-processes global input data for security |
| 33 | * |
| 34 | * @package CodeIgniter |
| 35 | * @subpackage Libraries |
| 36 | * @category Input |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 37 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 38 | * @link http://codeigniter.com/user_guide/libraries/input.html |
| 39 | */ |
| 40 | class CI_Input { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 42 | /** |
| 43 | * IP address of the current user |
| 44 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 45 | * @var string |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 46 | */ |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 47 | public $ip_address = FALSE; |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 48 | |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 49 | /** |
vlakoff | c941d85 | 2013-08-06 14:44:40 +0200 | [diff] [blame] | 50 | * User agent string |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 51 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 52 | * @var string |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 53 | */ |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 54 | public $user_agent = FALSE; |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 55 | |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 56 | /** |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 57 | * Allow GET array flag |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 58 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 59 | * If set to FALSE, then $_GET will be set to an empty array. |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 60 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 61 | * @var bool |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 62 | */ |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 63 | protected $_allow_get_array = TRUE; |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 64 | |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 65 | /** |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 66 | * Standartize new lines flag |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 67 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 68 | * If set to TRUE, then newlines are standardized. |
| 69 | * |
| 70 | * @var bool |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 71 | */ |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 72 | protected $_standardize_newlines = TRUE; |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 73 | |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 74 | /** |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 75 | * 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 Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 88 | * Enables a CSRF cookie token to be set. |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 89 | * Set automatically based on config setting. |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 90 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 91 | * @var bool |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 92 | */ |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 93 | protected $_enable_csrf = FALSE; |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 94 | |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 95 | /** |
| 96 | * List of all HTTP request headers |
| 97 | * |
| 98 | * @var array |
| 99 | */ |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 100 | protected $headers = array(); |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 101 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | /** |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 103 | * 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 Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 113 | * Class constructor |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 114 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 115 | * Determines whether to globally enable the XSS processing |
| 116 | * and whether to allow the $_GET array. |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 117 | * |
| 118 | * @return void |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 119 | */ |
| 120 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | { |
Andrey Andreev | 1377497 | 2012-01-08 04:30:33 +0200 | [diff] [blame] | 122 | log_message('debug', 'Input Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | |
Phil Sturgeon | c808915 | 2010-12-27 19:06:28 +0000 | [diff] [blame] | 124 | $this->_allow_get_array = (config_item('allow_get_array') === TRUE); |
Andrey Andreev | 64e98aa | 2012-01-07 20:29:10 +0200 | [diff] [blame] | 125 | $this->_enable_xss = (config_item('global_xss_filtering') === TRUE); |
| 126 | $this->_enable_csrf = (config_item('csrf_protection') === TRUE); |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 127 | |
Pascal Kriete | 14a0ac6 | 2011-04-05 14:55:56 -0400 | [diff] [blame] | 128 | global $SEC; |
| 129 | $this->security =& $SEC; |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 130 | |
Pascal Kriete | aaec1e4 | 2011-01-20 00:01:21 -0500 | [diff] [blame] | 131 | // Do we need the UTF-8 class? |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 132 | if (UTF8_ENABLED === TRUE) |
| 133 | { |
| 134 | global $UNI; |
| 135 | $this->uni =& $UNI; |
| 136 | } |
| 137 | |
| 138 | // Sanitize global arrays |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | $this->_sanitize_globals(); |
| 140 | } |
| 141 | |
| 142 | // -------------------------------------------------------------------- |
| 143 | |
| 144 | /** |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 145 | * Fetch from array |
| 146 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 147 | * Internal method used to retrieve values from global arrays. |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 148 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 149 | * @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 Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 153 | */ |
Andrey Andreev | 80a16b1 | 2014-01-08 17:19:03 +0200 | [diff] [blame^] | 154 | protected function _fetch_from_array(&$array, $index = '', $xss_clean = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | { |
Andrey Andreev | 80a16b1 | 2014-01-08 17:19:03 +0200 | [diff] [blame^] | 156 | is_bool($xss_clean) OR $xss_clean = $this->_enable_xss; |
| 157 | |
nisheeth-barthwal | a7447d2 | 2013-03-21 15:48:10 +0530 | [diff] [blame] | 158 | if (isset($array[$index])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | { |
nisheeth-barthwal | a7447d2 | 2013-03-21 15:48:10 +0530 | [diff] [blame] | 160 | $value = $array[$index]; |
| 161 | } |
nisheeth-barthwal | 47ea5a8 | 2013-03-26 18:57:28 +0530 | [diff] [blame] | 162 | elseif (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) // Does the index contain array notation |
nisheeth-barthwal | a7447d2 | 2013-03-21 15:48:10 +0530 | [diff] [blame] | 163 | { |
nisheeth-barthwal | 47ea5a8 | 2013-03-26 18:57:28 +0530 | [diff] [blame] | 164 | $value = $array; |
nisheeth-barthwal | 77236e0 | 2013-03-25 23:42:36 +0530 | [diff] [blame] | 165 | for ($i = 0; $i < $count; $i++) |
nisheeth-barthwal | a7447d2 | 2013-03-21 15:48:10 +0530 | [diff] [blame] | 166 | { |
nisheeth-barthwal | 77236e0 | 2013-03-25 23:42:36 +0530 | [diff] [blame] | 167 | $key = trim($matches[0][$i], '[]'); |
nisheeth-barthwal | 408cbb4 | 2013-03-26 19:06:40 +0530 | [diff] [blame] | 168 | if ($key === '') // Empty notation will return the value as array |
nisheeth-barthwal | a7447d2 | 2013-03-21 15:48:10 +0530 | [diff] [blame] | 169 | { |
nisheeth-barthwal | 77236e0 | 2013-03-25 23:42:36 +0530 | [diff] [blame] | 170 | break; |
nisheeth-barthwal | a7447d2 | 2013-03-21 15:48:10 +0530 | [diff] [blame] | 171 | } |
nisheeth-barthwal | 47ea5a8 | 2013-03-26 18:57:28 +0530 | [diff] [blame] | 172 | |
| 173 | if (isset($value[$key])) |
nisheeth-barthwal | a7447d2 | 2013-03-21 15:48:10 +0530 | [diff] [blame] | 174 | { |
nisheeth-barthwal | 47ea5a8 | 2013-03-26 18:57:28 +0530 | [diff] [blame] | 175 | $value = $value[$key]; |
nisheeth-barthwal | 77236e0 | 2013-03-25 23:42:36 +0530 | [diff] [blame] | 176 | } |
| 177 | else |
| 178 | { |
| 179 | return NULL; |
nisheeth-barthwal | a7447d2 | 2013-03-21 15:48:10 +0530 | [diff] [blame] | 180 | } |
| 181 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | } |
nisheeth-barthwal | 77236e0 | 2013-03-25 23:42:36 +0530 | [diff] [blame] | 183 | else |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | { |
nisheeth-barthwal | 77236e0 | 2013-03-25 23:42:36 +0530 | [diff] [blame] | 185 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | } |
| 187 | |
nisheeth-barthwal | 77236e0 | 2013-03-25 23:42:36 +0530 | [diff] [blame] | 188 | return ($xss_clean === TRUE) |
| 189 | ? $this->security->xss_clean($value) |
| 190 | : $value; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | // -------------------------------------------------------------------- |
| 194 | |
| 195 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 196 | * Fetch an item from the GET array |
| 197 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 198 | * @param string $index Index for item to be fetched from $_GET |
| 199 | * @param bool $xss_clean Whether to apply XSS filtering |
| 200 | * @return mixed |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 201 | */ |
Andrey Andreev | 80a16b1 | 2014-01-08 17:19:03 +0200 | [diff] [blame^] | 202 | public function get($index = NULL, $xss_clean = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | { |
Andrey Andreev | 80a16b1 | 2014-01-08 17:19:03 +0200 | [diff] [blame^] | 204 | is_bool($xss_clean) OR $xss_clean = $this->_enable_xss; |
| 205 | |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 206 | // Check if a field has been provided |
Andrey Andreev | 77bd21b | 2012-11-20 16:34:15 +0200 | [diff] [blame] | 207 | if ($index === NULL) |
vascopj | ff1cfa1 | 2011-02-13 21:30:19 +0000 | [diff] [blame] | 208 | { |
Andrey Andreev | 77bd21b | 2012-11-20 16:34:15 +0200 | [diff] [blame] | 209 | if (empty($_GET)) |
| 210 | { |
| 211 | return array(); |
| 212 | } |
| 213 | |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 214 | $get = array(); |
vascopj | ff1cfa1 | 2011-02-13 21:30:19 +0000 | [diff] [blame] | 215 | |
| 216 | // loop through the full _GET array |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 217 | foreach (array_keys($_GET) as $key) |
vascopj | ff1cfa1 | 2011-02-13 21:30:19 +0000 | [diff] [blame] | 218 | { |
nisheeth-barthwal | a5bcfb1 | 2013-03-23 10:53:51 +0530 | [diff] [blame] | 219 | $get[$key] = $this->_fetch_from_array($_GET, $key, $xss_clean); |
vascopj | ff1cfa1 | 2011-02-13 21:30:19 +0000 | [diff] [blame] | 220 | } |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 221 | return $get; |
vascopj | ff1cfa1 | 2011-02-13 21:30:19 +0000 | [diff] [blame] | 222 | } |
| 223 | |
nisheeth-barthwal | a5bcfb1 | 2013-03-23 10:53:51 +0530 | [diff] [blame] | 224 | return $this->_fetch_from_array($_GET, $index, $xss_clean); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | // -------------------------------------------------------------------- |
| 228 | |
| 229 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 230 | * Fetch an item from the POST array |
| 231 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 232 | * @param string $index Index for item to be fetched from $_POST |
| 233 | * @param bool $xss_clean Whether to apply XSS filtering |
| 234 | * @return mixed |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 235 | */ |
Andrey Andreev | 80a16b1 | 2014-01-08 17:19:03 +0200 | [diff] [blame^] | 236 | public function post($index = NULL, $xss_clean = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | { |
Andrey Andreev | 80a16b1 | 2014-01-08 17:19:03 +0200 | [diff] [blame^] | 238 | is_bool($xss_clean) OR $xss_clean = $this->_enable_xss; |
| 239 | |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 240 | // Check if a field has been provided |
Andrey Andreev | 77bd21b | 2012-11-20 16:34:15 +0200 | [diff] [blame] | 241 | if ($index === NULL) |
vascopj | 0ba58b8 | 2011-02-06 14:20:21 +0000 | [diff] [blame] | 242 | { |
Andrey Andreev | 77bd21b | 2012-11-20 16:34:15 +0200 | [diff] [blame] | 243 | if (empty($_POST)) |
| 244 | { |
| 245 | return array(); |
| 246 | } |
| 247 | |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 248 | $post = array(); |
vascopj | 0ba58b8 | 2011-02-06 14:20:21 +0000 | [diff] [blame] | 249 | |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 250 | // Loop through the full _POST array and return it |
| 251 | foreach (array_keys($_POST) as $key) |
vascopj | 0ba58b8 | 2011-02-06 14:20:21 +0000 | [diff] [blame] | 252 | { |
nisheeth-barthwal | a5bcfb1 | 2013-03-23 10:53:51 +0530 | [diff] [blame] | 253 | $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean); |
vascopj | 0ba58b8 | 2011-02-06 14:20:21 +0000 | [diff] [blame] | 254 | } |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 255 | return $post; |
vascopj | 0ba58b8 | 2011-02-06 14:20:21 +0000 | [diff] [blame] | 256 | } |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 257 | |
nisheeth-barthwal | a5bcfb1 | 2013-03-23 10:53:51 +0530 | [diff] [blame] | 258 | return $this->_fetch_from_array($_POST, $index, $xss_clean); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // -------------------------------------------------------------------- |
| 262 | |
| 263 | /** |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 264 | * Fetch an item from POST data with fallback to GET |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 265 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 266 | * @param string $index Index for item to be fetched from $_POST or $_GET |
| 267 | * @param bool $xss_clean Whether to apply XSS filtering |
| 268 | * @return mixed |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 269 | */ |
Andrey Andreev | 80a16b1 | 2014-01-08 17:19:03 +0200 | [diff] [blame^] | 270 | public function post_get($index = '', $xss_clean = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 271 | { |
Andrey Andreev | 80a16b1 | 2014-01-08 17:19:03 +0200 | [diff] [blame^] | 272 | is_bool($xss_clean) OR $xss_clean = $this->_enable_xss; |
| 273 | |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 274 | return isset($_POST[$index]) |
nisheeth-barthwal | a5bcfb1 | 2013-03-23 10:53:51 +0530 | [diff] [blame] | 275 | ? $this->post($index, $xss_clean) |
| 276 | : $this->get($index, $xss_clean); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | // -------------------------------------------------------------------- |
| 280 | |
| 281 | /** |
vlakoff | 441fd26 | 2013-08-11 20:36:41 +0200 | [diff] [blame] | 282 | * Fetch an item from GET data with fallback to POST |
| 283 | * |
| 284 | * @param string $index Index for item to be fetched from $_GET or $_POST |
| 285 | * @param bool $xss_clean Whether to apply XSS filtering |
| 286 | * @return mixed |
| 287 | */ |
Andrey Andreev | 80a16b1 | 2014-01-08 17:19:03 +0200 | [diff] [blame^] | 288 | public function get_post($index = '', $xss_clean = NULL) |
vlakoff | 441fd26 | 2013-08-11 20:36:41 +0200 | [diff] [blame] | 289 | { |
Andrey Andreev | 80a16b1 | 2014-01-08 17:19:03 +0200 | [diff] [blame^] | 290 | is_bool($xss_clean) OR $xss_clean = $this->_enable_xss; |
| 291 | |
vlakoff | 441fd26 | 2013-08-11 20:36:41 +0200 | [diff] [blame] | 292 | return isset($_GET[$index]) |
| 293 | ? $this->get($index, $xss_clean) |
| 294 | : $this->post($index, $xss_clean); |
| 295 | } |
| 296 | |
| 297 | // -------------------------------------------------------------------- |
| 298 | |
| 299 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 300 | * Fetch an item from the COOKIE array |
| 301 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 302 | * @param string $index Index for item to be fetched from $_COOKIE |
| 303 | * @param bool $xss_clean Whether to apply XSS filtering |
| 304 | * @return mixed |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 305 | */ |
Andrey Andreev | 80a16b1 | 2014-01-08 17:19:03 +0200 | [diff] [blame^] | 306 | public function cookie($index = '', $xss_clean = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 307 | { |
Andrey Andreev | 80a16b1 | 2014-01-08 17:19:03 +0200 | [diff] [blame^] | 308 | is_bool($xss_clean) OR $xss_clean = $this->_enable_xss; |
| 309 | |
nisheeth-barthwal | a5bcfb1 | 2013-03-23 10:53:51 +0530 | [diff] [blame] | 310 | return $this->_fetch_from_array($_COOKIE, $index, $xss_clean); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 313 | // -------------------------------------------------------------------- |
| 314 | |
| 315 | /** |
| 316 | * Fetch an item from the SERVER array |
| 317 | * |
| 318 | * @param string $index Index for item to be fetched from $_SERVER |
| 319 | * @param bool $xss_clean Whether to apply XSS filtering |
| 320 | * @return mixed |
| 321 | */ |
Andrey Andreev | 80a16b1 | 2014-01-08 17:19:03 +0200 | [diff] [blame^] | 322 | public function server($index = '', $xss_clean = NULL) |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 323 | { |
Andrey Andreev | 80a16b1 | 2014-01-08 17:19:03 +0200 | [diff] [blame^] | 324 | is_bool($xss_clean) OR $xss_clean = $this->_enable_xss; |
| 325 | |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 326 | return $this->_fetch_from_array($_SERVER, $index, $xss_clean); |
| 327 | } |
| 328 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 329 | // ------------------------------------------------------------------------ |
| 330 | |
| 331 | /** |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 332 | * Fetch an item from the php://input stream |
| 333 | * |
| 334 | * Useful when you need to access PUT, DELETE or PATCH request data. |
| 335 | * |
| 336 | * @param string $index Index for item to be fetched |
| 337 | * @param bool $xss_clean Whether to apply XSS filtering |
| 338 | * @return mixed |
| 339 | */ |
Andrey Andreev | 80a16b1 | 2014-01-08 17:19:03 +0200 | [diff] [blame^] | 340 | public function input_stream($index = '', $xss_clean = NULL) |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 341 | { |
Andrey Andreev | 80a16b1 | 2014-01-08 17:19:03 +0200 | [diff] [blame^] | 342 | is_bool($xss_clean) OR $xss_clean = $this->_enable_xss; |
| 343 | |
Andrey Andreev | 303eef0 | 2012-11-06 14:55:48 +0200 | [diff] [blame] | 344 | // The input stream can only be read once, so we'll need to check |
| 345 | // if we have already done that first. |
| 346 | if (is_array($this->_input_stream)) |
| 347 | { |
| 348 | return $this->_fetch_from_array($this->_input_stream, $index, $xss_clean); |
| 349 | } |
| 350 | |
| 351 | // Parse the input stream in our cache var |
| 352 | parse_str(file_get_contents('php://input'), $this->_input_stream); |
| 353 | if ( ! is_array($this->_input_stream)) |
| 354 | { |
| 355 | $this->_input_stream = array(); |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | return $this->_fetch_from_array($this->_input_stream, $index, $xss_clean); |
| 360 | } |
| 361 | |
| 362 | // ------------------------------------------------------------------------ |
| 363 | |
| 364 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 365 | * Set cookie |
| 366 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 367 | * Accepts an arbitrary number of parameters (up to 7) or an associative |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 368 | * array in the first parameter containing all the values. |
| 369 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 370 | * @param string|mixed[] $name Cookie name or an array containing parameters |
| 371 | * @param string $value Cookie value |
| 372 | * @param int $expire Cookie expiration time in seconds |
| 373 | * @param string $domain Cookie domain (e.g.: '.yourdomain.com') |
| 374 | * @param string $path Cookie path (default: '/') |
| 375 | * @param string $prefix Cookie name prefix |
| 376 | * @param bool $secure Whether to only transfer cookies via SSL |
| 377 | * @param bool $httponly Whether to only makes the cookie accessible via HTTP (no javascript) |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 378 | * @return void |
| 379 | */ |
Andrey Andreev | 8f5420b | 2014-01-06 10:34:23 +0200 | [diff] [blame] | 380 | public function set_cookie($name, $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE) |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 381 | { |
| 382 | if (is_array($name)) |
| 383 | { |
tobiasbg | 9aa7dc9 | 2011-02-18 21:57:13 +0100 | [diff] [blame] | 384 | // always leave 'name' in last place, as the loop will break otherwise, due to $$item |
freewil | 4ad0fd8 | 2012-03-13 22:37:42 -0400 | [diff] [blame] | 385 | foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name') as $item) |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 386 | { |
| 387 | if (isset($name[$item])) |
| 388 | { |
| 389 | $$item = $name[$item]; |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 394 | if ($prefix === '' && config_item('cookie_prefix') !== '') |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 395 | { |
| 396 | $prefix = config_item('cookie_prefix'); |
| 397 | } |
Andrey Andreev | 9ba661b | 2012-06-04 14:44:34 +0300 | [diff] [blame] | 398 | |
| 399 | if ($domain == '' && config_item('cookie_domain') != '') |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 400 | { |
| 401 | $domain = config_item('cookie_domain'); |
| 402 | } |
Andrey Andreev | 9ba661b | 2012-06-04 14:44:34 +0300 | [diff] [blame] | 403 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 404 | if ($path === '/' && config_item('cookie_path') !== '/') |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 405 | { |
| 406 | $path = config_item('cookie_path'); |
| 407 | } |
Andrey Andreev | 9ba661b | 2012-06-04 14:44:34 +0300 | [diff] [blame] | 408 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 409 | if ($secure === FALSE && config_item('cookie_secure') !== FALSE) |
tobiasbg | 9aa7dc9 | 2011-02-18 21:57:13 +0100 | [diff] [blame] | 410 | { |
| 411 | $secure = config_item('cookie_secure'); |
| 412 | } |
Andrey Andreev | 9ba661b | 2012-06-04 14:44:34 +0300 | [diff] [blame] | 413 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 414 | if ($httponly === FALSE && config_item('cookie_httponly') !== FALSE) |
freewil | 4ad0fd8 | 2012-03-13 22:37:42 -0400 | [diff] [blame] | 415 | { |
| 416 | $httponly = config_item('cookie_httponly'); |
| 417 | } |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 418 | |
| 419 | if ( ! is_numeric($expire)) |
| 420 | { |
| 421 | $expire = time() - 86500; |
| 422 | } |
| 423 | else |
| 424 | { |
Phil Sturgeon | c808915 | 2010-12-27 19:06:28 +0000 | [diff] [blame] | 425 | $expire = ($expire > 0) ? time() + $expire : 0; |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 426 | } |
| 427 | |
freewil | 4ad0fd8 | 2012-03-13 22:37:42 -0400 | [diff] [blame] | 428 | setcookie($prefix.$name, $value, $expire, $path, $domain, $secure, $httponly); |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 429 | } |
| 430 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | // -------------------------------------------------------------------- |
| 432 | |
| 433 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 434 | * Fetch the IP Address |
| 435 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 436 | * Determines and validates the visitor's IP address. |
| 437 | * |
| 438 | * @return string IP address |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 439 | */ |
Bo-Yi Wu | 4db872f | 2011-09-12 10:52:37 +0800 | [diff] [blame] | 440 | public function ip_address() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 441 | { |
| 442 | if ($this->ip_address !== FALSE) |
| 443 | { |
| 444 | return $this->ip_address; |
| 445 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 446 | |
Andrey Andreev | 9ac557f | 2012-10-06 20:27:57 +0300 | [diff] [blame] | 447 | $proxy_ips = config_item('proxy_ips'); |
Andrey Andreev | ea7a866 | 2012-10-09 13:36:31 +0300 | [diff] [blame] | 448 | if ( ! empty($proxy_ips) && ! is_array($proxy_ips)) |
Andrey Andreev | 9ac557f | 2012-10-06 20:27:57 +0300 | [diff] [blame] | 449 | { |
| 450 | $proxy_ips = explode(',', str_replace(' ', '', $proxy_ips)); |
| 451 | } |
Andrey Andreev | 5b92ae1 | 2012-10-04 13:05:03 +0300 | [diff] [blame] | 452 | |
Andrey Andreev | 9ac557f | 2012-10-06 20:27:57 +0300 | [diff] [blame] | 453 | $this->ip_address = $this->server('REMOTE_ADDR'); |
| 454 | |
| 455 | if ($proxy_ips) |
| 456 | { |
| 457 | foreach (array('HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'HTTP_X_CLIENT_IP', 'HTTP_X_CLUSTER_CLIENT_IP') as $header) |
Jordan Pittman | 8960acf | 2012-07-23 09:05:49 -0300 | [diff] [blame] | 458 | { |
Andrey Andreev | 9ac557f | 2012-10-06 20:27:57 +0300 | [diff] [blame] | 459 | if (($spoof = $this->server($header)) !== NULL) |
Jordan Pittman | 8960acf | 2012-07-23 09:05:49 -0300 | [diff] [blame] | 460 | { |
Andrey Andreev | 9ac557f | 2012-10-06 20:27:57 +0300 | [diff] [blame] | 461 | // Some proxies typically list the whole chain of IP |
| 462 | // addresses through which the client has reached us. |
| 463 | // e.g. client_ip, proxy_ip1, proxy_ip2, etc. |
Andrey Andreev | e24eed7 | 2012-11-02 23:33:45 +0200 | [diff] [blame] | 464 | sscanf($spoof, '%[^,]', $spoof); |
Andrey Andreev | 9ac557f | 2012-10-06 20:27:57 +0300 | [diff] [blame] | 465 | |
| 466 | if ( ! $this->valid_ip($spoof)) |
| 467 | { |
| 468 | $spoof = NULL; |
| 469 | } |
| 470 | else |
| 471 | { |
Jordan Pittman | a5a7135 | 2012-07-20 19:36:43 -0300 | [diff] [blame] | 472 | break; |
| 473 | } |
| 474 | } |
Andrey Andreev | 5b92ae1 | 2012-10-04 13:05:03 +0300 | [diff] [blame] | 475 | } |
Andrey Andreev | 9ac557f | 2012-10-06 20:27:57 +0300 | [diff] [blame] | 476 | |
Andrey Andreev | e45ad2b | 2012-10-09 13:11:15 +0300 | [diff] [blame] | 477 | if ($spoof) |
Andrey Andreev | 5b92ae1 | 2012-10-04 13:05:03 +0300 | [diff] [blame] | 478 | { |
Andrey Andreev | 9df35b4 | 2012-10-09 13:37:58 +0300 | [diff] [blame] | 479 | for ($i = 0, $c = count($proxy_ips); $i < $c; $i++) |
Andrey Andreev | 9ac557f | 2012-10-06 20:27:57 +0300 | [diff] [blame] | 480 | { |
| 481 | // Check if we have an IP address or a subnet |
| 482 | if (strpos($proxy_ips[$i], '/') === FALSE) |
| 483 | { |
| 484 | // An IP address (and not a subnet) is specified. |
| 485 | // We can compare right away. |
| 486 | if ($proxy_ips[$i] === $this->ip_address) |
| 487 | { |
| 488 | $this->ip_address = $spoof; |
| 489 | break; |
| 490 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | |
Andrey Andreev | 9ac557f | 2012-10-06 20:27:57 +0300 | [diff] [blame] | 492 | continue; |
| 493 | } |
| 494 | |
| 495 | // We have a subnet ... now the heavy lifting begins |
| 496 | isset($separator) OR $separator = $this->valid_ip($this->ip_address, 'ipv6') ? ':' : '.'; |
| 497 | |
| 498 | // If the proxy entry doesn't match the IP protocol - skip it |
| 499 | if (strpos($proxy_ips[$i], $separator) === FALSE) |
| 500 | { |
| 501 | continue; |
| 502 | } |
| 503 | |
| 504 | // Convert the REMOTE_ADDR IP address to binary, if needed |
Andrey Andreev | 82d2cf1 | 2012-10-13 12:38:42 +0300 | [diff] [blame] | 505 | if ( ! isset($ip, $sprintf)) |
Andrey Andreev | 9ac557f | 2012-10-06 20:27:57 +0300 | [diff] [blame] | 506 | { |
| 507 | if ($separator === ':') |
| 508 | { |
| 509 | // Make sure we're have the "full" IPv6 format |
Andrey Andreev | 82d2cf1 | 2012-10-13 12:38:42 +0300 | [diff] [blame] | 510 | $ip = explode(':', |
| 511 | str_replace('::', |
| 512 | str_repeat(':', 9 - substr_count($this->ip_address, ':')), |
| 513 | $this->ip_address |
| 514 | ) |
| 515 | ); |
| 516 | |
| 517 | for ($i = 0; $i < 8; $i++) |
| 518 | { |
| 519 | $ip[$i] = intval($ip[$i], 16); |
| 520 | } |
| 521 | |
| 522 | $sprintf = '%016b%016b%016b%016b%016b%016b%016b%016b'; |
Andrey Andreev | 9ac557f | 2012-10-06 20:27:57 +0300 | [diff] [blame] | 523 | } |
| 524 | else |
| 525 | { |
Andrey Andreev | 82d2cf1 | 2012-10-13 12:38:42 +0300 | [diff] [blame] | 526 | $ip = explode('.', $this->ip_address); |
| 527 | $sprintf = '%08b%08b%08b%08b'; |
Andrey Andreev | 9ac557f | 2012-10-06 20:27:57 +0300 | [diff] [blame] | 528 | } |
| 529 | |
Andrey Andreev | 82d2cf1 | 2012-10-13 12:38:42 +0300 | [diff] [blame] | 530 | $ip = vsprintf($sprintf, $ip); |
Andrey Andreev | 9ac557f | 2012-10-06 20:27:57 +0300 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | // Split the netmask length off the network address |
Andrey Andreev | e24eed7 | 2012-11-02 23:33:45 +0200 | [diff] [blame] | 534 | sscanf($proxy_ips[$i], '%[^/]/%d', $netaddr, $masklen); |
Andrey Andreev | 9ac557f | 2012-10-06 20:27:57 +0300 | [diff] [blame] | 535 | |
| 536 | // Again, an IPv6 address is most likely in a compressed form |
| 537 | if ($separator === ':') |
| 538 | { |
Andrey Andreev | 82d2cf1 | 2012-10-13 12:38:42 +0300 | [diff] [blame] | 539 | $netaddr = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($netaddr, ':')), $netaddr)); |
| 540 | for ($i = 0; $i < 8; $i++) |
| 541 | { |
| 542 | $netaddr[$i] = intval($netaddr[$i], 16); |
| 543 | } |
| 544 | } |
| 545 | else |
| 546 | { |
| 547 | $netaddr = explode('.', $netaddr); |
Andrey Andreev | 9ac557f | 2012-10-06 20:27:57 +0300 | [diff] [blame] | 548 | } |
| 549 | |
Andrey Andreev | 82d2cf1 | 2012-10-13 12:38:42 +0300 | [diff] [blame] | 550 | // Convert to binary and finally compare |
| 551 | if (strncmp($ip, vsprintf($sprintf, $netaddr), $masklen) === 0) |
Andrey Andreev | 9ac557f | 2012-10-06 20:27:57 +0300 | [diff] [blame] | 552 | { |
| 553 | $this->ip_address = $spoof; |
| 554 | break; |
| 555 | } |
| 556 | } |
| 557 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 560 | if ( ! $this->valid_ip($this->ip_address)) |
| 561 | { |
Andrey Andreev | 64e98aa | 2012-01-07 20:29:10 +0200 | [diff] [blame] | 562 | return $this->ip_address = '0.0.0.0'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | return $this->ip_address; |
| 566 | } |
| 567 | |
| 568 | // -------------------------------------------------------------------- |
| 569 | |
| 570 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 571 | * Validate IP Address |
| 572 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 573 | * @param string $ip IP address |
| 574 | * @param string $which IP protocol: 'ipv4' or 'ipv6' |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 575 | * @return bool |
| 576 | */ |
Andrey Andreev | 5a25718 | 2012-06-10 06:18:14 +0300 | [diff] [blame] | 577 | public function valid_ip($ip, $which = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 578 | { |
Andrey Andreev | 5a25718 | 2012-06-10 06:18:14 +0300 | [diff] [blame] | 579 | switch (strtolower($which)) |
| 580 | { |
| 581 | case 'ipv4': |
| 582 | $which = FILTER_FLAG_IPV4; |
| 583 | break; |
| 584 | case 'ipv6': |
| 585 | $which = FILTER_FLAG_IPV6; |
| 586 | break; |
| 587 | default: |
| 588 | $which = NULL; |
| 589 | break; |
| 590 | } |
| 591 | |
| 592 | return (bool) filter_var($ip, FILTER_VALIDATE_IP, $which); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | // -------------------------------------------------------------------- |
| 596 | |
| 597 | /** |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 598 | * Fetch User Agent string |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 599 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 600 | * @return string|null User Agent string or NULL if it doesn't exist |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 601 | */ |
Bo-Yi Wu | 4db872f | 2011-09-12 10:52:37 +0800 | [diff] [blame] | 602 | public function user_agent() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 603 | { |
| 604 | if ($this->user_agent !== FALSE) |
| 605 | { |
| 606 | return $this->user_agent; |
| 607 | } |
| 608 | |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 609 | return $this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | // -------------------------------------------------------------------- |
| 613 | |
| 614 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 615 | * Sanitize Globals |
| 616 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 617 | * Internal method serving for the following purposes: |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 618 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 619 | * - Unsets $_GET data (if query strings are not enabled) |
| 620 | * - Unsets all globals if register_globals is enabled |
| 621 | * - Cleans POST, COOKIE and SERVER data |
| 622 | * - Standardizes newline characters to PHP_EOL |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 623 | * |
| 624 | * @return void |
| 625 | */ |
Andrey Andreev | 90cfe14 | 2012-01-08 04:46:42 +0200 | [diff] [blame] | 626 | protected function _sanitize_globals() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 627 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 628 | // It would be "wrong" to unset any of these GLOBALS. |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 629 | $protected = array( |
| 630 | '_SERVER', |
| 631 | '_GET', |
| 632 | '_POST', |
| 633 | '_FILES', |
| 634 | '_REQUEST', |
| 635 | '_SESSION', |
| 636 | '_ENV', |
| 637 | 'GLOBALS', |
| 638 | 'HTTP_RAW_POST_DATA', |
| 639 | 'system_folder', |
| 640 | 'application_folder', |
| 641 | 'BM', |
| 642 | 'EXT', |
| 643 | 'CFG', |
| 644 | 'URI', |
| 645 | 'RTR', |
Timothy Warren | 67cb3ee | 2012-04-19 16:41:52 -0400 | [diff] [blame] | 646 | 'OUT', |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 647 | 'IN' |
| 648 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 649 | |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 650 | // Unset globals for security. |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 651 | // This is effectively the same as register_globals = off |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 652 | // PHP 5.4 no longer has the register_globals functionality. |
| 653 | if ( ! is_php('5.4')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 654 | { |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 655 | foreach (array($_GET, $_POST, $_COOKIE) as $global) |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 656 | { |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 657 | if (is_array($global)) |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 658 | { |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 659 | foreach ($global as $key => $val) |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 660 | { |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 661 | if ( ! in_array($key, $protected)) |
| 662 | { |
| 663 | global $$key; |
| 664 | $$key = NULL; |
| 665 | } |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 666 | } |
| 667 | } |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 668 | elseif ( ! in_array($global, $protected)) |
| 669 | { |
| 670 | global $$global; |
| 671 | $$global = NULL; |
| 672 | } |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 673 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 676 | // Is $_GET data allowed? If not we'll set the $_GET to an empty array |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 677 | if ($this->_allow_get_array === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 678 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 679 | $_GET = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 680 | } |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 681 | elseif (is_array($_GET) && count($_GET) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 682 | { |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 683 | foreach ($_GET as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 684 | { |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 685 | $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 689 | // Clean $_POST Data |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 690 | if (is_array($_POST) && count($_POST) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 691 | { |
Pascal Kriete | 5d5895f | 2011-02-14 13:27:07 -0500 | [diff] [blame] | 692 | foreach ($_POST as $key => $val) |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 693 | { |
| 694 | $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); |
| 695 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 698 | // Clean $_COOKIE Data |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 699 | if (is_array($_COOKIE) && count($_COOKIE) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 700 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 701 | // Also get rid of specially treated cookies that might be set by a server |
| 702 | // or silly application, that are of no use to a CI application anyway |
| 703 | // but that when present will trip our 'Disallowed Key Characters' alarm |
| 704 | // http://www.ietf.org/rfc/rfc2109.txt |
| 705 | // note that the key names below are single quoted strings, and are not PHP variables |
Andrey Andreev | 5ac428b | 2014-01-08 16:07:31 +0200 | [diff] [blame] | 706 | unset( |
| 707 | $_COOKIE['$Version'], |
| 708 | $_COOKIE['$Path'], |
| 709 | $_COOKIE['$Domain'] |
| 710 | ); |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 711 | |
Pascal Kriete | 5d5895f | 2011-02-14 13:27:07 -0500 | [diff] [blame] | 712 | foreach ($_COOKIE as $key => $val) |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 713 | { |
Andrey Andreev | fd0aabb | 2013-09-23 13:18:20 +0300 | [diff] [blame] | 714 | if (($cookie_key = $this->_clean_input_keys($key)) !== FALSE) |
| 715 | { |
| 716 | $_COOKIE[$cookie_key] = $this->_clean_input_data($val); |
| 717 | } |
| 718 | else |
| 719 | { |
| 720 | unset($_COOKIE[$key]); |
| 721 | } |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 722 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 725 | // Sanitize PHP_SELF |
| 726 | $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']); |
| 727 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 728 | // CSRF Protection check |
Andrey Andreev | f964b16 | 2013-11-12 17:04:55 +0200 | [diff] [blame] | 729 | if ($this->_enable_csrf === TRUE && ! is_cli()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 730 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 731 | $this->security->csrf_verify(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 732 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 733 | |
Andrey Andreev | fd0aabb | 2013-09-23 13:18:20 +0300 | [diff] [blame] | 734 | log_message('debug', 'Global POST, GET and COOKIE data sanitized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | // -------------------------------------------------------------------- |
| 738 | |
| 739 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 740 | * Clean Input Data |
| 741 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 742 | * Internal method that aids in escaping data and |
| 743 | * standardizing newline characters to PHP_EOL. |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 744 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 745 | * @param string|string[] $str Input string(s) |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 746 | * @return string |
| 747 | */ |
Andrey Andreev | 90cfe14 | 2012-01-08 04:46:42 +0200 | [diff] [blame] | 748 | protected function _clean_input_data($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 749 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 750 | if (is_array($str)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 751 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 752 | $new_array = array(); |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 753 | foreach (array_keys($str) as $key) |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 754 | { |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 755 | $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($str[$key]); |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 756 | } |
| 757 | return $new_array; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Andrey Andreev | af72862 | 2011-10-20 10:11:59 +0300 | [diff] [blame] | 760 | /* We strip slashes if magic quotes is on to keep things consistent |
| 761 | |
| 762 | NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and |
| 763 | it will probably not exist in future versions at all. |
| 764 | */ |
| 765 | if ( ! is_php('5.4') && get_magic_quotes_gpc()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 766 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 767 | $str = stripslashes($str); |
| 768 | } |
| 769 | |
| 770 | // Clean UTF-8 if supported |
| 771 | if (UTF8_ENABLED === TRUE) |
| 772 | { |
| 773 | $str = $this->uni->clean_string($str); |
| 774 | } |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 775 | |
Pascal Kriete | 14a0ac6 | 2011-04-05 14:55:56 -0400 | [diff] [blame] | 776 | // Remove control characters |
Andrey Andreev | 5ac428b | 2014-01-08 16:07:31 +0200 | [diff] [blame] | 777 | $str = remove_invisible_characters($str, FALSE); |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 778 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 779 | // Standardize newlines if needed |
Eric Roberts | b75e13d | 2013-01-27 20:10:09 -0600 | [diff] [blame] | 780 | if ($this->_standardize_newlines === TRUE) |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 781 | { |
Eric Roberts | b75e13d | 2013-01-27 20:10:09 -0600 | [diff] [blame] | 782 | return preg_replace('/(?:\r\n|[\r\n])/', PHP_EOL, $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | return $str; |
| 786 | } |
| 787 | |
| 788 | // -------------------------------------------------------------------- |
| 789 | |
| 790 | /** |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 791 | * Clean Keys |
| 792 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 793 | * Internal method that helps to prevent malicious users |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 794 | * from trying to exploit keys we make sure that keys are |
| 795 | * only named with alpha-numeric text and a few other items. |
| 796 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 797 | * @param string $str Input string |
Andrey Andreev | fd0aabb | 2013-09-23 13:18:20 +0300 | [diff] [blame] | 798 | * @param string $fatal Whether to terminate script exection |
| 799 | * or to return FALSE if an invalid |
| 800 | * key is encountered |
| 801 | * @return string|bool |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 802 | */ |
Andrey Andreev | fd0aabb | 2013-09-23 13:18:20 +0300 | [diff] [blame] | 803 | protected function _clean_input_keys($str, $fatal = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 804 | { |
bigCat | 3c0846b | 2012-08-21 00:20:20 +0800 | [diff] [blame] | 805 | if ( ! preg_match('/^[a-z0-9:_\/|-]+$/i', $str)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 806 | { |
Andrey Andreev | fd0aabb | 2013-09-23 13:18:20 +0300 | [diff] [blame] | 807 | if ($fatal === TRUE) |
| 808 | { |
| 809 | return FALSE; |
| 810 | } |
| 811 | else |
| 812 | { |
| 813 | set_status_header(503); |
| 814 | echo 'Disallowed Key Characters.'; |
| 815 | exit(EXIT_USER_INPUT); |
| 816 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 819 | // Clean UTF-8 if supported |
| 820 | if (UTF8_ENABLED === TRUE) |
| 821 | { |
Andrey Andreev | 64e98aa | 2012-01-07 20:29:10 +0200 | [diff] [blame] | 822 | return $this->uni->clean_string($str); |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 823 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 824 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 825 | return $str; |
| 826 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 827 | |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 828 | // -------------------------------------------------------------------- |
| 829 | |
| 830 | /** |
| 831 | * Request Headers |
| 832 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 833 | * @param bool $xss_clean Whether to apply XSS filtering |
Andrey Andreev | 64e98aa | 2012-01-07 20:29:10 +0200 | [diff] [blame] | 834 | * @return array |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 835 | */ |
| 836 | public function request_headers($xss_clean = FALSE) |
| 837 | { |
CJ | 71cff1d | 2013-04-16 21:50:55 +0800 | [diff] [blame] | 838 | // If header is already defined, return it immediately |
| 839 | if ( ! empty($this->headers)) |
| 840 | { |
| 841 | return $this->headers; |
| 842 | } |
| 843 | |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 844 | // In Apache, you can simply call apache_request_headers() |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 845 | if (function_exists('apache_request_headers')) |
| 846 | { |
CJ | 71cff1d | 2013-04-16 21:50:55 +0800 | [diff] [blame] | 847 | return $this->headers = apache_request_headers(); |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 848 | } |
CJ | d195f22 | 2013-04-17 01:04:13 +0800 | [diff] [blame] | 849 | |
CJ | 8347f91 | 2013-04-17 21:45:22 +0800 | [diff] [blame] | 850 | $this->headers['Content-Type'] = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : @getenv('CONTENT_TYPE'); |
CJ | d195f22 | 2013-04-17 01:04:13 +0800 | [diff] [blame] | 851 | |
| 852 | foreach ($_SERVER as $key => $val) |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 853 | { |
CJ | d195f22 | 2013-04-17 01:04:13 +0800 | [diff] [blame] | 854 | if (sscanf($key, 'HTTP_%s', $header) === 1) |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 855 | { |
CJ | d195f22 | 2013-04-17 01:04:13 +0800 | [diff] [blame] | 856 | // take SOME_HEADER and turn it into Some-Header |
| 857 | $header = str_replace('_', ' ', strtolower($header)); |
| 858 | $header = str_replace(' ', '-', ucwords($header)); |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 859 | |
CJ | d195f22 | 2013-04-17 01:04:13 +0800 | [diff] [blame] | 860 | $this->headers[$header] = $this->_fetch_from_array($_SERVER, $key, $xss_clean); |
CJ | 826990f | 2013-04-16 14:17:53 +0800 | [diff] [blame] | 861 | } |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 862 | } |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 863 | |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 864 | return $this->headers; |
| 865 | } |
| 866 | |
| 867 | // -------------------------------------------------------------------- |
| 868 | |
| 869 | /** |
| 870 | * Get Request Header |
| 871 | * |
| 872 | * Returns the value of a single member of the headers class member |
| 873 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 874 | * @param string $index Header name |
| 875 | * @param bool $xss_clean Whether to apply XSS filtering |
| 876 | * @return string|bool The requested header on success or FALSE on failure |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 877 | */ |
| 878 | public function get_request_header($index, $xss_clean = FALSE) |
| 879 | { |
| 880 | if (empty($this->headers)) |
| 881 | { |
| 882 | $this->request_headers(); |
| 883 | } |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 884 | |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 885 | if ( ! isset($this->headers[$index])) |
| 886 | { |
Phil Sturgeon | 55a6ddb | 2012-05-23 18:37:24 +0100 | [diff] [blame] | 887 | return NULL; |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 888 | } |
| 889 | |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 890 | return ($xss_clean === TRUE) |
| 891 | ? $this->security->xss_clean($this->headers[$index]) |
| 892 | : $this->headers[$index]; |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 893 | } |
| 894 | |
Greg Aker | 081ac9d | 2010-11-22 14:42:53 -0600 | [diff] [blame] | 895 | // -------------------------------------------------------------------- |
Phil Sturgeon | c382871 | 2011-01-19 12:31:47 +0000 | [diff] [blame] | 896 | |
Greg Aker | 081ac9d | 2010-11-22 14:42:53 -0600 | [diff] [blame] | 897 | /** |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 898 | * Is AJAX request? |
Greg Aker | 081ac9d | 2010-11-22 14:42:53 -0600 | [diff] [blame] | 899 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 900 | * Test to see if a request contains the HTTP_X_REQUESTED_WITH header. |
Greg Aker | 081ac9d | 2010-11-22 14:42:53 -0600 | [diff] [blame] | 901 | * |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 902 | * @return bool |
Greg Aker | 081ac9d | 2010-11-22 14:42:53 -0600 | [diff] [blame] | 903 | */ |
| 904 | public function is_ajax_request() |
| 905 | { |
Andrey Andreev | 9448afb | 2012-02-08 19:49:19 +0200 | [diff] [blame] | 906 | return ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'); |
Greg Aker | 081ac9d | 2010-11-22 14:42:53 -0600 | [diff] [blame] | 907 | } |
| 908 | |
Phil Sturgeon | c382871 | 2011-01-19 12:31:47 +0000 | [diff] [blame] | 909 | // -------------------------------------------------------------------- |
| 910 | |
| 911 | /** |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 912 | * Is CLI request? |
Phil Sturgeon | c382871 | 2011-01-19 12:31:47 +0000 | [diff] [blame] | 913 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 914 | * Test to see if a request was made from the command line. |
Phil Sturgeon | c382871 | 2011-01-19 12:31:47 +0000 | [diff] [blame] | 915 | * |
Andrey Andreev | f964b16 | 2013-11-12 17:04:55 +0200 | [diff] [blame] | 916 | * @deprecated 3.0.0 Use is_cli() instead |
| 917 | * @return bool |
Phil Sturgeon | c382871 | 2011-01-19 12:31:47 +0000 | [diff] [blame] | 918 | */ |
| 919 | public function is_cli_request() |
| 920 | { |
Andrey Andreev | f964b16 | 2013-11-12 17:04:55 +0200 | [diff] [blame] | 921 | return is_cli(); |
Phil Sturgeon | c382871 | 2011-01-19 12:31:47 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 924 | // -------------------------------------------------------------------- |
| 925 | |
| 926 | /** |
| 927 | * Get Request Method |
| 928 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 929 | * Return the request method |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 930 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 931 | * @param bool $upper Whether to return in upper or lower case |
| 932 | * (default: FALSE) |
| 933 | * @return string |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 934 | */ |
Michiel Vugteveen | 704fb16 | 2012-03-07 20:42:33 +0100 | [diff] [blame] | 935 | public function method($upper = FALSE) |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 936 | { |
Michiel Vugteveen | dc900df | 2012-03-07 20:41:37 +0100 | [diff] [blame] | 937 | return ($upper) |
| 938 | ? strtoupper($this->server('REQUEST_METHOD')) |
| 939 | : strtolower($this->server('REQUEST_METHOD')); |
Michiel Vugteveen | be0ca26 | 2012-03-07 19:09:51 +0100 | [diff] [blame] | 940 | } |
| 941 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 942 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 943 | |
| 944 | /* End of file Input.php */ |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 945 | /* Location: ./system/core/Input.php */ |