<?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 HappyFenceNGVRCF extends HappyFenceNGAVRoute {

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function doRoute() {
		try {
			$nonce_save_settings = wp_create_nonce( 'happyfenceng_CFC_save_settings' );
			$burn                = HappyFenceNGFailFast::setup();
			$params              = array(
				'texts'  => array(
					'title'                 => esc_html__( 'Cloudflare Reverse-Proxy Guard', 'happyfenceng-total-enterprise-security' ),
					'warning_cfc'           => esc_html__( 'Cloudflare Guard', 'happyfenceng-total-enterprise-security' ),
					'warning_cfc_line_1'    => esc_html__( 'This feature protects your site by allowing access only from specific IP ranges (such as Cloudflare or your reverse proxy). ', 'happyfenceng-total-enterprise-security' ),
					'warning_cfc_line_2'    => esc_html__( 'All other requests will be automatically blocked', 'happyfenceng-total-enterprise-security' ),
					'geoip_cfc'             => esc_html__( 'Cloudflare and Geoip', 'happyfenceng-total-enterprise-security' ),
					'geoip_cfc_line_1'      => esc_html__( 'If you use Cloudflare there is no need to connect to an external geoip provider.', 'happyfenceng-total-enterprise-security' ),
					'geoip_cfc_line_2'      => esc_html__( 'Just go to the GeoIp Blocking page and select Cloudflare as provider and the plugin will do the rest for you.', '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_cf_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 add the internal IP too!', 'happyfenceng-total-enterprise-security' ),

					'enable_guard'          => esc_html__( 'Enable Guard', 'happyfenceng-total-enterprise-security' ),
					'check_admin_only'      => esc_html__( '⚠️ Check Admin Only', 'happyfenceng-total-enterprise-security' ),
					'check_admin_only_desc' => esc_html__( 'If you check this option, no filter will be used for pages other than admin.php and wp-login.php. Use it only if necessary.', 'happyfenceng-total-enterprise-security' ),
					'allowed_ips_label'     => esc_html__( 'Allowed IP Ranges (CIDR, comma separated)', 'happyfenceng-total-enterprise-security' ),
					'allowed_ips_desc'      => esc_html__( 'Default list = Cloudflare IPv4/IPv6. Add your own reverse-proxy nets if needed.', 'happyfenceng-total-enterprise-security' ),
					'feature_requires'      => esc_html__( 'THIS FEATURE IS FREE', 'happyfenceng-total-enterprise-security' ),
					'th_enable_guard'       => esc_html__( 'Enable Guard', '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_cf_guard'  => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->CF->enable_cf_guard, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT ),
					'allowed_ips'      => esc_textarea( HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->CF->allowed_ips, HappyFenceNGPlaceholder::EMPTY_STRING ) ),
					'check_admin_only' => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->CF->enable_cf_guard_admin_only, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT ),
					'checkbox_on'      => HappyFenceNGPlaceholder::CHECKBOX_ON,
					'checkbox_off'     => HappyFenceNGPlaceholder::CHECKBOX_OFF,
					'nonce'            => $nonce_save_settings,
					'burnNonce'        => $burn,
					'feature_enabled'  => true,
				),
			);

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

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