mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-09-03 05:02:43 +02:00
refactor(BridgeFactory): make methods only accept valid class names (#2897)
This moves the responsibility for getting a valid class name to the users of BridgeFactory, avoiding the repeated sanitation. Improper use can also be checked statically.
This commit is contained in:
@@ -26,6 +26,13 @@ class ConnectivityAction implements ActionInterface
|
||||
{
|
||||
public $userData = [];
|
||||
|
||||
private BridgeFactory $bridgeFactory;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->bridgeFactory = new \BridgeFactory();
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
if (!Debug::isEnabled()) {
|
||||
@@ -39,7 +46,13 @@ class ConnectivityAction implements ActionInterface
|
||||
|
||||
$bridgeName = $this->userData['bridge'];
|
||||
|
||||
$this->reportBridgeConnectivity($bridgeName);
|
||||
$bridgeClassName = $this->bridgeFactory->sanitizeBridgeName($bridgeName);
|
||||
|
||||
if ($bridgeClassName === null) {
|
||||
throw new \InvalidArgumentException('Bridge name invalid!');
|
||||
}
|
||||
|
||||
$this->reportBridgeConnectivity($bridgeClassName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,14 +65,12 @@ class ConnectivityAction implements ActionInterface
|
||||
* "successful": true/false
|
||||
* }
|
||||
*
|
||||
* @param string $bridgeName Name of the bridge to generate the report for
|
||||
* @param class-string<BridgeInterface> $bridgeClassName Name of the bridge to generate the report for
|
||||
* @return void
|
||||
*/
|
||||
private function reportBridgeConnectivity($bridgeName)
|
||||
private function reportBridgeConnectivity($bridgeClassName)
|
||||
{
|
||||
$bridgeFactory = new \BridgeFactory();
|
||||
|
||||
if (!$bridgeFactory->isWhitelisted($bridgeName)) {
|
||||
if (!$this->bridgeFactory->isWhitelisted($bridgeClassName)) {
|
||||
header('Content-Type: text/html');
|
||||
returnServerError('Bridge is not whitelisted!');
|
||||
}
|
||||
@@ -67,12 +78,12 @@ class ConnectivityAction implements ActionInterface
|
||||
header('Content-Type: text/json');
|
||||
|
||||
$retVal = [
|
||||
'bridge' => $bridgeName,
|
||||
'bridge' => $bridgeClassName,
|
||||
'successful' => false,
|
||||
'http_code' => 200,
|
||||
];
|
||||
|
||||
$bridge = $bridgeFactory->create($bridgeName);
|
||||
$bridge = $this->bridgeFactory->create($bridgeClassName);
|
||||
|
||||
if ($bridge === false) {
|
||||
echo json_encode($retVal);
|
||||
|
@@ -26,12 +26,12 @@ class DetectAction implements ActionInterface
|
||||
|
||||
$bridgeFactory = new \BridgeFactory();
|
||||
|
||||
foreach ($bridgeFactory->getBridgeNames() as $bridgeName) {
|
||||
if (!$bridgeFactory->isWhitelisted($bridgeName)) {
|
||||
foreach ($bridgeFactory->getBridgeClassNames() as $bridgeClassName) {
|
||||
if (!$bridgeFactory->isWhitelisted($bridgeClassName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$bridge = $bridgeFactory->create($bridgeName);
|
||||
$bridge = $bridgeFactory->create($bridgeClassName);
|
||||
|
||||
if ($bridge === false) {
|
||||
continue;
|
||||
@@ -43,7 +43,7 @@ class DetectAction implements ActionInterface
|
||||
continue;
|
||||
}
|
||||
|
||||
$bridgeParams['bridge'] = $bridgeName;
|
||||
$bridgeParams['bridge'] = $bridgeClassName;
|
||||
$bridgeParams['format'] = $format;
|
||||
|
||||
header('Location: ?action=display&' . http_build_query($bridgeParams), true, 301);
|
||||
|
@@ -28,21 +28,25 @@ class DisplayAction implements ActionInterface
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$bridge = array_key_exists('bridge', $this->userData) ? $this->userData['bridge'] : null;
|
||||
$bridgeFactory = new \BridgeFactory();
|
||||
|
||||
$bridgeClassName = isset($this->userData['bridge']) ? $bridgeFactory->sanitizeBridgeName($this->userData['bridge']) : null;
|
||||
|
||||
if ($bridgeClassName === null) {
|
||||
throw new \InvalidArgumentException('Bridge name invalid!');
|
||||
}
|
||||
|
||||
$format = $this->userData['format']
|
||||
or returnClientError('You must specify a format!');
|
||||
|
||||
$bridgeFactory = new \BridgeFactory();
|
||||
|
||||
// whitelist control
|
||||
if (!$bridgeFactory->isWhitelisted($bridge)) {
|
||||
if (!$bridgeFactory->isWhitelisted($bridgeClassName)) {
|
||||
throw new \Exception('This bridge is not whitelisted', 401);
|
||||
die;
|
||||
}
|
||||
|
||||
// Data retrieval
|
||||
$bridge = $bridgeFactory->create($bridge);
|
||||
$bridge = $bridgeFactory->create($bridgeClassName);
|
||||
$bridge->loadConfiguration();
|
||||
|
||||
$noproxy = array_key_exists('_noproxy', $this->userData)
|
||||
|
@@ -22,20 +22,20 @@ class ListAction implements ActionInterface
|
||||
|
||||
$bridgeFactory = new \BridgeFactory();
|
||||
|
||||
foreach ($bridgeFactory->getBridgeNames() as $bridgeName) {
|
||||
$bridge = $bridgeFactory->create($bridgeName);
|
||||
foreach ($bridgeFactory->getBridgeClassNames() as $bridgeClassName) {
|
||||
$bridge = $bridgeFactory->create($bridgeClassName);
|
||||
|
||||
if ($bridge === false) { // Broken bridge, show as inactive
|
||||
$list->bridges[$bridgeName] = [
|
||||
$list->bridges[$bridgeClassName] = [
|
||||
'status' => 'inactive'
|
||||
];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$status = $bridgeFactory->isWhitelisted($bridgeName) ? 'active' : 'inactive';
|
||||
$status = $bridgeFactory->isWhitelisted($bridgeClassName) ? 'active' : 'inactive';
|
||||
|
||||
$list->bridges[$bridgeName] = [
|
||||
$list->bridges[$bridgeClassName] = [
|
||||
'status' => $status,
|
||||
'uri' => $bridge->getURI(),
|
||||
'donationUri' => $bridge->getDonationURI(),
|
||||
|
Reference in New Issue
Block a user