false, 'message' => '']; // Handle captcha refresh if ($_GET['action'] === 'refresh_captcha') { $response['success'] = true; $response['question'] = Captcha::getAjaxCaptcha()['question']; echo json_encode($response); exit; } try { // Only accept POST requests if ($_SERVER['REQUEST_METHOD'] !== 'POST') { throw new Exception('Invalid request method'); } // Get and validate input $publicationId = (int)($_POST['publication_id'] ?? 0); $parent_id = (int)($_POST['parent_id'] ?? 0); $name = trim($_POST['name'] ?? ''); $email = trim($_POST['email'] ?? ''); $content = trim($_POST['content'] ?? ''); $captchaAnswer = $_POST['captcha_answer'] ?? ''; // Verify captcha if (!Captcha::verify($captchaAnswer)) { throw new Exception(t('captcha_invalid')); } // Prepare comment data $commentData = [ 'publication_id' => $publicationId, 'parent_id' => $parent_id ?: null, 'name' => $name, 'email' => $email ?: null, 'content' => $content ]; // Create comment instance $comment = new Comment(); // Validate comment data $errors = $comment->validate($commentData, false); if (!empty($errors)) { throw new Exception(implode(', ', $errors)); } // Create comment if ($comment->create($commentData)) { $response['success'] = true; $response['message'] = t('comment_submitted_success'); $response['comment_count'] = $comment->getCountByPublication($publicationId); } else { throw new Exception(t('comment_submit_error')); } } catch (Exception $e) { $response['message'] = $e->getMessage(); // Log error for debugging error_log('Comment submission error: ' . $e->getMessage()); } echo json_encode($response);