<?php
class lastRSS
{
var$default_cp='utf-8';
var$CDATA='strip';
var$cp='';
var$items_limit=0;
var$stripHTML=true;
var$date_format='r';
var$gum_enable_curl=true;
var$gum_curl_timeout=5;
var$gum_allowed_tags='<br/>';
var$channeltags=['title','link','description','language','copyright','managingEditor','webMaster','lastBuildDate','rating','docs','name','email','subtitle'];
var$itemtags=['title','link','description','author','category','comments','enclosure','guid','pubDate','source','content','summary','published'];
var$item_tag_href_attributes=['alternate','via'];
var$imagetags=['title','url','link','width','height','logo'];
var$textinputtags=['title','description','name','link'];
function Get($rss_url)
{
if($this->cache_dir!='')
{
$cache_file=$this->cache_dir.'/rsscache_'.md5($rss_url);
$timedif=@($_SERVER['REQUEST_TIME']-filemtime($cache_file));
if($timedif<$this->cache_time)
{
$result=unserialize(join('',file($cache_file)));
if($result)
$result['cached']=1;
}
else
{
$result=$this->Parse($rss_url);
$serialized=serialize($result);
if($f=@fopen($cache_file,'w'))
{
fwrite($f,$serialized,mb_strlen($serialized));
fclose($f);
}
if($result)
$result['cached']=0;
}
}
else
{
$result=$this->Parse($rss_url);
if($result)
$result['cached']=0;
}
return$result;
}
function my_preg_match($pattern,$subject)
{
preg_match($pattern,$subject,$out);
if(isset($out[1]))
{
if($this->CDATA=='content')
$out[1]=strtr($out[1],['<![CDATA['=>'',']]>'=>'']);
elseif($this->CDATA=='strip')
$out[1]=strtr($out[1],['<![CDATA['=>'',']]>'=>'']);
if($this->cp!='')
$out[1]=iconv($this->rsscp,$this->cp.'//TRANSLIT',$out[1]);
return trim($out[1]);
}
else
return;
}
function unhtmlentities($string)
{
$trans_tbl=get_html_translation_table(HTML_ENTITIES,ENT_QUOTES);
$trans_tbl=array_flip($trans_tbl);
$trans_tbl+=['&apos;'=>"'"];
return strtr($string,$trans_tbl);
}
function Parse($rss_url)
{
if(mb_strlen($rss_url)>5)
{
$rss_content='';
if($this->gum_enable_curl==true)
{
$ch=curl_init();
curl_setopt($ch,CURLOPT_RETURNTRANSFER,$this->gum_curl_timeout);
curl_setopt($ch,CURLOPT_TIMEOUT,$this->gum_curl_timeout);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$this->gum_curl_timeout);
curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_0);
curl_setopt($ch,CURLOPT_URL,$rss_url);
$rss_content=curl_exec($ch);
curl_close($ch);
}
else
{
$f=@fopen($rss_url,'r');
while(!feof($f))
{
$rss_content.=fgets($f,4096);
}
fclose($f);
}
$result['encoding']=$this->my_preg_match("'encoding=[\'\"](.*?)[\'\"]'si",$rss_content);
if($result['encoding']!='')
$this->rsscp=$result['encoding'];
else
$this->rsscp=$this->default_cp;
if(preg_match("'<channel.*?>(.*?)</channel>'si",$rss_content,$out_channel)==false)
preg_match("'<feed.*?>(.*?)</feed>'si",$rss_content,$out_channel);
foreach($this->channeltags AS$channeltag)
{
$temp=$this->my_preg_match("'<$channeltag.*?>(.*?)</$channeltag>'si",$out_channel[1]);
if($temp!='')
$result[$channeltag]=$temp;
}
if($this->date_format!=''&&($timestamp=strtotime($result['lastBuildDate']))!==-1)
$result['lastBuildDate']=date($this->date_format,$timestamp);
###if($timestamp=strtotime($result['lastBuildDate'])!==-1)
###$result['lastBuildDate']=$timestamp;
preg_match("'<textinput(|[^>]*[^/])>(.*?)</textinput>'si",$rss_content,$out_textinfo);
if(isset($out_textinfo[2]))
{
foreach($this->textinputtags AS$textinputtag)
{
$temp=$this->my_preg_match("'<$textinputtag.*?>(.*?)</$textinputtag>'si",$out_textinfo[2]);
if($temp!='')
$result['textinput_'.$textinputtag]=$temp;
}
}
preg_match("'<image.*?>(.*?)</image>'si",$rss_content,$out_imageinfo);
if(isset($out_imageinfo[1]))
{
foreach($this->imagetags AS$imagetag)
{
$temp=$this->my_preg_match("'<$imagetag.*?>(.*?)</$imagetag>'si",$out_imageinfo[1]);
if($temp!='')
$result['image_'.$imagetag]=$temp;
}
}
if(preg_match_all("'<item(| .*?)>(.*?)</item>'si",$rss_content,$items)==false)
preg_match_all("'<entry(| .*?)>(.*?)</entry>'si",$rss_content,$items);
$rss_items=$items[2];
$i=0;
$result['items']=[];
foreach($rss_items AS$rss_item)
{
if($i<$this->items_limit||$this->items_limit==0)
{
foreach($this->itemtags AS$itemtag)
{
$temp=$this->my_preg_match("'<$itemtag.*?>(.*?)</$itemtag>'si",$rss_item);
if($temp!='')
$result['items'][$i][$itemtag]=$temp;
}
foreach($this->item_tag_href_attributes AS$attribute)
{
preg_match("'<.*?rel=[\'\"]alternate[\'\"].*?href=[\'\"]([^\'\"]*)[\'\"].*?[^>]>'si",$rss_item,$out_attribute);
if($out_attribute[1]!='')
$result['items'][$i][$attribute]=$out_attribute[1];
}
if($this->stripHTML&&$result['items'][$i]['description'])
$result['items'][$i]['description']=strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['description'],$this->gum_allowed_tags)),$this->gum_allowed_tags);
elseif($this->stripHTML&&$result['items'][$i]['content'])
$result['items'][$i]['content']=strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['content'],$this->gum_allowed_tags)),$this->gum_allowed_tags);
if($this->stripHTML&&$result['items'][$i]['title'])
$result['items'][$i]['title']=strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['title'])));
if($this->date_format!=''&&($timestamp=strtotime($result['items'][$i]['pubDate']))!==-1)
$result['items'][$i]['pubDate']=date($this->date_format,$timestamp);
elseif($this->date_format!=''&&($timestamp=strtotime($result['items'][$i]['published']))!==-1)
$result['items'][$i]['published']=date($this->date_format,$timestamp);
###if($timestamp=strtotime($result['items'][$i]['pubDate'])!==-1)
###$result['items'][$i]['pubDate']=$timestamp;
###elseif($timestamp=strtotime($result['items'][$i]['published'])!==-1)
###$result['items'][$i]['published']=$timestamp;
$result['items'][$i]['item_number']=$i+1;
$i++;
}
}
$result['items_count']=$i;
return$result;
}
else
return false;
}
}