55 lines
1.6 KiB
PHP
55 lines
1.6 KiB
PHP
<?php
|
|
include 'koneksi.php';
|
|
header('Content-Type: application/json');
|
|
|
|
$token = isset($_GET['token']) ? trim($_GET['token']) : '';
|
|
|
|
if (empty($token)) {
|
|
echo json_encode(['valid' => false, 'message' => 'Token tidak valid']);
|
|
exit;
|
|
}
|
|
|
|
$manifestPath = __DIR__ . '/manifest_pin_tokens.json';
|
|
if (!file_exists($manifestPath)) {
|
|
echo json_encode(['valid' => false, 'message' => 'Data token tidak ditemukan']);
|
|
exit;
|
|
}
|
|
|
|
$json = file_get_contents($manifestPath);
|
|
$tokens = json_decode($json, true) ?: [];
|
|
|
|
if (!isset($tokens[$token])) {
|
|
echo json_encode(['valid' => false, 'message' => 'Token tidak ditemukan atau tidak valid']);
|
|
exit;
|
|
}
|
|
|
|
$tokenData = $tokens[$token];
|
|
|
|
if (time() > $tokenData['expires_at']) {
|
|
// Hapus token yang sudah expired
|
|
unset($tokens[$token]);
|
|
file_put_contents($manifestPath, json_encode($tokens, JSON_PRETTY_PRINT));
|
|
echo json_encode(['valid' => false, 'message' => 'Token sudah kadaluwarsa. Silakan generate ulang QR code.']);
|
|
exit;
|
|
}
|
|
|
|
// Token valid. Hapus setelah digunakan (sekali pakai saja)
|
|
$id_occupant = $tokenData['id_occupant'];
|
|
unset($tokens[$token]);
|
|
file_put_contents($manifestPath, json_encode($tokens, JSON_PRETTY_PRINT));
|
|
|
|
// Ambil detail occupant terkait
|
|
$stmt = $pdo->prepare("SELECT name FROM occupant WHERE id_occupant = ?");
|
|
$stmt->execute([$id_occupant]);
|
|
$row = $stmt->fetch();
|
|
|
|
if ($row) {
|
|
echo json_encode([
|
|
'valid' => true,
|
|
'id_occupant' => $id_occupant,
|
|
'name' => $row['name']
|
|
]);
|
|
} else {
|
|
echo json_encode(['valid' => false, 'message' => 'Data occupant tidak ditemukan.']);
|
|
}
|