-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathUserPreferencesHandler.php
More file actions
220 lines (190 loc) · 7.85 KB
/
UserPreferencesHandler.php
File metadata and controls
220 lines (190 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Config;
use PhpMyAdmin\Config;
use PhpMyAdmin\Config\Settings\Server;
use PhpMyAdmin\Core;
use PhpMyAdmin\Current;
use PhpMyAdmin\Dbal\DatabaseInterface;
use PhpMyAdmin\I18n\LanguageManager;
use PhpMyAdmin\Message;
use PhpMyAdmin\Theme\ThemeManager;
use function array_replace_recursive;
use function is_array;
class UserPreferencesHandler
{
/** @var ''|'db'|'session' */
public string $storageType = '';
/** @var mixed[] default configuration settings */
public array $defaultSettings;
public function __construct(
private readonly Config $config,
private readonly DatabaseInterface $dbi,
private readonly UserPreferences $userPreferences,
private readonly LanguageManager $languageManager,
private readonly ThemeManager $themeManager,
) {
$this->defaultSettings = (new Settings([]))->asArray();
}
/**
* Loads user preferences and merges them with current config
* must be called after control connection has been established
*/
public function loadUserPreferences(bool $isMinimumCommon = false): void
{
$cacheKey = 'server_' . Current::$server;
if (Current::$server > 0 && ! $isMinimumCommon) {
// cache user preferences, use database only when needed
if (
! isset($_SESSION['cache'][$cacheKey]['userprefs'])
|| $_SESSION['cache'][$cacheKey]['config_mtime'] < $this->config->sourceMtime
) {
$prefs = $this->userPreferences->load();
$_SESSION['cache'][$cacheKey]['userprefs'] = $this->userPreferences->apply($prefs['config_data']);
$_SESSION['cache'][$cacheKey]['userprefs_type'] = $prefs['type'];
$_SESSION['cache'][$cacheKey]['config_mtime'] = $this->config->sourceMtime;
}
} elseif (Current::$server === 0 || ! isset($_SESSION['cache'][$cacheKey]['userprefs'])) {
$this->storageType = '';
return;
}
/** @var mixed[] $configData */
$configData = $_SESSION['cache'][$cacheKey]['userprefs'];
// type is 'db' or 'session'
$this->storageType = $_SESSION['cache'][$cacheKey]['userprefs_type'];
if (isset($configData['Server']) && is_array($configData['Server'])) {
$serverConfig = array_replace_recursive($this->config->selectedServer, $configData['Server']);
$this->config->selectedServer = (new Server($serverConfig))->asArray();
}
// load config array
$this->config->settings = array_replace_recursive($this->config->settings, $configData);
$this->config->config = new Settings($this->config->settings);
if ($isMinimumCommon) {
return;
}
// settings below start really working on next page load, but
// changes are made only in index.php so everything is set when
// in frames
// save theme
if ($this->themeManager->getThemeCookie() !== '' || isset($_REQUEST['set_theme'])) {
if (
(! isset($configData['ThemeDefault'])
&& $this->themeManager->theme->getId() !== 'original')
|| isset($configData['ThemeDefault'])
&& $configData['ThemeDefault'] !== $this->themeManager->theme->getId()
) {
$this->setUserValue(
'ThemeDefault',
$this->themeManager->theme->getId(),
'original',
);
$this->updateConfigValue('ThemeDefault', $this->themeManager->theme->getId());
}
} elseif (
$this->config->config->ThemeDefault !== $this->themeManager->theme->getId()
&& $this->themeManager->themeExists($this->config->config->ThemeDefault)
) {
// no cookie - read default from settings
$this->themeManager->setActiveTheme($this->config->config->ThemeDefault);
$this->themeManager->setThemeCookie();
}
// save language
if ($this->config->issetCookie('pma_lang') || isset($_POST['lang'])) {
if (
(! isset($configData['lang'])
&& Current::$lang !== 'en')
|| isset($configData['lang'])
&& Current::$lang !== $configData['lang']
) {
$this->setUserValue('lang', Current::$lang, 'en');
}
} elseif (isset($configData['lang'])) {
// read language from settings
$language = $this->languageManager->getLanguage($configData['lang']);
if ($language !== false) {
$this->languageManager->activate($language);
$this->config->setCookie('pma_lang', $language->getCode());
}
}
// set connection collation
$this->setConnectionCollation();
}
/**
* Get LoginCookieValidity from preferences cache.
*
* No generic solution for loading preferences from cache as some settings
* need to be kept for processing in {@link loadUserPreferences()}.
*/
public function getLoginCookieValidityFromCache(int $server): void
{
$cacheKey = 'server_' . $server;
if (! isset($_SESSION['cache'][$cacheKey]['userprefs']['LoginCookieValidity'])) {
return;
}
$value = $_SESSION['cache'][$cacheKey]['userprefs']['LoginCookieValidity'];
$this->config->set('LoginCookieValidity', $value);
}
/**
* Sets config value which is stored in user preferences (if available)
* or in a cookie.
*
* If user preferences are not yet initialized, option is applied to
* global config and added to a update queue, which is processed
* by {@link loadUserPreferences()}
*/
public function setUserValue(
string $cfgPath,
mixed $newCfgValue,
string|null $defaultValue = null,
string $cookieName = '',
): true|Message {
$result = true;
// use permanent user preferences if possible
if ($this->storageType !== '') {
if ($defaultValue === null) {
$defaultValue = Core::arrayRead($cfgPath, $this->defaultSettings);
}
$result = $this->userPreferences->persistOption($cfgPath, $newCfgValue, $defaultValue);
}
if ($this->storageType !== 'db' && $cookieName !== '') {
// fall back to cookies
if ($defaultValue === null) {
$defaultValue = Core::arrayRead($cfgPath, $this->config->settings);
}
$this->config->setCookie($cookieName, (string) $newCfgValue, $defaultValue);
}
return $result;
}
public function updateConfigValue(string $cfgPath, mixed $newCfgValue): void
{
$this->config->set($cfgPath, $newCfgValue);
}
/**
* Reads value stored by {@link setUserValue()}
*
* @param string $cookieName cookie name
* @param mixed $cfgValue config value
*/
public function getUserValue(string $cookieName, mixed $cfgValue): mixed
{
$cookieExists = ! empty($this->config->getCookie($cookieName));
if ($this->storageType === 'db') {
// permanent user preferences value exists, remove cookie
if ($cookieExists) {
$this->config->removeCookie($cookieName);
}
} elseif ($cookieExists) {
return $this->config->getCookie($cookieName);
}
// return value from $cfg array
return $cfgValue;
}
private function setConnectionCollation(): void
{
$collationConnection = $this->config->config->DefaultConnectionCollation;
if ($collationConnection === '' || $collationConnection === $this->dbi->getDefaultCollation()) {
return;
}
$this->dbi->setCollation($collationConnection);
}
}