mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-08-05 08:07:33 +02:00
refactor/fix (#3924)
This commit is contained in:
@@ -6,33 +6,30 @@ final class BridgeCard
|
||||
* Gets a single bridge card
|
||||
*
|
||||
* @param class-string<BridgeAbstract> $bridgeClassName The bridge name
|
||||
* @param array $formats A list of formats
|
||||
* @param bool $isActive Indicates if the bridge is active or not
|
||||
* @return string The bridge card
|
||||
*/
|
||||
public static function displayBridgeCard($bridgeClassName, $formats, $isActive = true)
|
||||
public static function displayBridgeCard($bridgeClassName, $isActive = true)
|
||||
{
|
||||
$bridgeFactory = new BridgeFactory();
|
||||
|
||||
$bridge = $bridgeFactory->create($bridgeClassName);
|
||||
|
||||
$isHttps = str_starts_with($bridge->getURI(), 'https');
|
||||
|
||||
$uri = $bridge->getURI();
|
||||
$name = $bridge->getName();
|
||||
$icon = $bridge->getIcon();
|
||||
$description = $bridge->getDescription();
|
||||
$parameters = $bridge->getParameters();
|
||||
$contexts = $bridge->getParameters();
|
||||
|
||||
if (Configuration::getConfig('proxy', 'url') && Configuration::getConfig('proxy', 'by_bridge')) {
|
||||
$parameters['global']['_noproxy'] = [
|
||||
$contexts['global']['_noproxy'] = [
|
||||
'name' => 'Disable proxy (' . (Configuration::getConfig('proxy', 'name') ?: Configuration::getConfig('proxy', 'url')) . ')',
|
||||
'type' => 'checkbox'
|
||||
];
|
||||
}
|
||||
|
||||
if (Configuration::getConfig('cache', 'custom_timeout')) {
|
||||
$parameters['global']['_cache_timeout'] = [
|
||||
$contexts['global']['_cache_timeout'] = [
|
||||
'name' => 'Cache timeout in seconds',
|
||||
'type' => 'number',
|
||||
'defaultValue' => $bridge->getCacheTimeout()
|
||||
@@ -41,45 +38,51 @@ final class BridgeCard
|
||||
|
||||
$shortName = $bridge->getShortName();
|
||||
$card = <<<CARD
|
||||
<section
|
||||
class="bridge-card"
|
||||
id="bridge-{$bridgeClassName}"
|
||||
data-ref="{$name}"
|
||||
data-short-name="$shortName"
|
||||
>
|
||||
<section
|
||||
class="bridge-card"
|
||||
id="bridge-{$bridgeClassName}"
|
||||
data-ref="{$name}"
|
||||
data-short-name="$shortName"
|
||||
>
|
||||
|
||||
<h2><a href="{$uri}">{$name}</a></h2>
|
||||
<p class="description">{$description}</p>
|
||||
<input type="checkbox" class="showmore-box" id="showmore-{$bridgeClassName}" />
|
||||
<label class="showmore" for="showmore-{$bridgeClassName}">Show more</label>
|
||||
CARD;
|
||||
<h2><a href="{$uri}">{$name}</a></h2>
|
||||
<p class="description">{$description}</p>
|
||||
<input type="checkbox" class="showmore-box" id="showmore-{$bridgeClassName}" />
|
||||
<label class="showmore" for="showmore-{$bridgeClassName}">Show more</label>
|
||||
|
||||
|
||||
CARD;
|
||||
|
||||
// If we don't have any parameter for the bridge, we print a generic form to load it.
|
||||
if (count($parameters) === 0) {
|
||||
$card .= self::getForm($bridgeClassName, $formats, $isActive, $isHttps);
|
||||
|
||||
// Display form with cache timeout and/or noproxy options (if enabled) when bridge has no parameters
|
||||
} elseif (count($parameters) === 1 && array_key_exists('global', $parameters)) {
|
||||
$card .= self::getForm($bridgeClassName, $formats, $isActive, $isHttps, '', $parameters['global']);
|
||||
if (count($contexts) === 0) {
|
||||
// The bridge has zero parameters
|
||||
$card .= self::getForm($bridgeClassName, $isActive);
|
||||
} elseif (count($contexts) === 1 && array_key_exists('global', $contexts)) {
|
||||
// The bridge has a single context with key 'global'
|
||||
$card .= self::getForm($bridgeClassName, $isActive, '', $contexts['global']);
|
||||
} else {
|
||||
foreach ($parameters as $parameterName => $parameter) {
|
||||
if (!is_numeric($parameterName) && $parameterName === 'global') {
|
||||
// The bridge has one or more contexts (named or unnamed)
|
||||
foreach ($contexts as $contextName => $contextParameters) {
|
||||
if ($contextName === 'global') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (array_key_exists('global', $parameters)) {
|
||||
$parameter = array_merge($parameter, $parameters['global']);
|
||||
if (array_key_exists('global', $contexts)) {
|
||||
// Merge the global parameters into current context
|
||||
$contextParameters = array_merge($contextParameters, $contexts['global']);
|
||||
}
|
||||
|
||||
if (!is_numeric($parameterName)) {
|
||||
$card .= '<h5>' . $parameterName . '</h5>' . PHP_EOL;
|
||||
if (!is_numeric($contextName)) {
|
||||
// This is a named context
|
||||
$card .= '<h5>' . $contextName . '</h5>' . PHP_EOL;
|
||||
}
|
||||
|
||||
$card .= self::getForm($bridgeClassName, $formats, $isActive, $isHttps, $parameterName, $parameter);
|
||||
$card .= self::getForm($bridgeClassName, $isActive, $contextName, $contextParameters);
|
||||
}
|
||||
}
|
||||
|
||||
$card .= sprintf('<label class="showless" for="showmore-%s">Show less</label>', $bridgeClassName);
|
||||
|
||||
if ($bridge->getDonationURI() !== '' && Configuration::getConfig('admin', 'donations')) {
|
||||
$card .= sprintf(
|
||||
'<p class="maintainer">%s ~ <a href="%s">Donate</a></p>',
|
||||
@@ -94,31 +97,27 @@ CARD;
|
||||
return $card;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form body for a bridge
|
||||
*
|
||||
* @param class-string<BridgeAbstract> $bridgeClassName The bridge name
|
||||
* @param array $formats A list of supported formats
|
||||
* @param bool $isActive Indicates if a bridge is enabled or not
|
||||
* @param bool $isHttps Indicates if a bridge uses HTTPS or not
|
||||
* @param string $parameterName Sets the bridge context for the current form
|
||||
* @param array $parameters The bridge parameters
|
||||
* @return string The form body
|
||||
*/
|
||||
private static function getForm(
|
||||
$bridgeClassName,
|
||||
$formats,
|
||||
$isActive = false,
|
||||
$isHttps = false,
|
||||
$parameterName = '',
|
||||
$parameters = []
|
||||
string $bridgeClassName,
|
||||
bool $isActive = false,
|
||||
string $contextName = '',
|
||||
array $contextParameters = []
|
||||
) {
|
||||
$form = self::getFormHeader($bridgeClassName, $isHttps, $parameterName);
|
||||
$form = <<<EOD
|
||||
<form method="GET" action="?">
|
||||
<input type="hidden" name="action" value="display" />
|
||||
<input type="hidden" name="bridge" value="{$bridgeClassName}" />
|
||||
|
||||
if (count($parameters) > 0) {
|
||||
EOD;
|
||||
|
||||
if (!empty($contextName)) {
|
||||
$form .= sprintf('<input type="hidden" name="context" value="%s" />', $contextName);
|
||||
}
|
||||
|
||||
if (count($contextParameters) > 0) {
|
||||
$form .= '<div class="parameters">';
|
||||
|
||||
foreach ($parameters as $id => $inputEntry) {
|
||||
foreach ($contextParameters as $id => $inputEntry) {
|
||||
if (!isset($inputEntry['exampleValue'])) {
|
||||
$inputEntry['exampleValue'] = '';
|
||||
}
|
||||
@@ -127,19 +126,25 @@ CARD;
|
||||
$inputEntry['defaultValue'] = '';
|
||||
}
|
||||
|
||||
$idArg = 'arg-' . urlencode($bridgeClassName) . '-' . urlencode($parameterName) . '-' . urlencode($id);
|
||||
$idArg = 'arg-' . urlencode($bridgeClassName) . '-' . urlencode($contextName) . '-' . urlencode($id);
|
||||
|
||||
$inputName = filter_var($inputEntry['name'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
|
||||
$form .= '<label for="' . $idArg . '">' . $inputName . '</label>' . PHP_EOL;
|
||||
|
||||
if (!isset($inputEntry['type']) || $inputEntry['type'] === 'text') {
|
||||
$form .= self::getTextInput($inputEntry, $idArg, $id);
|
||||
if (
|
||||
!isset($inputEntry['type'])
|
||||
|| $inputEntry['type'] === 'text'
|
||||
) {
|
||||
$form .= self::getTextInput($inputEntry, $idArg, $id) . "\n";
|
||||
} elseif ($inputEntry['type'] === 'number') {
|
||||
$form .= self::getNumberInput($inputEntry, $idArg, $id);
|
||||
} elseif ($inputEntry['type'] === 'list') {
|
||||
$form .= self::getListInput($inputEntry, $idArg, $id);
|
||||
$form .= self::getListInput($inputEntry, $idArg, $id) . "\n";
|
||||
} elseif ($inputEntry['type'] === 'checkbox') {
|
||||
$form .= self::getCheckboxInput($inputEntry, $idArg, $id);
|
||||
} else {
|
||||
$foo = 2;
|
||||
// oops?
|
||||
}
|
||||
|
||||
$infoText = [];
|
||||
@@ -171,39 +176,13 @@ CARD;
|
||||
return $form . '</form>' . PHP_EOL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form header for a bridge card
|
||||
*
|
||||
* @param class-string<BridgeAbstract> $bridgeClassName The bridge name
|
||||
* @param bool $isHttps If disabled, adds a warning to the form
|
||||
* @return string The form header
|
||||
*/
|
||||
private static function getFormHeader($bridgeClassName, $isHttps = false, $parameterName = '')
|
||||
{
|
||||
$form = <<<EOD
|
||||
<form method="GET" action="?">
|
||||
<input type="hidden" name="action" value="display" />
|
||||
<input type="hidden" name="bridge" value="{$bridgeClassName}" />
|
||||
EOD;
|
||||
|
||||
if (!empty($parameterName)) {
|
||||
$form .= sprintf('<input type="hidden" name="context" value="%s" />', $parameterName);
|
||||
}
|
||||
|
||||
if (!$isHttps) {
|
||||
$form .= '<div class="secure-warning">Warning: This bridge is not fetching its content through a secure connection</div>';
|
||||
}
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
public static function getTextInput(array $entry, string $id, string $name): string
|
||||
{
|
||||
$defaultValue = filter_var($entry['defaultValue'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
|
||||
$exampleValue = filter_var($entry['exampleValue'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
|
||||
$attributes = self::getInputAttributes($entry);
|
||||
|
||||
return sprintf('<input %s id="%s" type="text" value="%s" placeholder="%s" name="%s" />' . "\n", $attributes, $id, $defaultValue, $exampleValue, $name);
|
||||
return sprintf('<input %s id="%s" type="text" value="%s" placeholder="%s" name="%s" />', $attributes, $id, $defaultValue, $exampleValue, $name);
|
||||
}
|
||||
|
||||
public static function getNumberInput(array $entry, string $id, string $name): string
|
||||
@@ -224,7 +203,7 @@ EOD;
|
||||
}
|
||||
|
||||
$attributes = self::getInputAttributes($entry);
|
||||
$list = sprintf('<select %s id="%s" name="%s" >', $attributes, $id, $name);
|
||||
$list = sprintf('<select %s id="%s" name="%s" >' . "\n", $attributes, $id, $name);
|
||||
|
||||
foreach ($entry['values'] as $name => $value) {
|
||||
if (is_array($value)) {
|
||||
@@ -245,9 +224,9 @@ EOD;
|
||||
$entry['defaultValue'] === $name
|
||||
|| $entry['defaultValue'] === $value
|
||||
) {
|
||||
$list .= '<option value="' . $value . '" selected>' . $name . '</option>';
|
||||
$list .= '<option value="' . $value . '" selected>' . $name . '</option>' . "\n";
|
||||
} else {
|
||||
$list .= '<option value="' . $value . '">' . $name . '</option>';
|
||||
$list .= '<option value="' . $value . '">' . $name . '</option>' . "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user