83 lines
2.8 KiB
PHP
83 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/../../../../src/Core/Db.php';
|
|
require_once __DIR__ . '/../../../../src/Core/TokenManager.php';
|
|
require_once __DIR__ . '/../../../../src/Modules/Bugtracker/BugRepo.php';
|
|
|
|
use Deploymentcenter\Core\Db;
|
|
use Deploymentcenter\Core\TokenManager;
|
|
use Deploymentcenter\Modules\Bugtracker\BugRepo;
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
// Allow CORS for public ingest
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Agent-Token');
|
|
header('Access-Control-Allow-Methods: POST, OPTIONS');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
|
http_response_code(200);
|
|
exit;
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
echo json_encode(['status' => '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'],
|
|
'message' => $result['is_new']
|
|
? ($result['type'] === 'bug' ? 'New bug reported successfully.' : 'New feature request 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()]);
|
|
}
|