<?php
session_start();
include("./functions.php");

$sel_server_id = mysql_result(mysql_query("select server_id from autoposter_projects where id='".$_REQUEST['pjid']."'"),0);
$res = mysql_query("select * from Projects where project_id='{$sel_server_id}'");
$project_id = mysql_result($res, 0, "project_id");
$domain = mysql_result($res, 0, "domain");
$web_folder = mysql_result($res, 0, "web_folder");
$username = mysql_result($res, 0, "admin_login");
$password = mysql_result($res, 0, "admin_password");

if($web_folder=="") {
	$domain_url = $domain;
} else {
	$domain_url = $domain . "/" . $web_folder;
}

$domain_url = "http://" . $domain_url . "/xmlrpc.php";

$url = parse_url($domain_url);
$host = trim($url['host']);
$path = trim($url['path']);

include_once("./class.mtclient.php");
include_once("./blogger.php");

function get_usrid($value){
        $counter = 0;
        if ( is_array($value) ) {
                foreach($value as $key=>$val){
                        if (is_array($val)) {
                                get_usrid($val);
                        }
                        else {
                                if($key == "userid"){
                                 return $val;
                                }
                                //echo "<tr>\n<td>$counter</td>\n<td>&nbsp;" . $key . "&nbsp;</td>\n<td>&nbsp;" . $val . "&nbsp;</td>\n</tr>\n";
                        }
                        $counter++;
                }
        }
}

$data = blogger_getUserInfo($username, $password);
$gtuserid = get_usrid($data);
$blogID = $gtuserid;

//$cnt_array = count($_REQUEST['title']);
//echo $cnt_array;

echo "Please wait . ";

if(isset($_REQUEST['delete_only'])) {

	foreach($_REQUEST['delart'] AS $checked) {
         if(!is_null($checked)) {
         	$res = mysql_query("delete from autoposter_articles where id='".$checked."'");
         }
 	echo ".";
	}

}

if(isset($_REQUEST['save_update_only'])) {

mysql_query("update autoposter_projects set server_id='".$_REQUEST['selt_server']."', name='".$_REQUEST['projectnm']."' where id='".$_REQUEST['pjid']."'");

$iz=1;
for($i=0; $i < count($_REQUEST['title']); $i++){

        $selcatid = ($_REQUEST["selcat_$iz"]);
        //print_r($selcatid);
        foreach($selcatid as $selcatdataid => $selcatdata){
                $mkcatdata .= trim(trim($selcatdata)."[|]");
        }
        $fncatdata = substr($mkcatdata, 0, -3);

$res = mysql_query("update autoposter_articles set title='".$_REQUEST['title'][$i]."', description='".$_REQUEST['description'][$i]."', mt_excerpt='".$_REQUEST['mt_excerpt'][$i]."', meta_keywords='".$_REQUEST['metakeywords'][$i]."', meta_description='".$_REQUEST['metadescription'][$i]."', mt_keywords='".$_REQUEST['mt_keywords'][$i]."', selpublish='".$_REQUEST['selpublish'][$i]."', selcat='".$fncatdata."', mm='".$_REQUEST['mm'][$i]."', dd='".$_REQUEST['dd'][$i]."', yy='".$_REQUEST['yy'][$i]."', hh='".$_REQUEST['hh'][$i]."', mn='".$_REQUEST['mn'][$i]."', ss='".$_REQUEST['ss'][$i]."' where id='".$_REQUEST['article_id'][$i]."'");
$mkcatdata = ""; //clear old data

echo ".";
$iz++;
}

}

