2018-06-27 19:09:41 +02:00
|
|
|
<?php
|
2018-11-16 21:48:59 +01:00
|
|
|
/**
|
|
|
|
* This file is part of RSS-Bridge, a PHP project capable of generating RSS and
|
|
|
|
* Atom feeds for websites that don't have one.
|
|
|
|
*
|
|
|
|
* For the full license information, please view the UNLICENSE file distributed
|
|
|
|
* with this source code.
|
|
|
|
*
|
|
|
|
* @package Core
|
|
|
|
* @license http://unlicense.org/ UNLICENSE
|
|
|
|
* @link https://github.com/rss-bridge/rss-bridge
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configuration module for RSS-Bridge.
|
|
|
|
*
|
|
|
|
* This class implements a configuration module for RSS-Bridge.
|
|
|
|
*/
|
2018-11-18 15:33:02 +01:00
|
|
|
final class Configuration {
|
2018-06-27 19:09:41 +02:00
|
|
|
|
2018-11-16 21:48:59 +01:00
|
|
|
/**
|
|
|
|
* Holds the current release version of RSS-Bridge.
|
|
|
|
*
|
|
|
|
* Do not access this property directly!
|
|
|
|
* Use {@see Configuration::getVersion()} instead.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*
|
|
|
|
* @todo Replace this property by a constant.
|
|
|
|
*/
|
2019-06-08 20:04:57 +02:00
|
|
|
public static $VERSION = '2019-06-08';
|
2018-06-30 10:24:22 +02:00
|
|
|
|
2018-11-16 21:48:59 +01:00
|
|
|
/**
|
|
|
|
* Holds the configuration data.
|
|
|
|
*
|
|
|
|
* Do not access this property directly!
|
|
|
|
* Use {@see Configuration::getConfig()} instead.
|
|
|
|
*
|
|
|
|
* @var array|null
|
|
|
|
*/
|
2018-11-18 15:58:32 +01:00
|
|
|
private static $config = null;
|
2018-06-27 19:09:41 +02:00
|
|
|
|
2018-11-18 15:29:50 +01:00
|
|
|
/**
|
|
|
|
* Throw an exception when trying to create a new instance of this class.
|
|
|
|
*
|
|
|
|
* @throws \LogicException if called.
|
|
|
|
*/
|
|
|
|
public function __construct(){
|
|
|
|
throw new \LogicException('Can\'t create object of this class!');
|
|
|
|
}
|
|
|
|
|
2018-11-16 21:48:59 +01:00
|
|
|
/**
|
|
|
|
* Verifies the current installation of RSS-Bridge and PHP.
|
|
|
|
*
|
|
|
|
* Returns an error message and aborts execution if the installation does
|
|
|
|
* not satisfy the requirements of RSS-Bridge.
|
|
|
|
*
|
|
|
|
* **Requirements**
|
|
|
|
* - PHP 5.6.0 or higher
|
|
|
|
* - `openssl` extension
|
|
|
|
* - `libxml` extension
|
|
|
|
* - `mbstring` extension
|
|
|
|
* - `simplexml` extension
|
|
|
|
* - `curl` extension
|
|
|
|
* - `json` extension
|
|
|
|
* - The cache folder specified by {@see PATH_CACHE} requires write permission
|
|
|
|
* - The whitelist file specified by {@see WHITELIST} requires write permission
|
|
|
|
*
|
|
|
|
* @link http://php.net/supported-versions.php PHP Supported Versions
|
|
|
|
* @link http://php.net/manual/en/book.openssl.php OpenSSL
|
|
|
|
* @link http://php.net/manual/en/book.libxml.php libxml
|
|
|
|
* @link http://php.net/manual/en/book.mbstring.php Multibyte String (mbstring)
|
|
|
|
* @link http://php.net/manual/en/book.simplexml.php SimpleXML
|
|
|
|
* @link http://php.net/manual/en/book.curl.php Client URL Library (curl)
|
|
|
|
* @link http://php.net/manual/en/book.json.php JavaScript Object Notation (json)
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-06-27 19:09:41 +02:00
|
|
|
public static function verifyInstallation() {
|
|
|
|
|
|
|
|
// Check PHP version
|
2018-11-05 19:07:33 +01:00
|
|
|
if(version_compare(PHP_VERSION, '5.6.0') === -1)
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportError('RSS-Bridge requires at least PHP version 5.6.0!');
|
2018-06-27 19:09:41 +02:00
|
|
|
|
|
|
|
// extensions check
|
|
|
|
if(!extension_loaded('openssl'))
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportError('"openssl" extension not loaded. Please check "php.ini"');
|
2018-06-27 19:09:41 +02:00
|
|
|
|
|
|
|
if(!extension_loaded('libxml'))
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportError('"libxml" extension not loaded. Please check "php.ini"');
|
2018-06-27 19:09:41 +02:00
|
|
|
|
|
|
|
if(!extension_loaded('mbstring'))
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportError('"mbstring" extension not loaded. Please check "php.ini"');
|
2018-06-27 19:09:41 +02:00
|
|
|
|
|
|
|
if(!extension_loaded('simplexml'))
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportError('"simplexml" extension not loaded. Please check "php.ini"');
|
2018-06-27 19:09:41 +02:00
|
|
|
|
2018-12-26 22:31:30 +01:00
|
|
|
// 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'))))
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportError('"curl" extension not loaded. Please check "php.ini"');
|
2018-06-27 19:09:41 +02:00
|
|
|
|
2018-08-22 17:21:20 +02:00
|
|
|
if(!extension_loaded('json'))
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportError('"json" extension not loaded. Please check "php.ini"');
|
2018-08-22 17:21:20 +02:00
|
|
|
|
2018-06-27 19:09:41 +02:00
|
|
|
// Check cache folder permissions (write permissions required)
|
2018-11-06 18:35:40 +01:00
|
|
|
if(!is_writable(PATH_CACHE))
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportError('RSS-Bridge does not have write permissions for ' . PATH_CACHE . '!');
|
2018-06-27 19:09:41 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-16 21:48:59 +01:00
|
|
|
/**
|
|
|
|
* Loads the configuration from disk and checks if the parameters are valid.
|
|
|
|
*
|
|
|
|
* Returns an error message and aborts execution if the configuration is invalid.
|
|
|
|
*
|
|
|
|
* The RSS-Bridge configuration is split into two files:
|
2019-06-07 19:45:47 +02:00
|
|
|
* - {@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.
|
2018-11-16 21:48:59 +01:00
|
|
|
*
|
2019-06-07 19:45:47 +02:00
|
|
|
* 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
|
2018-11-16 21:48:59 +01:00
|
|
|
* 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).
|
|
|
|
*
|
|
|
|
* The configuration files must be placed in the root folder of RSS-Bridge
|
|
|
|
* (next to `index.php`).
|
|
|
|
*
|
|
|
|
* _Notice_: The configuration is stored in {@see Configuration::$config}.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-06-27 19:09:41 +02:00
|
|
|
public static function loadConfiguration() {
|
|
|
|
|
2019-06-07 19:45:47 +02:00
|
|
|
if(!file_exists(FILE_CONFIG_DEFAULT))
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportError('The default configuration file is missing at ' . FILE_CONFIG_DEFAULT);
|
2018-06-27 19:09:41 +02:00
|
|
|
|
2019-06-07 19:45:47 +02:00
|
|
|
Configuration::$config = parse_ini_file(FILE_CONFIG_DEFAULT, true, INI_SCANNER_TYPED);
|
2018-06-27 19:09:41 +02:00
|
|
|
if(!Configuration::$config)
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportError('Error parsing ' . FILE_CONFIG_DEFAULT);
|
2018-06-27 19:09:41 +02:00
|
|
|
|
2019-06-07 19:45:47 +02:00
|
|
|
if(file_exists(FILE_CONFIG)) {
|
2018-06-27 19:09:41 +02:00
|
|
|
// Replace default configuration with custom settings
|
2019-06-07 19:45:47 +02:00
|
|
|
foreach(parse_ini_file(FILE_CONFIG, true, INI_SCANNER_TYPED) as $header => $section) {
|
2018-06-27 19:09:41 +02:00
|
|
|
foreach($section as $key => $value) {
|
|
|
|
// Skip unknown sections and keys
|
|
|
|
if(array_key_exists($header, Configuration::$config) && array_key_exists($key, Configuration::$config[$header])) {
|
|
|
|
Configuration::$config[$header][$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 19:19:36 +02:00
|
|
|
if(!is_string(self::getConfig('system', 'timezone'))
|
|
|
|
|| !in_array(self::getConfig('system', 'timezone'), timezone_identifiers_list(DateTimeZone::ALL_WITH_BC)))
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportConfigurationError('system', 'timezone');
|
2019-06-07 19:19:36 +02:00
|
|
|
|
|
|
|
date_default_timezone_set(self::getConfig('system', 'timezone'));
|
|
|
|
|
2018-06-27 19:09:41 +02:00
|
|
|
if(!is_string(self::getConfig('proxy', 'url')))
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportConfigurationError('proxy', 'url', 'Is not a valid string');
|
2018-06-27 19:09:41 +02:00
|
|
|
|
2018-11-18 15:52:28 +01:00
|
|
|
if(!empty(self::getConfig('proxy', 'url'))) {
|
|
|
|
/** URL of the proxy server */
|
2018-06-27 19:09:41 +02:00
|
|
|
define('PROXY_URL', self::getConfig('proxy', 'url'));
|
2018-11-18 15:52:28 +01:00
|
|
|
}
|
2018-06-27 19:09:41 +02:00
|
|
|
|
|
|
|
if(!is_bool(self::getConfig('proxy', 'by_bridge')))
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportConfigurationError('proxy', 'by_bridge', 'Is not a valid Boolean');
|
2018-06-27 19:09:41 +02:00
|
|
|
|
2018-11-18 15:52:28 +01:00
|
|
|
/** True if proxy usage can be enabled selectively for each bridge */
|
2018-06-27 19:09:41 +02:00
|
|
|
define('PROXY_BYBRIDGE', self::getConfig('proxy', 'by_bridge'));
|
|
|
|
|
|
|
|
if(!is_string(self::getConfig('proxy', 'name')))
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportConfigurationError('proxy', 'name', 'Is not a valid string');
|
2018-06-27 19:09:41 +02:00
|
|
|
|
2018-11-18 15:52:28 +01:00
|
|
|
/** Name of the proxy server */
|
2018-06-27 19:09:41 +02:00
|
|
|
define('PROXY_NAME', self::getConfig('proxy', 'name'));
|
|
|
|
|
2019-02-06 18:52:44 +01:00
|
|
|
if(!is_string(self::getConfig('cache', 'type')))
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportConfigurationError('cache', 'type', 'Is not a valid string');
|
2019-02-06 18:52:44 +01:00
|
|
|
|
2018-06-27 19:09:41 +02:00
|
|
|
if(!is_bool(self::getConfig('cache', 'custom_timeout')))
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportConfigurationError('cache', 'custom_timeout', 'Is not a valid Boolean');
|
2018-06-27 19:09:41 +02:00
|
|
|
|
2018-11-18 15:52:28 +01:00
|
|
|
/** True if the cache timeout can be specified by the user */
|
2018-06-27 19:09:41 +02:00
|
|
|
define('CUSTOM_CACHE_TIMEOUT', self::getConfig('cache', 'custom_timeout'));
|
|
|
|
|
|
|
|
if(!is_bool(self::getConfig('authentication', 'enable')))
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportConfigurationError('authentication', 'enable', 'Is not a valid Boolean');
|
2018-06-27 19:09:41 +02:00
|
|
|
|
|
|
|
if(!is_string(self::getConfig('authentication', 'username')))
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportConfigurationError('authentication', 'username', 'Is not a valid string');
|
2018-06-27 19:09:41 +02:00
|
|
|
|
|
|
|
if(!is_string(self::getConfig('authentication', 'password')))
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportConfigurationError('authentication', 'password', 'Is not a valid string');
|
2018-06-27 19:09:41 +02:00
|
|
|
|
2018-11-10 18:42:36 +01:00
|
|
|
if(!empty(self::getConfig('admin', 'email'))
|
|
|
|
&& !filter_var(self::getConfig('admin', 'email'), FILTER_VALIDATE_EMAIL))
|
2019-06-07 20:27:17 +02:00
|
|
|
self::reportConfigurationError('admin', 'email', 'Is not a valid email address');
|
2018-11-10 18:42:36 +01:00
|
|
|
|
2018-06-27 19:09:41 +02:00
|
|
|
}
|
|
|
|
|
2018-11-16 21:48:59 +01:00
|
|
|
/**
|
2018-11-18 15:43:29 +01:00
|
|
|
* Returns the value of a parameter identified by section and key.
|
2018-11-16 21:48:59 +01:00
|
|
|
*
|
2018-11-18 15:43:29 +01:00
|
|
|
* @param string $section The section name.
|
2018-11-16 21:48:59 +01:00
|
|
|
* @param string $key The property name (key).
|
|
|
|
* @return mixed|null The parameter value.
|
|
|
|
*/
|
2018-11-18 15:43:29 +01:00
|
|
|
public static function getConfig($section, $key) {
|
2018-06-27 19:09:41 +02:00
|
|
|
|
2018-11-18 15:43:29 +01:00
|
|
|
if(array_key_exists($section, self::$config) && array_key_exists($key, self::$config[$section])) {
|
|
|
|
return self::$config[$section][$key];
|
2018-06-27 19:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-16 21:48:59 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-06-30 10:24:22 +02:00
|
|
|
public static function getVersion() {
|
|
|
|
|
2018-11-18 15:41:28 +01:00
|
|
|
$headFile = PATH_ROOT . '.git/HEAD';
|
2018-06-30 10:24:22 +02:00
|
|
|
|
2018-10-27 10:53:45 +02:00
|
|
|
// '@' is used to mute open_basedir warning
|
|
|
|
if(@is_readable($headFile)) {
|
2018-06-30 10:24:22 +02:00
|
|
|
|
|
|
|
$revisionHashFile = '.git/' . substr(file_get_contents($headFile), 5, -1);
|
|
|
|
$branchName = explode('/', $revisionHashFile)[3];
|
|
|
|
if(file_exists($revisionHashFile)) {
|
|
|
|
return 'git.' . $branchName . '.' . substr(file_get_contents($revisionHashFile), 0, 7);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Configuration::$VERSION;
|
|
|
|
|
|
|
|
}
|
2019-06-07 20:27:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
}
|
2018-06-27 19:09:41 +02:00
|
|
|
}
|