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.
virt/web/app/Services/SiteName.php

129 lines
3.9 KiB
PHTML

<?php
namespace App\Services;
use App\Models\Configurations;
class SiteName
{
private const FALLBACK_NAME = '站点';
public static function current(): string
{
static $mappings = null;
static $defaultName = null;
$host = isset($_SERVER['HTTP_HOST']) ? (string) $_SERVER['HTTP_HOST'] : '';
if ($host !== '' && $mappings === null) {
$mappings = self::domainConfigurations();
}
if ($defaultName === null) {
$defaultName = self::defaultAppName();
}
return self::resolveFromConfig($host, $defaultName, $mappings ?? []);
}
public static function resolveFromConfig($host, $defaultName, array $mappings): string
{
$requestHost = self::normalizeHost($host);
$defaultName = trim((string) $defaultName);
$fallbackName = $defaultName !== '' ? $defaultName : self::FALLBACK_NAME;
if ($requestHost !== '') {
for ($i = 1; $i <= 7; $i++) {
$hosts = preg_split('/[\s,]+/', (string) ($mappings['host_' . $i] ?? ''));
foreach ($hosts as $configuredHost) {
if (self::normalizeHost($configuredHost) === $requestHost) {
$name = trim((string) ($mappings['appname_' . $i] ?? ''));
return $name !== '' ? $name : $fallbackName;
}
}
}
}
return $fallbackName;
}
private static function domainConfigurations(): array
{
try {
$json = (new RedisClient())->get('domain_site_configs', 60, function () {
return json_encode(self::readDomainConfigurations());
});
$configurations = json_decode((string) $json, true);
if (is_array($configurations)) {
return $configurations;
}
} catch (\Throwable $exception) {
// Read the database directly when Redis is unavailable.
}
try {
return self::readDomainConfigurations();
} catch (\Throwable $exception) {
return [];
}
}
private static function readDomainConfigurations(): array
{
$keys = [];
$configurations = [];
for ($i = 1; $i <= 7; $i++) {
$keys[] = 'host_' . $i;
$keys[] = 'appname_' . $i;
$keys[] = 'theme_' . $i;
$configurations['host_' . $i] = '';
$configurations['appname_' . $i] = '';
$configurations['theme_' . $i] = 'venix';
}
foreach (Configurations::whereIn('name', $keys)->get() as $configuration) {
$configurations[$configuration->name] = (string) $configuration->value;
}
return $configurations;
}
private static function defaultAppName(): string
{
try {
$name = (new RedisClient())->get('default_app_name', 60, function () {
return (string) Configurations::where('name', 'default_app_name')->value('value');
});
return (string) $name;
} catch (\Throwable $exception) {
try {
return (string) Configurations::where('name', 'default_app_name')->value('value');
} catch (\Throwable $dbException) {
return '';
}
}
}
private static function normalizeHost($host): string
{
$host = trim(strtolower((string) $host));
if ($host === '') {
return '';
}
if (strpos($host, '://') !== false) {
$parsedHost = parse_url($host, PHP_URL_HOST);
$host = $parsedHost === null ? $host : $parsedHost;
}
$host = trim($host, " \t\n\r\0\x0B/");
if (strpos($host, ':') !== false && substr_count($host, ':') === 1) {
$host = explode(':', $host, 2)[0];
}
return $host;
}
}