2018-06-27 18:09:41 +01:00
|
|
|
<?php
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2018-11-16 21:48:59 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
{
|
2024-02-02 18:22:10 +01:00
|
|
|
private const VERSION = '2024-02-02';
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2022-09-04 04:50:01 +02:00
|
|
|
private static $config = [];
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2022-08-06 22:46:28 +02:00
|
|
|
private function __construct()
|
2018-11-18 15:29:50 +01:00
|
|
|
{
|
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2023-12-13 21:40:13 +01:00
|
|
|
public static function checkInstallation(): array
|
2018-06-27 18:09:41 +01:00
|
|
|
{
|
2022-08-06 22:46:28 +02:00
|
|
|
$errors = [];
|
2022-07-05 16:39:44 +02:00
|
|
|
|
|
|
|
// OpenSSL: https://www.php.net/manual/en/book.openssl.php
|
2018-06-27 18:09:41 +01:00
|
|
|
if (!extension_loaded('openssl')) {
|
2022-08-06 22:46:28 +02:00
|
|
|
$errors[] = 'openssl extension not loaded';
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
2022-07-05 16:39:44 +02:00
|
|
|
// libxml: https://www.php.net/manual/en/book.libxml.php
|
2018-06-27 18:09:41 +01:00
|
|
|
if (!extension_loaded('libxml')) {
|
2022-08-06 22:46:28 +02:00
|
|
|
$errors[] = 'libxml extension not loaded';
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
2022-07-05 16:39:44 +02:00
|
|
|
// Multibyte String (mbstring): https://www.php.net/manual/en/book.mbstring.php
|
2018-06-27 18:09:41 +01:00
|
|
|
if (!extension_loaded('mbstring')) {
|
2022-08-06 22:46:28 +02:00
|
|
|
$errors[] = 'mbstring extension not loaded';
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
2022-07-05 16:39:44 +02:00
|
|
|
// SimpleXML: https://www.php.net/manual/en/book.simplexml.php
|
2018-06-27 18:09:41 +01:00
|
|
|
if (!extension_loaded('simplexml')) {
|
2022-08-06 22:46:28 +02:00
|
|
|
$errors[] = 'simplexml extension not loaded';
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
2022-07-05 16:39:44 +02:00
|
|
|
// Client URL Library (curl): https://www.php.net/manual/en/book.curl.php
|
2018-12-26 21:31:30 +00: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')))) {
|
2022-08-06 22:46:28 +02:00
|
|
|
$errors[] = 'curl extension not loaded';
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
2022-07-05 16:39:44 +02:00
|
|
|
// JavaScript Object Notation (json): https://www.php.net/manual/en/book.json.php
|
2018-08-22 16:21:20 +01:00
|
|
|
if (!extension_loaded('json')) {
|
2022-08-06 22:46:28 +02:00
|
|
|
$errors[] = 'json extension not loaded';
|
|
|
|
}
|
2023-12-13 21:40:13 +01:00
|
|
|
return $errors;
|
2018-06-27 18:09:41 +01:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2022-08-23 21:19:53 +02:00
|
|
|
public static function loadConfiguration(array $customConfig = [], array $env = [])
|
2018-06-27 18:09:41 +01:00
|
|
|
{
|
2022-08-23 21:19:53 +02:00
|
|
|
if (!file_exists(__DIR__ . '/../config.default.ini.php')) {
|
|
|
|
throw new \Exception('The default configuration file is missing');
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
2022-08-23 21:19:53 +02:00
|
|
|
$config = parse_ini_file(__DIR__ . '/../config.default.ini.php', true, INI_SCANNER_TYPED);
|
2022-08-02 15:03:54 +02:00
|
|
|
if (!$config) {
|
2024-01-09 20:33:35 +01:00
|
|
|
throw new \Exception('Error parsing ini config');
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
2022-08-23 21:19:53 +02:00
|
|
|
foreach ($config as $header => $section) {
|
|
|
|
foreach ($section as $key => $value) {
|
|
|
|
self::setConfig($header, $key, $value);
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
2018-06-27 18:09:41 +01:00
|
|
|
}
|
2022-08-23 21:19:53 +02:00
|
|
|
foreach ($customConfig as $header => $section) {
|
|
|
|
foreach ($section as $key => $value) {
|
|
|
|
self::setConfig($header, $key, $value);
|
|
|
|
}
|
|
|
|
}
|
2023-06-11 03:16:03 +02:00
|
|
|
|
|
|
|
if (file_exists(__DIR__ . '/../DEBUG')) {
|
|
|
|
// The debug mode has been moved to config. Preserve existing installs which has this DEBUG file.
|
|
|
|
self::setConfig('system', 'enable_debug_mode', true);
|
|
|
|
$debug = trim(file_get_contents(__DIR__ . '/../DEBUG'));
|
|
|
|
if ($debug) {
|
|
|
|
self::setConfig('system', 'debug_mode_whitelist', explode("\n", str_replace("\r", '', $debug)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists(__DIR__ . '/../whitelist.txt')) {
|
2023-07-07 11:25:36 +02:00
|
|
|
$enabledBridges = trim(file_get_contents(__DIR__ . '/../whitelist.txt'));
|
|
|
|
if ($enabledBridges === '*') {
|
2023-06-11 03:16:03 +02:00
|
|
|
self::setConfig('system', 'enabled_bridges', ['*']);
|
|
|
|
} else {
|
2023-07-30 23:26:59 +02:00
|
|
|
self::setConfig('system', 'enabled_bridges', array_filter(array_map('trim', explode("\n", $enabledBridges))));
|
2023-06-11 03:16:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 21:19:53 +02:00
|
|
|
foreach ($env as $envName => $envValue) {
|
|
|
|
$nameParts = explode('_', $envName);
|
|
|
|
if ($nameParts[0] === 'RSSBRIDGE') {
|
2022-09-12 23:14:11 +02:00
|
|
|
if (count($nameParts) < 3) {
|
|
|
|
// Invalid env name
|
|
|
|
continue;
|
|
|
|
}
|
2023-06-11 03:16:03 +02:00
|
|
|
|
|
|
|
// The variable is named $header but it's actually the section in config.ini.php
|
2022-08-23 21:19:53 +02:00
|
|
|
$header = $nameParts[1];
|
2023-06-11 03:16:03 +02:00
|
|
|
|
|
|
|
// Recombine the key if it had multiple underscores
|
|
|
|
$key = implode('_', array_slice($nameParts, 2));
|
2023-07-02 06:47:21 +02:00
|
|
|
$key = strtolower($key);
|
2023-06-11 03:16:03 +02:00
|
|
|
|
|
|
|
// Handle this specifically because it's an array
|
|
|
|
if ($key === 'enabled_bridges') {
|
|
|
|
$envValue = explode(',', $envValue);
|
|
|
|
$envValue = array_map('trim', $envValue);
|
|
|
|
}
|
|
|
|
|
2022-08-02 15:03:54 +02:00
|
|
|
if ($envValue === 'true' || $envValue === 'false') {
|
|
|
|
$envValue = filter_var($envValue, FILTER_VALIDATE_BOOLEAN);
|
2021-12-03 07:15:08 +01:00
|
|
|
}
|
2023-06-11 03:16:03 +02:00
|
|
|
|
2022-08-23 21:19:53 +02:00
|
|
|
self::setConfig($header, $key, $envValue);
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
2021-12-03 07:15:08 +01:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2023-06-11 03:16:03 +02:00
|
|
|
if (!is_array(self::getConfig('system', 'enabled_bridges'))) {
|
|
|
|
self::throwConfigError('system', 'enabled_bridges', 'Is not an array');
|
2023-06-02 20:22:09 +02:00
|
|
|
}
|
|
|
|
|
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))
|
2022-07-01 15:10:30 +02:00
|
|
|
) {
|
2022-08-23 21:19:53 +02:00
|
|
|
self::throwConfigError('system', 'timezone');
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
2023-06-02 20:22:09 +02:00
|
|
|
if (!is_bool(self::getConfig('system', 'enable_debug_mode'))) {
|
|
|
|
self::throwConfigError('system', 'enable_debug_mode', 'Is not a valid Boolean');
|
|
|
|
}
|
|
|
|
if (!is_array(self::getConfig('system', 'debug_mode_whitelist') ?: [])) {
|
|
|
|
self::throwConfigError('system', 'debug_mode_whitelist', 'Is not a valid array');
|
|
|
|
}
|
|
|
|
|
2018-06-27 18:09:41 +01:00
|
|
|
if (!is_string(self::getConfig('proxy', 'url'))) {
|
2022-08-23 21:19:53 +02:00
|
|
|
self::throwConfigError('proxy', 'url', 'Is not a valid string');
|
2018-11-18 15:52:28 +01:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2018-06-27 18:09:41 +01:00
|
|
|
if (!is_bool(self::getConfig('proxy', 'by_bridge'))) {
|
2022-08-23 21:19:53 +02:00
|
|
|
self::throwConfigError('proxy', 'by_bridge', 'Is not a valid Boolean');
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
2018-06-27 18:09:41 +01:00
|
|
|
if (!is_string(self::getConfig('proxy', 'name'))) {
|
2022-07-24 19:26:12 +02:00
|
|
|
/** Name of the proxy server */
|
2022-08-23 21:19:53 +02:00
|
|
|
self::throwConfigError('proxy', 'name', 'Is not a valid string');
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
2019-02-06 18:52:44 +01:00
|
|
|
if (!is_string(self::getConfig('cache', 'type'))) {
|
2022-08-23 21:19:53 +02:00
|
|
|
self::throwConfigError('cache', 'type', 'Is not a valid string');
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
2018-06-27 18:09:41 +01:00
|
|
|
if (!is_bool(self::getConfig('cache', 'custom_timeout'))) {
|
2022-08-23 21:19:53 +02:00
|
|
|
self::throwConfigError('cache', 'custom_timeout', 'Is not a valid Boolean');
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
2018-06-27 18:09:41 +01:00
|
|
|
if (!is_bool(self::getConfig('authentication', 'enable'))) {
|
2022-08-23 21:19:53 +02:00
|
|
|
self::throwConfigError('authentication', 'enable', 'Is not a valid Boolean');
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
2022-08-31 18:16:19 +02:00
|
|
|
if (!is_string(self::getConfig('authentication', 'username'))) {
|
2022-08-23 21:19:53 +02:00
|
|
|
self::throwConfigError('authentication', 'username', 'Is not a valid string');
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
2022-08-31 18:16:19 +02:00
|
|
|
if (!is_string(self::getConfig('authentication', 'password'))) {
|
2022-08-23 21:19:53 +02:00
|
|
|
self::throwConfigError('authentication', 'password', 'Is not a valid string');
|
2022-07-01 15:10:30 +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)
|
2022-07-01 15:10:30 +02:00
|
|
|
) {
|
2022-08-23 21:19:53 +02:00
|
|
|
self::throwConfigError('admin', 'email', 'Is not a valid email address');
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-25 21:45:25 +02:00
|
|
|
if (!is_bool(self::getConfig('admin', 'donations'))) {
|
2022-08-23 21:19:53 +02:00
|
|
|
self::throwConfigError('admin', 'donations', 'Is not a valid Boolean');
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
2019-10-31 18:40:51 +01:00
|
|
|
if (!is_string(self::getConfig('error', 'output'))) {
|
2022-08-23 21:19:53 +02:00
|
|
|
self::throwConfigError('error', 'output', 'Is not a valid String');
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
2023-09-10 21:50:15 +02:00
|
|
|
if (!in_array(self::getConfig('error', 'output'), ['feed', 'http', 'none'])) {
|
|
|
|
self::throwConfigError('error', 'output', 'Invalid output');
|
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2019-10-31 18:49:45 +01:00
|
|
|
if (
|
|
|
|
!is_numeric(self::getConfig('error', 'report_limit'))
|
|
|
|
|| self::getConfig('error', 'report_limit') < 1
|
|
|
|
) {
|
2022-08-23 21:19:53 +02:00
|
|
|
self::throwConfigError('admin', 'report_limit', 'Value is invalid');
|
2018-06-27 18:09:41 +01:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
2023-07-05 05:33:22 +02:00
|
|
|
public static function getConfig(string $section, string $key, $default = null)
|
2018-11-18 15:43:29 +01:00
|
|
|
{
|
2024-08-07 03:15:43 +02:00
|
|
|
if (self::$config === []) {
|
|
|
|
throw new \Exception('Config has not been loaded');
|
|
|
|
}
|
2023-07-05 05:33:22 +02:00
|
|
|
return self::$config[strtolower($section)][strtolower($key)] ?? $default;
|
2022-08-23 21:19:53 +02:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2022-08-23 21:19:53 +02:00
|
|
|
private static function setConfig(string $section, string $key, $value): void
|
|
|
|
{
|
|
|
|
self::$config[strtolower($section)][strtolower($key)] = $value;
|
2018-06-27 18:09:41 +01:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2018-06-30 09:24:22 +01:00
|
|
|
public static function getVersion()
|
|
|
|
{
|
2022-08-06 22:46:28 +02:00
|
|
|
$headFile = __DIR__ . '/../.git/HEAD';
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2018-10-27 01:53:45 -07:00
|
|
|
if (@is_readable($headFile)) {
|
2018-06-30 09:24:22 +01:00
|
|
|
$revisionHashFile = '.git/' . substr(file_get_contents($headFile), 5, -1);
|
2020-05-27 21:08:06 +00:00
|
|
|
$parts = explode('/', $revisionHashFile);
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2020-05-27 21:08:06 +00:00
|
|
|
if (isset($parts[3])) {
|
|
|
|
$branchName = $parts[3];
|
|
|
|
if (file_exists($revisionHashFile)) {
|
2022-09-04 07:21:57 +02:00
|
|
|
return sprintf('%s (git.%s.%s)', self::VERSION, $branchName, substr(file_get_contents($revisionHashFile), 0, 7));
|
2018-06-30 09:24:22 +01:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-23 21:19:53 +02:00
|
|
|
return self::VERSION;
|
2019-06-07 20:27:17 +02:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2022-08-23 21:19:53 +02:00
|
|
|
private static function throwConfigError($section, $key, $message = '')
|
2019-06-07 20:27:17 +02:00
|
|
|
{
|
2022-08-23 21:19:53 +02:00
|
|
|
throw new \Exception("Config [$section] => [$key] is invalid. $message");
|
2019-06-07 20:27:17 +02:00
|
|
|
}
|
2018-06-27 18:09:41 +01:00
|
|
|
}
|