openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 2a362ea99163400efc4a69c0379b4c9c26443c57
parent 0ce2fff1d6956757925e1246ba1cceb96cd9c592
Author: Jan Dankert <devnull@localhost>
Date:   Wed,  6 Dec 2017 23:11:36 +0100

Preferences-Loader kann nun include-Anweisungen ausführen. Dafür entfällt die interne Logik zum Finden der Konfigurationsdateien.

Diffstat:
config/config.yml | 7+++++--
util/Preferences.class.php | 243+++++++++++++++++++++++++++++++++++++++----------------------------------------
2 files changed, 126 insertions(+), 124 deletions(-)

diff --git a/config/config.yml b/config/config.yml @@ -1,6 +1,6 @@ -# OpenRat configuration file - Minimal settings. -# change this file or copy it to 'config-<hostname>.yml'. +# OpenRat CMS configuration - Minimal settings +# Change this file or create 'config-<hostname>.yml'. database: db: @@ -9,4 +9,7 @@ database: password: dbpass # password database: cms # database name +include: + - ./config/config-${http:host}.yml + # There are a lot of more configuration settings available, see file 'config-all-example.yml' ... diff --git a/util/Preferences.class.php b/util/Preferences.class.php @@ -1,20 +1,5 @@ <?php -// OpenRat Content Management System -// Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + /** * Hilfsmethoden fuer das Lesen von Einstellungen. @@ -24,109 +9,124 @@ */ class Preferences { - /** - * Ermittelt den Zeitpunkt der letzten Änderung der Konfigurationsdatei. - * - * @return Zeitpunkt der letzten Änderung als Unix-Timestamp - */ - public static function lastModificationTime() - { - return filemtime( Preferences::configurationFile() ); - } - - - - /** - * Ermittelt den Dateinamen der Konfigurationsdatei. - */ - public static function configurationFile() - { - $config_files = array(); - - // Falls Umgebungsvariable OPENRAT_CONFIG_FILE gesetzt ist, - // dann diesen Dateinamen verwenden. - if ( !empty($_SERVER['OPENRAT_CONFIG_FILE']) ) - $config_files[] = $_SERVER['OPENRAT_CONFIG_FILE']; - - // Falls Umgebungsvariable OPENRAT_CONFIG_DIR gesetzt ist, dann - // die Datei in diesem Ordner suchen. - if ( !empty($_SERVER['OPENRAT_CONFIG_DIR']) ) - $dir = $_SERVER['OPENRAT_CONFIG_DIR']; - else - $dir = './config/'; - - - if ( !empty($_SERVER['HTTP_HOST']) ) - { - // Falls es eine Datei config-<hostname>.yml gibt, dann diese - // vor der Datei config.ini.php bevorzugen. - $config_files[] = slashify($dir).'config-'.$_SERVER['HTTP_HOST'].'.yml'; - } - - $config_files[] = slashify($dir).'config.yml'; - $config_files[] = '/etc/openrat/config.yml'; - $config_files[] = '/etc/openrat.yml'; - - // Alle Orte durchsuchen, bis die Config-Datei gefunden wird. - foreach( $config_files as $config_filename ) - { - if ( is_file($config_filename)) - return $config_filename; // Datei gefunden. - } - - throw new LogicException('Configuration file not found. Searched locations: '.implode(',',$config_files) ); - } - - - - /** - * Liest die Konfigurationsdateien im angegebenen Ordner. - * - * @param $dir Verzeichnis, welche gelesen wird. Optional. Falls nicht gesetzt, wird - * das Standard-Konfigurationsverzeichnis verwendet. - * @return Array - */ - public static function load() - { - // Fest eingebaute Standard-Konfiguration laden. - require('./util/config-default.php'); - - $filename = Preferences::configurationFile(); - $customConfig = Spyc::YAMLLoad( $filename ); - - // Besonderheit: - // Alle Konfigurationsschlüssel mit einem Punkt ('.') im Namen zu Arrays auflösen. - foreach ( $customConfig as $key=>$value ) - { - $parts = explode('.',$key); - if ( count($parts)==1 ) - ; // Kein Punkt enthalten. Dieser Konfigurationsschlüssel wird nicht geändert. - else - { - - if ( count($parts)==2) - $customConfig[$parts[0]][$parts[1]] = $value; - elseif ( count($parts)==3) - $customConfig[$parts[0]][$parts[1]][$parts[2]] = $value; - elseif ( count($parts)==4) - $customConfig[$parts[0]][$parts[1]][$parts[2]][$parts[3]] = $value; - elseif ( count($parts)==5) - $customConfig[$parts[0]][$parts[1]][$parts[2]][$parts[3]][$parts[4]] = $value; - elseif ( count($parts)==6) - $customConfig[$parts[0]][$parts[1]][$parts[2]][$parts[3]][$parts[4]][$parts[5]] = $value; - unset( $customConfig[$key] ); - } - } - - $conf = array_replace_recursive( $conf, $customConfig ); - - // Den Dateinamen der Konfigurationsdatei in die Konfiguration schreiben. - $conf['config']['filename' ] = $filename; - $conf['config']['last_modification_time'] = filemtime($filename); - $conf['config']['last_modification' ] = date('r',filemtime($filename)); - $conf['config']['read' ] = date('r'); - - return $conf; - } + /** + * Ermittelt den Zeitpunkt der letzten Änderung der Konfigurationsdatei. + * + * @return int Zeitpunkt der letzten Änderung als Unix-Timestamp + */ + public static function lastModificationTime() + { + return filemtime('./config/config.yml'); + } + + + /** + * Liest die Konfigurationsdateien im angegebenen Ordner. + * + * @return array Configuration + */ + public static function load() + { + $customConfig = Spyc::YAMLLoad('./config/config.yml'); + + // Does we have includes? + if (isset($customConfig['include'])) { + + // Resolve include file names + if (is_string($customConfig['include'])) + $customConfig['include'] = array(Preferences::evaluateFilename($customConfig['include'])); + elseif (is_array($customConfig['include'])) + foreach ($customConfig['include'] as $key => $file) + $customConfig['include'][$key] = Preferences::evaluateFilename($file); + + // Load include files. + foreach ($customConfig['include'] as $key => $file) { + if (is_file($file) || is_link($file)) { + + if (substr($file, -4) == '.yml' || substr($file, -8) == '.yml.php') + $customConfig += Spyc::YAMLLoad($file); + elseif (substr($file, -4) == '.ini' || substr($file, -8) == '.ini.php') + $customConfig += parse_ini_file($file); + + $customConfig['included-files'][] = $customConfig['include'][$key] . ' loaded'; + } else { + $customConfig['included-files'][] = $customConfig['include'][$key] . ' not found'; + } + } + } + + + // Besonderheit: + // Alle Konfigurationsschlüssel mit einem Punkt ('.') im Namen zu Arrays auflösen. + foreach ($customConfig as $key => $value) { + $parts = explode('.', $key); + if (count($parts) == 1) + ; // Kein Punkt enthalten. Dieser Konfigurationsschlüssel wird nicht geändert. + else { + + if (count($parts) == 2) + $customConfig[$parts[0]][$parts[1]] = $value; + elseif (count($parts) == 3) + $customConfig[$parts[0]][$parts[1]][$parts[2]] = $value; + elseif (count($parts) == 4) + $customConfig[$parts[0]][$parts[1]][$parts[2]][$parts[3]] = $value; + elseif (count($parts) == 5) + $customConfig[$parts[0]][$parts[1]][$parts[2]][$parts[3]][$parts[4]] = $value; + elseif (count($parts) == 6) + $customConfig[$parts[0]][$parts[1]][$parts[2]][$parts[3]][$parts[4]][$parts[5]] = $value; + unset($customConfig[$key]); + } + } + + // Fest eingebaute Standard-Konfiguration laden. + $conf = array(); + require('./util/config-default.php'); + + $conf = array_replace_recursive($conf, $customConfig); + + // Den Dateinamen der Konfigurationsdatei in die Konfiguration schreiben. + $conf['config']['last_modification_time'] = filemtime('./config/config.yml'); + $conf['config']['last_modification'] = date('r', filemtime('./config/config.yml')); + $conf['config']['read'] = date('r'); + + + return $conf; + } + + /** + * Evaluates variables in an include file string. + * Example: config-{http:host}.yml => config-yourdomain.yml + * @param $file String filename + * @return String + */ + private static function evaluateFilename($file) + { + return preg_replace_callback( + "|\\$\{([[:alnum:]]+)\:([[:alnum:]]+)\}|", + + function ($match) { + $type = $match[1]; + $value = $match[2]; + $value = str_replace('-', '_', $value); + switch (strtolower($type)) { + case 'env': + return $_ENV[strtoupper($value)]; + break; + + case 'http': // http:... is a shortcut for server:http-... + return $_SERVER['HTTP_' . strtoupper($value)]; + break; + case 'server': + return $_SERVER[strtoupper($value)]; + break; + default: + } + }, + + $file); + + return $file; + } + } -?>- \ No newline at end of file +