<?php
// Prevent direct access.
if (!defined('ABSPATH')) {
    exit;
}

/**
 * HTML template for å vise Automatic Protection siden.
 *
 * Forventer å motta følgende variabler:
 *
 * $msg = Eventuell success/error melding som skal settes inn på toppen av siden.  
 * $authentication = Tilsvarende $autoProtectAuth array fra FirewallConfig.
 * $rateLimit = Tilsvarende $rateLimit array fra FirewallConfig.
 */
?>

<div class="wrap wtb-firewall-admin">
    <h1>Automatic Protection</h1>
    <?php echo wp_kses_post($msg); ?>

    <p>
        Configure automatic protections such as blocking IPs that repeatedly fail authentication.
    </p>

    <div class="card">
        <h2>Auto-blocked IPs</h2>       
        <p>
            Review IPs that were automatically blocked and manage them.
        </p>

        <p>
            <a href="<?php echo esc_url(admin_url('admin.php?page=wtb-firewall-auto-protection-rules')); ?>" class="button button-secondary">
                View auto-blocked IP list</a>
        </p>

        <p>
            You can unblock specific IPs or convert them into permanent rules.
        </p>
    </div>

    <form method="POST" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
        <input type="hidden" name="action" value="wtb_auto_protect_block_authentication" />        
        <?php wp_nonce_field('wtb_firewall_auto_protect', 'wtb_firewall_nonce'); ?>

        <div class="card">
            <h2>Auto-block failed authentication</h2>            
            <p>
                Automatically block IPs that fail authentication too many times within a time window.
            </p>

            <table class="form-table" role="presentation">
                <tbody>

                    <tr>
                        <th>Enable auto-blocking</th>
                        <td>
                            <input type="checkbox" name="auth_auto_block_enabled" <?php checked($authentication['enabled'], true); ?> value="1" />
                            Enable automatic blocking for repeated failed authentication
                            <p>
                                When enabled, the plugin will track failed authentication attempts and block the IP when limits are exceeded.
                            </p>
                        </td>
                    </tr>

                    <tr>
                        <th>
                            Block after
                        </th>
                        <td>
                            <input type="number" class="small-text" name="auth_auto_block_threshold" min="1" step="1" value="<?php echo esc_html($authentication['attempts']); ?>" />
                            failed attempts
                            <p>
                                Number of failed authentication attempts allowed before the IP is blocked.
                            </p>
                        </td>
                    </tr>

                    <tr>
                        <th>
                            Within
                        </th>
                        <td>
                            <input type="number" class="small-text" name="auth_time_span" min="1" step="1" value="<?php echo esc_html($authentication['timeSpan']); ?>" />                           
                            <select name="time_span_unit">
                                <option value="minutes" <?php selected($authentication['timeSpanUnit'], 'minutes'); ?> >minutes</option>   
                                <option value="hours" <?php selected($authentication['timeSpanUnit'], 'hours'); ?> >hours</option>   
                                <option value="days" <?php selected($authentication['timeSpanUnit'], 'days'); ?> >days</option>                                   
                            </select>

                            <p>
                                Time window used when counting failed attempts (e.g. “10 attempts within 15 minutes”).
                            </p>
                        </td>
                    </tr>

                    <tr>
                        <th>
                            Block duration
                        </th>
                        <td>
                            <input type="number" class="small-text" name="auth_auto_block_duration" min="1" step="1" value="<?php echo esc_html($authentication['blockTime']); ?>" />                            
                            <select name="block_time_unit">
                                <option value="minutes" <?php selected($authentication['blockTimeUnit'], 'minutes'); ?> >minutes</option>   
                                <option value="hours" <?php selected($authentication['blockTimeUnit'], 'hours'); ?> >hours</option>   
                                <option value="days" <?php selected($authentication['blockTimeUnit'], 'days'); ?> >days</option>                                   
                            </select>
                            <p>
                                How long an IP stays blocked when auto-block triggers.
                            </p>
                        </td>
                    </tr>

                    <tr>
                        <th>Scope</th>
                        <td>
                            <fieldset>
                                <input type="checkbox" name="auth_auto_block_wp_login" value="1" <?php checked($authentication['wpLogin'], true); ?> />
                                Apply to WP login (wp-login.php / wp-admin)

                                <br><br>

                                <input type="checkbox" name="auth_auto_block_xmlrpc" value="1" <?php checked($authentication['XMLRPC'], true); ?> />
                                Apply to XML-RPC (xmlrpc.php)

                                <br><br>

                                <p>
                                    Choose which authentication surfaces should contribute to the failed-attempt counter.
                                </p>
                            </fieldset>
                        </td>
                    </tr>

                </tbody>
            </table>

            <p class="submit">
                <button type="submit" class="button button-primary">Save settings</button>
            </p>

        </div>

    </form>




    <div class="card">
        <h2>Rate Limiting</h2>
        <p>
            Configure basic request rate limiting and automatic blocking of IPs that generate too many 404 errors within one minute.', 'wtb-firewall');
        </p>

        <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
            <input type="hidden" name="action" value="wtb_firewall_rate_limit">
            <?php wp_nonce_field('wtb_firewall_rate_limit', 'wtb_firewall_nonce'); ?>

            <table class="form-table" role="presentation">
                <tbody>
                    <tr>
                        <th scope="row">                            
                            Enable rate limiting
                        </th>
                        <td>                            
                            <input
                                type="checkbox"                                
                                name="rate_limit_enabled"
                                value="1"
                                <?php checked(!empty($rateLimit['enabled'])); ?>
                                >
                            Enable request rate limiting
                            <p class="description">
                                When enabled, the firewall can temporarily block IPs that exceed the allowed number of requests per minute.
                            </p>
                        </td>
                    </tr>

                    <tr>
                        <th scope="row">                            
                            Requests per minute                           
                        </th>
                        <td>
                            <input
                                type="number"
                                class="small-text"
                                min="1"
                                step="1"                                
                                name="rate_limit_requests_per_minute"
                                value="<?php echo esc_attr($rateLimit['requestsPerMinute']); ?>"
                                >
                            <p class="description">
                                Maximum number of requests allowed from one IP during a one-minute window.
                            </p>
                        </td>
                    </tr>

                    <tr>
                        <th scope="row">                            
                            Block duration (minutes)                           
                        </th>
                        <td>
                            <input
                                type="number"
                                class="small-text"
                                min="1"
                                step="1"
                                name="rate_limit_block_minutes"
                                value="<?php echo esc_attr($rateLimit['blockMinutes']); ?>"
                                >
                            <p class="description">
                                How long an IP should be blocked after exceeding the request limit.
                            </p>
                        </td>
                    </tr>

                    <tr>
                        <th scope="row">
                            Enable 404 monitoring
                        </th>
                        <td>                            
                            <input
                                type="checkbox"                                
                                name="rate_limit_404_enabled"
                                value="1"
                                <?php checked($rateLimit['errors404Enabled']); ?>
                                >
                            Block IPs that trigger too many 404 errors
                            <p class="description">
                                Useful for detecting bots or scanners requesting many non-existent URLs.
                            </p>
                        </td>
                    </tr>

                    <tr>
                        <th scope="row">                            
                            404 errors per minute                            
                        </th>
                        <td>
                            <input
                                type="number"
                                class="small-text"
                                min="1"
                                step="1"                                
                                name="rate_limit_404_per_minute"
                                value="<?php echo esc_attr($rateLimit['errors404PerMinute']); ?>"
                                >
                            <p class="description">
                                Maximum number of 404 responses allowed from one IP during a one-minute window before it is blocked.
                            </p>
                        </td>
                    </tr>

                    <tr>
                        <th scope="row">                            
                            404 block duration (minutes)                            
                        </th>
                        <td>
                            <input
                                type="number"
                                class="small-text"
                                min="1"
                                step="1"                                
                                name="rate_limit_404_block_minutes"
                                value="<?php echo esc_attr($rateLimit['errors404BlockMinutes']); ?>"
                                >
                            <p class="description">
                                How long an IP should be blocked after exceeding the 404 limit.
                            </p>
                        </td>
                    </tr>
                </tbody>
            </table>

            <?php submit_button(__('Save settings', 'wtb-firewall')); ?>
        </form>
    </div>


</div>
