<?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 HappyFenceNGAlog extends HappyFenceNGRuntimeExceptions {

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @var mixed $provied .
	 */
	private static $proxied = null;

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string                     $what ... do you think it is?.
	 * @param HappyFenceNGSyslogFacility $facility .. guess it.
	 * @param HappyFenceNGSyslogSeverity $severity ..uh oh? a severity level?.
	 */
	abstract public function log( $what, HappyFenceNGSyslogFacility $facility = HappyFenceNGSyslogFacility::USER, HappyFenceNGSyslogSeverity $severity = HappyFenceNGSyslogSeverity::NOTICE );

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string                     $caller ... do you think it is?.
	 * @param HappyFenceNGSyslogFacility $fac .. guess it.
	 * @param HappyFenceNGSyslogSeverity $sev ..uh oh? a severity level?.
	 */
	protected function pre_filter( $caller, HappyFenceNGSyslogFacility $fac, HappyFenceNGSyslogSeverity $sev ): bool {
		try {
			switch ( $caller ) {
				case 'HappyFenceNGLOGGER':
					// qualsiasi versione può stampare SECURITY e WARNING SEMPRE.
					if ( HappyFenceNGTR::is_in( $caller, array( HappyFenceNGTR::FUNNEL ) ) && ( HappyFenceNGPROC::equals( $fac, HappyFenceNGSyslogFacility::SECURITY ) && HappyFenceNGPROC::equals( $sev, HappyFenceNGSyslogSeverity::WARNING ) ) ) {
						return true;
					} else {
						return (
							(
								function_exists( 'htes_fs' ) && HappyFenceNGTR::is_in( $caller, array( HappyFenceNGTR::PREAMBLE, HappyFenceNGTR::AGGREGATE ), true )
								&& in_array( $sev, HappyFenceNGCleaner::check_array( HappyFenceNGWhoAmI::get_instance()->LOGS->allowed_severity, HappyFenceNGPlaceholder::EMPTY_ARRAY ), true )
								)
							|| (
								function_exists( 'htes_fs' ) && HappyFenceNGTR::is_in( $caller, array( HappyFenceNGTR::BUNKER ), true )
								&& in_array( $sev, HappyFenceNGCleaner::check_array( HappyFenceNGWhoAmI::get_instance()->LOGS->allowed_severity, HappyFenceNGPlaceholder::EMPTY_ARRAY ), true )
								&& in_array( $fac, HappyFenceNGCleaner::check_array( HappyFenceNGWhoAmI::get_instance()->LOGS->allowed_facility, HappyFenceNGPlaceholder::EMPTY_ARRAY ), true )
								)
						);
					}

				case 'HappyFenceNGDBLOGS':
					return (
					(
						function_exists( 'htes_fs' ) && HappyFenceNGTR::is_in( $caller, array( HappyFenceNGTR::AGGREGATE ), true )
						&& in_array( $sev, HappyFenceNGCleaner::check_array( HappyFenceNGWhoAmI::get_instance()->LOGS->allowed_severity, HappyFenceNGPlaceholder::EMPTY_ARRAY ), true )
					)
					||
					(
						function_exists( 'htes_fs' ) && HappyFenceNGTR::is_in( $caller, array( HappyFenceNGTR::BUNKER ), true )
						&& in_array( $sev, HappyFenceNGCleaner::check_array( HappyFenceNGWhoAmI::get_instance()->LOGS->allowed_severity, HappyFenceNGPlaceholder::EMPTY_ARRAY ), true )
						&& in_array( $fac, HappyFenceNGCleaner::check_array( HappyFenceNGWhoAmI::get_instance()->LOGS->allowed_facility, HappyFenceNGPlaceholder::EMPTY_ARRAY ), true )
					)
				);
				case 'HappyFenceNGSIEM':
					return (
					(
						function_exists( 'htes_fs' ) && HappyFenceNGTR::is_in( $caller, array( HappyFenceNGTR::BUNKER ), true )
						&& in_array( $sev, HappyFenceNGCleaner::check_array( HappyFenceNGWhoAmI::get_instance()->LOGS->allowed_severity, HappyFenceNGPlaceholder::EMPTY_ARRAY ), true )
						&& in_array( $fac, HappyFenceNGCleaner::check_array( HappyFenceNGWhoAmI::get_instance()->LOGS->allowed_facility, HappyFenceNGPlaceholder::EMPTY_ARRAY ), true )
					)
				);
				default:
					HappyFenceNGLOGCORE::logcore(
						static::class . ' (PRE FILTER) called by unknown caller ' . $caller,
						HappyFenceNGSyslogFacility::CORE,
						HappyFenceNGSyslogSeverity::ERROR
					);
					return false;
			}
		} catch ( Throwable $e ) {
			HappyFenceNGLOGCORE::logcore(
				static::class . ' (PRE FILTER)' . $e->getMessage(),
				HappyFenceNGSyslogFacility::CORE,
				HappyFenceNGSyslogSeverity::ERROR
			);

			return false;
		}
	}


	/**PHPCS -> code compliance: we put useful comments where needed.. */
	private static function selectLogger() {
		try {
			switch ( HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->LOGS->log_type, 'file' ) ) {
				case 'file':
						self::$proxied = ( self::$proxied instanceof HappyFenceNGLOGGER ) ? self::$proxied : new HappyFenceNGLOGGER();
					break;
				case 'db':
					if ( function_exists( 'htes_fs' ) && ( HappyFenceNGTR::is_in( static::class, array( HappyFenceNGTR::AGGREGATE, HappyFenceNGTR::BUNKER ) ) ) ) {
							self::$proxied = ( self::$proxied instanceof HappyFenceNGDBLOGS ) ? self::$proxied : new HappyFenceNGDBLOGS();
					} else {
							self::$proxied = ( self::$proxied instanceof HappyFenceNGLOGGER ) ? self::$proxied : new HappyFenceNGLOGGER();
					}
					break;
				case 'siem':
					if ( function_exists( 'htes_fs' ) && ( HappyFenceNGTR::is_in( static::class, array( HappyFenceNGTR::BUNKER ) ) ) ) {
							self::$proxied = ( self::$proxied instanceof HappyFenceNGSIEM ) ? self::$proxied : new HappyFenceNGSIEM();
					} else {
							self::$proxied = ( self::$proxied instanceof HappyFenceNGLOGGER ) ? self::$proxied : new HappyFenceNGLOGGER();
					}
					break;

				default:
						self::$proxied = ( self::$proxied instanceof HappyFenceNGLOGGER ) ? self::$proxied : new HappyFenceNGLOGGER();
					break;
			}

			self::$proxied->log(
				'HappyFenceNGAlog - BOOT DONE SELECTED LOGGER IS ' . get_class( self::$proxied ),
				HappyFenceNGSyslogFacility::CORE,
				HappyFenceNGSyslogSeverity::INFO
			);
			return self::$proxied;
		} catch ( Throwable $e ) {

			HappyFenceNGLOGCORE::logcore(
				static::class . ' (FATAL EXCEPTION ON SELECTLOGGER) ' . $e->getMessage(),
				HappyFenceNGSyslogFacility::CORE,
				HappyFenceNGSyslogSeverity::ERROR
			);
		}
	}

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public static function get_instance(): static {
		return self::$proxied ??= self::selectLogger();
	}



	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $value ... uhm .. don't know that it is.
	 */
	public static function sanitize_siem_endpoint( $value ) {

		$pattern = '/^(?:(https?|tcp|udp):\/\/)?' .
				'(?:' .
					'\[([a-fA-F0-9:]+)\]' .
					'|' .
					'([a-zA-Z0-9\-\.]+|\d{1,3}(?:\.\d{1,3}){3})' .
				')' .
				':(\d+)$/';

		if ( preg_match( $pattern, $value ) ) {
			return sanitize_text_field( $value );
		} else {

			add_settings_error(
				'happyfenceng_siem_endpoint',
				'invalid-endpoint',
				esc_html__( 'Invalid SIEM endpoint. Use host:port, [IPv6]:port or protocol://host:port', 'happyfenceng-total-enterprise-security' ),
				'error'
			);
		}
		return HappyFenceNGPlaceholder::EMPTY_STRING;
	}


	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $what ... do you think it is?.
	 * @param string $t ... exception message?.
	 */
	private function log_fallback( $what, $t = null ) {
			$exc = ( isset( $t ) && HappyFenceNGPROC::not_equals( $t, null ) ) ? $t->getMessage() : 'none';
			HappyFenceNGLOGCORE::logcore(
				static::class . "original channel not ready: exception $exc, original message was $what , fallback to logcore!!",
				HappyFenceNGSyslogFacility::SYSLOG,
				HappyFenceNGSyslogSeverity::ERROR
			);
	}
}
