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lPSJmaWxsOnJnYigxNjcsMTcwLDE3Myk7ZmlsbC1ydWxlOm5vbnplcm87Ii8+Cjwvc3ZnPgo=';

	public $enqueue = true;
	public $cap = 'gd-security-headers-standard';
	public $plugin = 'gd-security-headers';

	/** @var d4p_datetime_core */
	public $datetime;

	private $_ip;
	private $_ua;

	/** @var bool|gdsih_component_csp */
	private $_csp = false;
	/** @var bool|gdsih_component_xxp */
	private $_xxp = false;
	/** @var bool|gdsih_component_headers */
	private $_hdr = false;
	/** @var bool|gdsih_component_feature_policy */
	private $_fep = false;

	public function __construct() {
		parent::__construct();

		if ( ! defined( 'GDSIH_HTACCESS_FILE_NAME' ) ) {
			define( 'GDSIH_HTACCESS_FILE_NAME', '.htaccess' );
		}

		$this->url      = GDSIH_URL;
		$this->datetime = new d4p_datetime_core();
	}

	public function ip() {
		return $this->_ip;
	}

	public function ua() {
		return $this->_ua;
	}

	public function plugins_loaded() {
		parent::plugins_loaded();

		$this->_ip = d4p_visitor_ip();
		$this->_ua = d4p_user_agent();

		define( 'GDSIH_WPV', intval( $this->wp_version ) );
		define( 'GDSIH_WPV_MAJOR', substr( $this->wp_version, 0, 3 ) );
		define( 'GDSIH_WP_VERSION', $this->wp_version_real );

		do_action( 'gdsih_load_settings' );

		do_action( 'gdsih_plugin_init' );

		$this->load();
	}

	public function load() {
		add_action( 'gdsih_saved_the_settings', array( $this, 'htaccess' ) );

		if ( gdsih_settings()->get( 'mode', 'csp' ) != 'disable' ) {
			require_once( GDSIH_PATH . 'core/objects/core.csp.build.php' );
			require_once( GDSIH_PATH . 'core/objects/core.csp.php' );

			$this->_csp = new gdsih_component_csp();
		}

		if ( gdsih_settings()->get( 'x_xss_protection', 'xxp' ) ) {
			require_once( GDSIH_PATH . 'core/objects/core.xxp.php' );

			$this->_xxp = new gdsih_component_xxp();
		}

		if ( gdsih_settings()->get( 'protection', 'feature' ) ) {
			require_once( GDSIH_PATH . 'core/objects/core.feature.build.php' );
			require_once( GDSIH_PATH . 'core/objects/core.feature.php' );

			$this->_fep = new gdsih_component_feature_policy();
		}

		require_once( GDSIH_PATH . 'core/objects/core.headers.php' );

		$this->_hdr = new gdsih_component_headers();
	}

	public function htaccess() {
		require_once( GDSIH_PATH . 'core/objects/core.htaccess.php' );

		$htaccess = new gdsih_core_htaccess();
		$status   = $htaccess->check();

		if ( $status['automatic'] ) {
			gdsih_settings()->set( 'htaccess_available', true, 'core' );

			if ( gdsih_settings()->get( 'htaccess' ) ) {
				gdsih_settings()->set( 'htaccess_added', true, 'core' );

				$htaccess->write();
			} else {
				gdsih_settings()->set( 'htaccess_added', false, 'core' );

				$htaccess->reset();
			}

			gdsih_settings()->save( 'core' );
		}
	}

	public function build_headers_to_array() : array {
		$list = array();

		if ( $this->_csp !== false ) {
			$list['content-security-policy'] = $this->_csp->csp->build( true );
		}

		if ( $this->_xxp !== false ) {
			$list['x-xss-protection'] = $this->_xxp->build( true );
		}

		if ( $this->_fep !== false ) {
			$headers = $this->_fep->fep->build( true );

			if ( ! empty( $headers ) ) {
				foreach ( $headers as $header ) {
					$parts                           = explode( ' ', $header, 2 );
					$list[ strtolower( $parts[0] ) ] = $header;
				}
			}
		}

		$list += $this->_hdr->build( true );

		return $list;
	}
}
