<?php
/**
 * PAGE DE BANNISSEMENT - re-info.fr
 * =================================
 * Affiche la durée et l'heure de déblocage de l'IP.
 * Fichier de ban : logs/banned/<ip> = "TIMESTAMP_EXPIRATION|DUREE_SECONDES"
 * (ancien format "TIMESTAMP" seul : durée inconnue -> "24 heures" par défaut)
 * Atteinte via /banned (réécrit en banned.php par le .htaccess).
 */

@ini_set('display_errors', '0');
@ini_set('display_startup_errors', '0');
@error_reporting(0);

$banDir = __DIR__ . '/logs/banned';
$ip     = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
$banFile = $banDir . '/' . $ip;

$unlockText = 'Réessayez plus tard.';
$durationText = '24 heures';
if (file_exists($banFile)) {
    $content = explode('|', (string)@file_get_contents($banFile));
    $expires = (int)$content[0];
    if ($expires > time()) {
        $unlockText = 'Déblocage le <strong>' . date('d/m/Y \à H:i', $expires) . '</strong>.';
        if (isset($content[1]) && (int)$content[1] > 0) {
            $durationText = formatDuration((int)$content[1]);
        }
    }
}

function formatDuration(int $seconds): string
{
    if ($seconds >= 48 * 3600 && $seconds % (24 * 3600) === 0) {
        $days = $seconds / (24 * 3600);
        return $days . ' jour' . ($days > 1 ? 's' : '');
    }
    if ($seconds % 3600 === 0) {
        $hours = $seconds / 3600;
        return $hours . ' heure' . ($hours > 1 ? 's' : '');
    }
    if ($seconds % 60 === 0) {
        $mins = $seconds / 60;
        return $mins . ' minute' . ($mins > 1 ? 's' : '');
    }
    return $seconds . ' secondes';
}

header('Content-Type: text/html; charset=UTF-8');
header('Cache-Control: no-store');
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex, nofollow">
<title>Accès refusé - IP bannie</title>
<style>
  body { font-family: Arial, sans-serif; background: #1a1a1a; color: #eee;
         display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; }
  .box { text-align: center; max-width: 600px; padding: 40px; background: #2a2a2a;
         border: 1px solid #c0392b; border-radius: 8px; }
  h1 { color: #e74c3c; }
</style>
</head>
<body>
  <div class="box">
    <h1>Accès refusé</h1>
    <p>Votre adresse IP est bloquée pour <strong><?= $durationText ?></strong>.
       <?= $unlockText ?></p>
  </div>
</body>
</html>
