<?php
// connect.php
// ----------------------------------------------------------------------------------------

// ----------------------------------------------------------------------------------------
header('Cache-Control: no-cache, no-store, must-revalidate');   
header('Expires: 0');
// Header to allow Cross-Server AJAX Connection
header('Access-Control-Allow-Origin: *');
set_time_limit(0);
date_default_timezone_set('Europe/London');
// Enable exceptions for MySQLi errors
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// Return Default mySql Connection
function dbConnection()
{
// get default System Settings from xml file    
    $system = "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++) 
	{			
		$systemId = "";
		$path = "";
		$dbHost = "";
		$dbUser = "";
		$dbPwd = "";
		$dbase = "";
		$mqServer = "";
		$company = "";
		if (("".$xml->systems->system[$i]->systemid."" == "") || ("".$xml->systems->system[$i]->systemid."" == false) || ("".$xml->systems->system[$i]->systemid."" == false))
			break;
		
		$systemId = "".$xml->systems->system[$i]->systemid."";
		if (($systemId == "") || ($systemId == false))
			break;
		
        $conn = array();
// Store System Settings from "trandef/systemsettings.xml"		
		if (strToUpper($system) == strToUpper($systemId))
		{
			$path = "".$xml->systems->system[$i]->path."";
			$dbHost = "".$xml->systems->system[$i]->dbhost."";
			$dbUser = "".$xml->systems->system[$i]->dbuser."";
			$dbPwd = "".$xml->systems->system[$i]->dbpwd."";
			$dbase = "".$xml->systems->system[$i]->dbase."";
			break;
		}
		if (($mSystemId == "") || ($mSystemId == false))
			break;
	}
    $connection = mysqli_connect($dbHost,$dbUser,$dbPwd,$dbase)or exit("FAIL-Failed to connect to Database: " . mysqli_connect_error().":".$system);
    
    $conn['path'] = $path;
    $conn['host'] = $dbHost;
    $conn['user'] = $dbUser;
    $conn['pwd'] = $dbPwd;
    $conn['dbase'] = $dbase;
    $conn['connection'] = $connection;
    
	return $conn;
}
?>