1
0
Fork 0
forked from blallo/rss-bridge

Little refactoring to reduce logic on index.php.

Moved RssExpander as a core logic system to lib/Bridge.php

Signed-off-by: teromene <teromene@teromene.fr>
This commit is contained in:
teromene 2015-11-05 20:26:48 +00:00
parent 4da956f365
commit d033bb51ac
9 changed files with 201 additions and 212 deletions

View file

@ -1,6 +1,5 @@
<?php <?php
require_once 'bridges/RssExpander.php'; define("FREENEWS_RSS", 'http://feeds.feedburner.com/Freenews-Freebox?format=xml');
define("RSS", 'http://feeds.feedburner.com/Freenews-Freebox?format=xml');
class Freenews extends RssExpander { class Freenews extends RssExpander {
public function loadMetadatas() { public function loadMetadatas() {
@ -21,8 +20,7 @@ class Freenews extends RssExpander {
} }
public function collectData(array $param){ public function collectData(array $param){
$param['url'] = RSS; parent::collectExpandableDatas($param, FREENEWS_RSS);
parent::collectData($param);
} }
protected function parseRSSItem($newsItem) { protected function parseRSSItem($newsItem) {

View file

@ -1,7 +1,7 @@
<?php <?php
require_once 'bridges/RssExpander.php';
define("RSS_PREFIX", "http://feeds.gawker.com/"); define("RSS_PREFIX", "http://feeds.gawker.com/");
define("RSS_SUFFIX", "/full"); define("RSS_SUFFIX", "/full");
class Gawker extends RssExpander{ class Gawker extends RssExpander{
public function loadMetadatas() { public function loadMetadatas() {
@ -34,7 +34,7 @@ class Gawker extends RssExpander{
$param['url'] = $this->toURI(strtolower($param['site'])); $param['url'] = $this->toURI(strtolower($param['site']));
} }
// $this->message("loading feed from ".$this->getURI()); // $this->message("loading feed from ".$this->getURI());
parent::collectData($param); parent::collectExpandableDatas($param, $name);
} }
protected function parseRSSItem($newsItem) { protected function parseRSSItem($newsItem) {

View file

@ -1,11 +1,7 @@
<?php <?php
require_once 'bridges/RssExpander.php';
define("SEXE", "http://sexes.blogs.liberation.fr"); define("SEXE", "http://sexes.blogs.liberation.fr");
define("SEXE_FEED", "http://sexes.blogs.liberation.fr/feeds/"); define("SEXE_FEED", "http://sexes.blogs.liberation.fr/feeds/");
/**
* As it seems that Les 400 culs currently offer a full feed, we won't change it content here.
* But I'm ready for the day where it will ... again ... provide some truncated content
*/
class Les400Culs extends RssExpander{ class Les400Culs extends RssExpander{
public function loadMetadatas() { public function loadMetadatas() {
@ -20,8 +16,7 @@ class Les400Culs extends RssExpander{
public function collectData(array $param){ public function collectData(array $param){
$param['url'] = SEXE_FEED; parent::collectExpandableDatas($param, SEXE_FEED);
parent::collectData($param);
} }
protected function parseRSSItem($newsItem) { protected function parseRSSItem($newsItem) {

View file

@ -1,68 +0,0 @@
<?php
/**
* A class providing facilities for RSS expansion. The goal here is to facilitate, as much as possible, writing bridges such as FreeNews, Gawker and other ones
* @name RssExpander
* @description Un bridge générique d'expansion automatique de contenu RSS ... pour tous ces sites qui ont un flux RSS mochement tonqué.
* @update 15/03/2015
* @use1(url="URL du flux dont vous souhaitez le contenu complet")
*/
abstract class RssExpander extends HttpCachingBridgeAbstract{
public $name;
public $uri;
public $description;
public function collectData(array $param){
if (empty($param['url'])) {
$this->returnError('There is no $param[\'url\'] for this RSS expander', 404);
}
// $this->message("Loading from ".$param['url']);
// Notice WE DO NOT use cache here on purpose : we want a fresh view of the RSS stream each time
$rssContent = simplexml_load_file($param['url']) or $this->returnError('Could not request '.$param['url'], 404);
// $this->message("loaded RSS from ".$param['url']);
// TODO insert RSS format detection
// we suppose for now, we have some RSS 2.0
$this->collect_RSS_2_0_data($rssContent);
}
protected function collect_RSS_2_0_data($rssContent) {
$rssContent = $rssContent->channel[0];
// $this->message("RSS content is ===========\n".var_export($rssContent, true)."===========");
$this->load_RSS_2_0_feed_data($rssContent);
foreach($rssContent->item as $item) {
// $this->message("parsing item ".var_export($item, true));
$this->items[] = $this->parseRSSItem($item);
}
}
protected function RSS_2_0_time_to_timestamp($item) {
return DateTime::createFromFormat('D, d M Y H:i:s e', $item->pubDate)->getTimestamp();
}
// TODO set title, link, description, language, and so on
protected function load_RSS_2_0_feed_data($rssContent) {
$this->name = trim($rssContent->title);
$this->uri = trim($rssContent->link);
$this->description = trim($rssContent->description);
}
/**
* Method should return, from a source RSS item given by lastRSS, one of our Items objects
* @param $item the input rss item
* @return a RSS-Bridge Item, with (hopefully) the whole content)
*/
abstract protected function parseRSSItem($item);
public function getName(){
return $this->name;
}
public function getURI(){
return $this->uri;
}
public function getDescription() {
return $this->description;
}
}

View file

@ -1,7 +1,4 @@
<?php <?php
require_once 'bridges/RssExpander.php';
define("THE_OATMEAL", "http://theoatmeal.com/"); define("THE_OATMEAL", "http://theoatmeal.com/");
define("THE_OATMEAL_RSS", "http://feeds.feedburner.com/oatmealfeed"); define("THE_OATMEAL_RSS", "http://feeds.feedburner.com/oatmealfeed");
@ -18,8 +15,7 @@ class TheOatmealBridge extends RssExpander{
} }
public function collectData(array $param){ public function collectData(array $param){
$param['url'] = THE_OATMEAL_RSS; parent::collectExpandableDatas($param, THE_OATMEAL_RSS);
parent::collectData($param);
} }

134
index.php
View file

@ -52,16 +52,10 @@ if (!file_exists($whitelist_file)) {
} }
else { else {
$whitelist_selection = explode("\n", file_get_contents($whitelist_file)); $whitelist_selection = explode("\n", file_get_contents($whitelist_file));
//Remove the last empty line.
array_pop($whitelist_selection); array_pop($whitelist_selection);
} }
// whitelist control function
function BridgeWhitelist( $whitelist, $name ) {
if(in_array("$name", $whitelist) or in_array("$name.php", $whitelist))
return TRUE;
else
return FALSE;
}
try{ try{
require_once __DIR__ . '/lib/RssBridge.php'; require_once __DIR__ . '/lib/RssBridge.php';
@ -81,7 +75,7 @@ try{
unset($_REQUEST['format']); unset($_REQUEST['format']);
// whitelist control // whitelist control
if(!BridgeWhitelist($whitelist_selection, $bridge)) { if(!Bridge::isWhitelisted($whitelist_selection, $bridge)) {
throw new \HttpException('This bridge is not whitelisted', 401); throw new \HttpException('This bridge is not whitelisted', 401);
die; die;
} }
@ -126,119 +120,6 @@ catch(\Exception $e){
die($e->getMessage()); die($e->getMessage());
} }
function getHelperButtonFormat($value, $name){
return '<button type="submit" name="format" value="' . $value . '">' . $name . '</button>';
}
function getHelperButtonsFormat($formats){
$buttons = '';
foreach( $formats as $name => $infos )
{
if ( isset($infos['name']) )
{
$buttons .= getHelperButtonFormat($name, $infos['name']) . PHP_EOL;
}
}
return $buttons;
}
function displayBridgeCard($bridgeName, $formats, $isActive = true)
{
$bridgeElement = Bridge::create($bridgeName);
if($bridgeElement == false) {
return "";
}
$bridgeElement->loadMetadatas();
$name = '<a href="'.$bridgeElement->uri.'">'.$bridgeElement->name.'</a>';
$description = $bridgeElement->description;
$card = <<<CARD
<section id="bridge-{$bridgeName}" data-ref="{$bridgeName}">
<h2>{$name}</h2>
<p class="description">
{$description}
</p>
CARD;
// If we don't have any parameter for the bridge, we print a generic form to load it.
if(count($bridgeElement->parameters) == 0) {
$card .= '<form method="POST" action="?">
<input type="hidden" name="action" value="display" />
<input type="hidden" name="bridge" value="' . $bridgeName . '" />' . PHP_EOL;
if ($isActive)
{
$card .= getHelperButtonsFormat($formats);
}
else
{
$card .= '<span style="font-weight: bold;">Inactive</span>';
}
$card .= '</form>' . PHP_EOL;
}
foreach($bridgeElement->parameters as $parameterName => $parameter)
{
$card .= '<ol class="list-use">' . PHP_EOL;
if(!is_numeric($parameterName)) {
$card .= '<h5>'.$parameterName.'</h5>' . PHP_EOL;
}
$card .= '<form method="POST" action="?">
<input type="hidden" name="action" value="display" />
<input type="hidden" name="bridge" value="' . $bridgeName . '" />' . PHP_EOL;
$parameter = json_decode($parameter, true);
foreach($parameter as $inputEntry) {
if(!isset($inputEntry['exampleValue'])) $inputEntry['exampleValue'] = "";
$idArg = 'arg-' . $bridgeName . '-' . $parameterName . '-' . $inputEntry['identifier'];
$card .= '<label for="' .$idArg. '">' .$inputEntry['name']. ' : </label>' . PHP_EOL;
if(!isset($inputEntry['type']) || $inputEntry['type'] == 'text') {
$card .= '<input id="' . $idArg . '" type="text" value="" placeholder="' . $inputEntry['exampleValue'] . '" name="' . $inputEntry['identifier'] . '" /><br />' . PHP_EOL;
} else if($inputEntry['type'] == 'number') {
$card .= '<input id="' . $idArg . '" type="number" value="" placeholder="' . $inputEntry['exampleValue'] . '" name="' . $inputEntry['identifier'] . '" /><br />' . PHP_EOL;
} else if($inputEntry['type'] == 'list') {
$card .= '<select id="' . $idArg . '" name="' . $inputEntry['name'] . '" >';
foreach($inputEntry['values'] as $listValues) {
$card .= "<option value='" . $listValues['value'] . "'>" . $listValues['name'] . "</option>";
}
$card .= '</select><br >';
} else if($inputEntry['type'] == 'checkbox') {
$card .= '<input id="' . $idArg . '" type="checkbox" name="' . $inputEntry['identifier'] . '" /><br />' . PHP_EOL;
}
}
if ($isActive)
{
$card .= getHelperButtonsFormat($formats);
}
else
{
$card .= '<span style="font-weight: bold;">Inactive</span>';
}
$card .= '</form>' . PHP_EOL;
}
$card .= '<span class="maintainer">'.$bridgeElement->maintainer.'</span>';
$card .= '</section>';
return $card;
}
$formats = Format::searchInformation(); $formats = Format::searchInformation();
?> ?>
@ -265,23 +146,24 @@ $formats = Format::searchInformation();
$activeFoundBridgeCount = 0; $activeFoundBridgeCount = 0;
$showInactive = isset($_REQUEST['show_inactive']) && $_REQUEST['show_inactive'] == 1; $showInactive = isset($_REQUEST['show_inactive']) && $_REQUEST['show_inactive'] == 1;
$inactiveBridges = ''; $inactiveBridges = '';
foreach(Bridge::listBridges() as $bridgeName) $bridgeList = Bridge::listBridges();
foreach($bridgeList as $bridgeName)
{ {
if(BridgeWhitelist($whitelist_selection, $bridgeName)) if(Bridge::isWhitelisted($whitelist_selection, $bridgeName))
{ {
echo displayBridgeCard($bridgeName, $formats); echo HTMLUtils::displayBridgeCard($bridgeName, $formats);
$activeFoundBridgeCount++; $activeFoundBridgeCount++;
} }
elseif ($showInactive) elseif ($showInactive)
{ {
// inactive bridges // inactive bridges
$inactiveBridges .= displayBridgeCard($bridgeName, $formats, false) . PHP_EOL; $inactiveBridges .= HTMLUtils::displayBridgeCard($bridgeName, $formats, false) . PHP_EOL;
} }
} }
echo '<hr />' . $inactiveBridges; echo '<hr />' . $inactiveBridges;
?> ?>
<footer> <footer>
<?= $activeFoundBridgeCount; ?>/<?= count($whitelist_selection) ?> active bridges (<a href="?show_inactive=1">Show inactive</a>)<br /> <?= $activeFoundBridgeCount; ?>/<?= count($bridgeList) ?> active bridges (<a href="?show_inactive=1">Show inactive</a>)<br />
<a href="https://github.com/sebsauvage/rss-bridge">RSS-Bridge alpha 0.2 ~ Public Domain</a> <a href="https://github.com/sebsauvage/rss-bridge">RSS-Bridge alpha 0.2 ~ Public Domain</a>
</footer> </footer>
</body> </body>

View file

@ -293,6 +293,72 @@ class Bridge{
return $listBridge; return $listBridge;
} }
static function isWhitelisted( $whitelist, $name ) {
if(in_array("$name", $whitelist) or in_array("$name.php", $whitelist))
return TRUE;
else
return FALSE;
}
} }
abstract class RssExpander extends HttpCachingBridgeAbstract{
public $name;
public $uri;
public $description;
public function collectExpandableDatas(array $param, $name){
if (empty($name)) {
$this->returnError('There is no $param[\'url\'] for this RSS expander', 404);
}
// $this->message("Loading from ".$param['url']);
// Notice WE DO NOT use cache here on purpose : we want a fresh view of the RSS stream each time
$rssContent = simplexml_load_file($name) or $this->returnError('Could not request '.$name, 404);
// $this->message("loaded RSS from ".$param['url']);
// TODO insert RSS format detection
// we suppose for now, we have some RSS 2.0
$this->collect_RSS_2_0_data($rssContent);
}
protected function collect_RSS_2_0_data($rssContent) {
$rssContent = $rssContent->channel[0];
// $this->message("RSS content is ===========\n".var_export($rssContent, true)."===========");
$this->load_RSS_2_0_feed_data($rssContent);
foreach($rssContent->item as $item) {
// $this->message("parsing item ".var_export($item, true));
$this->items[] = $this->parseRSSItem($item);
}
}
protected function RSS_2_0_time_to_timestamp($item) {
return DateTime::createFromFormat('D, d M Y H:i:s e', $item->pubDate)->getTimestamp();
}
// TODO set title, link, description, language, and so on
protected function load_RSS_2_0_feed_data($rssContent) {
$this->name = trim($rssContent->title);
$this->uri = trim($rssContent->link);
$this->description = trim($rssContent->description);
}
/**
* Method should return, from a source RSS item given by lastRSS, one of our Items objects
* @param $item the input rss item
* @return a RSS-Bridge Item, with (hopefully) the whole content)
*/
abstract protected function parseRSSItem($item);
public function getName(){
return $this->name;
}
public function getURI(){
return $this->uri;
}
public function getDescription() {
return $this->description;
}
}

119
lib/HTMLUtils.php Normal file
View file

@ -0,0 +1,119 @@
<?php
class HTMLUtils {
public static function getHelperButtonFormat($value, $name){
return '<button type="submit" name="format" value="' . $value . '">' . $name . '</button>';
}
public static function getHelperButtonsFormat($formats){
$buttons = '';
foreach( $formats as $name => $infos )
{
if ( isset($infos['name']) )
{
$buttons .= HTMLUtils::getHelperButtonFormat($name, $infos['name']) . PHP_EOL;
}
}
return $buttons;
}
public static function displayBridgeCard($bridgeName, $formats, $isActive = true)
{
$bridgeElement = Bridge::create($bridgeName);
if($bridgeElement == false) {
return "";
}
$bridgeElement->loadMetadatas();
$name = '<a href="'.$bridgeElement->uri.'">'.$bridgeElement->name.'</a>';
$description = $bridgeElement->description;
$card = <<<CARD
<section id="bridge-{$bridgeName}" data-ref="{$bridgeName}">
<h2>{$name}</h2>
<p class="description">
{$description}
</p>
CARD;
// If we don't have any parameter for the bridge, we print a generic form to load it.
if(count($bridgeElement->parameters) == 0) {
$card .= '<form method="POST" action="?">
<input type="hidden" name="action" value="display" />
<input type="hidden" name="bridge" value="' . $bridgeName . '" />' . PHP_EOL;
if ($isActive)
{
$card .= HTMLUtils::getHelperButtonsFormat($formats);
}
else
{
$card .= '<span style="font-weight: bold;">Inactive</span>';
}
$card .= '</form>' . PHP_EOL;
}
foreach($bridgeElement->parameters as $parameterName => $parameter)
{
$card .= '<ol class="list-use">' . PHP_EOL;
if(!is_numeric($parameterName)) {
$card .= '<h5>'.$parameterName.'</h5>' . PHP_EOL;
}
$card .= '<form method="POST" action="?">
<input type="hidden" name="action" value="display" />
<input type="hidden" name="bridge" value="' . $bridgeName . '" />' . PHP_EOL;
$parameter = json_decode($parameter, true);
foreach($parameter as $inputEntry) {
if(!isset($inputEntry['exampleValue'])) $inputEntry['exampleValue'] = "";
$idArg = 'arg-' . $bridgeName . '-' . $parameterName . '-' . $inputEntry['identifier'];
$card .= '<label for="' .$idArg. '">' .$inputEntry['name']. ' : </label>' . PHP_EOL;
if(!isset($inputEntry['type']) || $inputEntry['type'] == 'text') {
$card .= '<input id="' . $idArg . '" type="text" value="" placeholder="' . $inputEntry['exampleValue'] . '" name="' . $inputEntry['identifier'] . '" /><br />' . PHP_EOL;
} else if($inputEntry['type'] == 'number') {
$card .= '<input id="' . $idArg . '" type="number" value="" placeholder="' . $inputEntry['exampleValue'] . '" name="' . $inputEntry['identifier'] . '" /><br />' . PHP_EOL;
} else if($inputEntry['type'] == 'list') {
$card .= '<select id="' . $idArg . '" name="' . $inputEntry['name'] . '" >';
foreach($inputEntry['values'] as $listValues) {
$card .= "<option value='" . $listValues['value'] . "'>" . $listValues['name'] . "</option>";
}
$card .= '</select><br >';
} else if($inputEntry['type'] == 'checkbox') {
$card .= '<input id="' . $idArg . '" type="checkbox" name="' . $inputEntry['identifier'] . '" /><br />' . PHP_EOL;
}
}
if ($isActive)
{
$card .= HTMLUtils::getHelperButtonsFormat($formats);
}
else
{
$card .= '<span style="font-weight: bold;">Inactive</span>';
}
$card .= '</form>' . PHP_EOL;
}
$card .= '<span class="maintainer">'.$bridgeElement->maintainer.'</span>';
$card .= '</section>';
return $card;
}
}
?>

View file

@ -12,6 +12,7 @@ require __DIR__ . '/Item.php';
require __DIR__ . '/Format.php'; require __DIR__ . '/Format.php';
require __DIR__ . '/Bridge.php'; require __DIR__ . '/Bridge.php';
require __DIR__ . '/Cache.php'; require __DIR__ . '/Cache.php';
require __DIR__ . '/HTMLUtils.php';
$vendorLibSimpleHtmlDom = __DIR__ . PATH_VENDOR . '/simplehtmldom/simple_html_dom.php'; $vendorLibSimpleHtmlDom = __DIR__ . PATH_VENDOR . '/simplehtmldom/simple_html_dom.php';
if( !file_exists($vendorLibSimpleHtmlDom) ){ if( !file_exists($vendorLibSimpleHtmlDom) ){