\n"; $code .= "\tRewriteEngine On\n\n"; if( self::$is_cloudflare_enabled ) { $code .= "\t# Cloudflare compatibility\n"; // Use multiple RewriteCond lines for better reliability foreach( $block_specific_countries as $index => $country ) { $or_flag = ( $index < count( $block_specific_countries ) - 1 ) ? ' [OR]' : ''; $code .= "\tRewriteCond %{HTTP:CF-IPCountry} ^{$country}$" . $or_flag . "\n"; } } $code .= "\tRewriteRule ^(.*)$ - [F,L]\n"; $code .= "\n"; } if( !empty( $block_specific_countries ) && self::$is_geoip_enabled ) { $block_specific_countries = implode( '|', $block_specific_countries ); $code .= "\n\n"; $code .= "\tGeoIPEnable On\n"; $code .= "\tSetEnvIf GEOIP_COUNTRY_CODE ^($block_specific_countries)$ BlockCountry\n"; $code .= "\tOrder Allow,Deny\n"; $code .= "\tAllow from all\n"; $code .= "\tDeny from env=BlockCountry\n"; $code .= "\n"; } $code = apply_filters( 'adminease_block_specific_countries_code', $code ); Plugin::$FileHandler->stack_htaccess_rule( 'BLOCK_SPECIFIC_COUNTRIES', $code ); } /** * Checks if the request is coming through Cloudflare. * @return bool Returns true if Cloudflare headers are present, otherwise false. */ public static function is_cloudflare_enabled(): bool { return !empty( $_SERVER['HTTP_CF_IPCOUNTRY'] ) || !empty( $_SERVER['HTTP_CF_IPCountry'] ) || !empty( $_SERVER['HTTP:CF-IPCountry'] ) || !empty( $_SERVER['HTTP_CF_CONNECTING_IP'] ) || !empty( $_SERVER['HTTP_CF_RAY'] ); } /** * Checks if GeoIP functionality is enabled on the server. * @return bool Returns true if GeoIP is enabled, otherwise false. */ public static function is_geoip_enabled(): bool { return extension_loaded( 'geoip' ) || function_exists( 'geoip_country_code_by_name' ) || isset( $_SERVER['GEOIP_COUNTRY_CODE'] ); } }