<?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 HappyFenceNGEDPCFC extends HappyFenceNGAEndpoint {
	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function init() {
		add_action( 'wp_ajax_happyfenceng_CFC_save_settings', array( $this, 'doCallback' ) );
	}
	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function doCallback() {
		try {
			$burn = parent::checkBurn( 'happyfenceng_CFC_save_settings' );
			if ( HappyFenceNGPROC::not_equals( $burn, HappyFenceNGPlaceholder::KO ) ) {
				if ( check_ajax_referer( 'happyfenceng_CFC_save_settings', 'security' ) ) {

					if ( HappyFenceNGPROC::equals( HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->CF->allowed_ips, HappyFenceNGPlaceholder::EMPTY_STRING ), HappyFenceNGPlaceholder::EMPTY_STRING ) ) {
						HappyFenceNGWhoAmI::get_instance()->CF->allowed_ips = implode( ',', array_merge( HappyFenceNGWhoAmI::get_instance()->CF->_DEFAULT_IPV4, HappyFenceNGWhoAmI::get_instance()->CF->_DEFAULT_IPV6 ) );
					}

					if ( isset( $_POST['enable_cf_guard'] ) ) {
						HappyFenceNGWhoAmI::get_instance()->CF->enable_cf_guard = HappyFenceNGCleaner::must_be_bool( sanitize_text_field( wp_unslash( $_POST['enable_cf_guard'] ?? HappyFenceNGPlaceholder::CHECKBOX_OFF ) ) );
						parent::logChange( 'Cloudflare/RProxy Guard:', HappyFenceNGWhoAmI::get_instance()->CF->enable_cf_guard );
					} else {
						parent::audit( 'WARNING not received enable_cf_guard option as expected' );
					}
					if ( isset( $_POST['enable_cf_guard_admin_only'] ) ) {
						HappyFenceNGWhoAmI::get_instance()->CF->enable_cf_guard_admin_only = HappyFenceNGCleaner::must_be_bool( sanitize_text_field( wp_unslash( $_POST['enable_cf_guard_admin_only'] ?? HappyFenceNGPlaceholder::CHECKBOX_OFF ) ) );
						parent::logChange( 'Cloudflare/RProxy Guard ADMIN ONLY:', HappyFenceNGWhoAmI::get_instance()->CF->enable_cf_guard_admin_only );
					}

					if ( isset( $_POST['allowed_ips'] ) ) {
						$the_ip = HappyFenceNGCleaner::must_be_ip( sanitize_text_field( wp_unslash( $_POST['allowed_ips'] ?? HappyFenceNGPlaceholder::EMPTY_STRING ) ) );
						if ( HappyFenceNGPROC::equals( $the_ip, HappyFenceNGPlaceholder::NOT_A_VALID_IP ) ) {
							$the_ip = HappyFenceNGPlaceholder::UNKNOWN;
						}
						HappyFenceNGWhoAmI::get_instance()->CF->allowed_ips = $the_ip;
					}

					parent::audit( 'CloudFlare/RProxy Shields options changed successfully' );
					parent::answerOk( $burn, esc_html__( 'Saved successfully', 'happyfenceng-total-enterprise-security' ) );
				}
			}
			HappyFenceNGAlog::get_instance()->log(
				static::class . ' ->Unauthorized call to endpoint, invalid nonce or burn token. (BLOCKED) ',
				HappyFenceNGSyslogFacility::SECURITY,
				HappyFenceNGSyslogSeverity::WARNING
			);
			parent::answerKo( esc_html__( 'Not Authorized', 'happyfenceng-total-enterprise-security' ) );
		} catch ( Throwable $t ) {
			HappyFenceNGLOGCORE::logcore(
				static::class . $t->getMessage(),
				HappyFenceNGSyslogFacility::DAEMON,
				HappyFenceNGSyslogSeverity::ERROR
			);
			parent::answerKo( esc_html__( 'Denied', 'happyfenceng-total-enterprise-security' ) );
		}
	}
}
