'error', 'message' => 'Method Not Allowed']); exit; } $rawInput = file_get_contents('php://input'); $data = json_decode($rawInput, true) ?: $_POST; if (empty($data)) { http_response_code(400); echo json_encode(['status' => 'error', 'message' => 'Empty request body or invalid JSON']); exit; } try { $config = require __DIR__ . '/../../../../config/config.php'; $db = Db::connect($config['db']); // Optional Token Verification (if provided) $headers = getallheaders(); $token = $headers['X-Agent-Token'] ?? $headers['x-agent-token'] ?? null; if (!$token && !empty($headers['Authorization'])) { if (preg_match('/Bearer\s+(.+)/i', $headers['Authorization'], $matches)) { $token = trim($matches[1]); } } if ($token) { $tokenMgr = new TokenManager($db); $valid = $tokenMgr->validateToken($token, 'bugtracker:report', $data['environment'] ?? null); if (!$valid) { http_response_code(401); echo json_encode(['status' => 'error', 'message' => 'Invalid, revoked or unauthorized Token for bugtracker:report']); exit; } } $repo = new BugRepo($db); $result = $repo->reportItem($data); echo json_encode([ 'status' => 'success', 'item_id' => $result['id'], 'is_new' => $result['is_new'], 'occurrence_count' => $result['occurrence_count'], 'error_hash' => $result['error_hash'], 'type' => $result['type'], 'environment' => $result['environment'], 'push_id' => $result['push_id'] ?? null, 'message' => $result['is_new'] ? ($result['type'] === 'bug' ? 'New bug reported successfully.' : 'New feature request / idea submitted.') : 'Recurring bug count updated.', ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); } catch (Throwable $t) { http_response_code(500); echo json_encode(['status' => 'error', 'message' => 'Failed to log report: ' . $t->getMessage()]); }