<?php 
header('Cache-Control: no-cache, no-store, must-revalidate');   
header('Expires: 0');
set_time_limit(0);

// error_reporting(E_ALL);
// ini_set('display_errors', 1);

// Return Default mySql Connection
function defaultDbConnection()
{
// get default System Settings from xml file	
	$mSystem = "DEFAULT";
// Get System Definition XML from URL
	$mData = file_get_contents("../vimsconfig/systemsettings.xml");
// Parse XML to XML Object Array
	$xml = simplexml_load_string($mData, 'SimpleXMLElement', LIBXML_NOCDATA);
// Split-out System Definitions from XML 'fields' Nodes
	$i = 0;

	for ($i = 0; $i <= 150; $i++) 
	{		
		$mSystemId = "";
		$mPath = "";
		$mDbHost = "";
		$mDbUser = "";
		$mDbPwd = "";
		$mDbase = "";
		$mMqServer = "";
		$mCompany = "";
		if (("".$xml->systems->system[$i]->systemid."" == "") || ("".$xml->systems->system[$i]->systemid."" == false) || ("".$xml->systems->system[$i]->systemid."" == false))
			break;
		
		$mSystemId = "".$xml->systems->system[$i]->systemid."";
		if (($mSystemId == "") || ($mSystemId == false))
			break;
		
// Store System Settings from "trandef/systemsettings.xml"		
		if (strToUpper($mSystem) == strToUpper($mSystemId))
		{
			$mPath = "".$xml->systems->system[$i]->path."";
			$mDbHost = "".$xml->systems->system[$i]->dbhost."";
			$mDbUser = "".$xml->systems->system[$i]->dbuser."";
			$mDbPwd = "".$xml->systems->system[$i]->dbpwd."";
			$mDbase = "".$xml->systems->system[$i]->dbase."";
			break;
		}
		if (($mSystemId == "") || ($mSystemId == false))
			break;
	}
	$mDbDefaults = array();
	$mDbDefaults[0] = $mDbHost;
	$mDbDefaults[1] = $mDbUser;
	$mDbDefaults[2] = $mDbPwd;
	$mDbDefaults[3] = $mDbase;
	$mDbDefaults[4] = $mPath;
	return $mDbDefaults;
}


$error = false;
$mConnection = null;

$mActions = array();

$i = 0;

$mShortTms = "";
$mShortDte = "";
$mShortTme = "";

// Main Process
$mToday = date("Y-m-d");
	
$mActions  = explode(",",$_POST['actions']);
$mCurrentStatus 	= ($_POST['currentstatus']);
$mNext = 0;

if(($mCurrentStatus >= "0") && ($mCurrentStatus <= "9"))
{
	$mNext = (int)$mCurrentStatus;
}
else
{
	$mNext = 0;
}
try
{
	if ($mActions[0]<>"")
	{

// Get System Defaults
		$mDbDefaults = defaultDbConnection();
		$mfHost 	= $mDbDefaults[0];
		$mfUser 	= $mDbDefaults[1];
		$mfPwd  	= $mDbDefaults[2];
		$mfDbName  	= $mDbDefaults[3];
		$mfConnect 	= $mDbDefaults[4];
		
		$i = $mNext;
		try
		{
// php File name
			list($mFile,$mParms) = explode(":",$mActions[$i]);
// Function & Parms
			list($mCall,$mPassParm) = explode("?",$mParms);
// Replace Chars in Input String to emulate a JSON format instead of GET format
			$mPassParm = str_replace("=",":",$mPassParm);
			$mPassParm = str_replace("&",",",$mPassParm);
			$mPassParm .= ",currentUser:".$mCurrentUser;		
			$mPassParm .= ",host:".$mfHost;
			$mPassParm .= ",user:".$mfUser;
			$mPassParm .= ",pwd:".$mfPwd;
			$mPassParm .= ",dbase:".$mfDbName;
			$mPassParm .=",connect:".$mfConnect;			
//			echo("<li>[".date('Y-m-d H:i:s')."] - "."Task in Progress - ".$mFile." - ".$mCall." - ".$mPassParm."</li>");			
// Include File
			try
			{
				include_once($mFile.'.php');
			}
			catch(Exception $e)
			{
				$mReturn = "FAIL-".$e;
			}
// Function	
			try
			{
				if(function_exists($mCall))
				{
					$mStatus = $mCall($mPassParm);
					echo("<li>[".date('Y-m-d H:i:s')."] - "."Function - ".$mFile." - ".$mCall." - ".$mStatus."</li>");	
// Test for validation FAIL
					if (substr($mStatus,0,2)=="FA")
					{
						$mReturn = $mStatus;	
					}
					echo("<li>[".date('Y-m-d H:i:s')."] - "."Response - ".$mStatus."</li>");
				}
				else
				{
					echo("<li>[".date('Y-m-d H:i:s')."] - "."Function Does Not Exist - ".$mFile." - ".$mCall." - ".$mPassParm."</li>");	
				}
			}
			catch (Exception $e)
			{
				$mReturn = "FAIL-".$e;
			}
		}
		catch (Exception $e) 
		{
			$mReturn = "FAIL-".$e;
		}
		if ((substr($mReturn,0,2) != "FA") && ($mNext < count($mActions)-1))
		{
			$mNext++;
			$mReturnSts = $mNext;
		}
		else
			$mReturnSts = "end";
		echo($mReturn);
	}
}
catch (Exception $e)
{
	$mReturnSts = "wait";
}

// $mReturnSts = "end";
if ($mReturnSts == "")
	$mReturnSts = "wait";
?>

<script>
	window.top.window.uploadEnd(<?php echo "'".$mReturnSts."'"; ?>);
</script>
