<?php
/*
rssReader.php

ezgenerator Rss reader 0.21
http://www.ezgenerator.com

Copyright (c) 2004-2008 Image-line
*/

function GetFromString($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 GetFromStringAbi($src,$start,$stop){$res2=GetFromString($src,$start,$stop);return $start.$res2.$stop;}

function GetAllFromString($src,$start,$stop)
{
$res='';
while(strpos($src,$start)!==false):
 $temp=GetFromString($src,$start,$stop);$src=str_replace($start.$temp,'',$src);$res.=$temp;
endwhile;
return $res;
}

function GetAllFromStringAbi($src,$start,$stop)
{
$res='';
while(strpos($src,$start)!==false):
 $temp=GetFromStringAbi($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();

      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=GetAllFromStringAbi($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=GetAllFromString($result,'CDATA[',']]');};
 return $result;
}

function show_feed2($req)
{
 $rve_styles=array('33','25','17','1','9','8');
 $rss=new rssReader;
 $result='RSS feed empty!';
 $urls=explode(';',$req['url']);
 $etarget=(isset($req['etarget']))?$req['etarget']:'_blank';

 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!';

 if(isset($rs['items'])&&(count($rs['items'])>0))
 {
  $result='<table cellspacing="'.$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>':'';

  if($req['headeron']=='true')
  {
   $link=getCdata($rs['link']);
   $result.='<tr><td>';
   //main title
   if(isset($rs['title'])) $tit=encx(getCdata($rs['title']),false);else $tit='';
   if(isset($rs['img_title'])) $imatit=encx(getCdata($rs['img_title']),false);else $imatit='';
   $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($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;
  foreach($rs['items'] as $k=>$item)
  {
   if(!isset($rs_keys)||((array_search($k,$rs_keys)!==false)&&(array_search($k,$rs_keys)!== null)))
   {
    $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']);
    $result.=$al.'<a class="rvts'.$req['style'].'" target="'.$etarget.'" href="'.$itemlink.'">'.$tit.'</a><br>'.$ale;
    if(($req['dateon']=='true')&&(isset($item['pubDate'])))
      $result.=$al.'<span class="rvts'.$req['datestyle'].'">'.$item['pubDate'].'</span><br>'.$ale;

    $desc_on=true;
    if(isset($req['descon'])&&($req['descon']=='false')) $desc_on=false;
    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);$result.=encx($desc,true);}
     else
     {
      $desc=encx($desc,true);
      if (isset($req['descstyle']))
      {
       $dsst=$req['descstyle'];
       $result.=$al.'<span class="rvts'.$dsst.'">'.$desc.'</span>'.$ale;
      }
      else $result.=$al.$desc.$ale;
     }
    }
    $result.='</td></tr>';
    $counter++;
   }

   if(($req['max']>0)&&($req['max']==$counter)) break;
  }
  $result.='</table>';
  }
 return $result;
}

function process_feed()
{
 if(isset($_REQUEST['action'])){$result=show_feed2($_REQUEST);print "document.write('".$result."');";}
}

process_feed();
?>
