<?php
// Auto-identification vers MariaDB GMAO
$server = 'mariadb';
$username = 'gmao';
$password = getenv('MARIADB_PASSWORD') ?: 'gmao123';
$database = 'gmao_db';

// Forcer l'auto-login dans Adminer
$_POST['auth']['server'] = $server;
$_POST['auth']['username'] = $username;
$_POST['auth']['password'] = $password;
$_POST['auth']['db'] = $database;

$auth = [
    'server' => $server,
    'username' => $username,
    'password' => $password,
    'db' => $database,
];
$_SESSION['token'] ??= bin2hex(random_bytes(16));

// If accessed directly, redirect to auto-login
if (isset($_GET['auto'])) {
    // Auto-login by setting the auth cookie
    $auth_json = json_encode($auth);
    setcookie('adminer', base64_encode($auth_json), time() + 3600, '/', '', $_SERVER['HTTPS'] ?? false);
    header('Location: ?server=' . urlencode($server) . '&username=' . urlencode($username) . '&db=' . urlencode($database));
    exit;
}
