AzuraCast/src/Customization.php

273 lines
7.5 KiB
PHP
Raw Normal View History

<?php
namespace App;
use App\Entity;
use App\Http\ServerRequest;
use App\Service\NChan;
use Gettext\Translator;
2019-09-04 18:00:51 +00:00
use Locale;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
class Customization
{
public const DEFAULT_TIMEZONE = 'UTC';
public const DEFAULT_LOCALE = 'en_US.UTF-8';
2019-03-14 01:49:19 +00:00
public const DEFAULT_THEME = 'light';
protected ?Entity\User $user = null;
protected Entity\Repository\SettingsRepository $settingsRepo;
protected ?string $locale = null;
public function __construct(
Entity\Repository\SettingsRepository $settingsRepo,
ServerRequestInterface $request
) {
$this->settingsRepo = $settingsRepo;
2019-09-04 18:00:51 +00:00
$this->locale = $this->initLocale($request);
// Register current user
$this->user = $request->getAttribute(ServerRequest::ATTR_USER);
// Set up the PHP translator
$translator = new Translator();
$locale_base = Settings::getInstance()->getBaseDirectory() . '/resources/locale/compiled';
2019-09-04 18:00:51 +00:00
$locale_path = $locale_base . '/' . $this->locale . '.php';
if (file_exists($locale_path)) {
$translator->loadTranslations($locale_path);
}
$translator->register();
// Register translation superglobal functions
putenv('LANG=' . $this->locale);
2019-09-04 18:00:51 +00:00
setlocale(LC_ALL, $this->locale);
}
/**
* Return the user-customized, browser-specified or system default locale.
*
* @param Request|null $request
*
* @return string|null
*/
protected function initLocale(?Request $request = null): ?string
{
$settings = Settings::getInstance();
if ($settings->isTesting()) {
return self::DEFAULT_LOCALE;
}
$supported_locales = $settings['locale']['supported'];
$try_locales = [];
// Prefer user-based profile locale.
if ($this->user !== null && !empty($this->user->getLocale()) && 'default' !== $this->user->getLocale()) {
$try_locales[] = $this->user->getLocale();
}
// Attempt to load from browser headers.
if ($request instanceof Request) {
$server_params = $request->getServerParams();
2019-09-04 18:00:51 +00:00
$browser_locale = Locale::acceptFromHttp($server_params['HTTP_ACCEPT_LANGUAGE'] ?? null);
if (!empty($browser_locale)) {
2019-09-04 18:00:51 +00:00
$try_locales[] = substr($browser_locale, 0, 5) . '.UTF-8';
}
}
// Attempt to load from environment variable.
$env_locale = getenv('LANG');
if (!empty($env_locale)) {
2019-09-04 18:00:51 +00:00
$try_locales[] = substr($env_locale, 0, 5) . '.UTF-8';
}
2019-09-04 18:00:51 +00:00
foreach ($try_locales as $exact_locale) {
// Prefer exact match.
if (isset($supported_locales[$exact_locale])) {
return $exact_locale;
}
// Use approximate match if available.
foreach ($supported_locales as $lang_code => $lang_name) {
if (strpos($exact_locale, substr($lang_code, 0, 2)) === 0) {
return $lang_code;
}
}
}
// Default to system option.
return self::DEFAULT_LOCALE;
}
2019-03-14 01:49:19 +00:00
/**
* @return string
*/
public function getLocale(): string
{
return $this->locale ?? self::DEFAULT_LOCALE;
}
/**
* @return string A shortened locale (minus .UTF-8) for use in Vue.
*/
public function getVueLocale(): string
{
2020-07-08 07:03:50 +00:00
return json_encode(substr($this->getLocale(), 0, 5), JSON_THROW_ON_ERROR);
}
/**
* Returns the user-customized or system default theme.
*
* @return string
*/
2020-07-08 07:03:50 +00:00
public function getTheme(): string
{
if ($this->user !== null && !empty($this->user->getTheme())) {
2019-03-14 01:49:19 +00:00
return $this->user->getTheme();
}
2019-03-14 01:49:19 +00:00
return self::DEFAULT_THEME;
}
/**
* Get the instance name for this AzuraCast instance.
*
* @return string|null
*/
2019-03-14 01:49:19 +00:00
public function getInstanceName(): ?string
{
static $instance_name;
if ($instance_name === null) {
$instance_name = $this->settingsRepo->getSetting(Entity\Settings::INSTANCE_NAME, '');
}
return $instance_name;
}
/**
* Get the theme name to be used in public (non-logged-in) pages.
*
* @return string
*/
2019-03-14 01:49:19 +00:00
public function getPublicTheme(): string
{
return $this->settingsRepo->getSetting(Entity\Settings::PUBLIC_THEME, self::DEFAULT_THEME);
}
/**
* Return the administrator-supplied custom CSS for public (minimal layout) pages, if specified.
*
* @return string
*/
2020-07-08 07:03:50 +00:00
public function getCustomPublicCss(): string
{
return (string)$this->settingsRepo->getSetting(Entity\Settings::CUSTOM_CSS_PUBLIC, '');
}
/**
* Return the administrator-supplied custom JS for public (minimal layout) pages, if specified.
*
* @return string
*/
2020-07-08 07:03:50 +00:00
public function getCustomPublicJs(): string
{
return (string)$this->settingsRepo->getSetting(Entity\Settings::CUSTOM_JS_PUBLIC, '');
}
/**
* Return the administrator-supplied custom CSS for internal (full layout) pages, if specified.
*
* @return string
*/
2020-07-08 07:03:50 +00:00
public function getCustomInternalCss(): string
{
return (string)$this->settingsRepo->getSetting(Entity\Settings::CUSTOM_CSS_INTERNAL, '');
}
/**
* Return whether to show or hide album art on public pages.
*
* @return bool
*/
public function hideAlbumArt(): bool
{
return (bool)$this->settingsRepo->getSetting(Entity\Settings::HIDE_ALBUM_ART, false);
}
/**
* Return the calculated page title given branding settings and the application environment.
*
* @param string|null $title
*
* @return string
*/
public function getPageTitle($title = null): string
{
$settings = Settings::getInstance();
if (!$this->hideProductName()) {
if ($title) {
$title .= ' - ' . $settings[Settings::APP_NAME];
} else {
$title = $settings[Settings::APP_NAME];
}
}
if (!$settings->isProduction()) {
$title = '(' . ucfirst($settings[Settings::APP_ENV]) . ') ' . $title;
}
return $title;
}
/**
2019-09-04 18:00:51 +00:00
* Return whether to show or hide the AzuraCast name from public-facing pages.
*
* @return bool
*/
2019-09-04 18:00:51 +00:00
public function hideProductName(): bool
{
return (bool)$this->settingsRepo->getSetting(Entity\Settings::HIDE_PRODUCT_NAME, false);
}
/**
2019-09-04 18:00:51 +00:00
* @return bool
*/
2019-09-04 18:00:51 +00:00
public function useWebSocketsForNowPlaying(): bool
{
2019-09-04 18:00:51 +00:00
if (!NChan::isSupported()) {
return false;
}
return (bool)$this->settingsRepo->getSetting(Entity\Settings::NOWPLAYING_USE_WEBSOCKETS, false);
}
/**
* Initialize the CLI without instantiating the Doctrine DB stack (allowing cache clearing, etc.).
*/
public static function initCli(): void
{
$translator = new Translator();
/*
* TODO: Load translations from environment locale.
$locale_base = Settings::getInstance()->getBaseDirectory() . '/resources/locale/compiled';
$locale_path = $locale_base . '/' . $this->locale . '.php';
if (file_exists($locale_path)) {
$translator->loadTranslations($locale_path);
}
*/
$translator->register();
}
}