fixing SAP blogs

This commit is contained in:
francois.lutran
2016-12-16 17:19:15 +13:00
parent 7fecaa602f
commit 1bca2ec8ad
3 changed files with 166 additions and 71 deletions

View File

@@ -3,28 +3,95 @@
/**
* 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.0
*/
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;
public function __construct($asDesc, $asItems=array())
/**
* 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'));
}
public function loadRss($sUrl)
{
$sRssContent = file_get_contents($sUrl);
//Parse document encoding (useless)
$sEncoding = $this->getPregMatch("'encoding=[\'\"](.*?)[\'\"]'si", $sRssContent);
//Parse Channel info
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->asItems[] = $asItemTags;
}
}
public function addItem($asItem)
{
$this->asItems[] = $asItem;
@@ -38,12 +105,17 @@ class Feed extends PhpObject {
return $bExist;
}
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,24 +127,24 @@ class Feed extends PhpObject {
public function getFeed()
{
//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 = self::getHtml($this->asChannel['title'], 'title');
$sRssChannel .= self::getHtml($this->asChannel['link'], 'link');
$sRssChannel .= self::getHtml($this->asChannel['copyright'], 'copyright');
$sRssChannel .= self::getHtml($this->asChannel['description'], 'description');
$sRssChannel .= self::getHtml('', 'atom:link', '', '', array('href'=>$this->asChannel['link'], 'rel'=>'self', 'type'=>'application/atom+xml'), true);
$sRssChannel .= self::getHtml($this->asChannel['language'], 'language');
$sRssChannel .= self::getHtml($this->getGlobalPubDate(), 'lastBuildDate');
$sRssChannel .= self::getHtml('Lutran.fr RSS Feed Generator', 'generator');
$sRssChannel .= self::getHtml($this->asChannel['webMaster'].' (Webmaster)', 'webMaster');
//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.'" ?>';
$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'));
return $sFeed;
}
@@ -93,11 +165,26 @@ class Feed extends PhpObject {
$sRssItem .= self::getHtml($asItem['link'], 'link');
$sRssItem .= 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(self::getDate($asItem['pubDate']), 'pubDate');
$sRssItem .= 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 = '';