diff --git a/index.php b/index.php index eb3aff6..e2665fa 100644 --- a/index.php +++ b/index.php @@ -27,13 +27,20 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (string)($_POST['action'] ?? '') == || str_contains((string)($_SERVER['HTTP_ACCEPT'] ?? ''), 'application/json'); $commentSaved = false; + $savedComment = null; $errorMessage = ''; + $errorCode = 422; if ($token !== '' && $photoId > 0 && $text !== '') { $u = commenterByToken($token); if ($u) { - commentAdd($photoId, (int)$u['id'], limitText($text, 1000)); - $commentSaved = true; + try { + $savedComment = commentAdd($photoId, (int)$u['id'], limitText($text, 1000)); + $commentSaved = true; + } catch (Throwable) { + $errorMessage = 'Не удалось отправить комментарий.'; + $errorCode = 500; + } } else { $errorMessage = 'Ссылка для комментариев недействительна.'; } @@ -44,11 +51,20 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (string)($_POST['action'] ?? '') == if ($isAjax) { header('Content-Type: application/json; charset=utf-8'); if ($commentSaved) { - echo json_encode(['ok' => true, 'message' => 'Ваш комментарий отправлен.'], JSON_UNESCAPED_UNICODE); + echo json_encode([ + 'ok' => true, + 'message' => 'Ваш комментарий отправлен.', + 'comment' => $savedComment ? [ + 'id' => (int)($savedComment['id'] ?? 0), + 'comment_text' => (string)($savedComment['comment_text'] ?? ''), + 'created_at' => (string)($savedComment['created_at'] ?? ''), + 'display_name' => (string)($savedComment['display_name'] ?? 'Пользователь'), + ] : null, + ], JSON_UNESCAPED_UNICODE); exit; } - http_response_code(422); + http_response_code($errorCode); echo json_encode(['ok' => false, 'message' => $errorMessage !== '' ? $errorMessage : 'Не удалось отправить комментарий.'], JSON_UNESCAPED_UNICODE); exit; } @@ -688,9 +704,11 @@ function outputWatermarked(string $path, string $mime): never
Комментарии может оставлять только пользователь с персональной ссылкой.
- -
= nl2br(h((string)$c['comment_text'])) ?>