<?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 17 \ 6 \ 2025
 */

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

class FENSE_BPVT_FUN
{
	private static bool $hasRun = false;
	
	public function IsKeyValid(): bool
	{
		
		$transient = get_transient( 'bpvt_is_api_key_valid' );
		
		if ( !empty( $transient['status'] ) && $transient['status'] === 'success' ) {
			return true;
		}
		
		$apkey = get_option( 'FENSE_BPVT_DEVOZON_licence_key' );
		
		if ( empty( $apkey ) ) {
			return false;
		}
		
		$cache_time = (int)get_option( 'TOTAL_GUARD_DEVOZON_enable_cache' );
		
		$result = wp_remote_retrieve_body( wp_remote_get( sprintf( esc_url( FENSE_BPVT_MAIN_LINK ) . 'wp/%s', $apkey ) ) );
		$api = json_decode( $result );
		
		if ( $api->status === 'success' ) {
			
			set_transient( 'bpvt_is_api_key_valid', [ 'status' => 'success' ], $cache_time * 3600 );
			
			return true;
		}
		
		return false;
	}
	
	public function IsDailyLimitReached()
	{
		$apkey = get_option( 'FENSE_BPVT_DEVOZON_licence_key' );
		
		if ( empty( $apkey ) ) return false;
		
		$result = wp_remote_retrieve_body( wp_remote_get( sprintf( esc_url( FENSE_BPVT_MAIN_LINK ) . 'wp/%s', $apkey ) ) );
		$api = json_decode( $result );
		
		if ( empty( $api ) || $api->status == 'Error' ) return false;
		
		if ( $api->DailyLimitReached ) return true;
		
		return false;
	}
	
	public function Fense_data( $ip, $key )
	{
		
		$transient = get_transient( 'bpvt_ip_' . md5( $key . '_' . $ip ) );
		$expires = is_array( $transient ) && isset( $transient['time'] ) ? (int)$transient['time'] : 0;
		
		if ( time() >= $expires ) {
			$result = wp_remote_retrieve_body( wp_remote_get( sprintf( esc_url( FENSE_BPVT_API_LINK ) . '%s/%s', $key, $ip ) ) );
			$result = json_decode( $result );
			
			$cache_time = (int)get_option( 'TOTAL_GUARD_DEVOZON_enable_cache' );
			
			$expireTime = ( $cache_time == 0 || !$cache_time ) ? 1 : $cache_time;
			
			if ( $result->status == 'success' && $result->proxy == 'no' ) {
				
				$rData = [ 'time' => time() + 1800, 'data' => $result ];
				
				set_transient( 'bpvt_ip_' . md5( $key . '_' . $ip ), $rData, $expireTime * 3600 );
			}
			
		} else {
			return $transient['data'];
		}
		
		if ( !empty( $result ) ) {
			return $result;
		}
		
		$stdClassObject = new stdClass();
		$stdClassObject->status = 'error';
		
		return $stdClassObject;
	}
	
