Pihole
This commit is contained in:
116
modules/pihole/bootstrap.php
Normal file
116
modules/pihole/bootstrap.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
use App\ModuleConfigException;
|
||||
|
||||
$moduleName = 'pihole';
|
||||
$mm = isset($modules) && $modules instanceof App\ModuleManager ? $modules : modules();
|
||||
|
||||
$mm->registerFunction($moduleName, 'settings', function () use ($moduleName): array {
|
||||
return modules()->settings($moduleName);
|
||||
});
|
||||
|
||||
$mm->registerFunction($moduleName, 'instances', function () use ($moduleName): array {
|
||||
$settings = modules()->settings($moduleName);
|
||||
$apiPath = trim((string)($settings['api_path'] ?? '/admin/api.php'));
|
||||
if ($apiPath === '') {
|
||||
$apiPath = '/admin/api.php';
|
||||
}
|
||||
if ($apiPath[0] !== '/') {
|
||||
$apiPath = '/' . $apiPath;
|
||||
}
|
||||
|
||||
$timeout = (int)($settings['api_timeout_sec'] ?? 8);
|
||||
if ($timeout <= 0) {
|
||||
$timeout = 8;
|
||||
}
|
||||
|
||||
$verifyTls = !isset($settings['verify_tls']) || $settings['verify_tls'] === '1' || $settings['verify_tls'] === 1 || $settings['verify_tls'] === true;
|
||||
|
||||
$normalizeSecret = static function (array $row): string {
|
||||
$password = trim((string)($row['password'] ?? ''));
|
||||
if ($password !== '') {
|
||||
return $password;
|
||||
}
|
||||
|
||||
return trim((string)($row['token'] ?? ''));
|
||||
};
|
||||
|
||||
$instances = [];
|
||||
|
||||
$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, '/'),
|
||||
'password' => $normalizeSecret($row),
|
||||
'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, '/'),
|
||||
'password' => 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $instances;
|
||||
});
|
||||
|
||||
$mm->registerFunction($moduleName, 'lists_primary_only', function () use ($moduleName): bool {
|
||||
$settings = modules()->settings($moduleName);
|
||||
return !empty($settings['lists_primary_only']);
|
||||
});
|
||||
Reference in New Issue
Block a user