<?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.. */
class HappyFenceNGVRINTE extends HappyFenceNGAVRoute {

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function doRoute() {
		try {

			$server    = sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ?? '' ) );
			$is_apache = ( HappyFenceNGPROC::equals( preg_match( '/apache/i', $server ), 1 ) );

			global $wp_filesystem;
			$nonce_save_settings           = wp_create_nonce( 'happyfenceng_INTE_save_settings' );
			$nonce_htaccess_reset_md5      = wp_create_nonce( 'happyfenceng_INTE_htaccess_reset_md5' );
			$nonce_htaccess_apply_patch    = wp_create_nonce( 'happyfenceng_INTE_htaccess_apply_patch' );
			$nonce_htaccess_restore_backup = wp_create_nonce( 'happyfenceng_INTE_htaccess_restore_backup' );
			$burn                          = HappyFenceNGFailFast::setup();

			$home_path     = get_home_path();
			$htaccess_path = $home_path . '.htaccess';

			$has_proposal = false;
			$current      = '';
			if ( HappyFenceNGTR::is_in( static::class, array( HappyFenceNGTR::AGGREGATE ) ) ) {
						$current      = file_exists( $htaccess_path ) ? $wp_filesystem->get_contents( $htaccess_path ) : HappyFenceNGPlaceholder::EMPTY_STRING;
						$proposed     = HappyFenceNGUtils::get_patch();
						$has_proposal = true;
			} else {
				$current = esc_html__( 'THIS FEATURE REQUIRES AGENCY OR ENTERPRISE', 'happyfenceng-total-enterprise-security' );
			}

