<?php
/**
 * Banned URL 403 Block Page
 *
 * Displays when a request is blocked due to accessing a banned/trapped URL.
 *
 * @package VMPFence
 * @since 2.0.0
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Ensure required variables are available
if (!isset($violation) || !is_array($violation)) {
    $violation = array(
        'type' => 'banned_url',
        'action' => 'block',
        'reason' => 'Accessed banned URL',
        'blocked_time' => time(),
        'expiration' => 0
    );
}

$retry_after = VMPFence_Config::get('rate_block_duration', 5) * 60; // Convert minutes to seconds
$custom_text = VMPFence_Config::get('custom_block_text', '');
$block_duration_minutes = VMPFence_Config::get('rate_block_duration', 5);

// Calculate time until unblock
$time_remaining = 0;
if (isset($violation['expiration']) && $violation['expiration'] > 0) {
    $time_remaining = $violation['expiration'] - time();
    $time_remaining = max(0, $time_remaining); // Ensure non-negative
}

$minutes_remaining = ceil($time_remaining / 60);

// Send 403 Forbidden header
if (!headers_sent()) {
    http_response_code(403);
    header('Retry-After: ' . $retry_after);
    header('Content-Type: text/html; charset=UTF-8');
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Access Denied - Banned URL</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body { 
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
            margin: 0;
            padding: 0;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .container { 
            max-width: 600px; 
            width: 90%;
            background: white; 
            padding: 50px 40px; 
            border-radius: 10px; 
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
        }
        .error-code {
            font-size: 72px;
            font-weight: bold;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            text-align: center;
            margin: 0 0 20px 0;
            line-height: 1;
        }
        h1 { 
            color: #2d3748; 
            font-size: 28px;
            text-align: center;
            margin: 0 0 15px 0;
            font-weight: 600;
        }
        .subtitle {
            color: #718096;
            text-align: center;
            font-size: 16px;
            margin: 0 0 30px 0;
        }
        .alert-box {
            background: #fff5f5;
            border-left: 4px solid #fc8181;
            padding: 20px;
            border-radius: 5px;
            margin: 25px 0;
        }
        .alert-title {
            color: #c53030;
            font-weight: 600;
            font-size: 16px;
            margin: 0 0 10px 0;
        }
        .alert-message {
            color: #742a2a;
            line-height: 1.6;
            margin: 0;
        }
        .info-box {
            background: #f7fafc;
            padding: 20px;
            border-radius: 8px;
            margin: 25px 0;
            border: 1px solid #e2e8f0;
        }
        .info-row {
            display: flex;
            justify-content: space-between;
            padding: 10px 0;
            border-bottom: 1px solid #e2e8f0;
        }
        .info-row:last-child {
            border-bottom: none;
        }
        .info-label {
            color: #4a5568;
            font-weight: 500;
        }
        .info-value {
            color: #2d3748;
            font-weight: 600;
        }
        .timer-box {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 25px;
            border-radius: 8px;
            text-align: center;
            margin: 25px 0;
        }
        .timer-label {
            font-size: 14px;
            opacity: 0.9;
            margin: 0 0 10px 0;
        }
        .timer-value {
            font-size: 36px;
            font-weight: bold;
            margin: 0;
        }
        .custom-text {
            background: #edf2f7;
            padding: 20px;
            border-radius: 8px;
            margin: 25px 0;
            color: #2d3748;
            line-height: 1.6;
        }
        .footer {
            font-size: 13px;
            color: #a0aec0;
            text-align: center;
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #e2e8f0;
        }
        .shield-icon {
            text-align: center;
            font-size: 48px;
            margin-bottom: 20px;
        }
        @media (max-width: 600px) {
            .container {
                padding: 30px 20px;
            }
            .error-code {
                font-size: 56px;
            }
            h1 {
                font-size: 24px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="shield-icon">🛡️</div>
        <div class="error-code">403</div>
        <h1>Access Denied</h1>
        <p class="subtitle">Your IP address has been temporarily blocked</p>
        
        <div class="alert-box">
            <div class="alert-title">⚠️ Security Alert</div>
            <div class="alert-message">
                You attempted to access a restricted or monitored URL. For security reasons, your IP address has been automatically blocked.
            </div>
        </div>

        <?php if ($time_remaining > 0): ?>
        <div class="timer-box">
            <p class="timer-label">TIME REMAINING UNTIL UNBLOCK</p>
            <p class="timer-value"><?php echo esc_html($minutes_remaining); ?> minute<?php echo $minutes_remaining !== 1 ? 's' : ''; ?></p>
        </div>
        <?php endif; ?>

        <div class="info-box">
            <div class="info-row">
                <span class="info-label">Block Type</span>
                <span class="info-value">Banned URL Access</span>
            </div>
            <div class="info-row">
                <span class="info-label">Block Duration</span>
                <span class="info-value"><?php echo esc_html($block_duration_minutes); ?> minutes</span>
            </div>
            <div class="info-row">
                <span class="info-label">Status</span>
                <span class="info-value" style="color: #c53030;">Blocked</span>
            </div>
            <?php if (isset($violation['blocked_hits']) && $violation['blocked_hits'] > 0): ?>
            <div class="info-row">
                <span class="info-label">Repeated Attempts</span>
                <span class="info-value"><?php echo esc_html($violation['blocked_hits']); ?></span>
            </div>
            <?php endif; ?>
        </div>

        <?php if (!empty($custom_text)): ?>
        <div class="custom-text">
            <?php echo wp_kses_post(wpautop($custom_text)); ?>
        </div>
        <?php endif; ?>

        <div class="footer">
            <p><strong>What happened?</strong></p>
            <p>The URL you attempted to access is on our security watch list. This could be because:</p>
            <ul style="text-align: left; display: inline-block; margin: 10px auto;">
                <li>The URL doesn't exist on this server</li>
                <li>The URL is known to be targeted by attackers</li>
                <li>The URL contains sensitive administrative paths</li>
            </ul>
            <p style="margin-top: 20px;">If you believe this is an error, please contact the site administrator.</p>
            <p style="margin-top: 20px; font-size: 11px;">Protected by VMPFence Security • Incident logged for review</p>
        </div>
    </div>
</body>
</html>
