<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

$pdo = null;
$bdd = null;

$charset = 'utf8mb4';
$fuseau = 'Europe/Paris';
$langue = 'fr';

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

/*
|--------------------------------------------------------------------------
| MULTI
|--------------------------------------------------------------------------
*/

if (file_exists(__DIR__ . '/idl_multi.php')) {
    require_once __DIR__ . '/idl_multi.php';
}

if (!empty($multi) && $multi === "ON") {

    if (isset($_POST['etabchoix'])) {
        $_SESSION['etabchoix'] = filter_input(
            INPUT_POST,
            'etabchoix',
            FILTER_SANITIZE_FULL_SPECIAL_CHARS
        );
    }

} else {

    if (file_exists(__DIR__ . '/idl_connect_data.php')) {
        require_once __DIR__ . '/idl_connect_data.php';
    }
}

/*
|--------------------------------------------------------------------------
| VALIDATION CONFIG
|--------------------------------------------------------------------------
*/

if (
    empty($server) ||
    empty($user) ||
    !isset($pwd) ||
    empty($base)
) {
    return;
}

if (
    !isset($prefix) ||
    !preg_match('/^[a-zA-Z0-9_]+$/', $prefix)
) {
    return;
}

/*
|--------------------------------------------------------------------------
| CONNEXION DB
|--------------------------------------------------------------------------
*/

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

try {

    $bdd = mysqli_connect($server, $user, $pwd, $base);

    if (!empty($multi) && $multi === "ON") {
        if(isset($prefident)) {
            $_SESSION['prefident'] = $prefident;
        }
    }
    mysqli_set_charset($bdd, $charset);

    $dsn = "mysql:host=$server;dbname=$base;charset=$charset";

    $pdo = new PDO($dsn, $user, $pwd, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ]);

} catch (Throwable $e) {

    error_log("Connexion DB impossible : " . $e->getMessage());

    $pdo = null;
    $bdd = null;

    return;
}

/*
|--------------------------------------------------------------------------
| PARAMETRES APPLICATION
|--------------------------------------------------------------------------
*/

try {

    $stmt = $pdo->prepare("
        SELECT valeur
        FROM {$prefix}_parametres
        WHERE param_nom = :param
    ");

    // Fuseau
    $stmt->execute(['param' => 'fuseau']);

    if ($tmp = $stmt->fetch()) {
        $fuseau = $tmp['valeur'];
    }

    // Charset
    $stmt->execute(['param' => 'charset']);

    if ($tmp = $stmt->fetch()) {
        $charset = $tmp['valeur'];

        mysqli_set_charset($bdd, $charset);
    }

    // Langue
    $stmt->execute(['param' => 'language']);

    if ($tmp = $stmt->fetch()) {
        $langue = $tmp['valeur'];
    }

} catch (Throwable $e) {

    error_log("Lecture paramètres impossible : " . $e->getMessage());
}

/*
|--------------------------------------------------------------------------
| ENV
|--------------------------------------------------------------------------
*/

date_default_timezone_set($fuseau);

if (
    $server === "127.0.0.1" ||
    $server === "localhost"
) {
    try {
        mysqli_query($bdd, "SET sql_mode=''");
    } catch (Throwable $e) {
    }
}

/*
|--------------------------------------------------------------------------
| LANGUE
|--------------------------------------------------------------------------
*/

if (!isset($_SESSION['lang'])) {
    $_SESSION['lang'] = $langue;
}

$lang = preg_replace('/[^a-z]/', '', $_SESSION['lang']);

if (!defined('BASE_PATH')) {
    define('BASE_PATH', dirname(__DIR__));
}

$langFile = BASE_PATH . "/include/lang/{$lang}.php";

$translations = [];
$inverseTranslations = [];

if (file_exists($langFile)) {

    $langData = require $langFile;

    if (is_array($langData)) {

        $translations = $langData['translations'] ?? [];
        $inverseTranslations = $langData['inverseTranslations'] ?? [];
    }
}

if (file_exists(__DIR__ . '/idl_variables.php')) {
    require_once __DIR__ . '/idl_variables.php';
}
?>