2016-09-05 18:05:19 +02:00
|
|
|
<?php
|
|
|
|
require_once(__DIR__ . '/BridgeInterface.php');
|
2016-09-10 19:11:09 +02:00
|
|
|
abstract class FeedExpander extends BridgeAbstract {
|
2016-09-05 18:05:19 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
private $name;
|
|
|
|
private $uri;
|
2016-09-12 10:38:50 +02:00
|
|
|
private $feedType;
|
2016-09-10 20:41:11 +02:00
|
|
|
|
|
|
|
public function collectExpandableDatas($url, $maxItems = -1){
|
2017-07-29 19:28:00 +02:00
|
|
|
if(empty($url)) {
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('There is no $url for this RSS expander');
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
|
2016-09-25 23:22:33 +02:00
|
|
|
debugMessage('Loading from ' . $url);
|
2016-09-10 20:41:11 +02:00
|
|
|
|
|
|
|
/* Notice we do not use cache here on purpose:
|
|
|
|
* we want a fresh view of the RSS stream each time
|
|
|
|
*/
|
2016-09-25 23:22:33 +02:00
|
|
|
$content = getContents($url)
|
|
|
|
or returnServerError('Could not request ' . $url);
|
2017-08-10 13:20:32 +02:00
|
|
|
$rssContent = simplexml_load_string(trim($content));
|
2016-09-10 20:41:11 +02:00
|
|
|
|
2016-09-25 23:22:33 +02:00
|
|
|
debugMessage('Detecting feed format/version');
|
2017-07-29 19:28:00 +02:00
|
|
|
switch(true) {
|
2016-09-12 10:38:50 +02:00
|
|
|
case isset($rssContent->item[0]):
|
2016-09-25 23:22:33 +02:00
|
|
|
debugMessage('Detected RSS 1.0 format');
|
2018-06-29 23:55:33 +02:00
|
|
|
$this->feedType = 'RSS_1_0';
|
2016-09-12 10:38:50 +02:00
|
|
|
break;
|
|
|
|
case isset($rssContent->channel[0]):
|
2016-09-25 23:22:33 +02:00
|
|
|
debugMessage('Detected RSS 0.9x or 2.0 format');
|
2018-06-29 23:55:33 +02:00
|
|
|
$this->feedType = 'RSS_2_0';
|
2016-09-12 10:38:50 +02:00
|
|
|
break;
|
|
|
|
case isset($rssContent->entry[0]):
|
2016-09-25 23:22:33 +02:00
|
|
|
debugMessage('Detected ATOM format');
|
2018-06-29 23:55:33 +02:00
|
|
|
$this->feedType = 'ATOM_1_0';
|
2016-09-12 10:38:50 +02:00
|
|
|
break;
|
|
|
|
default:
|
2016-09-25 23:22:33 +02:00
|
|
|
debugMessage('Unknown feed format/version');
|
|
|
|
returnServerError('The feed format is unknown!');
|
2016-09-12 10:38:50 +02:00
|
|
|
break;
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2016-09-12 10:38:50 +02:00
|
|
|
|
2016-09-25 23:22:33 +02:00
|
|
|
debugMessage('Calling function "collect_' . $this->feedType . '_data"');
|
2016-09-17 18:16:25 +02:00
|
|
|
$this->{'collect_' . $this->feedType . '_data'}($rssContent, $maxItems);
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function collect_RSS_1_0_data($rssContent, $maxItems){
|
|
|
|
$this->load_RSS_2_0_feed_data($rssContent->channel[0]);
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($rssContent->item as $item) {
|
2016-09-25 23:22:33 +02:00
|
|
|
debugMessage('parsing item ' . var_export($item, true));
|
2017-06-11 12:58:53 +02:00
|
|
|
$tmp_item = $this->parseItem($item);
|
|
|
|
if (!empty($tmp_item)) {
|
|
|
|
$this->items[] = $tmp_item;
|
|
|
|
}
|
2016-09-10 20:41:11 +02:00
|
|
|
if($maxItems !== -1 && count($this->items) >= $maxItems) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function collect_RSS_2_0_data($rssContent, $maxItems){
|
|
|
|
$rssContent = $rssContent->channel[0];
|
2016-09-25 23:22:33 +02:00
|
|
|
debugMessage('RSS content is ===========\n'
|
2016-09-10 20:41:11 +02:00
|
|
|
. var_export($rssContent, true)
|
|
|
|
. '===========');
|
|
|
|
|
|
|
|
$this->load_RSS_2_0_feed_data($rssContent);
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($rssContent->item as $item) {
|
2016-09-25 23:22:33 +02:00
|
|
|
debugMessage('parsing item ' . var_export($item, true));
|
2017-06-11 12:58:53 +02:00
|
|
|
$tmp_item = $this->parseItem($item);
|
|
|
|
if (!empty($tmp_item)) {
|
|
|
|
$this->items[] = $tmp_item;
|
|
|
|
}
|
2016-09-10 20:41:11 +02:00
|
|
|
if($maxItems !== -1 && count($this->items) >= $maxItems) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 10:38:50 +02:00
|
|
|
protected function collect_ATOM_1_0_data($content, $maxItems){
|
2016-09-10 20:41:11 +02:00
|
|
|
$this->load_ATOM_feed_data($content);
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($content->entry as $item) {
|
2016-09-25 23:22:33 +02:00
|
|
|
debugMessage('parsing item ' . var_export($item, true));
|
2017-06-11 12:58:53 +02:00
|
|
|
$tmp_item = $this->parseItem($item);
|
|
|
|
if (!empty($tmp_item)) {
|
|
|
|
$this->items[] = $tmp_item;
|
|
|
|
}
|
2016-09-10 20:41:11 +02:00
|
|
|
if($maxItems !== -1 && count($this->items) >= $maxItems) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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){
|
2017-03-13 22:11:20 +01:00
|
|
|
$this->name = trim((string)$rssContent->title);
|
|
|
|
$this->uri = trim((string)$rssContent->link);
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function load_ATOM_feed_data($content){
|
2017-03-13 22:11:20 +01:00
|
|
|
$this->name = (string)$content->title;
|
2016-09-10 20:41:11 +02:00
|
|
|
|
|
|
|
// Find best link (only one, or first of 'alternate')
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!isset($content->link)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
$this->uri = '';
|
2017-07-29 19:28:00 +02:00
|
|
|
} elseif (count($content->link) === 1) {
|
2017-08-10 13:35:17 +02:00
|
|
|
$this->uri = (string)$content->link[0]['href'];
|
2016-09-10 20:41:11 +02:00
|
|
|
} else {
|
|
|
|
$this->uri = '';
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($content->link as $link) {
|
|
|
|
if(strtolower($link['rel']) === 'alternate') {
|
2017-08-10 13:35:17 +02:00
|
|
|
$this->uri = (string)$link['href'];
|
2016-09-10 20:41:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function parseATOMItem($feedItem){
|
|
|
|
$item = array();
|
2016-10-02 18:07:56 +02:00
|
|
|
if(isset($feedItem->id)) $item['uri'] = (string)$feedItem->id;
|
|
|
|
if(isset($feedItem->title)) $item['title'] = (string)$feedItem->title;
|
|
|
|
if(isset($feedItem->updated)) $item['timestamp'] = strtotime((string)$feedItem->updated);
|
|
|
|
if(isset($feedItem->author)) $item['author'] = (string)$feedItem->author->name;
|
|
|
|
if(isset($feedItem->content)) $item['content'] = (string)$feedItem->content;
|
2016-09-10 20:41:11 +02:00
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function parseRSS_0_9_1_Item($feedItem){
|
|
|
|
$item = array();
|
2016-10-02 18:07:56 +02:00
|
|
|
if(isset($feedItem->link)) $item['uri'] = (string)$feedItem->link;
|
|
|
|
if(isset($feedItem->title)) $item['title'] = (string)$feedItem->title;
|
2016-09-10 20:41:11 +02:00
|
|
|
// rss 0.91 doesn't support timestamps
|
|
|
|
// rss 0.91 doesn't support authors
|
2016-10-02 18:07:56 +02:00
|
|
|
if(isset($feedItem->description)) $item['content'] = (string)$feedItem->description;
|
2016-09-10 20:41:11 +02:00
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function parseRSS_1_0_Item($feedItem){
|
|
|
|
// 1.0 adds optional elements around the 0.91 standard
|
|
|
|
$item = $this->parseRSS_0_9_1_Item($feedItem);
|
|
|
|
|
|
|
|
$namespaces = $feedItem->getNamespaces(true);
|
2017-07-29 19:28:00 +02:00
|
|
|
if(isset($namespaces['dc'])) {
|
2016-09-10 20:41:11 +02:00
|
|
|
$dc = $feedItem->children($namespaces['dc']);
|
2016-10-02 18:07:56 +02:00
|
|
|
if(isset($dc->date)) $item['timestamp'] = strtotime((string)$dc->date);
|
|
|
|
if(isset($dc->creator)) $item['author'] = (string)$dc->creator;
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function parseRSS_2_0_Item($feedItem){
|
|
|
|
// Primary data is compatible to 0.91 with some additional data
|
|
|
|
$item = $this->parseRSS_0_9_1_Item($feedItem);
|
|
|
|
|
|
|
|
$namespaces = $feedItem->getNamespaces(true);
|
|
|
|
if(isset($namespaces['dc'])) $dc = $feedItem->children($namespaces['dc']);
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(isset($feedItem->guid)) {
|
|
|
|
foreach($feedItem->guid->attributes() as $attribute => $value) {
|
2016-09-17 20:43:05 +02:00
|
|
|
if($attribute === 'isPermaLink'
|
2017-07-29 19:28:00 +02:00
|
|
|
&& ($value === 'true' || filter_var($feedItem->guid, FILTER_VALIDATE_URL))) {
|
2016-10-02 18:07:56 +02:00
|
|
|
$item['uri'] = (string)$feedItem->guid;
|
2016-09-14 13:36:44 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(isset($feedItem->pubDate)) {
|
2016-10-02 18:07:56 +02:00
|
|
|
$item['timestamp'] = strtotime((string)$feedItem->pubDate);
|
2017-07-29 19:28:00 +02:00
|
|
|
} elseif(isset($dc->date)) {
|
2016-10-02 18:07:56 +02:00
|
|
|
$item['timestamp'] = strtotime((string)$dc->date);
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2017-07-29 19:28:00 +02:00
|
|
|
if(isset($feedItem->author)) {
|
2016-10-02 18:07:56 +02:00
|
|
|
$item['author'] = (string)$feedItem->author;
|
2017-07-29 19:28:00 +02:00
|
|
|
} elseif(isset($dc->creator)) {
|
2016-10-02 18:07:56 +02:00
|
|
|
$item['author'] = (string)$dc->creator;
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
*/
|
2016-09-12 10:39:34 +02:00
|
|
|
protected function parseItem($item){
|
2017-07-29 19:28:00 +02:00
|
|
|
switch($this->feedType) {
|
2016-09-12 10:39:34 +02:00
|
|
|
case 'RSS_1_0':
|
|
|
|
return $this->parseRSS_1_0_Item($item);
|
|
|
|
break;
|
|
|
|
case 'RSS_2_0':
|
|
|
|
return $this->parseRSS_2_0_Item($item);
|
|
|
|
break;
|
|
|
|
case 'ATOM_1_0':
|
|
|
|
return $this->parseATOMItem($item);
|
|
|
|
break;
|
2016-09-25 23:22:33 +02:00
|
|
|
default: returnClientError('Unknown version ' . $this->getInput('version') . '!');
|
2016-09-12 10:39:34 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-10 20:41:11 +02:00
|
|
|
|
|
|
|
public function getURI(){
|
2017-02-14 22:36:33 +01:00
|
|
|
return $this->uri ?: parent::getURI();
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getName(){
|
2017-02-14 22:20:55 +01:00
|
|
|
return $this->name ?: parent::getName();
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2018-08-21 17:46:47 +02:00
|
|
|
|
|
|
|
public function getIcon(){
|
|
|
|
return $this->icon ?: parent::getIcon();
|
|
|
|
}
|
2016-09-05 18:05:19 +02:00
|
|
|
}
|