Rss-Bridge/lib/BridgeAbstract.php

189 lines
4.6 KiB
PHP
Raw Normal View History

<?php
require_once(__DIR__ . '/BridgeInterface.php');
abstract class BridgeAbstract implements BridgeInterface {
const NAME = 'Unnamed bridge';
const URI = '';
const DESCRIPTION = 'No description provided';
const MAINTAINER = 'No maintainer';
const CACHE_TIMEOUT = 3600;
const PARAMETERS = array();
protected $items = array();
protected $inputs = array();
protected $queriedContext = '';
/**
* Return items stored in the bridge
* @return mixed
*/
public function getItems(){
return $this->items;
}
/**
* Sets the input values for a given context. Existing values are
* overwritten.
*
* @param array $inputs Associative array of inputs
* @param string $context The context name
*/
protected function setInputs(array $inputs, $queriedContext){
// Import and assign all inputs to their context
foreach($inputs as $name => $value) {
foreach(static::PARAMETERS as $context => $set) {
if(array_key_exists($name, static::PARAMETERS[$context])) {
$this->inputs[$context][$name]['value'] = $value;
}
}
}
// Apply default values to missing data
$contexts = array($queriedContext);
if(array_key_exists('global', static::PARAMETERS)) {
$contexts[] = 'global';
}
foreach($contexts as $context) {
foreach(static::PARAMETERS[$context] as $name => $properties) {
if(isset($this->inputs[$context][$name]['value'])) {
continue;
}
$type = isset($properties['type']) ? $properties['type'] : 'text';
switch($type) {
case 'checkbox':
if(!isset($properties['defaultValue'])) {
$this->inputs[$context][$name]['value'] = false;
} else {
$this->inputs[$context][$name]['value'] = $properties['defaultValue'];
}
break;
case 'list':
if(!isset($properties['defaultValue'])) {
$firstItem = reset($properties['values']);
if(is_array($firstItem)) {
$firstItem = reset($firstItem);
}
$this->inputs[$context][$name]['value'] = $firstItem;
} else {
$this->inputs[$context][$name]['value'] = $properties['defaultValue'];
}
break;
default:
if(isset($properties['defaultValue'])) {
$this->inputs[$context][$name]['value'] = $properties['defaultValue'];
}
break;
}
}
}
// Copy global parameter values to the guessed context
if(array_key_exists('global', static::PARAMETERS)) {
foreach(static::PARAMETERS['global'] as $name => $properties) {
if(isset($inputs[$name])) {
$value = $inputs[$name];
} elseif(isset($properties['value'])) {
$value = $properties['value'];
} else {
continue;
}
$this->inputs[$queriedContext][$name]['value'] = $value;
}
}
// Only keep guessed context parameters values
if(isset($this->inputs[$queriedContext])) {
$this->inputs = array($queriedContext => $this->inputs[$queriedContext]);
} else {
$this->inputs = array();
}
}
/**
* Defined datas with parameters depending choose bridge
* @param array array with expected bridge paramters
*/
public function setDatas(array $inputs){
if(empty(static::PARAMETERS)) {
Return exceptions in requested feed formats (#841) * [Exceptions] Don't return header for bridge exceptions * [Exceptions] Add link to list in exception message This is an alternative when the button is not rendered for some reason. * [index] Don't return bridge exception for formats * [index] Return feed item for bridge exceptions * [BridgeAbstract] Rename 'getCacheTime' to 'getModifiedTime' * [BridgeAbstract] Move caching to index.php to separate concerns index.php needs more control over caching behavior in order to cache exceptions. This cannot be done in a bridge, as the bridge might be broken, thus preventing caching from working. This also (and more importantly) separates concerns. The bridge should not even care if caching is involved or not. Its purpose is to collect and provide data. Response times should be faster, as more complex bridge functions like 'setDatas' (evaluates all input parameters to predict the current context) and 'collectData' (collects data from sites) can be skipped entirely. Notice: In its current form, index.php takes care of caching. This could, however, be moved into a separate class (i.e. CacheAbstract) in order to make implementation details cache specific. * [index] Add '_error_time' parameter to $item['uri'] This ensures that error messages are recognized by feed readers as new errors after 24 hours. During that time the same item is returned no matter how often the cache is cleared. References https://github.com/RSS-Bridge/rss-bridge/issues/814#issuecomment-420876162 * [index] Include '_error_time' in the title for errors This prevents feed readers from "updating" feeds based on the title * [index] Handle "HTTP_IF_MODIFIED_SINCE" client requests Implementation is based on `BridgeAbstract::dieIfNotModified()`, introduced in 422c125d8e25b9ac5209580b1976fb5d75dab8f8 and simplified based on https://stackoverflow.com/a/10847262 Basically, before returning cached data we check if the client send the "HTTP_IF_MODIFIED_SINCE" header. If the modification time is more recent or equal to the cache time, we reply with "HTTP/1.1 304 Not Modified" (same as before). Otherwise send the cached data. * [index] Don't encode exception message with `htmlspecialchars` * [Exceptions] Include error message in exception * [index] Show different error message for error code 0
2018-10-15 17:21:43 +02:00
if(!empty($inputs)) {
returnClientError('Invalid parameters value(s)');
}
return;
Return exceptions in requested feed formats (#841) * [Exceptions] Don't return header for bridge exceptions * [Exceptions] Add link to list in exception message This is an alternative when the button is not rendered for some reason. * [index] Don't return bridge exception for formats * [index] Return feed item for bridge exceptions * [BridgeAbstract] Rename 'getCacheTime' to 'getModifiedTime' * [BridgeAbstract] Move caching to index.php to separate concerns index.php needs more control over caching behavior in order to cache exceptions. This cannot be done in a bridge, as the bridge might be broken, thus preventing caching from working. This also (and more importantly) separates concerns. The bridge should not even care if caching is involved or not. Its purpose is to collect and provide data. Response times should be faster, as more complex bridge functions like 'setDatas' (evaluates all input parameters to predict the current context) and 'collectData' (collects data from sites) can be skipped entirely. Notice: In its current form, index.php takes care of caching. This could, however, be moved into a separate class (i.e. CacheAbstract) in order to make implementation details cache specific. * [index] Add '_error_time' parameter to $item['uri'] This ensures that error messages are recognized by feed readers as new errors after 24 hours. During that time the same item is returned no matter how often the cache is cleared. References https://github.com/RSS-Bridge/rss-bridge/issues/814#issuecomment-420876162 * [index] Include '_error_time' in the title for errors This prevents feed readers from "updating" feeds based on the title * [index] Handle "HTTP_IF_MODIFIED_SINCE" client requests Implementation is based on `BridgeAbstract::dieIfNotModified()`, introduced in 422c125d8e25b9ac5209580b1976fb5d75dab8f8 and simplified based on https://stackoverflow.com/a/10847262 Basically, before returning cached data we check if the client send the "HTTP_IF_MODIFIED_SINCE" header. If the modification time is more recent or equal to the cache time, we reply with "HTTP/1.1 304 Not Modified" (same as before). Otherwise send the cached data. * [index] Don't encode exception message with `htmlspecialchars` * [Exceptions] Include error message in exception * [index] Show different error message for error code 0
2018-10-15 17:21:43 +02: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)
);
}
// Guess the paramter context from input data
$this->queriedContext = $validator->getQueriedContext($inputs, static::PARAMETERS);
if(is_null($this->queriedContext)) {
returnClientError('Required parameter(s) missing');
} elseif($this->queriedContext === false) {
returnClientError('Mixed context parameters');
}
$this->setInputs($inputs, $this->queriedContext);
}
/**
* Returns the value for the provided input
*
* @param string $input The input name
* @return mixed Returns the input value or null if the input is not defined
*/
protected function getInput($input){
if(!isset($this->inputs[$this->queriedContext][$input]['value'])) {
return null;
}
return $this->inputs[$this->queriedContext][$input]['value'];
}
public function getDescription(){
return static::DESCRIPTION;
}
public function getMaintainer(){
return static::MAINTAINER;
}
public function getName(){
return static::NAME;
}
public function getIcon(){
return '';
}
public function getParameters(){
return static::PARAMETERS;
}
public function getURI(){
return static::URI;
}
2016-12-06 01:01:07 +01:00
public function getCacheTimeout(){
Return exceptions in requested feed formats (#841) * [Exceptions] Don't return header for bridge exceptions * [Exceptions] Add link to list in exception message This is an alternative when the button is not rendered for some reason. * [index] Don't return bridge exception for formats * [index] Return feed item for bridge exceptions * [BridgeAbstract] Rename 'getCacheTime' to 'getModifiedTime' * [BridgeAbstract] Move caching to index.php to separate concerns index.php needs more control over caching behavior in order to cache exceptions. This cannot be done in a bridge, as the bridge might be broken, thus preventing caching from working. This also (and more importantly) separates concerns. The bridge should not even care if caching is involved or not. Its purpose is to collect and provide data. Response times should be faster, as more complex bridge functions like 'setDatas' (evaluates all input parameters to predict the current context) and 'collectData' (collects data from sites) can be skipped entirely. Notice: In its current form, index.php takes care of caching. This could, however, be moved into a separate class (i.e. CacheAbstract) in order to make implementation details cache specific. * [index] Add '_error_time' parameter to $item['uri'] This ensures that error messages are recognized by feed readers as new errors after 24 hours. During that time the same item is returned no matter how often the cache is cleared. References https://github.com/RSS-Bridge/rss-bridge/issues/814#issuecomment-420876162 * [index] Include '_error_time' in the title for errors This prevents feed readers from "updating" feeds based on the title * [index] Handle "HTTP_IF_MODIFIED_SINCE" client requests Implementation is based on `BridgeAbstract::dieIfNotModified()`, introduced in 422c125d8e25b9ac5209580b1976fb5d75dab8f8 and simplified based on https://stackoverflow.com/a/10847262 Basically, before returning cached data we check if the client send the "HTTP_IF_MODIFIED_SINCE" header. If the modification time is more recent or equal to the cache time, we reply with "HTTP/1.1 304 Not Modified" (same as before). Otherwise send the cached data. * [index] Don't encode exception message with `htmlspecialchars` * [Exceptions] Include error message in exception * [index] Show different error message for error code 0
2018-10-15 17:21:43 +02:00
return static::CACHE_TIMEOUT;
}
}