<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class iron_security_Admin_Checker_Functionality {
	function __construct() {
	}

	public function mainPage() {
		?>
        <div class="wrap">
            <h1>Checker</h1>
            <table class="wp-list-table widefat fixed striped">
                <thead>
                <tr>
                    <th>Check</th>
                    <th>Status</th>
                    <th>Explanation</th>
                </tr>
                </thead>
                <tbody>
				<?php
				$this->wpironis_checker_statuses(); ?>
                </tbody>
            </table>
        </div>
		<?php
	}

	public function wpironis_checker_statuses() {
		$checks = [
			'Can rewrite .htaccess'     => $this->can_rewrite_htaccess() ? 'green' : 'red',
			'File Permissions'          => $this->check_file_permissions() ? 'green' : 'red',
			'Admin Username'            => $this->check_admin_username() ? 'green' : 'red',
			'Database Prefix'           => $this->check_db_prefix() ? 'green' : 'red',
			'Security Keys'             => $this->check_security_keys() ? 'green' : 'red',
			'PHP Version'               => $this->check_php_version() ? 'green' : 'red',
			'Plugin Updates'            => $this->check_plugin_updates() ? 'green' : 'red',
			'Theme Updates'             => $this->check_theme_updates() ? 'green' : 'red',
			'Directory Listing'         => $this->check_directory_listing() ? 'green' : 'red',
			'SSL Certificate'           => $this->check_ssl_certificate() ? 'green' : 'red',
			'Inactive Plugins'          => $this->check_inactive_plugins() ? 'green' : 'red',
			'Two-Factor Authentication' => $this->check_two_factor_auth() ? 'green' : 'red',
			'XML-RPC'                   => $this->check_xml_rpc() ? 'green' : 'red',
			'Firewall'                  => $this->check_firewall() ? 'green' : 'red',
			'Backup'                    => $this->check_backup() ? 'green' : 'red',
		];

		foreach ( $checks as $check => $status ) {
			$explanation = $this->get_explanation( $check, $status );

			$check       = esc_html( $check );
			$status      = esc_attr( $status );
			$explanation = esc_html( $explanation );

			echo "<tr><td>{$check}</td><td><span style='font-size:30px; color: {$status};'>&#9679;</span></td><td>{$explanation}</td></tr>";
		}
	}


	private function get_explanation( $check, $status ) {
		$explanations = [
			'Can rewrite .htaccess'     => [
				'green' => 'The .htaccess file is writable. The plugin or WordPress can modify this file for various configurations.',
				'red'   => 'The .htaccess file is not writable. Change the file permissions to make it writable. Use chmod 644 .htaccess or set appropriate permissions via your hosting control panel.'
			],
			'File Permissions'          => [
				'green' => 'Critical files (wp-config.php and .htaccess) have appropriate permissions.',
				'red'   => 'Critical files do not have secure permissions. Set appropriate permissions. Use chmod 644 wp-config.php and chmod 644 .htaccess.'
			],
			'Admin Username'            => [
				'green' => 'The default "admin" username is not used.',
				'red'   => 'The default "admin" username is used. Create a new admin user with a different username and delete the old "admin" user.'
			],
			'Database Prefix'           => [
				'green' => 'The database prefix is not "wp_".',
				'red'   => 'The database prefix is "wp_". Change the database prefix in the wp-config.php file and update all database tables accordingly. This is best done with a plugin or via a script.'
			],
			'Security Keys'             => [
				'green' => 'All security keys are set.',
				'red'   => 'One or more security keys are not set. Generate and add security keys in the wp-config.php file. You can use the WordPress secret-key service.'
			],
			'PHP Version'               => [
				'green' => 'PHP version is 7.4 or higher.',
				'red'   => 'PHP version is lower than 7.4. Upgrade to a newer PHP version via your hosting control panel.'
			],
			'Plugin Updates'            => [
				'green' => 'All plugins are up to date.',
				'red'   => 'One or more plugins have updates available. Update all plugins via the WordPress admin dashboard.'
			],
			'Theme Updates'             => [
				'green' => 'All themes are up to date.',
				'red'   => 'One or more themes have updates available. Update all themes via the WordPress admin dashboard.'
			],
			'Directory Listing'         => [
				'green' => 'Directory listing is disabled.',
				'red'   => 'Directory listing is enabled. Add Options -Indexes to the .htaccess file.'
			],
			'SSL Certificate'           => [
				'green' => 'SSL is enabled.',
				'red'   => 'SSL is not enabled. Obtain and install an SSL certificate, then configure WordPress to use HTTPS.'
			],
			'Inactive Plugins'          => [
				'green' => 'No inactive plugins are present.',
				'red'   => 'One or more inactive plugins are present. Delete all inactive plugins via the WordPress admin dashboard.'
			],
			'Two-Factor Authentication' => [
				'green' => 'Two-factor authentication is enabled.',
				'red'   => 'Two-factor authentication is not enabled. Enable 2FA using a plugin like Google Authenticator.'
			],
			'XML-RPC'                   => [
				'green' => 'XML-RPC is disabled.',
				'red'   => 'XML-RPC is enabled. Disable XML-RPC by adding add_filter(\'xmlrpc_enabled\', \'__return_false\'); to your theme\'s functions.php file.'
			],
			'Firewall'                  => [
				'green' => 'A firewall is enabled.',
				'red'   => 'A firewall is not enabled. Install a security plugin like Wordfence or configure a server-level firewall.'
			],
			'Backup'                    => [
				'green' => 'Regular backups are configured.',
				'red'   => 'Regular backups are not configured. Set up regular backups using a plugin like UpdraftPlus or via your hosting provider.'
			]
		];

		return $explanations[ $check ][ $status ];
	}

	private function can_rewrite_htaccess() {
		$htaccess_file = ABSPATH . '.htaccess';

		return is_writable( $htaccess_file );
	}

	private function check_file_permissions() {
		$files_to_check = [
			ABSPATH . 'wp-config.php',
			ABSPATH . '.htaccess'
		];
		foreach ( $files_to_check as $file ) {
			if ( file_exists( $file ) && ! is_writable( $file ) ) {
				return false;
			}
		}

		return true;
	}

	private function check_admin_username() {
		return username_exists( 'admin' ) ? false : true;
	}

	private function check_db_prefix() {
		global $wpdb;

		return $wpdb->prefix === 'wp_' ? false : true;
	}

	private function check_security_keys() {
		$keys = [
			'AUTH_KEY',
			'SECURE_AUTH_KEY',
			'LOGGED_IN_KEY',
			'NONCE_KEY',
			'AUTH_SALT',
			'SECURE_AUTH_SALT',
			'LOGGED_IN_SALT',
			'NONCE_SALT'
		];
		foreach ( $keys as $key ) {
			if ( defined( $key ) && constant( $key ) ) {
				continue;
			}

			return false;
		}

		return true;
	}

	private function check_php_version() {
		return version_compare( PHP_VERSION, '7.4', '>=' );
	}

	private function check_plugin_updates() {
		$updates = get_plugin_updates();

		return empty( $updates );
	}

	private function check_theme_updates() {
		$updates = wp_get_themes();
		foreach ( $updates as $theme ) {
			if ( $theme->update ) {
				return false;
			}
		}

		return true;
	}

	private function check_directory_listing() {
		// Check if directory listing is disabled (implementation may vary based on server setup)
		$directory_listing_disabled = true; // Example placeholder

		return $directory_listing_disabled;
	}

	private function check_ssl_certificate() {
		return is_ssl();
	}

	private function check_inactive_plugins() {
		$inactive_plugins = get_plugins();
		foreach ( $inactive_plugins as $plugin => $details ) {
			if ( ! is_plugin_active( $plugin ) ) {
				return false;
			}
		}

		return true;
	}

	private function check_two_factor_auth() {
		// Example placeholder - actual check depends on the 2FA plugin used
		$two_factor_enabled = true;

		return $two_factor_enabled;
	}

	private function check_xml_rpc() {
		return ! ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST );
	}

	private function check_firewall() {
		// Example placeholder - actual check depends on the firewall setup
		$firewall_enabled = true;

		return $firewall_enabled;
	}

	private function check_backup() {
		// Example placeholder - actual check depends on the backup plugin/setup
		$backup_configured = true;

		return $backup_configured;
	}
}
