Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/portal/api/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/portal/api/chat_fetch.php

<?php
require_once '../config/config.php';

header('Content-Type: application/json');

$conversationId = (int)($_GET['conversation_id'] ?? 0);
$lastMessageId = (int)($_GET['last_message_id'] ?? 0);

if ($conversationId <= 0) {
    echo json_encode(['success' => false, 'error' => 'Invalid conversation']);
    exit;
}

try {
    $db = Database::getInstance()->getConnection();

    $sql = "SELECT id, sender_type, message_text, created_at
            FROM chat_messages
            WHERE conversation_id = :conversation_id";

    $params = ['conversation_id' => $conversationId];

    if ($lastMessageId > 0) {
        $sql .= " AND id > :last_id";
        $params['last_id'] = $lastMessageId;
    }

    $sql .= " ORDER BY id ASC LIMIT 100";

    $stmt = $db->prepare($sql);
    $stmt->execute($params);
    $messages = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode([
        'success' => true,
        'messages' => $messages
    ]);
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['success' => false, 'error' => 'Failed to fetch messages.']);
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists