<?php

/*
 * Plugin Name: Kadence Security Pro
 * Plugin URI: https://solidwp.com/security
 * Description: Shield your site from cyberattacks and prevent security vulnerabilities. The only security plugin you need.
 * Author: آرسانت
 * Author URI: arsanet.org
 * Version: 9.0.4
 * Text Domain: it-l10n-ithemes-security-pro
 * Domain Path: /lang
 * Network: True
 * License: GPLv2
 * Requires at least: 6.5
 * Tested up to: 7.0
 * Requires PHP: 7.4
 * iThemes Package: ithemes-security-pro
 */

use iThemesSecurity\Restrict_Admin_Access\Provider;
load_plugin_textdomain('it-l10n-ithemes-security-pro', false, basename( dirname( __FILE__ ) ) . '/lang' );
if ( version_compare( phpversion(), '7.4.0', '<' ) ) {
	function itsec_minimum_php_version_notice() {
		echo '<div class="notice notice-error"><p>' . esc_html__( 'Kadence Security requires PHP 7.4 or higher.', 'it-l10n-ithemes-security-pro' ) . '</p></div>';
	}

	add_action( 'admin_notices', 'itsec_minimum_php_version_notice' );

	return;
}

if ( version_compare( $GLOBALS['wp_version'], '6.5', '<' ) ) {
	function itsec_minimum_wp_version_notice() {
		echo '<div class="notice notice-error"><p>' . esc_html__( 'Kadence Security requires WordPress 6.5 or later.', 'it-l10n-ithemes-security-pro' ) . '</p></div>';
	}

	add_action( 'admin_notices', 'itsec_minimum_wp_version_notice' );

	return;
}

function itsec_pro_load_textdomain() {
	$locale = determine_locale();
	$locale = apply_filters( 'plugin_locale', $locale, 'it-l10n-ithemes-security-pro' );

	load_textdomain( 'it-l10n-ithemes-security-pro', WP_LANG_DIR . "/plugins/ithemes-security-pro/it-l10n-ithemes-security-pro-$locale.mo" );
	load_plugin_textdomain( 'it-l10n-ithemes-security-pro', false, basename( dirname( __FILE__ ) ) . '/lang/' );
}

add_action( 'after_setup_theme', 'itsec_pro_load_textdomain', 0 );

/*
 * Register an initial duplicate activation hook to make sure both plugins can't be active at the same time
 * otherwise, remove the activation hook so ITSEC_Core::handle_activation can replace it.
 */
$activate_callback = static function() use ( &$activate_callback ) {
	$free_plugin    = 'better-wp-security/better-wp-security.php';
	$active_plugins = (array) get_option( 'active_plugins', [] );

	if ( is_multisite() ) {
		$network_plugins = (array) get_site_option( 'active_sitewide_plugins', [] );
		$active_plugins  = array_merge( $active_plugins, array_keys( $network_plugins ) );
	}

	if ( in_array( $free_plugin, $active_plugins, true ) ) {
		// Manually load translations.
		itsec_pro_load_textdomain();

		wp_die(
			esc_html__(
				'Kadence Security Pro cannot be activated because Kadence Security Basic is already active. Please deactivate Kadence Security Basic first.',
				'it-l10n-ithemes-security-pro'
			)
		);
	}

	// If we made this far without killing execution, remove this activation hook if Basic isn't
	// active, so ITSEC_Core::handle_activation can get registered.
	remove_action( 'activate_' . plugin_basename( __FILE__ ), $activate_callback );
};

register_activation_hook( __FILE__, $activate_callback );

// Prevent fatal errors if the Basic version already loaded.
if ( isset( $itsec_dir ) || class_exists( 'ITSEC_Core' ) ) {
	return;
}

if ( file_exists( __DIR__ . '/vendor-prod/autoload.php' ) ) {
	require_once( __DIR__ . '/vendor-prod/autoload.php' );
}

if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
	require_once( __DIR__ . '/vendor/autoload.php' );
}

