['header' => "x-api-key: {$cfg['api_key']}\r\n", 'method' => 'GET', 'timeout' => 2, 'ignore_errors' => true]];
$context = stream_context_create($options);
$base_url = "http://{$cfg['host']}:{$cfg['port']}/{$cfg['server_id']}";
$channels_res = @file_get_contents("$base_url/channellist", false, $context);
$clients_res = @file_get_contents("$base_url/clientlist?-groups", false, $context);
if ($channels_res !== false && $clients_res !== false) {
$data = [
'channels' => json_decode($channels_res, true)['body'] ?? [],
'clients' => json_decode($clients_res, true)['body'] ?? []
];
file_put_contents($cache_file, json_encode($data));
return $data;
}
return ['channels' => [], 'clients' => []];
}
// Tri hiérarchique
function sort_ts_channels(array $channels, $pid = 0) {
$ordered = []; $by_order = [];
foreach ($channels as $c) { if (($c['pid'] ?? 0) == $pid) $by_order[$c['channel_order'] ?? 0] = $c; }
$current_order = 0; $max_iterations = count($channels) + 1; $i = 0;
while (isset($by_order[$current_order]) && $i < $max_iterations) {
$channel = $by_order[$current_order]; $ordered[] = $channel;
$sub_channels = sort_ts_channels($channels, $channel['cid']);
foreach ($sub_channels as $sc) $ordered[] = $sc;
$current_order = $channel['cid']; $i++;
}
return $ordered;
}
// Traitement des données
$start_time = microtime(true);
$ts_data = fetch_teamspeak_data($config['server']);
$api_ping = round((microtime(true) - $start_time) * 1000); // Calcule le temps de réponse de l'API locale
$raw_channels = $ts_data['channels'];
$raw_clients = $ts_data['clients'];
$is_online = !empty($raw_channels);
$channels = sort_ts_channels($raw_channels, 0);
$clients_by_channel = [];
$total_real_clients = 0;
if ($is_online) {
foreach ($raw_clients as $client) {
if (($client['client_type'] ?? 0) == 1) continue; // On ignore les bots/ServerQuery
$clients_by_channel[$client['cid']][] = $client;
$total_real_clients++;
}
}
// Rendu du fragment AJAX
ob_start();
if ($is_online): ?>
En Ligne
Salons
Latence API
ms
-
';
if (isset($client['client_servergroups'])) {
$groups = explode(',', $client['client_servergroups']);
foreach ($groups as $g_id) {
if (isset($config['icones_roles'][$g_id])) {
$role_icon = '
';
break;
}
}
}
echo $role_icon;
?>
Impossible d'établir le contact avec le serveur central.