Update pihole module
This commit is contained in:
@@ -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;
|
$verifyTls = !isset($settings['verify_tls']) || $settings['verify_tls'] === '1' || $settings['verify_tls'] === 1 || $settings['verify_tls'] === true;
|
||||||
|
|
||||||
$instances = [];
|
$instances = [];
|
||||||
foreach (['primary', 'secondary'] as $key) {
|
|
||||||
$urlKey = $key . '_url';
|
$rawJson = trim((string)($settings['instances_json'] ?? ''));
|
||||||
$tokenKey = $key . '_token';
|
if ($rawJson !== '') {
|
||||||
$nameKey = $key . '_name';
|
$decoded = json_decode($rawJson, true);
|
||||||
$url = trim((string)($settings[$urlKey] ?? ''));
|
if (is_array($decoded)) {
|
||||||
if ($url === '') {
|
foreach ($decoded as $row) {
|
||||||
continue;
|
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;
|
return $instances;
|
||||||
|
|||||||
@@ -21,12 +21,14 @@
|
|||||||
},
|
},
|
||||||
"setup": {
|
"setup": {
|
||||||
"fields": [
|
"fields": [
|
||||||
{ "name": "primary_name", "label": "Primaer-Name", "type": "text", "required": false, "help": "Anzeige-Name (z.B. Pi-hole Main)" },
|
{ "name": "instances_json", "label": "Instanzen (JSON)", "type": "textarea", "required": false, "help": "Mehrere Instanzen als JSON-Array. Beispiel: [{\"id\":\"pihole-main\",\"name\":\"Pi-hole Main\",\"url\":\"http://pi-hole.local\",\"token\":\"TOKEN\",\"is_primary\":true},{\"id\":\"pihole-backup\",\"name\":\"Pi-hole Backup\",\"url\":\"http://pi-hole-2.local\",\"token\":\"TOKEN2\"}]" },
|
||||||
{ "name": "primary_url", "label": "Primaer-URL", "type": "text", "required": true, "help": "Basis-URL der Instanz, z.B. http://pi-hole.local" },
|
{ "name": "primary_id", "label": "Primaer-ID (optional)", "type": "text", "required": false, "help": "Wenn gesetzt, wird diese Instanz als Primaer fuer Listen-Updates genutzt." },
|
||||||
{ "name": "primary_token", "label": "Primaer-API Token", "type": "password", "required": false, "help": "Token aus dem Pi-hole Admin (API Token)." },
|
{ "name": "primary_name", "label": "Primaer-Name (Legacy)", "type": "text", "required": false, "help": "Anzeige-Name (z.B. Pi-hole Main)" },
|
||||||
{ "name": "secondary_name", "label": "Sekundaer-Name", "type": "text", "required": false, "help": "Anzeige-Name (z.B. Pi-hole Backup)" },
|
{ "name": "primary_url", "label": "Primaer-URL (Legacy)", "type": "text", "required": false, "help": "Basis-URL der Instanz, z.B. http://pi-hole.local" },
|
||||||
{ "name": "secondary_url", "label": "Sekundaer-URL", "type": "text", "required": false, "help": "Basis-URL der zweiten Instanz" },
|
{ "name": "primary_token", "label": "Primaer-API Token (Legacy)", "type": "password", "required": false, "help": "Token aus dem Pi-hole Admin (API Token)." },
|
||||||
{ "name": "secondary_token", "label": "Sekundaer-API Token", "type": "password", "required": false },
|
{ "name": "secondary_name", "label": "Sekundaer-Name (Legacy)", "type": "text", "required": false, "help": "Anzeige-Name (z.B. Pi-hole Backup)" },
|
||||||
|
{ "name": "secondary_url", "label": "Sekundaer-URL (Legacy)", "type": "text", "required": false, "help": "Basis-URL der zweiten Instanz" },
|
||||||
|
{ "name": "secondary_token", "label": "Sekundaer-API Token (Legacy)", "type": "password", "required": false },
|
||||||
{ "name": "api_path", "label": "API Pfad", "type": "text", "required": false, "help": "Standard: /admin/api.php" },
|
{ "name": "api_path", "label": "API Pfad", "type": "text", "required": false, "help": "Standard: /admin/api.php" },
|
||||||
{ "name": "api_timeout_sec", "label": "API Timeout (Sek.)", "type": "number", "required": false, "help": "Standard 8" },
|
{ "name": "api_timeout_sec", "label": "API Timeout (Sek.)", "type": "number", "required": false, "help": "Standard 8" },
|
||||||
{ "name": "verify_tls", "label": "TLS Verifikation aktiv", "type": "checkbox", "required": false, "help": "Deaktivieren nur wenn das Zertifikat unsicher ist." },
|
{ "name": "verify_tls", "label": "TLS Verifikation aktiv", "type": "checkbox", "required": false, "help": "Deaktivieren nur wenn das Zertifikat unsicher ist." },
|
||||||
|
|||||||
@@ -97,10 +97,26 @@ $apiRequest = function (array $instance, array $params) use ($normalizeApiPath):
|
|||||||
return ['ok' => true, 'data' => $data, 'http_code' => $httpCode, 'url' => $full];
|
return ['ok' => true, 'data' => $data, 'http_code' => $httpCode, 'url' => $full];
|
||||||
};
|
};
|
||||||
|
|
||||||
$pickInstances = function (string $target) use ($instances): array {
|
$resolvePrimaryId = function () use ($instances): ?string {
|
||||||
|
foreach ($instances as $id => $row) {
|
||||||
|
if (!empty($row['is_primary'])) {
|
||||||
|
return $id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$first = array_key_first($instances);
|
||||||
|
return $first !== null ? (string)$first : null;
|
||||||
|
};
|
||||||
|
|
||||||
|
$pickInstances = function (string $target) use ($instances, $resolvePrimaryId): array {
|
||||||
if ($target === 'all') {
|
if ($target === 'all') {
|
||||||
return $instances;
|
return $instances;
|
||||||
}
|
}
|
||||||
|
if ($target === 'primary') {
|
||||||
|
$primaryId = $resolvePrimaryId();
|
||||||
|
if ($primaryId !== null && isset($instances[$primaryId])) {
|
||||||
|
return [$primaryId => $instances[$primaryId]];
|
||||||
|
}
|
||||||
|
}
|
||||||
if (isset($instances[$target])) {
|
if (isset($instances[$target])) {
|
||||||
return [$target => $instances[$target]];
|
return [$target => $instances[$target]];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user