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