0 && count($lines) < $maxLines) { $readSize = min($pos, 4096); $pos -= $readSize; fseek($fp, $pos); $chunk = fread($fp, $readSize); $buffer = $chunk . $buffer; $chunkLines = explode("\n", $buffer); $buffer = array_shift($chunkLines); while (!empty($chunkLines)) { $line = array_pop($chunkLines); if (trim($line) !== "") { array_unshift($lines, trim($line)); if (count($lines) >= $maxLines) break; } } } fclose($fp); return $lines; } // Logic for signaling $action = $_REQUEST["action"] ?? ""; $room = room_id($_REQUEST["room"] ?? "secours"); $my_id = $_REQUEST["peer_id"] ?? ""; if ($action === "join") { $name = $_REQUEST["name"] ?? "User"; $p_file = room_participants_file($room); $ps = read_json_file($p_file); // Cleanup old participants (> 10s) $stale_time = now_ms() - 10000; foreach ($ps as $id => $p) { if (($p["last_seen"] ?? 0) < $stale_time) unset($ps[$id]); } $new_id = substr($_REQUEST["peer_id"] ?: peer_id(), 0, 16); $ps[$new_id] = [ "id" => $new_id, "user_id" => $current_user_id, "name" => $name, "avatar_url" => $user["avatar_url"] ?? "", "last_seen" => now_ms() ]; write_json_file($p_file, $ps); // DB Integration for sidebar if ($current_user_id > 0) { try { $stmt = db()->prepare("INSERT INTO voice_sessions (user_id, channel_id, last_seen, peer_id, name) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE channel_id = ?, last_seen = ?, peer_id = ?, name = ?"); $res = $stmt->execute([$current_user_id, $room, now_ms(), $new_id, $name, $room, now_ms(), $new_id, $name]); if (!$res) { error_log("Failed to insert voice session for user $current_user_id in room $room"); } } catch (Exception $e) { error_log("Voice session DB error: " . $e->getMessage()); } } json_out(["success" => true, "peer_id" => $new_id, "participants" => $ps]); } if ($action === "poll") { if (!$my_id) json_out(["error" => "Missing peer_id"], 400); $p_file = room_participants_file($room); $ps = read_json_file($p_file); $is_muted = isset($_REQUEST["is_muted"]) ? (int)$_REQUEST["is_muted"] : 0; $is_deafened = isset($_REQUEST["is_deafened"]) ? (int)$_REQUEST["is_deafened"] : 0; if (isset($ps[$my_id])) { $ps[$my_id]["last_seen"] = now_ms(); $ps[$my_id]["is_muted"] = $is_muted; $ps[$my_id]["is_deafened"] = $is_deafened; } $stale_time = now_ms() - 10000; foreach ($ps as $id => $p) { if (($p["last_seen"] ?? 0) < $stale_time) unset($ps[$id]); } write_json_file($p_file, $ps); // Update DB last_seen if ($current_user_id > 0) { try { $stmt = db()->prepare("UPDATE voice_sessions SET last_seen = ?, is_muted = ?, is_deafened = ? WHERE user_id = ?"); $stmt->execute([now_ms(), $is_muted, $is_deafened, $current_user_id]); } catch (Exception $e) {} } // Read signals from DB $signals = []; try { $stmt = db()->prepare("SELECT id, from_peer_id as `from`, data FROM voice_signals WHERE to_peer_id = ? ORDER BY id ASC"); $stmt->execute([$my_id]); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); if (!empty($rows)) { $ids = []; foreach ($rows as $r) { $signals[] = [ "from" => $r["from"], "data" => json_decode($r["data"], true) ]; $ids[] = $r["id"]; } // Delete signals we just read $placeholders = implode(',', array_fill(0, count($ids), '?')); $stmt_del = db()->prepare("DELETE FROM voice_signals WHERE id IN ($placeholders)"); $stmt_del->execute($ids); } // Periodic cleanup of old signals (> 1 minute) if (rand(1, 20) === 1) { $old_time = now_ms() - 60000; $stmt_clean = db()->prepare("DELETE FROM voice_signals WHERE created_at_ms < ?"); $stmt_clean->execute([$old_time]); } } catch (Exception $e) { error_log("Signal poll error: " . $e->getMessage()); } json_out(["success" => true, "participants" => $ps, "signals" => $signals]); } if ($action === "signal") { if (!$my_id) json_out(["error" => "Missing peer_id"], 400); $to = $_REQUEST["to"] ?? ""; $data = $_REQUEST["data"] ?? ""; if (!$to || !$data) json_out(["error" => "Missing to/data"], 400); try { $stmt = db()->prepare("INSERT INTO voice_signals (room_id, from_peer_id, to_peer_id, data, created_at_ms) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$room, $my_id, $to, $data, now_ms()]); json_out(["success" => true]); } catch (Exception $e) { json_out(["error" => "Signal send failed: " . $e->getMessage()], 500); } } if ($action === "list_all") { // Periodic cleanup of the DB table (stale sessions > 15s) if (rand(1, 10) === 1) { try { $stale_db_time = now_ms() - 15000; $stmt = db()->prepare("DELETE FROM voice_sessions WHERE last_seen < ?"); $stmt->execute([$stale_db_time]); } catch (Exception $e) {} } try { $stmt = db()->prepare(" SELECT vs.channel_id, vs.user_id, vs.is_muted, vs.is_deafened, u.username, u.display_name, u.avatar_url FROM voice_sessions vs JOIN users u ON vs.user_id = u.id WHERE vs.last_seen > ? "); $stale_db_time = now_ms() - 15000; $stmt->execute([$stale_db_time]); $sessions = $stmt->fetchAll(PDO::FETCH_ASSOC); $by_channel = []; foreach ($sessions as $s) { $by_channel[$s['channel_id']][] = $s; } json_out(["success" => true, "channels" => $by_channel]); } catch (Exception $e) { json_out(["error" => $e->getMessage()], 500); } } if ($action === "leave") { if ($my_id) { $p_file = room_participants_file($room); $ps = read_json_file($p_file); unset($ps[$my_id]); write_json_file($p_file, $ps); } if ($current_user_id > 0) { try { $stmt = db()->prepare("DELETE FROM voice_sessions WHERE user_id = ?"); $stmt->execute([$current_user_id]); } catch (Exception $e) {} } json_out(["success" => true]); } json_out(["error" => "Unknown action"], 404);