<?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.. */
abstract class HappyFenceNGRuntimeExceptions extends \RuntimeException {
    /**This comment is for the monkey PHPCS code scanner
     *
     * @param string     $msg ...oh yes .. a message should be.
     * @param int        $code ... something like an exit code?.
     * @param \Throwable $prev .
     * @throws \RuntimeException .
     */
    protected static function raiseException( string $msg, int $code = 0, ?\Throwable $prev = null ) : never {
        $cls = static::class;
        $msg = sprintf( '[%s] %s', $cls, $msg );
        HappyFenceNGLOGCORE::logcore( static::class . " ([HappyFenceNGSystem ] RAISE EXCEPTION =>  {$msg} )", HappyFenceNGSyslogFacility::CORE, HappyFenceNGSyslogSeverity::ERROR );
        if ( is_admin() ) {
            set_transient( 'happyfenceng_last_error', $msg, 14400 );
        }
        // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
        throw new \RuntimeException($msg, $code, $prev);
    }

    /**This comment is for the monkey PHPCS code scanner
     *
     * @param string $msg ... a message to another planet.
     */
    public static function tampered_die( string $msg ) {
        wp_die( '<div style="text-align:center;">            
                <img src="' . esc_url( HAPPYFENCENG_PLUGIN_URL . 'assets/images/happyfenceng-small.png' ) . '" style="max-width:150px;margin-bottom:20px;">
                <p>' . esc_html( $msg ) . '</p>
                <p>
                    The admin interface is now locked to protect your site.<br>
                    This strongly suggests a potential intrusion or unauthorized access.<br>
                    Your site might already be compromised.<br><br>

                    To recover safely, you must manually disable or remove the plugin,<br>
                    then download and reinstall the official, unmodified version.<br><br>

                    If your error code is <strong>DH100</strong>, check your database: the HappyFenceNG tables might have been modified.<br>
                    In this case, we strongly recommend restoring the HappyFenceNG tables from a backup and carefully verifying<br>
                    that your database has not been compromised by a malicious attacker.<br><br>

                    Ignoring this warning may lead to data loss, security breaches, or full site compromise.<br>
                    Act immediately to secure your installation!<br><br>

                    ' . esc_html__( 'This incident is logged and will be reported.', 'happyfenceng-total-enterprise-security' ) . '
                </p>
            </div>', esc_html__( 'HappyFenceNG - Code changed', 'happyfenceng-total-enterprise-security' ), array(
            'response'       => 200,
            'back_link'      => false,
            'wp_die_handler' => 'default',
        ) );
    }

    /**PHPCS -> code compliance: we put useful comments where needed..
     *
     * @param string $cls .
     */
    public static function __constructor( $cls ) {
        global $wp_filesystem;
    }

}

/**PHPCS -> code compliance: we put useful comments where needed.. */
HappyFenceNGRuntimeExceptions::__constructor( HappyFenceNGRuntimeExceptions::class );