<?php
/**
 * HappyFenceNG Total Enterprise Security Plugin
 * HAPPYWPRESS is a RazorVue Trademark.
 *
 * @package     HappyFenceNG
 * @developer   Simonluca Scillitani
 * @copyright   2025 RazorVue di Scillitani Simonluca
 * @link        https://razorvue.it
 * @author      RazorVue di Scillitani Simonluca
 * @brand       HappyWPress
 * @uri         https://happywpress.com
 * @set all
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**PHPCS -> code compliance: we put useful comments where needed.. */
class HappyFenceNGVRIPBL extends HappyFenceNGAVRoute {

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function doRoute() {
		try {

			$nonce_save_settings = wp_create_nonce( 'happyfenceng_IPBL_save_settings' );
			$nonce_check_my_ip   = wp_create_nonce( 'happyfenceng_IPBL_check_my_ip' );
			$burn                = HappyFenceNGFailFast::setup();

			$params = array(
				'texts'  => array(
					'title'                                => esc_html__( 'IP Blacklist', 'happyfenceng-total-enterprise-security' ),
					'warning_ipbl'                         => esc_html__( 'IP Blacklist', 'happyfenceng-total-enterprise-security' ),
					'warning_ipbl_line_1'                  => esc_html__( 'This feature lets you specify an IP blacklist (single ip or cidr) ', 'happyfenceng-total-enterprise-security' ),
					'warning_ipbl_line_2'                  => esc_html__( 'All requests matching will be automatically blocked', 'happyfenceng-total-enterprise-security' ),
					'important'                            => esc_html__( 'Important:', 'happyfenceng-total-enterprise-security' ),
					'important_line_0'                     => esc_html__( 'Incorrect values may lock you out of the site, including the admin area.', 'happyfenceng-total-enterprise-security' ),
					'important_line_1'                     => esc_html__( 'If that happens, you can always disable this protection via command line:', 'happyfenceng-total-enterprise-security' ),
					'important_line_2'                     => esc_html__( 'wp happyfenceng disable_ipbl_guard --cli-pass=\'your_secure_password\'', 'happyfenceng-total-enterprise-security' ),
					'note'                                 => esc_html__( 'NOTE:', 'happyfenceng-total-enterprise-security' ),
					'note_line_1'                          => esc_html__( 'If you are behind a local reverse proxy you may need to check twice the internal IP too!', 'happyfenceng-total-enterprise-security' ),

					'warning_ipbl_external_url'            => esc_html__( 'VPN and TOR features use external GitHub endpoint to get the updated list', 'happyfenceng-total-enterprise-security' ),
					'warning_ipbl_external_url_line_1'     => esc_html__( 'Those lists are not under happywpress.com or razorvue.it control', 'happyfenceng-total-enterprise-security' ),
					'warning_ipbl_external_url_line_2'     => esc_html__( 'If you experience problems with those filters you can disable it', 'happyfenceng-total-enterprise-security' ),

					'enable_ipbl_vpn_tor_guard_title'      => esc_html__( 'Enable IPBL Tor Guard', 'happyfenceng-total-enterprise-security' ),
					'enable_ipbl_vpn_tor_guard_description' => esc_html__( 'Protects against DarkWeb IPs', 'happyfenceng-total-enterprise-security' ),
					'enable_ipbl_vpn_tor_guard_tooltip'    => esc_html__( 'When enabled filters all ip that belong to a TOR exit point', 'happyfenceng-total-enterprise-security' ),

					'enable_ipbl_vpn_proton_guard_title'   => esc_html__( 'Enable Proton VPN Guard', 'happyfenceng-total-enterprise-security' ),
					'enable_ipbl_vpn_proton_guard_description' => esc_html__( 'Protects against Proton VPN IPs', 'happyfenceng-total-enterprise-security' ),
					'enable_ipbl_vpn_proton_guard_tooltip' => esc_html__( 'When enabled filters all ip that belong to a Proton VPN exit point', 'happyfenceng-total-enterprise-security' ),

					'enable_ipbl_guard_title'              => esc_html__( 'Enable IPBL Guard', 'happyfenceng-total-enterprise-security' ),
					'enable_ipbl_guard_description'        => esc_html__( 'Block the ip specified below', 'happyfenceng-total-enterprise-security' ),
					'enable_ipbl_guard_tooltip'            => esc_html__( 'By enabling this option all IPs that match the list proviced below will be blocked automatically before the page can be processed', 'happyfenceng-total-enterprise-security' ),
					'blacklist_label'                      => esc_html__( 'Blacklist IP Ranges (CIDR, comma separated)', 'happyfenceng-total-enterprise-security' ),
					'blacklist_desc'                       => esc_html__( 'Add IP or nets needed, if your wordpress is inside the internal LAN check twice the ip list.', 'happyfenceng-total-enterprise-security' ),
					'feature_requires'                     => esc_html__( 'THIS FEATURE REQUIRES AGENCY OR ENTERPRISE', 'happyfenceng-total-enterprise-security' ),
					'th_enable_guard'                      => esc_html__( 'Enable Guard', 'happyfenceng-total-enterprise-security' ),
					'check_my_ip'                          => esc_html__( 'Check My IP', 'happyfenceng-total-enterprise-security' ),
					'save_settings'                        => esc_html__( 'Save Settings', 'happyfenceng-total-enterprise-security' ),
					'settings_saved'                       => esc_html__( 'Settings Saved', 'happyfenceng-total-enterprise-security' ),
					'error_saving_settings'                => esc_html__( 'Error saving settings', 'happyfenceng-total-enterprise-security' ),
				),
				'links'  => array(
					'happyfenceng_logo_url' => esc_url( HAPPYFENCENG_PLUGIN_URL . 'assets/images/happyfenceng-small.png' ),

				),
				'values' => array(
					'enable_ipbl_guard'            => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->IPBL->enable_ipbl_guard, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT ),
					'enable_ipbl_vpn_proton_guard' => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->IPBL->enable_ipbl_vpn_proton_guard, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT ),
					'enable_ipbl_vpn_tor_guard'    => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->IPBL->enable_ipbl_vpn_tor_guard, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT ),
					'blacklist_ips'                => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->IPBL->blacklist_ips, HappyFenceNGPlaceholder::EMPTY_STRING ),

					'checkbox_on'                  => HappyFenceNGPlaceholder::CHECKBOX_ON,
					'checkbox_off'                 => HappyFenceNGPlaceholder::CHECKBOX_OFF,
					'nonce'                        => $nonce_save_settings,
					'burnNonce'                    => $burn,
					'nonceCheckMyIp'               => $nonce_check_my_ip,
					'feature_enabled'              => ( function_exists( 'htes_fs' ) && HappyFenceNGTR::is_in( static::class, array( HappyFenceNGTR::AGGREGATE ) ) ),
				),
			);

			ob_start();
			HappyFenceNGVControl::renderView( $params, 'ipbl' );
			// phpcs:ignore WordPress.Security.EscapeOutput
			echo ob_get_clean();

		} catch ( Throwable $e ) {
			HappyFenceNGLOGCORE::logcore(
				'HappyFenceNGVRIpBL - cannot render UI ' . $e->getMessage(),
				HappyFenceNGSyslogFacility::UI,
				HappyFenceNGSyslogSeverity::ERROR
			);
			ob_end_clean();
			HappyFenceNGCleaner::cleaner_oops();
		}
	}
}
