2016-09-05 19:05:19 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-09-20 03:45:48 +03:00
|
|
|
abstract class BridgeAbstract
|
2016-09-05 19:05:19 +03:00
|
|
|
{
|
2016-09-10 21:41:11 +03:00
|
|
|
const NAME = 'Unnamed bridge';
|
|
|
|
const URI = '';
|
2021-06-25 22:45:25 +03:00
|
|
|
const DONATION_URI = '';
|
2016-09-10 21:41:11 +03:00
|
|
|
const DESCRIPTION = 'No description provided';
|
2024-04-06 19:07:45 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Preferably a github username
|
|
|
|
*/
|
2016-09-10 21:41:11 +03:00
|
|
|
const MAINTAINER = 'No maintainer';
|
2024-04-06 19:07:45 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache TTL in seconds
|
|
|
|
*/
|
2016-09-25 18:04:28 +03:00
|
|
|
const CACHE_TIMEOUT = 3600;
|
2024-04-06 19:07:45 +03:00
|
|
|
|
2020-12-12 19:05:22 +03:00
|
|
|
const CONFIGURATION = [];
|
2016-09-10 21:41:11 +03:00
|
|
|
const PARAMETERS = [];
|
2021-06-05 07:19:07 +03:00
|
|
|
const TEST_DETECT_PARAMETERS = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-10 19:56:24 +03:00
|
|
|
/**
|
|
|
|
* This is a convenient const for the limit option in bridge contexts.
|
|
|
|
* Can be inlined and modified if necessary.
|
|
|
|
*/
|
|
|
|
protected const LIMIT = [
|
|
|
|
'name' => 'Limit',
|
|
|
|
'type' => 'number',
|
|
|
|
'title' => 'Maximum number of items to return',
|
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-08-06 23:46:28 +03:00
|
|
|
protected array $items = [];
|
|
|
|
protected array $inputs = [];
|
2023-09-23 17:52:39 +03:00
|
|
|
protected ?string $queriedContext = '';
|
2023-08-01 20:35:06 +03:00
|
|
|
private array $configuration = [];
|
|
|
|
|
2023-09-21 23:05:55 +03:00
|
|
|
protected CacheInterface $cache;
|
|
|
|
protected Logger $logger;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
CacheInterface $cache,
|
|
|
|
Logger $logger
|
|
|
|
) {
|
|
|
|
$this->cache = $cache;
|
|
|
|
$this->logger = $logger;
|
2023-09-10 22:50:15 +03:00
|
|
|
}
|
|
|
|
|
2023-09-20 03:45:48 +03:00
|
|
|
abstract public function collectData();
|
|
|
|
|
2024-01-09 22:18:33 +03:00
|
|
|
public function getFeed(): array
|
2016-09-10 21:41:11 +03:00
|
|
|
{
|
2024-01-09 22:18:33 +03:00
|
|
|
return [
|
|
|
|
'name' => $this->getName(),
|
|
|
|
'uri' => $this->getURI(),
|
|
|
|
'donationUri' => $this->getDonationURI(),
|
|
|
|
'icon' => $this->getIcon(),
|
|
|
|
];
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-01-09 22:18:33 +03:00
|
|
|
public function getName()
|
2023-09-20 03:45:48 +03:00
|
|
|
{
|
2024-01-09 22:18:33 +03:00
|
|
|
return static::NAME;
|
2023-09-20 03:45:48 +03:00
|
|
|
}
|
|
|
|
|
2024-01-09 22:18:33 +03:00
|
|
|
public function getURI()
|
2023-09-20 03:45:48 +03:00
|
|
|
{
|
2024-01-09 22:18:33 +03:00
|
|
|
return static::URI ?? 'https://github.com/RSS-Bridge/rss-bridge/';
|
2023-09-20 03:45:48 +03:00
|
|
|
}
|
|
|
|
|
2024-01-09 22:18:33 +03:00
|
|
|
public function getDonationURI(): string
|
2023-09-20 03:45:48 +03:00
|
|
|
{
|
2024-01-09 22:18:33 +03:00
|
|
|
return static::DONATION_URI;
|
2023-09-20 03:45:48 +03:00
|
|
|
}
|
|
|
|
|
2024-01-09 22:18:33 +03:00
|
|
|
public function getIcon()
|
2023-09-20 03:45:48 +03:00
|
|
|
{
|
2024-01-09 22:18:33 +03:00
|
|
|
if (static::URI) {
|
|
|
|
// This favicon may or may not exist
|
|
|
|
return rtrim(static::URI, '/') . '/favicon.ico';
|
|
|
|
}
|
|
|
|
return '';
|
2023-09-20 03:45:48 +03:00
|
|
|
}
|
|
|
|
|
2024-01-09 22:18:33 +03:00
|
|
|
public function getOption(string $name)
|
2023-09-20 03:45:48 +03:00
|
|
|
{
|
2024-01-09 22:18:33 +03:00
|
|
|
return $this->configuration[$name] ?? null;
|
2023-09-20 03:45:48 +03:00
|
|
|
}
|
|
|
|
|
2024-01-09 22:18:33 +03:00
|
|
|
/**
|
2024-01-29 23:51:06 +03:00
|
|
|
* The description is only used in bridge card rendering on frontpage
|
2024-01-09 22:18:33 +03:00
|
|
|
*/
|
|
|
|
public function getDescription()
|
2023-09-20 03:45:48 +03:00
|
|
|
{
|
2024-01-09 22:18:33 +03:00
|
|
|
return static::DESCRIPTION;
|
2023-09-20 03:45:48 +03:00
|
|
|
}
|
|
|
|
|
2024-01-09 22:18:33 +03:00
|
|
|
public function getMaintainer(): string
|
|
|
|
{
|
|
|
|
return static::MAINTAINER;
|
|
|
|
}
|
|
|
|
|
2024-01-25 15:03:00 +03:00
|
|
|
/**
|
|
|
|
* A more correct method name would have been "getContexts"
|
|
|
|
*/
|
2024-01-09 22:18:33 +03:00
|
|
|
public function getParameters(): array
|
2023-09-20 03:45:48 +03:00
|
|
|
{
|
2024-01-09 22:18:33 +03:00
|
|
|
return static::PARAMETERS;
|
2023-09-20 03:45:48 +03:00
|
|
|
}
|
|
|
|
|
2024-01-09 22:18:33 +03:00
|
|
|
public function getItems()
|
2023-09-20 03:45:48 +03:00
|
|
|
{
|
2024-01-09 22:18:33 +03:00
|
|
|
return $this->items;
|
2023-09-20 03:45:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getCacheTimeout()
|
|
|
|
{
|
|
|
|
return static::CACHE_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
2023-10-01 20:23:30 +03:00
|
|
|
public function loadConfiguration()
|
|
|
|
{
|
|
|
|
foreach (static::CONFIGURATION as $optionName => $optionValue) {
|
|
|
|
$section = $this->getShortName();
|
|
|
|
$configurationOption = Configuration::getConfig($section, $optionName);
|
|
|
|
|
|
|
|
if ($configurationOption !== null) {
|
|
|
|
$this->configuration[$optionName] = $configurationOption;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($optionValue['required']) && $optionValue['required'] === true) {
|
|
|
|
throw new \Exception(sprintf('Missing configuration option: %s', $optionName));
|
|
|
|
} elseif (isset($optionValue['defaultValue'])) {
|
|
|
|
$this->configuration[$optionName] = $optionValue['defaultValue'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setInput(array $input)
|
|
|
|
{
|
2024-01-25 15:03:00 +03:00
|
|
|
// This is the submitted context
|
|
|
|
$contextName = $input['context'] ?? null;
|
|
|
|
if ($contextName) {
|
2023-10-01 20:23:30 +03:00
|
|
|
// Context hinting (optional)
|
2024-01-25 15:03:00 +03:00
|
|
|
$this->queriedContext = $contextName;
|
2023-10-01 20:23:30 +03:00
|
|
|
unset($input['context']);
|
|
|
|
}
|
|
|
|
|
2024-01-25 15:03:00 +03:00
|
|
|
$contexts = $this->getParameters();
|
2023-10-01 20:23:30 +03:00
|
|
|
|
2024-01-25 15:03:00 +03:00
|
|
|
if (!$contexts) {
|
2023-10-01 20:23:30 +03:00
|
|
|
if ($input) {
|
|
|
|
throw new \Exception('Invalid parameters value(s)');
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$validator = new ParameterValidator();
|
|
|
|
|
2024-01-25 15:03:00 +03:00
|
|
|
// $input IS PASSED BY REFERENCE!
|
|
|
|
$errors = $validator->validateInput($input, $contexts);
|
|
|
|
if ($errors !== []) {
|
|
|
|
$invalidParameterKeys = array_column($errors, 'name');
|
2023-10-01 20:23:30 +03:00
|
|
|
throw new \Exception(sprintf('Invalid parameters value(s): %s', implode(', ', $invalidParameterKeys)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Guess the context from input data
|
|
|
|
if (empty($this->queriedContext)) {
|
2024-01-25 15:03:00 +03:00
|
|
|
$queriedContext = $validator->getQueriedContext($input, $contexts);
|
2023-10-01 20:23:30 +03:00
|
|
|
$this->queriedContext = $queriedContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($this->queriedContext)) {
|
|
|
|
throw new \Exception('Required parameter(s) missing');
|
|
|
|
} elseif ($this->queriedContext === false) {
|
|
|
|
throw new \Exception('Mixed context parameters');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->setInputWithContext($input, $this->queriedContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setInputWithContext(array $input, $queriedContext)
|
2016-09-10 21:41:11 +03:00
|
|
|
{
|
|
|
|
// Import and assign all inputs to their context
|
2023-10-01 20:23:30 +03:00
|
|
|
foreach ($input as $name => $value) {
|
2023-12-26 14:20:49 +03:00
|
|
|
foreach ($this->getParameters() as $context => $set) {
|
|
|
|
if (array_key_exists($name, $this->getParameters()[$context])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
$this->inputs[$context][$name]['value'] = $value;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
}
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
// Apply default values to missing data
|
2024-01-25 15:03:00 +03:00
|
|
|
$contextNames = [$queriedContext];
|
2023-12-26 14:20:49 +03:00
|
|
|
if (array_key_exists('global', $this->getParameters())) {
|
2024-01-25 15:03:00 +03:00
|
|
|
$contextNames[] = 'global';
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-01-25 15:03:00 +03:00
|
|
|
foreach ($contextNames as $context) {
|
2023-12-26 14:20:49 +03:00
|
|
|
if (!isset($this->getParameters()[$context])) {
|
2023-07-31 21:43:18 +03:00
|
|
|
// unknown context provided by client, throw exception here? or continue?
|
|
|
|
}
|
|
|
|
|
2023-12-26 14:20:49 +03:00
|
|
|
foreach ($this->getParameters()[$context] as $name => $properties) {
|
2017-07-29 20:28:00 +03:00
|
|
|
if (isset($this->inputs[$context][$name]['value'])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
continue;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
$type = $properties['type'] ?? 'text';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
switch ($type) {
|
2016-09-10 21:41:11 +03:00
|
|
|
case 'checkbox':
|
2023-10-01 20:23:30 +03:00
|
|
|
$this->inputs[$context][$name]['value'] = $input[$context][$name]['value'] ?? false;
|
2016-09-10 21:41:11 +03:00
|
|
|
break;
|
|
|
|
case 'list':
|
2017-07-29 20:28:00 +03:00
|
|
|
if (!isset($properties['defaultValue'])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
$firstItem = reset($properties['values']);
|
2017-07-29 20:28:00 +03:00
|
|
|
if (is_array($firstItem)) {
|
2016-09-10 21:41:11 +03:00
|
|
|
$firstItem = reset($firstItem);
|
|
|
|
}
|
|
|
|
$this->inputs[$context][$name]['value'] = $firstItem;
|
|
|
|
} else {
|
|
|
|
$this->inputs[$context][$name]['value'] = $properties['defaultValue'];
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2017-07-29 20:28:00 +03:00
|
|
|
if (isset($properties['defaultValue'])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
$this->inputs[$context][$name]['value'] = $properties['defaultValue'];
|
|
|
|
}
|
|
|
|
break;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
}
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
// Copy global parameter values to the guessed context
|
2023-12-26 14:20:49 +03:00
|
|
|
if (array_key_exists('global', $this->getParameters())) {
|
|
|
|
foreach ($this->getParameters()['global'] as $name => $properties) {
|
2023-10-01 20:23:30 +03:00
|
|
|
if (isset($input[$name])) {
|
|
|
|
$value = $input[$name];
|
2016-09-10 21:41:11 +03:00
|
|
|
} else {
|
2023-09-11 14:18:00 +03:00
|
|
|
if ($properties['type'] ?? null === 'checkbox') {
|
2023-09-10 05:15:05 +03:00
|
|
|
$value = false;
|
|
|
|
} elseif (isset($properties['defaultValue'])) {
|
|
|
|
$value = $properties['defaultValue'];
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
$this->inputs[$queriedContext][$name]['value'] = $value;
|
|
|
|
}
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
// Only keep guessed context parameters values
|
2017-07-29 20:28:00 +03:00
|
|
|
if (isset($this->inputs[$queriedContext])) {
|
2024-01-25 15:03:00 +03:00
|
|
|
$this->inputs = [
|
|
|
|
$queriedContext => $this->inputs[$queriedContext],
|
|
|
|
];
|
2016-09-10 21:41:11 +03:00
|
|
|
} else {
|
|
|
|
$this->inputs = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2017-02-13 21:26:39 +03:00
|
|
|
protected function getInput($input)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2023-09-20 03:45:48 +03:00
|
|
|
return $this->inputs[$this->queriedContext][$input]['value'] ?? null;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2023-03-02 15:25:57 +03:00
|
|
|
/**
|
|
|
|
* Get the key name of a given input
|
|
|
|
* Can process multilevel arrays with two levels, the max level a list can have
|
|
|
|
*
|
|
|
|
* @param string $input The input name
|
|
|
|
* @return string|null The accompaning key to a given input or null if the input is not defined
|
|
|
|
*/
|
|
|
|
public function getKey($input)
|
|
|
|
{
|
|
|
|
if (!isset($this->inputs[$this->queriedContext][$input]['value'])) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-01-25 15:03:00 +03:00
|
|
|
|
|
|
|
$contexts = $this->getParameters();
|
|
|
|
|
|
|
|
if (array_key_exists('global', $contexts)) {
|
|
|
|
if (array_key_exists($input, $contexts['global'])) {
|
|
|
|
$contextName = 'global';
|
2023-03-06 22:01:51 +03:00
|
|
|
}
|
|
|
|
}
|
2024-01-25 15:03:00 +03:00
|
|
|
if (!isset($contextName)) {
|
|
|
|
$contextName = $this->queriedContext;
|
2023-03-06 22:01:51 +03:00
|
|
|
}
|
2023-07-11 17:54:59 +03:00
|
|
|
|
2023-03-02 15:25:57 +03:00
|
|
|
$needle = $this->inputs[$this->queriedContext][$input]['value'];
|
2024-01-25 15:03:00 +03:00
|
|
|
foreach ($contexts[$contextName][$input]['values'] as $first_level_key => $first_level_value) {
|
2023-07-11 17:54:59 +03:00
|
|
|
if (!is_array($first_level_value) && $needle === (string)$first_level_value) {
|
2023-03-02 15:25:57 +03:00
|
|
|
return $first_level_key;
|
|
|
|
} elseif (is_array($first_level_value)) {
|
|
|
|
foreach ($first_level_value as $second_level_key => $second_level_value) {
|
|
|
|
if ($needle === (string)$second_level_value) {
|
|
|
|
return $second_level_key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 20:05:41 +03:00
|
|
|
public function detectParameters($url)
|
|
|
|
{
|
|
|
|
$regex = '/^(https?:\/\/)?(www\.)?(.+?)(\/)?$/';
|
2024-01-25 15:03:00 +03:00
|
|
|
|
|
|
|
$contexts = $this->getParameters();
|
|
|
|
|
2018-11-26 20:05:41 +03:00
|
|
|
if (
|
2024-01-25 15:03:00 +03:00
|
|
|
empty($contexts)
|
2018-11-26 20:05:41 +03:00
|
|
|
&& preg_match($regex, $url, $urlMatches) > 0
|
|
|
|
&& preg_match($regex, static::URI, $bridgeUriMatches) > 0
|
|
|
|
&& $urlMatches[3] === $bridgeUriMatches[3]
|
|
|
|
) {
|
|
|
|
return [];
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2022-08-06 23:46:28 +03:00
|
|
|
return null;
|
2018-11-26 20:05:41 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
protected function loadCacheValue(string $key)
|
2022-04-02 09:15:28 +03:00
|
|
|
{
|
2023-09-10 22:50:15 +03:00
|
|
|
$cacheKey = $this->getShortName() . '_' . $key;
|
2023-09-21 23:05:55 +03:00
|
|
|
return $this->cache->get($cacheKey);
|
2022-04-02 09:15:28 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
protected function saveCacheValue(string $key, $value, $ttl = 86400)
|
2022-04-02 09:15:28 +03:00
|
|
|
{
|
2023-09-10 22:50:15 +03:00
|
|
|
$cacheKey = $this->getShortName() . '_' . $key;
|
2023-09-21 23:05:55 +03:00
|
|
|
$this->cache->set($cacheKey, $value, $ttl);
|
2022-04-02 09:15:28 +03:00
|
|
|
}
|
2022-08-07 00:12:30 +03:00
|
|
|
|
|
|
|
public function getShortName(): string
|
|
|
|
{
|
|
|
|
return (new \ReflectionClass($this))->getShortName();
|
|
|
|
}
|
2016-09-05 19:05:19 +03:00
|
|
|
}
|