if(isset($_REQUEST['pub_later'])) {

mysql_query("update autoposter_projects set server_id='".$_REQUEST['selt_server']."', name='".$_REQUEST['projectnm']."' where id='".$_REQUEST['pjid']."'");

$iz=1;
for($i=0; $i < count($_REQUEST['title']); $i++){

        $selcatid = ($_REQUEST["selcat_$iz"]);
        //print_r($selcatid);
        foreach($selcatid as $selcatdataid => $selcatdata){
                $mkcatdata .= trim(trim($selcatdata)."[|]");
        }
        $fncatdata = substr($mkcatdata, 0, -3);

$res = mysql_query("update autoposter_articles set title='".$_REQUEST['title'][$i]."', description='".$_REQUEST['description'][$i]."', mt_excerpt='".$_REQUEST['mt_excerpt'][$i]."', meta_keywords='".$_REQUEST['metakeywords'][$i]."', meta_description='".$_REQUEST['metadescription'][$i]."', mt_keywords='".$_REQUEST['mt_keywords'][$i]."', selpublish='".$_REQUEST['selpublish'][$i]."', selcat='".$fncatdata."', mm='".$_REQUEST['mm'][$i]."', dd='".$_REQUEST['dd'][$i]."', yy='".$_REQUEST['yy'][$i]."', hh='".$_REQUEST['hh'][$i]."', mn='".$_REQUEST['mn'][$i]."', ss='".$_REQUEST['ss'][$i]."', posted='0' where id='".$_REQUEST['article_id'][$i]."'");
$mkcatdata = ""; //clear old data

echo ".";
$iz++;
}
}

if(isset($_REQUEST['pub_now'])) {

mysql_query("update autoposter_projects set server_id='".$_REQUEST['selt_server']."', name='".$_REQUEST['projectnm']."' where id='".$_REQUEST['pjid']."'");

$i=0;
$iz=1;
while ($i < count($_REQUEST['title'])){

if($_REQUEST['title'][$i] != "") {
        $selcatid = ($_REQUEST["selcat_$iz"]);
        //print_r($selcatid);
        $vals = array();
        foreach($selcatid as $selcatdataid => $selcatdata){
                $mkcatdata .= trim(trim($selcatdata)."[|]");
                $vals[] = new xmlrpcval(trim($selcatdata), 'string');
        }
        $fncatdata = substr($mkcatdata, 0, -3);
        //print_r($vals);

//$rt_date = "".$_REQUEST['yy'][$i].$_REQUEST['mm'][$i].$_REQUEST['dd'][$i].'T'.$_REQUEST['hh'][$i].':'.$_REQUEST['mn'][$i].':'.$_REQUEST['ss'][$i]."";

$description = $_REQUEST['description'][$i]."\n<!--keywords{".$_REQUEST['metakeywords'][$i]."}--> <!--description{".$_REQUEST['metadescription'][$i]."}-->";

$textPost = array("title" => new xmlrpcval(trim($_REQUEST['title'][$i]), 'string'),
                  "description" => new xmlrpcval(trim(stripslashes($description)), 'string'),
                  "mt_excerpt" => new xmlrpcval(trim($_REQUEST['mt_excerpt'][$i]), 'string'),
                  "mt_allow_comments" => new xmlrpcval(1, 'int'),
                  "mt_allow_pings" => new xmlrpcval(1, 'int'),
                  //"dateCreated" => new xmlrpcval($rt_date, 'dateTime.iso8601'),
                  "categories" => new xmlrpcval($vals, 'array'),
                  "mt_keywords" => new xmlrpcval(trim($_REQUEST['mt_keywords'][$i]), 'string')
                  );

                  $publish = $_REQUEST['selpublish'][$i];

if($i > -1){
$blog = new mtclient($username, $password, $host, $path);
$data = $blog->newPost($blogID, $textPost, $publish);
//debug($data);

$res = mysql_query("update autoposter_articles set title='".$_REQUEST['title'][$i]."', description='".$_REQUEST['description'][$i]."', mt_excerpt='".$_REQUEST['mt_excerpt'][$i]."', meta_keywords='".$_REQUEST['metakeywords'][$i]."', meta_description='".$_REQUEST['metadescription'][$i]."', mt_keywords='".$_REQUEST['mt_keywords'][$i]."', selpublish='".$_REQUEST['selpublish'][$i]."', selcat='".$fncatdata."', mm='".$_REQUEST['mm'][$i]."', dd='".$_REQUEST['dd'][$i]."', yy='".$_REQUEST['yy'][$i]."', hh='".$_REQUEST['hh'][$i]."', mn='".$_REQUEST['mn'][$i]."', ss='".$_REQUEST['ss'][$i]."', posted='1' where id='".$_REQUEST['article_id'][$i]."'");
}
$mkcatdata = ""; //clear old data

echo " . ";
$i++;
$iz++;
}
}

}

redirectTo("manage_wp_projects.php?done=1&pjid=".$_REQUEST['pjid']."");
exit();
?>