From cc09e8002b688fdfd2c8d1b3710bd5bacfd29a66 Mon Sep 17 00:00:00 2001 From: franzz Date: Sun, 2 Dec 2018 19:41:18 +0100 Subject: [PATCH] update rss --- class_management.php | 3 +- inc/rss.php | 179 ++++++++++++++++++++++++++-------- standalone/rarbg/rarbgrss.php | 15 +++ 3 files changed, 153 insertions(+), 44 deletions(-) create mode 100644 standalone/rarbg/rarbgrss.php diff --git a/class_management.php b/class_management.php index 921d143..d940ea8 100755 --- a/class_management.php +++ b/class_management.php @@ -102,7 +102,8 @@ class PhpObject $this->sChildClass = $sClass; } - public function setDebug($bDebug) + /* private function, use Settings::DEBUG for global setting */ + private function setDebug($bDebug) { $this->bDebug = $bDebug; } diff --git a/inc/rss.php b/inc/rss.php index 8942a5e..a3298ab 100755 --- a/inc/rss.php +++ b/inc/rss.php @@ -1,31 +1,102 @@ asDesc = $asDesc; + $this->asChannel = $asChannel; + + //Items $this->asItems = array(); 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("'(.*?)'si", $sRssContent, $sChannelContent)) { + foreach(self::CHANNEL_TAGS as $sChannelTag) + { + $sTagContent = $this->getPregMatch("'<$sChannelTag.*?>(.*?)'si", $sChannelContent[1]); + if($sTagContent != '') $this->asChannel[$sChannelTag] = $sTagContent; + } + } + //Parse Text Input info + /*preg_match("']*[^/])>(.*?)'si", $sRssContent, $out_textinfo); + if (isset($out_textinfo[2])) { + foreach($this->textinputtags as $textinputtag) { + $temp = $this->getPregMatch("'<$textinputtag.*?>(.*?)'si", $out_textinfo[2]); + if ($temp != '') $result['textinput_'.$textinputtag] = $temp; // Set only if not empty + } + }*/ + + //Parse Image info + /*preg_match("'(.*?)'si", $sRssContent, $out_imageinfo); + if (isset($out_imageinfo[1])) { + foreach($this->imagetags as $imagetag) { + $temp = $this->getPregMatch("'<$imagetag.*?>(.*?)'si", $out_imageinfo[1]); + if ($temp != '') $result['image_'.$imagetag] = $temp; // Set only if not empty + } + }*/ + + //Parse Items + preg_match_all("'(.*?)'si", $sRssContent, $asItems); + //$i = 0; + foreach($asItems[2] as $asItem) + { + $asItemTags = array(); + foreach(self::ITEM_TAGS as $sItemTag) + { + $sTagContent = $this->getPregMatch("'<$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("'(.*?)'si", $asItem); + if($sTagContent != '') $asItemTags['author'] = $sTagContent; + } + + $this->addItem($asItemTags); + } } public function addItem($asItem) @@ -41,12 +112,24 @@ class Feed extends PhpObject { 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() { $iGlobalPubDate = 0; foreach($this->asItems as $asItem) { - $iItemPubDate = strtotime($asItem['pub_date']); + $iItemPubDate = strtotime($asItem['pubDate']); if($iItemPubDate>$iGlobalPubDate) { $iGlobalPubDate = $iItemPubDate; @@ -55,31 +138,28 @@ class Feed extends PhpObject { return self::cleanRss(self::getDate($iGlobalPubDate)); } - public function getFeed() + public function getFeed($bSetMime=true) { - - //feed header - $sRssFeedHeader = self::getHtml($this->asDesc['title'], 'title'); - $sRssFeedHeader .= self::getHtml($this->asDesc['link'], 'link'); - $sRssFeedHeader .= self::getHtml($this->asDesc['copyright'], 'copyright'); - $sRssFeedHeader .= self::getHtml($this->asDesc['description'], 'description'); - $sRssFeedHeader .= self::getHtml('', 'atom:link', '', '', array('href'=>$this->asDesc['link'], 'rel'=>'self', 'type'=>'application/atom+xml'), true); - $sRssFeedHeader .= self::getHtml($this->asDesc['language'], 'language'); - $sRssFeedHeader .= self::getHtml($this->getGlobalPubDate(), 'pubDate'); - $sRssFeedHeader .= self::getHtml('Lutran.fr RSS Feed Generator', 'generator'); - $sRssFeedHeader .= self::getHtml($this->asDesc['webmaster_mail'].' (Webmaster)', 'webMaster'); + //Feed Channel + $sRssChannel = array_key_exists('title', $this->asChannel)?self::getHtml($this->asChannel['title'], 'title'):''; + $sRssChannel .= array_key_exists('link', $this->asChannel)?self::getHtml($this->asChannel['link'], 'link'):''; + $sRssChannel .= array_key_exists('copyright', $this->asChannel)?self::getHtml($this->asChannel['copyright'], 'copyright'):''; + $sRssChannel .= array_key_exists('description', $this->asChannel)?self::getHtml($this->asChannel['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):''; + $sRssChannel .= array_key_exists('language', $this->asChannel)?self::getHtml($this->asChannel['language'], 'language'):''; + $sRssChannel .= self::getHtml($this->getGlobalPubDate(), 'lastBuildDate'); + $sRssChannel .= self::getHtml('Lutran.fr RSS Feed Generator', 'generator'); + $sRssChannel .= array_key_exists('webMaster', $this->asChannel)?self::getHtml($this->asChannel['webMaster'].' (Webmaster)', 'webMaster'):''; - //Alert on empty feed - if(empty($this->asItems)) $this->addError('Feed "'.$this->asDesc['title'].'" is empty'); - - //feed items - $asSortedItems = $this->rSortTimeMatrix($this->asItems, 'pub_date'); + //Feed Items + $asSortedItems = $this->rSortTimeMatrix($this->asItems, 'pubDate'); $sItems = implode("\n", array_map(array($this, 'buildItem'), $asSortedItems)); - + //Global Feed $sFeed = ''; - if($this->sCssFile!='') $sFeed .= "\n".'sCssFile.'"?>'; - $sFeed .= self::getHtml(self::getHtml($sRssFeedHeader.$sItems, 'channel'), 'rss', '', '', array('version'=>'2.0', 'xmlns:atom'=>'http://www.w3.org/2005/Atom')); + $sFeed .= self::getHtml(self::getHtml($sRssChannel.$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; } @@ -96,15 +176,30 @@ class Feed extends PhpObject { private function buildItem($asItem) { $sRssItem = self::getHtml(self::cleanRss($asItem['title']), 'title'); - $sRssItem .= self::getHtml(self::cleanRss($asItem['author']), 'author'); - $sRssItem .= self::getHtml($asItem['link'], 'link'); - $sRssItem .= self::getHtml($asItem['category'], 'category'); + $sRssItem .= array_key_exists('author', $asItem)?self::getHtml(self::cleanRss($asItem['author']), 'author'):''; + $sRssItem .= array_key_exists('link', $asItem)?self::getHtml($asItem['link'], 'link'):''; + $sRssItem .= array_key_exists('category', $asItem)?self::getHtml($asItem['category'], 'category'):''; $sRssItem .= self::getHtml(self::cleanRss($asItem['description']), 'description'); - $sRssItem .= self::getHtml(self::getDate($asItem['pub_date']), 'pubDate'); - $sRssItem .= self::getHtml($asItem['guid'], 'guid', '', '', array('isPermaLink'=>'true')); + $sRssItem .= array_key_exists('pubDate', $asItem)?self::getHtml(self::getDate($asItem['pubDate']), 'pubDate'):''; + $sRssItem .= array_key_exists('guid', $asItem)?self::getHtml($asItem['guid'], 'guid', '', '', array('isPermaLink'=>'true')):''; 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(''), '', trim($out[1])); + } + else return ''; + } + private static function getHtml($oText, $sTag, $sClass='', $sStyle='', $asExtraAttr=array(), $bAutoClose=false, $sInter='') { $sHtmlAttr = ''; @@ -166,12 +261,10 @@ class Feed extends PhpObject { private static function rSortTimeMatrix($asMatrix, $sTimeCol) { - $asKeys = array(); $asResult = array(); foreach($asMatrix as $iRowId=>$asLine) $asKeys[$iRowId] = strtotime($asLine[$sTimeCol]); arsort($asKeys); foreach($asKeys as $iRowId=>$iTimeStamp) $asResult[$iRowId] = $asMatrix[$iRowId]; return $asResult; } -} -?> \ No newline at end of file +} \ No newline at end of file diff --git a/standalone/rarbg/rarbgrss.php b/standalone/rarbg/rarbgrss.php new file mode 100644 index 0000000..4b50f2f --- /dev/null +++ b/standalone/rarbg/rarbgrss.php @@ -0,0 +1,15 @@ +loadRss('https://rarbgmirror.org/rssdd_magnet.php?categories=17;42;44;45;46;48'); +$oRss->filterItems('title', '/'.date('Y').'/i'); +echo $oRss->getFeed(); \ No newline at end of file