webConfig = Factory::getInstance()->config->getConfig('web'); // Sanitize all global arrays $this->sanitizeGlobals(); if ($this->webConfig->get('empty_global_arrays') && $this->webConfig->get('restore_global_arrays')) Events::addListener( array($this, 'restoreGlobalArrays'), 'coreShutdownEvent', Priority::HIGH ); } public function restoreGlobalArrays(NotifierEvent $event) { $_GET = $this->inputArray['get']; $_POST = $this->inputArray['post']; $_COOKIE = $this->inputArray['cookie']; $_SERVER = $this->inputArray['server']; } /** * @todo Do this later */ protected function sanitizeGlobals() { // Copy all values from the global arrays into a local inputArray $this->inputArray['get'] = ($this->webConfig->get('allow_get_input') ? $_GET : []); $this->inputArray['post'] = $_POST; $this->inputArray['cookie'] = $_COOKIE; $this->inputArray['server'] = $_SERVER; // If required to, empty the global arrays if ($this->webConfig->get('empty_global_arrays')) unset($_GET, $_POST, $_COOKIE, $_SERVER); } /** * @param string $arrayName * @param null $index * @param bool $xssClean * @return mixed */ protected function getFromInputArray(string $arrayName, $index = null, bool $xssClean = true) { // Clean XSS if requested manually or forced through configuration $xssClean = $xssClean || $this->webConfig->get('xss_clean'); // If the index is null, the entire array is requested $index = (!is_null($index) ? $index : array_keys($this->inputArray[$arrayName])); // If the requested index is an array, fetch all requested fields if (is_array($index)) { $values = []; foreach ($index as $key) $values[$key] = $this->getFromInputArray($arrayName, $key, $xssClean); return $values; } // If the requested index is a string and found, take the value if (isset($this->inputArray[$arrayName][$index])) $value = $this->inputArray[$arrayName][$index]; else return null; // @todo Implement XSS Clean here return $value; } public function get($index = null, bool $xssClean = true) { return $this->getFromInputArray('get', $index, $xssClean); } public function post($index = null, bool $xssClean = true) { return $this->getFromInputArray('post', $index, $xssClean); } public function postGet($index, bool $xssClean = true) { return isset($this->inputArray['post'][$index]) ? $this->post($index, $xssClean) : $this->get($index, $xssClean); } public function getPost($index, bool $xssClean = true) { return isset($this->inputArray['get'][$index]) ? $this->get($index, $xssClean) : $this->post($index, $xssClean); } public function cookie($index = null, bool $xssClean = true) { return $this->getFromInputArray('cookie', $index, $xssClean); } public function server($index = null, bool $xssClean = true) { return $this->getFromInputArray('server', $index, $xssClean); } /** * @todo Extend with OldInput functionality */ public function ip() { $ip = ''; // Validate IP $valid = ( (bool)filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) || (bool)filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ); } public function userAgent(bool $xssClean = true): string { return $this->getFromInputArray('server', 'HTTP_USER_AGENT', $xssClean); } public function method(bool $xssClean = true): string { return $this->getFromInputArray('server', 'REQUEST_METHOD', $xssClean); } }