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

echo "<br>&nbsp;&nbsp;Please wait . .";

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

function getcatlistfromreq($pjid, $iz) {

	$mkcatdata = ""; //clear old data

	for($i=0; $i<count($_REQUEST['pjid']); $i++) {
		$selcatid = ($_REQUEST["selcat_{$pjid}_{$iz}"]);
		//print_r($selcatid);
		foreach($selcatid as $selcatdataid => $selcatdata){
			$mkcatdata .= trim(trim($selcatdata)."[|]");
		}
		echo " . ";
	}

return $mkcatdata;
}

if($_REQUEST['pub_later']) {

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

		if($_REQUEST['title'][$i] != "") {

			$pjlst = explode(",", $_REQUEST['pjid']);
			foreach($pjlst as $pjid) {
                $mkcatdata .= getcatlistfromreq($pjid, $iz);
                $fncatdata = substr($mkcatdata, 0, -3);
			}

			foreach($pjlst as $pjid) {
        		//echo $pjid . " - " . $fncatdata . "<br /><br />";

				mysql_query("insert into autoposter_articles set project_id='{$pjid}', 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'") or die("<b>Error found with:</b> " . $_REQUEST['title'][$i] . " " . mysql_error());

				echo " . ";
			}

			$mkcatdata = ""; //clear old data
		}

    $iz++;
	}

}

if($_REQUEST['pub_now']) {

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

		if($_REQUEST['title'][$i] != "") {

			$pjlst = explode(",", $_REQUEST['pjid']);
			foreach($pjlst as $pjid) {
                $mkcatdata .= getcatlistfromreq($pjid, $iz);
                $fncatdata = substr($mkcatdata, 0, -3);
			}

			foreach($pjlst as $pjid) {
				$rntctlst = 0;
				include('connect_xmlprc.php');

				$fncatnm = explode("[|]", $fncatdata);
				$vals = array();
				foreach($fncatnm as $selcatdataid => $selcatdata) {
					$vals[] = new xmlrpcval(trim($selcatdata), 'string');
        		}
        		//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(array(new xmlrpcval("$fncatdata", 'string')), 'struct')
                  "categories" => new xmlrpcval($vals, 'array'),
                  "mt_keywords" => new xmlrpcval(trim($_REQUEST['mt_keywords'][$i]), 'string')
				);

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

				//echo $blogID;

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

			echo " . ";
			}

		$mkcatdata = ""; //clear old data
		}

	//break;
	$i++;
	$iz++;
	}

	$pjlst = explode(",", $_REQUEST['pjid']);
	foreach($pjlst as $pjid) {
		@mysql_query("delete from autoposter_projects where id='{$pjid}'");
		$pjid = $pjid;
	}
}

redirectTo("done.php?pjid=".$_REQUEST['pjid']."#top");
exit();
?>