You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
431 lines
17 KiB
PHP
431 lines
17 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
use App\Controllers\AdminController;
|
|
use App\Models\Configurations;
|
|
use App\Services\AdminDataMasker;
|
|
use App\Services\AdminPermission;
|
|
use App\Services\Auth;
|
|
use App\Services\CaptchaConfig;
|
|
use App\Services\Cloud\CloudIpChangeBillingService;
|
|
use App\Services\Cloud\CloudPaidIpChangeFeature;
|
|
use App\Services\Cloud\CloudProvisioningBillingConfig;
|
|
use App\Services\Cloud\CloudUserIsoFeature;
|
|
use App\Services\Cloud\CloudVpcFeature;
|
|
use App\Services\LoginSessionDurationService;
|
|
use App\Services\LoginSessionRevocationService;
|
|
use App\Services\RegistrationEmailWhitelist;
|
|
use App\Services\ReferralConfig;
|
|
use App\Services\RedisClient;
|
|
use App\Services\TrustedLoginDeviceService;
|
|
|
|
class ConfigController extends AdminController
|
|
{
|
|
private const EDITABLE_CONFIG_DEFAULTS = [
|
|
'cdn' => '',
|
|
'backup_cdn' => '',
|
|
'default_app_name' => '',
|
|
'enable_login_email_verify' => 'false',
|
|
'login_session_days' => '30',
|
|
'trusted_login_device_days' => '180',
|
|
'login_forbidden_ips' => '',
|
|
'seo' => '',
|
|
'email_blacklist' => '',
|
|
RegistrationEmailWhitelist::ENABLED_KEY => RegistrationEmailWhitelist::DEFAULT_ENABLED,
|
|
RegistrationEmailWhitelist::DOMAINS_KEY => RegistrationEmailWhitelist::DEFAULT_DOMAINS,
|
|
'captch_reg' => 'false',
|
|
'captch_login' => 'false',
|
|
'captch_respass' => 'true',
|
|
'enable_email_verify' => 'true',
|
|
'email_verify_ttl' => '900',
|
|
'email_verify_iplimit' => '5',
|
|
'aff_method' => 'affid',
|
|
'enable_invite_code' => 'false',
|
|
ReferralConfig::RATE_KEY => ReferralConfig::DEFAULT_RATE,
|
|
ReferralConfig::VALID_DAYS_KEY => ReferralConfig::DEFAULT_VALID_DAYS,
|
|
ReferralConfig::ENABLED_KEY => ReferralConfig::DEFAULT_ENABLED,
|
|
CloudIpChangeBillingService::PRICE_CONFIG_KEY =>
|
|
CloudIpChangeBillingService::DEFAULT_PRICE,
|
|
CloudPaidIpChangeFeature::CONFIG_KEY =>
|
|
CloudPaidIpChangeFeature::DEFAULT_VALUE,
|
|
CloudProvisioningBillingConfig::PAYMENT_WINDOW_MINUTES_KEY =>
|
|
(string) CloudProvisioningBillingConfig::DEFAULT_PAYMENT_WINDOW_MINUTES,
|
|
CloudUserIsoFeature::CONFIG_KEY => 'false',
|
|
CloudVpcFeature::CONFIG_KEY => 'false',
|
|
'host_1' => '',
|
|
'host_2' => '',
|
|
'host_3' => '',
|
|
'host_4' => '',
|
|
'host_5' => '',
|
|
'host_6' => '',
|
|
'host_7' => '',
|
|
'appname_1' => '',
|
|
'appname_2' => '',
|
|
'appname_3' => '',
|
|
'appname_4' => '',
|
|
'appname_5' => '',
|
|
'appname_6' => '',
|
|
'appname_7' => '',
|
|
'theme_1' => 'venix',
|
|
'theme_2' => 'venix',
|
|
'theme_3' => 'venix',
|
|
'theme_4' => 'venix',
|
|
'theme_5' => 'venix',
|
|
'theme_6' => 'venix',
|
|
'theme_7' => 'venix',
|
|
];
|
|
|
|
public function config($request, $response, $args)
|
|
{
|
|
return $this->view()->display('admin/config.tpl');
|
|
}
|
|
|
|
public function save($request, $response, $args)
|
|
{
|
|
if (!$this->isSuperAdmin()) {
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => '你没有权限',
|
|
]);
|
|
}
|
|
|
|
$payload = $this->getRequestPayload($request);
|
|
try {
|
|
if (array_key_exists(LoginSessionDurationService::CONFIG_KEY, $payload)) {
|
|
$payload[LoginSessionDurationService::CONFIG_KEY] = (string) LoginSessionDurationService::validateDays(
|
|
$payload[LoginSessionDurationService::CONFIG_KEY]
|
|
);
|
|
}
|
|
if (array_key_exists(TrustedLoginDeviceService::CONFIG_KEY, $payload)) {
|
|
$payload[TrustedLoginDeviceService::CONFIG_KEY] = (string) TrustedLoginDeviceService::validateDays(
|
|
$payload[TrustedLoginDeviceService::CONFIG_KEY]
|
|
);
|
|
}
|
|
if (array_key_exists(RegistrationEmailWhitelist::ENABLED_KEY, $payload)) {
|
|
$payload[RegistrationEmailWhitelist::ENABLED_KEY] = RegistrationEmailWhitelist::validateEnabled(
|
|
$payload[RegistrationEmailWhitelist::ENABLED_KEY]
|
|
);
|
|
}
|
|
if (array_key_exists(RegistrationEmailWhitelist::DOMAINS_KEY, $payload)) {
|
|
$payload[RegistrationEmailWhitelist::DOMAINS_KEY] = RegistrationEmailWhitelist::validateDomains(
|
|
$payload[RegistrationEmailWhitelist::DOMAINS_KEY]
|
|
);
|
|
}
|
|
foreach (['captch_reg', 'captch_login', 'captch_respass', 'enable_email_verify'] as $key) {
|
|
if (array_key_exists($key, $payload)) {
|
|
$payload[$key] = CaptchaConfig::validateBoolean($payload[$key]);
|
|
}
|
|
}
|
|
foreach (['email_verify_ttl', 'email_verify_iplimit'] as $key) {
|
|
if (array_key_exists($key, $payload)) {
|
|
$payload[$key] = CaptchaConfig::validatePositiveInt($payload[$key], $key);
|
|
}
|
|
}
|
|
if (array_key_exists(ReferralConfig::RATE_KEY, $payload)) {
|
|
$payload[ReferralConfig::RATE_KEY] = ReferralConfig::validateRate($payload[ReferralConfig::RATE_KEY]);
|
|
}
|
|
if (array_key_exists(ReferralConfig::VALID_DAYS_KEY, $payload)) {
|
|
$payload[ReferralConfig::VALID_DAYS_KEY] = (string) ReferralConfig::validateDays(
|
|
$payload[ReferralConfig::VALID_DAYS_KEY]
|
|
);
|
|
}
|
|
if (array_key_exists(ReferralConfig::ENABLED_KEY, $payload)) {
|
|
$payload[ReferralConfig::ENABLED_KEY] = ReferralConfig::validateBoolean(
|
|
$payload[ReferralConfig::ENABLED_KEY]
|
|
);
|
|
}
|
|
if (array_key_exists(
|
|
CloudIpChangeBillingService::PRICE_CONFIG_KEY,
|
|
$payload
|
|
)) {
|
|
$payload[CloudIpChangeBillingService::PRICE_CONFIG_KEY] =
|
|
CloudIpChangeBillingService::normalizePrice(
|
|
$payload[CloudIpChangeBillingService::PRICE_CONFIG_KEY]
|
|
);
|
|
}
|
|
if (array_key_exists(
|
|
CloudProvisioningBillingConfig::PAYMENT_WINDOW_MINUTES_KEY,
|
|
$payload
|
|
)) {
|
|
$payload[CloudProvisioningBillingConfig::PAYMENT_WINDOW_MINUTES_KEY] =
|
|
(string) CloudProvisioningBillingConfig::validatePaymentWindowMinutes(
|
|
$payload[CloudProvisioningBillingConfig::PAYMENT_WINDOW_MINUTES_KEY]
|
|
);
|
|
}
|
|
foreach ([
|
|
CloudPaidIpChangeFeature::CONFIG_KEY =>
|
|
'付费更换公网 IPv4 展示',
|
|
CloudUserIsoFeature::CONFIG_KEY => '用户 ISO',
|
|
CloudVpcFeature::CONFIG_KEY => 'VPC 私网',
|
|
] as $featureKey => $featureLabel) {
|
|
if (!array_key_exists($featureKey, $payload)) {
|
|
continue;
|
|
}
|
|
$enabled = filter_var(
|
|
$payload[$featureKey],
|
|
FILTER_VALIDATE_BOOLEAN,
|
|
FILTER_NULL_ON_FAILURE
|
|
);
|
|
if ($enabled === null) {
|
|
throw new \InvalidArgumentException(
|
|
$featureLabel . '开关状态无效'
|
|
);
|
|
}
|
|
$payload[$featureKey] = $enabled ? 'true' : 'false';
|
|
}
|
|
} catch (\InvalidArgumentException $exception) {
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => $exception->getMessage(),
|
|
]);
|
|
}
|
|
$configKeys = array_keys(self::EDITABLE_CONFIG_DEFAULTS);
|
|
$configurations = Configurations::whereIn('name', $configKeys)->get()->keyBy('name');
|
|
|
|
if (array_key_exists(RegistrationEmailWhitelist::ENABLED_KEY, $payload)
|
|
|| array_key_exists(RegistrationEmailWhitelist::DOMAINS_KEY, $payload)) {
|
|
$enabled = $payload[RegistrationEmailWhitelist::ENABLED_KEY]
|
|
?? (isset($configurations[RegistrationEmailWhitelist::ENABLED_KEY])
|
|
? (string) $configurations[RegistrationEmailWhitelist::ENABLED_KEY]->value
|
|
: RegistrationEmailWhitelist::DEFAULT_ENABLED);
|
|
$domains = $payload[RegistrationEmailWhitelist::DOMAINS_KEY]
|
|
?? (isset($configurations[RegistrationEmailWhitelist::DOMAINS_KEY])
|
|
? (string) $configurations[RegistrationEmailWhitelist::DOMAINS_KEY]->value
|
|
: RegistrationEmailWhitelist::DEFAULT_DOMAINS);
|
|
try {
|
|
RegistrationEmailWhitelist::assertValidConfiguration($enabled, $domains);
|
|
} catch (\InvalidArgumentException $exception) {
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => $exception->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
$affectedRows = 0;
|
|
$keysToDelete = [];
|
|
|
|
foreach (self::EDITABLE_CONFIG_DEFAULTS as $key => $defaultValue) {
|
|
if (!array_key_exists($key, $payload)) {
|
|
continue;
|
|
}
|
|
|
|
$newValue = $this->normalizeConfigValue($payload[$key], $defaultValue, $key);
|
|
$current = isset($configurations[$key]) ? (string) $configurations[$key]->value : (string) $defaultValue;
|
|
$keysToDelete[] = $key;
|
|
|
|
if ($this->isDomainSiteConfigKey($key)) {
|
|
$keysToDelete[] = 'domain_site_configs';
|
|
}
|
|
|
|
// Referral settings have meaningful defaults even before their
|
|
// rows exist. Persist the first explicit save (including a
|
|
// value equal to that default) so administrators can opt into
|
|
// the global referral policy and mark the setting configured.
|
|
$isReferralKey = in_array($key, [
|
|
ReferralConfig::RATE_KEY,
|
|
ReferralConfig::VALID_DAYS_KEY,
|
|
ReferralConfig::ENABLED_KEY,
|
|
], true);
|
|
if ($newValue === $current && !($isReferralKey && !isset($configurations[$key]))) {
|
|
continue;
|
|
}
|
|
|
|
$configuration = isset($configurations[$key]) ? $configurations[$key] : new Configurations();
|
|
$configuration->name = $key;
|
|
$configuration->value = $newValue;
|
|
$configuration->save();
|
|
|
|
$affectedRows++;
|
|
}
|
|
|
|
if (!empty($keysToDelete)) {
|
|
CaptchaConfig::clearCache();
|
|
$redisClient = new RedisClient();
|
|
$redisClient->deleteMultiple(array_unique($keysToDelete));
|
|
ReferralConfig::clearCache($redisClient);
|
|
}
|
|
|
|
if ($affectedRows > 0) {
|
|
return $response->withJson([
|
|
'ret' => 1,
|
|
'msg' => '提交成功',
|
|
]);
|
|
}
|
|
|
|
return $response->withJson([
|
|
'ret' => 1,
|
|
'msg' => '配置无变化,缓存已刷新',
|
|
]);
|
|
}
|
|
|
|
public function get($request, $response, $args)
|
|
{
|
|
$configurations = Configurations::whereIn('name', array_keys(self::EDITABLE_CONFIG_DEFAULTS))
|
|
->get()
|
|
->keyBy('name');
|
|
|
|
$data = ['ret' => 1];
|
|
$captchaDefaults = CaptchaConfig::defaults();
|
|
|
|
foreach (self::EDITABLE_CONFIG_DEFAULTS as $key => $defaultValue) {
|
|
$value = isset($configurations[$key]) ? (string) $configurations[$key]->value : (string) $defaultValue;
|
|
if ($key === 'email_blacklist') {
|
|
$value = AdminDataMasker::emailList($value);
|
|
} elseif ($key === 'login_forbidden_ips') {
|
|
$value = AdminDataMasker::ipList($value);
|
|
}
|
|
$value = AdminDataMasker::ipsInText($value);
|
|
if (array_key_exists($key, $captchaDefaults)) {
|
|
$data[$key] = CaptchaConfig::get($key);
|
|
} elseif ($key === LoginSessionDurationService::CONFIG_KEY) {
|
|
$data[$key] = (string) LoginSessionDurationService::normalizeDays($value);
|
|
} elseif ($key === TrustedLoginDeviceService::CONFIG_KEY) {
|
|
$data[$key] = (string) TrustedLoginDeviceService::normalizeDays($value);
|
|
} elseif ($key === RegistrationEmailWhitelist::ENABLED_KEY) {
|
|
$data[$key] = RegistrationEmailWhitelist::normalizeEnabled($value);
|
|
} elseif ($key === ReferralConfig::RATE_KEY) {
|
|
$data[$key] = ReferralConfig::normalizeRate($value);
|
|
} elseif ($key === ReferralConfig::VALID_DAYS_KEY) {
|
|
$data[$key] = (string) ReferralConfig::normalizeDays($value);
|
|
} elseif ($key === ReferralConfig::ENABLED_KEY) {
|
|
$data[$key] = ReferralConfig::normalizeBoolean($value) ? 'true' : 'false';
|
|
} elseif ($key === CloudProvisioningBillingConfig::PAYMENT_WINDOW_MINUTES_KEY) {
|
|
$data[$key] = (string) CloudProvisioningBillingConfig::validatePaymentWindowMinutes(
|
|
$value
|
|
);
|
|
} else {
|
|
$data[$key] = $value;
|
|
}
|
|
}
|
|
|
|
$data['available_themes'] = $this->getAvailableThemes();
|
|
|
|
return $response->withJson($data);
|
|
}
|
|
|
|
public function clearTrustedLoginDevices($request, $response, $args)
|
|
{
|
|
if (!$this->isSuperAdmin()) {
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => '你没有权限',
|
|
]);
|
|
}
|
|
|
|
try {
|
|
$deleted = TrustedLoginDeviceService::clearAll(new RedisClient());
|
|
error_log('Trusted login devices cleared by admin #' . (int) Auth::getUser()->id . ': ' . $deleted);
|
|
|
|
return $response->withJson([
|
|
'ret' => 1,
|
|
'msg' => $deleted > 0
|
|
? '已清除全部用户 ' . $deleted . ' 个浏览器登录认证记录'
|
|
: '当前没有需要清除的浏览器登录认证记录',
|
|
'count' => $deleted,
|
|
]);
|
|
} catch (\Throwable $exception) {
|
|
error_log('Trusted login device cleanup failed: ' . $exception->getMessage());
|
|
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => '浏览器登录认证记录清理失败,请检查 Redis 和服务器日志',
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function clearLoginSessions($request, $response, $args)
|
|
{
|
|
if (!$this->isSuperAdmin()) {
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => '你没有权限',
|
|
]);
|
|
}
|
|
|
|
$admin = Auth::getUser();
|
|
try {
|
|
$affected = LoginSessionRevocationService::revokeAll();
|
|
error_log('All login sessions revoked by admin #' . (int) $admin->id . ': ' . $affected);
|
|
|
|
try {
|
|
Auth::logout();
|
|
} catch (\Throwable $logoutException) {
|
|
error_log('Current admin logout after global revocation failed: ' . $logoutException->getMessage());
|
|
}
|
|
|
|
return $response->withJson([
|
|
'ret' => 1,
|
|
'msg' => '已注销全部 ' . $affected . ' 个用户的登录状态,请重新登录',
|
|
'count' => $affected,
|
|
'logout_current' => true,
|
|
]);
|
|
} catch (\Throwable $exception) {
|
|
error_log('Global login session revocation failed: ' . $exception->getMessage());
|
|
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => '全部用户登录状态清除失败,请确认已导入登录会话升级 SQL',
|
|
]);
|
|
}
|
|
}
|
|
|
|
private function isSuperAdmin()
|
|
{
|
|
return AdminPermission::isSuperAdmin();
|
|
}
|
|
|
|
private function getRequestPayload($request)
|
|
{
|
|
$payload = $request->getParsedBody();
|
|
if (is_array($payload) && !empty($payload)) {
|
|
return $payload;
|
|
}
|
|
|
|
$rawBody = (string) $request->getBody();
|
|
if ($rawBody === '') {
|
|
return [];
|
|
}
|
|
|
|
$decoded = json_decode($rawBody, true);
|
|
return is_array($decoded) ? $decoded : [];
|
|
}
|
|
|
|
private function normalizeConfigValue($value, $defaultValue, $key = '')
|
|
{
|
|
if (is_array($value)) {
|
|
return (string) $defaultValue;
|
|
}
|
|
|
|
$value = trim((string) $value);
|
|
|
|
if (strpos($key, 'theme_') === 0) {
|
|
return $this->normalizeThemeValue($value);
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
|
|
private function normalizeThemeValue($value)
|
|
{
|
|
$themes = $this->getAvailableThemes();
|
|
|
|
if (in_array($value, $themes, true)) {
|
|
return $value;
|
|
}
|
|
|
|
return in_array('venix', $themes, true) ? 'venix' : (string) reset($themes);
|
|
}
|
|
|
|
private function getAvailableThemes()
|
|
{
|
|
return ['venix'];
|
|
}
|
|
|
|
private function isDomainSiteConfigKey($key)
|
|
{
|
|
return preg_match('/^(host|appname|theme)_[1-7]$/', $key) === 1;
|
|
}
|
|
}
|