From 74ff387c5d903f461ac61e1da9e19e7976be36e3 Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Fri, 29 Mar 2024 16:38:04 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=BF=A1=E6=81=AF=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=20*=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/SiteConfig.php | 96 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 models/SiteConfig.php diff --git a/models/SiteConfig.php b/models/SiteConfig.php new file mode 100644 index 0000000..802de3e --- /dev/null +++ b/models/SiteConfig.php @@ -0,0 +1,96 @@ +siteTitle = $_ENV['SITE_TITLE']; + $this->registrationEnabled = $_ENV['REGISTRATION_ENABLED'] === 'true'; + $this->domain = $_ENV['DOMAIN']; + $this->verifyProvider = $_ENV['VERIFY_PROVIDER']; + $this->recaptchaSiteKey = $_ENV['RECAPTCHA_SITE_KEY']; + $this->recaptchaSecret = $_ENV['RECAPTCHA_SECRET']; + $this->hcaptchaSiteKey = $_ENV['HCAPTCHA_SITE_KEY']; + $this->hcaptchaSecret = $_ENV['HCAPTCHA_SECRET']; + $this->turnstileSiteKey = $_ENV['TURNSTILE_SITE_KEY']; + $this->turnstileSecret = $_ENV['TURNSTILE_SECRET']; + $this->enableIpinfo = $_ENV['ENABLE_IPINFO'] === 'true'; + $this->ipinfoToken = $_ENV['IPINFO_TOKEN']; + return true; + } catch (Exception) { + return false; + } + } + + /** + * 保存配置信息 + * @return bool + */ + public function saveToEnv(): bool + { + try { + $env = parse_ini_file(Yii::getAlias('@app/.env')); + if($env === false) { + return false; + } + $env['SITE_TITLE'] = $this->siteTitle; + $env['REGISTRATION_ENABLED'] = $this->registrationEnabled ? 'true' : 'false'; + $env['DOMAIN'] = $this->domain; + $env['VERIFY_PROVIDER'] = $this->verifyProvider; + $env['RECAPTCHA_SITE_KEY'] = $this->recaptchaSiteKey; + $env['RECAPTCHA_SECRET'] = $this->recaptchaSecret; + $env['HCAPTCHA_SITE_KEY'] = $this->hcaptchaSiteKey; + $env['HCAPTCHA_SECRET'] = $this->hcaptchaSecret; + $env['TURNSTILE_SITE_KEY'] = $this->turnstileSiteKey; + $env['TURNSTILE_SECRET'] = $this->turnstileSecret; + $env['COOKIE_VALIDATION_KEY'] = $this->cookieValidationKey; + $env['ENABLE_IPINFO'] = $this->enableIpinfo ? 'true' : 'false'; + $env['IPINFO_TOKEN'] = $this->ipinfoToken; + $data = array_map(function ($key, $value) { + return "$key=$value"; + }, array_keys($env), $env); + return !(file_put_contents('.env', implode("\n", $data)) == false); + } catch (Exception) { + return false; + } + } +} \ No newline at end of file