diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2024-04-04 00:20:35 -0100 |
---|---|---|
committer | backportbot[bot] <backportbot[bot]@users.noreply.github.com> | 2024-04-17 15:12:22 +0000 |
commit | 9040bcb3b9294d7dd04afb905b93636b9accbd04 (patch) | |
tree | b4fef7460bd676c7b79488dd25eed96c067bd3bb /lib | |
parent | 643be3c81bfd576f76211beed6b96e273646b23f (diff) | |
download | nextcloud-server-9040bcb3b9294d7dd04afb905b93636b9accbd04.tar.gz nextcloud-server-9040bcb3b9294d7dd04afb905b93636b9accbd04.zip |
fix(appconfig): format app values
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/AppConfig.php | 52 | ||||
-rw-r--r-- | lib/public/IAppConfig.php | 4 |
2 files changed, 51 insertions, 5 deletions
diff --git a/lib/private/AppConfig.php b/lib/private/AppConfig.php index 5d753a6ee9c..3c6993201b5 100644 --- a/lib/private/AppConfig.php +++ b/lib/private/AppConfig.php @@ -219,8 +219,9 @@ class AppConfig implements IAppConfig { // if we want to filter values, we need to get sensitivity $this->loadConfigAll(); // array_merge() will remove numeric keys (here config keys), so addition arrays instead + $values = $this->formatAppValues($app, ($this->fastCache[$app] ?? []) + ($this->lazyCache[$app] ?? [])); $values = array_filter( - (($this->fastCache[$app] ?? []) + ($this->lazyCache[$app] ?? [])), + $values, function (string $key) use ($prefix): bool { return str_starts_with($key, $prefix); // filter values based on $prefix }, ARRAY_FILTER_USE_KEY @@ -271,7 +272,8 @@ class AppConfig implements IAppConfig { foreach (array_keys($cache) as $app) { if (isset($cache[$app][$key])) { - $values[$app] = $cache[$app][$key]; + $appCache = $this->formatAppValues((string)$app, $cache[$app], $lazy); + $values[$app] = $appCache[$key]; } } @@ -510,9 +512,9 @@ class AppConfig implements IAppConfig { * @see VALUE_BOOL * @see VALUE_ARRAY */ - public function getValueType(string $app, string $key): int { + public function getValueType(string $app, string $key, ?bool $lazy = null): int { $this->assertParams($app, $key); - $this->loadConfigAll(); + $this->loadConfig($lazy); if (!isset($this->valueTypes[$app][$key])) { throw new AppConfigUnknownKeyException('unknown config key'); @@ -1387,6 +1389,48 @@ class AppConfig implements IAppConfig { return $this->getAllValues($app, filtered: true); } + + /** + * **Warning:** avoid default NULL value for $lazy as this will + * load all lazy values from the database + * + * @param string $app + * @param array $values + * @param bool|null $lazy + * + * @return array + */ + private function formatAppValues(string $app, array $values, ?bool $lazy = null): array { + foreach($values as $key => $value) { + try { + $type = $this->getValueType($app, $key, $lazy); + } catch (AppConfigUnknownKeyException $e) { + continue; + } + + switch ($type) { + case self::VALUE_INT: + $values[$key] = (int)$value; + break; + case self::VALUE_FLOAT: + $values[$key] = (float)$value; + break; + case self::VALUE_BOOL: + $values[$key] = in_array(strtolower($value), ['1', 'true', 'yes', 'on']); + break; + case self::VALUE_ARRAY: + try { + $values[$key] = json_decode($value, true, flags: JSON_THROW_ON_ERROR); + } catch (JsonException $e) { + // ignoreable + } + break; + } + } + + return $values; + } + /** * @param string $app * diff --git a/lib/public/IAppConfig.php b/lib/public/IAppConfig.php index afcdf67f92c..421a735ef06 100644 --- a/lib/public/IAppConfig.php +++ b/lib/public/IAppConfig.php @@ -261,9 +261,11 @@ interface IAppConfig { * returns the type of config value * * **WARNING:** ignore lazy filtering, all config values are loaded from database + * unless lazy is set to false * * @param string $app id of the app * @param string $key config key + * @param bool|null $lazy * * @return int * @throws AppConfigUnknownKeyException @@ -274,7 +276,7 @@ interface IAppConfig { * @see VALUE_BOOL * @see VALUE_ARRAY */ - public function getValueType(string $app, string $key): int; + public function getValueType(string $app, string $key, ?bool $lazy = null): int; /** * Store a config key and its value in database |