<?php

$altdb = 'u943309104_bukid';
$altuser = 'u943309104_bukidb';
$altpass = 'lB&3I=t6';
$althost = 'mysql';
$root_passDB = 'as73n1^1dhu';

$defaultAdminAppUser = '777';
$defaultAdminAppPassword = 'OmegaPsilon32!';
$InitializeDB = false;

$GLOBALS['defaultAdminAppUser'] = $defaultAdminAppUser;
$GLOBALS['defaultAdminAppPassword'] =$defaultAdminAppPassword;


function loadSettings()
{
    $filePath = 'settings/settings.json';
    if (file_exists($filePath)) {
        $jsonContent = file_get_contents($filePath);

        $MainSettings = json_decode($jsonContent, true);
        return $MainSettings;
    } else {
        return null;
    }
}

$MainSettings = loadSettings();

if (!file_exists('settings/DBInitialized') && $InitializeDB) {
    RunDBInit();
}

function DB()
{
    global $althost;
    global $altdb;
    global $altuser;
    global $altpass;
    global $defaultAdminAppUser;
    global $defaultAdminAppPassword;

    try {
        $db = opennewdb($altdb, $althost, $altuser, $altpass);

    } catch (Exception $e) {
        echo $e->getMessage() . PHP_EOL;
        try {
            $db = opennewdb('ta');
        } catch (Exception $le) {
            echo 'DB Error';
            exit;
        }

    }
    return $db;
}


function RunDBInit()
{
    global $althost;
    global $altdb;
    global $altuser;
    global $altpass;
    $mysqli = new mysqli($althost, $altuser, $altpass, $altdb);
    echo 'Initializing DB...' . PHP_EOL;
    if ($mysqli->connect_error) {
        echo 'Error...' . '<br>';
        die("Connection failed: " . $mysqli->connect_error);
    }
    echo 'Connected to DB...' . '<br>';

    $query = file_get_contents("init/mysql.sql");

    if ($mysqli->multi_query($query)) {
        do {

        } while ($mysqli->more_results() && $mysqli->next_result());
        file_put_contents('settings/DBInitialized', 'true');
        echo "SQL imported successfully.".'<br>' ;
    } else {
        echo "Error importing SQL: " . $mysqli->error.'<br>';
    }
}

require_once('lib.php');
require_once('DB.php');
require_once('lib/logins.php');
