Files
Lucas/alert_ack.php
Debian 24dbc7cd6a Initial commit — Serveur Lucas SmartEye
API réception alertes chute (SmartEye/YOLO), analyse IA (Gemini 2.5 Flash),
gestion alertes avec escalade (watchdog), notifications Firebase,
dashboard web, documentation MkDocs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-14 21:26:06 +01:00

105 lines
3.0 KiB
PHP

<?php
/**
* SmartEye SENTINEL - Endpoint d'Acquittement d'Alerte
* =====================================================
* Appelé par LucasApp (Flutter) pour signaler qu'une alerte a été vue/traitée.
*
* URL : https://lucas.unigest.fr/alert_ack.php
*
* Méthode : POST
* Body JSON :
* {
* "alert_id": "uuid-de-l-alerte",
* "status": "SEEN" | "ACKNOWLEDGED" | "RESOLVED",
* "detail": "optionnel - texte libre",
* "app_token": "token firebase pour vérification"
* }
*
* Réponse :
* {"success": true, "message": "..."}
* {"success": false, "error": "..."}
*
* Auteur : Unigest Solutions / SmartEye V30
*/
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
// Pré-vol CORS
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
// Vérifier la méthode
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(["success" => false, "error" => "Méthode non autorisée"]);
exit;
}
// Lire le body JSON
$input = json_decode(file_get_contents('php://input'), true);
if (!$input || !isset($input['alert_id']) || !isset($input['status'])) {
http_response_code(400);
echo json_encode(["success" => false, "error" => "Paramètres manquants : alert_id et status requis"]);
exit;
}
$alert_id = trim($input['alert_id']);
$status = strtoupper(trim($input['status']));
$detail = isset($input['detail']) ? trim($input['detail']) : null;
// Valider le statut
$statuts_valides = ['DELIVERED', 'SEEN', 'ACKNOWLEDGED', 'RESOLVED'];
if (!in_array($status, $statuts_valides)) {
http_response_code(400);
echo json_encode([
"success" => false,
"error" => "Statut invalide. Valeurs acceptées : " . implode(', ', $statuts_valides)
]);
exit;
}
// Valider le format de l'alert_id (UUID)
if (!preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i', $alert_id)) {
http_response_code(400);
echo json_encode(["success" => false, "error" => "Format alert_id invalide (UUID attendu)"]);
exit;
}
// Appeler le script Python pour mettre à jour le statut
$python_script = '/var/www/lucas/alert_ack_handler.py';
$cmd_args = escapeshellarg($alert_id) . ' ' . escapeshellarg($status);
if ($detail) {
$cmd_args .= ' ' . escapeshellarg($detail);
}
$command = "/usr/bin/python3 {$python_script} {$cmd_args} 2>&1";
$output = shell_exec($command);
if ($output === null) {
http_response_code(500);
echo json_encode(["success" => false, "error" => "Erreur exécution script Python"]);
exit;
}
// Le script Python retourne du JSON
$result = json_decode(trim($output), true);
if ($result === null) {
http_response_code(500);
echo json_encode([
"success" => false,
"error" => "Réponse Python invalide",
"raw" => substr($output, 0, 200)
]);
exit;
}
http_response_code($result['success'] ? 200 : 404);
echo json_encode($result);