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

$admin_email = $Config->OWNER_EMAIL;

if($_REQUEST['reset_cron'] == 1) {
   mysql_query("update autoposter_settings set cron_is_runing='0' where id='1'");
   exit();
}

if($_REQUEST['test'] == 1) {
	$headers = "";
	$headers .= "From: me <".$admin_email.">\r\n";
	$headers .= "To: you <".$admin_email.">\r\n";
	$headers .= "Reply-To: me <".$admin_email.">\r\n";

	mail($admin_email, "WPManagerDX: AutoPoster - Cron Testing...", "cron is working fine...\n\n please remove (?test=1) from the cron path/url", $headers);

	mysql_query("update autoposter_settings set cron_is_runing='0' where id='1'");
	exit();
}

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++;
                }
        }
}

echo "Please wait . ";
flush();

//keep adding for all tables
$result = mysql_query("select * from autoposter_articles where posted='0' order by id asc") or die('Query failed: ' . mysql_error());

while ($row = mysql_fetch_assoc($result)) {
	//Load db data
	$project_id = $row["project_id"];

	$server_id = mysql_result(mysql_query("select server_id from autoposter_projects where id='$project_id'"),0);

	$res = mysql_query("select * from Projects where project_id='{$server_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");

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

	//Load db data
	$id = $row["id"];
	$title = $row["title"];
	$description = $row["description"];
	$mt_keywords = $row["mt_keywords"];
	$mt_excerpt = $row["mt_excerpt"];
	$meta_keywords = $row["meta_keywords"];
	$meta_description = $row["meta_description"];
	$selpublish = $row["selpublish"];
	$selcat = $row["selcat"];
	$mm = $row["mm"];
	$dd = $row["dd"];
	$yy = $row["yy"];
	$hh = $row["hh"];
	$mn = $row["mn"];
	$ss = $row["ss"];

	$time = time();
	$start_time = mktime($hh, $mn, $ss, $mm, $dd, $yy);

	$is_cron_runing = mysql_result(mysql_query("select cron_is_runing from autoposter_settings where id='1'"),0);

	if($time > $start_time and $is_cron_runing == 0) {

		mysql_query("update autoposter_settings set cron_is_runing='1' where id='1'");

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

		$fndescription = trim($description)."\n<!--keywords{".trim($meta_keywords)."}--> <!--description{".trim($meta_description)."}-->";

		$fncatnm = explode("[|]", $selcat);
		$vals = array();
		foreach($fncatnm as $selcatdataid => $selcatdata){
        	$vals[] = new xmlrpcval(trim($selcatdata), 'string');
		}
        $fncatdata = substr($mkcatdata, 0, -3);
        //print_r($vals);

		$textPost = array("title" => new xmlrpcval(trim($title), 'string'),
                  "description" => new xmlrpcval(trim(stripslashes($fndescription)), 'string'),
                  "mt_excerpt" => new xmlrpcval(trim($mt_excerpt), '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($mt_keywords), 'string')
		);

		$blog = new mtclient($username, $password, $host, $path);
		$data = $blog->newPost($blogID, $textPost, $selpublish);

		mysql_query("update autoposter_articles set posted='1' where id='$id'");

		mysql_query("update autoposter_settings set cron_is_runing='0' where id='1'");

	echo " . ";
	flush();
	}

	$email_notification = mysql_result(mysql_query("select email_notification from autoposter_settings where id='1'"),0);

	if($email_notification == "ON" and $project_id > 0) {
		$total_articles = mysql_result(mysql_query("select count(*) from autoposter_articles where project_id='$project_id'"),0);
		$total_articles_posts = mysql_result(mysql_query("select count(*) from autoposter_articles where posted='1' and project_id='$project_id'"),0);

		if($total_articles > 0 and $total_articles_posts == $total_articles) {
			$pjnm = mysql_result(mysql_query("select name from autoposter_projects where id='$project_id'"),0);

			$headers = "";
			$headers .= "From: me <".$admin_email.">\r\n";
			$headers .= "To: you <".$admin_email.">\r\n";
			$headers .= "Reply-To: me <".$admin_email.">\r\n";

			mail($admin_email, "WPManagerDX: AutoPoster - ARTICLES ARE POSTED", "All Pending Articles are posted for\n\n WordPress Project ID: $project_id\n Project: $pjnm", $headers);
		}
	}
}

mysql_free_result($result);

echo " . . ";
flush();

// End

// Closing connection
mysql_close();

echo "<br><br><b>Done</b>";
?>