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

207 lines
5.4 KiB
PHP

<?php
namespace App\Controllers;
use App\Models\Configurations;
use App\Services\Auth;
use App\Services\Config;
use App\Services\RedisClient;
use App\Services\View;
/**
* BaseController
*/
#[\AllowDynamicProperties]
class BaseController
{
public $view;
public $smarty;
protected $viewTheme = null;
public function construct__()
{
}
public function smarty()
{
$this->smarty = View::getSmarty($this->viewTheme);
return $this->smarty;
}
public function view()
{
return $this->smarty();
}
/**
* @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;
}
}