Update pihole module

This commit is contained in:
2026-03-09 01:44:57 +01:00
parent c77b4b3ea7
commit 4adb50dc57
3 changed files with 94 additions and 24 deletions

View File

@@ -26,24 +26,76 @@ $mm->registerFunction($moduleName, 'instances', function () use ($moduleName): a
$verifyTls = !isset($settings['verify_tls']) || $settings['verify_tls'] === '1' || $settings['verify_tls'] === 1 || $settings['verify_tls'] === true;
$instances = [];
foreach (['primary', 'secondary'] as $key) {
$urlKey = $key . '_url';
$tokenKey = $key . '_token';
$nameKey = $key . '_name';
$url = trim((string)($settings[$urlKey] ?? ''));
if ($url === '') {
continue;
$rawJson = trim((string)($settings['instances_json'] ?? ''));
if ($rawJson !== '') {
$decoded = json_decode($rawJson, true);
if (is_array($decoded)) {
foreach ($decoded as $row) {
if (!is_array($row)) {
continue;
}
$id = trim((string)($row['id'] ?? ''));
$url = trim((string)($row['url'] ?? ''));
if ($id === '' || $url === '') {
continue;
}
$instances[$id] = [
'id' => $id,
'name' => trim((string)($row['name'] ?? '')) ?: $id,
'url' => rtrim($url, '/'),
'token' => trim((string)($row['token'] ?? '')),
'api_path' => $apiPath,
'timeout' => $timeout,
'verify_tls' => $verifyTls,
'is_primary' => !empty($row['is_primary']),
];
}
}
}
if (empty($instances)) {
foreach (['primary', 'secondary'] as $key) {
$urlKey = $key . '_url';
$tokenKey = $key . '_token';
$nameKey = $key . '_name';
$url = trim((string)($settings[$urlKey] ?? ''));
if ($url === '') {
continue;
}
$instances[$key] = [
'id' => $key,
'name' => trim((string)($settings[$nameKey] ?? '')) ?: ($key === 'primary' ? 'Primaer' : 'Sekundaer'),
'url' => rtrim($url, '/'),
'token' => trim((string)($settings[$tokenKey] ?? '')),
'api_path' => $apiPath,
'timeout' => $timeout,
'verify_tls' => $verifyTls,
'is_primary' => $key === 'primary',
];
}
}
$primaryId = trim((string)($settings['primary_id'] ?? ''));
if ($primaryId !== '' && isset($instances[$primaryId])) {
foreach ($instances as $id => &$row) {
$row['is_primary'] = ($id === $primaryId);
}
unset($row);
} else {
$hasPrimary = false;
foreach ($instances as $row) {
if (!empty($row['is_primary'])) {
$hasPrimary = true;
break;
}
}
if (!$hasPrimary && $instances) {
$firstKey = array_key_first($instances);
if ($firstKey !== null) {
$instances[$firstKey]['is_primary'] = true;
}
}
$instances[$key] = [
'id' => $key,
'name' => trim((string)($settings[$nameKey] ?? '')) ?: ($key === 'primary' ? 'Primaer' : 'Sekundaer'),
'url' => rtrim($url, '/'),
'token' => trim((string)($settings[$tokenKey] ?? '')),
'api_path' => $apiPath,
'timeout' => $timeout,
'verify_tls' => $verifyTls,
'is_primary' => $key === 'primary',
];
}
return $instances;