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.
|
|
|
|
*/
|
2018-12-11 17:07:41 +01:00
|
|
|
public static $VERSION = 'dev.2018-12-11';
|
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)
|
|
|
|
die('RSS-Bridge requires at least PHP version 5.6.0!');
|
2018-06-27 19:09:41 +02:00
|
|
|
|
|
|
|
// extensions check
|
|
|
|
if(!extension_loaded('openssl'))
|
|
|
|
die('"openssl" extension not loaded. Please check "php.ini"');
|
|
|
|
|
|
|
|
if(!extension_loaded('libxml'))
|
|
|
|
die('"libxml" extension not loaded. Please check "php.ini"');
|
|
|
|
|
|
|
|
if(!extension_loaded('mbstring'))
|
|
|
|
die('"mbstring" extension not loaded. Please check "php.ini"');
|
|
|
|
|
|
|
|
if(!extension_loaded('simplexml'))
|
|
|
|
die('"simplexml" extension not loaded. Please check "php.ini"');
|
|
|
|
|
|
|
|
if(!extension_loaded('curl'))
|
|
|
|
die('"curl" extension not loaded. Please check "php.ini"');
|
|
|
|
|
2018-08-22 17:21:20 +02:00
|
|
|
if(!extension_loaded('json'))
|
|
|
|
die('"json" extension not loaded. Please check "php.ini"');
|
|
|
|
|
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))
|
|
|
|
die('RSS-Bridge does not have write permissions for ' . PATH_CACHE . '!');
|
2018-06-27 19:09:41 +02:00
|
|
|
|
2018-11-10 19:51:37 +01:00
|
|
|
// Check whitelist file permissions
|
|
|
|
if(!file_exists(WHITELIST) && !is_writable(dirname(WHITELIST)))
|
|
|
|
die('RSS-Bridge does not have write permissions for ' . WHITELIST . '!');
|
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:
|
|
|
|
* - `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.
|
|
|
|
*
|
2018-11-18 15:41:28 +01:00
|
|
|
* The files must be located at {@see PATH_ROOT}
|
|
|
|
*
|
2018-11-16 21:48:59 +01:00
|
|
|
* 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
|
|
|
|
* 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() {
|
|
|
|
|
2018-11-18 15:41:28 +01:00
|
|
|
if(!file_exists(PATH_ROOT . 'config.default.ini.php'))
|
2018-06-27 19:09:41 +02:00
|
|
|
die('The default configuration file "config.default.ini.php" is missing!');
|
|
|
|
|
2018-11-18 15:41:28 +01:00
|
|
|
Configuration::$config = parse_ini_file(PATH_ROOT . 'config.default.ini.php', true, INI_SCANNER_TYPED);
|
2018-06-27 19:09:41 +02:00
|
|
|
if(!Configuration::$config)
|
|
|
|
die('Error parsing config.default.ini.php');
|
|
|
|
|
2018-11-18 15:41:28 +01:00
|
|
|
if(file_exists(PATH_ROOT . 'config.ini.php')) {
|
2018-06-27 19:09:41 +02:00
|
|
|
// Replace default configuration with custom settings
|
2018-11-18 15:41:28 +01:00
|
|
|
foreach(parse_ini_file(PATH_ROOT . 'config.ini.php', 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!is_string(self::getConfig('proxy', 'url')))
|
|
|
|
die('Parameter [proxy] => "url" is not a valid string! Please check "config.ini.php"!');
|
|
|
|
|
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')))
|
|
|
|
die('Parameter [proxy] => "by_bridge" is not a valid Boolean! Please check "config.ini.php"!');
|
|
|
|
|
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')))
|
|
|
|
die('Parameter [proxy] => "name" is not a valid string! Please check "config.ini.php"!');
|
|
|
|
|
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'));
|
|
|
|
|
|
|
|
if(!is_bool(self::getConfig('cache', 'custom_timeout')))
|
|
|
|
die('Parameter [cache] => "custom_timeout" is not a valid Boolean! Please check "config.ini.php"!');
|
|
|
|
|
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')))
|
|
|
|
die('Parameter [authentication] => "enable" is not a valid Boolean! Please check "config.ini.php"!');
|
|
|
|
|
|
|
|
if(!is_string(self::getConfig('authentication', 'username')))
|
|
|
|
die('Parameter [authentication] => "username" is not a valid string! Please check "config.ini.php"!');
|
|
|
|
|
|
|
|
if(!is_string(self::getConfig('authentication', 'password')))
|
|
|
|
die('Parameter [authentication] => "password" is not a valid string! Please check "config.ini.php"!');
|
|
|
|
|
2018-11-10 18:42:36 +01:00
|
|
|
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"!');
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-27 19:09:41 +02:00
|
|
|
}
|