2016-10-07 02:41:04 +00:00
|
|
|
<?php
|
2016-12-16 20:49:07 +00:00
|
|
|
namespace AzuraCast;
|
2016-10-07 02:41:04 +00:00
|
|
|
|
2018-02-01 11:49:40 +00:00
|
|
|
use App\Auth;
|
2017-05-21 04:27:34 +00:00
|
|
|
use Entity;
|
2016-10-07 02:41:04 +00:00
|
|
|
|
|
|
|
class Customization
|
|
|
|
{
|
2017-07-19 00:09:29 +00:00
|
|
|
/** @var array */
|
|
|
|
protected $app_settings;
|
2016-10-07 02:41:04 +00:00
|
|
|
|
2018-02-01 11:49:40 +00:00
|
|
|
/** @var Entity\User */
|
|
|
|
protected $user = null;
|
2016-10-07 02:41:04 +00:00
|
|
|
|
2017-07-19 00:09:29 +00:00
|
|
|
/** @var Entity\Repository\SettingsRepository */
|
|
|
|
protected $settings_repo;
|
2016-10-07 02:41:04 +00:00
|
|
|
|
2018-02-01 11:49:40 +00:00
|
|
|
public function __construct($app_settings, Entity\Repository\SettingsRepository $settings_repo)
|
2016-10-07 02:41:04 +00:00
|
|
|
{
|
2017-07-19 00:09:29 +00:00
|
|
|
$this->app_settings = $app_settings;
|
|
|
|
$this->settings_repo = $settings_repo;
|
2016-10-07 02:41:04 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 11:49:40 +00:00
|
|
|
/**
|
|
|
|
* Initialize timezone and locale settings for the current user.
|
|
|
|
*/
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
if (!APP_IS_COMMAND_LINE || APP_TESTING_MODE) {
|
|
|
|
// Set time zone.
|
|
|
|
date_default_timezone_set($this->getTimeZone());
|
|
|
|
|
|
|
|
// Localization
|
|
|
|
$locale = $this->getLocale();
|
|
|
|
putenv("LANG=" . $locale);
|
|
|
|
setlocale(LC_ALL, $locale);
|
|
|
|
|
|
|
|
$locale_domain = 'default';
|
|
|
|
bindtextdomain($locale_domain, APP_INCLUDE_BASE . '/locale');
|
|
|
|
bind_textdomain_codeset($locale_domain, 'UTF-8');
|
|
|
|
textdomain($locale_domain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the currently active/logged in user.
|
|
|
|
*
|
|
|
|
* @param Entity\User $user
|
|
|
|
*/
|
|
|
|
public function setUser(\Entity\User $user = null)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
2016-10-07 02:41:04 +00:00
|
|
|
/**
|
|
|
|
* Get the user's custom time zone or the system default.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTimeZone()
|
|
|
|
{
|
2017-08-17 18:28:48 +00:00
|
|
|
if ($this->user !== null && !empty($this->user->getTimezone())) {
|
|
|
|
return $this->user->getTimezone();
|
2017-01-24 00:35:16 +00:00
|
|
|
}
|
2018-02-01 11:49:40 +00:00
|
|
|
|
|
|
|
return date_default_timezone_get();
|
2016-10-07 02:41:04 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 18:04:58 +00:00
|
|
|
/**
|
|
|
|
* Format the given UNIX timestamp into a locale-friendly time.
|
|
|
|
*
|
|
|
|
* @param $timestamp
|
|
|
|
* @param bool $use_gmt
|
|
|
|
* @param bool $show_timezone_abbr
|
|
|
|
* @return string Formatted time for presentation.
|
|
|
|
*/
|
|
|
|
public function formatTime($timestamp = null, $use_gmt = false, $show_timezone_abbr = false)
|
|
|
|
{
|
|
|
|
$timestamp = $timestamp ?? time();
|
|
|
|
|
|
|
|
$time_formats = $this->app_settings['time_formats'];
|
|
|
|
$locale = $this->getLocale();
|
|
|
|
|
|
|
|
$time_format = $time_formats[$locale] ?? $time_formats['default'];
|
|
|
|
|
|
|
|
if ($show_timezone_abbr) {
|
|
|
|
$time_format .= ($use_gmt) ? ' UTC' : ' %Z';
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($use_gmt) ? gmstrftime($time_format, $timestamp) : strftime($time_format, $timestamp);
|
|
|
|
}
|
|
|
|
|
2016-10-07 02:41:04 +00:00
|
|
|
/**
|
|
|
|
* Return the user-customized, browser-specified or system default locale.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLocale()
|
|
|
|
{
|
2017-12-14 18:04:58 +00:00
|
|
|
static $locale = null;
|
|
|
|
|
|
|
|
if ($locale === null) {
|
|
|
|
$supported_locales = $this->app_settings['locale']['supported'];
|
2016-10-07 02:41:04 +00:00
|
|
|
|
2017-12-14 18:04:58 +00:00
|
|
|
// Prefer user-based profile locale.
|
|
|
|
if ($this->user !== null && !empty($this->user->getLocale()) && $this->user->getLocale() !== 'default') {
|
|
|
|
if (isset($supported_locales[$this->user->getLocale()])) {
|
|
|
|
$locale = $this->user->getLocale();
|
|
|
|
}
|
2017-01-24 00:35:16 +00:00
|
|
|
}
|
2016-10-07 02:41:04 +00:00
|
|
|
|
2017-12-14 18:04:58 +00:00
|
|
|
// Attempt to load from browser headers.
|
|
|
|
if (!$locale) {
|
|
|
|
$browser_locale = \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
|
2016-10-07 02:41:04 +00:00
|
|
|
|
2017-12-14 18:04:58 +00:00
|
|
|
foreach ($supported_locales as $lang_code => $lang_name) {
|
|
|
|
if (strcmp(substr($browser_locale, 0, 2), substr($lang_code, 0, 2)) == 0) {
|
|
|
|
$locale = $lang_code;
|
|
|
|
break;
|
|
|
|
}
|
2016-10-07 02:41:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 18:04:58 +00:00
|
|
|
// Default to system option.
|
|
|
|
if (!$locale) {
|
|
|
|
$locale = $this->app_settings['locale']['default'];
|
|
|
|
}
|
2017-01-24 00:35:16 +00:00
|
|
|
}
|
2016-10-07 02:41:04 +00:00
|
|
|
|
|
|
|
return $locale;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the user-customized or system default theme.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTheme()
|
|
|
|
{
|
2017-08-17 18:28:48 +00:00
|
|
|
if ($this->user !== null && !empty($this->user->getTheme())) {
|
2017-07-19 00:09:29 +00:00
|
|
|
$available_themes = $this->app_settings['themes']['available'];
|
2017-08-17 18:28:48 +00:00
|
|
|
if (isset($available_themes[$this->user->getTheme()])) {
|
|
|
|
return $this->user->getTheme();
|
2017-01-24 00:35:16 +00:00
|
|
|
}
|
2016-10-07 02:41:04 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 00:09:29 +00:00
|
|
|
return $this->app_settings['themes']['default'];
|
2016-10-07 02:41:04 +00:00
|
|
|
}
|
2017-05-21 04:27:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the instance name for this AzuraCast instance.
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getInstanceName()
|
|
|
|
{
|
|
|
|
static $instance_name;
|
|
|
|
|
|
|
|
if ($instance_name === null) {
|
2017-07-19 00:09:29 +00:00
|
|
|
$instance_name = $this->settings_repo->getSetting('instance_name', '');
|
2017-05-21 04:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $instance_name;
|
|
|
|
}
|
2017-09-20 02:10:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the theme name to be used in public (non-logged-in) pages.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPublicTheme()
|
|
|
|
{
|
|
|
|
return $this->settings_repo->getSetting('public_theme', $this->app_settings['themes']['default']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the administrator-supplied custom CSS for public (minimal layout) pages, if specified.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCustomPublicCss()
|
|
|
|
{
|
|
|
|
return (string)$this->settings_repo->getSetting('custom_css_public', '');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the administrator-supplied custom CSS for internal (full layout) pages, if specified.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCustomInternalCss()
|
|
|
|
{
|
|
|
|
return (string)$this->settings_repo->getSetting('custom_css_internal', '');
|
|
|
|
}
|
2016-10-07 02:41:04 +00:00
|
|
|
}
|