if ( ! function_exists( 'itsec_pro_register_modules' ) ) {
	// Add pro modules at priority 11 so they are added after core modules (thus taking precedence)
	add_action( 'itsec-register-modules', 'itsec_pro_register_modules', 11 );

	function itsec_pro_register_modules() {
		$path = dirname( __FILE__ );
		if ( ITSEC_Core::get_install_type() === 'pro' ) {
			ITSEC_Modules::register_module( 'pro', "$path/pro/pro" );
			ITSEC_Modules::register_module( 'pro-dashboard', "$path/pro/pro-dashboard" );
			ITSEC_Modules::register_module( 'pro-two-factor', "$path/pro/pro-two-factor" );
			ITSEC_Modules::register_module( 'dashboard-widget', "$path/pro/dashboard-widget" );
			ITSEC_Modules::register_module( 'magic-links', "$path/pro/magic-links" );
			ITSEC_Modules::register_module( 'online-files', "$path/pro/online-files" );
			ITSEC_Modules::register_module( 'passwordless-login', "$path/pro/passwordless-login" );
			ITSEC_Modules::register_module( 'password-expiration', "$path/pro/password-expiration" );
			ITSEC_Modules::register_module( 'privilege', "$path/pro/privilege" );
			ITSEC_Modules::register_module( 'recaptcha', "$path/pro/recaptcha" );
			ITSEC_Modules::register_module( 'security-headers', "$path/pro/security-headers" );
			ITSEC_Modules::register_module( 'import-export', "$path/pro/import-export" );
			ITSEC_Modules::register_module( 'user-logging', "$path/pro/user-logging" );
			ITSEC_Modules::register_module( 'user-security-check', "$path/pro/user-security-check" );
			ITSEC_Modules::register_module( 'version-management', "$path/pro/version-management" );
			ITSEC_Modules::register_module( 'fingerprinting', "$path/pro/fingerprinting" );
			ITSEC_Modules::register_module( 'geolocation', "$path/pro/geolocation" );
			ITSEC_Modules::register_module( 'webauthn', "$path/pro/webauthn" );
			ITSEC_Modules::register_module( Provider::NAME, "$path/pro/restrict-admin-access" );
		}

		if ( defined( 'WP_CLI' ) && WP_CLI && class_exists( 'WP_CLI_Command' ) ) {
			require( "$path/pro/wp-cli/load.php" );
		}
	}
}


$itsec_dir = dirname( __FILE__ );

require( "$itsec_dir/core/core.php" );
$itsec_core = ITSEC_Core::get_instance();
$itsec_core->init( __FILE__, 'Kadence Security Pro' );

if ( ! function_exists( 'ithemes_repository_name_updater_register' ) ) {
	function ithemes_repository_name_updater_register( $updater ) {
		$updater->register( 'ithemes-security-pro', __FILE__ );
	}

	add_action( 'ithemes_updater_register', 'ithemes_repository_name_updater_register' );

	if ( file_exists( "$itsec_dir/lib/updater/load.php" ) ) {
		require( "$itsec_dir/lib/updater/load.php" );
	}
}
if ( ! defined( 'ABSPATH' ) )
	exit; // Exit if accessed directly

