<?php
$root = $_SERVER['DOCUMENT_ROOT'];
if (empty($root))
{
	$root = (__DIR__);
}

$root = str_replace("\\","/",$root);
$dir = (dirname(__FILE__));
$dir = str_replace("\\","/",$dir);

// get Base Directory
$baseDir = $dir;
for ($i=0;$i<10;$i++)
{
	$testDir = dirname($baseDir);
	$testDir = str_replace("\\","/",$testDir);
	
	$baseDir = str_replace("\\","/",$baseDir);
	if ($baseDir == $root)
	{
		break;
	}
	if ($testDir == $root)
	{
		break;
	}
	$baseDir = dirname($baseDir);
}
if (empty($env))
{
	if (isset($_GET['env']))
	{
		$env = $_GET['env'];
	}
	else
	{
		$env = "";
	}
}
if (!empty($env))
{
	$envFilePath = $baseDir.'/'.$env.'/.env';
}
else
{
	$envFilePath = $baseDir.'/.env';
}
$environmentVariables = parse_ini_file($envFilePath, true, INI_SCANNER_TYPED);

// DB credentials
define('DB_HOST', $environmentVariables['database']['DB_HOST']);
define('DB_NAME', $environmentVariables['database']['DB_DATABASE']);
define('DB_USERNAME', $environmentVariables['database']['DB_USERNAME']);
define('DB_PASSWORD', $environmentVariables['database']['DB_PASSWORD']);
// Server
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT'] . $baseDir);
define('SERVER_DOMAIN', $environmentVariables['server']['SERVER_DOMAIN']);
define('JAVA_PATH', $environmentVariables['server']['JAVA_PATH']);

// Session
define('SESSION_NAME', $environmentVariables['php-session']['NAME']);
define('REGENERATION_PERIOD', (int)$environmentVariables['php-session']['REGENERATION_PERIOD']);
define('SESSION_TIMEOUT', (int)$environmentVariables['php-session']['TIMEOUT']);
define('SESSION_TIMEZONE', $environmentVariables['php-session']['TIMEZONE']);

// App
define('APP_TITLE_NAME', 'RRMC / VIMS');

// Admin allowed users array
// Sets users that are allowed to delete containers from inventory
define('ADMIN_USERS', [1, 2, 4]); // 1 - SUPER USER, 2 - ADMINDATA, 4 - ADMINEDIT

function dbConnection()
{
	$connection = mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME)or exit("FAIL-Failed to connect to Database: " . mysqli_connect_error());
	$conn['path'] = JAVA_PATH;
    $conn['host'] = DB_HOST;
    $conn['user'] = DB_USERNAME;
    $conn['pwd'] = DB_PASSWORD;
    $conn['dbase'] = DB_NAME;
    $conn['connection'] = $connection;
	
	return $conn;
}