Files
html/api/blade-task-queue.php
2026-04-16 03:09:54 +02:00

53 lines
1.9 KiB
PHP

<?php
// Blade Task Queue API — polled by sentinel-agent.ps1 every 60s
header("Content-Type: application/json");
$k = $_GET["k"] ?? $_POST["k"] ?? "";
if ($k !== "BLADE2026") { echo json_encode(["error"=>"auth"]); exit; }
$action = $_GET["action"] ?? $_POST["action"] ?? "poll";
$queue_dir = "/var/www/html/api/blade-tasks/";
@mkdir($queue_dir, 0777, true);
switch ($action) {
case "poll":
// Return pending tasks
$tasks = [];
foreach (glob($queue_dir . "*.json") as $f) {
$t = json_decode(file_get_contents($f), true);
if (($t["status"] ?? "") === "pending") {
$tasks[] = $t;
}
}
echo json_encode(["tasks" => $tasks, "count" => count($tasks)]);
break;
case "complete":
$id = $_POST["task_id"] ?? "";
$result = $_POST["result"] ?? "";
$f = $queue_dir . $id . ".json";
if (file_exists($f)) {
$t = json_decode(file_get_contents($f), true);
$t["status"] = "completed";
$t["result"] = $result;
$t["completed_at"] = date("c");
file_put_contents($f, json_encode($t, JSON_PRETTY_PRINT));
echo json_encode(["ok" => true]);
}
break;
case "create":
$id = "task_" . date("YmdHis") . "_" . substr(md5(rand()), 0, 6);
$task = [
"id" => $id,
"goal" => $_POST["goal"] ?? "",
"type" => $_POST["type"] ?? "powershell",
"commands" => json_decode($_POST["commands"] ?? "[]", true) ?: [],
"priority" => $_POST["priority"] ?? "normal",
"status" => "pending",
"created_at" => date("c")
];
file_put_contents($queue_dir . $id . ".json", json_encode($task, JSON_PRETTY_PRINT));
echo json_encode(["ok" => true, "task_id" => $id]);
break;
}