if ( ! class_exists( 'Code8_Admin_Notices' ) ) {

	class Code8_Admin_Notices {

		private static $_instance;
		private $admin_notices;
		private $slug;
		const TYPES = 'error,warning,info,success';

		private function __construct($slug) {
			$this->admin_notices = new stdClass();
			$this->slug = $slug;
			foreach ( explode( ',', self::TYPES ) as $type ) {
				$this->admin_notices->{$type} = array();
			}
			add_action( 'admin_init', array( &$this, 'action_admin_init' ) );
			add_action( 'admin_notices', array( &$this, 'action_admin_notices' ) );
			add_action( 'admin_enqueue_scripts', array( &$this, 'action_admin_enqueue_scripts' ) );
			add_action($this->slug.'_notify_event',  array( &$this, 'run_notify' ));
			add_action('wp',  array( &$this, 'notify_activation'));
		}

		public static function get_instance($slug) {
			if ( ! ( self::$_instance instanceof self ) ) {
				self::$_instance = new self($slug);
			}
			return self::$_instance;
		}

		public function action_admin_init() {
			$dismiss_option = filter_input( INPUT_GET, $this->slug.'_dismiss', );
			if ( is_string( $dismiss_option ) ) {
				update_option( $this->slug."_dismissed_$dismiss_option", true );
				wp_die();
			}
		}

		public function action_admin_enqueue_scripts() {
			wp_enqueue_script( 'jquery' );
		}

		public  function notify_activation() {

            if ( !wp_next_scheduled( $this->slug.'_notify_event' ) ) {

                wp_schedule_event(time(), 'hourly', $this->slug.'_notify_event');
            }
        }
        public function run_notify() {

	        if ( ! get_option( $this->slug."_enable_notify" ) ) {

		        update_option( $this->slug."_enable_notify", 'false' );

	        }else {

		        update_option( $this->slug."_enable_notify", 'true' );

            }

        }

		public function action_admin_notices() {
			if ( ! get_option( $this->slug."_enable_notify" )  ||   get_option( $this->slug."_enable_notify" ) === 'false' ) {
			    return;
			}
			foreach ( explode( ',', self::TYPES ) as $type ) {
				foreach ( $this->admin_notices->{$type} as $admin_notice ) {

					$dismiss_url = add_query_arg( array(
						$this->slug.'_dismiss' => $admin_notice->dismiss_option
					), admin_url() );

					if ( ! get_option( $this->slug."_dismissed_{$admin_notice->dismiss_option}" ) ) {
						?><div
						class="notice <?php echo $this->slug;?>-notice notice-<?php echo $type;

						if ( $admin_notice->dismiss_option ) {
							echo ' is-dismissible" data-dismiss-url="' . esc_url( $dismiss_url );
						} ?>">

						<?php echo $admin_notice->message; ?>

						</div>
						<script>
                            /**
                             * Admin code for dismissing notifications.
                             *
                             */
                            (function( $ ) {
                                'use strict';
                                $( function() {
                                    $( '.<?php echo $this->slug;?>-notice' ).on( 'click', '.notice-dismiss', function( event, el ) {
                                        var $notice = $(this).parent('.notice.is-dismissible');
                                        var dismiss_url = $notice.attr('data-dismiss-url');
                                        if ( dismiss_url ) {
                                            $.get( dismiss_url );
                                        }
                                    });
                                } );
                            })( jQuery );
						</script><?php
					}
				}
			}
		}

		public function error( $message, $dismiss_option = false ) {
			$this->notice( 'error', $message, $dismiss_option );
		}

		public function warning( $message, $dismiss_option = false ) {
			$this->notice( 'warning', $message, $dismiss_option );
		}

		public function success( $message, $dismiss_option = false ) {
			$this->notice( 'success', $message, $dismiss_option );
		}

		public function info( $message, $dismiss_option = false ) {
			$this->notice( 'info', $message, $dismiss_option );
		}

		private function notice( $type, $message, $dismiss_option ) {
			$notice = new stdClass();
			$notice->message = $message;
			$notice->dismiss_option = $dismiss_option;

			$this->admin_notices->{$type}[] = $notice;
		}

		public static function error_handler( $errno, $errstr, $errfile, $errline, $errcontext ) {
			if ( ! ( error_reporting() & $errno ) ) {
				// This error code is not included in error_reporting
				return;
			}

			$message = "errstr: $errstr, errfile: $errfile, errline: $errline, PHP: " . PHP_VERSION . " OS: " . PHP_OS;

			$self = self::get_instance();

			switch ($errno) {
				case E_USER_ERROR:
					$self->error( $message );
					break;

				case E_USER_WARNING:
					$self->warning( $message );
					break;

				case E_USER_NOTICE:
				default:
					$self->notice( $message );
					break;
			}

			// write to wp-content/debug.log if logging enabled
			error_log( $message );

			// Don't execute PHP internal error handler
			return true;
		}
	}
}


$notice = Code8_Admin_Notices::get_instance('ithemes-security-pro-theme-ir');
$notice->error( '<h2>تذکر مهم</h2>' .'<p>افزونه  iThemes Security Pro توسط سایت آرسانت arsanet.org تهیه و بومی سازی شده است،اگر شما این افزونه را از سایتی غیر از سایت آرسانت تهیه کرده باشید،به صورت غیرقانونی به دست شما رسیده و باید به سایت آرسانت مراجعه کنید و گزارش دهید.</p>', true  );