2018-07-21 19:15:07 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03: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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A generator class for a single bridge card on the home page of RSS-Bridge.
|
|
|
|
*
|
|
|
|
* This class generates the HTML content for a single bridge card for the home
|
|
|
|
* page of RSS-Bridge.
|
|
|
|
*
|
|
|
|
* @todo Return error if a caller creates an object of this class.
|
|
|
|
*/
|
2018-07-21 19:15:07 +03:00
|
|
|
final class BridgeCard
|
|
|
|
{
|
2022-08-06 23:46:28 +03:00
|
|
|
/**
|
|
|
|
* Gets a single bridge card
|
|
|
|
*
|
|
|
|
* @param class-string<BridgeInterface> $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)
|
|
|
|
{
|
|
|
|
$bridgeFactory = new BridgeFactory();
|
|
|
|
|
|
|
|
$bridge = $bridgeFactory->create($bridgeClassName);
|
|
|
|
|
|
|
|
$isHttps = strpos($bridge->getURI(), 'https') === 0;
|
|
|
|
|
|
|
|
$uri = $bridge->getURI();
|
|
|
|
$name = $bridge->getName();
|
|
|
|
$icon = $bridge->getIcon();
|
|
|
|
$description = $bridge->getDescription();
|
|
|
|
$parameters = $bridge->getParameters();
|
|
|
|
if (Configuration::getConfig('proxy', 'url') && Configuration::getConfig('proxy', 'by_bridge')) {
|
|
|
|
$parameters['global']['_noproxy'] = [
|
|
|
|
'name' => 'Disable proxy (' . (Configuration::getConfig('proxy', 'name') ?: Configuration::getConfig('proxy', 'url')) . ')',
|
|
|
|
'type' => 'checkbox'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-08-23 22:19:53 +03:00
|
|
|
if (Configuration::getConfig('cache', 'custom_timeout')) {
|
2022-08-06 23:46:28 +03:00
|
|
|
$parameters['global']['_cache_timeout'] = [
|
|
|
|
'name' => 'Cache timeout in seconds',
|
|
|
|
'type' => 'number',
|
|
|
|
'defaultValue' => $bridge->getCacheTimeout()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-08-07 00:12:30 +03:00
|
|
|
$shortName = $bridge->getShortName();
|
2022-08-06 23:46:28 +03:00
|
|
|
$card = <<<CARD
|
2022-08-07 00:12:30 +03:00
|
|
|
<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>
|
2022-08-06 23:46:28 +03:00
|
|
|
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']);
|
|
|
|
} else {
|
|
|
|
foreach ($parameters as $parameterName => $parameter) {
|
|
|
|
if (!is_numeric($parameterName) && $parameterName === 'global') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('global', $parameters)) {
|
|
|
|
$parameter = array_merge($parameter, $parameters['global']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_numeric($parameterName)) {
|
|
|
|
$card .= '<h5>' . $parameterName . '</h5>' . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
$card .= self::getForm($bridgeClassName, $formats, $isActive, $isHttps, $parameterName, $parameter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$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>',
|
|
|
|
$bridge->getMaintainer(),
|
|
|
|
$bridge->getDonationURI()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$card .= sprintf('<p class="maintainer">%s</p>', $bridge->getMaintainer());
|
|
|
|
}
|
|
|
|
$card .= '</section>';
|
|
|
|
|
|
|
|
return $card;
|
|
|
|
}
|
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* Get the form header for a bridge card
|
|
|
|
*
|
2022-07-08 13:54:23 +03:00
|
|
|
* @param class-string<BridgeInterface> $bridgeClassName The bridge name
|
2018-11-16 23:48:59 +03:00
|
|
|
* @param bool $isHttps If disabled, adds a warning to the form
|
|
|
|
* @return string The form header
|
|
|
|
*/
|
2022-07-08 13:54:23 +03:00
|
|
|
private static function getFormHeader($bridgeClassName, $isHttps = false, $parameterName = '')
|
2019-06-21 20:08:59 +03:00
|
|
|
{
|
2018-07-21 19:15:07 +03:00
|
|
|
$form = <<<EOD
|
2022-08-07 00:12:30 +03:00
|
|
|
<form method="GET" action="?">
|
|
|
|
<input type="hidden" name="action" value="display" />
|
|
|
|
<input type="hidden" name="bridge" value="{$bridgeClassName}" />
|
2018-07-21 19:15:07 +03:00
|
|
|
EOD;
|
|
|
|
|
2019-06-21 20:08:59 +03:00
|
|
|
if (!empty($parameterName)) {
|
2022-08-06 23:46:28 +03:00
|
|
|
$form .= sprintf('<input type="hidden" name="context" value="%s" />', $parameterName);
|
2019-06-21 20:08:59 +03:00
|
|
|
}
|
|
|
|
|
2018-07-21 19:15:07 +03:00
|
|
|
if (!$isHttps) {
|
|
|
|
$form .= '<div class="secure-warning">Warning :
|
|
|
|
This bridge is not fetching its content through a secure connection</div>';
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-21 19:15:07 +03:00
|
|
|
return $form;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* Get the form body for a bridge
|
|
|
|
*
|
2022-07-08 13:54:23 +03:00
|
|
|
* @param class-string<BridgeInterface> $bridgeClassName The bridge name
|
2018-11-16 23:48:59 +03:00
|
|
|
* @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
|
|
|
|
*/
|
2018-07-21 19:15:07 +03:00
|
|
|
private static function getForm(
|
2022-07-08 13:54:23 +03:00
|
|
|
$bridgeClassName,
|
2018-07-21 19:15:07 +03:00
|
|
|
$formats,
|
|
|
|
$isActive = false,
|
|
|
|
$isHttps = false,
|
|
|
|
$parameterName = '',
|
|
|
|
$parameters = []
|
|
|
|
) {
|
2022-07-08 13:54:23 +03:00
|
|
|
$form = self::getFormHeader($bridgeClassName, $isHttps, $parameterName);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-09-15 17:39:50 +03:00
|
|
|
if (count($parameters) > 0) {
|
|
|
|
$form .= '<div class="parameters">';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-09-15 17:39:50 +03:00
|
|
|
foreach ($parameters as $id => $inputEntry) {
|
|
|
|
if (!isset($inputEntry['exampleValue'])) {
|
|
|
|
$inputEntry['exampleValue'] = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:39:50 +03:00
|
|
|
if (!isset($inputEntry['defaultValue'])) {
|
|
|
|
$inputEntry['defaultValue'] = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:39:50 +03:00
|
|
|
$idArg = 'arg-'
|
2022-07-08 13:54:23 +03:00
|
|
|
. urlencode($bridgeClassName)
|
2018-09-15 17:39:50 +03:00
|
|
|
. '-'
|
|
|
|
. urlencode($parameterName)
|
|
|
|
. '-'
|
|
|
|
. urlencode($id);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-09-15 17:39:50 +03:00
|
|
|
$form .= '<label for="'
|
|
|
|
. $idArg
|
|
|
|
. '">'
|
2022-04-03 10:53:13 +03:00
|
|
|
. filter_var($inputEntry['name'], FILTER_SANITIZE_FULL_SPECIAL_CHARS)
|
2018-09-15 17:39:50 +03:00
|
|
|
. '</label>'
|
|
|
|
. PHP_EOL;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-09-15 17:39:50 +03:00
|
|
|
if (!isset($inputEntry['type']) || $inputEntry['type'] === 'text') {
|
2018-11-18 18:59:13 +03:00
|
|
|
$form .= self::getTextInput($inputEntry, $idArg, $id);
|
2018-09-15 17:39:50 +03:00
|
|
|
} elseif ($inputEntry['type'] === 'number') {
|
2018-11-18 18:59:13 +03:00
|
|
|
$form .= self::getNumberInput($inputEntry, $idArg, $id);
|
2018-09-15 17:39:50 +03:00
|
|
|
} elseif ($inputEntry['type'] === 'list') {
|
2018-11-18 18:59:13 +03:00
|
|
|
$form .= self::getListInput($inputEntry, $idArg, $id);
|
2018-09-15 17:39:50 +03:00
|
|
|
} elseif ($inputEntry['type'] === 'checkbox') {
|
2018-11-18 18:59:13 +03:00
|
|
|
$form .= self::getCheckboxInput($inputEntry, $idArg, $id);
|
2018-09-15 17:39:50 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-03 10:53:13 +03:00
|
|
|
if (isset($inputEntry['title'])) {
|
|
|
|
$title_filtered = filter_var($inputEntry['title'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
|
|
|
|
$form .= '<i class="info" title="' . $title_filtered . '">i</i>';
|
|
|
|
} else {
|
2020-10-26 10:11:58 +03:00
|
|
|
$form .= '<i class="no-info"></i>';
|
2022-04-03 10:53:13 +03:00
|
|
|
}
|
2018-07-21 19:15:07 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-09-15 17:39:50 +03:00
|
|
|
$form .= '</div>';
|
2018-07-21 19:15:07 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-21 19:15:07 +03:00
|
|
|
if ($isActive) {
|
2022-03-24 22:37:34 +03:00
|
|
|
$form .= '<button type="submit" name="format" formtarget="_blank" value="Html">Generate feed</button>';
|
2018-07-21 19:15:07 +03:00
|
|
|
} else {
|
|
|
|
$form .= '<span style="font-weight: bold;">Inactive</span>';
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-21 19:15:07 +03:00
|
|
|
return $form . '</form>' . PHP_EOL;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* Get input field attributes
|
|
|
|
*
|
|
|
|
* @param array $entry The current entry
|
|
|
|
* @return string The input field attributes
|
|
|
|
*/
|
2018-07-21 19:15:07 +03:00
|
|
|
private static function getInputAttributes($entry)
|
|
|
|
{
|
|
|
|
$retVal = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-21 19:15:07 +03:00
|
|
|
if (isset($entry['required']) && $entry['required'] === true) {
|
|
|
|
$retVal .= ' required';
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-07-21 19:15:07 +03:00
|
|
|
if (isset($entry['pattern'])) {
|
|
|
|
$retVal .= ' pattern="' . $entry['pattern'] . '"';
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-07-21 19:15:07 +03:00
|
|
|
return $retVal;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* Get text input
|
|
|
|
*
|
|
|
|
* @param array $entry The current entry
|
|
|
|
* @param string $id The field ID
|
|
|
|
* @param string $name The field name
|
|
|
|
* @return string The text input field
|
|
|
|
*/
|
2018-07-21 19:15:07 +03:00
|
|
|
private static function getTextInput($entry, $id, $name)
|
|
|
|
{
|
|
|
|
return '<input '
|
2018-11-18 18:59:13 +03:00
|
|
|
. self::getInputAttributes($entry)
|
2018-07-21 19:15:07 +03:00
|
|
|
. ' id="'
|
|
|
|
. $id
|
|
|
|
. '" type="text" value="'
|
2022-04-03 10:53:13 +03:00
|
|
|
. filter_var($entry['defaultValue'], FILTER_SANITIZE_FULL_SPECIAL_CHARS)
|
2018-07-21 19:15:07 +03:00
|
|
|
. '" placeholder="'
|
2022-04-03 10:53:13 +03:00
|
|
|
. filter_var($entry['exampleValue'], FILTER_SANITIZE_FULL_SPECIAL_CHARS)
|
2018-07-21 19:15:07 +03:00
|
|
|
. '" name="'
|
|
|
|
. $name
|
2018-09-15 17:39:50 +03:00
|
|
|
. '" />'
|
2018-07-21 19:15:07 +03:00
|
|
|
. PHP_EOL;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* Get number input
|
|
|
|
*
|
|
|
|
* @param array $entry The current entry
|
|
|
|
* @param string $id The field ID
|
|
|
|
* @param string $name The field name
|
|
|
|
* @return string The number input field
|
|
|
|
*/
|
2018-07-21 19:15:07 +03:00
|
|
|
private static function getNumberInput($entry, $id, $name)
|
|
|
|
{
|
|
|
|
return '<input '
|
2018-11-18 18:59:13 +03:00
|
|
|
. self::getInputAttributes($entry)
|
2018-07-21 19:15:07 +03:00
|
|
|
. ' id="'
|
|
|
|
. $id
|
|
|
|
. '" type="number" value="'
|
|
|
|
. filter_var($entry['defaultValue'], FILTER_SANITIZE_NUMBER_INT)
|
|
|
|
. '" placeholder="'
|
|
|
|
. filter_var($entry['exampleValue'], FILTER_SANITIZE_NUMBER_INT)
|
|
|
|
. '" name="'
|
|
|
|
. $name
|
2018-09-15 17:39:50 +03:00
|
|
|
. '" />'
|
2018-07-21 19:15:07 +03:00
|
|
|
. PHP_EOL;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* Get list input
|
|
|
|
*
|
|
|
|
* @param array $entry The current entry
|
|
|
|
* @param string $id The field ID
|
|
|
|
* @param string $name The field name
|
|
|
|
* @return string The list input field
|
|
|
|
*/
|
2018-07-21 19:15:07 +03:00
|
|
|
private static function getListInput($entry, $id, $name)
|
|
|
|
{
|
2019-01-22 21:12:37 +03:00
|
|
|
if (isset($entry['required']) && $entry['required'] === true) {
|
2019-01-22 20:11:52 +03:00
|
|
|
Debug::log('The "required" attribute is not supported for lists.');
|
|
|
|
unset($entry['required']);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-21 19:15:07 +03:00
|
|
|
$list = '<select '
|
2018-11-18 18:59:13 +03:00
|
|
|
. self::getInputAttributes($entry)
|
2018-07-21 19:15:07 +03:00
|
|
|
. ' id="'
|
|
|
|
. $id
|
|
|
|
. '" name="'
|
|
|
|
. $name
|
|
|
|
. '" >';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-21 19:15:07 +03:00
|
|
|
foreach ($entry['values'] as $name => $value) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
$list .= '<optgroup label="' . htmlentities($name) . '">';
|
|
|
|
foreach ($value as $subname => $subvalue) {
|
|
|
|
if (
|
|
|
|
$entry['defaultValue'] === $subname
|
|
|
|
|| $entry['defaultValue'] === $subvalue
|
|
|
|
) {
|
|
|
|
$list .= '<option value="'
|
|
|
|
. $subvalue
|
|
|
|
. '" selected>'
|
|
|
|
. $subname
|
|
|
|
. '</option>';
|
|
|
|
} else {
|
|
|
|
$list .= '<option value="'
|
|
|
|
. $subvalue
|
|
|
|
. '">'
|
|
|
|
. $subname
|
|
|
|
. '</option>';
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2018-07-21 19:15:07 +03:00
|
|
|
$list .= '</optgroup>';
|
|
|
|
} else {
|
|
|
|
if (
|
|
|
|
$entry['defaultValue'] === $name
|
|
|
|
|| $entry['defaultValue'] === $value
|
|
|
|
) {
|
|
|
|
$list .= '<option value="'
|
|
|
|
. $value
|
|
|
|
. '" selected>'
|
|
|
|
. $name
|
|
|
|
. '</option>';
|
|
|
|
} else {
|
|
|
|
$list .= '<option value="'
|
|
|
|
. $value
|
|
|
|
. '">'
|
|
|
|
. $name
|
|
|
|
. '</option>';
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:39:50 +03:00
|
|
|
$list .= '</select>';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-21 19:15:07 +03:00
|
|
|
return $list;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* Get checkbox input
|
|
|
|
*
|
|
|
|
* @param array $entry The current entry
|
|
|
|
* @param string $id The field ID
|
|
|
|
* @param string $name The field name
|
|
|
|
* @return string The checkbox input field
|
|
|
|
*/
|
2018-07-21 19:15:07 +03:00
|
|
|
private static function getCheckboxInput($entry, $id, $name)
|
|
|
|
{
|
2019-01-22 21:12:37 +03:00
|
|
|
if (isset($entry['required']) && $entry['required'] === true) {
|
2019-01-22 20:11:52 +03:00
|
|
|
Debug::log('The "required" attribute is not supported for checkboxes.');
|
|
|
|
unset($entry['required']);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-21 19:15:07 +03:00
|
|
|
return '<input '
|
2018-11-18 18:59:13 +03:00
|
|
|
. self::getInputAttributes($entry)
|
2018-07-21 19:15:07 +03:00
|
|
|
. ' id="'
|
|
|
|
. $id
|
|
|
|
. '" type="checkbox" name="'
|
|
|
|
. $name
|
|
|
|
. '" '
|
2018-10-20 14:14:46 +03:00
|
|
|
. ($entry['defaultValue'] === 'checked' ? 'checked' : '')
|
2018-09-15 17:39:50 +03:00
|
|
|
. ' />'
|
2018-07-21 19:15:07 +03:00
|
|
|
. PHP_EOL;
|
|
|
|
}
|
|
|
|
}
|