Location: PHPKode > projects > News Manager - RSS aggregator > newsmanager/channel_items.php
<?php
	/*******************************************************************************
	 **  File          :  channel_items.php                                       **
	 **  Author        :  G.Ramkumar                                              **
	 **  Description   :  Fetching The channel items Information from Xml Url     **
	 **  Date          :  14/07/2005                                              **
	 **  Last Modified :  02/08/2005                                              **
	 **  PHP Version   :  5.0.3                                                   **
	 **                                                                           **
     *******************************************************************************/
set_time_limit(0);
include('date.php');
include('db/dbconnect.php');
include('del_archive_news.php');
//*******including variables*****

$rssflag = false;
$rsstag = "";
$rsstitle = "";
$rssdescription ="";
$rsslink = "";
$rsspubdate="";
$rsscategory='';
$rssurl="";

// Including variables in array for inserting the news items

$rssnews_title=array();
$rssnews_desc=array();
$rssnews_link=array();
$rssnews_last=array();
$rssnews_pub=array();
$rssnews_imgurl=array();
$rssnews_category=array();

function rssstartElement($parser,$tagname)
{
	global $rssflag, $rsstag, $rsstitle, $rssdescription, $rsslink ,$rsspubdate,$rssurl,$rsscategory;

	if ($rssflag)
	{
		$rsstag = $tagname;
	}
	elseif ($tagname == "ITEM" || $tagname == "ENTRY")
	{
       $rssflag = true;
    }

}

function rssendElement($parser, $tagname)
{
	global $rssflag, $rsstag, $rsstitle, $rssdescription, $rsslink,$rsspubdate,$rssurl,$rsscategory;
    global $rssnews_title,$rssnews_desc,$rssnews_link,$rssnews_pub,$rssnews_last,$rssnews_category;

	if ($tagname == "ITEM" || $tagname == "ENTRY")
	{
			   $rssnews_title[]=htmlspecialchars(trim($rsstitle));
			   $rssnews_desc[]=htmlspecialchars(trim($rssdescription));
			   $rssnews_link[]=htmlspecialchars(trim($rsslink));
			   $rssnews_pub[]=htmlspecialchars(trim($rsspubdate));
			   $rssnews_last[]=date("Y-n-j");
			   $rssnews_imgurl[]=trim($rssurl);
			   $rssnews_category[]=htmlspecialchars(trim($rsscategory));

		//*******Emptying the variables...********

				$rsstitle = "";
				$rssdescription = "";
				$rsslink = "";
				$rsspubdate="";
				$rssurl="";
				$rsscategory='';
				$rssflag = false;
	}

}

function rsscharacterData($parser, $data)
{
	global $rssflag, $rsstag, $rsstitle, $rssdescription, $rsslink,$rsspubdate,$rssurl,$rsscategory;
    $data=trim($data);

	 if($rssflag)
	 {
	   switch ($rsstag)
	    {
			case "TITLE":
				$rsstitle .= $data;
				break;
		    case "DESCRIPTION":
				$rssdescription .=$data;
				break;
			case "LINK":
				$rsslink .= $data;
				break;
			case "PUBDATE":
			    $rsspubdate .=$data;
			    break;
			case "DC:DATE":
				$rsspubdate .=$data;
			    break;
			case "URL":
			    $rssurl .=$data;
			    break;
			case "CATEGORY":
			    $rsscategory .=($rsscategory? ',':'').$data;
			    break;
		 }

	  }

}

$chan_qry="select * from rsschannel where channel_status='Y' order by channel_id desc";
$chan_qry_res=$pg_sql->query_pass($chan_qry,"4");

