<?php
// Include the GeoIP2 PHP library
require_once '/home/prosentry/geoip/vendor/autoload.php'; // Adjust path if needed
use GeoIp2\Database\Reader;

// Path to the GeoLite2 database 
$databaseFile = '/home/prosentry/geoip/country-db/GeoLite2-Country.mmdb';

// Check if the database file exists
if (!file_exists($databaseFile)) {
    die('GeoLite2 database not found.');
}

try {
    $ip = $_SERVER['HTTP_CF_CONNECTING_IP'] ?? $_SERVER['X-Forwarded-For'] ?? $_SERVER['REMOTE_ADDR'];
    
    // Define the target URL for logging
    $target_url = "/key-holding-alarm-response/";

    // Log user agent if the requested URL matches
    if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], $target_url) !== false) {
        $logFile = '/home/prosentry/logs/user_agents.log'; // Change to your desired log file location
        $logData = date("Y-m-d H:i:s") . " | " . $_SERVER['HTTP_USER_AGENT'] . " | " . $ip . "\n";

        // Append log to file
        file_put_contents($logFile, $logData, FILE_APPEND | LOCK_EX);
    }
    
    if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/Googlebot|AdsBot-Google|Mediapartners-Google|APIs-Google|Google Favicon|Google Web Preview/', $_SERVER['HTTP_USER_AGENT'])) {
        return; // Allow all Google crawlers
    }

    $reader = new Reader($databaseFile);

    $record = $reader->country($ip);

    if ($record->country->isoCode !== 'GB') {
        // Deny access to non-UK users
        header('HTTP/1.0 403 Forbidden');
        echo "Access denied. This website is available only in the UK.";
        exit;
    }
} catch (Exception $e) {

}

