<?php
$version="EZGenerator Rss Reader 0.32";
/*
	rssReader.php
	http://www.ezgenerator.com
	Copyright (c) 2004-2009 Image-line
*/

function gfs($src,$start,$stop)
{
	if($start=='') $res=$src;
	else if(strpos($src,$start)===false){$res='';return $res;}
	else $res=substr($src,strpos($src,$start)+strlen($start));
	if(($stop!='')&&(strpos($res,$stop)!==false)) $res=substr($res,0,strpos($res,$stop));
	return $res;
}

function gfsAbi($src,$start,$stop){$res2=gfs($src,$start,$stop);return $start.$res2.$stop;}

function gfsALL($src,$start,$stop)
{
	$res='';
	while(strpos($src,$start)!==false):
		$temp=gfs($src,$start,$stop);$src=str_replace($start.$temp,'',$src);$res.=$temp;
	endwhile;
	return $res;
}

function gfsAllAbi($src,$start,$stop)
{
	$res='';
	$temp=gfsAbi($src,'<items>','</items>');
	if($temp!=='')$src=str_replace($temp,'',$src);
	while(strpos($src,$start)!==false):
		$temp=gfsAbi($src,$start,$stop);$src=str_replace($temp,'',$src);$res.=$temp;
	endwhile;
	return $res;
}

class rssReader {
	var $ch_tags=array('title','link','description','language','copyright','managingEditor','webMaster','lastBuildDate','rating','docs');
	var $it_tags=array('title','link','description','author','category','comments','enclosure','guid','pubDate','source');
	var $ima_tags=array('title','link','url','width','height');
	var $ti_tags=array('title','link','description','name');

function parse($url)
{
	$content='';$content=file_get_contents($url);
	if($content=='')
	{
		if ($fp=@fopen($url,'r'))
		{
			while(!feof($fp))$content.=fgets($fp,4096);
			fclose($fp);
		}
	}
	if($content!=='')
	{
		$counter=0;$result['charset']='';$result['items']=array();
		$atom=(strpos($content,'xmlns="http://www.w3.org/2005/Atom"')!==false)?true:false;
		if($atom) $content=str_replace(array('<entry','</entry>','<content','</content','<published','</published'),array('<item','</item>','<description','</description','<pubDate','</pubDate'),$content);        
		preg_match("'encoding=[\'\"](.*?)[\'\"]'si",$content,$matches);
		if(isset($matches[1])) $result['charset']=trim($matches[1]);
		$this->charset=($result['charset']!='')?$result['charset']:'UTF-8';
		$content_items=gfsAllAbi($content,'<item','</item>');
		$content=str_replace($content_items,'',$content);
		preg_match("'<channel.*?>(.*?)</channel>'si",$content,$res_channels);
		if (isset($res_channels[1]))
		{
			foreach($this->ch_tags as $cht)
			{
				preg_match("'<$cht.*?>(.*?)</$cht>'si",$res_channels[1],$matches);
				if((isset($matches[1])) && ($matches[1] != '')) $result[$cht]=trim($matches[1]);
			}
		}

		preg_match("'<textinput(|[^>]*[^/])>(.*?)</textinput>'si",$content,$res_text);
		if (isset($res_text[2]))
		{
			foreach($this->ti_tags as $ti)
			{
				preg_match("'<$ti.*?>(.*?)</$ti>'si",$res_text[2],$matches);
				if((isset($matches[1]))&&($matches[1] != '')) $result['textinput_'.$ti]=trim($matches[1]);
			}
		}

		preg_match("'<image.*?>(.*?)</image>'si",$content,$res_image);
		if(isset($res_image[1]))
		{
			foreach($this->ima_tags as $it)
			{
				preg_match("'<$it.*?>(.*?)</$it>'si",$res_image[1],$matches);
				if((isset($matches[1]))&&($matches[1]!='')) $result['img_'.$it]=trim($matches[1]);
			}
		}

		preg_match_all("'<item(| .*?)>(.*?)</item>'si",$content_items,$items);     
		$items=$items[2];

		foreach($items as $item)
		{
			foreach($this->it_tags as $itemtag)
			{
				preg_match("'<$itemtag.*?>(.*?)</$itemtag>'si",$item,$matches);
				if((isset($matches[1]))&&($matches[1]!='')) $result['items'][$counter][$itemtag]=trim($matches[1]);
			}
			$counter++;
		}
		$result['i_count']=$counter;
		return $result;
	}
	else return false;
 }
}

