commit e276a7ca29f6567fb183d6808ed40c7f87344582 Author: Abel Hoogeveen Date: Mon Feb 4 00:10:18 2019 +0100 Initial commit diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..c7b07b0 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,4 @@ +.gitattributes export-ignore +.gitignore export-ignore +.gitlab-ci.yml export-ignore +test/ export-ignore \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eb2af7b --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +composer.lock +composer.phar +.idea/ +build/ +test/temp/ +vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..d630e82 --- /dev/null +++ b/composer.json @@ -0,0 +1,30 @@ +{ + "name": "fuzeworks/webcomponent", + "description": "FuzeWorks WebComponent", + "license": ["MIT"], + "authors": [ + { + "name": "TechFuze", + "homepage": "https://techfuze.net" + }, + { + "name": "FuzeWorks Community", + "homepage": "https://techfuze.net/fuzeworks/contributors" + } + ], + "require": { + "php": ">=7.1.0", + "fuzeworks/core": "dev-development", + "fuzeworks/mvcr": "dev-master" + }, + "require-dev": { + "phpunit/phpunit": "^7", + "fuzeworks/tracycomponent": "dev-master" + }, + "autoload": { + "psr-4": { + "FuzeWorks\\": "src/FuzeWorks/" + } + } + +} \ No newline at end of file diff --git a/src/Config/config.web.php b/src/Config/config.web.php new file mode 100644 index 0000000..2bf554b --- /dev/null +++ b/src/Config/config.web.php @@ -0,0 +1,40 @@ + true, + 'empty_global_arrays' => true, + 'restore_global_arrays' => true +]; \ No newline at end of file diff --git a/src/FuzeWorks/Input.php b/src/FuzeWorks/Input.php new file mode 100644 index 0000000..ce73219 --- /dev/null +++ b/src/FuzeWorks/Input.php @@ -0,0 +1,186 @@ +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); + } + + +} \ No newline at end of file diff --git a/src/FuzeWorks/Output.php b/src/FuzeWorks/Output.php new file mode 100644 index 0000000..125b6b2 --- /dev/null +++ b/src/FuzeWorks/Output.php @@ -0,0 +1,102 @@ +output; + } + + public function setOutput(string $output) + { + $this->output = $output; + } + + public function appendOutput(string $output) + { + $this->output .= $output; + } + + public function setHeader(string $header) + { + $this->headers[] = $header; + } + + public function getHeader() + { + + } + + public function setContentType() + { + + } + + public function getContentType() + { + + } + + public function setStatusHeader() + { + + } + + public function cache() + { + + } + +} \ No newline at end of file diff --git a/src/FuzeWorks/URI.php b/src/FuzeWorks/URI.php new file mode 100644 index 0000000..08e3996 --- /dev/null +++ b/src/FuzeWorks/URI.php @@ -0,0 +1,43 @@ + $this, + 'input' => '\FuzeWorks\Input', + 'output' => '\FuzeWorks\Output' + ]; + } + + /** + * @param Configurator $configurator + * @todo WebComponent will not always be running when added to FuzeWorks, move this into a separate method + */ + public function onAddComponent(Configurator $configurator) + { + // Add dependencies + $configurator->addComponent(new MVCRComponent()); + + // Invoke methods to prepare system for HTTP calls + $configurator->call('logger', 'setLoggerTemplate', null, 'logger_http'); + + // Add fallback config directory + $configurator->addDirectory( + dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Config', + 'config', + Priority::LOWEST + ); + } + + public function onCreateContainer(Factory $container) + { + } +} \ No newline at end of file