? ORDER BY gm.sent_at DESC LIMIT 25"; $stmt = $conn->prepare($sql_group_messages); $stmt->bind_param("s", $last_timestamp); $stmt->execute(); $result_group_messages = $stmt->get_result(); if ($result_group_messages->num_rows > 0) { $messages = []; $latest_timestamp = ''; $sql_users = "SELECT username FROM users"; $user_result = $conn->query($sql_users); $users = []; while ($user = $user_result->fetch_assoc()) { $users[$user['username']] = true; } while ($message = $result_group_messages->fetch_assoc()) { $message_text = htmlspecialchars($message['message']); $highlight_class = ''; preg_match_all('/@(\w+)/', $message['message'], $mentions); $unique_mentions = array_unique($mentions[1]); foreach ($unique_mentions as $mention) { if (isset($users[$mention])) { $message_text = preg_replace('/@' . preg_quote($mention, '/') . '/', "@$mention", $message_text, 1); if ($mention === $current_user) { $highlight_class = 'highlight'; } } } $message_text = preg_replace('/(https?:\/\/[^\s]+)/', '$1', $message_text); $message_text = preg_replace('/\b(www\.[^\s]+)/', '$1', $message_text); $output = "" . htmlspecialchars($message['username']) . ": " . $message_text; if (!empty($message['file_path'])) { $path = '/projects/strife/uploads/'; $full_file_path = $_SERVER['DOCUMENT_ROOT'] . $path . $message['file_path']; if (file_exists($full_file_path)) { $file_type = mime_content_type($full_file_path); if (strpos($file_type, 'image') !== false) { $output .= "
image"; } elseif (strpos($file_type, 'audio') !== false) { $output .= "
"; } elseif (strpos($file_type, 'video') !== false) { $output .= "
"; } else { $output .= "
Unsupported file type: " . htmlspecialchars($file_type); } } else { $output .= "
File does not exist."; } } $output .= " (" . htmlspecialchars($message['sent_at']) . ")
"; $messages[] = $output; if ($latest_timestamp === '' || $message['sent_at'] > $latest_timestamp) { $latest_timestamp = $message['sent_at']; } } header('Content-Type: application/json'); echo json_encode(['messages' => array_reverse($messages), 'latest_timestamp' => $latest_timestamp]); exit; } if (time() - $start_time >= $timeout) { break; } usleep(5000); } header('Content-Type: application/json'); echo json_encode(['messages' => [], 'latest_timestamp' => $last_timestamp]);