<?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;
}

if ( ! function_exists( 'wp_filesystem' ) ) {
	require_once ABSPATH . 'wp-admin/includes/file.php';
}
define( 'FS_METHOD', 'direct' );
WP_Filesystem();

/**PHPCS -> code compliance: we put useful comments where needed.. */
final class HappyFenceNGCleaner extends HappyFenceNGRuntimeExceptions {

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $input ..
	 */
	public static function must_be_color( string $input ): string {
		$input = wp_unslash( $input ?? '' );

		if ( ! preg_match( '/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/', $input ) ) {
			HappyFenceNGLOGCORE::logcore(
				__METHOD__ . 'must_be_color:User input not permitted:' . wp_json_encode( $input, true ),
				HappyFenceNGSyslogFacility::USER,
				HappyFenceNGSyslogSeverity::WARNING
			);
			self::cleaner_die( 'The proposed value is not accepted.', 'happyfenceng-total-enterprise-security' );
		}

		return $input;
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $dirlist ..
	 */
	public static function validate_dirlist( string $dirlist ) {
		try {
			// Verifica che la dirlist non sia vuota, nulla o un valore placeholder.
			if ( empty( $dirlist ) || ! isset( $dirlist ) || HappyFenceNGPROC::equals( $dirlist, HappyFenceNGPlaceholder::EMPTY_STRING ) ) {
				return HappyFenceNGPlaceholder::EMPTY_ARRAY;
			}

			// Dividi la lista di directory separata da virgole in un array.
			$dir_array  = explode( ',', $dirlist );
			$valid_dirs = array();

			foreach ( $dir_array as $a_dir ) {
				// Rimuovi gli spazi vuoti all'inizio e alla fine del percorso.
				$a_dir = trim( $a_dir );

				// Verifica che la directory non contenga sequenze pericolose come ../ .
				if ( preg_match( '/\.\./', $a_dir ) ) {
					// Logga il tentativo di path traversal.
					HappyFenceNGAlog::get_instance()->log(
						__METHOD__ . 'validate_dirlist: Path traversal detected in: ' . wp_json_encode( $a_dir, true ),
						HappyFenceNGSyslogFacility::SECURITY,
						HappyFenceNGSyslogSeverity::WARNING
					);
					continue;
				}

				// Verifica se il percorso è una directory valida.
				if ( is_dir( $a_dir ) ) {
					$valid_dirs[] = $a_dir;
				} else {
					// Logga se la directory non è valida.
					HappyFenceNGAlog::get_instance()->log(
						__METHOD__ . 'validate_dirlist: Invalid directory: ' . wp_json_encode( $a_dir, true ),
						HappyFenceNGSyslogFacility::SECURITY,
						HappyFenceNGSyslogSeverity::WARNING
					);
				}
			}

			// Ritorna le directory valide, o un array vuoto se nessuna è valida .
			return ! empty( $valid_dirs ) ? $valid_dirs : HappyFenceNGPlaceholder::EMPTY_ARRAY;

		} catch ( \Throwable $t ) {
			// Gestione errori generali .
			HappyFenceNGLOGCORE::logcore(
				__METHOD__ . 'validate_dirlist: Error processing user input: ' . wp_json_encode( $dirlist, true ) . ' Exception: ' . $t->getMessage(),
				HappyFenceNGSyslogFacility::USER,
				HappyFenceNGSyslogSeverity::ERROR
			);
			return HappyFenceNGPlaceholder::EMPTY_ARRAY;
		}
	}


	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $input ... you need one.
	 */
	public static function must_be_string( string $input ): string {
		$input = wp_unslash( $input ?? '' );

		if ( ! preg_match( '/^[\p{L}\p{N}\s.,_\-:\/]*$/u', $input ) ) {
			HappyFenceNGLOGCORE::logcore(
				__METHOD__ . 'must_be_string:User input not permitted:' . wp_json_encode( $input, true ),
				HappyFenceNGSyslogFacility::USER,
				HappyFenceNGSyslogSeverity::WARNING
			);

			return HappyFenceNGPlaceholder::EMPTY_STRING;
		}

		if ( preg_match( '/(?:javascript|data|vbscript|php|file|ftp|sftp|blob|about|chrome|http|https)\s*:/i', $input ) ) {
			HappyFenceNGLOGCORE::logcore(
				__METHOD__ . 'must_be_string:User input not permitted:' . wp_json_encode( $input, true ),
				HappyFenceNGSyslogFacility::USER,
				HappyFenceNGSyslogSeverity::WARNING
			);

			return HappyFenceNGPlaceholder::EMPTY_STRING;
		}

		if ( preg_match( '/<(script|iframe|object|embed|img|svg|style|meta)[^>]*>|on\w+\s*=/i', $input ) ) {
			HappyFenceNGLOGCORE::logcore(
				__METHOD__ . 'must_be_string:User input not permitted:' . wp_json_encode( $input, true ),
				HappyFenceNGSyslogFacility::USER,
				HappyFenceNGSyslogSeverity::WARNING
			);

			return HappyFenceNGPlaceholder::EMPTY_STRING;
		}

		if ( preg_match( '/(<\?php|\?>|include|require|exec|system|passthru|shell_exec|eval|file_get_contents|fopen|base64_decode)/i', $input ) ) {
			HappyFenceNGLOGCORE::logcore(
				__METHOD__ . 'must_be_string:User input not permitted:' . wp_json_encode( $input, true ),
				HappyFenceNGSyslogFacility::USER,
				HappyFenceNGSyslogSeverity::WARNING
			);

			return HappyFenceNGPlaceholder::EMPTY_STRING;
		}

		if ( preg_match( '/(\\\x|%[0-9a-fA-F]{2})/', $input ) ) {
			HappyFenceNGLOGCORE::logcore(
				__METHOD__ . 'must_be_string:User input not permitted:' . wp_json_encode( $input, true ),
				HappyFenceNGSyslogFacility::USER,
				HappyFenceNGSyslogSeverity::WARNING
			);

			return HappyFenceNGPlaceholder::EMPTY_STRING;
		}

		return $input;
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $ip_list ... you need one.
	 */
	public static function must_be_ip( string $ip_list ) {
		$ip_list = $ip_list ?? '';
		$items   = preg_split( '/[\s,]+/', $ip_list, -1, PREG_SPLIT_NO_EMPTY );
		$valid   = array();

		foreach ( $items as $token ) {
			$token = ( $token ?? '' );
			if ( HappyFenceNGPROC::equals( $token, '' ) ) {
				continue;
			}

			if ( ! self::isValidIpOrCidr( $token ) ) {
				HappyFenceNGLOGCORE::logcore(
					__METHOD__ . 'must_be_ip: User input not permitted (invalid token): ' . $token,
					HappyFenceNGSyslogFacility::USER,
					HappyFenceNGSyslogSeverity::WARNING
				);

				return HappyFenceNGPlaceholder::NOT_A_VALID_IP_OR_CIDR;
			}

			$valid[] = $token;
		}

		return implode( ',', $valid );
	}


	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $ip ... you need one.
	 * @param string $cidr ... network man said you need also this.
	 * @return true if true, false if false.
	 */
	public static function ip_in_cidr( string $ip, string $cidr ): bool {
		try {
			if ( HappyFenceNGPROC::equals( strpos( $cidr, '/' ), false ) ) {
				$cidr .= HappyFenceNGPROC::equals( strpos( $cidr, ':' ), false ) ? '/32' : '/128';
			}

			list($subnet, $mask) = explode( '/', $cidr, 2 );
			$mask                = (int) $mask;

			if ( HappyFenceNGPROC::not_equals( strpos( $ip, ':' ), false ) ) {
				return $this->ipv6_in_cidr( $ip, $subnet, $mask );
			}

			$ip_long     = ip2long( $ip );
			$subnet_long = ip2long( $subnet );
			$mask_long   = ~( ( 1 << ( 32 - $mask ) ) - 1 );

			return HappyFenceNGPROC::equals( ( $ip_long & $mask_long ), ( $subnet_long & $mask_long ) );
		} catch ( \Throwable $t ) {
			HappyFenceNGLOGCORE::logcore(
				__METHOD__ . "error while parsing ip/cidr ip: $ip cird $cidr raised:" . $t->getMessage(),
				HappyFenceNGSyslogFacility::USER,
				HappyFenceNGSyslogSeverity::WARNING
			);
			return HappyFenceNGPlaceholder::NOT_A_VALID_IP_OR_CIDR;
		}
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $ip ... you need one.
	 * @param string $subnet ... yep the entire internet is not an option.
	 * @param string $mask ... no not the one for halloween.
	 * @return true if true, false if false.
	 */
	public static function ipv6_in_cidr( $ip, $subnet, $mask ) {
		try {
			$ip_bin     = inet_pton( $ip );
			$subnet_bin = inet_pton( $subnet );
			if ( HappyFenceNGPROC::equals( $ip_bin, false ) || HappyFenceNGPROC::equals( $subnet_bin, false ) ) {
				return false;
			}

			$bytes = (int) ( $mask / 8 );
			$bits  = $mask % 8;

			if ( $bytes ) {
				if ( HappyFenceNGPROC::not_equals( strncmp( $ip_bin, $subnet_bin, $bytes ), 0 ) ) {
					return false;
				}
			}
			if ( $bits ) {
				$ip_byte     = ord( $ip_bin[ $bytes ] );
				$subnet_byte = ord( $subnet_bin[ $bytes ] );
				$mask_byte   = ( ~( ( 1 << ( 8 - $bits ) ) - 1 ) ) & 0xFF;
				return HappyFenceNGPROC::equals( ( $ip_byte & $mask_byte ), ( $subnet_byte & $mask_byte ) );
			}
			return true;
		} catch ( \Throwable $t ) {
			HappyFenceNGLOGCORE::logcore(
				__METHOD__ . "error while parsing ipv6/cidr ip: $ip subnet $subnet mask $mask raised:" . $t->getMessage(),
				HappyFenceNGSyslogFacility::USER,
				HappyFenceNGSyslogSeverity::WARNING
			);
			return HappyFenceNGPlaceholder::NOT_A_VALID_IP_OR_CIDR;
		}
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $s ... you need one.
	 * @return true if true, false if false.
	 */
	private static function isValidIpOrCidr( string $s ): bool {
		try {
			if ( HappyFenceNGPROC::not_equals( strpos( $s, '/' ), false ) ) {
				[$addr, $prefix] = explode( '/', $s, 2 );
				$addr            = ( $addr ?? '' );
				$prefix          = ( $prefix ?? '' );

				if ( ! filter_var( $addr, FILTER_VALIDATE_IP ) ) {
					return false;
				}
				if ( HappyFenceNGPROC::equals( $prefix, '' ) || ! preg_match( '/^\d{1,3}$/', $prefix ) ) {
					return false;
				}

				$p = (int) $prefix;

				if ( filter_var( $addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) {
					return $p >= 0 && $p <= 32;
				} else {
					return $p >= 0 && $p <= 128;
				}
			}

			return (bool) filter_var( $s, FILTER_VALIDATE_IP );
		} catch ( \Throwable $t ) {
			HappyFenceNGLOGCORE::logcore(
				__METHOD__ . 'error while parsing ip/cidr:' . $s . ' got:' . $t->getMessage(),
				HappyFenceNGSyslogFacility::USER,
				HappyFenceNGSyslogSeverity::WARNING
			);
			return HappyFenceNGPlaceholder::NOT_A_VALID_IP_OR_CIDR;
		}
	}


	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $key ... you need one.
	 * @return read the code.
	 */
	public static function must_be_api_key( string $key ): string {
		$key = $key ?? '';
		$key = wp_unslash( $key );

		if ( ! preg_match( '/^[a-zA-Z0-9_-]+$/', $key ) ) {
			HappyFenceNGLOGCORE::logcore(
				__METHOD__ . 'must_be_api_key:User input not permitted:' . wp_json_encode( $key, true ),
				HappyFenceNGSyslogFacility::USER,
				HappyFenceNGSyslogSeverity::WARNING
			);

			return HappyFenceNGPlaceholder::EMPTY_STRING;
		}

		$len = strlen( $key );
		if ( $len < 16 || $len > 64 ) {
			HappyFenceNGLOGCORE::logcore(
				__METHOD__ . 'must_be_api_key:User input not permitted:' . wp_json_encode( $key, true ),
				HappyFenceNGSyslogFacility::USER,
				HappyFenceNGSyslogSeverity::WARNING
			);

			return HappyFenceNGPlaceholder::EMPTY_STRING;
		}

		return $key;
	}






	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $slug ... you need one.
	 * @return read the code.
	 */
	public static function must_be_slug( string $slug ): string {
		$slug = $slug ?? '';
		$slug = wp_unslash( $slug );
		if ( ! preg_match( '#^/?[a-z0-9-_]+/?$#i', $slug ) ) {
			HappyFenceNGLOGCORE::logcore(
				__METHOD__ . 'must_be_slug:User input not permitted:' . wp_json_encode( $slug, true ),
				HappyFenceNGSyslogFacility::USER,
				HappyFenceNGSyslogSeverity::WARNING
			);

			return HappyFenceNGPlaceholder::NOT_A_VALID_SLUG;
		}
		return sanitize_title( $slug );
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $url ... you need one.
	 * @return read the code.
	 */
	public static function must_be_url( string $url ): string {
		$url = $url ?? '';
		$url = wp_unslash( $url );
		if ( ! filter_var( $url, FILTER_VALIDATE_URL ) ) {
			HappyFenceNGLOGCORE::logcore(
				__METHOD__ . 'must_be_url:User input not permitted:' . wp_json_encode( $url, true ),
				HappyFenceNGSyslogFacility::USER,
				HappyFenceNGSyslogSeverity::WARNING
			);

			HappyFenceNGPlaceholder::NOT_A_VALID_URL;
		}
		return esc_url_raw( $url ?? '' );
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $email ... you need one.
	 * @return read the code.
	 */
	public static function must_be_email( string $email ): string {
		$email = $email ?? '';
		$email = wp_unslash( $email );
		if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
			HappyFenceNGLOGCORE::logcore(
				__METHOD__ . 'must_be_email:User input not permitted:' . wp_json_encode( $email, true ),
				HappyFenceNGSyslogFacility::USER,
				HappyFenceNGSyslogSeverity::WARNING
			);

			HappyFenceNGPlaceholder::NOT_A_VALID_EMAIL;
		}
		return sanitize_email( $email ?? '' );
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $val ... you need one.
	 * @return read the code.
	 */
	public static function must_be_int( $val ): int {
		$val = $val ?? '';
		if ( ( ( is_int( $val ) ) || ( is_string( $val ) && preg_match( '/^\d+$/', $val ) ) ) ) {
			return absint( $val );
		}
		HappyFenceNGLOGCORE::logcore(
			__METHOD__ . 'must_be_int:User input not permitted:' . wp_json_encode( $val, true ),
			HappyFenceNGSyslogFacility::USER,
			HappyFenceNGSyslogSeverity::WARNING
		);
		HappyFenceNGPlaceholder::NOT_A_VALID_INT;
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $to_check ... need to explain?.
	 * @return read the code.
	 */
	public static function must_be_md5( $to_check ): bool {
		if ( preg_match( '/^[a-f0-9]{32}$/i', $to_check ) ) {
			return true;
		}
		HappyFenceNGLOGCORE::logcore(
			__METHOD__ . 'must_be_md5:User input not permitted:' . wp_json_encode( $to_check, true ),
			HappyFenceNGSyslogFacility::USER,
			HappyFenceNGSyslogSeverity::WARNING
		);

		return false;
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $input ... you need one.
	 * @param int    $expected_length ...uhm.. guess it.
	 * @return read the code.
	 */
	public static function sanitize_cli_password( string $input, int $expected_length ): ?string {
		$input = ( $input ?? '' );

		if ( HappyFenceNGPROC::not_equals( strlen( $input ), $expected_length ) ) {
			return HappyFenceNGPlaceholder::EMPTY_STRING;
		}

		$allowed = '/^[a-zA-Z0-9@#%\^_\-\+=]+$/';

		if ( ! preg_match( $allowed, $input ) ) {
			return HappyFenceNGPlaceholder::EMPTY_STRING;
		}

		return $input;
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $val ... you need one.
	 * @return sometimes true,, sometimes not.
	 */
	public static function must_be_bool( $val ): bool {
				$response = false;
		switch ( $val ) {
			case 0:
						$response = false;
				break;
			case 1:
						$response = true;
				break;
			case '0':
						$response = false;
				break;
			case '1':
						$response = true;
				break;
			case 'true':
						$response = true;
				break;
			case 'false':
						$response = false;
				break;

			default:
				if ( is_bool( $val ) ) {
					$response = $val; } else {
					HappyFenceNGLOGCORE::logcore(
						__METHOD__ . ':User input not permitted:' . wp_json_encode( $val, true ),
						HappyFenceNGSyslogFacility::USER,
						HappyFenceNGSyslogSeverity::WARNING
					);
					return false;
					}
				break;
		}
				return $response;
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $val ... you need one.
	 * @param string $adefault ... really?.
	 * @return read the code.
	 */
	public static function checkMe( $val, $adefault ) {
		if ( HappyFenceNGPROC::equals( $val, HappyFenceNGPlaceholder::NOT_INITIALIZED )
			|| HappyFenceNGPROC::equals( $val, HappyFenceNGPlaceholder::IGNORE )
			|| HappyFenceNGPROC::equals( $val, HappyFenceNGPlaceholder::INVALID )
			|| HappyFenceNGPROC::equals( $val, HappyFenceNGPlaceholder::NO_RESULT )
			|| ( ! isset( $val ) )
			|| ( empty( $val ) )
			|| HappyFenceNGPROC::equals( $val, null ) ) {
			return $adefault;
		}
		return $val;
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $val ... you need one.
	 * @param string $adefault ... really?.
	 * @return read the code.
	 */
	public static function check_array( $val, $adefault ) {
		$result = self::checkMe( $val, $adefault );
		if ( is_array( $result ) ) {
			return $result;
		}
		return $adefault;
	}


	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $msg ... before last breath.
	 */
	public static function cleaner_die( string $msg = null ) {
		$msg = isset( $msg ) ? esc_html( $msg ) : esc_html__( 'Security refused this request, sorry.', 'happyfenceng-total-enterprise-security' );
		HappyFenceNGLOGCORE::logcore(
			$msg,
			HappyFenceNGSyslogFacility::CORE,
			HappyFenceNGSyslogSeverity::WARNING
		);
		$params = array(
			'texts'  => array(
				'title'        => esc_html__( 'Security Control', 'happyfenceng-total-enterprise-security' ),
				'message'      => $msg,
				'closing_line' => esc_html__( 'This incident is logged and will be reported.', 'happyfenceng-total-enterprise-security' ),
			),
			'links'  => array(
				'happyfenceng_logo_url' => esc_url( HAPPYFENCENG_PLUGIN_URL . 'assets/images/happyfenceng-small.png' ),

			),
			'values' => array(),
		);
		header( 'X-Robots-Tag: noindex, nofollow', true );
		HappyFenceNGVControl::renderView( $params, 'die' );
		exit( 200 );
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $title ... as title.
	 * @param string $msg ... as message.
	 * @param string $closing_line ... it is what is is.
	 */
	public static function fatal( string $title, string $msg, $closing_line ) {

		HappyFenceNGLOGCORE::logcore(
			$msg,
			HappyFenceNGSyslogFacility::CORE,
			HappyFenceNGSyslogSeverity::WARNING
		);
		$params = array(
			'texts'  => array(
				'title'        => $title,
				'message'      => $msg,
				'closing_line' => $closing_line,
			),
			'links'  => array(
				'happyfenceng_logo_url' => esc_url( HAPPYFENCENG_PLUGIN_URL . 'assets/images/happyfenceng-small.png' ),

			),
			'values' => array(),
		);
		HappyFenceNGVControl::renderView( $params, 'die' );
		exit;
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $msg ... put an excuse here.
	 */
	public static function cleaner_oops( $msg = null ) {
		$msg = isset( $msg ) ? esc_html( $msg ) : esc_html__( 'Something wrong happened, sorry.', 'happyfenceng-total-enterprise-security' );
		HappyFenceNGLOGCORE::logcore(
			$msg,
			HappyFenceNGSyslogFacility::SCANNER,
			HappyFenceNGSyslogSeverity::CRITICAL
		);
		$params = array(
			'texts'  => array(
				'title'        => 'Oops!',
				'message'      => $msg,
				'closing_line' => esc_html__( 'This error is logged and will be reported.', 'happyfenceng-total-enterprise-security' ),
			),
			'links'  => array(
				'happyfenceng_logo_url' => esc_url( HAPPYFENCENG_PLUGIN_URL . 'assets/images/happyfenceng-small.png' ),

			),
			'values' => array(),
		);
		HappyFenceNGVControl::renderView( $params, 'oops' );
		exit( 500 );
	}


	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $input ... the input to check.
	 */
	public static function must_be_hash( string $input ): string {
		// Rimuovere eventuali slash.
		$input = wp_unslash( $input ?? '' );

		// Verifica se l'input è un hash MD5 (32 caratteri alfanumerici).
		if ( ! preg_match( '/^[a-fA-F0-9]{32}$/', $input ) ) {
			// Log degli input non permessi.
			HappyFenceNGLOGCORE::logcore(
				__METHOD__ . 'mustBeHash:User input not permitted (invalid hash MD5): ' . wp_json_encode( $input, true ),
				HappyFenceNGSyslogFacility::USER,
				HappyFenceNGSyslogSeverity::WARNING
			);
			// Interrompe l'esecuzione con messaggio di errore.
			HappyFenceNGPlaceholder::NOT_A_VALID_HASH;
		}

		// Verifica se l'input contiene qualsiasi tag HTML o script (previene XSS).
		if ( preg_match( '/<(script|iframe|object|embed|img|svg|style|meta)[^>]*>|on\w+\s*=/i', $input ) ) {
			HappyFenceNGLOGCORE::logcore(
				__METHOD__ . 'mustBeHash:User input not permitted (contains HTML tags): ' . wp_json_encode( $input, true ),
				HappyFenceNGSyslogFacility::USER,
				HappyFenceNGSyslogSeverity::WARNING
			);
			HappyFenceNGPlaceholder::NOT_A_VALID_HASH;
		}

		// Verifica per codice PHP o funzioni potenzialmente dannose.
		if ( preg_match( '/(<\?php|\?>|include|require|exec|system|passthru|shell_exec|eval|file_get_contents|fopen|base64_decode)/i', $input ) ) {
			HappyFenceNGLOGCORE::logcore(
				__METHOD__ . 'mustBeHash:User input not permitted (contains PHP or dangerous functions): ' . wp_json_encode( $input, true ),
				HappyFenceNGSyslogFacility::USER,
				HappyFenceNGSyslogSeverity::WARNING
			);
			HappyFenceNGPlaceholder::NOT_A_VALID_HASH;
		}

		// Verifica per caratteri di encoding dannosi.
		if ( preg_match( '/(\\\x|%[0-9a-fA-F]{2})/', $input ) ) {
			HappyFenceNGLOGCORE::logcore(
				__METHOD__ . 'mustBeHash:User input not permitted (contains encoded characters): ' . wp_json_encode( $input, true ),
				HappyFenceNGSyslogFacility::USER,
				HappyFenceNGSyslogSeverity::WARNING
			);
			HappyFenceNGPlaceholder::NOT_A_VALID_HASH;
		}

		// Se tutte le verifiche sono passate, ritorna l'input.
		return $input;
	}
}
