<?php
/*
 * Developed By Devozon
 *
 * Check Out at https://devozon.com/
 * Email: support@devozon.com
 * Message: https://www.facebook.com/devozon
 *
 * Please feel free to contact me at any time if you need a custom script or any development work.
 *
 * Devozon Team:
 * Coded by Sanad Nabil
 * Theme by Ahmed Reda
 * Tested By Smith Carter
 *
 * Devozon All rights reserved
 * Copyright (c) 2023-2025.
 * Last Update 22 \ 6 \ 2025
 */

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

class FENSE_BPVT_Cache_Safe_Mode
{
	public array $LOADER = [ 'SaveMode', 'GetSaveMode' ];
	public string $SaveMode = FENSE_BPVT_PLUGIN_KEY . 'SaveMode';
	public string $GetSaveMode = FENSE_BPVT_PLUGIN_KEY . 'GetSaveMode';
	
	public function __construct()
	{
		foreach ( $this->LOADER as $mode ) {
			/**  Main Api */
			add_action( 'wp_ajax_nopriv_' . FENSE_BPVT_PLUGIN_KEY . $mode, [ $this, $mode ] );
			add_action( 'wp_ajax_' . FENSE_BPVT_PLUGIN_KEY . $mode, [ $this, $mode ] );
		}
	}
	
	public function SaveMode()
	{
		if ( !$this->__isAction() ) $this->error();
		if ( !$this->__isOrigin() ) $this->error();
		
		if ( FENSE_BPVT_FUN::runOnce() ) return $this->success();
		
		if ( !$this->__isSecret() ) $this->error();
		if ( !$this->__isNonce( $_COOKIE[ $this->SaveMode ] ?? '' ) ) $this->error();
		
		/**  Load Settings Class */
		$SaveMode = new FENSE_BPVT_RUN_SAVE_MODE();
		
		/**  Disable on Logged in Or admin */
		if ( $SaveMode->__isAdminOrLoggedIn() ) return $this->success();
		
		/**  Disable on Daily Limit Reached */
		if ( $SaveMode->__isDailyLimitReached() ) return $this->success();
		
		/** Get Plugin License Key */
		if ( empty( $SaveMode->loadAppKey() ) ) return $this->success();
		
		/** Get Visitor IP */
		if ( !$SaveMode->loadUserIp() ) return $this->success();
		
		/** Exit if Plugin is Disabled */
		if ( !$SaveMode->loadSettings()->enable ) return $this->success();
		
		/** Handle cache is exists */
		if ( $SaveMode->isCache()['stop'] ) return $this->success( $SaveMode->__country()['isProxy'] );
		
		/** Handle IP Whitelisting, Blacklisting Settings */
		if ( $SaveMode->__ipHandleWhitelistBlacklist() ) return $this->success();
		
		/** Handle Bot */
		if ( $SaveMode->__isCrawlersBot() ) return $this->success();
		
		/** Handle Plugin Page settings */
		if ( $SaveMode->__enableOn() ) return $this->success();
		
		/** Get IP Data From Fense */
		$data = $SaveMode->loadData();
		
		/** Handle Plugin Blocked Domains Settings */
		if ( $SaveMode->__blockReferrerDomain() ) return $this->success( true );
		
		/**  Exit if there was an error in API */
		if ( !empty( $data->status ) && in_array( $data->status, [ 'Error', 'error' ] ) ) return $this->success();
		
		/** Handle Country Filters */
		if ( $SaveMode->__country()['stop'] ) return $this->success( $SaveMode->__country()['isProxy'] );
		
		$res = $SaveMode->__protection_mode();
		
		/** Protection Code */
		return $this->success( $res['status'], $res );
	}
	
	public function GetSaveMode()
	{
		
		if ( !$this->__isOrigin() ) $this->error();
		
		if ( empty( $_COOKIE[ $this->GetSaveMode ] ) ) {
			$token = wp_generate_password( 32, false ); // random string
			setcookie(
				$this->GetSaveMode,
				$token,
				time() + HOUR_IN_SECONDS,
				COOKIEPATH,
				COOKIE_DOMAIN,
				is_ssl(),
				true // HttpOnly
			);
			set_transient( 'bpvt_sm_' . md5( $token ), $token, HOUR_IN_SECONDS );
		}
		
		if ( empty( $_COOKIE[ $this->SaveMode ] ) ) {
			setcookie(
				$this->SaveMode,
				wp_create_nonce( 'SaveMode-nonce' ),
				time() + HOUR_IN_SECONDS,
				COOKIEPATH,
				COOKIE_DOMAIN,
				is_ssl(),
				true // HttpOnly
			);
		}
		
		wp_send_json_success( [ 'status' => true ] );
	}
	
	public function success( $isProxy = false, $data = [] ): bool
	{
		wp_send_json_success( [ 'status' => $isProxy, ...$data ] );
		return true;
	}
	
	public function error( array $msg = [], $code = 404 )
	{
		wp_send_json_error( [ 'status' => false, ...$msg ], $code );
	}
	
	public function __isOrigin(): bool
	{
		$allowed_origin = parse_url( home_url(), PHP_URL_HOST );
		if ( empty( $_SERVER['HTTP_ORIGIN'] ) || parse_url( $_SERVER['HTTP_ORIGIN'], PHP_URL_HOST ) !== $allowed_origin ) {
			return false;
		}
		
		return true;
	}
	
	public function __isSecret(): bool
	{
		$secret = $_COOKIE[ $this->GetSaveMode ] ?? '';
		
		$transKey = 'bpvt_sm_' . md5( $secret );
		
		$trans = get_transient( $transKey ) ?? null;
		
		if ( $secret !== $trans ) {
			setcookie(
				$this->GetSaveMode,
				null,
				time() + HOUR_IN_SECONDS,
				COOKIEPATH,
				COOKIE_DOMAIN,
				is_ssl(),
				true // HttpOnly
			);
			return false;
		}
		
		return true;
	}
	
	public function __isNonce( $nonce ): bool
	{
		if ( !wp_verify_nonce( $nonce, 'SaveMode-nonce' ) ) {
			return false;
		}
		
		return true;
	}
	
	public function __isAction(): bool
	{
		// Check action exists
		if ( empty( $_REQUEST['action'] ) || $_REQUEST['action'] !== $this->SaveMode ) {
			return false;
		}
		
		return true;
	}
}

new FENSE_BPVT_Cache_Safe_Mode();


