Files
piclock/www/log_swipe.php
T
jpd a201d5d7ac Update 34 files
- /nfc/README.md
- /nfc/clean_old_logs.sh
- /nfc/clockin.py
- /nfc/emptycards.py
- /nfc/sync_clocks.sh
- /piservices/Dockerfile
- /piservices/Portainer-lamp-stack.env
- /piservices/Portainer-lamp-stack.yaml
- /piservices/README.md
- /piservices/avahi-alias.service
- /piservices/clockpy.service
- /piservices/emptycards.service
- /piservices/nfc-sync.service
- /piservices/test.sh
- /www/404
- /www/get-table-data.php
- /www/habiwan copy.png
- /www/habiwan.png
- /www/index.php
- /www/log_swipe.php
- /www/manage_data.php
- /www/manifest.json
- /www/names.csv
- /www/network_audit.csv
- /www/network-tracker.html
- /www/nfc_debug.log
- /www/scanner.html
- /www/staff_names.php
- /www/times.csv
- /www/check_update.php
- /www/dev.php
- /www/favicon.png
- /www/JsBarcode.all.min.js
- /www/README.md
2026-07-24 14:37:45 +01:00

87 lines
3.3 KiB
PHP

<?php
date_default_timezone_set('Europe/London');
$log_file = 'nfc_debug.log';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$raw_input = file_get_contents('php://input');
$data = json_decode($raw_input, true);
$raw_uid = $data['uid'] ?? null;
if (!$raw_uid) { die("No UID"); }
// Normalize UID
$clean_uid = "";
foreach (explode(':', $raw_uid) as $part) { $clean_uid .= dechex(hexdec($part)); }
// --- SERVER-SIDE DEBOUNCE LOGIC ---
$debounce_file = sys_get_temp_dir() . '/nfc_swipe_debounce_' . $clean_uid . '.txt';
$debounce_seconds = 60;
$current_time = time();
if (file_exists($debounce_file)) {
$last_time = (int)file_get_contents($debounce_file);
if (($current_time - $last_time) < $debounce_seconds) {
// Return a safe JSON response so the front-end doesn't crash if this catches a glitch
echo json_encode(['status' => 'ignored', 'name' => 'Already Logged', 'time' => date('H:i:s')]);
exit;
}
}
file_put_contents($debounce_file, $current_time);
// ----------------------------------
try {
$db = new PDO("mysql:host=db;dbname=PiClock", "root", "rootpassword");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// --- AUTOMATIC TABLE CREATION ---
$db->exec("CREATE TABLE IF NOT EXISTS names (
id INT AUTO_INCREMENT PRIMARY KEY,
CardID VARCHAR(12) NOT NULL UNIQUE,
name VARCHAR(100) DEFAULT NULL
)");
$db->exec("CREATE TABLE IF NOT EXISTS times (
UID VARCHAR(12) NOT NULL,
temp FLOAT,
timestamp DATETIME
)");
// --------------------------------
// 1. Check if the card already exists in the names table
$stmt = $db->prepare("SELECT name FROM names WHERE CardID = ?");
$stmt->execute([$clean_uid]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user) {
// Card is known, grab the existing name
$name = $user['name'];
} else {
// Card is UNKNOWN. Auto-register it.
$maxStmt = $db->query("SELECT MAX(id) FROM names");
$max_id = $maxStmt->fetchColumn();
// If the table is empty, start at 1, otherwise use the highest + 1
$next_id = ($max_id !== null) ? ($max_id + 1) : 1;
// Format the name to zz_UNASSIGNED-XX
$name = sprintf("zz_UNASSIGNED-%02d", $next_id);
// Insert the new card into the database
$insertStmt = $db->prepare("INSERT INTO names (CardID, name) VALUES (?, ?)");
$insertStmt->execute([$clean_uid, $name]);
}
// 2. Log the swipe in the times table
$stmt = $db->prepare("INSERT INTO times (UID, temp, timestamp) VALUES (?, 56.0, ?)");
$stmt->execute([$clean_uid, date('Y-m-d H:i:s')]);
// 3. Send the result back to the front-end
header('Content-Type: application/json');
echo json_encode(['status' => 'success', 'name' => $name, 'time' => date('H:i:s')]);
} catch (PDOException $e) {
file_put_contents($log_file, "DB ERROR: " . $e->getMessage() . PHP_EOL, FILE_APPEND);
http_response_code(500);
}
}
?>