AzuraCast/src/Customization.php

322 lines
8.7 KiB
PHP
Raw Normal View History

<?php
namespace App;
use App\Entity;
use App\Service\NChan;
use Gettext\Translator;
use GuzzleHttp\Psr7\Uri;
2019-09-04 18:00:51 +00:00
use Locale;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\UriInterface;
2019-09-04 18:00:51 +00:00
use const LC_ALL;
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';
/** @var Entity\User|null */
protected ?Entity\User $user = null;
protected Entity\Repository\SettingsRepository $settingsRepo;
protected ?string $locale = null;
public function __construct(Entity\Repository\SettingsRepository $settingsRepo)
{
$this->settingsRepo = $settingsRepo;
2019-09-04 18:00:51 +00:00
}
/**
* Set the currently active/logged in user.
*
* @param Entity\User $user
*/
public function setUser(Entity\User $user = null): void
{
$this->user = $user;
}
/**
* Initialize timezone and locale settings for the current user, and write them as attributes to the request.
*
* @param Request|null $request
*
* @return Request|null
*/
public function init(?Request $request = null): ?Request
{
$this->locale = $this->initLocale($request);
// 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);
if ($request instanceof Request) {
$request = $request->withAttribute('locale', $this->locale);
}
return $request;
}
/**
* 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
{
return json_encode(substr($this->getLocale(), 0, 5));
}
/**
* Returns the user-customized or system default theme.
*
* @return string
*/
public function getTheme()
{
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
*/
public function getCustomPublicCss()
{
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
*/
public function getCustomPublicJs()
{
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
*/
public function getCustomInternalCss()
{
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 URL to use for songs with no specified album artwork, when artwork is displayed.
*
* @param Entity\Station|null $station
*
* @return UriInterface
*/
public function getDefaultAlbumArtUrl(?Entity\Station $station = null): UriInterface
{
if ($station instanceof Entity\Station) {
$stationCustomUrl = trim($station->getDefaultAlbumArtUrl());
if (!empty($stationCustomUrl)) {
return new Uri($stationCustomUrl);
}
}
$custom_url = trim($this->settingsRepo->getSetting(Entity\Settings::DEFAULT_ALBUM_ART_URL));
if (!empty($custom_url)) {
return new Uri($custom_url);
}
return new Uri('/static/img/generic_song.jpg');
}
/**
* 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();
}
}