<?php
error_reporting(0);

$file_to_get = "rtx/image_filenames.json";
if(isset($_GET['mac'])) {
    
    $mac = $_GET['mac'];
    $mac_address = chunk_split($mac_address, 2, ':');
    $mac_address = rtrim($mac_address, ':');
    $usuario_mac = strtoupper($usuario_mac);
    
} else {
    $usuario_mac = file_get_contents("last_mac.txt");
    $usuario_mac = strtoupper($usuario_mac);
}
$db1 = new SQLite3('.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 = "rtx/image_filenames_$user_seller.json";
    if (!file_exists($file_to_get)) {
        $file_to_get = "rtx/image_filenames.json";
    }
    
}

    file_put_contents('teste.txt', "$count - $usuario_mac - $file_to_get");
    // Read the JSON file contents
    $jsonData = file_get_contents($file_to_get);
    // Decode the JSON data
    $imageData = json_decode($jsonData, true);
    // Extract the filename
    $filename = $imageData[0]['ImageName'];
    // Get the current path
    $parsedUrl = parse_url($_SERVER['REQUEST_URI']);
    $currentPath = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']) . '/';
    $imageFile = "$filename";
    // Set the appropriate Content-Type header for the image
    header("Content-Type: image/jpeg");
    // Read the image file and output it to the browser
    //readfile($imageFile);
    header('Location: '.$imageFile);
    exit;


?>
