'Token manquant']); exit(); } $token = str_replace('Bearer ', '', $headers['Authorization']); try { return JWT::decode($token, new Key(JWT_SECRET, 'HS256')); } catch (Exception $e) { http_response_code(401); echo json_encode(['message' => 'Token invalide']); exit(); } } verify_token(); $method = $_SERVER['REQUEST_METHOD']; $db = get_db(); // Récupérer l'action demandée $action = $_GET['action'] ?? ''; if ($action === 'users') { // GET — Récupérer tous les utilisateurs if ($method === 'GET') { $result = $db->query("SELECT id, username FROM users"); $data = []; while ($row = $result->fetch_assoc()) { $data[] = $row; } echo json_encode($data); // POST — Ajouter un utilisateur } elseif ($method === 'POST') { $body = json_decode(file_get_contents('php://input'), true); $stmt = $db->prepare("INSERT INTO users (username, password) VALUES (?, ?)"); $stmt->bind_param("ss", $body['username'], $body['password']); $stmt->execute(); http_response_code(201); echo json_encode(['message' => 'Utilisateur ajouté avec succès']); } } elseif ($action === 'thresholds') { // GET — Récupérer les seuils if ($method === 'GET') { $result = $db->query("SELECT * FROM thresholds"); $data = []; while ($row = $result->fetch_assoc()) { $data[] = $row; } echo json_encode($data); // POST — Modifier un seuil } elseif ($method === 'POST') { $body = json_decode(file_get_contents('php://input'), true); $stmt = $db->prepare("INSERT INTO thresholds (sensor, min_value, max_value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE min_value=?, max_value=?"); $stmt->bind_param("sdddd", $body['sensor'], $body['min_value'], $body['max_value'], $body['min_value'], $body['max_value'] ); $stmt->execute(); echo json_encode(['message' => 'Seuil mis à jour avec succès']); } } $db->close(); ?>