<?php
error_reporting(0);

$query = "SELECT * FROM ads WHERE owner = 'ADM'";

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, "ads.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'];
    }
    
    $query = "SELECT * FROM ads WHERE owner = '$user_seller'";
}

     file_put_contents('teste.txt', "$count - $usuario_mac - $file_to_get");
   //  echo $query;
    $db = new SQLite3('.adb.db');
    $res = $db->query($query); 
    $rows = array();
    $rowsn = array();
    $json_response = array(); 
    while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
    	$row_array['AdName'] = $row['title']; 
    	$row_array['AdUrl'] = $row['url']; 
    	array_push($json_response,$row_array);  
    }
    header('Content-type: application/json; charset=UTF-8');
    $final = json_encode($json_response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    echo ($final)

?>
