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