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

/*
 * HTML template for å vise Log siden.
 * 
 * Forventer å motta følgende variabler:
 * 
 * $msg = Eventuell success/error melding som skal settes inn på toppen av siden.
 * $eventType = Event type som vises i loggen. 
 * $ip = IP addresse som skal vises i filter IP tekstfeltet.
 * $num = Antall log entries per side.
 * $log = Listen over log innholdet. Klar til å settes inn i tabellen.
 * $currentPage = Nåværende side.
 * $totalPages = Totalt antall sider med log entries.
 * $totalLogEntries = Totalt antall log entries.
 * $prevPage = HTML som settes inn for previous page knapp.
 * $nextPage = HTML som settes inn for next page knapp.
 * $firstPage = HTML som settes inn for first page knapp.
 * $lastPage = HTML som settes inn for last page knapp.
 */
?>
<div class="wrap">
    <h1>Firewall Log</h1> 
    <?php echo wp_kses_post($msg); ?>

    <div class="tablenav top">
        <!-- Filters -->
        <form method="GET" action="<?php echo esc_url(admin_url('admin.php')); ?>">            
            <input type="hidden" name="page" value="wtb-firewall-log">            
            <input type="hidden" name="num" value="<?php echo esc_html($num); ?>">

            <div class="alignleft actions">                                
                <select name="event_type">
                    <option value="" <?php echo selected($eventType, null); ?> >All Event Types</option>
                    <option value="<?php echo esc_html(\WtbFirewall\Firewall\EventType::CONNECTION_BLOCKED->value); ?>" <?php echo selected($eventType, \WtbFirewall\Firewall\EventType::CONNECTION_BLOCKED->value); ?> >Blocked by firewall.</option>
                    <option value="<?php echo esc_html(\WtbFirewall\Firewall\EventType::WPLOGIN_AUTH_FAILED->value); ?>" <?php echo selected($eventType, \WtbFirewall\Firewall\EventType::WPLOGIN_AUTH_FAILED->value); ?> >Wp-login authentication failed.</option>
                    <option value="<?php echo esc_html(\WtbFirewall\Firewall\EventType::XMLRPC_AUTH_FAILED->value); ?>" <?php echo selected($eventType, \WtbFirewall\Firewall\EventType::XMLRPC_AUTH_FAILED->value); ?> >XMLRPC authentication failed.</option>                    
                    <option value="<?php echo esc_html(\WtbFirewall\Firewall\EventType::RATELIMIT_REQUESTS->value); ?>" <?php echo selected($eventType, \WtbFirewall\Firewall\EventType::RATELIMIT_REQUESTS->value); ?> >Rate limiting, too many requests.</option>
                    <option value="<?php echo esc_html(\WtbFirewall\Firewall\EventType::RATELIMIT_404->value); ?>" <?php echo selected($eventType, \WtbFirewall\Firewall\EventType::RATELIMIT_404->value); ?> >Rate limiting, too many 404 errors.</option>
                </select>
                &nbsp;&nbsp;
                <input type="text" name="ip" value="<?php echo esc_html($ip); ?>" placeholder="IP address (IPv4/IPv6)" inputmode="text"/>

                <button type="submit" class="button">Filter</button>
                <a style="text-align:center" class="button button-secondary" href="<?php echo esc_url(admin_url('admin.php?page=wtb-firewall-log')); ?>">Reset</a>
            </div>
        </form>


        <form action="<?php echo esc_url(admin_url('admin.php')); ?>" method="GET">
            <input type="hidden" name="page" value="wtb-firewall-log">            
            <input type="hidden" name="ip" value="<?php echo esc_html($ip); ?>">
            <input type="hidden" name="event_type" value="<?php echo esc_html($eventType); ?>">
            <div class="alignright actions">                                  
                <select name="num">
                    <option value="50" <?php selected($num, 50); ?> >50 / page</option>
                    <option value="100" <?php selected($num, 100); ?> >100 / page</option>
                    <option value="250" <?php selected($num, 250); ?> >250 / page</option>
                    <option value="500" <?php selected($num, 500); ?> >500 / page</option>
                </select>
                &nbsp;&nbsp;
                <button type="submit" class="button button-secondary">Apply</button>
            </div>         
        </form>
    </div>

    <!-- Table -->
    <table class="wp-list-table widefat fixed striped">
        <thead>
            <tr>
                <th scope="col" class="manage-column column-event-time">Event time</th>
                <th scope="col" class="manage-column column-ip">IP address</th>
                <th scope="col" class="manage-column column-type">Event type</th>
                <th scope="col" class="manage-column column-details">Details</th>         
            </tr>
        </thead>

        <tbody id="the-list">
            <?php echo wp_kses_post($log); ?>               
        </tbody>

        <tfoot>
            <tr>
                <th scope="col" class="manage-column">Event time</th>
                <th scope="col" class="manage-column">IP address</th>
                <th scope="col" class="manage-column">Event type</th>
                <th scope="col" class="manage-column">Details</th>          
            </tr>
        </tfoot>
    </table>

    <!-- Pagination -->
    <div class="tablenav bottom">
        <div class="tablenav-pages">
            <span class="displaying-num"><?php echo esc_html($totalLogEntries); ?> items</span>

            <span class="pagination-links">
                <?php echo wp_kses_post($firstPage); ?>
                <?php echo wp_kses_post($prevPage); ?>

                <?php echo esc_html($currentPage); ?> of <?php echo esc_html($totalPages); ?>                      

                <?php echo wp_kses_post($nextPage); ?>
                <?php echo wp_kses_post($lastPage); ?>
            </span>
        </div>

        <br class="clear" />
    </div>  
</div>