			$params = array(
				'texts'  => array(
					'title'                      => esc_html__( '.htaccess File Protection', 'happyfenceng-total-enterprise-security' ),
					'description'                => esc_html__( 'This module checks your .htaccess.', 'happyfenceng-total-enterprise-security' ),
					'description_line_1'         => esc_html__( 'You can apply the proposed changes and, in case something does not work, revert to the original version.', 'happyfenceng-total-enterprise-security' ),
					'description_line_2'         => esc_html__( 'Just remember that if another plugin also change the same file and you revert the changes you will loose the part modified by the other plugin.', 'happyfenceng-total-enterprise-security' ),
					'description_not_apache'     => esc_html__( 'Notice, you are not running apache as web server so changing .htaccess is useless.', 'happyfenceng-total-enterprise-security' ),
					'description_not_apache_2'   => esc_html__( '.htaccess protection feature is disabled.', 'happyfenceng-total-enterprise-security' ),
					'enable_htaccess_protection' => esc_html__( 'Enable .htaccess Protection', 'happyfenceng-total-enterprise-security' ),
					'apply_patch'                => esc_html__( 'Apply patch', 'happyfenceng-total-enterprise-security' ),
					'restore_patch'              => esc_html__( 'Restore backup', 'happyfenceng-total-enterprise-security' ),
					'reset_md5'                  => esc_html__( 'Reset check', 'happyfenceng-total-enterprise-security' ),
					'explain_button'             => esc_html__( 'Click this button to reset the control, but only if you are sure no one else has modified your .htaccess', 'happyfenceng-total-enterprise-security' ),
					'proposed_patch'             => $has_proposal ? esc_html( $proposed ) : esc_html__( 'THIS FEATURE REQUIRES AGENCY OR ENTERPRISE', 'happyfenceng-total-enterprise-security' ),
					'current_htaccess'           => esc_html( $current ),
					'proposed_patch_title'       => esc_html__( 'Proposed patched .htaccess', 'happyfenceng-total-enterprise-security' ),
					'current_htaccess_title'     => esc_html__( 'Current .htaccess', 'happyfenceng-total-enterprise-security' ),
					'code_anti_tampering_title'  => esc_html__( 'Code-Tampering check', 'happyfenceng-total-enterprise-security' ),
					'code_anti_tampering_line_1' => esc_html__( 'This advanced feature monitors the core plugin code for any unauthorized modifications.', 'happyfenceng-total-enterprise-security' ),
					'code_anti_tampering_line_2' => esc_html__( 'Important notes:', 'happyfenceng-total-enterprise-security' ),
					'code_anti_tampering_line_3' => esc_html__( 'All tampering events are logged exclusively in the "happyfenceng-system.log" file. They are NOT sent to the SIEM or stored in the database, as the core plugin cannot depend on other logging libraries. It is crucial to monitor this log with your SIEM or other monitoring tools.', 'happyfenceng-total-enterprise-security' ),
					'code_anti_tampering_line_4' => esc_html__( 'If a code tampering event is detected, the plugin and the Admin UI will be immediately locked to prevent execution of potentially harmful code.', 'happyfenceng-total-enterprise-security' ),
					'code_anti_tampering_line_5' => esc_html__( 'To restore access, you must manually delete the plugin folder and reinstall an unmodified version of HappyFenceNG.', 'happyfenceng-total-enterprise-security' ),
					'code_anti_tampering_line_6' => esc_html__( 'It is strongly recommended to perform a security review of your site to determine how the code modification occurred.', 'happyfenceng-total-enterprise-security' ),
					'code_anti_tampering_line_7' => esc_html__( 'This feature is available only in premium editions and SHOULD NEVER be disabled.', 'happyfenceng-total-enterprise-security' ),
					'web_server'                 => esc_html__( 'Your webserver is:', 'happyfenceng-total-enterprise-security' ) . $server,
					'feature_requires'           => esc_html__( 'THIS FEATURE REQUIRES AGENCY OR ENTERPRISE', 'happyfenceng-total-enterprise-security' ),
					'save_settings'              => esc_html__( 'Save Settings', 'happyfenceng-total-enterprise-security' ),
					'settings_saved'             => esc_html__( 'Settings Saved', 'happyfenceng-total-enterprise-security' ),
					'error_saving_settings'      => esc_html__( 'Error saving settings', 'happyfenceng-total-enterprise-security' ),

				),
				'links'  => array(
					'happyfenceng_logo_url' => esc_url( HAPPYFENCENG_PLUGIN_URL . 'assets/images/happyfenceng-small.png' ),

				),
				'values' => array(
					'enable_htaccess_protection' => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->FILESYSTEM->enable_htaccess_protection, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT ),
					'htaccess_does_not_match'    => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->FILESYSTEM->htaccess_md5_does_not_match, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT ),
					'checkbox_on'                => HappyFenceNGPlaceholder::CHECKBOX_ON,
					'checkbox_off'               => HappyFenceNGPlaceholder::CHECKBOX_OFF,
					'is_apache'                  => $is_apache,
					'nonce'                      => $nonce_save_settings,
					'nonceHtaccessResetMd5'      => $nonce_htaccess_reset_md5,
					'nonceHtaccessApplyPatch'    => $nonce_htaccess_apply_patch,
					'nonceHtaccessRestoreBackup' => $nonce_htaccess_restore_backup,
					'burnNonce'                  => $burn,
					'feature_enabled'            => ( function_exists( 'htes_fs' ) && HappyFenceNGTR::is_in( static::class, array( HappyFenceNGTR::AGGREGATE ) ) ),
				),
			);

			ob_start();
			HappyFenceNGVControl::renderView( $params, 'inte' );
			// phpcs:ignore WordPress.Security.EscapeOutput
			echo ob_get_clean();

		} catch ( Throwable $e ) {
			HappyFenceNGLOGCORE::logcore(
				'HappyFenceNGVRINTE - cannot render UI ' . $e->getMessage(),
				HappyFenceNGSyslogFacility::UI,
				HappyFenceNGSyslogSeverity::ERROR
			);
			ob_end_clean();
			HappyFenceNGCleaner::cleaner_oops();
		}
	}
}
