<?php
class TokenRevoked
{
    private $conn;
    private $table_name = "revoked_tokens";

    public $id;
    public $token_id;
    public $revoked_at;

    public function __construct($db)
    {
        $this->conn = $db;
    }

    public function revoke($token_id)
    {
        $query = "INSERT INTO " . $this->table_name . " SET token_id=:token_id, revoked_at=:revoked_at";
        $stmt = $this->conn->prepare($query);

        $this->token_id = htmlspecialchars(strip_tags($token_id));
        $this->revoked_at = date('Y-m-d H:i:s');

        $stmt->bindParam(":token_id", $this->token_id);
        $stmt->bindParam(":revoked_at", $this->revoked_at);

        if ($stmt->execute()) {
            return true;
        }
        return false;
    }

    public function isRevoked($token_id)
    {
        $query = "SELECT id FROM " . $this->table_name . " WHERE token_id = ? LIMIT 0,1";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $token_id);
        $stmt->execute();
        if ($stmt->rowCount() > 0) {
            return true;
        }
        return false;
    }
}
?>