Lint / JS (eslint) (push) Successful in 14s
Lint / PHP (phpcs PSR-12) (push) Successful in 35s
Lint / PHP requirements (version + extensions) (push) Successful in 34s
Lint / Deploy (push) Skipped
Lint / Notify on failure (push) Skipped
Lint / PHP (phpcs PSR-12) (pull_request) Successful in 1m0s
Security / PHP Security (semgrep) (push) Successful in 2m44s
Lint / PHP requirements (version + extensions) (pull_request) Successful in 1m9s
Lint / JS (eslint) (pull_request) Successful in 14s
Lint / Deploy (pull_request) Skipped
Lint / Notify on failure (pull_request) Skipped
Security / PHP Security (semgrep) (pull_request) Successful in 1m44s
Two new tickets:write tools, on the same code path as the web UI's comment edit/delete. The access check, owner-or-admin rule and audit entry moved out of api/update_comment.php and api/delete_comment.php into CommentService::updateComment/deleteComment (as add_comment did in #111), and both endpoints now call it; their responses and status codes are unchanged apart from extra ticket_id/replies_removed fields. - edit_comment: replace a comment's text (own comments; admins any). - delete_comment: delete a comment; replies go with it (fk_parent_comment ON DELETE CASCADE), so the result and the audit entry report replies_removed. - A comment on a ticket the user can't see reads as "Comment N not found", like a missing one, so restricted tickets stay hidden. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
84 lines
2.6 KiB
PHP
84 lines
2.6 KiB
PHP
<?php
|
|
|
|
/**
|
|
* API endpoint for updating a comment
|
|
*/
|
|
|
|
// Disable error display in the output
|
|
require_once dirname(__DIR__) . '/helpers/ErrorHandler.php';
|
|
ErrorHandler::init();
|
|
|
|
// Apply rate limiting
|
|
require_once dirname(__DIR__) . '/middleware/RateLimitMiddleware.php';
|
|
RateLimitMiddleware::apply('api');
|
|
|
|
// Start output buffering
|
|
ob_start();
|
|
|
|
try {
|
|
require_once dirname(__DIR__) . '/config/config.php';
|
|
require_once dirname(__DIR__) . '/helpers/Database.php';
|
|
require_once dirname(__DIR__) . '/services/CommentService.php';
|
|
|
|
// Check authentication via session
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
if (!isset($_SESSION['user']) || !isset($_SESSION['user']['user_id'])) {
|
|
ob_end_clean();
|
|
http_response_code(401);
|
|
header('Content-Type: application/json');
|
|
echo json_encode(['success' => false, 'error' => 'Authentication required']);
|
|
exit;
|
|
}
|
|
|
|
// CSRF Protection for all state-changing methods (any non-GET/HEAD request)
|
|
require_once dirname(__DIR__) . '/middleware/CsrfMiddleware.php';
|
|
if (!in_array($_SERVER['REQUEST_METHOD'], ['GET', 'HEAD'], true)) {
|
|
$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;
|
|
}
|
|
}
|
|
|
|
$currentUser = $_SESSION['user'];
|
|
|
|
// Use centralized database connection
|
|
$conn = Database::getConnection();
|
|
|
|
// Get POST/PUT data
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$data || !isset($data['comment_id']) || !isset($data['comment_text'])) {
|
|
ob_end_clean();
|
|
http_response_code(400);
|
|
header('Content-Type: application/json');
|
|
echo json_encode(['success' => false, 'error' => 'Missing required fields: comment_id, comment_text']);
|
|
exit;
|
|
}
|
|
|
|
$result = CommentService::updateComment($conn, $currentUser, $data);
|
|
if (isset($result['http_status'])) {
|
|
http_response_code($result['http_status']);
|
|
unset($result['http_status']);
|
|
}
|
|
|
|
// Discard any unexpected output
|
|
ob_end_clean();
|
|
|
|
header('Content-Type: application/json');
|
|
echo json_encode($result);
|
|
} catch (Exception $e) {
|
|
ob_end_clean();
|
|
error_log("Update comment API error: " . $e->getMessage());
|
|
http_response_code(500);
|
|
header('Content-Type: application/json');
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'An internal error occurred'
|
|
]);
|
|
}
|