Improved some AppData handling

This commit is contained in:
the-djmaze 2024-03-07 01:16:58 +01:00
parent 565e8f0fb7
commit 8e6f35bfad
8 changed files with 84 additions and 81 deletions

View file

@ -1,6 +1,6 @@
import 'External/ko';
import { Settings, SettingsGet } from 'Common/Globals';
import { SettingsGet, SettingsAdmin } from 'Common/Globals';
import { initThemes } from 'Stores/Theme';
import Remote from 'Remote/Admin/Fetch';
@ -23,7 +23,8 @@ export class AdminApp extends AbstractApp {
}
start() {
if (!Settings.app('adminAllowed')) {
// if (!Settings.app('adminAllowed')) {
if (!SettingsAdmin('allowed')) {
rl.route.root();
setTimeout(() => location.href = '/', 1);
} else if (SettingsGet('Auth')) {

View file

@ -15,6 +15,7 @@ export const
Settings = rl.settings,
SettingsGet = Settings.get,
SettingsAdmin = name => (SettingsGet('Admin') || {})[name],
SettingsCapa = name => name && !!(SettingsGet('Capa') || {})[name],
dropdowns = [],

View file

@ -1,13 +1,13 @@
import { pInt } from 'Common/Utils';
import { doc, Settings } from 'Common/Globals';
import { doc, Settings, SettingsAdmin } from 'Common/Globals';
const
BASE = doc.location.pathname.replace(/\/+$/,'') + '/',
HASH_PREFIX = '#/',
adminPath = () => rl.adminArea() && !Settings.app('adminHost'),
adminPath = () => rl.adminArea() && !SettingsAdmin('host'),
prefix = () => BASE + '?' + (adminPath() ? Settings.app('adminPath') : '');
prefix = () => BASE + '?' + (adminPath() ? SettingsAdmin('path') : '');
export const
SUB_QUERY_PREFIX = '&q[]=',

View file

@ -7,7 +7,7 @@ import {
import { addObservablesTo, addSubscribablesTo, addComputablesTo } from 'External/ko';
import { SaveSettingStatus } from 'Common/Enums';
import { Settings, SettingsGet, SettingsCapa } from 'Common/Globals';
import { SettingsAdmin, SettingsGet, SettingsCapa } from 'Common/Globals';
import { translatorReload, convertLangName } from 'Common/Translator';
import { AbstractViewSettings } from 'Knoin/AbstractViews';
@ -24,11 +24,7 @@ export class AdminSettingsGeneral extends AbstractViewSettings {
super();
this.language = LanguageStore.language;
this.languages = LanguageStore.languages;
const aLanguagesAdmin = Settings.app('languagesAdmin');
this.languagesAdmin = ko.observableArray(isArray(aLanguagesAdmin) ? aLanguagesAdmin : []);
this.languageAdmin = ko.observable(SettingsGet('languageAdmin'));
this.languageAdmin = ko.observable(SettingsAdmin('language'));
this.theme = ThemeStore.theme;
this.themes = ThemeStore.themes;
@ -107,14 +103,19 @@ export class AdminSettingsGeneral extends AbstractViewSettings {
}
selectLanguage() {
showScreenPopup(LanguagesPopupView, [this.language, this.languages(), LanguageStore.userLanguage()]);
showScreenPopup(LanguagesPopupView, [
this.language,
LanguageStore.languages,
LanguageStore.userLanguage()
]);
}
selectLanguageAdmin() {
const aLanguagesAdmin = SettingsAdmin('languages');
showScreenPopup(LanguagesPopupView, [
this.languageAdmin,
this.languagesAdmin(),
SettingsGet('languageUsers')
isArray(aLanguagesAdmin) ? aLanguagesAdmin : [],
SettingsAdmin('clientLanguage')
]);
}
}

View file

@ -12,7 +12,7 @@ export const LanguageStore = {
const aLanguages = Settings.app('languages');
this.languages(isArray(aLanguages) ? aLanguages : []);
this.language(SettingsGet('language'));
this.userLanguage(SettingsGet('userLanguage'));
this.userLanguage(SettingsGet('clientLanguage'));
this.hourCycle(SettingsGet('hourCycle'));
}
}

View file

@ -610,69 +610,18 @@ class Actions
'title' => $oConfig->Get('webmail', 'title', 'SnappyMail Webmail'),
'loadingDescription' => $oConfig->Get('webmail', 'loading_description', 'SnappyMail'),
'Plugins' => array(),
'System' => \array_merge(
array(
'version' => APP_VERSION,
'token' => Utils::GetCsrfToken(),
'languages' => \SnappyMail\L10n::getLanguages(false),
'webPath' => \RainLoop\Utils::WebPath(),
'webVersionPath' => \RainLoop\Utils::WebVersionPath()
), $bAdmin ? array(
'adminHost' => '' !== $oConfig->Get('security', 'admin_panel_host', ''),
'adminPath' => $oConfig->Get('security', 'admin_panel_key', '') ?: 'admin',
'adminAllowed' => (bool)$oConfig->Get('security', 'allow_admin_panel', true)
) : array()
'System' => array(
'version' => APP_VERSION,
'token' => Utils::GetCsrfToken(),
'languages' => \SnappyMail\L10n::getLanguages(false),
'webPath' => \RainLoop\Utils::WebPath(),
'webVersionPath' => \RainLoop\Utils::WebVersionPath()
),
'allowLanguagesOnLogin' => (bool) $oConfig->Get('login', 'allow_languages_on_login', true)
);
$UserLanguageRaw = $this->detectUserLanguage($bAdmin);
if ($bAdmin) {
// $this->AdminAppData($aResult);
$aResult['Auth'] = $this->IsAdminLoggined(false);
if ($aResult['Auth']) {
$aResult['adminLogin'] = (string)$oConfig->Get('security', 'admin_login', '');
$aResult['adminTOTP'] = (string)$oConfig->Get('security', 'admin_totp', '');
$aResult['pluginsEnable'] = (bool)$oConfig->Get('plugins', 'enable', false);
$aResult['loginDefaultDomain'] = $oConfig->Get('login', 'default_domain', '');
$aResult['determineUserLanguage'] = (bool)$oConfig->Get('login', 'determine_user_language', true);
$aResult['determineUserDomain'] = (bool)$oConfig->Get('login', 'determine_user_domain', false);
$aResult['supportedPdoDrivers'] = \RainLoop\Pdo\Base::getAvailableDrivers();
$aResult['contactsEnable'] = (bool)$oConfig->Get('contacts', 'enable', false);
$aResult['contactsSync'] = (bool)$oConfig->Get('contacts', 'allow_sync', false);
$aResult['contactsPdoType'] = Providers\AddressBook\PdoAddressBook::validPdoType($oConfig->Get('contacts', 'type', 'sqlite'));
$aResult['contactsPdoDsn'] = (string)$oConfig->Get('contacts', 'pdo_dsn', '');
$aResult['contactsPdoType'] = (string)$oConfig->Get('contacts', 'type', '');
$aResult['contactsPdoUser'] = (string)$oConfig->Get('contacts', 'pdo_user', '');
$aResult['contactsPdoPassword'] = static::APP_DUMMY;
$aResult['contactsMySQLSSLCA'] = (string) $oConfig->Get('contacts', 'mysql_ssl_ca', '');
$aResult['contactsMySQLSSLVerify'] = !!$oConfig->Get('contacts', 'mysql_ssl_verify', true);
$aResult['contactsMySQLSSLCiphers'] = (string) $oConfig->Get('contacts', 'mysql_ssl_ciphers', '');
$aResult['contactsSQLiteGlobal'] = !!$oConfig->Get('contacts', 'sqlite_global', \is_file(APP_PRIVATE_DATA . '/AddressBook.sqlite'));
$aResult['contactsSuggestionsLimit'] = (int)$oConfig->Get('contacts', 'suggestions_limit', 20);
$aResult['faviconUrl'] = $oConfig->Get('webmail', 'favicon_url', '');
$aResult['weakPassword'] = \is_file(APP_PRIVATE_DATA.'admin_password.txt');
$aResult['System']['languagesAdmin'] = \SnappyMail\L10n::getLanguages(true);
$aResult['languageAdmin'] = $this->ValidateLanguage($oConfig->Get('webmail', 'language_admin', 'en'), '', true);
$aResult['languageUsers'] = $this->ValidateLanguage($UserLanguageRaw, '', true, true);
} else {
$passfile = APP_PRIVATE_DATA.'admin_password.txt';
$sPassword = $oConfig->Get('security', 'admin_password', '');
if (!$sPassword) {
$sPassword = \substr(\base64_encode(\random_bytes(16)), 0, 12);
Utils::saveFile($passfile, $sPassword . "\n");
// \chmod($passfile, 0600);
$oConfig->SetPassword(new \SnappyMail\SensitiveString($sPassword));
$oConfig->Save();
}
}
ActionsAdmin::AdminAppData($this, $aResult);
} else {
$oAccount = $this->getAccountFromToken(false);
if ($oAccount) {
@ -866,7 +815,7 @@ class Actions
$aResult['Theme'] = $this->GetTheme($bAdmin);
$aResult['language'] = $this->GetLanguage();
$aResult['userLanguage'] = $this->ValidateLanguage($UserLanguageRaw, '', false, true);
$aResult['clientLanguage'] = $this->ValidateLanguage($this->detectClientLanguage($bAdmin), '', false, true);
$aResult['PluginsLink'] = $this->oPlugins->HaveJs($bAdmin)
? 'Plugins/0/' . ($bAdmin ? 'Admin' : 'User') . '/' . $this->etag($this->oPlugins->Hash()) . '/'

View file

@ -13,14 +13,14 @@ trait Localization
$sLanguage = $oConfig->Get('webmail', 'language', 'en');
if ($oAccount = $this->getAccountFromToken(false)) {
if ($oConfig->Get('login', 'determine_user_language', true)) {
$sLanguage = $this->ValidateLanguage($this->detectUserLanguage($bAdmin), $sLanguage, false);
$sLanguage = $this->ValidateLanguage($this->detectClientLanguage($bAdmin), $sLanguage, false);
}
if ($oConfig->Get('webmail', 'allow_languages_on_settings', true)
&& ($oSettings = $this->SettingsProvider()->Load($oAccount))) {
$sLanguage = $oSettings->GetConf('language', $sLanguage);
}
} else if ($oConfig->Get('login', 'allow_languages_on_login', true) && $oConfig->Get('login', 'determine_user_language', true)) {
$sLanguage = $this->ValidateLanguage($this->detectUserLanguage($bAdmin), $sLanguage, false);
$sLanguage = $this->ValidateLanguage($this->detectClientLanguage($bAdmin), $sLanguage, false);
}
}
$sHookLanguage = $sLanguage = $this->ValidateLanguage($sLanguage, '', $bAdmin) ?: 'en';
@ -81,9 +81,8 @@ trait Localization
return \in_array($sResult, $aLang) ? $sResult : 'en';
}
public function detectUserLanguage(bool $bAdmin): string
public function detectClientLanguage(bool $bAdmin): string
{
$sResult = '';
$aLangs = $aList = array();
$sAcceptLang = \strtolower(\MailSo\Base\Http::GetServer('HTTP_ACCEPT_LANGUAGE', 'en'));
@ -99,12 +98,11 @@ trait Localization
foreach (\array_keys($aLangs) as $sLang) {
$sLang = $this->ValidateLanguage($sLang, '', $bAdmin, true);
if (!empty($sLang)) {
$sResult = $sLang;
break;
return $sLang;
}
}
return $sResult;
return '';
}
public function StaticI18N(string $sKey): string

View file

@ -415,4 +415,57 @@ class ActionsAdmin extends Actions
}
}
public static function AdminAppData(Actions $oActions, array &$aResult): void
{
$oConfig = $oActions->Config();
$aResult['Admin'] = [
'host' => '' !== $oConfig->Get('security', 'admin_panel_host', ''),
'path' => $oConfig->Get('security', 'admin_panel_key', '') ?: 'admin',
'allowed' => (bool)$oConfig->Get('security', 'allow_admin_panel', true)
];
$aResult['Auth'] = $oActions->IsAdminLoggined(false);
if ($aResult['Auth']) {
$aResult['adminLogin'] = (string)$oConfig->Get('security', 'admin_login', '');
$aResult['adminTOTP'] = (string)$oConfig->Get('security', 'admin_totp', '');
$aResult['pluginsEnable'] = (bool)$oConfig->Get('plugins', 'enable', false);
$aResult['loginDefaultDomain'] = $oConfig->Get('login', 'default_domain', '');
$aResult['determineUserLanguage'] = (bool)$oConfig->Get('login', 'determine_user_language', true);
$aResult['determineUserDomain'] = (bool)$oConfig->Get('login', 'determine_user_domain', false);
$aResult['supportedPdoDrivers'] = \RainLoop\Pdo\Base::getAvailableDrivers();
$aResult['contactsEnable'] = (bool)$oConfig->Get('contacts', 'enable', false);
$aResult['contactsSync'] = (bool)$oConfig->Get('contacts', 'allow_sync', false);
$aResult['contactsPdoType'] = Providers\AddressBook\PdoAddressBook::validPdoType($oConfig->Get('contacts', 'type', 'sqlite'));
$aResult['contactsPdoDsn'] = (string)$oConfig->Get('contacts', 'pdo_dsn', '');
$aResult['contactsPdoType'] = (string)$oConfig->Get('contacts', 'type', '');
$aResult['contactsPdoUser'] = (string)$oConfig->Get('contacts', 'pdo_user', '');
$aResult['contactsPdoPassword'] = static::APP_DUMMY;
$aResult['contactsMySQLSSLCA'] = (string) $oConfig->Get('contacts', 'mysql_ssl_ca', '');
$aResult['contactsMySQLSSLVerify'] = !!$oConfig->Get('contacts', 'mysql_ssl_verify', true);
$aResult['contactsMySQLSSLCiphers'] = (string) $oConfig->Get('contacts', 'mysql_ssl_ciphers', '');
$aResult['contactsSQLiteGlobal'] = !!$oConfig->Get('contacts', 'sqlite_global', \is_file(APP_PRIVATE_DATA . '/AddressBook.sqlite'));
$aResult['contactsSuggestionsLimit'] = (int)$oConfig->Get('contacts', 'suggestions_limit', 20);
$aResult['faviconUrl'] = $oConfig->Get('webmail', 'favicon_url', '');
$aResult['weakPassword'] = \is_file(APP_PRIVATE_DATA.'admin_password.txt');
$aResult['Admin']['language'] = $oActions->ValidateLanguage($oConfig->Get('webmail', 'language_admin', 'en'), '', true);
$aResult['Admin']['languages'] = \SnappyMail\L10n::getLanguages(true);
$aResult['Admin']['clientLanguage'] = $oActions->ValidateLanguage($oActions->detectClientLanguage(true), '', true, true);
} else {
$passfile = APP_PRIVATE_DATA.'admin_password.txt';
$sPassword = $oConfig->Get('security', 'admin_password', '');
if (!$sPassword) {
$sPassword = \substr(\base64_encode(\random_bytes(16)), 0, 12);
Utils::saveFile($passfile, $sPassword . "\n");
// \chmod($passfile, 0600);
$oConfig->SetPassword(new \SnappyMail\SensitiveString($sPassword));
$oConfig->Save();
}
}
}
}