update rss

This commit is contained in:
2018-12-02 19:41:18 +01:00
parent 260bc06370
commit cc09e8002b
3 changed files with 153 additions and 44 deletions

View File

@@ -102,7 +102,8 @@ class PhpObject
$this->sChildClass = $sClass; $this->sChildClass = $sClass;
} }
public function setDebug($bDebug) /* private function, use Settings::DEBUG for global setting */
private function setDebug($bDebug)
{ {
$this->bDebug = $bDebug; $this->bDebug = $bDebug;
} }

View File

@@ -1,31 +1,102 @@
<?php <?php
/** /**
* RSS Feed Class * RSS Feed Class
* @author franzz * @author franzz
* @version 1.1 * @version 2.1
*
* Input:
* - $asDesc: Array. Description of the feed: fields 'title', 'link' (optional), 'copyright', 'description', 'language', 'webmaster_mail'
* - $asItems: Array. Feed item data: fields 'title', 'description', 'author', 'link', 'pub_date', 'guid'
*/ */
class Feed extends PhpObject { class Feed extends PhpObject
{
const CHANNEL_TAGS = array('title', 'link', 'copyright', 'description', 'language', 'lastBuildDate', 'generator', 'webMaster');
const ITEM_TAGS = array('title', 'author', 'link', 'category', 'description', 'pubDate', 'guid');
private $asDesc; private $asChannel;
private $asItems; private $asItems;
private $sCssFile;
public function __construct($asDesc, $asItems=array(), $sCssFile='') /**
* Constructor
* @param array $asChannel Description of the feed: fields 'title', 'link' (optional), 'copyright', 'description', 'language', 'webMaster'
* @param array $asItems Feed item data: fields 'title', 'author', 'link', 'category', 'description', 'pubDate', 'guid'
*/
public function __construct($asChannel=array(), $asItems=array())
{ {
parent::__construct(__CLASS__, Settings::DEBUG); parent::__construct(__CLASS__, Settings::DEBUG);
if(!array_key_exists('link', $asDesc)) if(!empty($asChannel) && !array_key_exists('link', $asChannel))
{ {
$asDesc['link'] = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'].'/rss'; $asChannel['link'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'].'/rss';
} }
$this->asDesc = $asDesc; $this->asChannel = $asChannel;
//Items
$this->asItems = array(); $this->asItems = array();
array_walk($asItems, array($this, 'addItem')); array_walk($asItems, array($this, 'addItem'));
$this->sCssFile = $sCssFile; }
public function loadRss($sUrl)
{
$oCurl = curl_init();
curl_setopt($oCurl, CURLOPT_URL, $sUrl);
curl_setopt($oCurl, CURLOPT_HEADER, false);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($oCurl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
//curl_setopt($oCurl, CURLOPT_ENCODING, 'gzip');
$sRssContent = curl_exec($oCurl);
curl_close($oCurl);
//Parse document encoding (useless)
$sEncoding = $this->getPregMatch("'encoding=[\'\"](.*?)[\'\"]'si", $sRssContent);
//Parse Channel info
if(preg_match("'<channel.*?>(.*?)</channel>'si", $sRssContent, $sChannelContent)) {
foreach(self::CHANNEL_TAGS as $sChannelTag)
{
$sTagContent = $this->getPregMatch("'<$sChannelTag.*?>(.*?)</$sChannelTag>'si", $sChannelContent[1]);
if($sTagContent != '') $this->asChannel[$sChannelTag] = $sTagContent;
}
}
//Parse Text Input info
/*preg_match("'<textinput(|[^>]*[^/])>(.*?)</textinput>'si", $sRssContent, $out_textinfo);
if (isset($out_textinfo[2])) {
foreach($this->textinputtags as $textinputtag) {
$temp = $this->getPregMatch("'<$textinputtag.*?>(.*?)</$textinputtag>'si", $out_textinfo[2]);
if ($temp != '') $result['textinput_'.$textinputtag] = $temp; // Set only if not empty
}
}*/
//Parse Image info
/*preg_match("'<image.*?>(.*?)</image>'si", $sRssContent, $out_imageinfo);
if (isset($out_imageinfo[1])) {
foreach($this->imagetags as $imagetag) {
$temp = $this->getPregMatch("'<$imagetag.*?>(.*?)</$imagetag>'si", $out_imageinfo[1]);
if ($temp != '') $result['image_'.$imagetag] = $temp; // Set only if not empty
}
}*/
//Parse Items
preg_match_all("'<item(| .*?)>(.*?)</item>'si", $sRssContent, $asItems);
//$i = 0;
foreach($asItems[2] as $asItem)
{
$asItemTags = array();
foreach(self::ITEM_TAGS as $sItemTag)
{
$sTagContent = $this->getPregMatch("'<$sItemTag.*?>(.*?)</$sItemTag>'si", $asItem);
if($sTagContent != '') $asItemTags[$sItemTag] = $sTagContent;
}
//Strip HTML tags and other bullshit from DESCRIPTION
//if($this->stripHTML && $this->asItems[$i]['description']) $this->asItems[$i]['description'] = strip_tags($this->unhtmlentities(strip_tags($this->asItems[$i]['description'])));
//Strip HTML tags and other bullshit from TITLE
//if($this->stripHTML && $this->asItems[$i]['title']) $this->asItems[$i]['title'] = strip_tags($this->unhtmlentities(strip_tags($this->asItems[$i]['title'])));
//Fix for author
if(!array_key_exists('author', $asItemTags))
{
$sTagContent = $this->getPregMatch("'<dc\:creator.*?>(.*?)</dc\:creator>'si", $asItem);
if($sTagContent != '') $asItemTags['author'] = $sTagContent;
}
$this->addItem($asItemTags);
}
} }
public function addItem($asItem) public function addItem($asItem)
@@ -41,12 +112,24 @@ class Feed extends PhpObject {
return $bExist; return $bExist;
} }
public function filterItems($sField, $sRegex)
{
$this->asItems = array_filter($this->asItems, function($asItem) use ($sField, $sRegex) {
return preg_match($sRegex, $asItem[$sField]);
});
}
public function getItems()
{
return $this->asItems;
}
private function getGlobalPubDate() private function getGlobalPubDate()
{ {
$iGlobalPubDate = 0; $iGlobalPubDate = 0;
foreach($this->asItems as $asItem) foreach($this->asItems as $asItem)
{ {
$iItemPubDate = strtotime($asItem['pub_date']); $iItemPubDate = strtotime($asItem['pubDate']);
if($iItemPubDate>$iGlobalPubDate) if($iItemPubDate>$iGlobalPubDate)
{ {
$iGlobalPubDate = $iItemPubDate; $iGlobalPubDate = $iItemPubDate;
@@ -55,31 +138,28 @@ class Feed extends PhpObject {
return self::cleanRss(self::getDate($iGlobalPubDate)); return self::cleanRss(self::getDate($iGlobalPubDate));
} }
public function getFeed() public function getFeed($bSetMime=true)
{ {
//Feed Channel
//feed header $sRssChannel = array_key_exists('title', $this->asChannel)?self::getHtml($this->asChannel['title'], 'title'):'';
$sRssFeedHeader = self::getHtml($this->asDesc['title'], 'title'); $sRssChannel .= array_key_exists('link', $this->asChannel)?self::getHtml($this->asChannel['link'], 'link'):'';
$sRssFeedHeader .= self::getHtml($this->asDesc['link'], 'link'); $sRssChannel .= array_key_exists('copyright', $this->asChannel)?self::getHtml($this->asChannel['copyright'], 'copyright'):'';
$sRssFeedHeader .= self::getHtml($this->asDesc['copyright'], 'copyright'); $sRssChannel .= array_key_exists('description', $this->asChannel)?self::getHtml($this->asChannel['description'], 'description'):'';
$sRssFeedHeader .= self::getHtml($this->asDesc['description'], 'description'); $sRssChannel .= array_key_exists('link', $this->asChannel)?self::getHtml('', 'atom:link', '', '', array('href'=>$this->asChannel['link'], 'rel'=>'self', 'type'=>'application/atom+xml'), true):'';
$sRssFeedHeader .= self::getHtml('', 'atom:link', '', '', array('href'=>$this->asDesc['link'], 'rel'=>'self', 'type'=>'application/atom+xml'), true); $sRssChannel .= array_key_exists('language', $this->asChannel)?self::getHtml($this->asChannel['language'], 'language'):'';
$sRssFeedHeader .= self::getHtml($this->asDesc['language'], 'language'); $sRssChannel .= self::getHtml($this->getGlobalPubDate(), 'lastBuildDate');
$sRssFeedHeader .= self::getHtml($this->getGlobalPubDate(), 'pubDate'); $sRssChannel .= self::getHtml('Lutran.fr RSS Feed Generator', 'generator');
$sRssFeedHeader .= self::getHtml('Lutran.fr RSS Feed Generator', 'generator'); $sRssChannel .= array_key_exists('webMaster', $this->asChannel)?self::getHtml($this->asChannel['webMaster'].' (Webmaster)', 'webMaster'):'';
$sRssFeedHeader .= self::getHtml($this->asDesc['webmaster_mail'].' (Webmaster)', 'webMaster');
//Alert on empty feed //Feed Items
if(empty($this->asItems)) $this->addError('Feed "'.$this->asDesc['title'].'" is empty'); $asSortedItems = $this->rSortTimeMatrix($this->asItems, 'pubDate');
//feed items
$asSortedItems = $this->rSortTimeMatrix($this->asItems, 'pub_date');
$sItems = implode("\n", array_map(array($this, 'buildItem'), $asSortedItems)); $sItems = implode("\n", array_map(array($this, 'buildItem'), $asSortedItems));
//Global Feed //Global Feed
$sFeed = '<?xml version="1.0" encoding="'.Settings::TEXT_ENC.'" ?>'; $sFeed = '<?xml version="1.0" encoding="'.Settings::TEXT_ENC.'" ?>';
if($this->sCssFile!='') $sFeed .= "\n".'<?xml-stylesheet type="text/css" href="'.$this->sCssFile.'"?>'; $sFeed .= self::getHtml(self::getHtml($sRssChannel.$sItems, 'channel'), 'rss', '', '', array('version'=>'2.0', 'xmlns:atom'=>'http://www.w3.org/2005/Atom'));
$sFeed .= self::getHtml(self::getHtml($sRssFeedHeader.$sItems, 'channel'), 'rss', '', '', array('version'=>'2.0', 'xmlns:atom'=>'http://www.w3.org/2005/Atom'));
if($bSetMime) header('Content-type: application/rss+xml');
return $sFeed; return $sFeed;
} }
@@ -96,15 +176,30 @@ class Feed extends PhpObject {
private function buildItem($asItem) private function buildItem($asItem)
{ {
$sRssItem = self::getHtml(self::cleanRss($asItem['title']), 'title'); $sRssItem = self::getHtml(self::cleanRss($asItem['title']), 'title');
$sRssItem .= self::getHtml(self::cleanRss($asItem['author']), 'author'); $sRssItem .= array_key_exists('author', $asItem)?self::getHtml(self::cleanRss($asItem['author']), 'author'):'';
$sRssItem .= self::getHtml($asItem['link'], 'link'); $sRssItem .= array_key_exists('link', $asItem)?self::getHtml($asItem['link'], 'link'):'';
$sRssItem .= self::getHtml($asItem['category'], 'category'); $sRssItem .= array_key_exists('category', $asItem)?self::getHtml($asItem['category'], 'category'):'';
$sRssItem .= self::getHtml(self::cleanRss($asItem['description']), 'description'); $sRssItem .= self::getHtml(self::cleanRss($asItem['description']), 'description');
$sRssItem .= self::getHtml(self::getDate($asItem['pub_date']), 'pubDate'); $sRssItem .= array_key_exists('pubDate', $asItem)?self::getHtml(self::getDate($asItem['pubDate']), 'pubDate'):'';
$sRssItem .= self::getHtml($asItem['guid'], 'guid', '', '', array('isPermaLink'=>'true')); $sRssItem .= array_key_exists('guid', $asItem)?self::getHtml($asItem['guid'], 'guid', '', '', array('isPermaLink'=>'true')):'';
return self::getHtml($sRssItem, 'item'); return self::getHtml($sRssItem, 'item');
} }
private function getPregMatch($pattern, $subject)
{
preg_match($pattern, $subject, $out);
//if there is some result... process it and return it
if(isset($out[1]))
{
//If code page is set convert character encoding to required
//if ($this->cp != '') $out[1] = iconv($this->rsscp, $this->cp.'//TRANSLIT', $out[1]);
return str_replace(array('<![CDATA[', ']]>'), '', trim($out[1]));
}
else return '';
}
private static function getHtml($oText, $sTag, $sClass='', $sStyle='', $asExtraAttr=array(), $bAutoClose=false, $sInter='') private static function getHtml($oText, $sTag, $sClass='', $sStyle='', $asExtraAttr=array(), $bAutoClose=false, $sInter='')
{ {
$sHtmlAttr = ''; $sHtmlAttr = '';
@@ -166,12 +261,10 @@ class Feed extends PhpObject {
private static function rSortTimeMatrix($asMatrix, $sTimeCol) private static function rSortTimeMatrix($asMatrix, $sTimeCol)
{ {
$asKeys = array();
$asResult = array(); $asResult = array();
foreach($asMatrix as $iRowId=>$asLine) $asKeys[$iRowId] = strtotime($asLine[$sTimeCol]); foreach($asMatrix as $iRowId=>$asLine) $asKeys[$iRowId] = strtotime($asLine[$sTimeCol]);
arsort($asKeys); arsort($asKeys);
foreach($asKeys as $iRowId=>$iTimeStamp) $asResult[$iRowId] = $asMatrix[$iRowId]; foreach($asKeys as $iRowId=>$iTimeStamp) $asResult[$iRowId] = $asMatrix[$iRowId];
return $asResult; return $asResult;
} }
} }
?>

View File

@@ -0,0 +1,15 @@
<?php
class Settings
{
const DEBUG = true;
const TEXT_ENC = 'UTF-8';
}
require_once '../../class_management.php';
require_once '../../inc/rss.php';
$oRss = new Feed();
$oRss->loadRss('https://rarbgmirror.org/rssdd_magnet.php?categories=17;42;44;45;46;48');
$oRss->filterItems('title', '/'.date('Y').'/i');
echo $oRss->getFeed();