From 8e6f35bfad95e874a214d0ef0145d9e38e8e041f Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Thu, 7 Mar 2024 01:16:58 +0100 Subject: [PATCH] Improved some AppData handling --- dev/App/Admin.js | 5 +- dev/Common/Globals.js | 1 + dev/Common/Links.js | 6 +- dev/Settings/Admin/General.js | 19 +++--- dev/Stores/Language.js | 2 +- .../0.0.0/app/libraries/RainLoop/Actions.php | 67 +++---------------- .../RainLoop/Actions/Localization.php | 12 ++-- .../app/libraries/RainLoop/ActionsAdmin.php | 53 +++++++++++++++ 8 files changed, 84 insertions(+), 81 deletions(-) diff --git a/dev/App/Admin.js b/dev/App/Admin.js index fb970bffc..c40e15598 100644 --- a/dev/App/Admin.js +++ b/dev/App/Admin.js @@ -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')) { diff --git a/dev/Common/Globals.js b/dev/Common/Globals.js index 4d86df9dc..105660c31 100644 --- a/dev/Common/Globals.js +++ b/dev/Common/Globals.js @@ -15,6 +15,7 @@ export const Settings = rl.settings, SettingsGet = Settings.get, + SettingsAdmin = name => (SettingsGet('Admin') || {})[name], SettingsCapa = name => name && !!(SettingsGet('Capa') || {})[name], dropdowns = [], diff --git a/dev/Common/Links.js b/dev/Common/Links.js index 525169078..f8847b95f 100644 --- a/dev/Common/Links.js +++ b/dev/Common/Links.js @@ -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[]=', diff --git a/dev/Settings/Admin/General.js b/dev/Settings/Admin/General.js index 611b244ba..22983bfb2 100644 --- a/dev/Settings/Admin/General.js +++ b/dev/Settings/Admin/General.js @@ -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') ]); } } diff --git a/dev/Stores/Language.js b/dev/Stores/Language.js index df27841e7..aeeb716e3 100644 --- a/dev/Stores/Language.js +++ b/dev/Stores/Language.js @@ -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')); } } diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions.php index 32ff245c8..c16888c0f 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions.php @@ -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()) . '/' diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Localization.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Localization.php index 88d6ddb2d..b1d2d1298 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Localization.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Localization.php @@ -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 diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/ActionsAdmin.php b/snappymail/v/0.0.0/app/libraries/RainLoop/ActionsAdmin.php index 5e56b3881..d32e92221 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/ActionsAdmin.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/ActionsAdmin.php @@ -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(); + } + } + } }