	public function timeAgo( $timestamp )
	{
		$datetime1 = new DateTime( "now" );
		$datetime2 = date_create( $timestamp );
		$diff = date_diff( $datetime1, $datetime2 );
		$timemsg = '';
		if ( $diff->y > 0 ) {
			$timemsg = $diff->y . ' ' . esc_html__( 'year', 'fense-block-vpn-proxy' ) . ( $diff->y > 1 ? esc_html__( "'s", 'fense-block-vpn-proxy' ) : '' );
			
		} else if ( $diff->m > 0 ) {
			$timemsg = esc_html__( ".", 'fense-block-vpn-proxy' ) . $diff->m . ' ' . ( $diff->m > 1 ? esc_html__( "months", 'fense-block-vpn-proxy' ) : esc_html__( 'month', 'fense-block-vpn-proxy' ) );
		} else if ( $diff->d > 0 ) {
			$timemsg = esc_html__( ".", 'fense-block-vpn-proxy' ) . $diff->d . ' ' . ( $diff->d > 1 ? esc_html__( "days", 'fense-block-vpn-proxy' ) : esc_html__( 'day', 'fense-block-vpn-proxy' ) );
		} else if ( $diff->h > 0 ) {
			$timemsg = esc_html__( ".", 'fense-block-vpn-proxy' ) . $diff->h . ' ' . ( $diff->h > 1 ? esc_html__( "hours", 'fense-block-vpn-proxy' ) : esc_html__( 'hour', 'fense-block-vpn-proxy' ) );
		} else if ( $diff->i > 0 ) {
			$timemsg = esc_html__( ".", 'fense-block-vpn-proxy' ) . $diff->i . ' ' . ( $diff->i > 1 ? esc_html__( "minutes", 'fense-block-vpn-proxy' ) : esc_html__( 'minute', 'fense-block-vpn-proxy' ) );
		} else if ( $diff->s > 0 ) {
			$timemsg = esc_html__( ".", 'fense-block-vpn-proxy' ) . $diff->s . ' ' . ( $diff->s > 1 ? esc_html__( "seconds", 'fense-block-vpn-proxy' ) : esc_html__( 'second', 'fense-block-vpn-proxy' ) );
		}
		
		$timemsg = $timemsg . ' ' . esc_html__( 'ago', 'fense-block-vpn-proxy' );
		return $timemsg;
	}
	
	public function stats_ip_type( $type )
	{
		
		switch ( $type ) {
			case 0:
				$data = '<span class="ms clean">Clean</span>';
				break;
			case 4:
			case 1:
				$data = '<span class="ms proxy">Proxy</span>';
				break;
			case 2:
				$data = '<span class="ms vpn">Vpn</span>';
				break;
			case 3:
				$data = '<span class="ms tor">Tor</span>';
				break;
			default:
				$data = '<span class="ms clean">Clean</span>';
		}
		
		return $data;
	}
	
	public function visit_format( $number )
	{
		
		if ( $number >= 1000000 ) {
			
			$num = $number / 1000000;
			$num = round( $num, 0 );
			
			return $num . "M";
			
		} elseif ( $number >= 1000 ) {
			
			$num = $number / 1000;
			$num = round( $num, 0 );
			
			return $num . "K";
		} else {
			return $number;
		}
	}
	
	public function banned_multi_domains_list( $data )
	{
		$domains = explode( ',', $data );
		$domains = array_map( 'trim', $domains );
		$domains = array_filter( $domains );
		$domains = array_unique( $domains );
		$domains = array_combine( $domains, $domains );
		
		return $domains;
	}
	
