Téléverser les fichiers vers "admin"
This commit is contained in:
86
admin/admin_routes.php
Normal file
86
admin/admin_routes.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
require_once '../config.php';
|
||||
require_once '../vendor/autoload.php';
|
||||
|
||||
use Firebase\JWT\JWT;
|
||||
use Firebase\JWT\Key;
|
||||
|
||||
header('Content-Type: application/json');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: GET, POST');
|
||||
header('Access-Control-Allow-Headers: Content-Type, Authorization');
|
||||
|
||||
// Vérification du token JWT
|
||||
function verify_token() {
|
||||
$headers = getallheaders();
|
||||
if (!isset($headers['Authorization'])) {
|
||||
http_response_code(401);
|
||||
echo json_encode(['message' => '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();
|
||||
?>
|
||||
Reference in New Issue
Block a user