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/Controllers/BaseController.php

276 lines
7.8 KiB
PHTML

<?php
namespace App\Controllers;
use App\Models\Configurations;
use App\Services\Auth;
use App\Services\Config;
use App\Services\PublicLocale;
use App\Services\RedisClient;
use App\Services\View;
use App\Services\UserCopy\UserCopyViewRuntime;
use App\Services\UserCopy\UserCopyCatalog;
use App\Utils\Cookie;
/**
* BaseController
*/
#[\AllowDynamicProperties]
class BaseController
{
public $view;
public $smarty;
protected $viewTheme = null;
protected $language = 'false';
protected $publicLocale = PublicLocale::CHINESE;
private ?UserCopyCatalog $publicCopyCatalog = null;
private ?string $publicCopyPage = null;
public function construct__()
{
}
public function smarty()
{
$this->smarty = View::getSmarty($this->viewTheme);
return $this->smarty;
}
public function view()
{
return $this->smarty();
}
protected function initializePublicLocale()
{
$query = is_array($_GET) ? $_GET : [];
$cookies = is_array($_COOKIE) ? $_COOKIE : [];
$requestedLocale = PublicLocale::requestedLocale($query);
if ($requestedLocale !== null) {
Cookie::set([
PublicLocale::COOKIE_NAME => $requestedLocale,
], time() + PublicLocale::COOKIE_TTL);
$cookies[PublicLocale::COOKIE_NAME] = $requestedLocale;
}
$this->publicLocale = PublicLocale::resolve(
$query,
$cookies,
isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? (string) $_SERVER['HTTP_ACCEPT_LANGUAGE'] : ''
);
$this->language = PublicLocale::templateLanguage($this->publicLocale);
}
protected function publicView(?string $copyPage = null)
{
$requestUri = isset($_SERVER['REQUEST_URI']) ? (string) $_SERVER['REQUEST_URI'] : '/';
$smarty = $this->view()->assign(
PublicLocale::viewData($this->publicLocale, $requestUri)
);
$this->publicCopyCatalog = UserCopyViewRuntime::production()->bind(
$smarty,
$this->publicLocale,
$requestUri,
$copyPage
);
$this->publicCopyPage = $copyPage;
return $smarty;
}
/** @param array<string,mixed> $parameters */
protected function publicCopy(
string $page,
string $key,
array $parameters = []
): string {
if (!$this->publicCopyCatalog instanceof UserCopyCatalog
|| $this->publicCopyPage !== $page) {
$requestUri = isset($_SERVER['REQUEST_URI'])
? (string) $_SERVER['REQUEST_URI'] : '/';
$this->publicCopyCatalog = UserCopyViewRuntime::production()->bind(
$this->view(),
$this->publicLocale,
$requestUri,
$page
);
$this->publicCopyPage = $page;
}
return $this->publicCopyCatalog->text($key, $parameters);
}
/**
* @param $response
* @param $res
* @return mixed
*/
public function echoJson($response, $res)
{
return $response->getBody()->write(json_encode($res));
}
protected function setViewTheme($theme)
{
$theme = trim((string) $theme);
if ($theme !== '' && preg_match('/^[A-Za-z0-9_-]+$/', $theme) && is_dir(BASE_PATH . '/resources/views/' . $theme)) {
$this->viewTheme = $theme;
return;
}
$this->viewTheme = Config::get('theme', 'venix');
}
protected function getDomainConfigurations()
{
$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);
if (!is_array($data)) {
return [];
}
return $data;
}
protected function applyDomainTheme($configurations)
{
if ($this->getDomainConfigIndex($configurations) === null) {
return;
}
$theme = $this->getDomainConfigValue($configurations, 'theme', Config::get('theme', 'venix'));
$this->setViewTheme($theme);
}
protected function getDomainAppName($configurations, $defaultAppName)
{
return $this->getDomainConfigValue($configurations, 'appname', $defaultAppName);
}
protected function defaultIfBlank($value, $defaultValue)
{
$value = trim((string) $value);
return $value === '' ? (string) $defaultValue : $value;
}
protected function getDomainConfigValue($configurations, $prefix, $defaultValue = '')
{
$index = $this->getDomainConfigIndex($configurations);
if ($index === null) {
return $defaultValue;
}
$value = $this->getConfigurationValue($configurations, $prefix . '_' . $index, '');
return $value === '' ? $defaultValue : $value;
}
protected function getDomainConfigIndex($configurations)
{
$requestHost = $this->normalizeHost(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
if ($requestHost === '') {
return null;
}
for ($i = 1; $i <= 7; $i++) {
$hosts = preg_split('/[\s,]+/', $this->getConfigurationValue($configurations, 'host_' . $i, ''));
foreach ($hosts as $host) {
if ($this->normalizeHost($host) === $requestHost) {
return $i;
}
}
}
return null;
}
protected function domainMatchesConfigHost($configurations, $hostKey, $domain = null)
{
$requestHost = $this->normalizeHost($domain === null && isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $domain);
if ($requestHost === '') {
return false;
}
$hosts = preg_split('/[\s,]+/', $this->getConfigurationValue($configurations, $hostKey, ''));
foreach ($hosts as $host) {
if ($this->normalizeHost($host) === $requestHost) {
return true;
}
}
return false;
}
protected function getConfigurationValue($configurations, $key, $defaultValue = '')
{
if (is_object($configurations) && method_exists($configurations, 'get')) {
$configuration = $configurations->get($key);
if ($configuration !== null) {
return (string) $configuration->value;
}
}
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;
}
protected 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;
}
}