Minor fixes

This commit is contained in:
2021-05-24 21:47:32 +02:00
parent 3b3066c83b
commit cc58284478
3 changed files with 138 additions and 140 deletions

View File

@@ -9,10 +9,10 @@ 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 $asChannel;
private $asItems;
/**
* Constructor
* @param array $asChannel Description of the feed: fields 'title', 'link' (optional), 'copyright', 'description', 'language', 'webMaster'
@@ -26,12 +26,12 @@ class Feed extends PhpObject
$asChannel['link'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'].'/rss';
}
$this->asChannel = $asChannel;
//Items
$this->asItems = array();
array_walk($asItems, array($this, 'addItem'));
}
public function loadRss($sUrl)
{
$oCurl = curl_init();
@@ -46,10 +46,10 @@ class Feed extends PhpObject
//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)
@@ -66,7 +66,7 @@ class Feed extends PhpObject
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])) {
@@ -75,7 +75,7 @@ class Feed extends PhpObject
if ($temp != '') $result['image_'.$imagetag] = $temp; // Set only if not empty
}
}*/
//Parse Items
preg_match_all("'<item(| .*?)>(.*?)</item>'si", $sRssContent, $asItems);
//$i = 0;
@@ -91,43 +91,43 @@ class Feed extends PhpObject
//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)
{
$this->asItems[] = $asItem;
return count($this->asItems) - 1;
}
public function removeItem($iItemId)
{
$bExist = array_key_exists($iItemId, $this->asItems);
if($bExist) unset($this->asItems[$iItemId]);
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;
@@ -141,7 +141,7 @@ class Feed extends PhpObject
}
return self::cleanRss(self::getDate($iGlobalPubDate));
}
public function getFeed($bSetMime=true)
{
//Feed Channel
@@ -154,20 +154,20 @@ class Feed extends PhpObject
$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'):'';
//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($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;
}
private static function getDate($sDate)
{
if(!is_numeric($sDate))
@@ -176,7 +176,7 @@ class Feed extends PhpObject
}
return date('r', $sDate);
}
private function buildItem($asItem)
{
$sRssItem = self::getHtml(self::cleanRss($asItem['title']), 'title');
@@ -188,22 +188,22 @@ class Feed extends PhpObject
$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 = '';
@@ -229,19 +229,17 @@ class Feed extends PhpObject
}
return $sHtml;
}
private static function cleanRss($oText)
{
$asForbiddenChars = array('&', '<', '>', '"', "'");
$asReplacementCode = array('&amp;', '&lt;', '&gt;', '&quot;', '&apos;');
if(!is_array($oText))
{
return str_replace($asForbiddenChars, $asReplacementCode, $oText);
return htmlspecialchars($oText, ENT_QUOTES);
}
elseif(count($oText)>0)
{
$oTextKeys = array_map(array($this, 'cleanRss'), array_keys($oText));
$oTextValues = array_map(array($this, 'cleanRss'), array_values($oText));
$oTextKeys = array_map(array(self, 'cleanRss'), array_keys($oText));
$oTextValues = array_map(array(self, 'cleanRss'), array_values($oText));
return array_combine($oTextKeys, $oTextValues);
}
else
@@ -249,7 +247,7 @@ class Feed extends PhpObject
return $oText;
}
}
private static function encapsulate($oText, $sPre='', $sPost=false, $sInter='')
{
if($sPost===false)
@@ -262,7 +260,7 @@ class Feed extends PhpObject
}
return $sPre.$oText.$sPost;
}
private static function rSortTimeMatrix($asMatrix, $sTimeCol)
{
$asResult = array();
@@ -271,4 +269,4 @@ class Feed extends PhpObject
foreach($asKeys as $iRowId=>$iTimeStamp) $asResult[$iRowId] = $asMatrix[$iRowId];
return $asResult;
}
}
}