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' ]); }