<?php

$offername = $_GET['offer'];

// 1. Prevent redirect loop: If already ending with /[lang] (like /offer/test/fr)
if (preg_match('#/([a-z]{2})(/)?(\?.*)?$#', $_SERVER['REQUEST_URI'])) {
    return;
}

// 2. Get user IP address
$ip = $_SERVER['REMOTE_ADDR'];

// 3. Use your IPStack API key
$apiKey = '061a864446c4fc350c3a20c2b0405122';
$apiUrl = "http://api.ipstack.com/{$ip}?access_key={$apiKey}&fields=country_code";

// 4. Fetch location data
$response = @file_get_contents($apiUrl);
$data = @json_decode($response, true);

// 5. Country-to-language mapping
$countryToLang = [
    'BE' => 'be',
    'BR' => 'br',
    'DE' => 'de',
    'ES' => 'es',
    'FR' => 'fr',
    'IT' => 'it',
    'MX' => 'mx',
    'NL' => 'nl',
    'PL' => 'pl',
    'PT' => 'pt',
    'RO' => 'ro',
    'TR' => 'tr',
    'CZ' => 'cz',
    // add more as needed
];

$lang = (isset($data['country_code']) && isset($countryToLang[$data['country_code']]))
    ? $countryToLang[$data['country_code']]
    : null;

if ($lang) {
    // Get the current path without query
    $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

    // Remove trailing slash for consistency
    $path = rtrim($path, '/');

    // Build the new path with /[lang] at the end
    $newPath = $path . '/' . $lang;

    // Preserve query parameters
    $params = $_SERVER['QUERY_STRING'] ? '?' . $_SERVER['QUERY_STRING'] : '';

    // Redirect
    header("Location: {$newPath}{$params}", true, 302);
    exit;
}
?>
<!-- Your homepage HTML starts here -->

<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <style class="vjs-styles-defaults">
      .video-js {
        width: 300px;
        height: 150px;
      }

      .vjs-fluid {
        padding-top: 56.25%
      }
    </style>
    
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <link rel="stylesheet" href="./index_files/forms2.css">
        <link rel="stylesheet" href="./index_files/flow.css">    
        <link rel="stylesheet" href="./index_files/funnel2.css">
        <link rel="shortcut icon" href="https://static-133.b-cdn.net/65291/images/rVnsWlthPpD-i.png" type="image/x-icon">


    
    <title><?php echo $offername; ?></title>
        </head>

<body>
    
<div class="bitcode-method">

    <section class="warning">
        <div class="container">
            <p class="text-center warning_text p-2 text-sm">ATTENTION: Very High Media Demand, Registration Closing in <span id="counter"> 09:24</span></p>
        </div>
    </section>

    <section class="intro">
                <div class="intro-logo-name">
            <img src="./index_files/nBrsSRgPt-onl.png" alt="Logo">
            <div><?php echo $offername; ?></div>
        </div>
        
                <img class="intro_logo intro_logo_tagesschau" src="./index_files/nBrsSRgPt-onl.png" alt="logo" style="display:none; !important;">
            </section>

    <section class="main">
        <div class="container">
            <div class="row">
                <div class="col-md-6">
                    <div class="main_wrapper">
                        <h5 class="main_title">Create your Free Account </h5>
                        <ul class="main_list">
                            <li>Free Personal Account Manager </li>
                            <li>No Trading Experience Needed</li>
                            <li>Get Started in 15 minutes</li>
                        </ul>
                        <img class="main_img" src="./index_files/forbes-verified.png" alt="Forbes">
                        <div class="box"></div>

                    </div>
                </div>
                <div class="col-md-6">
                   
                   <div><?php include 'form.php'; ?></div>
                </div>
            </div>
        </div>
    </section>
    </div>

    
    


</body></html>