169 lines
5.3 KiB
PHP
169 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Setting;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
class SettingsService
|
|
{
|
|
public const KEYS = [
|
|
'telegram.bot_token',
|
|
'telegram.bot_username',
|
|
'telegram.owner_chat_id',
|
|
'channels.max_per_agent',
|
|
'channels.domains',
|
|
'panel.admin_hosts',
|
|
'panel.agent_hosts',
|
|
];
|
|
|
|
/** @return array<string, string|null> */
|
|
public function all(): array
|
|
{
|
|
$out = [];
|
|
foreach (self::KEYS as $key) {
|
|
$out[$key] = $this->get($key);
|
|
}
|
|
|
|
return $out;
|
|
}
|
|
|
|
public function get(string $key, ?string $default = null): ?string
|
|
{
|
|
if (! $this->tableReady()) {
|
|
return $default;
|
|
}
|
|
|
|
$map = $this->cachedMap();
|
|
|
|
return array_key_exists($key, $map) ? $map[$key] : $default;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $values
|
|
*/
|
|
public function putMany(array $values): void
|
|
{
|
|
foreach ($values as $key => $value) {
|
|
if (! in_array($key, self::KEYS, true)) {
|
|
continue;
|
|
}
|
|
$str = $value === null ? null : trim((string) $value);
|
|
Setting::query()->updateOrCreate(
|
|
['key' => $key],
|
|
['value' => $str === '' ? null : $str]
|
|
);
|
|
}
|
|
Cache::forget($this->cacheKey());
|
|
$this->applyToConfig();
|
|
}
|
|
|
|
public function applyToConfig(): void
|
|
{
|
|
if (! $this->tableReady()) {
|
|
return;
|
|
}
|
|
|
|
$map = $this->cachedMap();
|
|
|
|
$bot = $map['telegram.bot_token'] ?? null;
|
|
if ($bot !== null && $bot !== '') {
|
|
config([
|
|
'coruna.telegram.bot_token' => $bot,
|
|
'nutgram.token' => $bot,
|
|
]);
|
|
}
|
|
$botName = $map['telegram.bot_username'] ?? null;
|
|
if ($botName !== null) {
|
|
config(['coruna.telegram.bot_username' => $botName]);
|
|
}
|
|
$chat = $map['telegram.owner_chat_id'] ?? null;
|
|
if ($chat !== null && $chat !== '') {
|
|
config(['coruna.telegram.owner_chat_id' => $chat]);
|
|
}
|
|
|
|
$max = $map['channels.max_per_agent'] ?? null;
|
|
if ($max !== null && $max !== '' && is_numeric($max)) {
|
|
config(['coruna.channels.max_per_agent' => max(1, (int) $max)]);
|
|
}
|
|
|
|
if (array_key_exists('channels.domains', $map) && $map['channels.domains'] !== null) {
|
|
$domains = self::parseDomains($map['channels.domains']);
|
|
// Settings override channel hosts only; reporting stays CORUNA_REPORTING_DOMAINS.
|
|
config([
|
|
'coruna.channel_domains' => $domains,
|
|
'coruna.deployment_domains' => $domains,
|
|
]);
|
|
}
|
|
|
|
// Once saved in settings, DB wins (null/empty = no Host restriction).
|
|
if (array_key_exists('panel.admin_hosts', $map)) {
|
|
config(['coruna.panel.admin_hosts' => self::parseDomains($map['panel.admin_hosts'])]);
|
|
}
|
|
if (array_key_exists('panel.agent_hosts', $map)) {
|
|
config(['coruna.panel.agent_hosts' => self::parseDomains($map['panel.agent_hosts'])]);
|
|
}
|
|
}
|
|
|
|
/** Effective values for the settings form (DB overrides env). */
|
|
public function effectiveForForm(): array
|
|
{
|
|
$domains = config('coruna.channel_domains', []);
|
|
$adminHosts = config('coruna.panel.admin_hosts', []);
|
|
$agentHosts = config('coruna.panel.agent_hosts', []);
|
|
|
|
return [
|
|
'telegram.bot_token' => (string) (config('coruna.telegram.bot_token') ?: ''),
|
|
'telegram.bot_username' => (string) (config('coruna.telegram.bot_username') ?: ''),
|
|
'telegram.owner_chat_id' => (string) (config('coruna.telegram.owner_chat_id') ?: ''),
|
|
'channels.max_per_agent' => (string) (int) config('coruna.channels.max_per_agent', 5),
|
|
'channels.domains' => is_array($domains) ? implode("\n", $domains) : '',
|
|
'panel.admin_hosts' => is_array($adminHosts) ? implode("\n", $adminHosts) : '',
|
|
'panel.agent_hosts' => is_array($agentHosts) ? implode("\n", $agentHosts) : '',
|
|
];
|
|
}
|
|
|
|
/** @return list<string> */
|
|
public static function parseDomains(?string $raw): array
|
|
{
|
|
if ($raw === null || trim($raw) === '') {
|
|
return [];
|
|
}
|
|
$parts = preg_split('/[\s,;]+/', trim($raw)) ?: [];
|
|
|
|
return array_values(array_filter(array_map(static function ($d) {
|
|
$d = trim((string) $d);
|
|
$d = preg_replace('#^https?://#i', '', $d) ?? $d;
|
|
$d = rtrim($d, '/');
|
|
// Host only (drop path / port).
|
|
$d = explode('/', $d, 2)[0];
|
|
$d = preg_replace('/:\d+$/', '', $d) ?? $d;
|
|
|
|
return strtolower($d);
|
|
}, $parts)));
|
|
}
|
|
|
|
/** @return array<string, string|null> */
|
|
private function cachedMap(): array
|
|
{
|
|
return Cache::remember($this->cacheKey(), 300, function () {
|
|
return Setting::query()->pluck('value', 'key')->all();
|
|
});
|
|
}
|
|
|
|
private function cacheKey(): string
|
|
{
|
|
return 'coruna.settings.map';
|
|
}
|
|
|
|
private function tableReady(): bool
|
|
{
|
|
try {
|
|
return Schema::hasTable('settings');
|
|
} catch (\Throwable) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|