<?php
session_start();

$db = new SQLite3(".ansdb.db");

function getUserIP() {
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        return $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        return $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        return $_SERVER['REMOTE_ADDR'];
    }
}

$user_ip = getUserIP();

$query = $db->prepare("SELECT expire_date FROM ibo WHERE ip_endereco = :ip");
$query->bindValue(':ip', $user_ip, SQLITE3_TEXT);
$result = $query->execute();
$row = $result->fetchArray();

$expire_date_db = $row ? $row['expire_date'] : null;
$response = ['success' => false];

if ($expire_date_db) {
    $response = [
        'success' => true,
        'expire_date' => $expire_date_db
    ];
}

echo json_encode($response);