	public function get_country_by_code( $code )
	{
		
		$countries_list = [
			"AF" => "Afghanistan",
			"AX" => "Aland Islands",
			"AL" => "Albania",
			"DZ" => "Algeria",
			"AS" => "American Samoa",
			"AD" => "Andorra",
			"AO" => "Angola",
			"AI" => "Anguilla",
			"AQ" => "Antarctica",
			"AG" => "Antigua and Barbuda",
			"AR" => "Argentina",
			"AM" => "Armenia",
			"AW" => "Aruba",
			"AU" => "Australia",
			"AT" => "Austria",
			"AZ" => "Azerbaijan",
			"BS" => "Bahamas",
			"BH" => "Bahrain",
			"BD" => "Bangladesh",
			"BB" => "Barbados",
			"BY" => "Belarus",
			"BE" => "Belgium",
			"BZ" => "Belize",
			"BJ" => "Benin",
			"BM" => "Bermuda",
			"BT" => "Bhutan",
			"BO" => "Bolivia",
			"BQ" => "Bonaire, Sint Eustatius and Saba",
			"BA" => "Bosnia and Herzegovina",
			"BW" => "Botswana",
			"BV" => "Bouvet Island",
			"BR" => "Brazil",
			"IO" => "British Indian Ocean Territory",
			"BN" => "Brunei Darussalam",
			"BG" => "Bulgaria",
			"BF" => "Burkina Faso",
			"BI" => "Burundi",
			"KH" => "Cambodia",
			"CM" => "Cameroon",
			"CA" => "Canada",
			"CV" => "Cape Verde",
			"KY" => "Cayman Islands",
			"CF" => "Central African Republic",
			"TD" => "Chad",
			"CL" => "Chile",
			"CN" => "China",
			"CX" => "Christmas Island",
			"CC" => "Cocos (Keeling) Islands",
			"CO" => "Colombia",
			"KM" => "Comoros",
			"CG" => "Congo",
			"CD" => "Congo, the Democratic Republic of the",
			"CK" => "Cook Islands",
			"CR" => "Costa Rica",
			"CI" => "Cote D'Ivoire",
			"HR" => "Croatia",
			"CU" => "Cuba",
			"CW" => "Curacao",
			"CY" => "Cyprus",
			"CZ" => "Czech Republic",
			"DK" => "Denmark",
			"DJ" => "Djibouti",
			"DM" => "Dominica",
			"DO" => "Dominican Republic",
			"EC" => "Ecuador",
			"EG" => "Egypt",
			"SV" => "El Salvador",
			"GQ" => "Equatorial Guinea",
			"ER" => "Eritrea",
			"EE" => "Estonia",
			"ET" => "Ethiopia",
			"FK" => "Falkland Islands (Malvinas)",
			"FO" => "Faroe Islands",
			"FJ" => "Fiji",
			"FI" => "Finland",
			"FR" => "France",
			"GF" => "French Guiana",
			"PF" => "French Polynesia",
			"TF" => "French Southern Territories",
			"GA" => "Gabon",
			"GM" => "Gambia",
			"GE" => "Georgia",
			"DE" => "Germany",
			"GH" => "Ghana",
			"GI" => "Gibraltar",
			"GR" => "Greece",
			"GL" => "Greenland",
			"GD" => "Grenada",
			"GP" => "Guadeloupe",
			"GU" => "Guam",
			"GT" => "Guatemala",
			"GG" => "Guernsey",
			"GN" => "Guinea",
			"GW" => "Guinea-Bissau",
			"GY" => "Guyana",
			"HT" => "Haiti",
			"HM" => "Heard Island and Mcdonald Islands",
			"VA" => "Holy See (Vatican City State)",
			"HN" => "Honduras",
			"HK" => "Hong Kong",
			"HU" => "Hungary",
			"IS" => "Iceland",
			"IN" => "India",
			"ID" => "Indonesia",
			"IR" => "Iran, Islamic Republic of",
			"IQ" => "Iraq",
			"IE" => "Ireland",
			"IM" => "Isle of Man",
			"IL" => "Israel",
			"IT" => "Italy",
			"JM" => "Jamaica",
			"JP" => "Japan",
			"JE" => "Jersey",
			"JO" => "Jordan",
			"KZ" => "Kazakhstan",
			"KE" => "Kenya",
			"KI" => "Kiribati",
			"KP" => "Korea, Democratic People's Republic of",
			"KR" => "Korea, Republic of",
			"XK" => "Kosovo",
			"KW" => "Kuwait",
			"KG" => "Kyrgyzstan",
			"LA" => "Lao People's Democratic Republic",
			"LV" => "Latvia",
			"LB" => "Lebanon",
			"LS" => "Lesotho",
			"LR" => "Liberia",
			"LY" => "Libyan Arab Jamahiriya",
			"LI" => "Liechtenstein",
			"LT" => "Lithuania",
			"LU" => "Luxembourg",
			"MO" => "Macao",
			"MK" => "Macedonia, the Former Yugoslav Republic of",
			"MG" => "Madagascar",
			"MW" => "Malawi",
			"MY" => "Malaysia",
			"MV" => "Maldives",
			"ML" => "Mali",
			"MT" => "Malta",
			"MH" => "Marshall Islands",
			"MQ" => "Martinique",
			"MR" => "Mauritania",
			"MU" => "Mauritius",
			"YT" => "Mayotte",
			"MX" => "Mexico",
			"FM" => "Micronesia, Federated States of",
			"MD" => "Moldova, Republic of",
			"MC" => "Monaco",
			"MN" => "Mongolia",
			"ME" => "Montenegro",
			"MS" => "Montserrat",
			"MA" => "Morocco",
			"MZ" => "Mozambique",
			"MM" => "Myanmar",
			"NA" => "Namibia",
			"NR" => "Nauru",
			"NP" => "Nepal",
			"NL" => "Netherlands",
			"AN" => "Netherlands Antilles",
			"NC" => "New Caledonia",
			"NZ" => "New Zealand",
			"NI" => "Nicaragua",
			"NE" => "Niger",
			"NG" => "Nigeria",
			"NU" => "Niue",
			"NF" => "Norfolk Island",
			"MP" => "Northern Mariana Islands",
			"NO" => "Norway",
			"OM" => "Oman",
			"PK" => "Pakistan",
			"PW" => "Palau",
			"PS" => "Palestinian Territory, Occupied",
			"PA" => "Panama",
			"PG" => "Papua New Guinea",
			"PY" => "Paraguay",
			"PE" => "Peru",
			"PH" => "Philippines",
			"PN" => "Pitcairn",
			"PL" => "Poland",
			"PT" => "Portugal",
			"PR" => "Puerto Rico",
			"QA" => "Qatar",
			"RE" => "Reunion",
			"RO" => "Romania",
			"RU" => "Russian Federation",
			"RW" => "Rwanda",
			"BL" => "Saint Barthelemy",
			"SH" => "Saint Helena",
			"KN" => "Saint Kitts and Nevis",
			"LC" => "Saint Lucia",
			"MF" => "Saint Martin",
			"PM" => "Saint Pierre and Miquelon",
			"VC" => "Saint Vincent and the Grenadines",
			"WS" => "Samoa",
			"SM" => "San Marino",
			"ST" => "Sao Tome and Principe",
			"SA" => "Saudi Arabia",
			"SN" => "Senegal",
			"RS" => "Serbia",
			"CS" => "Serbia and Montenegro",
			"SC" => "Seychelles",
			"SL" => "Sierra Leone",
			"SG" => "Singapore",
			"SX" => "Sint Maarten",
			"SK" => "Slovakia",
			"SI" => "Slovenia",
			"SB" => "Solomon Islands",
			"SO" => "Somalia",
			"ZA" => "South Africa",
			"GS" => "South Georgia and the South Sandwich Islands",
			"SS" => "South Sudan",
			"ES" => "Spain",
			"LK" => "Sri Lanka",
			"SD" => "Sudan",
			"SR" => "Suriname",
			"SJ" => "Svalbard and Jan Mayen",
			"SZ" => "Swaziland",
			"SE" => "Sweden",
			"CH" => "Switzerland",
			"SY" => "Syrian Arab Republic",
			"TW" => "Taiwan, Province of China",
			"TJ" => "Tajikistan",
			"TZ" => "Tanzania, United Republic of",
			"TH" => "Thailand",
			"TL" => "Timor-Leste",
			"TG" => "Togo",
			"TK" => "Tokelau",
			"TO" => "Tonga",
			"TT" => "Trinidad and Tobago",
			"TN" => "Tunisia",
			"TR" => "Turkey",
			"TM" => "Turkmenistan",
			"TC" => "Turks and Caicos Islands",
			"TV" => "Tuvalu",
			"UG" => "Uganda",
			"UA" => "Ukraine",
			"AE" => "United Arab Emirates",
			"GB" => "United Kingdom",
			"US" => "United States",
			"UM" => "United States Minor Outlying Islands",
			"UY" => "Uruguay",
			"UZ" => "Uzbekistan",
			"VU" => "Vanuatu",
			"VE" => "Venezuela",
			"VN" => "Viet Nam",
			"VG" => "Virgin Islands, British",
			"VI" => "Virgin Islands, U.s.",
			"WF" => "Wallis and Futuna",
			"EH" => "Western Sahara",
			"YE" => "Yemen",
			"ZM" => "Zambia",
			"ZW" => "Zimbabwe",
		];
		
		if ( empty( $countries_list[ $code ] ) ) {
			return 'Unknown';
		}
		
		return $countries_list[ $code ];
	}
	
