update rss
This commit is contained in:
179
inc/rss.php
179
inc/rss.php
@@ -1,31 +1,102 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
/**
|
||||
* RSS Feed Class
|
||||
* @author franzz
|
||||
* @version 1.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'
|
||||
* @version 2.1
|
||||
*/
|
||||
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 $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);
|
||||
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();
|
||||
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)
|
||||
@@ -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 = '<?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($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('<![CDATA[', ']]>'), '', 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;
|
||||
}
|
||||
}
|
||||
?>
|
||||
}
|
||||
Reference in New Issue
Block a user