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.
109 lines
3.4 KiB
PHP
109 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Configurations;
|
|
|
|
/**
|
|
* Captcha and email-verification settings stored in the admin configurations table.
|
|
* Values are loaded once per PHP request; the PHP configuration file is not read.
|
|
*/
|
|
class CaptchaConfig
|
|
{
|
|
private const DEFAULTS = [
|
|
'captch_reg' => 'false',
|
|
'captch_login' => 'false',
|
|
'captch_respass' => 'true',
|
|
'enable_email_verify' => 'true',
|
|
'email_verify_ttl' => '900',
|
|
'email_verify_iplimit' => '5',
|
|
];
|
|
|
|
private const NUMBER_LIMITS = [
|
|
'email_verify_ttl' => 86400,
|
|
'email_verify_iplimit' => 10000,
|
|
];
|
|
|
|
private static $cachedValues;
|
|
|
|
public static function defaults(): array
|
|
{
|
|
return self::DEFAULTS;
|
|
}
|
|
|
|
public static function get(string $key): string
|
|
{
|
|
if (!array_key_exists($key, self::DEFAULTS)) {
|
|
throw new \InvalidArgumentException('Unknown verification setting: ' . $key);
|
|
}
|
|
|
|
if (self::$cachedValues === null) {
|
|
self::$cachedValues = self::loadValues();
|
|
}
|
|
|
|
return self::$cachedValues[$key];
|
|
}
|
|
|
|
public static function clearCache(): void
|
|
{
|
|
self::$cachedValues = null;
|
|
}
|
|
|
|
public static function validateBoolean($value): string
|
|
{
|
|
if (!is_string($value) || !in_array($value, ['true', 'false'], true)) {
|
|
throw new \InvalidArgumentException('验证码开关只能选择开启或关闭');
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
|
|
public static function validatePositiveInt($value, string $key): string
|
|
{
|
|
if (!isset(self::NUMBER_LIMITS[$key])) {
|
|
throw new \InvalidArgumentException('未知的验证码数值设置');
|
|
}
|
|
|
|
$raw = is_string($value) || is_int($value) ? trim((string) $value) : '';
|
|
$limit = self::NUMBER_LIMITS[$key];
|
|
if (!preg_match('/^[0-9]+$/D', $raw) || (int) $raw < 1 || (int) $raw > $limit) {
|
|
$label = $key === 'email_verify_ttl' ? '邮箱验证码有效期' : '单 IP 验证码请求次数';
|
|
throw new \InvalidArgumentException($label . '必须是 1 至 ' . $limit . ' 的整数');
|
|
}
|
|
|
|
return (string) (int) $raw;
|
|
}
|
|
|
|
private static function loadValues(): array
|
|
{
|
|
$values = self::DEFAULTS;
|
|
try {
|
|
$rows = Configurations::whereIn('name', array_keys(self::DEFAULTS))->get();
|
|
foreach ($rows as $row) {
|
|
$key = (string) $row->name;
|
|
if (!array_key_exists($key, self::DEFAULTS)) {
|
|
continue;
|
|
}
|
|
|
|
$value = (string) $row->value;
|
|
if (isset(self::NUMBER_LIMITS[$key])) {
|
|
try {
|
|
$values[$key] = self::validatePositiveInt($value, $key);
|
|
} catch (\InvalidArgumentException $exception) {
|
|
// Invalid historical rows must not disable the limit.
|
|
}
|
|
} elseif ($value === 'true' || $value === 'false') {
|
|
$values[$key] = $value;
|
|
} else {
|
|
// A corrupt switch must never silently disable a captcha.
|
|
$values[$key] = 'true';
|
|
}
|
|
}
|
|
} catch (\Throwable $exception) {
|
|
throw new \RuntimeException('Verification settings could not be loaded.', 0, $exception);
|
|
}
|
|
|
|
return $values;
|
|
}
|
|
}
|