	public function get_the_user_ip()
	{
		
		if ( !empty( $_SERVER["HTTP_CF_CONNECTING_IP"] ) ) {
			$ip = sanitize_text_field( $_SERVER["HTTP_CF_CONNECTING_IP"] );
		} elseif ( !empty( $_SERVER["HTTP_FASTLY_CLIENT_IP"] ) ) {
			$ip = sanitize_text_field( $_SERVER["HTTP_FASTLY_CLIENT_IP"] );
		} elseif ( !empty( $_SERVER["HTTP_CLIENT_IP"] ) ) {
			$ip = sanitize_text_field( $_SERVER["HTTP_CLIENT_IP"] );
		} else {
			$ip = sanitize_text_field( $_SERVER["REMOTE_ADDR"] );
		}
		
		return esc_html( $ip );
	}
	
	public function get_main_template( $lang, $message, $disable_link, $link, $mode, $url )
	{
		if ( $mode == 'sanad' ) {
			return null;
		}
		
		if ( empty( $message ) ) {
			if ( $lang == 'Arabic' ) {
				$message = 'تم اكتشاف انك تستخدم بروكسي او في بي ان . برجاء التعطيل للوصول لهذا الموقع!';
				$how = 'تعلم كيف يمكنك تعطيلة';
			} else {
				$message = 'Proxy or VPN detected - Please disable to access this website!';
				$how = 'Learn How to disable';
			}
		} else {
			$how = 'Learn How to disable';
		}
		
		if ( !empty( $link ) && $disable_link == 'yes' ) {
			$mlink = '<a href="' . esc_url( $link ) . '">' . esc_html( $how ) . '</a>';
		} else {
			$mlink = '';
		}
		
		if ( !defined( 'DONOTCACHEPAGE' ) ) {
			define( 'DONOTCACHEPAGE', true );
		}
		
		wp_die( '<p>' . $message . ' ' . $mlink . '</p>', $message, [ 'back_link' => true, 'response' => 403 ] );
	}
	
