<?php

/**
 * The file that defines the core plugin class
 *
 * A class definition that includes attributes and functions used across both the
 * public-facing side of the site and the admin area.
 *
 * @link       https://wpiron.com
 * @since      1.0.0
 *
 * @package    iron-security
 * @subpackage iron-security/includes
 */

/**
 * The core plugin class.
 *
 * This is used to define internationalization, admin-specific hooks, and
 * public-facing site hooks.
 *
 * Also maintains the unique identifier of this plugin as well as the current
 * version of the plugin.
 *
 * @since      1.0.0
 * @package    iron-security
 * @subpackage iron-security/includes
 * @author     WPIron <info@wpiron.com>
 */
class iron_security {

	protected $loader;
	protected $plugin_name;
	protected $version;
	protected $login_logout_functionality;


	public function __construct() {
		if ( defined( 'IRON_SECURITY_VERSION' ) ) {
			$this->version = IRON_SECURITY_VERSION;
		} else {
			$this->version = '1.0.0';
		}
		$this->plugin_name = 'iron-security';

		$this->load_dependencies();
		$this->set_locale();
		$this->define_admin_hooks();
		$this->define_public_hooks();
	}

	private function load_dependencies() {
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/ServiceContainer.php';
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-iron-security-loader.php';
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-iron-security-i18n.php';
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-iron-security-admin.php';
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/classes/login-logout-functionality.php';
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/classes/general-security.php';
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-iron-security-public.php';
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/classes/WpironHelper.php';
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/classes/WpironFilesDirectoryProtection.php';
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/classes/WpironLogging.php';
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/classes/WpironSecurityHeaders.php';
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/classes/WpironTwoFactorAuth.php';
		$this->loader = new iron_securi_Loader();
	}

	private function set_locale() {
		$plugin_i18n = new iron_securi_i18n();

		$this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' );
	}

