2025-05-16 20:02:49 -04:00
|
|
|
<?php
|
|
|
|
|
// Disable error display in the output
|
|
|
|
|
ini_set('display_errors', 0);
|
|
|
|
|
error_reporting(E_ALL);
|
|
|
|
|
|
2026-01-20 09:55:01 -05:00
|
|
|
// Apply rate limiting
|
|
|
|
|
require_once dirname(__DIR__) . '/middleware/RateLimitMiddleware.php';
|
|
|
|
|
RateLimitMiddleware::apply('api');
|
|
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Start output buffering to capture any errors
|
|
|
|
|
ob_start();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Include required files with proper error handling
|
|
|
|
|
$configPath = dirname(__DIR__) . '/config/config.php';
|
|
|
|
|
$commentModelPath = dirname(__DIR__) . '/models/CommentModel.php';
|
2026-01-01 15:40:32 -05:00
|
|
|
$auditLogModelPath = dirname(__DIR__) . '/models/AuditLogModel.php';
|
|
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
if (!file_exists($configPath)) {
|
|
|
|
|
throw new Exception("Config file not found: $configPath");
|
|
|
|
|
}
|
2026-01-01 15:40:32 -05:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
if (!file_exists($commentModelPath)) {
|
|
|
|
|
throw new Exception("CommentModel file not found: $commentModelPath");
|
|
|
|
|
}
|
2026-01-01 15:40:32 -05:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
require_once $configPath;
|
|
|
|
|
require_once $commentModelPath;
|
2026-01-01 15:40:32 -05:00
|
|
|
require_once $auditLogModelPath;
|
2026-01-30 14:39:13 -05:00
|
|
|
require_once dirname(__DIR__) . '/helpers/Database.php';
|
2026-03-20 21:39:02 -04:00
|
|
|
require_once dirname(__DIR__) . '/models/TicketModel.php';
|
2026-03-29 21:34:16 -04:00
|
|
|
require_once dirname(__DIR__) . '/helpers/NotificationHelper.php';
|
|
|
|
|
require_once dirname(__DIR__) . '/helpers/SynapseHelper.php';
|
2026-01-01 15:40:32 -05:00
|
|
|
|
|
|
|
|
// Check authentication via session
|
2026-03-17 23:22:24 -04:00
|
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
|
|
|
session_start();
|
|
|
|
|
}
|
2026-01-01 15:40:32 -05:00
|
|
|
if (!isset($_SESSION['user']) || !isset($_SESSION['user']['user_id'])) {
|
|
|
|
|
throw new Exception("Authentication required");
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-09 12:32:34 -05:00
|
|
|
// CSRF Protection
|
|
|
|
|
require_once dirname(__DIR__) . '/middleware/CsrfMiddleware.php';
|
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
|
|
$csrfToken = $_SERVER['HTTP_X_CSRF_TOKEN'] ?? '';
|
|
|
|
|
if (!CsrfMiddleware::validateToken($csrfToken)) {
|
|
|
|
|
http_response_code(403);
|
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
|
echo json_encode(['success' => false, 'error' => 'Invalid CSRF token']);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-01 15:40:32 -05:00
|
|
|
$currentUser = $_SESSION['user'];
|
|
|
|
|
$userId = $currentUser['user_id'];
|
|
|
|
|
|
2026-01-30 14:39:13 -05:00
|
|
|
// Use centralized database connection
|
|
|
|
|
$conn = Database::getConnection();
|
2026-01-01 15:40:32 -05:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Get POST data
|
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
2026-01-01 15:40:32 -05:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
if (!$data) {
|
|
|
|
|
throw new Exception("Invalid JSON data received");
|
|
|
|
|
}
|
2026-01-01 15:40:32 -05:00
|
|
|
|
2026-04-11 12:43:18 -04:00
|
|
|
$ticketId = isset($data['ticket_id']) ? trim((string)$data['ticket_id']) : '';
|
|
|
|
|
if (!ctype_digit($ticketId) || (int)$ticketId <= 0) {
|
2026-03-19 22:20:43 -04:00
|
|
|
http_response_code(400);
|
|
|
|
|
ob_end_clean();
|
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
|
echo json_encode(['success' => false, 'error' => 'Invalid ticket ID']);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
2026-01-01 15:40:32 -05:00
|
|
|
|
2026-03-20 21:39:02 -04:00
|
|
|
// Verify user can access the ticket before allowing a comment
|
|
|
|
|
$ticketModel = new TicketModel($conn);
|
|
|
|
|
$ticket = $ticketModel->getTicketById($ticketId);
|
|
|
|
|
if (!$ticket) {
|
|
|
|
|
http_response_code(404);
|
|
|
|
|
ob_end_clean();
|
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
|
echo json_encode(['success' => false, 'error' => 'Ticket not found']);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
if (!$ticketModel->canUserAccessTicket($ticket, $currentUser)) {
|
|
|
|
|
http_response_code(403);
|
|
|
|
|
ob_end_clean();
|
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
|
echo json_encode(['success' => false, 'error' => 'Access denied']);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-01 15:40:32 -05:00
|
|
|
// Initialize models
|
2025-05-16 20:02:49 -04:00
|
|
|
$commentModel = new CommentModel($conn);
|
2026-01-01 15:40:32 -05:00
|
|
|
$auditLog = new AuditLogModel($conn);
|
|
|
|
|
|
2026-01-20 09:55:01 -05:00
|
|
|
// Extract @mentions from comment text
|
|
|
|
|
$mentions = $commentModel->extractMentions($data['comment_text'] ?? '');
|
|
|
|
|
$mentionedUsers = [];
|
|
|
|
|
if (!empty($mentions)) {
|
|
|
|
|
$mentionedUsers = $commentModel->getMentionedUsers($mentions);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-01 15:40:32 -05:00
|
|
|
// Add comment with user tracking
|
|
|
|
|
$result = $commentModel->addComment($ticketId, $data, $userId);
|
|
|
|
|
|
|
|
|
|
// Log comment creation to audit log
|
|
|
|
|
if ($result['success'] && isset($result['comment_id'])) {
|
|
|
|
|
$auditLog->logCommentCreate($userId, $result['comment_id'], $ticketId);
|
2026-01-20 09:55:01 -05:00
|
|
|
|
|
|
|
|
// Log mentions to audit log
|
|
|
|
|
foreach ($mentionedUsers as $mentionedUser) {
|
|
|
|
|
$auditLog->log(
|
|
|
|
|
$userId,
|
|
|
|
|
'mention',
|
|
|
|
|
'user',
|
|
|
|
|
(string)$mentionedUser['user_id'],
|
|
|
|
|
[
|
|
|
|
|
'ticket_id' => $ticketId,
|
|
|
|
|
'comment_id' => $result['comment_id'],
|
|
|
|
|
'mentioned_username' => $mentionedUser['username']
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 21:34:16 -04:00
|
|
|
// Matrix notifications
|
|
|
|
|
$authorDisplay = $currentUser['display_name'] ?? $currentUser['username'] ?? null;
|
|
|
|
|
$commentText = $data['comment_text'] ?? '';
|
|
|
|
|
$ticketTitle = $ticket['title'] ?? "Ticket #{$ticketId}";
|
|
|
|
|
|
|
|
|
|
// @mention notifications — resolve usernames → Matrix IDs via Synapse Admin API
|
|
|
|
|
if (!empty($mentionedUsers)) {
|
|
|
|
|
$mentionedUsernames = array_column($mentionedUsers, 'username');
|
|
|
|
|
$mentionedMatrixIds = SynapseHelper::resolveUsernames($mentionedUsernames);
|
|
|
|
|
if (!empty($mentionedMatrixIds)) {
|
|
|
|
|
NotificationHelper::sendMentionNotification($ticketId, $ticketTitle, $commentText, $authorDisplay, $mentionedMatrixIds);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// General comment notification (opt-in via MATRIX_NOTIFY_COMMENTS)
|
|
|
|
|
if (!empty($GLOBALS['config']['MATRIX_NOTIFY_COMMENTS'])) {
|
|
|
|
|
NotificationHelper::sendCommentNotification($ticketId, $ticketTitle, $commentText, $authorDisplay);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 22:00:32 -04:00
|
|
|
// Notify watchers of the new comment
|
|
|
|
|
NotificationHelper::notifyWatchers(
|
|
|
|
|
$conn, $ticketId, $ticketTitle, 'comment_added',
|
|
|
|
|
['author' => $authorDisplay, 'preview' => mb_strimwidth($commentText, 0, 200, '…')],
|
|
|
|
|
(int)$userId
|
|
|
|
|
);
|
|
|
|
|
|
2026-01-20 09:55:01 -05:00
|
|
|
// Add mentioned users to result for frontend
|
|
|
|
|
$result['mentions'] = array_map(function($u) {
|
|
|
|
|
return $u['username'];
|
|
|
|
|
}, $mentionedUsers);
|
2026-01-01 15:40:32 -05:00
|
|
|
}
|
|
|
|
|
|
2026-04-06 22:37:53 -04:00
|
|
|
// Add user info to result for frontend avatar rendering
|
2026-01-01 16:14:56 -05:00
|
|
|
if ($result['success']) {
|
|
|
|
|
$result['user_name'] = $currentUser['display_name'] ?? $currentUser['username'];
|
2026-04-06 22:37:53 -04:00
|
|
|
$result['user_id'] = $userId;
|
2026-01-01 16:14:56 -05:00
|
|
|
}
|
|
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Discard any unexpected output
|
|
|
|
|
ob_end_clean();
|
2026-01-01 15:40:32 -05:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Return JSON response
|
2026-03-29 18:23:16 -04:00
|
|
|
if ($result['success']) {
|
|
|
|
|
http_response_code(201);
|
|
|
|
|
}
|
2025-05-16 20:02:49 -04:00
|
|
|
header('Content-Type: application/json');
|
|
|
|
|
echo json_encode($result);
|
2026-03-29 18:23:16 -04:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
} catch (Exception $e) {
|
|
|
|
|
// Discard any unexpected output
|
|
|
|
|
ob_end_clean();
|
2026-01-30 18:56:29 -05:00
|
|
|
|
|
|
|
|
// Log error details but don't expose to client
|
|
|
|
|
error_log("Add comment API error: " . $e->getMessage());
|
|
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Return error response
|
2026-03-29 18:23:16 -04:00
|
|
|
http_response_code(500);
|
2025-05-16 20:02:49 -04:00
|
|
|
header('Content-Type: application/json');
|
|
|
|
|
echo json_encode([
|
|
|
|
|
'success' => false,
|
2026-01-30 18:56:29 -05:00
|
|
|
'error' => 'An internal error occurred'
|
2025-05-16 20:02:49 -04:00
|
|
|
]);
|
|
|
|
|
}
|