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

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

			$nonce_save_settings = wp_create_nonce( 'happyfenceng_AGG_save_settings' );
			$burn                = HappyFenceNGFailFast::setup();

			$params = array(
				'texts'  => array(
					'title'                               => esc_html__( 'System Shields Security', 'happyfenceng-total-enterprise-security' ),
					'important'                           => esc_html__( 'Important!', 'happyfenceng-total-enterprise-security' ),
					'sub_title_filesystem_sec'            => esc_html__( 'FileSystem Security', 'happyfenceng-total-enterprise-security' ),
					'enable_filesystem_scan_title'        => esc_html__( 'Warn if dangerous files are present', 'happyfenceng-total-enterprise-security' ),
					'enable_filesystem_scan_description'  => esc_html__( 'Scan for readme.html, license.txt, wp-config-sample.php', 'happyfenceng-total-enterprise-security' ),
					'enable_filesystem_scan_tooltip'      => esc_html__( 'Scans for leftover default WordPress files like readme.html, license.txt, and wp-config-sample.php. This function only provides a warning. Auto-removal is available in the Enterprise version under Advanced Shields.', 'happyfenceng-total-enterprise-security' ),

					'disable_builtin_file_edit_title'     => esc_html__( 'Disable built-in file edit', 'happyfenceng-total-enterprise-security' ),
					'disable_builtin_file_edit_description' => esc_html__( 'Prevent access', 'happyfenceng-total-enterprise-security' ),
					'disable_builtin_file_edit_tooltip_1' => esc_html__( 'When enabled, this option prevents access to the built-in plugin and theme file editors inside the WordPress admin panel. It improves security by blocking direct code edits via the dashboard.', 'happyfenceng-total-enterprise-security' ),
					'disable_builtin_file_edit_tooltip_2' => esc_html__( 'Please note: you need to reload WordPress or restart the site for this change to apply. ', 'happyfenceng-total-enterprise-security' ),

					'monitor_unused_plugin_title'         => esc_html__( 'Monitor unused plugins', 'happyfenceng-total-enterprise-security' ),
					'monitor_unused_plugin_description'   => esc_html__( 'Scan for unused plugins', 'happyfenceng-total-enterprise-security' ),
					'monitor_unused_plugin_tooltip'       => esc_html__( 'When enabled, this option monitors unused plugins. Unused plugins can be a security risk, as they are not updated automatically.', 'happyfenceng-total-enterprise-security' ),
					'sub_title_rate_limiting'             => esc_html__( 'Rate limiting for unauthenticated requests', 'happyfenceng-total-enterprise-security' ),
					'enable_login_rate_limit_title'       => esc_html__( 'Enable rate limit', 'happyfenceng-total-enterprise-security' ),
					'enable_login_rate_limit_description' => esc_html__( 'Throttle requests from the same IP (login, REST, XML-RPC)', 'happyfenceng-total-enterprise-security' ),
					'enable_login_rate_limit_tooltip'     => esc_html__( 'Limits repeated login requests from the same IP to stop brute force attempts.', 'happyfenceng-total-enterprise-security' ),
					'login_rate_interval_title'           => esc_html__( 'Login rate interval', 'happyfenceng-total-enterprise-security' ),
					'login_rate_interval_description'     => esc_html__( 'Fine tune your interval time', 'happyfenceng-total-enterprise-security' ),
					'login_rate_interval_tooltip'         => esc_html__( 'Time in seconds between request attemps set', 'happyfenceng-total-enterprise-security' ),
					'login_rate_max_title'                => esc_html__( 'Login rate max', 'happyfenceng-total-enterprise-security' ),
					'login_rate_max_description'          => esc_html__( 'Max attempts per interval', 'happyfenceng-total-enterprise-security' ),
					'login_rate_max_tooltip'              => esc_html__( 'Set the maximum number of login attempts allowed per interval. Adjust carefully to avoid locking out legitimate users.', 'happyfenceng-total-enterprise-security' ),
					'sub_title_robots_txt'                => esc_html__( 'robots.txt Management', 'happyfenceng-total-enterprise-security' ),
					'auto_patch_robots_title'             => esc_html__( 'Auto-patch robots.txt', 'happyfenceng-total-enterprise-security' ),
					'auto_patch_robots_description'       => esc_html__( 'Automatically add Disallow: /wp-json/ if missing ', 'happyfenceng-total-enterprise-security' ),
					'auto_patch_robots_tooltip'           => esc_html__( 'Prevents search engines from indexing your REST API by updating robots.txt automatically.', 'happyfenceng-total-enterprise-security' ),
					'sub_title_light_uri_filters'         => esc_html__( 'Light URI filters', 'happyfenceng-total-enterprise-security' ),
					'enabled_uri_patterns_info'           => esc_html__( 'Warning: some aggressive filters like mod_security can interfere with HappyFenceNG while dealing with the patterns below. For this reason we use leet encoding. If you expericence troubles when saving this options and/or get Unauthorized message, you know where to point your finger.', 'happyfenceng-total-enterprise-security' ),
					'enabled_uri_patterns_title'          => esc_html__( 'Enable detection for:', 'happyfenceng-total-enterprise-security' ),
					'feature_requires'                    => esc_html__( 'THIS FEATURE REQUIRES AGENCY OR ENTERPRISE', 'happyfenceng-total-enterprise-security' ),
					'enable_filesystem_scan'              => esc_html__( 'Enable Filesystem Scan', '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_filesystem_scan'   => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->FILESYSTEM->enable_filesystem_scan, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT ),
					'disable_file_edit'        => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->FILESYSTEM->disable_file_edit, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT ),
					'enable_plugin_monitoring' => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->FILESYSTEM->enable_plugin_monitoring, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT ),
					'enable_login_rate_limit'  => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->LOGIN->enable_login_rate_limit, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT ),
					'login_rate_interval'      => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->LOGIN->login_rate_interval, HappyFenceNGWhoAmI::get_instance()->LOGIN::_LOGIN_RATE_INTERVAL ),
					'login_rate_max'           => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->LOGIN->login_rate_max, HappyFenceNGWhoAmI::get_instance()->LOGIN::_LOGIN_RATE_MAX ),
					'auto_patch_robots'        => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->ROBOTS->auto_patch_robots, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT ),
					'enabled_uri_patterns'     => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->FILESYSTEM->enabled_uri_patterns, HappyFenceNGPlaceholder::EMPTY_ARRAY ),
					'uri_patterns'             => HappyFenceNGURIPattern::as_keys(),
					'checkbox_on'              => HappyFenceNGPlaceholder::CHECKBOX_ON,
					'checkbox_off'             => HappyFenceNGPlaceholder::CHECKBOX_OFF,
					'nonce'                    => $nonce_save_settings,
					'burnNonce'                => $burn,
					'feature_enabled'          => ( function_exists( 'htes_fs' ) && HappyFenceNGTR::is_in( static::class, array( HappyFenceNGTR::AGGREGATE ) ) ),

				),
			);

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

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