<?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
 * This is part of Zero-Trust Vector (ZTV) Engine
 * @set all
 */

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

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

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public static function setup() {
		$data = array(
			'ip'        => HappyFenceNGUtils::get_user_ip(),
			'userAgent' => sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ?? '' ) ),
			'sessionId' => session_id(),
			'userId'    => get_current_user_id(),
			'timestamp' => time(),
		);

		//phpcs:ignore we know what we do!
		$burn_nonce = md5( maybe_serialize( $data ) );
		//phpcs:ignore we know what we do!
		HappyFenceNGBStore::set( static::class, $burn_nonce, maybe_serialize( $data ) );
		return $burn_nonce;
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $burn_nonce ... there was a time in which nonce were burning forever.
	 */
	public static function verify( $burn_nonce ): string {
		try {

			if ( HappyFenceNGPROC::not_equals( $burn_nonce, null ) ) {
				$ip = HappyFenceNGUtils::get_user_ip();
				if ( HappyFenceNGPROC::equals( $ip, HappyFenceNGPlaceholder::WP_CRON ) ) {

					return HappyFenceNGPlaceholder::OK;
				}

				//phpcs:ignore we know what we do!
				$data = maybe_unserialize( HappyFenceNGBStore::get( static::class, $burn_nonce ) );
				if ( $data && is_array( $data ) ) {
					if (
						( HappyFenceNGPROC::equals( $data['sessionId'], session_id() ) ) &&
						( HappyFenceNGPROC::equals( $data['userId'], get_current_user_id() ) ) &&
						( HappyFenceNGPROC::equals( $data['ip'], $ip ) ) &&
						( HappyFenceNGPROC::equals( $data['userAgent'], ( sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ?? '' ) ) ) ) ) ) {
							return HappyFenceNGBStore::burn( self::setup(), static::class, $burn_nonce );
					}
				}

				HappyFenceNGAlog::get_instance()->log(
					'Invalid burn token ' . $burn_nonce . ' access will be denied <<NO MATCH>>. Stored data: ' . wp_json_encode( $data ) . ' current data: session_id=>' . session_id()
							. ' user id=>' . get_current_user_id() . ' ip=>' . $ip
							. ' user agent=>' . ( sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ?? '' ) ) ),
					HappyFenceNGSyslogFacility::SECURITY,
					HappyFenceNGSyslogSeverity::WARNING
				);
			}

			return HappyFenceNGPlaceholder::KO;
		} catch ( Throwable $t ) {
			HappyFenceNGLOGCORE::logcore(
				'Burn Error: ' . $t->getMessage(),
				HappyFenceNGSyslogFacility::FAILFAST,
				HappyFenceNGSyslogSeverity::ERROR
			);

			return HappyFenceNGPlaceholder::KO;
		}
	}
}
