<?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.. */
abstract class HappyFenceNGAEndpoint extends HappyFenceNGSystem {
	/**PHPCS -> code compliance: we put useful comments where needed.. */
	abstract public function init();

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	abstract public function doCallback();

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $burn ... this will be burned by evil.
	 * @param string $message .. say something.
	 * @param bool   $warning .. oh that's interesting.
	 * @param array  $options ..usually an array of options.
	 */
	public function answerOk( $burn, string $message = '', bool $warning = false, array $options = array() ) {
		wp_send_json(
			array(
				'result'  => 'OK',
				'message' => $message,
				'burn'    => $burn,
				'warning' => $warning,
				'options' => $options,
			)
		);
		exit;
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $message .. say something.
	 * @param array  $options ..usually an array of options.
	 */
	public function answerKo( string $message = 'NOT AUTHORIZED', array $options = array() ) {
		wp_send_json(
			array(
				'result'  => 'KO',
				'message' => $message,
				'options' => $options,
			)
		);
		exit;
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $referer_to_check , the referer to check.
	 */
	public function checkBurn( $referer_to_check ): string {

		if ( check_ajax_referer( $referer_to_check, 'security' ) && isset( $_POST['burn'] ) && ( ! empty( $_POST['burn'] ) ) ) {

			$to_check = sanitize_text_field( wp_unslash( $_POST['burn'] ?? '' ) );
			return HappyFenceNGFailFast::verify( $to_check );
		}
		HappyFenceNGAlog::get_instance()->log(
			static::class . ' ->Unauthorized call to endpoint, invalid ajax_referer=>' . $referer_to_check . ' or burn token =>' . $burn . ' (BLOCKED) ',
			HappyFenceNGSyslogFacility::SECURITY,
			HappyFenceNGSyslogSeverity::WARNING
		);
		return HappyFenceNGPlaceholder::KO;
	}

	/**PHPCS -> audit, logs when a ON/OFF property is changed
	 *
	 * @param string $property_name the name.
	 * @param string $property_value ... the value.
	 */
	public function logChange( $property_name, $property_value ) {
		HappyFenceNGAlog::get_instance()->log(
			$property_name . ' is ' . ( $property_value ? 'enabled' : 'disabled' ),
			HappyFenceNGSyslogFacility::SECURITY,
			HappyFenceNGSyslogSeverity::AUDIT
		);
	}

	/**PHPCS -> audit, logs when a ON/OFF property is changed
	 *
	 * @param string $property_name the name.
	 * @param string $property_value ... the value.
	 */
	public function logValueChange( $property_name, $property_value ) {
		HappyFenceNGAlog::get_instance()->log(
			$property_name . ' is ' . ( $property_value ),
			HappyFenceNGSyslogFacility::SECURITY,
			HappyFenceNGSyslogSeverity::AUDIT
		);
	}

	/**PHPCS -> audit, logs a specific audit message from the enedpoint
	 *
	 * @param string $message ... guess what .
	 */
	public function audit( $message ) {
		HappyFenceNGAlog::get_instance()->log(
			$message,
			HappyFenceNGSyslogFacility::SECURITY,
			HappyFenceNGSyslogSeverity::AUDIT
		);
	}
}
