Consolidations (config.php gains 5 shared utilities): - vv_format_uptime() replaces 4 inline uptime-formatting blocks - vv_parse_conf_scalar() replaces vv_arr_scalar/vv_wd_scalar/vv_fb_scalar/vv_media_conf_scalar - vv_known_hosts() replaces vv_arr_known_hosts/vv_fb_known_hosts + inline parser in watchdog - vv_parse_kv_db() replaces inline key=value parsing in snapshot and monitor - vv_local_ip() replaces duplicate in docker_folders.php and inline in docker.php All module-level function names kept as thin aliases so call sites unchanged. Critical bug fixes: - api/system.php: added require_once config.php and POST-only guard (no auth on shutdown) - api/movescript.php + reorderarray.php: use vv_write_conf_raw (atomic) + vv_push_master_conf - api/snapshot.php: share /tmp/vv_cpu_stat.json with vv_cpu_per_core() instead of own state file Correctness: - vv_cpu_per_core() and vv_network_stats(): atomic tmp+rename for state files (concurrent poll safety) - ext_ip curl cache moved from /tmp/vv_ext_ip.cache to vv_cache_read/write (canonical cache dir) - monitor_remote.php + board.php + snapshot.php: all use vv_cache_read/write instead of ad-hoc /tmp files HTTP method guards added to write-only APIs that were missing them: - api/scheduler.php, conf_toggle.php, flag_toggle.php
94 lines
3.1 KiB
PHP
94 lines
3.1 KiB
PHP
<?php
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
function vv_container_webui(string $name, array $portMap): string {
|
|
$template = '/boot/config/plugins/dockerMan/templates-user/my-' . $name . '.xml';
|
|
if (!file_exists($template)) return '';
|
|
|
|
$xml = @file_get_contents($template) ?: '';
|
|
if (!preg_match('/<WebUI>(.*?)<\/WebUI>/s', $xml, $m)) return '';
|
|
|
|
$url = trim($m[1]);
|
|
if (!$url) return '';
|
|
|
|
$url = str_replace('[IP]', vv_local_ip(), $url);
|
|
|
|
// [PORT:XXXX] → mapped host port
|
|
$url = preg_replace_callback('/\[PORT:(\d+)\]/', function($pm) use ($name, $portMap) {
|
|
return $portMap[$name][$pm[1]] ?? $pm[1];
|
|
}, $url);
|
|
|
|
return $url;
|
|
}
|
|
|
|
function vv_get_docker_folders(): array {
|
|
$folderFile = '/boot/config/plugins/folder.view3/docker.json';
|
|
$folderData = file_exists($folderFile)
|
|
? (json_decode(@file_get_contents($folderFile), true) ?: [])
|
|
: [];
|
|
|
|
// One docker ps call: names, status, port mappings
|
|
$raw = shell_exec("docker ps -a --format '{{.Names}}\t{{.Status}}\t{{.Ports}}' 2>/dev/null") ?? '';
|
|
$statusMap = [];
|
|
$portMap = [];
|
|
foreach (explode("\n", trim($raw)) as $line) {
|
|
$parts = explode("\t", $line, 3);
|
|
if (count($parts) < 2) continue;
|
|
[$cname, $status, $ports] = array_pad($parts, 3, '');
|
|
$cname = trim($cname);
|
|
if ($cname === '') continue;
|
|
$statusMap[$cname] = trim($status);
|
|
foreach (explode(',', $ports) as $entry) {
|
|
if (preg_match('/(\d+)->(\d+)\/tcp/', trim($entry), $pm)) {
|
|
$portMap[$cname][$pm[2]] = $pm[1]; // containerPort => hostPort
|
|
}
|
|
}
|
|
}
|
|
|
|
$folderContainerNames = [];
|
|
$folders = [];
|
|
|
|
foreach ($folderData as $id => $f) {
|
|
$containers = [];
|
|
foreach ($f['containers'] ?? [] as $cname) {
|
|
$folderContainerNames[] = $cname;
|
|
$status = $statusMap[$cname] ?? '';
|
|
$running = str_starts_with($status, 'Up');
|
|
$containers[] = [
|
|
'name' => $cname,
|
|
'running' => $running,
|
|
'status' => $status,
|
|
'webui' => vv_container_webui($cname, $portMap),
|
|
];
|
|
}
|
|
usort($containers, fn($a, $b) => $b['running'] <=> $a['running'] ?: strcmp($a['name'], $b['name']));
|
|
|
|
$folders[] = [
|
|
'id' => $id,
|
|
'name' => $f['name'] ?? 'Unnamed',
|
|
'icon' => $f['icon'] ?? '',
|
|
'containers' => $containers,
|
|
];
|
|
}
|
|
usort($folders, fn($a, $b) => strcmp($a['name'], $b['name']));
|
|
|
|
$ungrouped = [];
|
|
foreach ($statusMap as $cname => $status) {
|
|
if (in_array($cname, $folderContainerNames, true)) continue;
|
|
$running = str_starts_with($status, 'Up');
|
|
$ungrouped[] = [
|
|
'name' => $cname,
|
|
'running' => $running,
|
|
'status' => $status,
|
|
'webui' => vv_container_webui($cname, $portMap),
|
|
];
|
|
}
|
|
usort($ungrouped, fn($a, $b) => strcmp($a['name'], $b['name']));
|
|
|
|
return [
|
|
'available' => true,
|
|
'folders' => $folders,
|
|
'ungrouped' => $ungrouped,
|
|
];
|
|
}
|