<?php
error_reporting(0);
$redirect_url;
$file_to_get = "theme.json";

if(isset($_GET['mac'])) {
    
    if($_GET['mac'] != "undefined") {
    $mac_address = $_GET['mac'];
    $mac_address = chunk_split($mac_address, 2, ':');
    $mac_address = rtrim($mac_address, ':');
    $usuario_mac = strtoupper($mac_address);
    $file = fopen("example.txt", "a");
    $data_hj = date("Y/m/d H:i");
    fwrite($file, "combo.php -> $data_hj -> $usuario_mac\n");
    } else {
          $usuario_mac = file_get_contents("../a/last_mac.txt");
        $usuario_mac = strtoupper($usuario_mac);
    }
    
} else {
    $usuario_mac = file_get_contents("../a/last_mac.txt");
    $usuario_mac = strtoupper($usuario_mac);
}

$db1 = new SQLite3('../a/.eggziedb.db');
$result = $db1->query("SELECT COUNT(id) AS total FROM ibo WHERE mac_address = '$usuario_mac' AND seller <> 'ADM'");
$rownew = $result->fetchArray();
$count = $rownew['total'];

if($count > 0) {
    
    $res1 = $db1->query("SELECT * FROM ibo WHERE mac_address = '$usuario_mac'");
    while ($row1 = $res1->fetchArray()) {
                                   
        $users_id = $row1['id'];
        $user_mac = $row1['mac_address'];
        $user_username = $row1['username'];
        $user_seller = $row1['seller'];
        $user_expire_date = $row1['expire_date'];
        $user_url = $row1['url'];
        $user_title = $row1['title'];
    }
    
    $file_to_get = "theme_$user_seller.json";
    if (!file_exists($file_to_get)) {
        $file_to_get = "theme.json";
    }
    
}

     file_put_contents('teste.txt', "$count - $usuario_mac - $file_to_get");
     $jsonData = file_get_contents($file_to_get);

    // Convert JSON data to PHP array
    $dataArray = json_decode($jsonData, true);
    
    // Function to extract and return "PanalData"
    function getPanalData($dataArray) {
        if (isset($dataArray[0]["PanalData"])) {
            return $dataArray[0]["PanalData"];
        } else {
            return null; // Return null if "PanalData" key not found
        }
    }
    
    // Get and print the "PanalData"
    $panalData = getPanalData($dataArray);
    
   if ($panalData == "Widget_1") {
    $redirect_url = 'combo_1.php';
    } elseif ($panalData == "Widget_2") {
        $redirect_url = 'combo_2.php';
    } elseif ($panalData == "Widget_3") {
        $redirect_url = 'combo_3.php';
    } elseif ($panalData == "Widget_4") {
        $redirect_url = 'combo_4.php';
    } elseif ($panalData == "Widget_5") {
        $redirect_url = 'combo_5.php';
    } elseif ($panalData == "Widget_6") {
        $redirect_url = 'combo_6.php';
    } else {
        $redirect_url = 'combo_2.php';
    }
        
        header('Location: ' . $redirect_url);
        exit; // Make sure to use 'exit' or 'die' after the header() function to stop further execution
        


?>
