aboutsummaryrefslogtreecommitdiff
path: root/api/debug/agent-replies.php
diff options
context:
space:
mode:
Diffstat (limited to 'api/debug/agent-replies.php')
-rw-r--r--api/debug/agent-replies.php57
1 files changed, 57 insertions, 0 deletions
diff --git a/api/debug/agent-replies.php b/api/debug/agent-replies.php
new file mode 100644
index 0000000..070c0e6
--- /dev/null
+++ b/api/debug/agent-replies.php
@@ -0,0 +1,57 @@
+<?php
+// api/debug/agent-replies.php — outbound half of the maintenance thread.
+// Hermes (or the operator) writes replies here; the in-page agent reads them with
+// `feedback-inbox`. nginx gates this path behind the WebX11 admin session, so it is
+// owner-only: never expose it in the public debug/course location block.
+require_once __DIR__ . '/config.php';
+
+define('REPLIES_DIR', __DIR__ . '/../../data/maintenance');
+define('REPLIES_FILE', REPLIES_DIR . '/replies.json');
+
+if (!is_dir(REPLIES_DIR)) {
+ @mkdir(REPLIES_DIR, 0775, true);
+ @chgrp(REPLIES_DIR, 'www-data');
+}
+
+$method = $_SERVER['REQUEST_METHOD'];
+
+if ($method === 'GET') {
+ $since = $_GET['since'] ?? null;
+ $all = load_json(REPLIES_FILE);
+ if (!is_array($all)) $all = [];
+ if ($since) {
+ $all = array_values(array_filter($all, fn($r) => ($r['created_at'] ?? '') > $since));
+ }
+ json_response(['success' => true, 'replies' => array_slice($all, -100)]);
+}
+
+if ($method !== 'POST') {
+ error_response('Method not allowed', 405);
+}
+
+$raw = file_get_contents('php://input');
+if (!$raw) error_response('Empty request body');
+if (strlen($raw) > 16384) error_response('Payload too large (max 16KB)', 413);
+
+$p = json_decode($raw, true);
+if (!is_array($p)) error_response('Invalid JSON');
+
+$body = trim((string)($p['body'] ?? ''));
+if ($body === '') error_response('body is required');
+if (mb_strlen($body) > 2000) $body = mb_substr($body, 0, 2000);
+
+$list = load_json(REPLIES_FILE);
+if (!is_array($list)) $list = [];
+
+$author = (($p['author'] ?? 'hermes') === 'newos4') ? 'newos4' : 'hermes';
+$entry = [
+ 'id' => 'REP-' . gmdate('Ymd-His') . '-' . substr(bin2hex(random_bytes(3)), 0, 6),
+ 'created_at' => gmdate('c'),
+ 'author' => $author,
+ 'body' => $body,
+];
+
+$list[] = $entry;
+save_json(REPLIES_FILE, array_slice($list, -200));
+
+json_response(['success' => true, 'id' => $entry['id']], 201);