//====================================
require('wp-config.php');
//DB_NAME
$notinstalled=false;
$db_wp_connection = mysql_pconnect(DB_HOST, DB_USER, DB_PASSWORD) or trigger_error(mysql_error(),E_USER_ERROR);
$database_name=DB_NAME;
//====================================
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
function addtext_desc($feedid, $c_id, $database_name, $db_wp_connection, $table_prefix){
mysql_select_db($database_name, $db_wp_connection);
$addtext_sql="select * from ".$table_prefix."rsstoblog where id=".$feedid;
$rs_addtext = mysql_query($addtext_sql, $db_wp_connection) or die(mysql_error());
$row_rs_addtext = mysql_fetch_assoc($rs_addtext);
$totalRows_rs_addtext = mysql_num_rows($rs_addtext);
$main_text_arr=explode("#BREAK#",$row_rs_addtext['text_fulltext']);
foreach($main_text_arr as $ivalue){
if($ivalue<>""){
$main_err[]=$ivalue;
}
}
if($row_rs_addtext['text_sequence']=="Random"){
$rndNumber=rand(1,count($main_err));
$return_text=$main_err[$rndNumber-1];
}else{
if($row_rs_addtext['last_insertion']<=count($main_err)-1){
$return_text=$main_err[$row_rs_addtext['last_insertion']+1];
$insert_seq=$row_rs_addtext['last_insertion']+1;
$query_update="UPDATE ".$table_prefix."rsstoblog SET last_insertion=".$insert_seq." WHERE id =".$feedid;
$result3=mysql_query($query_update,$db_wp_connection) or die($query_update.mysql_error());
}else{
$return_text=$main_err[0];
$query_update="UPDATE ".$table_prefix."rsstoblog SET last_insertion=1 WHERE id =".$feedid;
$result3=mysql_query($query_update,$db_wp_connection) or die($query_update.mysql_error());
}
}
return trim($return_text);
}
function insertblogs($feedid, $rss, $bform, $bto, $cat_id, $linkadd, $show_content){
global $total_no_feeds;
global $arrtitles;
global $arrdescs;
global $arrlinks;
global $script_base_url;
global $database_name;
global $db_wp_connection;
global $table_prefix;
mysql_select_db($database_name, $db_wp_connection);
$rsstoblog_sql="select * from ".$table_prefix."rsstoblog where id=".$feedid;
$rs_rsstoblog = mysql_query($rsstoblog_sql, $db_wp_connection) or $notinstalled=true;
$row_rs_rsstoblog = mysql_fetch_assoc($rs_rsstoblog);
$totalRows_rs_rsstoblog = mysql_num_rows($rs_rsstoblog);
$rel_follow =$row_rs_rsstoblog['rel_follow'];
//first read the xml file
if(!($fp=@fopen($rss,"r"))) //open a xml file
{die("Unable to open XML file : ".$rss);}
if(!($xml_parser=xml_parser_create())) //create parser for xml file
{die("couldnt create xml parser");}
xml_set_element_handler($xml_parser,"startelement","endelement");
xml_set_character_data_handler($xml_parser,"characterdata");
while($data = fread($fp,4096))
{
if(!xml_parse($xml_parser,$data,feof($fp)))
{
break;
}
}
xml_parser_free($xml_parser); //releasing SAX from memory
//================================Insert Blg Data In Table=======================================
//===============================================================================================
$data="";
//$bto=$bto+$row_rs_rsstoblog['last_feed'];
if(count($arrtitles)>$bto){
$blogto=$bto+1;
}else{
$blogto=count($arrtitles);
}
$rssid=$feedid;
$j=0;
$count_blogs=0;
//==
// print_r($arrtitles);
//==
//$total_no_feeds=$row_rs_rsstoblog['last_feed'];///$row_rs_rsstoblog['total_feeds']);
//$bfrom=$total_no_feeds;
for($i=$bfrom+1;$i<$blogto;$i++){
if (trim($arrtitles[$i]["TITLE"])<>""){
$blog_title_content=trim($arrtitles[$i]["TITLE"]);
$blog_title_content=str_replace(" "," ",$blog_title_content);
$blog_title_dashed=str_replace(" ","-",$blog_title_content);
//=============================Checking Title==============================================
mysql_select_db($database_name, $db_wp_connection);
$rsstitleCheck_sql="select * from ".$table_prefix."posts where post_category=".$cat_id." and post_name='".addslashes($blog_title_dashed)."'";
$rs_rsstitleCheck = mysql_query($rsstitleCheck_sql, $db_wp_connection) or die(mysql_error());
$row_rs_rsstitleCheck = mysql_fetch_assoc($rs_rsstitleCheck);
$totalRows_rs_rsstitleCheck = mysql_num_rows($rs_rsstitleCheck);
//echo "
".$rsstitleCheck_sql;
//=========================================================================================
if($totalRows_rs_rsstitleCheck<=0){
if(trim($row_rs_rsstoblog['text_fulltext'])<>""){
if($row_rs_rsstoblog['text_position']=="Before"){
$blog_main_content=addtext_desc($feedid, $row_rs_rsstoblog['cat_id'], $database_name, $db_wp_connection, $table_prefix)."
".strip_tags($arrdescs[$i]["DESCRIPTION"]);
}else{
$blog_main_content=strip_tags($arrdescs[$i]["DESCRIPTION"])."
".addtext_desc($feedid, $row_rs_rsstoblog['cat_id'], $database_name, $db_wp_connection, $table_prefix);
}
}else{
$blog_main_content=strip_tags($arrdescs[$i]["DESCRIPTION"]);
}
//echo " LINK : ".$arrlinks[$i]['LINK'];
if($linkadd=="Yes"){
if($rel_follow=="Yes"){
$blog_main_content=$blog_main_content."
Read full story";
}else{
$blog_main_content=$blog_main_content."
Read full story";
}
}
$now = date('Y-m-d H:i:s');
$now_gmt = gmdate('Y-m-d H:i:s');//current_time('mysql', 1);
$blog_insert_query=sprintf("INSERT INTO ".$table_prefix."posts (post_author, post_date, post_date_gmt, post_content, post_title, post_category, post_status, comment_status, ping_status, post_name, post_modified, post_modified_gmt) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString(1, "int"),
GetSQLValueString($now, "date"),
GetSQLValueString($now_gmt, "date"),
GetSQLValueString(addslashes($blog_main_content), "text"),
GetSQLValueString(addslashes($blog_title_content), "text"),
GetSQLValueString($cat_id, "text"),
GetSQLValueString('publish', "text"),
GetSQLValueString('open', "text"),
GetSQLValueString('open', "text"),
GetSQLValueString(addslashes($blog_title_dashed), "text"),
GetSQLValueString($now, "date"),
GetSQLValueString($now_gmt, "date"));
mysql_select_db($database_name, $db_wp_connection);
$Result1 = mysql_query($blog_insert_query, $db_wp_connection) or die($blog_insert_query.mysql_error());
$count_blogs++;
$inserted_bolgid=mysql_insert_id();
$guid="http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/?p=".$inserted_bolgid;
mysql_select_db($database_name, $db_wp_connection);
$query_update="UPDATE ".$table_prefix."posts SET guid='".$guid."' WHERE id =".$inserted_bolgid;
$result3=mysql_query($query_update,$db_wp_connection) or die($query_update.mysql_error());
$blog_cat_insert=sprintf("INSERT INTO ".$table_prefix."post2cat (post_id, category_id) VALUES (%s, %s)",
GetSQLValueString($inserted_bolgid, "int"),
GetSQLValueString($cat_id, "int"));
mysql_select_db($database_name, $db_wp_connection);
$Result2 = mysql_query($blog_cat_insert, $db_wp_connection) or die($blog_cat_insert.mysql_error());
//=====================================================
$total_no_feeds++;
$updateSQL = sprintf("UPDATE ".$table_prefix."rsstoblog SET last_feed=%s, modify_date=%s WHERE id=%s",
GetSQLValueString($total_no_feeds, "int"),
GetSQLValueString(date("Y-m-d"), "date"),
GetSQLValueString($rssid, "int"));
// echo $updateSQL;
mysql_select_db($database_name, $db_wp_connection);
$Result1 = mysql_query($updateSQL, $db_wp_connection) or die($updateSQL.mysql_error());
//=====================================================
}
}
}
if($show_content){
echo "Total ".$count_blogs." Has Been Inserted.";
}
//===============================================================================================
}
function startelement($parser, $name, $attrs) {
global $current_tag;
global $last_tag;
global $count;
global $arrtitles;
global $arrdescs;
global $arrlinks;
$last_tag=$current_tag;
$current_tag=$name;
if ($current_tag=="ITEM"){
$count++;
$arrtitles[$count]["TITLE"]="";
$arrlinks[$count]["LINK"]="";
$arrdescs[$count]["DESCRIPTION"]="";
}
}
function endelement($parser, $name) {
global $current_tag;
$current_tag="";
}
function characterdata($parser, $data) {
global $count;
global $last_tag;
global $current_tag;
global $arrtitles;
global $arrdescs;
global $arrlinks;
if (!$current_tag)
{return;}
if ($count<0)
{return;}
//echo "Count is : $count :: $current_tag :: $data\n";
if($current_tag=="TITLE")
{
$arrtitles[$count]["TITLE"].=$data;
}
if($current_tag=="LINK")
{$arrlinks[$count]["LINK"].=$data;}
if($current_tag=="DESCRIPTION")
{$arrdescs[$count]["DESCRIPTION"].=$data;}
}
mysql_select_db($database_name, $db_wp_connection);
$p_sql="select * from ".$table_prefix."rsstoblog";
$rs_table = mysql_query($p_sql, $db_wp_connection) or $notinstalled=true;
if($notinstalled==false){
$row_rs_para = mysql_fetch_assoc($rs_table);
$totalRows_rs_table = mysql_num_rows($rs_table);
}
do{
$total_feeds=explode("\n",$row_rs_para['rss_feeds']);
//echo "RSS : ".print_r($total_feeds). " : $J";
$tfeeds=count($total_feeds);
$bfrom=$row_rs_para['result_from'];
$bto=$row_rs_para['result_to'];
$cat_id=$row_rs_para['cat_id'];
$linkadd=$row_rs_para['siteurl'];
$abc=strtotime($row_rs_para['modify_date']);
$data_date=date("Y-m-d",$abc);
$feedid=$row_rs_para['id'];
if(isset($_GET['chk']) and $_GET['chk']=="manual"){
$show_content=true;
}else{
$show_content=false;
}
$current_date=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$row_rs_para['chk_every'],date("Y")));
if($show_content==false){
if($current_date>$data_date){
for($j=0;$j<$tfeeds;$j++){
if($total_feeds[$j]<>""){
// echo "RSS : ".$total_feeds[$j]. " : $J";
//echo "FEED ID: ".$feedid ."
";
$count=0;
$arrtitles="";
$arrdescs="";
$arrlinks="";
insertblogs($feedid, $total_feeds[$j], $bfrom, $bto, $cat_id, $linkadd, $show_content);
}
}
}
}else{
for($j=0;$j<$tfeeds;$j++){
if($total_feeds[$j]<>""){
$count=0;
$arrtitles="";
$arrdescs="";
$arrlinks="";
insertblogs($feedid, $total_feeds[$j], $bfrom, $bto, $cat_id, $linkadd, $show_content);
}
}
}
}while($row_rs_para = mysql_fetch_assoc($rs_table))
?>