o2system /
kernel
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * This file is part of the O2System Framework package. |
||||
| 4 | * |
||||
| 5 | * For the full copyright and license information, please view the LICENSE |
||||
| 6 | * file that was distributed with this source code. |
||||
| 7 | * |
||||
| 8 | * @author Steeve Andrian Salim |
||||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
| 10 | */ |
||||
| 11 | |||||
| 12 | // ------------------------------------------------------------------------ |
||||
| 13 | |||||
| 14 | namespace O2System\Kernel\Http; |
||||
| 15 | |||||
| 16 | // ------------------------------------------------------------------------ |
||||
| 17 | |||||
| 18 | use O2System\Psr\Http\Message\UploadedFileInterface; |
||||
| 19 | use O2System\Spl\DataStructures\SplArrayObject; |
||||
| 20 | |||||
| 21 | /** |
||||
| 22 | * Class Input |
||||
| 23 | * |
||||
| 24 | * Http Kernel Input data with optional filter functionality, all data as it has arrived to the |
||||
| 25 | * Kernel Input from the CGI and/or PHP environment, including: |
||||
| 26 | * |
||||
| 27 | * - The values represented in $_SERVER, $_ENV, $_REQUEST and $_SESSION. |
||||
| 28 | * - Any cookies provided (generally via $_COOKIE) |
||||
| 29 | * - Query string arguments (generally via $_GET, or as parsed via parse_str()) |
||||
| 30 | * - Uploader files, if any (as represented by $_FILES) |
||||
| 31 | * - Deserialized body binds (generally from $_POST) |
||||
| 32 | * |
||||
| 33 | * @package O2System\Kernel\Http |
||||
| 34 | */ |
||||
| 35 | class Input |
||||
| 36 | { |
||||
| 37 | /** |
||||
| 38 | * Input::__construct |
||||
| 39 | */ |
||||
| 40 | public function __construct() |
||||
| 41 | { |
||||
| 42 | // Turn register_globals off. |
||||
| 43 | if ( ! ini_get('register_globals')) { |
||||
| 44 | return; |
||||
| 45 | } |
||||
| 46 | |||||
| 47 | if (isset($_REQUEST[ 'GLOBALS' ])) { |
||||
| 48 | die('GLOBALS overwrite attempt detected'); |
||||
|
0 ignored issues
–
show
|
|||||
| 49 | } |
||||
| 50 | |||||
| 51 | // Variables that shouldn't be unset |
||||
| 52 | $no_unset = ['GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', 'table_prefix']; |
||||
| 53 | |||||
| 54 | $input = array_merge($_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES, |
||||
| 55 | isset($_SESSION) && is_array($_SESSION) ? $_SESSION : []); |
||||
| 56 | foreach ($input as $k => $v) { |
||||
| 57 | if ( ! in_array($k, $no_unset) && isset($GLOBALS[ $k ])) { |
||||
| 58 | unset($GLOBALS[ $k ]); |
||||
| 59 | } |
||||
| 60 | } |
||||
| 61 | |||||
| 62 | // Standardize $_SERVER variables across setups. |
||||
| 63 | $default_server_values = [ |
||||
| 64 | 'SERVER_SOFTWARE' => '', |
||||
| 65 | 'REQUEST_URI' => '', |
||||
| 66 | ]; |
||||
| 67 | |||||
| 68 | $_SERVER = array_merge($default_server_values, $_SERVER); |
||||
| 69 | |||||
| 70 | // Fix for IIS when running with PHP ISAPI |
||||
| 71 | if (empty($_SERVER[ 'REQUEST_URI' ]) || (PHP_SAPI != 'cgi-fcgi' && preg_match('/^Microsoft-IIS\//', |
||||
| 72 | $_SERVER[ 'SERVER_SOFTWARE' ]))) { |
||||
| 73 | |||||
| 74 | if (isset($_SERVER[ 'HTTP_X_ORIGINAL_URL' ])) { |
||||
| 75 | // IIS Mod-Rewrite |
||||
| 76 | $_SERVER[ 'REQUEST_URI' ] = $_SERVER[ 'HTTP_X_ORIGINAL_URL' ]; |
||||
| 77 | } elseif (isset($_SERVER[ 'HTTP_X_REWRITE_URL' ])) { |
||||
| 78 | // IIS Isapi_Rewrite |
||||
| 79 | $_SERVER[ 'REQUEST_URI' ] = $_SERVER[ 'HTTP_X_REWRITE_URL' ]; |
||||
| 80 | } else { |
||||
| 81 | // Use ORIG_PATH_INFO if there is no PATH_INFO |
||||
| 82 | if ( ! isset($_SERVER[ 'PATH_INFO' ]) && isset($_SERVER[ 'ORIG_PATH_INFO' ])) { |
||||
| 83 | $_SERVER[ 'PATH_INFO' ] = $_SERVER[ 'ORIG_PATH_INFO' ]; |
||||
| 84 | } |
||||
| 85 | |||||
| 86 | // Some IIS + PHP configurations puts the script-name in the path-info (No need to append it twice) |
||||
| 87 | if (isset($_SERVER[ 'PATH_INFO' ])) { |
||||
| 88 | if ($_SERVER[ 'PATH_INFO' ] == $_SERVER[ 'SCRIPT_NAME' ]) { |
||||
| 89 | $_SERVER[ 'REQUEST_URI' ] = $_SERVER[ 'PATH_INFO' ]; |
||||
| 90 | } else { |
||||
| 91 | $_SERVER[ 'REQUEST_URI' ] = $_SERVER[ 'SCRIPT_NAME' ] . $_SERVER[ 'PATH_INFO' ]; |
||||
| 92 | } |
||||
| 93 | } |
||||
| 94 | |||||
| 95 | // Append the query string if it exists and isn't null |
||||
| 96 | if ( ! empty($_SERVER[ 'QUERY_STRING' ])) { |
||||
| 97 | $_SERVER[ 'REQUEST_URI' ] .= '?' . $_SERVER[ 'QUERY_STRING' ]; |
||||
| 98 | } |
||||
| 99 | } |
||||
| 100 | } |
||||
| 101 | |||||
| 102 | // Fix for PHP as CGI hosts that set SCRIPT_FILENAME to something ending in php.cgi for all requests |
||||
| 103 | if (isset($_SERVER[ 'SCRIPT_FILENAME' ]) && (strpos($_SERVER[ 'SCRIPT_FILENAME' ], |
||||
| 104 | 'php.cgi') == strlen($_SERVER[ 'SCRIPT_FILENAME' ]) - 7)) { |
||||
| 105 | $_SERVER[ 'SCRIPT_FILENAME' ] = $_SERVER[ 'PATH_TRANSLATED' ]; |
||||
| 106 | } |
||||
| 107 | |||||
| 108 | // Fix for Dreamhost and other PHP as CGI hosts |
||||
| 109 | if (strpos($_SERVER[ 'SCRIPT_NAME' ], 'php.cgi') !== false) { |
||||
| 110 | unset($_SERVER[ 'PATH_INFO' ]); |
||||
| 111 | } |
||||
| 112 | |||||
| 113 | // Fix empty PHP_SELF |
||||
| 114 | if (empty($PHP_SELF)) { |
||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
| 115 | $_SERVER[ 'PHP_SELF' ] = $PHP_SELF = preg_replace('/(\?.*)?$/', '', $_SERVER[ 'REQUEST_URI' ]); |
||||
|
0 ignored issues
–
show
|
|||||
| 116 | } |
||||
| 117 | } |
||||
| 118 | |||||
| 119 | // ------------------------------------------------------------------------ |
||||
| 120 | |||||
| 121 | /** |
||||
| 122 | * Input::getPost |
||||
| 123 | * |
||||
| 124 | * Fetch input from GET data with fallback to POST. |
||||
| 125 | * |
||||
| 126 | * @param string|null $offset The offset of $_GET or $_POST variable to fetch. |
||||
| 127 | * When set null will returns filtered $_GET or $_POST variable. |
||||
| 128 | * @param int $filter The ID of the filter to apply. |
||||
| 129 | * The Types of filters manual page lists the available filters. |
||||
| 130 | * If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. |
||||
| 131 | * This will result in no filtering taking place by default. |
||||
| 132 | * |
||||
| 133 | * @return mixed |
||||
| 134 | */ |
||||
| 135 | final public function getPost($offset, $filter = null) |
||||
| 136 | { |
||||
| 137 | // Use $_GET directly here, since filter_has_var only |
||||
| 138 | // checks the initial GET data, not anything that might |
||||
| 139 | // have been added since. |
||||
| 140 | return isset($_GET[ $offset ]) |
||||
| 141 | ? $this->get($offset, $filter) |
||||
| 142 | : $this->post($offset, $filter); |
||||
| 143 | } |
||||
| 144 | |||||
| 145 | // ------------------------------------------------------------------------ |
||||
| 146 | |||||
| 147 | /** |
||||
| 148 | * Input::get |
||||
| 149 | * |
||||
| 150 | * Fetch input from GET data. |
||||
| 151 | * |
||||
| 152 | * @param string|null $offset The offset of $_GET variable to fetch. |
||||
| 153 | * When set null will returns filtered $_GET variable. |
||||
| 154 | * @param int $filter The ID of the filter to apply. |
||||
| 155 | * The Types of filters manual page lists the available filters. |
||||
| 156 | * If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. |
||||
| 157 | * This will result in no filtering taking place by default. |
||||
| 158 | * |
||||
| 159 | * @return mixed |
||||
| 160 | */ |
||||
| 161 | final public function get($offset = null, $filter = null) |
||||
| 162 | { |
||||
| 163 | return $this->filter(INPUT_GET, $offset, $filter); |
||||
| 164 | } |
||||
| 165 | |||||
| 166 | // ------------------------------------------------------------------------ |
||||
| 167 | |||||
| 168 | /** |
||||
| 169 | * Input::filter |
||||
| 170 | * |
||||
| 171 | * Gets a specific external variable by name and optionally filters it. |
||||
| 172 | * |
||||
| 173 | * @see http://php.net/manual/en/function.filter-input.php |
||||
| 174 | * |
||||
| 175 | * @param int $type One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV. |
||||
| 176 | * @param mixed $offset The offset key of input variable. |
||||
| 177 | * @param int $filter The ID of the filter to apply. |
||||
| 178 | * The Types of filters manual page lists the available filters. |
||||
| 179 | * If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. |
||||
| 180 | * This will result in no filtering taking place by default. |
||||
| 181 | * |
||||
| 182 | * @return mixed|\O2System\Spl\DataStructures\SplArrayObject |
||||
| 183 | */ |
||||
| 184 | protected function filter($type, $offset = null, $filter = FILTER_DEFAULT) |
||||
| 185 | { |
||||
| 186 | // If $offset is null, it means that the whole input type array is requested |
||||
| 187 | if (is_null($offset)) { |
||||
| 188 | $loopThrough = []; |
||||
| 189 | |||||
| 190 | switch ($type) { |
||||
| 191 | case INPUT_GET : |
||||
| 192 | $loopThrough = $_GET; |
||||
| 193 | break; |
||||
| 194 | case INPUT_POST : |
||||
| 195 | $loopThrough = $_POST; |
||||
| 196 | break; |
||||
| 197 | case INPUT_COOKIE : |
||||
| 198 | $loopThrough = $_COOKIE; |
||||
| 199 | break; |
||||
| 200 | case INPUT_SERVER : |
||||
| 201 | $loopThrough = $_SERVER; |
||||
| 202 | break; |
||||
| 203 | case INPUT_ENV : |
||||
| 204 | $loopThrough = $_ENV; |
||||
| 205 | break; |
||||
| 206 | case INPUT_REQUEST : |
||||
| 207 | $loopThrough = $_REQUEST; |
||||
| 208 | break; |
||||
| 209 | case INPUT_SESSION : |
||||
| 210 | $loopThrough = $_ENV; |
||||
| 211 | break; |
||||
| 212 | } |
||||
| 213 | |||||
| 214 | $loopThrough = $this->filterRecursive($loopThrough, $filter); |
||||
| 215 | |||||
| 216 | if (empty($loopThrough)) { |
||||
| 217 | return false; |
||||
| 218 | } |
||||
| 219 | |||||
| 220 | return new SplArrayObject($loopThrough); |
||||
| 221 | } // allow fetching multiple keys at once |
||||
| 222 | elseif (is_array($offset)) { |
||||
| 223 | $loopThrough = []; |
||||
| 224 | |||||
| 225 | foreach ($offset as $key) { |
||||
| 226 | $loopThrough[ $key ] = $this->filter($type, $key, $filter); |
||||
| 227 | } |
||||
| 228 | |||||
| 229 | if (empty($loopThrough)) { |
||||
| 230 | return false; |
||||
| 231 | } |
||||
| 232 | |||||
| 233 | return new SplArrayObject($loopThrough); |
||||
| 234 | } elseif (isset($offset)) { |
||||
| 235 | // Due to issues with FastCGI and testing, |
||||
| 236 | // we need to do these all manually instead |
||||
| 237 | // of the simpler filter_input(); |
||||
| 238 | switch ($type) { |
||||
| 239 | case INPUT_GET: |
||||
| 240 | $value = isset($_GET[ $offset ]) |
||||
| 241 | ? $_GET[ $offset ] |
||||
| 242 | : null; |
||||
| 243 | break; |
||||
| 244 | case INPUT_POST: |
||||
| 245 | $value = isset($_POST[ $offset ]) |
||||
| 246 | ? $_POST[ $offset ] |
||||
| 247 | : null; |
||||
| 248 | break; |
||||
| 249 | case INPUT_SERVER: |
||||
| 250 | $value = isset($_SERVER[ $offset ]) |
||||
| 251 | ? $_SERVER[ $offset ] |
||||
| 252 | : null; |
||||
| 253 | break; |
||||
| 254 | case INPUT_ENV: |
||||
| 255 | $value = isset($_ENV[ $offset ]) |
||||
| 256 | ? $_ENV[ $offset ] |
||||
| 257 | : null; |
||||
| 258 | break; |
||||
| 259 | case INPUT_COOKIE: |
||||
| 260 | $value = isset($_COOKIE[ $offset ]) |
||||
| 261 | ? $_COOKIE[ $offset ] |
||||
| 262 | : null; |
||||
| 263 | break; |
||||
| 264 | case INPUT_REQUEST: |
||||
| 265 | $value = isset($_REQUEST[ $offset ]) |
||||
| 266 | ? $_REQUEST[ $offset ] |
||||
| 267 | : null; |
||||
| 268 | break; |
||||
| 269 | case INPUT_SESSION: |
||||
| 270 | $value = isset($_SESSION[ $offset ]) |
||||
| 271 | ? $_SESSION[ $offset ] |
||||
| 272 | : null; |
||||
| 273 | break; |
||||
| 274 | default: |
||||
| 275 | $value = ''; |
||||
| 276 | } |
||||
| 277 | |||||
| 278 | if (is_array($value)) { |
||||
| 279 | $value = $this->filterRecursive($value, $filter); |
||||
| 280 | |||||
| 281 | if (is_string(key($value))) { |
||||
| 282 | return new SplArrayObject($value); |
||||
| 283 | } else { |
||||
| 284 | return $value; |
||||
| 285 | } |
||||
| 286 | } elseif (is_object($value)) { |
||||
| 287 | return $value; |
||||
| 288 | } |
||||
| 289 | |||||
| 290 | if (isset($filter)) { |
||||
| 291 | return filter_var($value, $filter); |
||||
| 292 | } |
||||
| 293 | |||||
| 294 | return $value; |
||||
| 295 | } |
||||
| 296 | |||||
| 297 | return null; |
||||
| 298 | } |
||||
| 299 | |||||
| 300 | // ------------------------------------------------------------------------ |
||||
| 301 | |||||
| 302 | /** |
||||
| 303 | * Input::filterRecursive |
||||
| 304 | * |
||||
| 305 | * Gets multiple variables and optionally filters them. |
||||
| 306 | * |
||||
| 307 | * @see http://php.net/manual/en/function.filter-var.php |
||||
| 308 | * @see http://php.net/manual/en/function.filter-var-array.php |
||||
| 309 | * |
||||
| 310 | * |
||||
| 311 | * @param array $data An array with string keys containing the data to filter. |
||||
| 312 | * @param int|mixed $filter The ID of the filter to apply. |
||||
| 313 | * The Types of filters manual page lists the available filters. |
||||
| 314 | * If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. |
||||
| 315 | * This will result in no filtering taking place by default. |
||||
| 316 | * Its also can be An array defining the arguments. |
||||
| 317 | * A valid key is a string containing a variable name and a valid value is either |
||||
| 318 | * a filter type, or an array optionally specifying the filter, flags and options. |
||||
| 319 | * If the value is an array, valid keys are filter which specifies the filter type, |
||||
| 320 | * flags which specifies any flags that apply to the filter, and options which |
||||
| 321 | * specifies any options that apply to the filter. See the example below for |
||||
| 322 | * a better understanding. |
||||
| 323 | * |
||||
| 324 | * @return mixed |
||||
| 325 | */ |
||||
| 326 | protected function filterRecursive(array $data, $filter = FILTER_DEFAULT) |
||||
| 327 | { |
||||
| 328 | foreach ($data as $key => $value) { |
||||
| 329 | if (is_array($value) AND is_array($filter)) { |
||||
| 330 | $data[ $key ] = filter_var_array($value, $filter); |
||||
| 331 | } elseif (is_array($value)) { |
||||
| 332 | $data[ $key ] = $this->filterRecursive($value, $filter); |
||||
| 333 | } elseif (isset($filter)) { |
||||
| 334 | $data[ $key ] = filter_var($value, $filter); |
||||
| 335 | } else { |
||||
| 336 | $data[ $key ] = $value; |
||||
| 337 | } |
||||
| 338 | } |
||||
| 339 | |||||
| 340 | return $data; |
||||
| 341 | } |
||||
| 342 | |||||
| 343 | // ------------------------------------------------------------------------ |
||||
| 344 | |||||
| 345 | /** |
||||
| 346 | * Input::post |
||||
| 347 | * |
||||
| 348 | * Fetch input from POST data. |
||||
| 349 | * |
||||
| 350 | * @param string|null $offset The offset of $_POST variable to fetch. |
||||
| 351 | * When set null will returns filtered $_POST variable. |
||||
| 352 | * @param int $filter The ID of the filter to apply. |
||||
| 353 | * The Types of filters manual page lists the available filters. |
||||
| 354 | * If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. |
||||
| 355 | * This will result in no filtering taking place by default. |
||||
| 356 | * |
||||
| 357 | * @return mixed |
||||
| 358 | */ |
||||
| 359 | final public function post($offset = null, $filter = null) |
||||
| 360 | { |
||||
| 361 | return $this->filter(INPUT_POST, $offset, $filter); |
||||
| 362 | } |
||||
| 363 | |||||
| 364 | // ------------------------------------------------------------------------ |
||||
| 365 | |||||
| 366 | /** |
||||
| 367 | * Input::getPost |
||||
| 368 | * |
||||
| 369 | * Fetch input from POST data with fallback to GET. |
||||
| 370 | * |
||||
| 371 | * @param string|null $offset The offset of $_POST or $_GET variable to fetch. |
||||
| 372 | * When set null will returns filtered $_POST or $_GET variable. |
||||
| 373 | * @param int $filter The ID of the filter to apply. |
||||
| 374 | * The Types of filters manual page lists the available filters. |
||||
| 375 | * If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. |
||||
| 376 | * This will result in no filtering taking place by default. |
||||
| 377 | * |
||||
| 378 | * @return mixed |
||||
| 379 | */ |
||||
| 380 | final public function postGet($offset, $filter = null) |
||||
| 381 | { |
||||
| 382 | // Use $_POST directly here, since filter_has_var only |
||||
| 383 | // checks the initial POST data, not anything that might |
||||
| 384 | // have been added since. |
||||
| 385 | return isset($_POST[ $offset ]) |
||||
| 386 | ? $this->post($offset, $filter) |
||||
| 387 | : $this->get($offset, $filter); |
||||
| 388 | } |
||||
| 389 | |||||
| 390 | //-------------------------------------------------------------------- |
||||
| 391 | |||||
| 392 | /** |
||||
| 393 | * Input::files |
||||
| 394 | * |
||||
| 395 | * Fetch input from FILES data. Returns an array of all files that have been uploaded with this |
||||
| 396 | * request. Each file is represented by an UploadedFileInterface instance. |
||||
| 397 | * |
||||
| 398 | * @param string|null $offset The offset of $_FILES variable to fetch. |
||||
| 399 | * When set null will returns filtered $_FILES variable. |
||||
| 400 | * |
||||
| 401 | * @return array|UploadedFileInterface |
||||
| 402 | */ |
||||
| 403 | final public function files($offset = null) |
||||
| 404 | { |
||||
| 405 | $uploadFiles = server_request()->getUploadedFiles(); |
||||
|
0 ignored issues
–
show
The method
getUploadedFiles() does not exist on O2System\Kernel\Http\Message\Request. Maybe you want to declare this class abstract?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 406 | |||||
| 407 | if (isset($offset)) { |
||||
| 408 | if (isset($uploadFiles[ $offset ])) { |
||||
| 409 | return $uploadFiles[ $offset ]; |
||||
| 410 | } |
||||
| 411 | } |
||||
| 412 | |||||
| 413 | return $uploadFiles; |
||||
| 414 | } |
||||
| 415 | |||||
| 416 | //-------------------------------------------------------------------- |
||||
| 417 | |||||
| 418 | /** |
||||
| 419 | * Input::env |
||||
| 420 | * |
||||
| 421 | * Fetch input from ENV data. |
||||
| 422 | * |
||||
| 423 | * @param string|null $offset The offset of $_ENV variable to fetch. |
||||
| 424 | * When set null will returns filtered $_ENV variable. |
||||
| 425 | * @param int $filter The ID of the filter to apply. |
||||
| 426 | * The Types of filters manual page lists the available filters. |
||||
| 427 | * If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. |
||||
| 428 | * This will result in no filtering taking place by default. |
||||
| 429 | * |
||||
| 430 | * @return mixed |
||||
| 431 | */ |
||||
| 432 | final public function env($offset = null, $filter = null) |
||||
| 433 | { |
||||
| 434 | return $this->filter(INPUT_ENV, $offset, $filter); |
||||
| 435 | } |
||||
| 436 | |||||
| 437 | //-------------------------------------------------------------------- |
||||
| 438 | |||||
| 439 | /** |
||||
| 440 | * Input::cookie |
||||
| 441 | * |
||||
| 442 | * Fetch input from COOKIE data. |
||||
| 443 | * |
||||
| 444 | * @param string|null $offset The offset of $_COOKIE variable to fetch. |
||||
| 445 | * When set null will returns filtered $_COOKIE variable. |
||||
| 446 | * @param int $filter The ID of the filter to apply. |
||||
| 447 | * The Types of filters manual page lists the available filters. |
||||
| 448 | * If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. |
||||
| 449 | * This will result in no filtering taking place by default. |
||||
| 450 | * |
||||
| 451 | * @return mixed |
||||
| 452 | */ |
||||
| 453 | final public function cookie($offset = null, $filter = null) |
||||
| 454 | { |
||||
| 455 | return $this->filter(INPUT_COOKIE, $offset, $filter); |
||||
| 456 | } |
||||
| 457 | |||||
| 458 | //-------------------------------------------------------------------- |
||||
| 459 | |||||
| 460 | /** |
||||
| 461 | * Input::request |
||||
| 462 | * |
||||
| 463 | * Fetch input from REQUEST data. |
||||
| 464 | * |
||||
| 465 | * @param string|null $offset The offset of $_REQUEST variable to fetch. |
||||
| 466 | * When set null will returns filtered $_REQUEST variable. |
||||
| 467 | * @param int $filter The ID of the filter to apply. |
||||
| 468 | * The Types of filters manual page lists the available filters. |
||||
| 469 | * If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. |
||||
| 470 | * This will result in no filtering taking place by default. |
||||
| 471 | * |
||||
| 472 | * @return mixed |
||||
| 473 | */ |
||||
| 474 | final public function request($offset = null, $filter = null) |
||||
| 475 | { |
||||
| 476 | return $this->filter(INPUT_REQUEST, $offset, $filter); |
||||
| 477 | } |
||||
| 478 | |||||
| 479 | //-------------------------------------------------------------------- |
||||
| 480 | |||||
| 481 | /** |
||||
| 482 | * Input::session |
||||
| 483 | * |
||||
| 484 | * Fetch input from SESSION data. |
||||
| 485 | * |
||||
| 486 | * @param string|null $offset The offset of $_SESSION variable to fetch. |
||||
| 487 | * When set null will returns filtered $_SESSION variable. |
||||
| 488 | * @param int $filter The ID of the filter to apply. |
||||
| 489 | * The Types of filters manual page lists the available filters. |
||||
| 490 | * If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. |
||||
| 491 | * This will result in no filtering taking place by default. |
||||
| 492 | * |
||||
| 493 | * @return mixed |
||||
| 494 | */ |
||||
| 495 | final public function session($offset = null, $filter = null) |
||||
| 496 | { |
||||
| 497 | return $this->filter(INPUT_SESSION, $offset, $filter); |
||||
| 498 | } |
||||
| 499 | |||||
| 500 | //-------------------------------------------------------------------- |
||||
| 501 | |||||
| 502 | /** |
||||
| 503 | * Input::ipAddress |
||||
| 504 | * |
||||
| 505 | * Fetch input ip address. |
||||
| 506 | * Determines and validates the visitor's IP address. |
||||
| 507 | * |
||||
| 508 | * @param string|array $proxyIps List of proxy ip addresses. |
||||
| 509 | * |
||||
| 510 | * @return string |
||||
| 511 | */ |
||||
| 512 | public function ipAddress($proxyIps = []) |
||||
| 513 | { |
||||
| 514 | if ( ! empty($proxyIps) && ! is_array($proxyIps)) { |
||||
| 515 | $proxyIps = explode(',', str_replace(' ', '', $proxyIps)); |
||||
| 516 | } |
||||
| 517 | |||||
| 518 | foreach ([ |
||||
| 519 | 'HTTP_CLIENT_IP', |
||||
| 520 | 'HTTP_FORWARDED', |
||||
| 521 | 'HTTP_X_FORWARDED_FOR', |
||||
| 522 | 'HTTP_X_CLIENT_IP', |
||||
| 523 | 'HTTP_X_CLUSTER_CLIENT_IP', |
||||
| 524 | 'REMOTE_ADDR', |
||||
| 525 | ] as $header |
||||
| 526 | ) { |
||||
| 527 | if (null !== ($ipAddress = $this->server($header))) { |
||||
| 528 | if (filter_var($ipAddress, FILTER_VALIDATE_IP)) { |
||||
| 529 | if ( ! in_array($ipAddress, $proxyIps)) { |
||||
| 530 | break; |
||||
| 531 | } |
||||
| 532 | } |
||||
| 533 | } |
||||
| 534 | } |
||||
| 535 | |||||
| 536 | return (empty($ipAddress) ? '0.0.0.0' : $ipAddress); |
||||
| 537 | } |
||||
| 538 | |||||
| 539 | //-------------------------------------------------------------------- |
||||
| 540 | |||||
| 541 | /** |
||||
| 542 | * Input::server |
||||
| 543 | * |
||||
| 544 | * Fetch input from SERVER data. |
||||
| 545 | * |
||||
| 546 | * @param string|null $offset The offset of $_SERVER variable to fetch. |
||||
| 547 | * When set null will returns filtered $_SERVER variable. |
||||
| 548 | * @param int $filter The ID of the filter to apply. |
||||
| 549 | * The Types of filters manual page lists the available filters. |
||||
| 550 | * If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. |
||||
| 551 | * This will result in no filtering taking place by default. |
||||
| 552 | * |
||||
| 553 | * @return mixed |
||||
| 554 | */ |
||||
| 555 | final public function server($offset = null, $filter = null) |
||||
| 556 | { |
||||
| 557 | return $this->filter(INPUT_SERVER, $offset, $filter); |
||||
| 558 | } |
||||
| 559 | |||||
| 560 | //-------------------------------------------------------------------- |
||||
| 561 | |||||
| 562 | /** |
||||
| 563 | * Input::userAgent |
||||
| 564 | * |
||||
| 565 | * @return string |
||||
| 566 | */ |
||||
| 567 | public function userAgent() |
||||
| 568 | { |
||||
| 569 | return $this->server('HTTP_USER_AGENT'); |
||||
| 570 | } |
||||
| 571 | |||||
| 572 | //-------------------------------------------------------------------- |
||||
| 573 | |||||
| 574 | /** |
||||
| 575 | * Input::bearerToken |
||||
| 576 | * |
||||
| 577 | * @return string |
||||
| 578 | */ |
||||
| 579 | public function bearerToken() |
||||
| 580 | { |
||||
| 581 | $authorization = $this->server('HTTP_AUTHORIZATION'); |
||||
| 582 | |||||
| 583 | if (preg_match('/(Bearer)/', $authorization)) { |
||||
| 584 | return str_replace('Bearer ', '', $authorization); |
||||
| 585 | } |
||||
| 586 | |||||
| 587 | return false; |
||||
|
0 ignored issues
–
show
|
|||||
| 588 | } |
||||
| 589 | |||||
| 590 | //-------------------------------------------------------------------- |
||||
| 591 | |||||
| 592 | /** |
||||
| 593 | * Input::webToken |
||||
| 594 | * |
||||
| 595 | * @return string |
||||
| 596 | */ |
||||
| 597 | public function webToken() |
||||
| 598 | { |
||||
| 599 | if ($webToken = $this->server('HTTP_X_WEB_TOKEN')) { |
||||
| 600 | return $webToken; |
||||
| 601 | } |
||||
| 602 | |||||
| 603 | return false; |
||||
|
0 ignored issues
–
show
|
|||||
| 604 | } |
||||
| 605 | |||||
| 606 | //-------------------------------------------------------------------- |
||||
| 607 | |||||
| 608 | /** |
||||
| 609 | * Input::basicAuth |
||||
| 610 | * |
||||
| 611 | * @return string |
||||
| 612 | */ |
||||
| 613 | public function basicAuth() |
||||
| 614 | { |
||||
| 615 | $authorization = $this->server('HTTP_AUTHORIZATION'); |
||||
| 616 | |||||
| 617 | if (preg_match('/(Basic)/', $authorization)) { |
||||
| 618 | return str_replace('Basic ', '', $authorization); |
||||
| 619 | } |
||||
| 620 | |||||
| 621 | return false; |
||||
|
0 ignored issues
–
show
|
|||||
| 622 | } |
||||
| 623 | |||||
| 624 | //-------------------------------------------------------------------- |
||||
| 625 | |||||
| 626 | /** |
||||
| 627 | * Input::submit |
||||
| 628 | * |
||||
| 629 | * Determines if the POST input is submit |
||||
| 630 | * |
||||
| 631 | * @return bool |
||||
| 632 | */ |
||||
| 633 | final public function submit() |
||||
| 634 | { |
||||
| 635 | return (bool)isset($_POST[ 'submit' ]); |
||||
| 636 | } |
||||
| 637 | } |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.