	private function define_admin_hooks() {

		$container = new ServiceContainer();
		$container->register('helper', new WpironHelper());
		$container->register('logs', new WpironLogging($container->get('helper')));
		$container->register('security_headers', new WpironSecurityHeaders($container->get('helper')));
		$container->register('file_directory_protection', new WpironFilesDirectoryProtection($container->get('helper')));
		$container->register('authentication', new iron_securi_Login_Logout_Functionality($container->get('helper')));
		$container->register('general', new iron_security_General_Security($container->get('helper')));
		$container->register('two_factor_auth', new WpironTwoFactorAuth($container->get('helper')));

		$plugin_admin = new iron_security_Admin(
			$this->get_plugin_name(),
			$this->version,
			$container->get('general'),
			$container->get('authentication'),
			$container->get('helper'),
			$container->get('file_directory_protection'),
			$container->get('security_headers'),
			$container->get('two_factor_auth'),
			$container->get('logs')
		);

		// Register the autoupdate setting during plugin initialization
		$this->loader->add_action('init', $container->get('general'), 'register_plugin_autoupdate_setting');

		$this->loader->add_action( 'init', $plugin_admin, 'wpironis_verify2fa', 2 );
		$this->loader->add_action( 'login_form', $plugin_admin, 'wpironis_add_2fa_field' );
		$this->loader->add_action( 'wp_login', $plugin_admin, 'wpironis_auth_redirect', 10, 2 );
		$this->loader->add_action( 'admin_notices', $plugin_admin, 'wpironis_display_2fa_setup_notice' );
		$this->loader->add_action( 'show_user_profile', $plugin_admin, 'wpironis_add_user_profile_fields' );
		$this->loader->add_action( 'edit_user_profile', $plugin_admin, 'wpironis_add_user_profile_fields' );
		$this->loader->add_action( 'personal_options_update', $plugin_admin, 'wpironis_save_user_profile_fields' );
		$this->loader->add_action( 'edit_user_profile_update', $plugin_admin, 'wpironis_save_user_profile_fields' );
		$this->loader->add_action( 'wp_ajax_iron_security_verify_2fa_code',
			$plugin_admin,
			'wpironis_ajax_verify_2fa_code' );
		$this->loader->add_action( 'wp_ajax_iron_security_verify_2fa_setup',
			$plugin_admin,
			'wpironis_ajax_verify_2fa_setup' );
		$this->loader->add_action( 'wp_ajax_iron_security_save_2fa_settings',
			$plugin_admin,
			'wpironis_ajax_save_2fa_settings' );
		$this->loader->add_action( 'wp_ajax_iron_security_toggle_2fa', $plugin_admin, 'wpironis_ajax_toggle_2fa' );
		$this->loader->add_action( 'wp_ajax_iron_security_save_2fa_settings_loginlogout',
			$plugin_admin,
			'wpironis_ajax_save_2fa_settings_from_loginlogout' );
		$this->loader->add_action( 'wp_ajax_nopriv_iron_security_check_2fa_required',
			$plugin_admin,
			'wpironis_check_2fa_required' );
		$this->loader->add_action( 'admin_init', $plugin_admin, 'wpironis_check_auth_status', 1 );
		$this->loader->add_action( 'wp_logout', $plugin_admin, 'wpironis_clear_2fa_verification_on_logout' );

		$this->loader->add_filter(
			'plugin_action_links_iron-security/iron-security.php',
			$plugin_admin,
			'wpironis_additional_links'
		);

		$this->loader->add_action( 'activated_plugin', $plugin_admin, 'wpironis_activate_plugin' );
		$this->loader->add_action( 'deactivated_plugin', $plugin_admin, 'wpironis_deactivate_plugin' );
		$this->loader->add_action( 'deleted_plugin', $plugin_admin, 'wpironis_delete_plugin' );

		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'wpironis_enqueue_styles' );
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'wpironis_enqueue_scripts' );
		$this->loader->add_action( 'admin_menu', $plugin_admin, 'wpironis_plugin_menu' );

		$this->loader->add_action( 'init', $plugin_admin, 'wpironis_block_locked_out_ip_early', 1 );
		$this->loader->add_action( 'login_init', $plugin_admin, 'wpironis_block_locked_out_ip_early', 1 );

		$this->loader->add_action( 'template_redirect', $plugin_admin, 'wpironis_template_redirect' );
		$this->loader->add_action( 'parse_request', $plugin_admin, 'wpironis_parse_request' );
		$this->loader->add_action( 'init', $plugin_admin, 'wpironis_block_entry_points' );
		$this->loader->add_filter( 'login_url', $plugin_admin, 'wpironis_login_url', 10, 3 );
		$this->loader->add_filter( 'site_url', $plugin_admin, 'wpironis_site_url', 10, 3 );
		$this->loader->add_filter( 'query_vars', $plugin_admin, 'wpironis_query_vars' );

		$this->loader->add_filter( 'auth_cookie_expiration', $plugin_admin, 'wpironis_set_session_timeout', 10, 1 );
		$this->loader->add_action( 'wp_login_failed', $plugin_admin, 'wpironis_handle_failed_login', 10, 3 );
		$this->loader->add_action( 'wp_login', $plugin_admin, 'log_successful_login', 10, 2 );
		$this->loader->add_filter( 'authenticate', $plugin_admin, 'wpironis_block_locked_out_users', 10, 3 );
		$this->loader->add_action( 'login_form',
			$plugin_admin,
			'wpironsecwp_verify_nonce_before_authentication',
			10,
			3 );

		$this->loader->add_action( 'update_option_wpironis_plugin_settings_general',
			$plugin_admin,
			'wpironis_manage_htaccess',
			10,
			2 );
		$this->loader->add_filter( 'upload_mimes', $plugin_admin, 'wpironis_prevent_php_upload' );
		$this->loader->add_filter( 'wp_handle_upload_prefilter', $plugin_admin, 'wpironis_prevent_php_upload' );


		$this->loader->add_action( 'wp_ajax_iron_security_toggle_xmlrpc',
			$plugin_admin,
			'wpironis_handle_xmlrpc_toggle' );
		$this->loader->add_action( 'wp_ajax_iron_security_toggle_wp_version',
			$plugin_admin,
			'wpironis_handle_wp_version_toggle' );
		$this->loader->add_action( 'wp_ajax_iron_security_toggle_security_headers',
			$plugin_admin,
			'wpironis_handle_security_headers_toggle' );


		$this->loader->add_action( 'wp_ajax_iron_security_toggle_direct_access',
			$plugin_admin,
			'wpironis_handle_direct_file_access_toggle' );
		$this->loader->add_action( 'wp_ajax_iron_security_toggle_php_uploads',
			$plugin_admin,
			'wpironis_handle_php_uploads_toggle' );


		$this->loader->add_action( 'wp_ajax_iron_security_toggle_file_editor',
			$plugin_admin,
			'wpironis_handle_file_editor_toggle' );
		$this->loader->add_action( 'wp_ajax_iron_security_toggle_rest_api',
			$plugin_admin,
			'wpironis_handle_rest_api_toggle' );
		$this->loader->add_action( 'wp_ajax_iron_security_toggle_plugin_autoupdate',
			$plugin_admin,
			'wpironis_handle_plugin_autoupdate_toggle' );
		$this->loader->add_action( 'wp_ajax_iron_security_toggle_core_autoupdate',
			$plugin_admin,
			'wpironis_handle_core_autoupdate_toggle' );

		// Authentication
		$this->loader->add_action( 'wp_ajax_iron_security_toggle_custom_url',
			$plugin_admin,
			'wpironis_handle_custom_url_toggle' );
		$this->loader->add_action( 'wp_ajax_iron_security_save_custom_url',
			$plugin_admin,
			'wpironis_handle_custom_url_save' );


		$this->loader->add_action( 'wp_ajax_iron_security_toggle_session_timeout',
			$plugin_admin,
			'wpironis_handle_session_timeout_toggle' );
		$this->loader->add_action( 'wp_ajax_iron_security_save_session_timeout',
			$plugin_admin,
			'wpironis_handle_session_timeout_save' );


		$this->loader->add_action( 'wp_ajax_iron_security_toggle_limit_login',
			$plugin_admin,
			'wpironis_handle_limit_login_toggle' );
		$this->loader->add_action( 'wp_ajax_iron_security_save_limit_login',
			$plugin_admin,
			'wpironis_handle_limit_login_save' );


		$this->loader->add_action( 'wp_ajax_iron_security_toggle_limit_admins',
			$plugin_admin,
			'wpironis_handle_limit_admins_toggle' );
		$this->loader->add_action( 'wp_ajax_iron_security_save_limit_admins',
			$plugin_admin,
			'wpironis_handle_limit_admins_save' );


		$this->loader->add_action( 'wp_ajax_iron_security_toggle_admin_id_protection',
			$plugin_admin,
			'wpironis_handle_admin_id_protection_toggle' );

		$this->loader->add_action( 'wp_ajax_iron_security_toggle_change_admin_username',
			$plugin_admin,
			'wpironis_handle_change_admin_username_toggle' );
		$this->loader->add_action( 'wp_ajax_iron_security_save_change_admin_username',
			$plugin_admin,
			'wpironis_handle_change_admin_username_save' );

		$this->loader->add_action( 'set_user_role',
			$plugin_admin,
			'wpironis_enforce_admin_limit',
			10,
			3 );

		$this->loader->add_action( 'user_register',
			$plugin_admin,
			'wpironis_check_admin_limit_on_register',
			10,
			1 );


		$this->loader->add_action( 'init', $plugin_admin, 'wpironis_block_user_enumeration' );
		$this->loader->add_filter( 'login_errors', $plugin_admin, 'wpironis_modify_login_errors' );

		$this->loader->add_filter( 'wp_ajax_iron_security_toggle_user_enum',
			$plugin_admin,
			'wpironis_handle_user_enum_toggle' );
		$this->loader->add_filter( 'wp_ajax_iron_security_save_user_enum_message',
			$plugin_admin,
			'wpironis_handle_user_enum_message_save' );

		$options = get_option( 'wpironis_options', array() );


		if ( ! empty( $options['wpironis_enable_core_autoupdate'] ) && $options['wpironis_enable_core_autoupdate'] === 1 ) {
			add_filter( 'allow_major_auto_core_updates', '__return_true' );
			add_filter( 'allow_minor_auto_core_updates', '__return_true' );
			add_filter( 'auto_update_core', '__return_true' );
			add_filter( 'auto_update_translation', '__return_true' );
		} else {
			add_filter( 'allow_major_auto_core_updates', '__return_false' );
			add_filter( 'allow_minor_auto_core_updates', '__return_false' );
			add_filter( 'auto_update_core', '__return_false' );
			add_filter( 'auto_update_translation', '__return_false' );
		}

		$this->loader->add_action( 'wp_ajax_iron_security_get_admin_info', $plugin_admin, 'wpironsec_get_admin_info' );

		$this->loader->add_action( 'wp_ajax_iron_security_extend_session', $plugin_admin, 'extend_session' );

		$this->loader->add_action( 'wp_ajax_iron_security_get_logs', $plugin_admin, 'wpironsec_get_security_logs' );
		$this->loader->add_action( 'wp_ajax_iron_security_clear_logs', $plugin_admin, 'wpironsec_clear_security_logs' );
		$this->loader->add_action( 'wp_logout', $plugin_admin, 'wpironsec_custom_function_for_logout' );
		$this->loader->add_action( 'wp_ajax_iron_security_get_settings', $plugin_admin, 'wpironis_get_settings' );
		$this->loader->add_action( 'wp_ajax_iron_security_toggle_ai_bot_blocking',
			$plugin_admin,
			'wpironis_handle_ai_bot_blocking_toggle' );
		$this->loader->add_action( 'wp_ajax_iron_security_get_system_info', $plugin_admin, 'wpironis_get_system_info' );
	}

	private function define_public_hooks() {
		$plugin_public = new iron_securi_Public( $this->get_plugin_name(), $this->get_version() );

		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'wpironis_enqueue_styles' );
		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'wpironis_enqueue_scripts' );
		$this->loader->add_filter( 'xmlrpc_enabled', $plugin_public, 'wpironis_xmlrpc_enabled' );
		$this->loader->add_action( 'init', $plugin_public, 'wpironis_init' );
		$this->loader->add_filter( 'rest_authentication_errors', $plugin_public, 'wpironis_restrict_rest_api' );
		$this->loader->add_filter( 'rest_endpoints', $plugin_public, 'wpironis_disable_rest_endpoints' );
	}

	public function run() {
		$this->loader->run();
	}

	public function get_plugin_name() {
		return $this->plugin_name;
	}

	public function get_loader() {
		return $this->loader;
	}

	public function get_version() {
		return $this->version;
	}


}
