UserConfig.php 55 KB

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