Location: PHPKode > projects > News Manager - RSS aggregator > newsmanager/feed_insert.php
<?php
include('db/dbconnect.php');

$url=$_REQUEST['feed_url'];
function is_url($url) {
    if (!preg_match('#^http\\:\\/\\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', $url)) {
        return false;
    } else {
        return true;
    }
}
// Including Variables .....

$tag = "";
$title=array();
$description=array();
$link=array();
$pubdate=array();
$language=array();
$copyright="";
$lastbuild=array();
$imageurl=array();
$webmaster="";
$author=array();
$managing_editor="";
$generator="";
$docs="";
$cloud="";
$ttl="";
$rating=array();
$textinput="";
$skiphours="";
$skipdays="";

function ch_startElement($parser, $tagname, $attrs)
{
	global $tag;
	global $title, $description,$link,$pubdate,$language,$copyright,$lastbuild,$imageurl,$webmaster,$author,$managing_editor,$generator,$docs,$cloud,$ttl,$rating,$textinput,$skiphours,$skipdays;
	$tag = $tagname;
}

function ch_endElement($parser, $tagname)
{
	global $tag ;
	global $title, $description,$link,$pubdate,$language,$copyright,$lastbuild,$imageurl,$webmaster,$author,$managing_editor,$generator,$docs,$cloud,$ttl,$rating,$textinput,$skiphours,$skipdays;
	$tag="";
}

function ch_characterData($parser, $data)
{
	global $tag;
	global $title, $description,$link,$pubdate,$language,$copyright,$lastbuild,$imageurl,$webmaster,$author,$managing_editor,$generator,$docs,$cloud,$ttl,$rating,$textinput,$skiphours,$skipdays;
        $data=trim($data);

  switch ($tag)
  {
       case "TITLE":
			$title[] = str_replace("'","||",$data);
			break;
	   case "DESCRIPTION":
			$description[] = $data;
			break;
	   case "LINK":
			$link[] = $data;
			break;
	   case "PUBDATE":
			$pubdate[] = $data;
			break;
	   case "DC:DATE":
			$pubdate[] =$data;
	        break;
	   case "LANGUAGE":
	        $language[] = $data;
            break;
       case "LASTBUILDDATE":
			$lastbuild[] = $data;
	 		break;
	   case "COPYRIGHT":
	        $copyright .= $data;
	        break;
	   case "URL":
	        $imageurl[] = $data;
	        break;
	   case "WEBMASTER":
	        $webmaster .= $data;
	        break;
	   case "AUTHOR":
	        $author[] = $data;
	        break;
	   case "MANAGINGEDITOR":
	       $managing_editor .=$data;
	       break;
	   case "GENERATOR":
	       $generator .=$data;
	       break;
	   case "DOCS":
	       $docs .=$data;
	       break;
	   case "CLOUD":
	      $cloud .=$data;
	      break;
	   case "TTL":
	      $ttl .=$data;
	      break;
	   case "RATING":
	      $rating[] =$data;
	      break;
	   case "TEXTINPUT":
	      $textinput .=$data;
	      break;
	   case "HOUR":
	      $skiphours .=($skiphours? ',':'').$data;
	      break;
	   case "SKIPDAYS":
          $skipdays .=$data;
          break;
	  }

	}
//Date Format Conversion dd/mm/yyyy
	include('date.php');

