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.
170 lines
5.4 KiB
PHP
170 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Configurations;
|
|
use App\Services\Cloud\CloudUserIsoFeature;
|
|
use App\Services\Cloud\CloudVpcFeature;
|
|
use Smarty;
|
|
|
|
class View
|
|
{
|
|
public static function getSmarty($themeOverride = null)
|
|
{
|
|
$smarty=new smarty(); //实例化smarty
|
|
|
|
if ($themeOverride !== null) {
|
|
$theme = $themeOverride;
|
|
} else {
|
|
$theme = self::getDomainTheme();
|
|
|
|
if ($theme === null) {
|
|
$user = Auth::getUser();
|
|
|
|
if ($user->isLogin) {
|
|
$theme=$user->theme;
|
|
} else {
|
|
$theme=Config::get('theme');
|
|
}
|
|
}
|
|
}
|
|
|
|
$theme = self::normalizeTheme($theme);
|
|
$smarty->settemplatedir(BASE_PATH.'/resources/views/'.$theme.'/'); //设置模板文件存放目录
|
|
$smarty->setcompiledir(BASE_PATH.'/storage/framework/smarty/compile/'); //设置生成文件存放目录
|
|
$smarty->setcachedir(BASE_PATH.'/storage/framework/smarty/cache/'); //设置缓存文件存放目录
|
|
self::registerLegacyModifiers($smarty);
|
|
// add config
|
|
$user = Auth::getUser();
|
|
$adminCanManage = AdminPermission::isSuperAdmin();
|
|
$publicConfig = Config::getPublicConfig();
|
|
$smarty->assign('config', $publicConfig);
|
|
$smarty->assign('appName', $publicConfig['appName']);
|
|
$smarty->assign('user', $user);
|
|
$smarty->assign('admin_can_manage', $adminCanManage);
|
|
$smarty->assign('admin_csrf_token', $adminCanManage ? AdminCsrf::token() : '');
|
|
$smarty->assign(
|
|
'admin_display_user_name',
|
|
is_object($user) && isset($user->user_name) ? AdminDataMasker::account($user->user_name) : ''
|
|
);
|
|
$smarty->assign('user_iso_enabled', is_object($user) && $user->isLogin
|
|
? CloudUserIsoFeature::enabled() : false);
|
|
$smarty->assign('vpc_enabled', is_object($user) && $user->isLogin
|
|
? CloudVpcFeature::enabled() : false);
|
|
return $smarty;
|
|
}
|
|
|
|
private static function registerLegacyModifiers($smarty)
|
|
{
|
|
$smarty->registerPlugin('modifier', 'urlencode', 'urlencode');
|
|
$smarty->registerPlugin('modifier', 'number_format', 'number_format');
|
|
$smarty->registerPlugin('modifier', 'date', 'date');
|
|
}
|
|
|
|
private static function getDomainTheme()
|
|
{
|
|
$configurations = self::getDomainConfigurations();
|
|
$index = self::getDomainConfigIndex($configurations);
|
|
|
|
if ($index === null) {
|
|
return null;
|
|
}
|
|
|
|
$theme = self::getConfigurationValue($configurations, 'theme_' . $index, '');
|
|
return $theme === '' ? null : $theme;
|
|
}
|
|
|
|
private static function getDomainConfigurations()
|
|
{
|
|
try {
|
|
$redis = new RedisClient();
|
|
$json = $redis->get('domain_site_configs', 60, function () {
|
|
$keys = [];
|
|
$data = [];
|
|
|
|
for ($i = 1; $i <= 7; $i++) {
|
|
$keys[] = 'host_' . $i;
|
|
$keys[] = 'appname_' . $i;
|
|
$keys[] = 'theme_' . $i;
|
|
|
|
$data['host_' . $i] = '';
|
|
$data['appname_' . $i] = '';
|
|
$data['theme_' . $i] = 'venix';
|
|
}
|
|
|
|
$configurations = Configurations::whereIn('name', $keys)->get();
|
|
|
|
foreach ($configurations as $configuration) {
|
|
$data[$configuration->name] = (string) $configuration->value;
|
|
}
|
|
|
|
return json_encode($data);
|
|
});
|
|
|
|
$data = json_decode((string) $json, true);
|
|
return is_array($data) ? $data : [];
|
|
} catch (\Exception $exception) {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
private static function getDomainConfigIndex($configurations)
|
|
{
|
|
$requestHost = self::normalizeHost(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
|
|
|
|
if ($requestHost === '') {
|
|
return null;
|
|
}
|
|
|
|
for ($i = 1; $i <= 7; $i++) {
|
|
$hosts = preg_split('/[\s,]+/', self::getConfigurationValue($configurations, 'host_' . $i, ''));
|
|
foreach ($hosts as $host) {
|
|
if (self::normalizeHost($host) === $requestHost) {
|
|
return $i;
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private static function getConfigurationValue($configurations, $key, $defaultValue = '')
|
|
{
|
|
if (isset($configurations[$key])) {
|
|
if (is_object($configurations[$key]) && isset($configurations[$key]->value)) {
|
|
return (string) $configurations[$key]->value;
|
|
}
|
|
return (string) $configurations[$key];
|
|
}
|
|
|
|
return (string) $defaultValue;
|
|
}
|
|
|
|
private static function normalizeTheme($theme)
|
|
{
|
|
return 'venix';
|
|
}
|
|
|
|
private static function normalizeHost($host)
|
|
{
|
|
$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;
|
|
}
|
|
}
|