<?php

// Return Default mySql Connection
function defaultDbConnection($mSystem)
{
// Get System Definition XML from URL
	$mData = file_get_contents("systemsettings.xml", FILE_USE_INCLUDE_PATH);
// 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;
	}
	

	$connection = mysqli_connect($mDbHost,$mDbUser,$mDbPwd,$mDbase)or die("FAIL-Failed to connect to Database: " . mysqli_connect_error().":".$mSystem);
	return $connection;
}

// Return Default mySql Connection
function defaultDbConnectionVal($mUser)
{
// Get User Definition XML from URL
	$mData = file_get_contents("../../vimsconfig/usersettings.xml");
	if ($mData == "")
	{
	}
// Parse XML to XML Object Array
	$xml = simplexml_load_string($mData, 'SimpleXMLElement', LIBXML_NOCDATA);
	
// Split-out User Definitions from XML 'fields' Nodes
	$i = 0;
	$mUserId = "";
	$mMenu = "";
	$mPath = "";
	$mSystem = "";
	$mCompany = "";
	$mMqServer = "";
	$mStatus = "";
	for ($i = 0; $i <= 150; $i++) 
	{	
		if (("".$xml->users->user[$i]->name."" == "") || ("".$xml->users->user[$i]->name."" == false) || ("".$xml->users->user[$i]->name."" == false))
			break;
		
		$mUserId = "".$xml->users->user[$i]->name."";
		if (($mUserId == "") || ($mUserId == false))
			break;
		
		if ((strtolower($mUserId) == "default") && ($mSystem == ""))
		{
			$mUserId = $mUser;
			$mStatus = "".$xml->users->user[$i]->status."";
			$mPath = "".$xml->users->user[$i]->path."";
			$mMenu = "".$xml->users->user[$i]->menu."";
			$mSystem = "".$xml->users->user[$i]->system."";
			$mCompany = "".$xml->users->user[$i]->company."";
			$mDbHost = "".$xml->users->user[$i]->dbhost."";
			$mDbUser = "".$xml->users->user[$i]->dbuser."";
			$mDbPwd = "".$xml->users->user[$i]->dbpwd."";
			$mDbase = "".$xml->users->user[$i]->dbase."";
			$mMqServer = "".$xml->users->user[$i]->mqserver."";			
		}
		else if (strToUpper($mUserId) == $mUser)
		{
			$mStatus = "".$xml->users->user[$i]->status."";
			$mPath = "".$xml->users->user[$i]->path."";
			$mMenu = "".$xml->users->user[$i]->menu."";
			$mSystem = "".$xml->users->user[$i]->system."";
			$mCompany = "".$xml->users->user[$i]->company."";
			$mDbHost = "".$xml->users->user[$i]->dbhost."";
			$mDbUser = "".$xml->users->user[$i]->dbuser."";
			$mDbPwd = "".$xml->users->user[$i]->dbpwd."";
			$mDbase = "".$xml->users->user[$i]->dbase."";
			$mMqServer = "".$xml->users->user[$i]->mqserver."";
			break;
		}

		if (($mUserId == "") || ($mUserId == false) || ($mUserId == null))
			break;
	}
// Get System-wide settings if required
	if (($mPath == "") && ($mSystem != ""))
	{	
// Get System Definition XML from URL
		$mData = file_get_contents("systemsettings.xml", FILE_USE_INCLUDE_PATH);
// 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 "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;
		}
	}
	
	$connection = $mPath.",".$mDbHost.",".$mDbUser.",".$mDbPwd.",".$mDbase;
	return $connection;
}

// Base Settings
//database
//...................
$db = "172.18.25.35";
//...................

//table connection 
//...............
$connection = mysqli_connect($db,"root","root","vantec") or die("Error " . mysqli_error($connection));
//...............

//dropdown connection 
//.................
$con = mysqli_connect($db, 'root', 'root', 'vantec');
//.................

//file Upload location
//..................
$dir = "C:/upload/input/";
//.................

// System Settings	
$mSystem = "DEFAULT";

// Get Input Parameters from POST or GET
if (!empty($_POST))
{
	$mFilter = "";
	foreach($_POST as $k1=>$v1)
	{
//		print_r("TEST".$k1.":".$v1);
		if ($mFilter != "")
			$mFilter .= "&";
		$mFilter .= $k1."=".$v1;
	}
}
else
{
	$mFilter = $_SERVER['QUERY_STRING'];
}

$mFilter = str_replace("=",":",$mFilter);
$mFilter = str_replace("&",",",$mFilter);

if (strpos($mFilter,"defaultconnection"))
{
// Explode Input String Into Array - Separated by ,
	$mArray1 = explode(",",$mFilter);
// Create Associative Array by Exploding Array Elements on :
	foreach($mArray1 as $k1=>$v1)
	{
		list($k2, $v2) = explode(":",$mArray1[$k1]);
		if ($k2 == "function")
			$mFunction = $v2;
		elseif($k2 == "system")
			$mSystem = $v2;
	}	
	$connection = defaultDbConnection($mSystem);
}
if (strpos($mFilter,"defaultconnectionval"))
{
// Explode Input String Into Array - Separated by ,
	$mArray1 = explode(",",$mFilter);
// Create Associative Array by Exploding Array Elements on :
	foreach($mArray1 as $k1=>$v1)
	{
		list($k2, $v2) = explode(":",$mArray1[$k1]);
		if ($k2 == "function")
			$mFunction = $v2;
		elseif($k2 == "system")
			$mSystem = $v2;
	}	
	$connection = defaultDbConnectionVal($mSystem);
}
else
{
	$connection = mysqli_connect($db,"root","root","vantec");
}

// echo $mConnection;
?>