<?php
ini_set("display_errors", 0);
error_reporting(0);
// Optional local config override (do not commit to public repos)
$localConfig = __DIR__ . '/config.local.php';
if (file_exists($localConfig)) {
  include $localConfig;
}
// Environment variable support
$host = getenv('ONEPANEL_DB_HOST') ?: ($host ?? 'localhost');
$username = getenv('ONEPANEL_DB_USER') ?: ($username ?? '');
$password = getenv('ONEPANEL_DB_PASS') ?: ($password ?? '');
$dbname = getenv('ONEPANEL_DB_NAME') ?: ($dbname ?? 'onepanel');

//Add database details below:
// Add database details below (can be overridden by includes/config.local.php or env vars)
$host = $host ?? "localhost";
$username = $username ?? "";
$password = $password ?? "";
$dbname = $dbname ?? "onepanel";

//<-- name of database you just created
$GLOBALS['panelroot'] = '';
//db version
$expectedVersion = 11;
//app version
$appVersion = "1.4";
$dbConnected = false;
$mysqli = new mysqli($host, $username, $password, $dbname);
if ($mysqli->connect_errno) {
	//Error
} else {
	$dbConnected = true;
}
$insVersion = 0;
try { 
	$insResult = $mysqli->prepare("SELECT * FROM config");
	if ($insResult) {
		$insResult->execute();
		$insResult = $insResult->get_result();
		$configArray = $insResult->fetch_array();
		$insVersion = $configArray['dbversion'];
		$GLOBALS['panelroot'] = $configArray['panel_root'];
		if (substr($GLOBALS['panelroot'], -1) != "/") {
			$GLOBALS['panelroot'] .= "/";
			
		}
		$authVar = md5($GLOBALS['panelroot'] . $insVersion);
	}
	$isInstalled = $insResult !== false;
	$dbIsLatest = $expectedVersion == $insVersion;
} catch (Exception $e) {
	$isInstalled = false;
	$dbIsLatest = false;
}
