<?php
if (!defined('ABSPATH')) {
    exit;
}
if (!class_exists('hpixl_securicheck_ScreenProtectionLogin')) {

    class hpixl_securicheck_ScreenProtectionLogin
    {
        public function __construct() {}

        function render_screen()
        {

?>
            <?php settings_errors(); ?>
            <div class='wrap'>
                <h1 style="display:none;"></h1>
                <div class="hpixl-securicheck-admin-audit-header">
                    <div class="hpixl-securicheck-admin-audit-header-panneau-logo">
                        <img src="<?php echo esc_html(HPIXL_SECURICHECK_PLUGIN_URL . "images/securicheck.png"); ?>" alt="logo hpixl-securicheck" />
                        <h1><?php esc_html_e('Securicheck - PROTECTION LOGIN', 'securicheck'); ?></h1>
                    </div>
                    <span>V<?php echo esc_html(HPIXL_SECURICHECK_PLUGIN_VERSION); ?></span>
                </div>

                <?php
                $active_tab = isset($_GET['tab']) ? sanitize_text_field($_GET['tab']) : 'url-connexion';
                ?>

                <h2 class="nav-tab-wrapper">
                    <a href="?page=<?php echo esc_html(HPIXL_SECURICHECK_SETTINGS_SLUG . '-protection-login'); ?>&tab=url-connexion" class="nav-tab <?php echo esc_html($active_tab == 'url-connexion' ? 'nav-tab-active' : ''); ?>"><?php esc_html_e('URL de connexion', 'securicheck'); ?></a>
                    <a href="?page=<?php echo esc_html(HPIXL_SECURICHECK_SETTINGS_SLUG . '-protection-login'); ?>&tab=brute-force-protection" class="nav-tab <?php echo esc_html($active_tab == 'brute-force-protection' ? 'nav-tab-active' : ''); ?>"><?php esc_html_e('Protection Brute Force', 'securicheck'); ?></a>
                    <a href="?page=<?php echo esc_html(HPIXL_SECURICHECK_SETTINGS_SLUG . '-protection-login'); ?>&tab=ip-bloquees" class="nav-tab <?php echo esc_html($active_tab == 'ip-bloquees' ? 'nav-tab-active' : ''); ?>"><?php esc_html_e('IP Bloquées', 'securicheck'); ?></a>
                    <a href="?page=<?php echo esc_html(HPIXL_SECURICHECK_SETTINGS_SLUG . '-protection-login'); ?>&tab=historique" class="nav-tab <?php echo esc_html($active_tab == 'historique' ? 'nav-tab-active' : ''); ?>"><?php esc_html_e('Historique', 'securicheck'); ?></a>
                </h2>

                <?php if ($active_tab == 'ip-bloquees'): ?>
                    <?php hpixl_securicheck_render_protection_login_onglet_ip_bloquees(); ?>
                <?php elseif ($active_tab == 'historique'): ?>
                    <?php hpixl_securicheck_render_protection_login_onglet_historique(); ?>
                <?php else: ?>
                    <div class='hpixl-securicheck-settings-form-panel'>
                        <form action='options.php' method='POST'>
                            <?php

                            if ($active_tab == 'url-connexion') {
                                settings_fields('hpixl_securicheck_settings_securite_groupe_connexion');
                                do_settings_sections('hpixl_securicheck_settings_securite_groupe_connexion');
                                hpixl_securicheck_render_page_securite_onglet_connexion();
                            } elseif ($active_tab == 'brute-force-protection') {
                                settings_fields('hpixl_securicheck_brute_force_groupe');
                                do_settings_sections('hpixl_securicheck_brute_force_groupe');
                                hpixl_securicheck_render_page_securite_onglet_brute_force();
                            }
                            ?>
                        </form>
                    </div>
                <?php endif; ?>

            </div>
<?php
        }
    }
}

//Ajout d'un settings pour notre plugin 
add_action('admin_init', 'hpixl_securicheck_settings_protection_login');
function hpixl_securicheck_settings_protection_login()
{
    register_setting('hpixl_securicheck_settings_securite_groupe_connexion', 'hpixl_securicheck_toggle_page_connexion_url');
    register_setting('hpixl_securicheck_settings_securite_groupe_connexion', 'hpixl_securicheck_textarea_page_connexion_url');
    register_setting('hpixl_securicheck_settings_securite_groupe_connexion', 'hpixl_securicheck_toggle_page_connexion_redirection');
    register_setting('hpixl_securicheck_settings_securite_groupe_connexion', 'hpixl_securicheck_textarea_page_connexion_redirection');
    //valeurs par défaut
    add_option('hpixl_securicheck_toggle_page_connexion_url', '0');
    add_option('hpixl_securicheck_textarea_page_connexion_url', 'cachette');
    add_option('hpixl_securicheck_toggle_page_connexion_redirection', '0');
    add_option('hpixl_securicheck_textarea_page_connexion_redirection', '404');

    register_setting('hpixl_securicheck_brute_force_groupe', 'hpixl_securicheck_toggle_brute_force');
    register_setting('hpixl_securicheck_brute_force_groupe', 'hpixl_securicheck_text_max_tentatives_brute_force');
    register_setting('hpixl_securicheck_brute_force_groupe', 'hpixl_securicheck_text_periode_tentatives_brute_force');
    register_setting('hpixl_securicheck_brute_force_groupe', 'hpixl_securicheck_text_duree_blocage_brute_force');
    register_setting('hpixl_securicheck_brute_force_groupe', 'hpixl_securicheck_toggle_block_ip_connexion_admin');

    //valeurs par défaut
    add_option('hpixl_securicheck_toggle_brute_force', '0');
    add_option('hpixl_securicheck_text_max_tentatives_brute_force', '5');
    add_option('hpixl_securicheck_text_periode_tentatives_brute_force', '5');
    add_option('hpixl_securicheck_text_duree_blocage_brute_force', '60');
    add_option('hpixl_securicheck_toggle_block_ip_connexion_admin', '0');
}


function hpixl_securicheck_render_protection_login_onglet_ip_bloquees()
{
    require plugin_dir_path(dirname(__FILE__)) . 'admin/Screen_protection_login_onglet_ip_bloquees.php';
}

function hpixl_securicheck_render_protection_login_onglet_historique()
{
    require plugin_dir_path(dirname(__FILE__)) . 'admin/Screen_protection_login_onglet_historique.php';
}