for($n=0;$n<count($chan_qry_res);$n++)
{
	$chan_id=$chan_qry_res[$n]['channel_id'];
    $author=$chan_qry_res[$n]['channel_author'];
	$copyright=$chan_qry_res[$n]['channel_copyright'];
	$webmaster=$chan_qry_res[$n]['channel_webmaster'];
	$rssfront_end=$chan_qry_res[$n]['channel_status'];
	$langu_new=$chan_qry_res[$n]['channel_language'];

	$file=$chan_qry_res[$n]['channel_url'];

	// *** For Getting Enclosures

	$url=@file_get_contents($file);
	$content=htmlentities($url);
	$lines=explode("&lt;/item&gt;",$content);

		for($e=0;$e<count($lines);$e++)
		{
		 $startstr=strstr($lines[$e],"&lt;enclosure");
		 $endstr=explode("/&gt",$startstr);
		 if($e<count($lines)-1)
			 $enclosed[]=$endstr[0]."/>";
		}
		$link_lines_con=strstr($content,"&lt;entry&gt;");
		$link_lines_clo=explode("&lt;/entry&gt;",$link_lines_con);

		for($l=0;$l<count($link_lines_clo);$l++)
			{
			 $startstr_=strstr($link_lines_clo[$l],"&lt;link rel=");
			 $endstr_=explode("/&gt",$startstr_);
			 if($l<count($link_lines_clo)-1)
			   $link_new[]=$endstr_[0]."/>";
			}
		  if((strstr($url,"<rss")|| strstr($url,"<rdf") || strstr($url,"<feed")) && !strstr($url,"<html")){
				$xml_parser = xml_parser_create();
				xml_set_element_handler($xml_parser, "rssstartElement", "rssendElement");
				xml_set_character_data_handler($xml_parser, "rsscharacterData");

				$fp = fopen($file,"r")
					or die("Error reading RSS data.");
				while ($data = fread($fp, 4096))
					xml_parse($xml_parser, $data, feof($fp))
						or die(sprintf("XML error: %s at line %d",
							xml_error_string(xml_get_error_code($xml_parser)),
							xml_get_current_line_number($xml_parser)));
				fclose($fp);

				xml_parser_free($xml_parser);
		  }

 $itemcnt=count($rssnews_title);
// $ex_itemcnt=count($rssch_item_res);

 $rsshour=date("H:m");

	for($i_=0;$i_<$itemcnt;$i_++)
	{
	  $rssnews_title[$i_]=str_replace("'","||",$rssnews_title[$i_]);
	  $rssnews_desc[$i_]=str_replace('"',"",$rssnews_desc[$i_]);
	  $rssnews_desc[$i_]=str_replace("'","||",$rssnews_desc[$i_]);

			  $arr1 = array("&Atilde;","&pound;","&Acirc;","&micro;","&frac14;","&middot;","&acute;","&sup3;","&brvbar;","&para;","&reg;","&macr;","&yen;","&uml;","&deg;","&iquest;","&Auml;","&iexcl;","&cent;",
					 "&curren;","&copy;","&sect;","&ordf;","&laquo;","&not;","&plusmn;","&sup2;","&sup1;","&cedil;",
					 "&ordm;","&raquo;","&frac12;","&frac34;","&Agrave;","&Aacute;","&Aring;","&AElig;","&Ccedil;",
					 "&Egrave;","&Eacute;","&Ecirc;","&Euml;","&Igrave;","&Iacute;","&Icirc;","&Iuml;","&ETH;","&Ntilde;",
					 "&Ograve;","&Oacute;","&Ocirc;","&Otilde;","&Ouml;","&times;","&Oslash;","&Ugrave;","&Uacute;","&Ucirc;",
					 "&Uuml;","&Yacute;","&THORN;","&szlig;","&agrave;","&aacute;","&atilde;","&auml;","&aring;","&aelig;",
					 "&ccedil;","&egrave;","&eacute;","&ecirc;","&igrave;","&iacute;","&icirc;","&iuml;","&eth;","&ntilde;","&ograve;",
					 "&oacute;","&otilde;","&ouml;","&divide;","&oslash;","&ugrave;","&uacute;","&ucirc;","&yacute;","&thorn;","&yuml;","&euro;","&shy;","&acirc;","&nbsp;");

			   $arr2 = array("&#195;","&#163;","&#194;","&#181;","&#188;","&#183;","&#180;","&#179;","&#166;","&#182;","&#174;","&#175;","&#165;","&#168;","&#160;","&#191;","&#196;","&#161;","&#162;","&#164;",
					 "&#169;","&#167;","&#170;","&#171;","&#172;","&#177;","&#178;","&#185;","&#184;","&#186;","&#187;","&#189;",
					 "&#190;","&#192;","&#193;","&#194;","&#197;","&#198;","&#199;","&#200;","&#201;","&#202;","&#203;","&#204;",
					 "&#205;","&#206;","&#207;","&#208;","&#209;","&#210;","&#211;","&#212;","&#213;","&#214;","&#215;","&#216;",
					 "&#217;","&#218;","&#219;","&#220;","&#221;","&#222;","&#223;","&#224;","&#225;","&#227;","&#228;","&#229;",
					 "&#230;","&#231;","&#232;","&#233;","&#234;","&#235;","&#236;","&#237;","&#238;","&#239;","&#240;","&#241;","&#242;",
					 "&#243;","&#244;","&#245;","&#246;","&#247;","&#248;","&#249;","&#250;","&#251;","&#253;","&#254;","&#255;","&#8364;","&#173;","&#226;","&#176;");


					   $j_=0;
					   foreach ($arr1 as $element)
						{
							$rssnews_title[$i_]=str_replace($element,$arr2[$j_],$rssnews_title[$i_]);
							$rssnews_desc[$i_] = str_replace($element,$arr2[$j_],$rssnews_desc[$i_]);
							$j_ = $j_ + 1;

						}

	$rsspubdate=$rssnews_pub[$i_];

	include('date_new.php');

	if(strstr($rsspubdate,","))
	   $newpubdate=substr($rsspubdate,0,16);
	else
   	  $newpubdate=substr($rsspubdate,0,10);

	$newpubtime=$newpubdate." ".$newtime;

	// Enlosure
		if($enclosed[$i_]!="")
		{
			preg_match("/(type=&quot;)(.*?)(\/)/i",$enclosed[$i_],$matchesnew);
		    $rssnews_enclosuretype=$matchesnew[2];
			if($rssnews_enclosuretype!='image'){
				preg_match("/(url=&quot;)(.*?)(&quot;)/i",$enclosed[$i_],$matches);
				$rssnews_enclosure=$matches[2];
			} 
		}

	  //For link rel
	   if($link_new[$i_]!="")
		{
			preg_match("/(href=&quot;)(.*?)(&quot;)/i",$link_new[$i_],$matches1);
			$rssnews_link_=$matches1[2];
		}else{
			$rssnews_link_=$rssnews_link[$i_];
		}

  // For a News_cateory....

		$news_cat=explode(",",rtrim($rssnews_category[$i_],','));
		for($c=0;$c<count($news_cat);$c++){
		  if($news_cat[$c]!=''){
		     $news_catval .=$news_cat[$c].'/';
		  }
		}
         $news_catval =rtrim($news_catval,'/');

		 $rssch_item="select * from rssurl_newsitem where news_title='$rssnews_title[$i_]' and rss_channel_id=$chan_id";
	     $rssch_item_res=$pg_sql->query_pass($rssch_item,"4");
         if(count($rssch_item_res)<=0)
 		 {
	      	$rssitems_ins="insert into rssurl_newsitem (rss_channel_id,news_title,description,url,pubdate,last_builddate,language,hour,image,enclosure,frontend_export,pubtime,category) values('$chan_id','$rssnews_title[$i_]','$rssnews_desc[$i_]','$rssnews_link_','$newpubdate','$rssnews_last[$i_]','$langu_new','$newtime','$rssnews_imgurl[$i_]','$rssnews_enclosure','$rssfront_end','$newpubtime','$news_catval')";
			$pg_sql->query_pass($rssitems_ins,"4");

		    $ins_catnew="select max(url_newsitem_id) as url_newsitem_id from rssurl_newsitem";
  			$ins_catnew_res=$pg_sql->query_pass($ins_catnew,"4");

			$ins_cat="select * from rssurl_newsitem where url_newsitem_id=".$ins_catnew_res[0]['url_newsitem_id'];  
			$ins_cat_res=$pg_sql->query_pass($ins_cat,"4");

			$rssch_cat="select * from rsschannel_category where rss_channel_id=$chan_id";
			$rssch_cat_res=$pg_sql->query_pass($rssch_cat,"4");

			for($a=0;$a<count($rssch_cat_res);$a++)
			{
			  if($rssch_cat_res[$a][category_id]!="")
			  {
				$rssnew_cat .=$rssch_cat_res[$a][category_id].",";
			  }
			  if($rssch_cat_res[$a][subcategory_id]!="")
			  {
				  $rssnew_subcat .=$rssch_cat_res[$a][subcategory_id].",";
			  }

			}
			 $new_category=explode(",",$rssnew_cat);
			 $new_subcategory=explode(",",$rssnew_subcat);

			       	$rssid__=$ins_cat_res[0][url_newsitem_id];
					$rssid_email[]=$rssid__;
					$newpub__=trim($ins_cat_res[0][pubdate]);
			   	 	$rsstime__=$ins_cat_res[0]['hour'];
			   	 	$rsspub__=$newpub__." ".$rsstime__;

			   		for($s=0;$s<count($new_category);$s++)
			   		{
			   			$rsscat_id__=$new_category[$s];
			   			if($rsscat_id__!="")
			   			{
			   				$ins__="insert into newsitem_category (newsitem_id,category_id,pubdate,flag) values ('$rssid__','$rsscat_id__','$rsspub__','$rssfront_end')";
			  				$pg_sql->query_pass($ins__,"4");
			   			}
			   		}
			   		for($p=0;$p<count($new_subcategory);$p++)
			   		{
			   			$rsssubcat_id__=$new_subcategory[$p];
			   			if($rsssubcat_id__!="")
			   			{
			   				$subins__="insert into newsitem_category (newsitem_id,subcategory_id,pubdate,flag) values ('$rssid__','$rsssubcat_id__','$rsspub__','$rssfront_end')";
			  				$pg_sql->query_pass($subins__,"4");
			   			}
			   		}

$rssid__='';
$rsscat_id__='';
$rsssubcat_id__='';
$rssnew_cat='';
$rssnew_subcat='';
$news_catval='';
  } // If count ending****
  else{
        $up_qry="update rssurl_newsitem set pubdate='$newpubdate',pubtime='$newpubtime' where news_title='$rssnews_title[$i_]' and rss_channel_id=$chan_id";  
		$up_qry_res=$pg_sql->query_pass($up_qry,"4");
		//Newsitem_cateory
		$upcat_qry="update newsitem_category set pubdate='$newpubdate' where newsitem_id=$chan_id";  
		$upcat_qry_res=$pg_sql->query_pass($upcat_qry,"4");
  } 
$news_catval='';
}// *** For item cnt ending**

// ** Emptying the 	variables
$rsspubdate='';
$rssid__='';
$rsscat_id__='';
$rsssubcat_id__='';
$rssnew_cat='';
$rssnew_subcat='';
$new_category='';
$new_subcategory='';
$news_catval='';
$news_cat='';
$enclosed='';
$link_new='';
$rssnews_title='';
$rssnews_desc='';
$rssnews_link_='';
$rssnews_link='';
$rssnews_pub='';
$rssnews_last='';
$rssnews_imgurl='';
$rssnews_category='';

}  // *** For channel count ending**
include('cron_email.php');
?>
Return current item: News Manager - RSS aggregator