function encx($src,$q)
{
	$result=str_replace("\r\n"," ",$src);$result=str_replace("\n"," ",$result);$result=str_replace("\r"," ",$result);$result=str_replace("'","\'",$result);$result=str_replace("&amp;#","&#",$result);$result=str_replace("&amp;","&",$result);
	$result=str_replace("&#60;","<",$result);$result=str_replace("&lt;","<",$result);$result=str_replace("&gt;",">",$result);if($q)$result=str_replace("&quot;",'\"',$result);
	return $result;
}

function getCdata($src)
{
	$result=$src;
	if(strpos($src,'CDATA[')!==false) $result=gfsALL($result,'CDATA[',']]');
	if(strpos($result,'<script')!==false){$script=gfsAbi($src,'<script','script>');$result=str_replace($script,'',$result);}
	return $result;
}

function show_feed2($req)
{
	$rve_styles=array('33','25','17','1','9','8');
	$rss=new rssReader; 
	$urls=explode(';',$req['url']);
	$etarget=(isset($req['etarget']))?$req['etarget']:'_blank';
	$df=(isset($req['df']))?$req['df']:'';

	for($i=0;$i<(count($urls));$i++)
	{
		if($i==0) $rs=$rss->parse($urls[$i]);
		else {$rs1=$rss->parse($urls[$i]);$rs['items']=array_merge($rs['items'],$rs1['items']);}
	}

	if($rs===false)$result='RSS feed not found!';
	else
	{
		$result='<table cellspacing="'.(($req['cellspacing']=='')?0:$req['cellspacing']).'" style="width:'.$req['twidth'].'px;';
		if($req['bgcolor']!=='none') $result.='background:#'.$req['bgcolor'].';';
		if($req['tbstyle']!=='none') $result.=sprintf('border: %spx %s #%s;',$req['tbwidth'],$req['tbstyle'],$req['tbcolor']);
		$result.= '">';

		$ali=(isset($req['align']))?$req['align']:0;
		$al=($ali>0)?'<p class="rvps'.$ali.'">':'';
		$ale=($ali>0)?'</p>':'';
		$link=getCdata($rs['link']);

		if($req['headeron']=='true')
		{
			$result.='<tr><td>';
			//main title
			$tit=(isset($rs['title']))?encx(getCdata($rs['title']),false):'';
			$imatit=(isset($rs['img_title']))?encx(getCdata($rs['img_title']),false):'';
			$result.=$al.'<a class="rvts'.$rve_styles[$req['h']].'" target="'.$etarget.'" href="'.$link.'">'.$tit.'</a><br>'.$ale;
			//main image
			if(($req['ima']=='true')&&((isset($rs['img_url']))&&($rs['img_url']!= '')))
			{
				$imalink=getCdata($rs['img_link']);
				$result.='<a href="'.$imalink.'"><img src="'.$rs['img_url'].'" alt="'.$imatit.'" border="0"></a><br>';
			}

			if((isset($rs['description']))&&($rs['description']!==$rs['title']))
			{
				$desc=$rs['description'];
				if(strpos($desc,'CDATA[')!==false) {$desc=getCdata($desc);$result.=encx($desc,true);}
				else {$desc=encx($desc,true);$result.=$al.'<span class="rvts0">'.$desc.'</span>'.$ale;}
			}
			$result.='</td></tr>';
		}

		if(isset($rs['items'])&&(count($rs['items'])>0))
		{
			if(isset($req['rand'])&&($req['rand']=='true'))
				{shuffle($rs['items']);$rs_keys=array_rand($rs['items'],($req['max']>count($rs['items'])) ? count($rs['items']):$req['max']); }

			$counter=0;
			$desc_on=(isset($req['descon'])&&($req['descon']=='false'))?false:true;
			$title_on=(isset($req['titleon'])&&($req['titleon']=='false'))?false:true;
			$titlelink=(isset($req['titlelink'])&&($req['titlelink']=='false'))?false:true;

			$itc=count($rs['items']);    
			foreach($rs['items'] as $k=>$item)
			{
				if(!isset($rs_keys)||(is_array($rs_keys)&&(array_search($k,$rs_keys)!==false)&&(array_search($k,$rs_keys)!== null))||(!(is_array($rs_keys)) && ($rs_keys=$k || $itc==1)))
				{
					$result.='<tr><td style="';
					if($req['cbgcolor']!=='none') $result .= 'background-color:#'.$req['cbgcolor'].';';
					if($req['cbstyle']!=='none')
					{
						$result.=sprintf('border: %spx %s #%s;',$req['cbwidth'],$req['cbstyle'],$req['cbcolor']);
						if(isset($req['cbb'])&&($req['cbb']!==''))
						{
							if(strpos($req['cbb'],'t')===false)$result.='border-top-width:0px;';
							if(strpos($req['cbb'],'b')===false)$result.='border-bottom-width:0px;';
							if(strpos($req['cbb'],'l')===false)$result.='border-left-width:0px;';
							if(strpos($req['cbb'],'r')===false)$result.='border-right-width:0px;';
						}
					}
					$result.='">';

					$tit=getCdata($item['title']);$tit=encx($tit,false);
					$st=(int)$req['style']-4;
					if(isset($req['cnt'])&&($req['cnt']=='true')) $result.='<span class="rvts'.$st.'">'.strval($counter +1).'.  </span>';
					$itemlink=getCdata($item['link']);
					if($title_on) $result.=($titlelink)?$al.'<a class="rvts'.$req['style'].'" target="'.$etarget.'" href="'.$itemlink.'">'.$tit.'</a><br>'.$ale:'<span class="rvts'.$req['style'].'">'.$tit.'</span><br>'.$ale;
					if(($req['dateon']=='true')&&(isset($item['pubDate'])))
					{
						$pd=$item['pubDate'];
						if($df!=='') $pd=date($df,strtotime((string)($pd)));
						else if(strpos($pd,'.')!==false)
						{
							$pd=gfs($pd,'','.');
							$pd=str_replace('T',' ',$pd);
						}
						else
						{
							if(strpos($pd,'+')!==false)$pd=gfs($pd,'','+');
							if(strpos($pd,'-')!==false)$pd=gfs($pd,'','-');
						}
						$result.=$al.'<span class="rvts'.$req['datestyle'].'">'.$pd.'</span><br>'.$ale;
					}
//2008-12-29T12:34:00.006-05:00  atom  //Mon, 29 Dec 2008 14:24:10 +0000  rss
					if(isset($req['div'])&&($req['div']>0)) $result.='<p style="line-height:'.$req['div'].'px">&nbsp;</p>';

					if($desc_on && (isset($item['description'])))
					{
						$desc=$item['description'];
						if(strpos($desc,'CDATA[')!==false){$desc=getCdata($desc);$temp=encx($desc,true);}
						else
						{
							$desc=encx($desc,true);
							if(isset($req['descstyle'])) {$dsst=$req['descstyle'];$temp=$al.'<span class="rvts'.$dsst.'">'.$desc.'</span>'.$ale;}
							else $temp=$al.$desc.$ale;
						}
						$temp=str_replace('HTTP','http',$temp);
						$temp=str_replace(' src="http',' msrc="http',$temp);
						$temp=str_replace(' src="',' src="'.dirname($link).'/',$temp);
						$temp=str_replace(' msrc="http',' src="http',$temp);
						$result.=$temp;
					}
					$result.='</td></tr>';
					$counter++;
				}

				if(($req['max']>0)&&($req['max']==$counter)) break;
			}
		}
		$result.='</table>';
	}
	return $result;
}

function process_feed()
{
	if(isset($_REQUEST['action']))
	{
		if($_REQUEST['action']=="version") {echo $version;exit;}
		$result=show_feed2($_REQUEST);print "document.write('".$result."');";
	}
}

process_feed();
?>
