openrat-cms

# OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs

commit e3dd018d008eab1795e9a338ac0fe3253e7c37ca
parent 470f36b5dcb35d32dc01637b71d0fdc5d6594154
Author: Jan Dankert <develop@jandankert.de>
Date:   Sun, 23 Feb 2020 00:38:18 +0100

Refactoring: Namespaces for modules 'security' and 'configuration'

Diffstat:
modules/cms-core/Dispatcher.class.php | 2+-
modules/cms-ui/action/IndexAction.class.php | 2+-
modules/configuration/Config.php | 98+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
modules/configuration/Configuration.class.php | 146-------------------------------------------------------------------------------
modules/configuration/ConfigurationLoader.class.php | 219+++++++++++++++++++++++++++++++++++++++++--------------------------------------
modules/configuration/configuration.php | 50++++++++++++++++++++++++++++++++++++++++++++++++++
modules/configuration/require.php | 3+--
modules/security/require.php | 5-----
8 files changed, 264 insertions(+), 261 deletions(-)

diff --git a/modules/cms-core/Dispatcher.class.php b/modules/cms-core/Dispatcher.class.php @@ -8,7 +8,7 @@ namespace cms; use BadMethodCallException; use cms\action\Action; use cms\action\RequestParams; -use ConfigurationLoader; +use configuration\ConfigurationLoader; use database\Database; use cms\update\Update; use util\Http; diff --git a/modules/cms-ui/action/IndexAction.class.php b/modules/cms-ui/action/IndexAction.class.php @@ -864,7 +864,7 @@ class IndexAction extends Action /** * @param User $user - * @return \Config|string + * @return \configuration\Config|string */ private function getUserStyle( $user ) { diff --git a/modules/configuration/Config.php b/modules/configuration/Config.php @@ -0,0 +1,97 @@ +<?php + +namespace configuration; + +class Config +{ + private $config = array(); + + + /** + * Config constructor. + * @param $config + */ + public function __construct($config) + { + $this->config = $config; + } + + + /** + * Giving the child configuration with a fluent interface. + * + * @param $name + * @return Config + */ + public function subset($name) + { + if (isset($this->config[$name])) + return new Config($this->config[$name]); + else + return new Config(array()); + } + + + /** + * Gets the configuration value for this key. + * + * @param $name + * @param null $default + * @return mixed|null + */ + public function get($name, $default = null) + { + if (isset($this->config[$name])) { + $value = $this->config[$name]; + + // if default-value is given, the type of the default-value is forced. + if (!is_null($default)) + settype($value, gettype($default)); + return $value; + } else { + return $default; + } + } + + + /** + * Is the Config key present? + * + * @param $name + * @return bool + */ + public function has($name) + { + return isset($this->config[$name]); + } + + + /** + * Is the boolean Value true? + * + * @param $name + * @param bool $default false + * @return bool + */ + public function is($name, $default = false) + { + if (isset($this->config[$name])) + return (bool)$this->config[$name]; + else + return $default; + } + + + /** + * The configuration entries as an array. + * + * @return array + */ + public function getConfig() + { + + return $this->config; + } + + +}+ \ No newline at end of file diff --git a/modules/configuration/Configuration.class.php b/modules/configuration/Configuration.class.php @@ -1,145 +0,0 @@ -<?php - - -/* - * Liest einen Schluessel aus der Konfiguration - * - * @return String, leer falls Schluessel nicht vorhanden - */ -function config( $part1=null,$part2=null,$part3=null,$part4=null ) -{ - global $conf; - - if ( $part1==null ) - return new Config( $conf ); - - if ( $part2 == null) - if ( isset($conf[$part1])) - return $conf[$part1]; - else - return ''; - - if ( $part3 == null) - if ( isset($conf[$part1][$part2])) - return $conf[$part1][$part2]; - else - return ''; - - if ( $part4 == null) - if ( isset($conf[$part1][$part2][$part3])) - return $conf[$part1][$part2][$part3]; - else - return ''; - - if ( isset($conf[$part1][$part2][$part3][$part4])) - return $conf[$part1][$part2][$part3][$part4]; - else - return ''; -} - - -/** - * @return Config - */ -function Conf() { - - global $conf; - return new Config( $conf ); - -} - -class Config -{ - private $config = array(); - - - /** - * Config constructor. - * @param $config - */ - public function __construct( $config ) - { - $this->config = $config; - } - - - /** - * Giving the child configuration with a fluent interface. - * - * @param $name - * @return Config - */ - public function subset( $name ) - { - if ( isset( $this->config[ $name ] )) - return new Config( $this->config[$name] ); - else - return new Config( array() ); - } - - - /** - * Gets the configuration value for this key. - * - * @param $name - * @param null $default - * @return mixed|null - */ - public function get( $name, $default = null ) - { - if ( isset( $this->config[ $name ] ) ) - { - $value = $this->config[$name]; - - // if default-value is given, the type of the default-value is forced. - if( !is_null( $default) ) - settype( $value, gettype($default) ); - return $value; - } - else - { - return $default; - } - } - - - /** - * Is the Config key present? - * - * @param $name - * @return bool - */ - public function has( $name ) - { - return isset( $this->config[ $name ] ); - } - - - /** - * Is the boolean Value true? - * - * @param $name - * @param bool $default false - * @return bool - */ - public function is( $name, $default = false ) - { - if ( isset( $this->config[ $name ] ) ) - return (bool) $this->config[$name]; - else - return $default; - } - - - /** - * The configuration entries as an array. - * - * @return array - */ - public function getConfig() { - - return $this->config; - } - - -}- \ No newline at end of file diff --git a/modules/configuration/ConfigurationLoader.class.php b/modules/configuration/ConfigurationLoader.class.php @@ -1,5 +1,7 @@ <?php +namespace configuration; + use util\VariableResolver; use util\YAML; @@ -14,116 +16,121 @@ use util\YAML; */ class ConfigurationLoader { - public $configFile; - - - /* - * Erzeugt eine neue Instanz. - */ - public function __construct( $configFile ) - { - $this->configFile = $configFile; - } - - - - /** - * Ermittelt den Zeitpunkt der letzten Änderung der Konfigurationsdatei. - * - * @return int Zeitpunkt der letzten Änderung als Unix-Timestamp - */ - public function lastModificationTime() - { - return filemtime($this->configFile); - } - - - /** - * Loads the custom configuration file. - * - * @return array Configuration - */ - public function load() - { - $customConfig = ConfigurationLoader::loadCustomConfig($this->configFile); - - // Den Dateinamen der Konfigurationsdatei in die Konfiguration schreiben. - $customConfig['config']['filename' ] = $this->configFile; - $customConfig['config']['last_modification_time'] = filemtime($this->configFile); - $customConfig['config']['last_modification' ] = date('r', filemtime($this->configFile)); - $customConfig['config']['read' ] = date('r'); - - return $customConfig; - } - - - /** - * Loads the configuration file an resolves all include-commands. - * - * @return array Configuration - */ - private function loadCustomConfig( $configFile ) - { - if (!is_file($configFile) && !is_link($configFile)) { - error_log('Warning: Configuration file ' . $configFile . ' not found'); - return array(); - } - - $customConfig = YAML::parse( file_get_contents($configFile) ); - - // resolve variables - $customConfig = ConfigurationLoader::resolveVariables($customConfig); - - // Does we have includes? - if (isset($customConfig['include'])) { - - // 'include' must be an array - if (is_string($customConfig['include'])) - $customConfig['include'] = array($customConfig['include']); - - // Load include files. - foreach ($customConfig['include'] as $key => $file) { - - if ( $file[0] == '/') // File begins with '?' - ; // File has an absolute path - do not change. - else - // Prepend file path with our config directory. - $file = __DIR__.'/../../config/'.$file; - - if (substr($file, -4) == '.yml' || - substr($file, -5) == '.yaml' || - substr($file, -8) == '.yml.php' ) - $customConfig = array_replace_recursive( $customConfig, ConfigurationLoader::loadCustomConfig($file) ); - else - error_log('Warning: ' . $file . ' is no .yml file - not loaded'); - - } - } - - return $customConfig; - } - - /** - * Evaluates variables in a config array. - * Examples: - * - config-${http:host}.yml => config-yourdomain.yml - * - config-${server:http-host}.yml => config-yourdomain.yml - * - config-${env:myvar}.yml => config-myvalue.yml - * @param $config array Configuration - * @return array - */ - private function resolveVariables($config) - { + public $configFile; + + + /* + * Erzeugt eine neue Instanz. + */ + public function __construct($configFile) + { + $this->configFile = $configFile; + } + + + /** + * Ermittelt den Zeitpunkt der letzten Änderung der Konfigurationsdatei. + * + * @return int Zeitpunkt der letzten Änderung als Unix-Timestamp + */ + public function lastModificationTime() + { + return filemtime($this->configFile); + } + + + /** + * Loads the custom configuration file. + * + * @return array Configuration + */ + public function load() + { + $customConfig = ConfigurationLoader::loadCustomConfig($this->configFile); + + // Den Dateinamen der Konfigurationsdatei in die Konfiguration schreiben. + $customConfig['config']['filename'] = $this->configFile; + $customConfig['config']['last_modification_time'] = filemtime($this->configFile); + $customConfig['config']['last_modification'] = date('r', filemtime($this->configFile)); + $customConfig['config']['read'] = date('r'); + + return $customConfig; + } + + + /** + * Loads the configuration file an resolves all include-commands. + * + * @return array Configuration + */ + private function loadCustomConfig($configFile) + { + if (!is_file($configFile) && !is_link($configFile)) { + error_log('Warning: Configuration file ' . $configFile . ' not found'); + return array(); + } + + $customConfig = YAML::parse(file_get_contents($configFile)); + + // resolve variables + $customConfig = ConfigurationLoader::resolveVariables($customConfig); + + // Does we have includes? + if (isset($customConfig['include'])) { + + // 'include' must be an array + if (is_string($customConfig['include'])) + $customConfig['include'] = array($customConfig['include']); + + // Load include files. + foreach ($customConfig['include'] as $key => $file) { + + if ($file[0] == '/') // File begins with '?' + ; // File has an absolute path - do not change. + else + // Prepend file path with our config directory. + $file = __DIR__ . '/../../config/' . $file; + + if (substr($file, -4) == '.yml' || + substr($file, -5) == '.yaml' || + substr($file, -8) == '.yml.php') + $customConfig = array_replace_recursive($customConfig, ConfigurationLoader::loadCustomConfig($file)); + else + error_log('Warning: ' . $file . ' is no .yml file - not loaded'); + + } + } + + return $customConfig; + } + + /** + * Evaluates variables in a config array. + * Examples: + * - config-${http:host}.yml => config-yourdomain.yml + * - config-${server:http-host}.yml => config-yourdomain.yml + * - config-${env:myvar}.yml => config-myvalue.yml + * @param $config array Configuration + * @return array + */ + private function resolveVariables($config) + { $resolver = new VariableResolver(); - return $resolver->resolveVariablesInArrayWith($config,[ + return $resolver->resolveVariablesInArrayWith($config, [ - 'env' => function($var) { return getenv(strtoupper($var)); }, + 'env' => function ($var) { + return getenv(strtoupper($var)); + }, // http:... is a shortcut for server:http-... - 'http' => function($var) { return @$_SERVER['HTTP_' . strtoupper($var)]; }, - 'server'=> function($var) { return @$_SERVER[strtoupper($var)]; } - ] ); - } + 'http' => function ($var) { + return @$_SERVER['HTTP_' . strtoupper($var)]; + }, + 'server' => function ($var) { + return @$_SERVER[strtoupper($var)]; + } + ]); + } } diff --git a/modules/configuration/configuration.php b/modules/configuration/configuration.php @@ -0,0 +1,50 @@ +<?php + + +/* + * Liest einen Schluessel aus der Konfiguration + * + * @return String, leer falls Schluessel nicht vorhanden + */ +function config( $part1=null,$part2=null,$part3=null,$part4=null ) +{ + global $conf; + + if ( $part1==null ) + return new configuration\Config( $conf ); + + if ( $part2 == null) + if ( isset($conf[$part1])) + return $conf[$part1]; + else + return ''; + + if ( $part3 == null) + if ( isset($conf[$part1][$part2])) + return $conf[$part1][$part2]; + else + return ''; + + if ( $part4 == null) + if ( isset($conf[$part1][$part2][$part3])) + return $conf[$part1][$part2][$part3]; + else + return ''; + + if ( isset($conf[$part1][$part2][$part3][$part4])) + return $conf[$part1][$part2][$part3][$part4]; + else + return ''; +} + + +/** + * @return \configuration\Config + */ +function Conf() { + + global $conf; + return new configuration\Config( $conf ); + +} + diff --git a/modules/configuration/require.php b/modules/configuration/require.php @@ -1,4 +1,3 @@ <?php -require_once(__DIR__ . "/ConfigurationLoader.class.php"); -require_once(__DIR__ . "/Configuration.class.php"); +require_once(__DIR__ . "/configuration.php"); diff --git a/modules/security/require.php b/modules/security/require.php @@ -1,6 +1,2 @@ <?php -include( dirname(__FILE__) . '/Base2n.class.php'); -include( dirname(__FILE__) . '/Password.class.php'); - -?>- \ No newline at end of file