$section) { foreach ($section as $key => $value) { $config[$header][$key] = $value; } } } foreach (getenv() as $envName => $envValue) { // Replace all settings with their respective environment variable if available $keyArray = explode('_', $envName); if ($keyArray[0] === 'RSSBRIDGE') { $header = strtolower($keyArray[1]); $key = strtolower($keyArray[2]); if ($envValue === 'true' || $envValue === 'false') { $envValue = filter_var($envValue, FILTER_VALIDATE_BOOLEAN); } $config[$header][$key] = $envValue; } } self::$config = $config; if ( !is_string(self::getConfig('system', 'timezone')) || !in_array(self::getConfig('system', 'timezone'), timezone_identifiers_list(DateTimeZone::ALL_WITH_BC)) ) { self::reportConfigurationError('system', 'timezone'); } if (!is_string(self::getConfig('proxy', 'url'))) { self::reportConfigurationError('proxy', 'url', 'Is not a valid string'); } if (!is_bool(self::getConfig('proxy', 'by_bridge'))) { self::reportConfigurationError('proxy', 'by_bridge', 'Is not a valid Boolean'); } if (!is_string(self::getConfig('proxy', 'name'))) { /** Name of the proxy server */ self::reportConfigurationError('proxy', 'name', 'Is not a valid string'); } if (!is_string(self::getConfig('cache', 'type'))) { self::reportConfigurationError('cache', 'type', 'Is not a valid string'); } if (!is_bool(self::getConfig('cache', 'custom_timeout'))) { self::reportConfigurationError('cache', 'custom_timeout', 'Is not a valid Boolean'); } if (!is_bool(self::getConfig('authentication', 'enable'))) { self::reportConfigurationError('authentication', 'enable', 'Is not a valid Boolean'); } if (!self::getConfig('authentication', 'username')) { self::reportConfigurationError('authentication', 'username', 'Is not a valid string'); } if (! self::getConfig('authentication', 'password')) { self::reportConfigurationError('authentication', 'password', 'Is not a valid string'); } if ( !empty(self::getConfig('admin', 'email')) && !filter_var(self::getConfig('admin', 'email'), FILTER_VALIDATE_EMAIL) ) { self::reportConfigurationError('admin', 'email', 'Is not a valid email address'); } if (!is_bool(self::getConfig('admin', 'donations'))) { self::reportConfigurationError('admin', 'donations', 'Is not a valid Boolean'); } if (!is_string(self::getConfig('error', 'output'))) { self::reportConfigurationError('error', 'output', 'Is not a valid String'); } if ( !is_numeric(self::getConfig('error', 'report_limit')) || self::getConfig('error', 'report_limit') < 1 ) { self::reportConfigurationError('admin', 'report_limit', 'Value is invalid'); } } /** * Returns the value of a parameter identified by section and key. * * @param string $section The section name. * @param string $key The property name (key). * @return mixed|null The parameter value. */ public static function getConfig($section, $key) { if (array_key_exists($section, self::$config) && array_key_exists($key, self::$config[$section])) { return self::$config[$section][$key]; } return null; } /** * Returns the current version string of RSS-Bridge. * * This function returns the contents of {@see Configuration::$VERSION} for * regular installations and the git branch name and commit id for instances * running in a git environment. * * @return string The version string. */ public static function getVersion() { $headFile = __DIR__ . '/../.git/HEAD'; // '@' is used to mute open_basedir warning if (@is_readable($headFile)) { $revisionHashFile = '.git/' . substr(file_get_contents($headFile), 5, -1); $parts = explode('/', $revisionHashFile); if (isset($parts[3])) { $branchName = $parts[3]; if (file_exists($revisionHashFile)) { return 'git.' . $branchName . '.' . substr(file_get_contents($revisionHashFile), 0, 7); } } } 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); } private static function reportError($message) { throw new \Exception(sprintf('Configuration error: %s', $message)); } }