aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/BridgeFactory.php92
-rw-r--r--lib/Configuration.php42
-rw-r--r--lib/bootstrap.php6
3 files changed, 64 insertions, 76 deletions
diff --git a/lib/BridgeFactory.php b/lib/BridgeFactory.php
index 6bd832bf..db2c394a 100644
--- a/lib/BridgeFactory.php
+++ b/lib/BridgeFactory.php
@@ -2,101 +2,69 @@
final class BridgeFactory
{
- /** @var array<class-string<BridgeInterface>> */
private $bridgeClassNames = [];
-
- /** @var array<class-string<BridgeInterface>> */
- private $whitelist = [];
+ private $enabledBridges = [];
public function __construct()
{
- // create names
+ // Create all possible bridge class names from fs
foreach (scandir(__DIR__ . '/../bridges/') as $file) {
if (preg_match('/^([^.]+Bridge)\.php$/U', $file, $m)) {
$this->bridgeClassNames[] = $m[1];
}
}
- // create whitelist
- if (file_exists(WHITELIST)) {
- $contents = trim(file_get_contents(WHITELIST));
- } elseif (file_exists(WHITELIST_DEFAULT)) {
- $contents = trim(file_get_contents(WHITELIST_DEFAULT));
- } else {
- $contents = '';
+ $enabledBridges = Configuration::getConfig('system', 'enabled_bridges');
+ if ($enabledBridges === null) {
+ throw new \Exception('No bridges are enabled... wtf?');
}
-
- if ($contents === '*') {
- // Whitelist all bridges
- $this->whitelist = $this->getBridgeClassNames();
- } else {
- foreach (explode("\n", $contents) as $bridgeName) {
- $bridgeClassName = $this->sanitizeBridgeName($bridgeName);
- if ($bridgeClassName !== null) {
- $this->whitelist[] = $bridgeClassName;
- }
+ foreach ($enabledBridges as $enabledBridge) {
+ if ($enabledBridge === '*') {
+ $this->enabledBridges = $this->bridgeClassNames;
+ break;
}
+ $this->enabledBridges[] = $this->createBridgeClassName($enabledBridge);
}
}
- /**
- * @param class-string<BridgeInterface> $name
- */
public function create(string $name): BridgeInterface
{
return new $name();
}
- /**
- * @return array<class-string<BridgeInterface>>
- */
- public function getBridgeClassNames(): array
+ public function isEnabled(string $bridgeName): bool
{
- return $this->bridgeClassNames;
+ return in_array($bridgeName, $this->enabledBridges);
}
- /**
- * @param class-string<BridgeInterface>|null $name
- */
- public function isWhitelisted(string $name): bool
+ public function createBridgeClassName(string $bridgeName): ?string
{
- return in_array($name, $this->whitelist);
- }
+ $name = self::normalizeBridgeName($bridgeName);
+ $namesLoweredCase = array_map('strtolower', $this->bridgeClassNames);
+ $nameLoweredCase = strtolower($name);
- /**
- * Tries to turn a potentially human produced bridge name into a class name.
- *
- * @param mixed $name
- * @return class-string<BridgeInterface>|null
- */
- public function sanitizeBridgeName($name): ?string
- {
- if (!is_string($name)) {
- return null;
+ if (! in_array($nameLoweredCase, $namesLoweredCase)) {
+ throw new \Exception(sprintf('Bridge name invalid: %s', $bridgeName));
}
- // Trim trailing '.php' if exists
+ $index = array_search($nameLoweredCase, $namesLoweredCase);
+
+ return $this->bridgeClassNames[$index];
+ }
+
+ public static function normalizeBridgeName(string $name)
+ {
if (preg_match('/(.+)(?:\.php)/', $name, $matches)) {
$name = $matches[1];
}
-
- // Append 'Bridge' suffix if not present.
if (!preg_match('/(Bridge)$/i', $name)) {
$name = sprintf('%sBridge', $name);
}
+ return $name;
+ }
- // Improve performance for correctly written bridge names
- if (in_array($name, $this->getBridgeClassNames())) {
- $index = array_search($name, $this->getBridgeClassNames());
- return $this->getBridgeClassNames()[$index];
- }
-
- // The name is valid if a corresponding bridge file is found on disk
- if (in_array(strtolower($name), array_map('strtolower', $this->getBridgeClassNames()))) {
- $index = array_search(strtolower($name), array_map('strtolower', $this->getBridgeClassNames()));
- return $this->getBridgeClassNames()[$index];
- }
-
- return null;
+ public function getBridgeClassNames(): array
+ {
+ return $this->bridgeClassNames;
}
}
diff --git a/lib/Configuration.php b/lib/Configuration.php
index 6eec33f6..2f677ae5 100644
--- a/lib/Configuration.php
+++ b/lib/Configuration.php
@@ -98,6 +98,25 @@ final class Configuration
self::setConfig($header, $key, $value);
}
}
+
+ 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')) {
+ $whitelist = trim(file_get_contents(__DIR__ . '/../whitelist.txt'));
+ if ($whitelist === '*') {
+ self::setConfig('system', 'enabled_bridges', ['*']);
+ } else {
+ self::setConfig('system', 'enabled_bridges', explode("\n", $whitelist));
+ }
+ }
+
foreach ($env as $envName => $envValue) {
$nameParts = explode('_', $envName);
if ($nameParts[0] === 'RSSBRIDGE') {
@@ -105,22 +124,29 @@ final class Configuration
// Invalid env name
continue;
}
+
+ // The variable is named $header but it's actually the section in config.ini.php
$header = $nameParts[1];
- $key = $nameParts[2];
+
+ // Recombine the key if it had multiple underscores
+ $key = implode('_', array_slice($nameParts, 2));
+
+ // Handle this specifically because it's an array
+ if ($key === 'enabled_bridges') {
+ $envValue = explode(',', $envValue);
+ $envValue = array_map('trim', $envValue);
+ }
+
if ($envValue === 'true' || $envValue === 'false') {
$envValue = filter_var($envValue, FILTER_VALIDATE_BOOLEAN);
}
+
self::setConfig($header, $key, $envValue);
}
}
- 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 (!is_array(self::getConfig('system', 'enabled_bridges'))) {
+ self::throwConfigError('system', 'enabled_bridges', 'Is not an array');
}
if (
diff --git a/lib/bootstrap.php b/lib/bootstrap.php
index 86182801..e05dd94a 100644
--- a/lib/bootstrap.php
+++ b/lib/bootstrap.php
@@ -29,12 +29,6 @@ const PATH_LIB_ACTIONS = __DIR__ . '/../actions/';
/** Path to the cache folder */
const PATH_CACHE = __DIR__ . '/../cache/';
-/** Path to the whitelist file */
-const WHITELIST = __DIR__ . '/../whitelist.txt';
-
-/** Path to the default whitelist file */
-const WHITELIST_DEFAULT = __DIR__ . '/../whitelist.default.txt';
-
/** URL to the RSS-Bridge repository */
const REPOSITORY = 'https://github.com/RSS-Bridge/rss-bridge/';