	public function loadLang(): string
	{
		return '<span id="fense-bpvt-settings-page"
				  data-s1="' . esc_html__( 'Settings error.', 'fense-block-vpn-proxy' ) . '"
				  data-s2="' . esc_html__( 'saving', 'fense-block-vpn-proxy' ) . '"
				  data-s3="' . esc_url( admin_url( '/options.php' ) ) . '"
				  data-s4="' . esc_html__( 'Save Changes', 'fense-block-vpn-proxy' ) . '"
				  data-s5="' . esc_html__( 'Settings saved.', 'fense-block-vpn-proxy' ) . '"
				  data-s6="' . esc_url( admin_url( 'admin-ajax.php' ) ) . '"
				></span>';
	}
	
	public static function runOnce(): bool
	{
		if ( self::$hasRun ) {
			return true;
		}
		self::$hasRun = true;
		
		return false;
	}
	
	public static function cleanUP(): bool
	{
		$transient = get_transient( 'fense_bpvt_clean_up' );
		
		if ( $transient ) return false;
		
		/** Clean Up and optimize */
		if ( function_exists( 'delete_expired_transients' ) ) {
			// true -> force DB cleanup (needed when object cache is in use)
			delete_expired_transients( true );
		}
		
		set_transient( 'fense_bpvt_clean_up', 'done', 3600 );
		
		return true;
	}
	
}
