2016-09-05 19:05:19 +03:00
|
|
|
<?php
|
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
|
|
|
|
*/
|
2018-11-06 21:23:32 +03:00
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* An abstract class for bridges
|
|
|
|
*
|
|
|
|
* This class implements {@see BridgeInterface} with most common functions in
|
|
|
|
* order to reduce code duplication. Bridges should inherit from this class
|
|
|
|
* instead of implementing the interface manually.
|
|
|
|
*
|
|
|
|
* @todo Move constants to the interface (this is supported by PHP)
|
|
|
|
* @todo Change visibility of constants to protected
|
|
|
|
* @todo Return `self` on more functions to allow chaining
|
|
|
|
* @todo Add specification for PARAMETERS ()
|
|
|
|
* @todo Add specification for $items
|
|
|
|
*/
|
2016-09-05 19:05:19 +03:00
|
|
|
abstract class BridgeAbstract implements BridgeInterface {
|
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* Name of the bridge
|
|
|
|
*
|
|
|
|
* Use {@see BridgeAbstract::getName()} to read this parameter
|
|
|
|
*/
|
2016-09-10 21:41:11 +03:00
|
|
|
const NAME = 'Unnamed bridge';
|
2018-11-16 23:48:59 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* URI to the site the bridge is intended to be used for.
|
|
|
|
*
|
|
|
|
* Use {@see BridgeAbstract::getURI()} to read this parameter
|
|
|
|
*/
|
2016-09-10 21:41:11 +03:00
|
|
|
const URI = '';
|
2018-11-16 23:48:59 +03:00
|
|
|
|
2021-06-25 22:45:25 +03:00
|
|
|
/**
|
|
|
|
* Donation URI to the site the bridge is intended to be used for.
|
|
|
|
*
|
|
|
|
* Use {@see BridgeAbstract::getDonationURI()} to read this parameter
|
|
|
|
*/
|
|
|
|
const DONATION_URI = '';
|
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* A brief description of what the bridge can do
|
|
|
|
*
|
|
|
|
* Use {@see BridgeAbstract::getDescription()} to read this parameter
|
|
|
|
*/
|
2016-09-10 21:41:11 +03:00
|
|
|
const DESCRIPTION = 'No description provided';
|
2018-11-16 23:48:59 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the maintainer. Multiple maintainers can be separated by comma
|
|
|
|
*
|
|
|
|
* Use {@see BridgeAbstract::getMaintainer()} to read this parameter
|
|
|
|
*/
|
2016-09-10 21:41:11 +03:00
|
|
|
const MAINTAINER = 'No maintainer';
|
2018-11-16 23:48:59 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The default cache timeout for the bridge
|
|
|
|
*
|
|
|
|
* Use {@see BridgeAbstract::getCacheTimeout()} to read this parameter
|
|
|
|
*/
|
2016-09-25 18:04:28 +03:00
|
|
|
const CACHE_TIMEOUT = 3600;
|
2018-11-16 23:48:59 +03:00
|
|
|
|
2020-12-12 19:05:22 +03:00
|
|
|
/**
|
|
|
|
* Configuration for the bridge
|
|
|
|
*
|
|
|
|
* Use {@see BridgeAbstract::getConfiguration()} to read this parameter
|
|
|
|
*/
|
|
|
|
const CONFIGURATION = array();
|
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* Parameters for the bridge
|
|
|
|
*
|
|
|
|
* Use {@see BridgeAbstract::getParameters()} to read this parameter
|
|
|
|
*/
|
2016-09-10 21:41:11 +03:00
|
|
|
const PARAMETERS = array();
|
|
|
|
|
2021-06-05 07:19:07 +03:00
|
|
|
/**
|
|
|
|
* Test cases for detectParameters for the bridge
|
|
|
|
*/
|
|
|
|
const TEST_DETECT_PARAMETERS = array();
|
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/**
|
|
|
|
* Holds the list of items collected by the bridge
|
|
|
|
*
|
|
|
|
* Items must be collected by {@see BridgeInterface::collectData()}
|
|
|
|
*
|
|
|
|
* Use {@see BridgeAbstract::getItems()} to access items.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-09-10 21:41:11 +03:00
|
|
|
protected $items = array();
|
2018-11-16 23:48:59 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds the list of input parameters used by the bridge
|
|
|
|
*
|
|
|
|
* Do not access this parameter directly!
|
|
|
|
* Use {@see BridgeAbstract::setInputs()} and {@see BridgeAbstract::getInput()} instead!
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-09-10 21:41:11 +03:00
|
|
|
protected $inputs = array();
|
2017-01-03 13:28:47 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
/**
|
2018-11-16 23:48:59 +03:00
|
|
|
* Holds the name of the queried context
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $queriedContext = '';
|
|
|
|
|
|
|
|
/** {@inheritdoc} */
|
2016-09-10 21:41:11 +03:00
|
|
|
public function getItems(){
|
|
|
|
return $this->items;
|
|
|
|
}
|
|
|
|
|
2017-02-13 21:26:39 +03:00
|
|
|
/**
|
2018-11-16 23:48:59 +03:00
|
|
|
* Sets the input values for a given context.
|
2017-02-13 21:26:39 +03:00
|
|
|
*
|
|
|
|
* @param array $inputs Associative array of inputs
|
2018-11-16 23:48:59 +03:00
|
|
|
* @param string $queriedContext The context name
|
|
|
|
* @return void
|
2017-02-13 21:26:39 +03:00
|
|
|
*/
|
2016-09-10 21:41:11 +03:00
|
|
|
protected function setInputs(array $inputs, $queriedContext){
|
|
|
|
// Import and assign all inputs to their context
|
2017-07-29 20:28:00 +03:00
|
|
|
foreach($inputs as $name => $value) {
|
|
|
|
foreach(static::PARAMETERS as $context => $set) {
|
|
|
|
if(array_key_exists($name, static::PARAMETERS[$context])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
$this->inputs[$context][$name]['value'] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply default values to missing data
|
|
|
|
$contexts = array($queriedContext);
|
2017-07-29 20:28:00 +03:00
|
|
|
if(array_key_exists('global', static::PARAMETERS)) {
|
2016-09-10 21:41:11 +03:00
|
|
|
$contexts[] = 'global';
|
|
|
|
}
|
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
foreach($contexts as $context) {
|
|
|
|
foreach(static::PARAMETERS[$context] as $name => $properties) {
|
|
|
|
if(isset($this->inputs[$context][$name]['value'])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = isset($properties['type']) ? $properties['type'] : 'text';
|
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
switch($type) {
|
2016-09-10 21:41:11 +03:00
|
|
|
case 'checkbox':
|
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'] = false;
|
|
|
|
} else {
|
|
|
|
$this->inputs[$context][$name]['value'] = $properties['defaultValue'];
|
|
|
|
}
|
|
|
|
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'];
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy global parameter values to the guessed context
|
2017-07-29 20:28:00 +03:00
|
|
|
if(array_key_exists('global', static::PARAMETERS)) {
|
|
|
|
foreach(static::PARAMETERS['global'] as $name => $properties) {
|
|
|
|
if(isset($inputs[$name])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
$value = $inputs[$name];
|
2019-11-01 17:29:16 +03:00
|
|
|
} elseif(isset($properties['defaultValue'])) {
|
|
|
|
$value = $properties['defaultValue'];
|
2016-09-10 21:41:11 +03:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->inputs[$queriedContext][$name]['value'] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only keep guessed context parameters values
|
2017-07-29 20:28:00 +03:00
|
|
|
if(isset($this->inputs[$queriedContext])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
$this->inputs = array($queriedContext => $this->inputs[$queriedContext]);
|
|
|
|
} else {
|
|
|
|
$this->inputs = array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-16 23:48:59 +03:00
|
|
|
* Set inputs for the bridge
|
|
|
|
*
|
|
|
|
* Returns errors and aborts execution if the provided input parameters are
|
|
|
|
* invalid.
|
|
|
|
*
|
|
|
|
* @param array List of input parameters. Each element in this list must
|
|
|
|
* relate to an item in {@see BridgeAbstract::PARAMETERS}
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-09-10 21:41:11 +03:00
|
|
|
public function setDatas(array $inputs){
|
|
|
|
|
2019-06-21 20:08:59 +03:00
|
|
|
if(isset($inputs['context'])) { // Context hinting (optional)
|
|
|
|
$this->queriedContext = $inputs['context'];
|
|
|
|
unset($inputs['context']);
|
|
|
|
}
|
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
if(empty(static::PARAMETERS)) {
|
2018-10-15 18:21:43 +03:00
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
if(!empty($inputs)) {
|
2016-09-26 00:22:33 +03:00
|
|
|
returnClientError('Invalid parameters value(s)');
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2018-10-15 18:21:43 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
|
2018-09-22 17:42:04 +03:00
|
|
|
$validator = new ParameterValidator();
|
|
|
|
|
|
|
|
if(!$validator->validateData($inputs, static::PARAMETERS)) {
|
|
|
|
$parameters = array_map(
|
|
|
|
function($i){ return $i['name']; }, // Just display parameter names
|
|
|
|
$validator->getInvalidParameters()
|
|
|
|
);
|
|
|
|
|
|
|
|
returnClientError(
|
|
|
|
'Invalid parameters value(s): '
|
|
|
|
. implode(', ', $parameters)
|
|
|
|
);
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
|
2019-06-21 20:08:59 +03:00
|
|
|
// Guess the context from input data
|
|
|
|
if(empty($this->queriedContext)) {
|
|
|
|
$this->queriedContext = $validator->getQueriedContext($inputs, static::PARAMETERS);
|
|
|
|
}
|
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
if(is_null($this->queriedContext)) {
|
2016-09-26 00:22:33 +03:00
|
|
|
returnClientError('Required parameter(s) missing');
|
2017-07-29 20:28:00 +03:00
|
|
|
} elseif($this->queriedContext === false) {
|
2016-09-26 00:22:33 +03:00
|
|
|
returnClientError('Mixed context parameters');
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->setInputs($inputs, $this->queriedContext);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-12-12 19:05:22 +03:00
|
|
|
/**
|
|
|
|
* Loads configuration for the bridge
|
|
|
|
*
|
|
|
|
* Returns errors and aborts execution if the provided configuration is
|
|
|
|
* invalid.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function loadConfiguration() {
|
|
|
|
foreach(static::CONFIGURATION as $optionName => $optionValue) {
|
|
|
|
|
|
|
|
$configurationOption = Configuration::getConfig(get_class($this), $optionName);
|
|
|
|
|
|
|
|
if($configurationOption !== null) {
|
|
|
|
$this->configuration[$optionName] = $configurationOption;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($optionValue['required']) && $optionValue['required'] === true) {
|
|
|
|
returnServerError(
|
|
|
|
'Missing configuration option: '
|
|
|
|
. $optionName
|
|
|
|
);
|
|
|
|
} elseif(isset($optionValue['defaultValue'])) {
|
|
|
|
$this->configuration[$optionName] = $optionValue['defaultValue'];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 21:26:39 +03:00
|
|
|
/**
|
|
|
|
* Returns the value for the provided input
|
|
|
|
*
|
|
|
|
* @param string $input The input name
|
2018-11-16 23:48:59 +03:00
|
|
|
* @return mixed|null The input value or null if the input is not defined
|
2017-02-13 21:26:39 +03:00
|
|
|
*/
|
|
|
|
protected function getInput($input){
|
2017-07-29 20:28:00 +03:00
|
|
|
if(!isset($this->inputs[$this->queriedContext][$input]['value'])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->inputs[$this->queriedContext][$input]['value'];
|
|
|
|
}
|
|
|
|
|
2020-12-12 19:05:22 +03:00
|
|
|
/**
|
|
|
|
* Returns the value for the selected configuration
|
|
|
|
*
|
|
|
|
* @param string $input The option name
|
|
|
|
* @return mixed|null The option value or null if the input is not defined
|
|
|
|
*/
|
|
|
|
public function getOption($name){
|
|
|
|
if(!isset($this->configuration[$name])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->configuration[$name];
|
|
|
|
}
|
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/** {@inheritdoc} */
|
2017-02-13 22:56:19 +03:00
|
|
|
public function getDescription(){
|
|
|
|
return static::DESCRIPTION;
|
|
|
|
}
|
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/** {@inheritdoc} */
|
2017-02-13 22:56:19 +03:00
|
|
|
public function getMaintainer(){
|
|
|
|
return static::MAINTAINER;
|
|
|
|
}
|
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/** {@inheritdoc} */
|
2016-09-10 21:41:11 +03:00
|
|
|
public function getName(){
|
|
|
|
return static::NAME;
|
|
|
|
}
|
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/** {@inheritdoc} */
|
2018-08-21 18:46:47 +03:00
|
|
|
public function getIcon(){
|
2020-12-07 20:08:58 +03:00
|
|
|
return static::URI . '/favicon.ico';
|
2018-08-21 18:46:47 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 19:05:22 +03:00
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function getConfiguration(){
|
|
|
|
return static::CONFIGURATION;
|
|
|
|
}
|
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/** {@inheritdoc} */
|
2017-02-13 22:56:19 +03:00
|
|
|
public function getParameters(){
|
|
|
|
return static::PARAMETERS;
|
|
|
|
}
|
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/** {@inheritdoc} */
|
2016-09-10 21:41:11 +03:00
|
|
|
public function getURI(){
|
|
|
|
return static::URI;
|
|
|
|
}
|
2016-12-06 03:01:07 +03:00
|
|
|
|
2021-06-25 22:45:25 +03:00
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function getDonationURI(){
|
|
|
|
return static::DONATION_URI;
|
|
|
|
}
|
|
|
|
|
2018-11-16 23:48:59 +03:00
|
|
|
/** {@inheritdoc} */
|
2018-03-14 20:06:36 +03:00
|
|
|
public function getCacheTimeout(){
|
2018-10-15 18:21:43 +03:00
|
|
|
return static::CACHE_TIMEOUT;
|
2018-03-14 20:06:36 +03:00
|
|
|
}
|
2018-08-25 22:00:38 +03:00
|
|
|
|
2018-11-26 20:05:41 +03:00
|
|
|
/** {@inheritdoc} */
|
|
|
|
public function detectParameters($url){
|
|
|
|
$regex = '/^(https?:\/\/)?(www\.)?(.+?)(\/)?$/';
|
|
|
|
if(empty(static::PARAMETERS)
|
|
|
|
&& preg_match($regex, $url, $urlMatches) > 0
|
|
|
|
&& preg_match($regex, static::URI, $bridgeUriMatches) > 0
|
|
|
|
&& $urlMatches[3] === $bridgeUriMatches[3]) {
|
|
|
|
return array();
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2016-09-05 19:05:19 +03:00
|
|
|
}
|