if (is_url($url))
{
  $chk_url="select channel_url from rsschannel where channel_url='".trim($url)."' and channel_language='".$_REQUEST['lang']."'";
  $exist=$pg_sql->query_pass($chk_url,"4");
  if(count($exist)==0)
  {
	  $con=@file_get_contents($url);
	  if(strstr($con,"<rss")|| strstr($con,"<rdf") || strstr($con,"<feed") || strstr($con,"<opml") || !strstr($con,"<html"))
	  {
		 $xml_parser = xml_parser_create();
		 xml_set_element_handler($xml_parser, "ch_startElement", "ch_endElement");
		 xml_set_character_data_handler($xml_parser, "ch_characterData");
		 $fp = fopen($url,"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);
			  
		  $channel_title=htmlspecialchars(trim(str_replace("'",'',$_REQUEST['feed_title'])));
		  $channel_shortname=$channel_title;
		  $channel_desc=htmlspecialchars(trim(str_replace("'",'',$description[0])));
		  if(strlen($channel_desc)<5)
			 $channel_desc=$channel_title;
		  else
			 $channel_desc=$channel_desc;
		  $channel_url=$url;
		  $channel_pubdate=trim($pubdate[0]);
		  $channel_language=$_REQUEST['lang'];
		  $lang=trim($_REQUEST['lang']);
		  $channel_image=$imageurl[0];
		  $channel_lastdate=date("Y-n-j");
		  $channel_copyright=$copyright;
		  $channel_webmaster=$webmaster;
		  $channel_author=$author[0];
		  $channel_managing=$managing_editor;
		  $channel_generator=$generator;
		  $channel_docs=$docs;
		  $channel_cloud=$cloud;
		  $channel_ttl=$ttl;
		  $channel_rating=$rating[0];
		  $channel_textinput=$textinput;
		  $channel_skiphours=$skiphours;
		  $channel_skipdays=$skipdays;

		  $pubdate=$pubdate[0];		   
		  if(strstr($pubdate,",")){
			$date=explode(" ",$pubdate);
		  if(strstr($date[1],"-")){
			$datehyp=explode("-",$date[1]);

		  if(substr($datehyp[0],0,1)==0)
			 $datehyp[0]=str_replace("0","",$datehyp[0]);
		  else
			 $datehyp[0]=$datehyp[0];
			 $newdate= $datehyp[0]."/".get_month($datehyp[1])."/".$datehyp[2];
		  }else{
			 if(empty($date[1]))
			 {
			   if(substr($date[2],0,1)==0)
				$date[2]=str_replace("0","",$date[2]);
			   else
				$date[2]=$date[2];

				$newdate=$date[2]."/".get_month($date[3])."/".$date[4];
			 }
			 else{
				 if(substr($date[1],0,1)==0)
				  $date[1]=str_replace("0","",$date[1]);
				 else
				  $date[1]=$date[1];
				  $newdate=$date[1]."/".get_month($date[2])."/".$date[3];
			 }
		  }
		}elseif(!strstr($pubdate,",") && strstr($pubdate,"-")){
		   $date=explode("-",$pubdate);
		   $hypdat=substr($date[2],0,2);
			   if(substr($hypdat,0,1)==0)
				 $hypdat=str_replace("0","",$hypdat);
			   else
				 $hypdat=$hypdat;

			if(substr($date[1],0,1)==0)
			  $date[1]=str_replace("0","",$date[1]);
			 else
			  $date[1]=$date[1];

		   $newdate=$hypdat."/".$date[1]."/".$date[0];
		}else{
		  $newdate=$pubdate;
		}
		 $channel_pubdate=$newdate;

	 //****** Inserting Channel Details in to table Rsschannel*******

	 $Rss_qry="Insert into rsschannel (channel_title,channel_shortname,channel_description,channel_url,channel_image,channel_author,channel_copyright,channel_webmaster,channel_language,channel_pubdate,channel_lastdate,channel_status,channel_managing_editor,channel_generator,channel_docs,channel_cloud,channel_rating,channel_text_input,channel_skiphours,channel_skipdays,channel_ttl) values ('$channel_title','$channel_shortname','$channel_desc','$url','$image','$channel_author','$channel_copyright','$channel_webmaster','$channel_language','$channel_pubdate','$channel_lastdate','Y','$channel_managing','$channel_generator','$channel_docs','$channel_cloud','$channel_rating','$channel_textinput','$channel_skiphours','$channel_skipdays','$channel_ttl')";
	  $Rss_res=$pg_sql->query_pass($Rss_qry,"4");

	// Get Last id from Table
			$get_max="select max(channel_id) as channel_id from rsschannel";
			$get_maxres=$pg_sql->query_pass($get_max,"4");
			$channel_id=$get_maxres[0]['channel_id'];
			$category_id=explode(",",$_REQUEST['cat_val']);
			for($c=0;$c<count($category_id);$c++)
			{
				$cat_id=$category_id[$c];
				if($cat_id !=""){
						$Rsscat_chan="insert into rsschannel_category (rss_channel_id,category_id) values ('$channel_id','$cat_id')";
						$Rsscat_res=$pg_sql->query_pass($Rsscat_chan,"4");
				  }
			}

	//Inserting Newsitems in to Rssurl_item table
		 $_REQUEST['Url']=$url; 
		 include('login/rssitems.php');

		$ins_cat="select * from rssurl_newsitem where rss_channel_id='$channel_id'";
		$ins_cat_res=$pg_sql->query_pass($ins_cat,"4");


		for($n=0;$n<count($ins_cat_res);$n++)
		{
			$id__=$ins_cat_res[$n][url_newsitem_id];
			$newpub__=trim($ins_cat_res[$n][pubdate]);
			$time__=$ins_cat_res[$n]['hour'];
			$pub__=$newpub__." ".$time__ ;

			for($x=0;$x<count($category_id);$x++)
			{
				$cat_id__=$category_id[$x];
				if($cat_id__!="")
				{
					$ins__="insert into newsitem_category (newsitem_id,category_id,pubdate,flag) values ('$id__','$cat_id__','$pub__','Y')";
					$ins_res__=$pg_sql->query_pass($ins__,"4");
				}
			}
		}
	//Sending Confirmation to Respective Email Address
			 $Email=$_REQUEST['email'];
			 if($lang=='nl'){
				$body ="<font face='arial' size='2'>Beste ".$_REQUEST['name'].",<br>
					   &nbsp;&nbsp;De feed/channel die je hebt aangevraagd, werd met succes toegevoegd aan de site.
					   <br>Bedankt,<br>
					   het News Manager team .<br><br>
					   <b><u>PS</u></b>:Dit is een automatisch gegenereerde mail. Gelieve hier niet op de antwoorden.<br></font>";
             }elseif($lang=='fr'){
				 $body ="<font face='arial' size='2'>Cher/chère ".$_REQUEST['name'].",<br>
					   &nbsp;&nbsp;Le feed/channel a été ajouté à notre site.
					   <br>Merci,<br>
					   L'équipe News Manager.<br><br>
					   <b><u>PS</u></b>:Ce mail est automatique: veuillez ne pas y répondre. <br></font>";
			 }else{
				$body ="<font face='arial' size='2'>Dear ".$_REQUEST['name'].",<br>
					   &nbsp;&nbsp;Your RSS Feed have been added to our site.
					   <br>Regards,<br>
					   NewsManager Admin .<br><br>
					   <b><u>Note</u></b>:This is auto generated mail thanks for not replying . <br></font>";
             }  
				$sub="RSS Feed added";
				$from ="From:Newsmanager Admin \nContent-type:text/html";
				mail($Email,$sub,$body,$from);

		header("location:index.php?lang=$lang");
		exit;
	 }else{
		header("location:feed_form.php?flag=1");
		exit;
     }
 }else{
    header("location:feed_form.php?flag=2");
    exit;
   }
}else{
    header("location:feed_form.php?flag=1");
	exit;
}
?>
Return current item: News Manager - RSS aggregator