You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2093 lines
65 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Config;
  8. use Generator;
  9. use InvalidArgumentException;
  10. use JsonException;
  11. use OC\AppFramework\Bootstrap\Coordinator;
  12. use OCP\Config\Exceptions\IncorrectTypeException;
  13. use OCP\Config\Exceptions\TypeConflictException;
  14. use OCP\Config\Exceptions\UnknownKeyException;
  15. use OCP\Config\IUserConfig;
  16. use OCP\Config\Lexicon\Entry;
  17. use OCP\Config\Lexicon\ILexicon;
  18. use OCP\Config\Lexicon\Strictness;
  19. use OCP\Config\ValueType;
  20. use OCP\DB\Exception as DBException;
  21. use OCP\DB\IResult;
  22. use OCP\DB\QueryBuilder\IQueryBuilder;
  23. use OCP\EventDispatcher\IEventDispatcher;
  24. use OCP\IConfig;
  25. use OCP\IDBConnection;
  26. use OCP\Security\ICrypto;
  27. use OCP\User\Events\UserConfigChangedEvent;
  28. use Psr\Log\LoggerInterface;
  29. /**
  30. * This class provides an easy way for apps to store user config in the
  31. * database.
  32. * Supports **lazy loading**
  33. *
  34. * ### What is lazy loading ?
  35. * In order to avoid loading useless user config into memory for each request,
  36. * only non-lazy values are now loaded.
  37. *
  38. * Once a value that is lazy is requested, all lazy values will be loaded.
  39. *
  40. * Similarly, some methods from this class are marked with a warning about ignoring
  41. * lazy loading. Use them wisely and only on parts of the code that are called
  42. * during specific requests or actions to avoid loading the lazy values all the time.
  43. *
  44. * @since 31.0.0
  45. */
  46. class UserConfig implements IUserConfig {
  47. private const USER_MAX_LENGTH = 64;
  48. private const APP_MAX_LENGTH = 32;
  49. private const KEY_MAX_LENGTH = 64;
  50. private const INDEX_MAX_LENGTH = 64;
  51. private const ENCRYPTION_PREFIX = '$UserConfigEncryption$';
  52. private const ENCRYPTION_PREFIX_LENGTH = 22; // strlen(self::ENCRYPTION_PREFIX)
  53. /** @var array<string, array<string, array<string, mixed>>> [ass'user_id' => ['app_id' => ['key' => 'value']]] */
  54. private array $fastCache = []; // cache for normal config keys
  55. /** @var array<string, array<string, array<string, mixed>>> ['user_id' => ['app_id' => ['key' => 'value']]] */
  56. private array $lazyCache = []; // cache for lazy config keys
  57. /** @var array<string, array<string, array<string, array<string, mixed>>>> ['user_id' => ['app_id' => ['key' => ['type' => ValueType, 'flags' => bitflag]]]] */
  58. private array $valueDetails = []; // type for all config values
  59. /** @var array<string, boolean> ['user_id' => bool] */
  60. private array $fastLoaded = [];
  61. /** @var array<string, boolean> ['user_id' => bool] */
  62. private array $lazyLoaded = [];
  63. /** @var array<string, array{entries: array<string, Entry>, aliases: array<string, string>, strictness: Strictness}> ['app_id' => ['strictness' => ConfigLexiconStrictness, 'entries' => ['config_key' => ConfigLexiconEntry[]]] */
  64. private array $configLexiconDetails = [];
  65. private bool $ignoreLexiconAliases = false;
  66. private array $strictnessApplied = [];
  67. public function __construct(
  68. protected IDBConnection $connection,
  69. protected IConfig $config,
  70. private readonly ConfigManager $configManager,
  71. private readonly PresetManager $presetManager,
  72. protected LoggerInterface $logger,
  73. protected ICrypto $crypto,
  74. protected IEventDispatcher $dispatcher,
  75. ) {
  76. }
  77. /**
  78. * @inheritDoc
  79. *
  80. * @param string $appId optional id of app
  81. *
  82. * @return list<string> list of userIds
  83. * @since 31.0.0
  84. */
  85. public function getUserIds(string $appId = ''): array {
  86. $this->assertParams(app: $appId, allowEmptyUser: true, allowEmptyApp: true);
  87. $qb = $this->connection->getQueryBuilder();
  88. $qb->from('preferences');
  89. $qb->select('userid');
  90. $qb->groupBy('userid');
  91. if ($appId !== '') {
  92. $qb->where($qb->expr()->eq('appid', $qb->createNamedParameter($appId)));
  93. }
  94. $result = $qb->executeQuery();
  95. $rows = $result->fetchAll();
  96. $userIds = [];
  97. foreach ($rows as $row) {
  98. $userIds[] = $row['userid'];
  99. }
  100. return $userIds;
  101. }
  102. /**
  103. * @inheritDoc
  104. *
  105. * @return list<string> list of app ids
  106. * @since 31.0.0
  107. */
  108. public function getApps(string $userId): array {
  109. $this->assertParams($userId, allowEmptyApp: true);
  110. $this->loadConfigAll($userId);
  111. $apps = array_merge(array_keys($this->fastCache[$userId] ?? []), array_keys($this->lazyCache[$userId] ?? []));
  112. sort($apps);
  113. return array_values(array_unique($apps));
  114. }
  115. /**
  116. * @inheritDoc
  117. *
  118. * @param string $userId id of the user
  119. * @param string $app id of the app
  120. *
  121. * @return list<string> list of stored config keys
  122. * @since 31.0.0
  123. */
  124. public function getKeys(string $userId, string $app): array {
  125. $this->assertParams($userId, $app);
  126. $this->loadConfigAll($userId);
  127. // array_merge() will remove numeric keys (here config keys), so addition arrays instead
  128. $keys = array_map('strval', array_keys(($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? [])));
  129. sort($keys);
  130. return array_values(array_unique($keys));
  131. }
  132. /**
  133. * @inheritDoc
  134. *
  135. * @param string $userId id of the user
  136. * @param string $app id of the app
  137. * @param string $key config key
  138. * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config
  139. *
  140. * @return bool TRUE if key exists
  141. * @since 31.0.0
  142. */
  143. public function hasKey(string $userId, string $app, string $key, ?bool $lazy = false): bool {
  144. $this->assertParams($userId, $app, $key);
  145. $this->loadConfig($userId, $lazy);
  146. $this->matchAndApplyLexiconDefinition($userId, $app, $key);
  147. if ($lazy === null) {
  148. $appCache = $this->getValues($userId, $app);
  149. return isset($appCache[$key]);
  150. }
  151. if ($lazy) {
  152. return isset($this->lazyCache[$userId][$app][$key]);
  153. }
  154. return isset($this->fastCache[$userId][$app][$key]);
  155. }
  156. /**
  157. * @inheritDoc
  158. *
  159. * @param string $userId id of the user
  160. * @param string $app id of the app
  161. * @param string $key config key
  162. * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config
  163. *
  164. * @return bool
  165. * @throws UnknownKeyException if config key is not known
  166. * @since 31.0.0
  167. */
  168. public function isSensitive(string $userId, string $app, string $key, ?bool $lazy = false): bool {
  169. $this->assertParams($userId, $app, $key);
  170. $this->loadConfig($userId, $lazy);
  171. $this->matchAndApplyLexiconDefinition($userId, $app, $key);
  172. if (!isset($this->valueDetails[$userId][$app][$key])) {
  173. throw new UnknownKeyException('unknown config key');
  174. }
  175. return $this->isFlagged(self::FLAG_SENSITIVE, $this->valueDetails[$userId][$app][$key]['flags']);
  176. }
  177. /**
  178. * @inheritDoc
  179. *
  180. * @param string $userId id of the user
  181. * @param string $app id of the app
  182. * @param string $key config key
  183. * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config
  184. *
  185. * @return bool
  186. * @throws UnknownKeyException if config key is not known
  187. * @since 31.0.0
  188. */
  189. public function isIndexed(string $userId, string $app, string $key, ?bool $lazy = false): bool {
  190. $this->assertParams($userId, $app, $key);
  191. $this->loadConfig($userId, $lazy);
  192. $this->matchAndApplyLexiconDefinition($userId, $app, $key);
  193. if (!isset($this->valueDetails[$userId][$app][$key])) {
  194. throw new UnknownKeyException('unknown config key');
  195. }
  196. return $this->isFlagged(self::FLAG_INDEXED, $this->valueDetails[$userId][$app][$key]['flags']);
  197. }
  198. /**
  199. * @inheritDoc
  200. *
  201. * @param string $userId id of the user
  202. * @param string $app if of the app
  203. * @param string $key config key
  204. *
  205. * @return bool TRUE if config is lazy loaded
  206. * @throws UnknownKeyException if config key is not known
  207. * @see IUserConfig for details about lazy loading
  208. * @since 31.0.0
  209. */
  210. public function isLazy(string $userId, string $app, string $key): bool {
  211. $this->matchAndApplyLexiconDefinition($userId, $app, $key);
  212. // there is a huge probability the non-lazy config are already loaded
  213. // meaning that we can start by only checking if a current non-lazy key exists
  214. if ($this->hasKey($userId, $app, $key, false)) {
  215. // meaning key is not lazy.
  216. return false;
  217. }
  218. // as key is not found as non-lazy, we load and search in the lazy config
  219. if ($this->hasKey($userId, $app, $key, true)) {
  220. return true;
  221. }
  222. throw new UnknownKeyException('unknown config key');
  223. }
  224. /**
  225. * @inheritDoc
  226. *
  227. * @param string $userId id of the user
  228. * @param string $app id of the app
  229. * @param string $prefix config keys prefix to search
  230. * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE}
  231. *
  232. * @return array<string, string|int|float|bool|array> [key => value]
  233. * @since 31.0.0
  234. */
  235. public function getValues(
  236. string $userId,
  237. string $app,
  238. string $prefix = '',
  239. bool $filtered = false,
  240. ): array {
  241. $this->assertParams($userId, $app, $prefix);
  242. // if we want to filter values, we need to get sensitivity
  243. $this->loadConfigAll($userId);
  244. // array_merge() will remove numeric keys (here config keys), so addition arrays instead
  245. $values = array_filter(
  246. $this->formatAppValues($userId, $app, ($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []), $filtered),
  247. function (string $key) use ($prefix): bool {
  248. // filter values based on $prefix
  249. return str_starts_with($key, $prefix);
  250. }, ARRAY_FILTER_USE_KEY
  251. );
  252. return $values;
  253. }
  254. /**
  255. * @inheritDoc
  256. *
  257. * @param string $userId id of the user
  258. * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE}
  259. *
  260. * @return array<string, array<string, string|int|float|bool|array>> [appId => [key => value]]
  261. * @since 31.0.0
  262. */
  263. public function getAllValues(string $userId, bool $filtered = false): array {
  264. $this->assertParams($userId, allowEmptyApp: true);
  265. $this->loadConfigAll($userId);
  266. $result = [];
  267. foreach ($this->getApps($userId) as $app) {
  268. // array_merge() will remove numeric keys (here config keys), so addition arrays instead
  269. $cached = ($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []);
  270. $result[$app] = $this->formatAppValues($userId, $app, $cached, $filtered);
  271. }
  272. return $result;
  273. }
  274. /**
  275. * @inheritDoc
  276. *
  277. * @param string $userId id of the user
  278. * @param string $key config key
  279. * @param bool $lazy search within lazy loaded config
  280. * @param ValueType|null $typedAs enforce type for the returned values
  281. *
  282. * @return array<string, string|int|float|bool|array> [appId => value]
  283. * @since 31.0.0
  284. */
  285. public function getValuesByApps(string $userId, string $key, bool $lazy = false, ?ValueType $typedAs = null): array {
  286. $this->assertParams($userId, '', $key, allowEmptyApp: true);
  287. $this->loadConfig($userId, $lazy);
  288. /** @var array<array-key, array<array-key, mixed>> $cache */
  289. if ($lazy) {
  290. $cache = $this->lazyCache[$userId];
  291. } else {
  292. $cache = $this->fastCache[$userId];
  293. }
  294. $values = [];
  295. foreach (array_keys($cache) as $app) {
  296. if (isset($cache[$app][$key])) {
  297. $value = $cache[$app][$key];
  298. try {
  299. $this->decryptSensitiveValue($userId, $app, $key, $value);
  300. $value = $this->convertTypedValue($value, $typedAs ?? $this->getValueType($userId, $app, $key, $lazy));
  301. } catch (IncorrectTypeException|UnknownKeyException) {
  302. }
  303. $values[$app] = $value;
  304. }
  305. }
  306. return $values;
  307. }
  308. /**
  309. * @inheritDoc
  310. *
  311. * @param string $app id of the app
  312. * @param string $key config key
  313. * @param ValueType|null $typedAs enforce type for the returned values
  314. * @param array|null $userIds limit to a list of user ids
  315. *
  316. * @return array<string, string|int|float|bool|array> [userId => value]
  317. * @since 31.0.0
  318. */
  319. public function getValuesByUsers(
  320. string $app,
  321. string $key,
  322. ?ValueType $typedAs = null,
  323. ?array $userIds = null,
  324. ): array {
  325. $this->assertParams('', $app, $key, allowEmptyUser: true);
  326. $this->matchAndApplyLexiconDefinition('', $app, $key);
  327. $qb = $this->connection->getQueryBuilder();
  328. $qb->select('userid', 'configvalue', 'type')
  329. ->from('preferences')
  330. ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app)))
  331. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key)));
  332. $values = [];
  333. // this nested function will execute current Query and store result within $values.
  334. $executeAndStoreValue = function (IQueryBuilder $qb) use (&$values, $typedAs): IResult {
  335. $result = $qb->executeQuery();
  336. while ($row = $result->fetch()) {
  337. $value = $row['configvalue'];
  338. try {
  339. $value = $this->convertTypedValue($value, $typedAs ?? ValueType::from((int)$row['type']));
  340. } catch (IncorrectTypeException) {
  341. }
  342. $values[$row['userid']] = $value;
  343. }
  344. return $result;
  345. };
  346. // if no userIds to filter, we execute query as it is and returns all values ...
  347. if ($userIds === null) {
  348. $result = $executeAndStoreValue($qb);
  349. $result->closeCursor();
  350. return $values;
  351. }
  352. // if userIds to filter, we chunk the list and execute the same query multiple times until we get all values
  353. $result = null;
  354. $qb->andWhere($qb->expr()->in('userid', $qb->createParameter('userIds')));
  355. foreach (array_chunk($userIds, 50, true) as $chunk) {
  356. $qb->setParameter('userIds', $chunk, IQueryBuilder::PARAM_STR_ARRAY);
  357. $result = $executeAndStoreValue($qb);
  358. }
  359. $result?->closeCursor();
  360. return $values;
  361. }
  362. /**
  363. * @inheritDoc
  364. *
  365. * @param string $app id of the app
  366. * @param string $key config key
  367. * @param string $value config value
  368. * @param bool $caseInsensitive non-case-sensitive search, only works if $value is a string
  369. *
  370. * @return Generator<string>
  371. * @since 31.0.0
  372. */
  373. public function searchUsersByValueString(string $app, string $key, string $value, bool $caseInsensitive = false): Generator {
  374. return $this->searchUsersByTypedValue($app, $key, $value, $caseInsensitive);
  375. }
  376. /**
  377. * @inheritDoc
  378. *
  379. * @param string $app id of the app
  380. * @param string $key config key
  381. * @param int $value config value
  382. *
  383. * @return Generator<string>
  384. * @since 31.0.0
  385. */
  386. public function searchUsersByValueInt(string $app, string $key, int $value): Generator {
  387. return $this->searchUsersByValueString($app, $key, (string)$value);
  388. }
  389. /**
  390. * @inheritDoc
  391. *
  392. * @param string $app id of the app
  393. * @param string $key config key
  394. * @param array $values list of config values
  395. *
  396. * @return Generator<string>
  397. * @since 31.0.0
  398. */
  399. public function searchUsersByValues(string $app, string $key, array $values): Generator {
  400. return $this->searchUsersByTypedValue($app, $key, $values);
  401. }
  402. /**
  403. * @inheritDoc
  404. *
  405. * @param string $app id of the app
  406. * @param string $key config key
  407. * @param bool $value config value
  408. *
  409. * @return Generator<string>
  410. * @since 31.0.0
  411. */
  412. public function searchUsersByValueBool(string $app, string $key, bool $value): Generator {
  413. $values = ['0', 'off', 'false', 'no'];
  414. if ($value) {
  415. $values = ['1', 'on', 'true', 'yes'];
  416. }
  417. return $this->searchUsersByValues($app, $key, $values);
  418. }
  419. /**
  420. * returns a list of users with config key set to a specific value, or within the list of
  421. * possible values
  422. *
  423. * @param string $app
  424. * @param string $key
  425. * @param string|array $value
  426. * @param bool $caseInsensitive
  427. *
  428. * @return Generator<string>
  429. */
  430. private function searchUsersByTypedValue(string $app, string $key, string|array $value, bool $caseInsensitive = false): Generator {
  431. $this->assertParams('', $app, $key, allowEmptyUser: true);
  432. $this->matchAndApplyLexiconDefinition('', $app, $key);
  433. $lexiconEntry = $this->getLexiconEntry($app, $key);
  434. if ($lexiconEntry?->isFlagged(self::FLAG_INDEXED) === false) {
  435. $this->logger->notice('UserConfig+Lexicon: using searchUsersByTypedValue on config key ' . $app . '/' . $key . ' which is not set as indexed');
  436. }
  437. $qb = $this->connection->getQueryBuilder();
  438. $qb->from('preferences');
  439. $qb->select('userid');
  440. $qb->where($qb->expr()->eq('appid', $qb->createNamedParameter($app)));
  441. $qb->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key)));
  442. $configValueColumn = ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) ? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR) : 'configvalue';
  443. if (is_array($value)) {
  444. $where = $qb->expr()->in('indexed', $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR_ARRAY));
  445. // in case lexicon does not exist for this key - or is not set as indexed - we keep searching for non-index entries if 'flags' is set as not indexed
  446. if ($lexiconEntry?->isFlagged(self::FLAG_INDEXED) !== true) {
  447. $where = $qb->expr()->orX(
  448. $where,
  449. $qb->expr()->andX(
  450. $qb->expr()->neq($qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), $qb->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)),
  451. $qb->expr()->in($configValueColumn, $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR_ARRAY))
  452. )
  453. );
  454. }
  455. } else {
  456. if ($caseInsensitive) {
  457. $where = $qb->expr()->eq($qb->func()->lower('indexed'), $qb->createNamedParameter(strtolower($value)));
  458. // in case lexicon does not exist for this key - or is not set as indexed - we keep searching for non-index entries if 'flags' is set as not indexed
  459. if ($lexiconEntry?->isFlagged(self::FLAG_INDEXED) !== true) {
  460. $where = $qb->expr()->orX(
  461. $where,
  462. $qb->expr()->andX(
  463. $qb->expr()->neq($qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), $qb->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)),
  464. $qb->expr()->eq($qb->func()->lower($configValueColumn), $qb->createNamedParameter(strtolower($value)))
  465. )
  466. );
  467. }
  468. } else {
  469. $where = $qb->expr()->eq('indexed', $qb->createNamedParameter($value));
  470. // in case lexicon does not exist for this key - or is not set as indexed - we keep searching for non-index entries if 'flags' is set as not indexed
  471. if ($lexiconEntry?->isFlagged(self::FLAG_INDEXED) !== true) {
  472. $where = $qb->expr()->orX(
  473. $where,
  474. $qb->expr()->andX(
  475. $qb->expr()->neq($qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), $qb->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)),
  476. $qb->expr()->eq($configValueColumn, $qb->createNamedParameter($value))
  477. )
  478. );
  479. }
  480. }
  481. }
  482. $qb->andWhere($where);
  483. $result = $qb->executeQuery();
  484. while ($row = $result->fetch()) {
  485. yield $row['userid'];
  486. }
  487. }
  488. /**
  489. * Get the config value as string.
  490. * If the value does not exist the given default will be returned.
  491. *
  492. * Set lazy to `null` to ignore it and get the value from either source.
  493. *
  494. * **WARNING:** Method is internal and **SHOULD** not be used, as it is better to get the value with a type.
  495. *
  496. * @param string $userId id of the user
  497. * @param string $app id of the app
  498. * @param string $key config key
  499. * @param string $default config value
  500. * @param null|bool $lazy get config as lazy loaded or not. can be NULL
  501. *
  502. * @return string the value or $default
  503. * @throws TypeConflictException
  504. * @internal
  505. * @since 31.0.0
  506. * @see IUserConfig for explanation about lazy loading
  507. * @see getValueString()
  508. * @see getValueInt()
  509. * @see getValueFloat()
  510. * @see getValueBool()
  511. * @see getValueArray()
  512. */
  513. public function getValueMixed(
  514. string $userId,
  515. string $app,
  516. string $key,
  517. string $default = '',
  518. ?bool $lazy = false,
  519. ): string {
  520. $this->matchAndApplyLexiconDefinition($userId, $app, $key);
  521. try {
  522. $lazy ??= $this->isLazy($userId, $app, $key);
  523. } catch (UnknownKeyException) {
  524. return $default;
  525. }
  526. return $this->getTypedValue(
  527. $userId,
  528. $app,
  529. $key,
  530. $default,
  531. $lazy,
  532. ValueType::MIXED
  533. );
  534. }
  535. /**
  536. * @inheritDoc
  537. *
  538. * @param string $userId id of the user
  539. * @param string $app id of the app
  540. * @param string $key config key
  541. * @param string $default default value
  542. * @param bool $lazy search within lazy loaded config
  543. *
  544. * @return string stored config value or $default if not set in database
  545. * @throws InvalidArgumentException if one of the argument format is invalid
  546. * @throws TypeConflictException in case of conflict with the value type set in database
  547. * @since 31.0.0
  548. * @see IUserConfig for explanation about lazy loading
  549. */
  550. public function getValueString(
  551. string $userId,
  552. string $app,
  553. string $key,
  554. string $default = '',
  555. bool $lazy = false,
  556. ): string {
  557. return $this->getTypedValue($userId, $app, $key, $default, $lazy, ValueType::STRING);
  558. }
  559. /**
  560. * @inheritDoc
  561. *
  562. * @param string $userId id of the user
  563. * @param string $app id of the app
  564. * @param string $key config key
  565. * @param int $default default value
  566. * @param bool $lazy search within lazy loaded config
  567. *
  568. * @return int stored config value or $default if not set in database
  569. * @throws InvalidArgumentException if one of the argument format is invalid
  570. * @throws TypeConflictException in case of conflict with the value type set in database
  571. * @since 31.0.0
  572. * @see IUserConfig for explanation about lazy loading
  573. */
  574. public function getValueInt(
  575. string $userId,
  576. string $app,
  577. string $key,
  578. int $default = 0,
  579. bool $lazy = false,
  580. ): int {
  581. return (int)$this->getTypedValue($userId, $app, $key, (string)$default, $lazy, ValueType::INT);
  582. }
  583. /**
  584. * @inheritDoc
  585. *
  586. * @param string $userId id of the user
  587. * @param string $app id of the app
  588. * @param string $key config key
  589. * @param float $default default value
  590. * @param bool $lazy search within lazy loaded config
  591. *
  592. * @return float stored config value or $default if not set in database
  593. * @throws InvalidArgumentException if one of the argument format is invalid
  594. * @throws TypeConflictException in case of conflict with the value type set in database
  595. * @since 31.0.0
  596. * @see IUserConfig for explanation about lazy loading
  597. */
  598. public function getValueFloat(
  599. string $userId,
  600. string $app,
  601. string $key,
  602. float $default = 0,
  603. bool $lazy = false,
  604. ): float {
  605. return (float)$this->getTypedValue($userId, $app, $key, (string)$default, $lazy, ValueType::FLOAT);
  606. }
  607. /**
  608. * @inheritDoc
  609. *
  610. * @param string $userId id of the user
  611. * @param string $app id of the app
  612. * @param string $key config key
  613. * @param bool $default default value
  614. * @param bool $lazy search within lazy loaded config
  615. *
  616. * @return bool stored config value or $default if not set in database
  617. * @throws InvalidArgumentException if one of the argument format is invalid
  618. * @throws TypeConflictException in case of conflict with the value type set in database
  619. * @since 31.0.0
  620. * @see IUserConfig for explanation about lazy loading
  621. */
  622. public function getValueBool(
  623. string $userId,
  624. string $app,
  625. string $key,
  626. bool $default = false,
  627. bool $lazy = false,
  628. ): bool {
  629. $b = strtolower($this->getTypedValue($userId, $app, $key, $default ? 'true' : 'false', $lazy, ValueType::BOOL));
  630. return in_array($b, ['1', 'true', 'yes', 'on']);
  631. }
  632. /**
  633. * @inheritDoc
  634. *
  635. * @param string $userId id of the user
  636. * @param string $app id of the app
  637. * @param string $key config key
  638. * @param array $default default value
  639. * @param bool $lazy search within lazy loaded config
  640. *
  641. * @return array stored config value or $default if not set in database
  642. * @throws InvalidArgumentException if one of the argument format is invalid
  643. * @throws TypeConflictException in case of conflict with the value type set in database
  644. * @since 31.0.0
  645. * @see IUserConfig for explanation about lazy loading
  646. */
  647. public function getValueArray(
  648. string $userId,
  649. string $app,
  650. string $key,
  651. array $default = [],
  652. bool $lazy = false,
  653. ): array {
  654. try {
  655. $defaultJson = json_encode($default, JSON_THROW_ON_ERROR);
  656. $value = json_decode($this->getTypedValue($userId, $app, $key, $defaultJson, $lazy, ValueType::ARRAY), true, flags: JSON_THROW_ON_ERROR);
  657. return is_array($value) ? $value : [];
  658. } catch (JsonException) {
  659. return [];
  660. }
  661. }
  662. /**
  663. * @param string $userId
  664. * @param string $app id of the app
  665. * @param string $key config key
  666. * @param string $default default value
  667. * @param bool $lazy search within lazy loaded config
  668. * @param ValueType $type value type
  669. *
  670. * @return string
  671. * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one
  672. */
  673. private function getTypedValue(
  674. string $userId,
  675. string $app,
  676. string $key,
  677. string $default,
  678. bool $lazy,
  679. ValueType $type,
  680. ): string {
  681. $this->assertParams($userId, $app, $key);
  682. $origKey = $key;
  683. $matched = $this->matchAndApplyLexiconDefinition($userId, $app, $key, $lazy, $type, default: $default);
  684. if ($default === null) {
  685. // there is no logical reason for it to be null
  686. throw new \Exception('default cannot be null');
  687. }
  688. // returns default if strictness of lexicon is set to WARNING (block and report)
  689. if (!$matched) {
  690. return $default;
  691. }
  692. $this->loadConfig($userId, $lazy);
  693. /**
  694. * We ignore check if mixed type is requested.
  695. * If type of stored value is set as mixed, we don't filter.
  696. * If type of stored value is defined, we compare with the one requested.
  697. */
  698. $knownType = $this->valueDetails[$userId][$app][$key]['type'] ?? null;
  699. if ($type !== ValueType::MIXED
  700. && $knownType !== null
  701. && $knownType !== ValueType::MIXED
  702. && $type !== $knownType) {
  703. $this->logger->warning('conflict with value type from database', ['app' => $app, 'key' => $key, 'type' => $type, 'knownType' => $knownType]);
  704. throw new TypeConflictException('conflict with value type from database');
  705. }
  706. /**
  707. * - the pair $app/$key cannot exist in both array,
  708. * - we should still return an existing non-lazy value even if current method
  709. * is called with $lazy is true
  710. *
  711. * This way, lazyCache will be empty until the load for lazy config value is requested.
  712. */
  713. if (isset($this->lazyCache[$userId][$app][$key])) {
  714. $value = $this->lazyCache[$userId][$app][$key];
  715. } elseif (isset($this->fastCache[$userId][$app][$key])) {
  716. $value = $this->fastCache[$userId][$app][$key];
  717. } else {
  718. return $default;
  719. }
  720. $this->decryptSensitiveValue($userId, $app, $key, $value);
  721. // in case the key was modified while running matchAndApplyLexiconDefinition() we are
  722. // interested to check options in case a modification of the value is needed
  723. // ie inverting value from previous key when using lexicon option RENAME_INVERT_BOOLEAN
  724. if ($origKey !== $key && $type === ValueType::BOOL) {
  725. $value = ($this->configManager->convertToBool($value, $this->getLexiconEntry($app, $key))) ? '1' : '0';
  726. }
  727. return $value;
  728. }
  729. /**
  730. * @inheritDoc
  731. *
  732. * @param string $userId id of the user
  733. * @param string $app id of the app
  734. * @param string $key config key
  735. *
  736. * @return ValueType type of the value
  737. * @throws UnknownKeyException if config key is not known
  738. * @throws IncorrectTypeException if config value type is not known
  739. * @since 31.0.0
  740. */
  741. public function getValueType(string $userId, string $app, string $key, ?bool $lazy = null): ValueType {
  742. $this->assertParams($userId, $app, $key);
  743. $this->loadConfig($userId, $lazy);
  744. $this->matchAndApplyLexiconDefinition($userId, $app, $key);
  745. if (!isset($this->valueDetails[$userId][$app][$key]['type'])) {
  746. throw new UnknownKeyException('unknown config key');
  747. }
  748. return $this->valueDetails[$userId][$app][$key]['type'];
  749. }
  750. /**
  751. * @inheritDoc
  752. *
  753. * @param string $userId id of the user
  754. * @param string $app id of the app
  755. * @param string $key config key
  756. * @param bool $lazy lazy loading
  757. *
  758. * @return int flags applied to value
  759. * @throws UnknownKeyException if config key is not known
  760. * @throws IncorrectTypeException if config value type is not known
  761. * @since 31.0.0
  762. */
  763. public function getValueFlags(string $userId, string $app, string $key, bool $lazy = false): int {
  764. $this->assertParams($userId, $app, $key);
  765. $this->loadConfig($userId, $lazy);
  766. $this->matchAndApplyLexiconDefinition($userId, $app, $key);
  767. if (!isset($this->valueDetails[$userId][$app][$key])) {
  768. throw new UnknownKeyException('unknown config key');
  769. }
  770. return $this->valueDetails[$userId][$app][$key]['flags'];
  771. }
  772. /**
  773. * Store a config key and its value in database as VALUE_MIXED
  774. *
  775. * **WARNING:** Method is internal and **MUST** not be used as it is best to set a real value type
  776. *
  777. * @param string $userId id of the user
  778. * @param string $app id of the app
  779. * @param string $key config key
  780. * @param string $value config value
  781. * @param bool $lazy set config as lazy loaded
  782. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  783. *
  784. * @return bool TRUE if value was different, therefor updated in database
  785. * @throws TypeConflictException if type from database is not VALUE_MIXED
  786. * @internal
  787. * @since 31.0.0
  788. * @see IUserConfig for explanation about lazy loading
  789. * @see setValueString()
  790. * @see setValueInt()
  791. * @see setValueFloat()
  792. * @see setValueBool()
  793. * @see setValueArray()
  794. */
  795. public function setValueMixed(
  796. string $userId,
  797. string $app,
  798. string $key,
  799. string $value,
  800. bool $lazy = false,
  801. int $flags = 0,
  802. ): bool {
  803. return $this->setTypedValue(
  804. $userId,
  805. $app,
  806. $key,
  807. $value,
  808. $lazy,
  809. $flags,
  810. ValueType::MIXED
  811. );
  812. }
  813. /**
  814. * @inheritDoc
  815. *
  816. * @param string $userId id of the user
  817. * @param string $app id of the app
  818. * @param string $key config key
  819. * @param string $value config value
  820. * @param bool $lazy set config as lazy loaded
  821. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  822. *
  823. * @return bool TRUE if value was different, therefor updated in database
  824. * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one
  825. * @since 31.0.0
  826. * @see IUserConfig for explanation about lazy loading
  827. */
  828. public function setValueString(
  829. string $userId,
  830. string $app,
  831. string $key,
  832. string $value,
  833. bool $lazy = false,
  834. int $flags = 0,
  835. ): bool {
  836. return $this->setTypedValue(
  837. $userId,
  838. $app,
  839. $key,
  840. $value,
  841. $lazy,
  842. $flags,
  843. ValueType::STRING
  844. );
  845. }
  846. /**
  847. * @inheritDoc
  848. *
  849. * @param string $userId id of the user
  850. * @param string $app id of the app
  851. * @param string $key config key
  852. * @param int $value config value
  853. * @param bool $lazy set config as lazy loaded
  854. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  855. *
  856. * @return bool TRUE if value was different, therefor updated in database
  857. * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one
  858. * @since 31.0.0
  859. * @see IUserConfig for explanation about lazy loading
  860. */
  861. public function setValueInt(
  862. string $userId,
  863. string $app,
  864. string $key,
  865. int $value,
  866. bool $lazy = false,
  867. int $flags = 0,
  868. ): bool {
  869. if ($value > 2000000000) {
  870. $this->logger->debug('You are trying to store an integer value around/above 2,147,483,647. This is a reminder that reaching this theoretical limit on 32 bits system will throw an exception.');
  871. }
  872. return $this->setTypedValue(
  873. $userId,
  874. $app,
  875. $key,
  876. (string)$value,
  877. $lazy,
  878. $flags,
  879. ValueType::INT
  880. );
  881. }
  882. /**
  883. * @inheritDoc
  884. *
  885. * @param string $userId id of the user
  886. * @param string $app id of the app
  887. * @param string $key config key
  888. * @param float $value config value
  889. * @param bool $lazy set config as lazy loaded
  890. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  891. *
  892. * @return bool TRUE if value was different, therefor updated in database
  893. * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one
  894. * @since 31.0.0
  895. * @see IUserConfig for explanation about lazy loading
  896. */
  897. public function setValueFloat(
  898. string $userId,
  899. string $app,
  900. string $key,
  901. float $value,
  902. bool $lazy = false,
  903. int $flags = 0,
  904. ): bool {
  905. return $this->setTypedValue(
  906. $userId,
  907. $app,
  908. $key,
  909. (string)$value,
  910. $lazy,
  911. $flags,
  912. ValueType::FLOAT
  913. );
  914. }
  915. /**
  916. * @inheritDoc
  917. *
  918. * @param string $userId id of the user
  919. * @param string $app id of the app
  920. * @param string $key config key
  921. * @param bool $value config value
  922. * @param bool $lazy set config as lazy loaded
  923. *
  924. * @return bool TRUE if value was different, therefor updated in database
  925. * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one
  926. * @since 31.0.0
  927. * @see IUserConfig for explanation about lazy loading
  928. */
  929. public function setValueBool(
  930. string $userId,
  931. string $app,
  932. string $key,
  933. bool $value,
  934. bool $lazy = false,
  935. int $flags = 0,
  936. ): bool {
  937. return $this->setTypedValue(
  938. $userId,
  939. $app,
  940. $key,
  941. ($value) ? '1' : '0',
  942. $lazy,
  943. $flags,
  944. ValueType::BOOL
  945. );
  946. }
  947. /**
  948. * @inheritDoc
  949. *
  950. * @param string $userId id of the user
  951. * @param string $app id of the app
  952. * @param string $key config key
  953. * @param array $value config value
  954. * @param bool $lazy set config as lazy loaded
  955. * @param bool $sensitive if TRUE value will be hidden when listing config values.
  956. *
  957. * @return bool TRUE if value was different, therefor updated in database
  958. * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one
  959. * @throws JsonException
  960. * @since 31.0.0
  961. * @see IUserConfig for explanation about lazy loading
  962. */
  963. public function setValueArray(
  964. string $userId,
  965. string $app,
  966. string $key,
  967. array $value,
  968. bool $lazy = false,
  969. int $flags = 0,
  970. ): bool {
  971. try {
  972. return $this->setTypedValue(
  973. $userId,
  974. $app,
  975. $key,
  976. json_encode($value, JSON_THROW_ON_ERROR),
  977. $lazy,
  978. $flags,
  979. ValueType::ARRAY
  980. );
  981. } catch (JsonException $e) {
  982. $this->logger->warning('could not setValueArray', ['app' => $app, 'key' => $key, 'exception' => $e]);
  983. throw $e;
  984. }
  985. }
  986. /**
  987. * Store a config key and its value in database
  988. *
  989. * If config key is already known with the exact same config value and same sensitive/lazy status, the
  990. * database is not updated. If config value was previously stored as sensitive, status will not be
  991. * altered.
  992. *
  993. * @param string $userId id of the user
  994. * @param string $app id of the app
  995. * @param string $key config key
  996. * @param string $value config value
  997. * @param bool $lazy config set as lazy loaded
  998. * @param ValueType $type value type
  999. *
  1000. * @return bool TRUE if value was updated in database
  1001. * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one
  1002. * @see IUserConfig for explanation about lazy loading
  1003. */
  1004. private function setTypedValue(
  1005. string $userId,
  1006. string $app,
  1007. string $key,
  1008. string $value,
  1009. bool $lazy,
  1010. int $flags,
  1011. ValueType $type,
  1012. ): bool {
  1013. // Primary email addresses are always(!) expected to be lowercase
  1014. if ($app === 'settings' && $key === 'email') {
  1015. $value = strtolower($value);
  1016. }
  1017. $this->assertParams($userId, $app, $key);
  1018. if (!$this->matchAndApplyLexiconDefinition($userId, $app, $key, $lazy, $type, $flags)) {
  1019. // returns false as database is not updated
  1020. return false;
  1021. }
  1022. $this->loadConfig($userId, $lazy);
  1023. $inserted = $refreshCache = false;
  1024. $origValue = $value;
  1025. $sensitive = $this->isFlagged(self::FLAG_SENSITIVE, $flags);
  1026. if ($sensitive || ($this->hasKey($userId, $app, $key, $lazy) && $this->isSensitive($userId, $app, $key, $lazy))) {
  1027. $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value);
  1028. $flags |= self::FLAG_SENSITIVE;
  1029. }
  1030. // if requested, we fill the 'indexed' field with current value
  1031. $indexed = '';
  1032. if ($type !== ValueType::ARRAY && $this->isFlagged(self::FLAG_INDEXED, $flags)) {
  1033. if ($this->isFlagged(self::FLAG_SENSITIVE, $flags)) {
  1034. $this->logger->warning('sensitive value are not to be indexed');
  1035. } elseif (strlen($value) > self::USER_MAX_LENGTH) {
  1036. $this->logger->warning('value is too lengthy to be indexed');
  1037. } else {
  1038. $indexed = $value;
  1039. }
  1040. }
  1041. $oldValue = null;
  1042. if ($this->hasKey($userId, $app, $key, $lazy)) {
  1043. /**
  1044. * no update if key is already known with set lazy status and value is
  1045. * not different, unless sensitivity is switched from false to true.
  1046. */
  1047. $oldValue = $this->getTypedValue($userId, $app, $key, $value, $lazy, $type);
  1048. if ($origValue === $oldValue
  1049. && (!$sensitive || $this->isSensitive($userId, $app, $key, $lazy))) {
  1050. return false;
  1051. }
  1052. } else {
  1053. /**
  1054. * if key is not known yet, we try to insert.
  1055. * It might fail if the key exists with a different lazy flag.
  1056. */
  1057. try {
  1058. $insert = $this->connection->getQueryBuilder();
  1059. $insert->insert('preferences')
  1060. ->setValue('userid', $insert->createNamedParameter($userId))
  1061. ->setValue('appid', $insert->createNamedParameter($app))
  1062. ->setValue('lazy', $insert->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT))
  1063. ->setValue('type', $insert->createNamedParameter($type->value, IQueryBuilder::PARAM_INT))
  1064. ->setValue('flags', $insert->createNamedParameter($flags, IQueryBuilder::PARAM_INT))
  1065. ->setValue('indexed', $insert->createNamedParameter($indexed))
  1066. ->setValue('configkey', $insert->createNamedParameter($key))
  1067. ->setValue('configvalue', $insert->createNamedParameter($value));
  1068. $insert->executeStatement();
  1069. $inserted = true;
  1070. } catch (DBException $e) {
  1071. if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
  1072. // TODO: throw exception or just log and returns false !?
  1073. throw $e;
  1074. }
  1075. }
  1076. }
  1077. /**
  1078. * We cannot insert a new row, meaning we need to update an already existing one
  1079. */
  1080. if (!$inserted) {
  1081. $currType = $this->valueDetails[$userId][$app][$key]['type'] ?? null;
  1082. if ($currType === null) { // this might happen when switching lazy loading status
  1083. $this->loadConfigAll($userId);
  1084. $currType = $this->valueDetails[$userId][$app][$key]['type'];
  1085. }
  1086. /**
  1087. * We only log a warning and set it to VALUE_MIXED.
  1088. */
  1089. if ($currType === null) {
  1090. $this->logger->warning('Value type is set to zero (0) in database. This is not supposed to happens', ['app' => $app, 'key' => $key]);
  1091. $currType = ValueType::MIXED;
  1092. }
  1093. /**
  1094. * we only accept a different type from the one stored in database
  1095. * if the one stored in database is not-defined (VALUE_MIXED)
  1096. */
  1097. if ($currType !== ValueType::MIXED
  1098. && $currType !== $type) {
  1099. try {
  1100. $currTypeDef = $currType->getDefinition();
  1101. $typeDef = $type->getDefinition();
  1102. } catch (IncorrectTypeException) {
  1103. $currTypeDef = $currType->value;
  1104. $typeDef = $type->value;
  1105. }
  1106. throw new TypeConflictException('conflict between new type (' . $typeDef . ') and old type (' . $currTypeDef . ')');
  1107. }
  1108. if ($lazy !== $this->isLazy($userId, $app, $key)) {
  1109. $refreshCache = true;
  1110. }
  1111. $update = $this->connection->getQueryBuilder();
  1112. $update->update('preferences')
  1113. ->set('configvalue', $update->createNamedParameter($value))
  1114. ->set('lazy', $update->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT))
  1115. ->set('type', $update->createNamedParameter($type->value, IQueryBuilder::PARAM_INT))
  1116. ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT))
  1117. ->set('indexed', $update->createNamedParameter($indexed))
  1118. ->where($update->expr()->eq('userid', $update->createNamedParameter($userId)))
  1119. ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app)))
  1120. ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key)));
  1121. $update->executeStatement();
  1122. }
  1123. $this->dispatcher->dispatchTyped(new UserConfigChangedEvent($userId, $app, $key, $value, $oldValue));
  1124. if ($refreshCache) {
  1125. $this->clearCache($userId);
  1126. return true;
  1127. }
  1128. // update local cache
  1129. if ($lazy) {
  1130. $this->lazyCache[$userId][$app][$key] = $value;
  1131. } else {
  1132. $this->fastCache[$userId][$app][$key] = $value;
  1133. }
  1134. $this->valueDetails[$userId][$app][$key] = [
  1135. 'type' => $type,
  1136. 'flags' => $flags
  1137. ];
  1138. return true;
  1139. }
  1140. /**
  1141. * Change the type of config value.
  1142. *
  1143. * **WARNING:** Method is internal and **MUST** not be used as it may break things.
  1144. *
  1145. * @param string $userId id of the user
  1146. * @param string $app id of the app
  1147. * @param string $key config key
  1148. * @param ValueType $type value type
  1149. *
  1150. * @return bool TRUE if database update were necessary
  1151. * @throws UnknownKeyException if $key is now known in database
  1152. * @throws IncorrectTypeException if $type is not valid
  1153. * @internal
  1154. * @since 31.0.0
  1155. */
  1156. public function updateType(string $userId, string $app, string $key, ValueType $type = ValueType::MIXED): bool {
  1157. $this->assertParams($userId, $app, $key);
  1158. $this->loadConfigAll($userId);
  1159. $this->matchAndApplyLexiconDefinition($userId, $app, $key);
  1160. $this->isLazy($userId, $app, $key); // confirm key exists
  1161. $update = $this->connection->getQueryBuilder();
  1162. $update->update('preferences')
  1163. ->set('type', $update->createNamedParameter($type->value, IQueryBuilder::PARAM_INT))
  1164. ->where($update->expr()->eq('userid', $update->createNamedParameter($userId)))
  1165. ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app)))
  1166. ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key)));
  1167. $update->executeStatement();
  1168. $this->valueDetails[$userId][$app][$key]['type'] = $type;
  1169. return true;
  1170. }
  1171. /**
  1172. * @inheritDoc
  1173. *
  1174. * @param string $userId id of the user
  1175. * @param string $app id of the app
  1176. * @param string $key config key
  1177. * @param bool $sensitive TRUE to set as sensitive, FALSE to unset
  1178. *
  1179. * @return bool TRUE if entry was found in database and an update was necessary
  1180. * @since 31.0.0
  1181. */
  1182. public function updateSensitive(string $userId, string $app, string $key, bool $sensitive): bool {
  1183. $this->assertParams($userId, $app, $key);
  1184. $this->loadConfigAll($userId);
  1185. $this->matchAndApplyLexiconDefinition($userId, $app, $key);
  1186. try {
  1187. if ($sensitive === $this->isSensitive($userId, $app, $key, null)) {
  1188. return false;
  1189. }
  1190. } catch (UnknownKeyException) {
  1191. return false;
  1192. }
  1193. $lazy = $this->isLazy($userId, $app, $key);
  1194. if ($lazy) {
  1195. $cache = $this->lazyCache;
  1196. } else {
  1197. $cache = $this->fastCache;
  1198. }
  1199. if (!isset($cache[$userId][$app][$key])) {
  1200. throw new UnknownKeyException('unknown config key');
  1201. }
  1202. $value = $cache[$userId][$app][$key];
  1203. $flags = $this->getValueFlags($userId, $app, $key);
  1204. if ($sensitive) {
  1205. $flags |= self::FLAG_SENSITIVE;
  1206. $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value);
  1207. } else {
  1208. $flags &= ~self::FLAG_SENSITIVE;
  1209. $this->decryptSensitiveValue($userId, $app, $key, $value);
  1210. }
  1211. $update = $this->connection->getQueryBuilder();
  1212. $update->update('preferences')
  1213. ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT))
  1214. ->set('configvalue', $update->createNamedParameter($value))
  1215. ->where($update->expr()->eq('userid', $update->createNamedParameter($userId)))
  1216. ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app)))
  1217. ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key)));
  1218. $update->executeStatement();
  1219. $this->valueDetails[$userId][$app][$key]['flags'] = $flags;
  1220. return true;
  1221. }
  1222. /**
  1223. * @inheritDoc
  1224. *
  1225. * @param string $app
  1226. * @param string $key
  1227. * @param bool $sensitive
  1228. *
  1229. * @since 31.0.0
  1230. */
  1231. public function updateGlobalSensitive(string $app, string $key, bool $sensitive): void {
  1232. $this->assertParams('', $app, $key, allowEmptyUser: true);
  1233. $this->matchAndApplyLexiconDefinition('', $app, $key);
  1234. foreach (array_keys($this->getValuesByUsers($app, $key)) as $userId) {
  1235. try {
  1236. $this->updateSensitive($userId, $app, $key, $sensitive);
  1237. } catch (UnknownKeyException) {
  1238. // should not happen and can be ignored
  1239. }
  1240. }
  1241. // we clear all cache
  1242. $this->clearCacheAll();
  1243. }
  1244. /**
  1245. * @inheritDoc
  1246. *
  1247. * @param string $userId
  1248. * @param string $app
  1249. * @param string $key
  1250. * @param bool $indexed
  1251. *
  1252. * @return bool
  1253. * @throws DBException
  1254. * @throws IncorrectTypeException
  1255. * @throws UnknownKeyException
  1256. * @since 31.0.0
  1257. */
  1258. public function updateIndexed(string $userId, string $app, string $key, bool $indexed): bool {
  1259. $this->assertParams($userId, $app, $key);
  1260. $this->loadConfigAll($userId);
  1261. $this->matchAndApplyLexiconDefinition($userId, $app, $key);
  1262. try {
  1263. if ($indexed === $this->isIndexed($userId, $app, $key, null)) {
  1264. return false;
  1265. }
  1266. } catch (UnknownKeyException) {
  1267. return false;
  1268. }
  1269. $lazy = $this->isLazy($userId, $app, $key);
  1270. if ($lazy) {
  1271. $cache = $this->lazyCache;
  1272. } else {
  1273. $cache = $this->fastCache;
  1274. }
  1275. if (!isset($cache[$userId][$app][$key])) {
  1276. throw new UnknownKeyException('unknown config key');
  1277. }
  1278. $value = $cache[$userId][$app][$key];
  1279. $flags = $this->getValueFlags($userId, $app, $key);
  1280. if ($indexed) {
  1281. $indexed = $value;
  1282. } else {
  1283. $flags &= ~self::FLAG_INDEXED;
  1284. $indexed = '';
  1285. }
  1286. $update = $this->connection->getQueryBuilder();
  1287. $update->update('preferences')
  1288. ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT))
  1289. ->set('indexed', $update->createNamedParameter($indexed))
  1290. ->where($update->expr()->eq('userid', $update->createNamedParameter($userId)))
  1291. ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app)))
  1292. ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key)));
  1293. $update->executeStatement();
  1294. $this->valueDetails[$userId][$app][$key]['flags'] = $flags;
  1295. return true;
  1296. }
  1297. /**
  1298. * @inheritDoc
  1299. *
  1300. * @param string $app
  1301. * @param string $key
  1302. * @param bool $indexed
  1303. *
  1304. * @since 31.0.0
  1305. */
  1306. public function updateGlobalIndexed(string $app, string $key, bool $indexed): void {
  1307. $this->assertParams('', $app, $key, allowEmptyUser: true);
  1308. $this->matchAndApplyLexiconDefinition('', $app, $key);
  1309. $update = $this->connection->getQueryBuilder();
  1310. $update->update('preferences')
  1311. ->where(
  1312. $update->expr()->eq('appid', $update->createNamedParameter($app)),
  1313. $update->expr()->eq('configkey', $update->createNamedParameter($key))
  1314. );
  1315. // switching flags 'indexed' on and off is about adding/removing the bit value on the correct entries
  1316. if ($indexed) {
  1317. $update->set('indexed', $update->func()->substring('configvalue', $update->createNamedParameter(1, IQueryBuilder::PARAM_INT), $update->createNamedParameter(64, IQueryBuilder::PARAM_INT)));
  1318. $update->set('flags', $update->func()->add('flags', $update->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)));
  1319. $update->andWhere(
  1320. $update->expr()->neq($update->expr()->castColumn(
  1321. $update->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), IQueryBuilder::PARAM_INT), $update->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)
  1322. ));
  1323. } else {
  1324. // emptying field 'indexed' if key is not set as indexed anymore
  1325. $update->set('indexed', $update->createNamedParameter(''));
  1326. $update->set('flags', $update->func()->subtract('flags', $update->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)));
  1327. $update->andWhere(
  1328. $update->expr()->eq($update->expr()->castColumn(
  1329. $update->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), IQueryBuilder::PARAM_INT), $update->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)
  1330. ));
  1331. }
  1332. $update->executeStatement();
  1333. // we clear all cache
  1334. $this->clearCacheAll();
  1335. }
  1336. /**
  1337. * @inheritDoc
  1338. *
  1339. * @param string $userId id of the user
  1340. * @param string $app id of the app
  1341. * @param string $key config key
  1342. * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset
  1343. *
  1344. * @return bool TRUE if entry was found in database and an update was necessary
  1345. * @since 31.0.0
  1346. */
  1347. public function updateLazy(string $userId, string $app, string $key, bool $lazy): bool {
  1348. $this->assertParams($userId, $app, $key);
  1349. $this->loadConfigAll($userId);
  1350. $this->matchAndApplyLexiconDefinition($userId, $app, $key);
  1351. try {
  1352. if ($lazy === $this->isLazy($userId, $app, $key)) {
  1353. return false;
  1354. }
  1355. } catch (UnknownKeyException) {
  1356. return false;
  1357. }
  1358. $update = $this->connection->getQueryBuilder();
  1359. $update->update('preferences')
  1360. ->set('lazy', $update->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT))
  1361. ->where($update->expr()->eq('userid', $update->createNamedParameter($userId)))
  1362. ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app)))
  1363. ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key)));
  1364. $update->executeStatement();
  1365. // At this point, it is a lot safer to clean cache
  1366. $this->clearCache($userId);
  1367. return true;
  1368. }
  1369. /**
  1370. * @inheritDoc
  1371. *
  1372. * @param string $app id of the app
  1373. * @param string $key config key
  1374. * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset
  1375. *
  1376. * @since 31.0.0
  1377. */
  1378. public function updateGlobalLazy(string $app, string $key, bool $lazy): void {
  1379. $this->assertParams('', $app, $key, allowEmptyUser: true);
  1380. $this->matchAndApplyLexiconDefinition('', $app, $key);
  1381. $update = $this->connection->getQueryBuilder();
  1382. $update->update('preferences')
  1383. ->set('lazy', $update->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT))
  1384. ->where($update->expr()->eq('appid', $update->createNamedParameter($app)))
  1385. ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key)));
  1386. $update->executeStatement();
  1387. $this->clearCacheAll();
  1388. }
  1389. /**
  1390. * @inheritDoc
  1391. *
  1392. * @param string $userId id of the user
  1393. * @param string $app id of the app
  1394. * @param string $key config key
  1395. *
  1396. * @return array
  1397. * @throws UnknownKeyException if config key is not known in database
  1398. * @since 31.0.0
  1399. */
  1400. public function getDetails(string $userId, string $app, string $key): array {
  1401. $this->assertParams($userId, $app, $key);
  1402. $this->loadConfigAll($userId);
  1403. $this->matchAndApplyLexiconDefinition($userId, $app, $key);
  1404. $lazy = $this->isLazy($userId, $app, $key);
  1405. if ($lazy) {
  1406. $cache = $this->lazyCache[$userId];
  1407. } else {
  1408. $cache = $this->fastCache[$userId];
  1409. }
  1410. $type = $this->getValueType($userId, $app, $key);
  1411. try {
  1412. $typeString = $type->getDefinition();
  1413. } catch (IncorrectTypeException $e) {
  1414. $this->logger->warning('type stored in database is not correct', ['exception' => $e, 'type' => $type]);
  1415. $typeString = (string)$type->value;
  1416. }
  1417. if (!isset($cache[$app][$key])) {
  1418. throw new UnknownKeyException('unknown config key');
  1419. }
  1420. $value = $cache[$app][$key];
  1421. $sensitive = $this->isSensitive($userId, $app, $key, null);
  1422. $this->decryptSensitiveValue($userId, $app, $key, $value);
  1423. return [
  1424. 'userId' => $userId,
  1425. 'app' => $app,
  1426. 'key' => $key,
  1427. 'value' => $value,
  1428. 'type' => $type->value,
  1429. 'lazy' => $lazy,
  1430. 'typeString' => $typeString,
  1431. 'sensitive' => $sensitive
  1432. ];
  1433. }
  1434. /**
  1435. * @inheritDoc
  1436. *
  1437. * @param string $userId id of the user
  1438. * @param string $app id of the app
  1439. * @param string $key config key
  1440. *
  1441. * @since 31.0.0
  1442. */
  1443. public function deleteUserConfig(string $userId, string $app, string $key): void {
  1444. $this->assertParams($userId, $app, $key);
  1445. $this->matchAndApplyLexiconDefinition($userId, $app, $key);
  1446. $qb = $this->connection->getQueryBuilder();
  1447. $qb->delete('preferences')
  1448. ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId)))
  1449. ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter($app)))
  1450. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key)));
  1451. $qb->executeStatement();
  1452. unset($this->lazyCache[$userId][$app][$key]);
  1453. unset($this->fastCache[$userId][$app][$key]);
  1454. unset($this->valueDetails[$userId][$app][$key]);
  1455. }
  1456. /**
  1457. * @inheritDoc
  1458. *
  1459. * @param string $app id of the app
  1460. * @param string $key config key
  1461. *
  1462. * @since 31.0.0
  1463. */
  1464. public function deleteKey(string $app, string $key): void {
  1465. $this->assertParams('', $app, $key, allowEmptyUser: true);
  1466. $this->matchAndApplyLexiconDefinition('', $app, $key);
  1467. $qb = $this->connection->getQueryBuilder();
  1468. $qb->delete('preferences')
  1469. ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app)))
  1470. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key)));
  1471. $qb->executeStatement();
  1472. $this->clearCacheAll();
  1473. }
  1474. /**
  1475. * @inheritDoc
  1476. *
  1477. * @param string $app id of the app
  1478. *
  1479. * @since 31.0.0
  1480. */
  1481. public function deleteApp(string $app): void {
  1482. $this->assertParams('', $app, allowEmptyUser: true);
  1483. $qb = $this->connection->getQueryBuilder();
  1484. $qb->delete('preferences')
  1485. ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app)));
  1486. $qb->executeStatement();
  1487. $this->clearCacheAll();
  1488. }
  1489. public function deleteAllUserConfig(string $userId): void {
  1490. $this->assertParams($userId, '', allowEmptyApp: true);
  1491. $qb = $this->connection->getQueryBuilder();
  1492. $qb->delete('preferences')
  1493. ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId)));
  1494. $qb->executeStatement();
  1495. $this->clearCache($userId);
  1496. }
  1497. /**
  1498. * @inheritDoc
  1499. *
  1500. * @param string $userId id of the user
  1501. * @param bool $reload set to TRUE to refill cache instantly after clearing it.
  1502. *
  1503. * @since 31.0.0
  1504. */
  1505. public function clearCache(string $userId, bool $reload = false): void {
  1506. $this->assertParams($userId, allowEmptyApp: true);
  1507. $this->lazyLoaded[$userId] = $this->fastLoaded[$userId] = false;
  1508. $this->lazyCache[$userId] = $this->fastCache[$userId] = $this->valueDetails[$userId] = [];
  1509. if (!$reload) {
  1510. return;
  1511. }
  1512. $this->loadConfigAll($userId);
  1513. }
  1514. /**
  1515. * @inheritDoc
  1516. *
  1517. * @since 31.0.0
  1518. */
  1519. public function clearCacheAll(): void {
  1520. $this->lazyLoaded = $this->fastLoaded = [];
  1521. $this->lazyCache = $this->fastCache = $this->valueDetails = $this->configLexiconDetails = [];
  1522. }
  1523. /**
  1524. * For debug purpose.
  1525. * Returns the cached data.
  1526. *
  1527. * @return array
  1528. * @since 31.0.0
  1529. * @internal
  1530. */
  1531. public function statusCache(): array {
  1532. return [
  1533. 'fastLoaded' => $this->fastLoaded,
  1534. 'fastCache' => $this->fastCache,
  1535. 'lazyLoaded' => $this->lazyLoaded,
  1536. 'lazyCache' => $this->lazyCache,
  1537. 'valueDetails' => $this->valueDetails,
  1538. ];
  1539. }
  1540. /**
  1541. * @param int $needle bitflag to search
  1542. * @param int $flags all flags
  1543. *
  1544. * @return bool TRUE if bitflag $needle is set in $flags
  1545. */
  1546. private function isFlagged(int $needle, int $flags): bool {
  1547. return (($needle & $flags) !== 0);
  1548. }
  1549. /**
  1550. * Confirm the string set for app and key fit the database description
  1551. *
  1552. * @param string $userId
  1553. * @param string $app assert $app fit in database
  1554. * @param string $prefKey assert config key fit in database
  1555. * @param bool $allowEmptyUser
  1556. * @param bool $allowEmptyApp $app can be empty string
  1557. * @param ValueType|null $valueType assert value type is only one type
  1558. */
  1559. private function assertParams(
  1560. string $userId = '',
  1561. string $app = '',
  1562. string $prefKey = '',
  1563. bool $allowEmptyUser = false,
  1564. bool $allowEmptyApp = false,
  1565. ): void {
  1566. if (!$allowEmptyUser && $userId === '') {
  1567. throw new InvalidArgumentException('userId cannot be an empty string');
  1568. }
  1569. if (!$allowEmptyApp && $app === '') {
  1570. throw new InvalidArgumentException('app cannot be an empty string');
  1571. }
  1572. if (strlen($userId) > self::USER_MAX_LENGTH) {
  1573. throw new InvalidArgumentException('Value (' . $userId . ') for userId is too long (' . self::USER_MAX_LENGTH . ')');
  1574. }
  1575. if (strlen($app) > self::APP_MAX_LENGTH) {
  1576. throw new InvalidArgumentException('Value (' . $app . ') for app is too long (' . self::APP_MAX_LENGTH . ')');
  1577. }
  1578. if (strlen($prefKey) > self::KEY_MAX_LENGTH) {
  1579. throw new InvalidArgumentException('Value (' . $prefKey . ') for key is too long (' . self::KEY_MAX_LENGTH . ')');
  1580. }
  1581. }
  1582. private function loadConfigAll(string $userId): void {
  1583. $this->loadConfig($userId, null);
  1584. }
  1585. /**
  1586. * Load normal config or config set as lazy loaded
  1587. *
  1588. * @param bool|null $lazy set to TRUE to load config set as lazy loaded, set to NULL to load all config
  1589. */
  1590. private function loadConfig(string $userId, ?bool $lazy = false): void {
  1591. if ($this->isLoaded($userId, $lazy)) {
  1592. return;
  1593. }
  1594. if (($lazy ?? true) !== false) { // if lazy is null or true, we debug log
  1595. $this->logger->debug('The loading of lazy UserConfig values have been requested', ['exception' => new \RuntimeException('ignorable exception')]);
  1596. }
  1597. $qb = $this->connection->getQueryBuilder();
  1598. $qb->from('preferences');
  1599. $qb->select('appid', 'configkey', 'configvalue', 'type', 'flags');
  1600. $qb->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId)));
  1601. // we only need value from lazy when loadConfig does not specify it
  1602. if ($lazy !== null) {
  1603. $qb->andWhere($qb->expr()->eq('lazy', $qb->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT)));
  1604. } else {
  1605. $qb->addSelect('lazy');
  1606. }
  1607. $result = $qb->executeQuery();
  1608. $rows = $result->fetchAll();
  1609. foreach ($rows as $row) {
  1610. if (($row['lazy'] ?? ($lazy ?? 0) ? 1 : 0) === 1) {
  1611. $this->lazyCache[$userId][$row['appid']][$row['configkey']] = $row['configvalue'] ?? '';
  1612. } else {
  1613. $this->fastCache[$userId][$row['appid']][$row['configkey']] = $row['configvalue'] ?? '';
  1614. }
  1615. $this->valueDetails[$userId][$row['appid']][$row['configkey']] = ['type' => ValueType::from((int)($row['type'] ?? 0)), 'flags' => (int)$row['flags']];
  1616. }
  1617. $result->closeCursor();
  1618. $this->setAsLoaded($userId, $lazy);
  1619. }
  1620. /**
  1621. * if $lazy is:
  1622. * - false: will returns true if fast config are loaded
  1623. * - true : will returns true if lazy config are loaded
  1624. * - null : will returns true if both config are loaded
  1625. *
  1626. * @param string $userId
  1627. * @param bool $lazy
  1628. *
  1629. * @return bool
  1630. */
  1631. private function isLoaded(string $userId, ?bool $lazy): bool {
  1632. if ($lazy === null) {
  1633. return ($this->lazyLoaded[$userId] ?? false) && ($this->fastLoaded[$userId] ?? false);
  1634. }
  1635. return $lazy ? $this->lazyLoaded[$userId] ?? false : $this->fastLoaded[$userId] ?? false;
  1636. }
  1637. /**
  1638. * if $lazy is:
  1639. * - false: set fast config as loaded
  1640. * - true : set lazy config as loaded
  1641. * - null : set both config as loaded
  1642. *
  1643. * @param string $userId
  1644. * @param bool $lazy
  1645. */
  1646. private function setAsLoaded(string $userId, ?bool $lazy): void {
  1647. if ($lazy === null) {
  1648. $this->fastLoaded[$userId] = $this->lazyLoaded[$userId] = true;
  1649. return;
  1650. }
  1651. // We also create empty entry to keep both fastLoaded/lazyLoaded synced
  1652. if ($lazy) {
  1653. $this->lazyLoaded[$userId] = true;
  1654. $this->fastLoaded[$userId] = $this->fastLoaded[$userId] ?? false;
  1655. $this->fastCache[$userId] = $this->fastCache[$userId] ?? [];
  1656. } else {
  1657. $this->fastLoaded[$userId] = true;
  1658. $this->lazyLoaded[$userId] = $this->lazyLoaded[$userId] ?? false;
  1659. $this->lazyCache[$userId] = $this->lazyCache[$userId] ?? [];
  1660. }
  1661. }
  1662. /**
  1663. * **Warning:** this will load all lazy values from the database
  1664. *
  1665. * @param string $userId id of the user
  1666. * @param string $app id of the app
  1667. * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE}
  1668. *
  1669. * @return array<string, string|int|float|bool|array>
  1670. */
  1671. private function formatAppValues(string $userId, string $app, array $values, bool $filtered = false): array {
  1672. foreach ($values as $key => $value) {
  1673. //$key = (string)$key;
  1674. try {
  1675. $type = $this->getValueType($userId, $app, (string)$key);
  1676. } catch (UnknownKeyException) {
  1677. continue;
  1678. }
  1679. if ($this->isFlagged(self::FLAG_SENSITIVE, $this->valueDetails[$userId][$app][$key]['flags'] ?? 0)) {
  1680. if ($filtered) {
  1681. $value = IConfig::SENSITIVE_VALUE;
  1682. $type = ValueType::STRING;
  1683. } else {
  1684. $this->decryptSensitiveValue($userId, $app, (string)$key, $value);
  1685. }
  1686. }
  1687. $values[$key] = $this->convertTypedValue($value, $type);
  1688. }
  1689. return $values;
  1690. }
  1691. /**
  1692. * convert string value to the expected type
  1693. *
  1694. * @param string $value
  1695. * @param ValueType $type
  1696. *
  1697. * @return string|int|float|bool|array
  1698. */
  1699. private function convertTypedValue(string $value, ValueType $type): string|int|float|bool|array {
  1700. switch ($type) {
  1701. case ValueType::INT:
  1702. return (int)$value;
  1703. case ValueType::FLOAT:
  1704. return (float)$value;
  1705. case ValueType::BOOL:
  1706. return in_array(strtolower($value), ['1', 'true', 'yes', 'on']);
  1707. case ValueType::ARRAY:
  1708. try {
  1709. return json_decode($value, true, flags: JSON_THROW_ON_ERROR);
  1710. } catch (JsonException) {
  1711. // ignoreable
  1712. }
  1713. break;
  1714. }
  1715. return $value;
  1716. }
  1717. /**
  1718. * will change referenced $value with the decrypted value in case of encrypted (sensitive value)
  1719. *
  1720. * @param string $userId
  1721. * @param string $app
  1722. * @param string $key
  1723. * @param string $value
  1724. */
  1725. private function decryptSensitiveValue(string $userId, string $app, string $key, string &$value): void {
  1726. if (!$this->isFlagged(self::FLAG_SENSITIVE, $this->valueDetails[$userId][$app][$key]['flags'] ?? 0)) {
  1727. return;
  1728. }
  1729. if (!str_starts_with($value, self::ENCRYPTION_PREFIX)) {
  1730. return;
  1731. }
  1732. try {
  1733. $value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH));
  1734. } catch (\Exception $e) {
  1735. $this->logger->warning('could not decrypt sensitive value', [
  1736. 'userId' => $userId,
  1737. 'app' => $app,
  1738. 'key' => $key,
  1739. 'value' => $value,
  1740. 'exception' => $e
  1741. ]);
  1742. }
  1743. }
  1744. /**
  1745. * Match and apply current use of config values with defined lexicon.
  1746. * Set $lazy to NULL only if only interested into checking that $key is alias.
  1747. *
  1748. * @throws UnknownKeyException
  1749. * @throws TypeConflictException
  1750. * @return bool FALSE if conflict with defined lexicon were observed in the process
  1751. */
  1752. private function matchAndApplyLexiconDefinition(
  1753. string $userId,
  1754. string $app,
  1755. string &$key,
  1756. ?bool &$lazy = null,
  1757. ValueType &$type = ValueType::MIXED,
  1758. int &$flags = 0,
  1759. ?string &$default = null,
  1760. ): bool {
  1761. $configDetails = $this->getConfigDetailsFromLexicon($app);
  1762. if (array_key_exists($key, $configDetails['aliases']) && !$this->ignoreLexiconAliases) {
  1763. // in case '$rename' is set in ConfigLexiconEntry, we use the new config key
  1764. $key = $configDetails['aliases'][$key];
  1765. }
  1766. if (!array_key_exists($key, $configDetails['entries'])) {
  1767. return $this->applyLexiconStrictness($configDetails['strictness'], $app . '/' . $key);
  1768. }
  1769. // if lazy is NULL, we ignore all check on the type/lazyness/default from Lexicon
  1770. if ($lazy === null) {
  1771. return true;
  1772. }
  1773. /** @var Entry $configValue */
  1774. $configValue = $configDetails['entries'][$key];
  1775. if ($type === ValueType::MIXED) {
  1776. // we overwrite if value was requested as mixed
  1777. $type = $configValue->getValueType();
  1778. } elseif ($configValue->getValueType() !== $type) {
  1779. throw new TypeConflictException('The user config key ' . $app . '/' . $key . ' is typed incorrectly in relation to the config lexicon');
  1780. }
  1781. $lazy = $configValue->isLazy();
  1782. $flags = $configValue->getFlags();
  1783. if ($configValue->isDeprecated()) {
  1784. $this->logger->notice('User config key ' . $app . '/' . $key . ' is set as deprecated.');
  1785. }
  1786. $enforcedValue = $this->config->getSystemValue('lexicon.default.userconfig.enforced', [])[$app][$key] ?? false;
  1787. if (!$enforcedValue && $this->hasKey($userId, $app, $key, $lazy)) {
  1788. // if key exists there should be no need to extract default
  1789. return true;
  1790. }
  1791. // only look for default if needed, default from Lexicon got priority if not overwritten by admin
  1792. if ($default !== null) {
  1793. $default = $this->getSystemDefault($app, $configValue) ?? $configValue->getDefault($this->presetManager->getLexiconPreset()) ?? $default;
  1794. }
  1795. // returning false will make get() returning $default and set() not changing value in database
  1796. return !$enforcedValue;
  1797. }
  1798. /**
  1799. * get default value set in config/config.php if stored in key:
  1800. *
  1801. * 'lexicon.default.userconfig' => [
  1802. * <appId> => [
  1803. * <configKey> => 'my value',
  1804. * ]
  1805. * ],
  1806. *
  1807. * The entry is converted to string to fit the expected type when managing default value
  1808. */
  1809. private function getSystemDefault(string $appId, Entry $configValue): ?string {
  1810. $default = $this->config->getSystemValue('lexicon.default.userconfig', [])[$appId][$configValue->getKey()] ?? null;
  1811. if ($default === null) {
  1812. // no system default, using default default.
  1813. return null;
  1814. }
  1815. return $configValue->convertToString($default);
  1816. }
  1817. /**
  1818. * manage ConfigLexicon behavior based on strictness set in IConfigLexicon
  1819. *
  1820. * @param Strictness|null $strictness
  1821. * @param string $line
  1822. *
  1823. * @return bool TRUE if conflict can be fully ignored
  1824. * @throws UnknownKeyException
  1825. * @see ILexicon::getStrictness()
  1826. */
  1827. private function applyLexiconStrictness(?Strictness $strictness, string $configAppKey): bool {
  1828. if ($strictness === null) {
  1829. return true;
  1830. }
  1831. $line = 'The user config key ' . $configAppKey . ' is not defined in the config lexicon';
  1832. switch ($strictness) {
  1833. case Strictness::IGNORE:
  1834. return true;
  1835. case Strictness::NOTICE:
  1836. if (!in_array($configAppKey, $this->strictnessApplied, true)) {
  1837. $this->strictnessApplied[] = $configAppKey;
  1838. $this->logger->notice($line);
  1839. }
  1840. return true;
  1841. case Strictness::WARNING:
  1842. if (!in_array($configAppKey, $this->strictnessApplied, true)) {
  1843. $this->strictnessApplied[] = $configAppKey;
  1844. $this->logger->warning($line);
  1845. }
  1846. return false;
  1847. case Strictness::EXCEPTION:
  1848. throw new UnknownKeyException($line);
  1849. }
  1850. throw new UnknownKeyException($line);
  1851. }
  1852. /**
  1853. * extract details from registered $appId's config lexicon
  1854. *
  1855. * @param string $appId
  1856. *
  1857. * @return array{entries: array<string, Entry>, aliases: array<string, string>, strictness: Strictness}
  1858. * @internal
  1859. */
  1860. public function getConfigDetailsFromLexicon(string $appId): array {
  1861. if (!array_key_exists($appId, $this->configLexiconDetails)) {
  1862. $entries = $aliases = [];
  1863. $bootstrapCoordinator = \OCP\Server::get(Coordinator::class);
  1864. $configLexicon = $bootstrapCoordinator->getRegistrationContext()?->getConfigLexicon($appId);
  1865. foreach ($configLexicon?->getUserConfigs() ?? [] as $configEntry) {
  1866. $entries[$configEntry->getKey()] = $configEntry;
  1867. if ($configEntry->getRename() !== null) {
  1868. $aliases[$configEntry->getRename()] = $configEntry->getKey();
  1869. }
  1870. }
  1871. $this->configLexiconDetails[$appId] = [
  1872. 'entries' => $entries,
  1873. 'aliases' => $aliases,
  1874. 'strictness' => $configLexicon?->getStrictness() ?? Strictness::IGNORE
  1875. ];
  1876. }
  1877. return $this->configLexiconDetails[$appId];
  1878. }
  1879. /**
  1880. * get Lexicon Entry using appId and config key entry
  1881. *
  1882. * @return Entry|null NULL if entry does not exist in user's Lexicon
  1883. * @internal
  1884. */
  1885. public function getLexiconEntry(string $appId, string $key): ?Entry {
  1886. return $this->getConfigDetailsFromLexicon($appId)['entries'][$key] ?? null;
  1887. }
  1888. /**
  1889. * if set to TRUE, ignore aliases defined in Config Lexicon during the use of the methods of this class
  1890. *
  1891. * @internal
  1892. */
  1893. public function ignoreLexiconAliases(bool $ignore): void {
  1894. $this->ignoreLexiconAliases = $ignore;
  1895. }
  1896. }