| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- // Start session for captcha verification
- if (session_status() === PHP_SESSION_NONE) {
- session_start();
- }
- require_once '../includes/config.php';
- require_once '../includes/database.php';
- require_once '../includes/comment.php';
- require_once '../includes/captcha.php';
- require_once '../includes/translation.php';
- // Translation system is auto-initialized when translation.php is included
- header('Content-Type: application/json');
- $response = ['success' => 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);
|