diff options
Diffstat (limited to 'lib/Configuration.php')
-rw-r--r-- | lib/Configuration.php | 115 |
1 files changed, 80 insertions, 35 deletions
diff --git a/lib/Configuration.php b/lib/Configuration.php index 64d9dde..fc575d6 100644 --- a/lib/Configuration.php +++ b/lib/Configuration.php @@ -28,7 +28,7 @@ final class Configuration { * * @todo Replace this property by a constant. */ - public static $VERSION = '2019-01-13'; + public static $VERSION = '2019-09-12'; /** * Holds the configuration data. @@ -80,35 +80,31 @@ final class Configuration { // Check PHP version if(version_compare(PHP_VERSION, '5.6.0') === -1) - die('RSS-Bridge requires at least PHP version 5.6.0!'); + self::reportError('RSS-Bridge requires at least PHP version 5.6.0!'); // extensions check if(!extension_loaded('openssl')) - die('"openssl" extension not loaded. Please check "php.ini"'); + self::reportError('"openssl" extension not loaded. Please check "php.ini"'); if(!extension_loaded('libxml')) - die('"libxml" extension not loaded. Please check "php.ini"'); + self::reportError('"libxml" extension not loaded. Please check "php.ini"'); if(!extension_loaded('mbstring')) - die('"mbstring" extension not loaded. Please check "php.ini"'); + self::reportError('"mbstring" extension not loaded. Please check "php.ini"'); if(!extension_loaded('simplexml')) - die('"simplexml" extension not loaded. Please check "php.ini"'); + self::reportError('"simplexml" extension not loaded. Please check "php.ini"'); // Allow RSS-Bridge to run without curl module in CLI mode without root certificates if(!extension_loaded('curl') && !(php_sapi_name() === 'cli' && empty(ini_get('curl.cainfo')))) - die('"curl" extension not loaded. Please check "php.ini"'); + self::reportError('"curl" extension not loaded. Please check "php.ini"'); if(!extension_loaded('json')) - die('"json" extension not loaded. Please check "php.ini"'); + self::reportError('"json" extension not loaded. Please check "php.ini"'); // Check cache folder permissions (write permissions required) if(!is_writable(PATH_CACHE)) - die('RSS-Bridge does not have write permissions for ' . PATH_CACHE . '!'); - - // Check whitelist file permissions - if(!file_exists(WHITELIST) && !is_writable(dirname(WHITELIST))) - die('RSS-Bridge does not have write permissions for ' . WHITELIST . '!'); + self::reportError('RSS-Bridge does not have write permissions for ' . PATH_CACHE . '!'); } @@ -118,15 +114,13 @@ final class Configuration { * Returns an error message and aborts execution if the configuration is invalid. * * The RSS-Bridge configuration is split into two files: - * - `config.default.ini.php`: The default configuration file that ships with - * every release of RSS-Bridge (do not modify this file!). - * - `config.ini.php`: The local configuration file that can be modified by - * server administrators. - * - * The files must be located at {@see PATH_ROOT} + * - {@see FILE_CONFIG_DEFAULT} The default configuration file that ships + * with every release of RSS-Bridge (do not modify this file!). + * - {@see FILE_CONFIG} The local configuration file that can be modified + * by server administrators. * - * RSS-Bridge will first load `config.default.ini.php` into memory and then - * replace parameters with the contents of `config.ini.php`. That way new + * RSS-Bridge will first load {@see FILE_CONFIG_DEFAULT} into memory and then + * replace parameters with the contents of {@see FILE_CONFIG}. That way new * parameters are automatically initialized with default values and custom * configurations can be reduced to the minimum set of parametes necessary * (only the ones that changed). @@ -140,16 +134,16 @@ final class Configuration { */ public static function loadConfiguration() { - if(!file_exists(PATH_ROOT . 'config.default.ini.php')) - die('The default configuration file "config.default.ini.php" is missing!'); + if(!file_exists(FILE_CONFIG_DEFAULT)) + self::reportError('The default configuration file is missing at ' . FILE_CONFIG_DEFAULT); - Configuration::$config = parse_ini_file(PATH_ROOT . 'config.default.ini.php', true, INI_SCANNER_TYPED); + Configuration::$config = parse_ini_file(FILE_CONFIG_DEFAULT, true, INI_SCANNER_TYPED); if(!Configuration::$config) - die('Error parsing config.default.ini.php'); + self::reportError('Error parsing ' . FILE_CONFIG_DEFAULT); - if(file_exists(PATH_ROOT . 'config.ini.php')) { + if(file_exists(FILE_CONFIG)) { // Replace default configuration with custom settings - foreach(parse_ini_file(PATH_ROOT . 'config.ini.php', true, INI_SCANNER_TYPED) as $header => $section) { + foreach(parse_ini_file(FILE_CONFIG, true, INI_SCANNER_TYPED) as $header => $section) { foreach($section as $key => $value) { // Skip unknown sections and keys if(array_key_exists($header, Configuration::$config) && array_key_exists($key, Configuration::$config[$header])) { @@ -159,8 +153,14 @@ final class Configuration { } } + if(!is_string(self::getConfig('system', 'timezone')) + || !in_array(self::getConfig('system', 'timezone'), timezone_identifiers_list(DateTimeZone::ALL_WITH_BC))) + self::reportConfigurationError('system', 'timezone'); + + date_default_timezone_set(self::getConfig('system', 'timezone')); + if(!is_string(self::getConfig('proxy', 'url'))) - die('Parameter [proxy] => "url" is not a valid string! Please check "config.ini.php"!'); + self::reportConfigurationError('proxy', 'url', 'Is not a valid string'); if(!empty(self::getConfig('proxy', 'url'))) { /** URL of the proxy server */ @@ -168,35 +168,38 @@ final class Configuration { } if(!is_bool(self::getConfig('proxy', 'by_bridge'))) - die('Parameter [proxy] => "by_bridge" is not a valid Boolean! Please check "config.ini.php"!'); + self::reportConfigurationError('proxy', 'by_bridge', 'Is not a valid Boolean'); /** True if proxy usage can be enabled selectively for each bridge */ define('PROXY_BYBRIDGE', self::getConfig('proxy', 'by_bridge')); if(!is_string(self::getConfig('proxy', 'name'))) - die('Parameter [proxy] => "name" is not a valid string! Please check "config.ini.php"!'); + self::reportConfigurationError('proxy', 'name', 'Is not a valid string'); /** Name of the proxy server */ define('PROXY_NAME', self::getConfig('proxy', 'name')); + if(!is_string(self::getConfig('cache', 'type'))) + self::reportConfigurationError('cache', 'type', 'Is not a valid string'); + if(!is_bool(self::getConfig('cache', 'custom_timeout'))) - die('Parameter [cache] => "custom_timeout" is not a valid Boolean! Please check "config.ini.php"!'); + self::reportConfigurationError('cache', 'custom_timeout', 'Is not a valid Boolean'); /** True if the cache timeout can be specified by the user */ define('CUSTOM_CACHE_TIMEOUT', self::getConfig('cache', 'custom_timeout')); if(!is_bool(self::getConfig('authentication', 'enable'))) - die('Parameter [authentication] => "enable" is not a valid Boolean! Please check "config.ini.php"!'); + self::reportConfigurationError('authentication', 'enable', 'Is not a valid Boolean'); if(!is_string(self::getConfig('authentication', 'username'))) - die('Parameter [authentication] => "username" is not a valid string! Please check "config.ini.php"!'); + self::reportConfigurationError('authentication', 'username', 'Is not a valid string'); if(!is_string(self::getConfig('authentication', 'password'))) - die('Parameter [authentication] => "password" is not a valid string! Please check "config.ini.php"!'); + self::reportConfigurationError('authentication', 'password', 'Is not a valid string'); if(!empty(self::getConfig('admin', 'email')) && !filter_var(self::getConfig('admin', 'email'), FILTER_VALIDATE_EMAIL)) - die('Parameter [admin] => "email" is not a valid email address! Please check "config.ini.php"!'); + self::reportConfigurationError('admin', 'email', 'Is not a valid email address'); } @@ -243,4 +246,46 @@ final class Configuration { return Configuration::$VERSION; } + + /** + * Reports an configuration error for the specified section and key to the + * user and ends execution + * + * @param string $section The section name + * @param string $key The configuration key + * @param string $message An optional message to the user + * + * @return void + */ + private static function reportConfigurationError($section, $key, $message = '') { + + $report = "Parameter [{$section}] => \"{$key}\" is invalid!" . PHP_EOL; + + if(file_exists(FILE_CONFIG)) { + $report .= 'Please check your configuration file at ' . FILE_CONFIG . PHP_EOL; + } elseif(!file_exists(FILE_CONFIG_DEFAULT)) { + $report .= 'The default configuration file is missing at ' . FILE_CONFIG_DEFAULT . PHP_EOL; + } else { + $report .= 'The default configuration file is broken.' . PHP_EOL + . 'Restore the original file from ' . REPOSITORY . PHP_EOL; + } + + $report .= $message; + self::reportError($report); + + } + + /** + * Reports an error message to the user and ends execution + * + * @param string $message The error message + * + * @return void + */ + private static function reportError($message) { + + header('Content-Type: text/plain', true, 500); + die('Configuration error' . PHP_EOL . $message); + + } } |