AllConfig.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Loki3000 <github@labcms.ru>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Robin McCorkell <robin@mccorkell.me.uk>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. * @author Vincent Petry <pvince81@owncloud.com>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OC;
  33. use OC\Cache\CappedMemoryCache;
  34. use OCP\IDBConnection;
  35. use OCP\PreConditionNotMetException;
  36. /**
  37. * Class to combine all the configuration options ownCloud offers
  38. */
  39. class AllConfig implements \OCP\IConfig {
  40. /** @var SystemConfig */
  41. private $systemConfig;
  42. /** @var IDBConnection */
  43. private $connection;
  44. /**
  45. * 3 dimensional array with the following structure:
  46. * [ $userId =>
  47. * [ $appId =>
  48. * [ $key => $value ]
  49. * ]
  50. * ]
  51. *
  52. * database table: preferences
  53. *
  54. * methods that use this:
  55. * - setUserValue
  56. * - getUserValue
  57. * - getUserKeys
  58. * - deleteUserValue
  59. * - deleteAllUserValues
  60. * - deleteAppFromAllUsers
  61. *
  62. * @var CappedMemoryCache $userCache
  63. */
  64. private $userCache;
  65. /**
  66. * @param SystemConfig $systemConfig
  67. */
  68. public function __construct(SystemConfig $systemConfig) {
  69. $this->userCache = new CappedMemoryCache();
  70. $this->systemConfig = $systemConfig;
  71. }
  72. /**
  73. * TODO - FIXME This fixes an issue with base.php that cause cyclic
  74. * dependencies, especially with autoconfig setup
  75. *
  76. * Replace this by properly injected database connection. Currently the
  77. * base.php triggers the getDatabaseConnection too early which causes in
  78. * autoconfig setup case a too early distributed database connection and
  79. * the autoconfig then needs to reinit all already initialized dependencies
  80. * that use the database connection.
  81. *
  82. * otherwise a SQLite database is created in the wrong directory
  83. * because the database connection was created with an uninitialized config
  84. */
  85. private function fixDIInit() {
  86. if($this->connection === null) {
  87. $this->connection = \OC::$server->getDatabaseConnection();
  88. }
  89. }
  90. /**
  91. * Sets and deletes system wide values
  92. *
  93. * @param array $configs Associative array with `key => value` pairs
  94. * If value is null, the config key will be deleted
  95. */
  96. public function setSystemValues(array $configs) {
  97. $this->systemConfig->setValues($configs);
  98. }
  99. /**
  100. * Sets a new system wide value
  101. *
  102. * @param string $key the key of the value, under which will be saved
  103. * @param mixed $value the value that should be stored
  104. */
  105. public function setSystemValue($key, $value) {
  106. $this->systemConfig->setValue($key, $value);
  107. }
  108. /**
  109. * Looks up a system wide defined value
  110. *
  111. * @param string $key the key of the value, under which it was saved
  112. * @param mixed $default the default value to be returned if the value isn't set
  113. * @return mixed the value or $default
  114. */
  115. public function getSystemValue($key, $default = '') {
  116. return $this->systemConfig->getValue($key, $default);
  117. }
  118. /**
  119. * Looks up a boolean system wide defined value
  120. *
  121. * @param string $key the key of the value, under which it was saved
  122. * @param mixed $default the default value to be returned if the value isn't set
  123. * @return mixed the value or $default
  124. * @since 16.0.0
  125. */
  126. public function getSystemValueBool(string $key, bool $default = false): bool {
  127. return (bool) $this->getSystemValue($key, $default);
  128. }
  129. /**
  130. * Looks up an integer system wide defined value
  131. *
  132. * @param string $key the key of the value, under which it was saved
  133. * @param mixed $default the default value to be returned if the value isn't set
  134. * @return mixed the value or $default
  135. * @since 16.0.0
  136. */
  137. public function getSystemValueInt(string $key, int $default = 0): int {
  138. return (int) $this->getSystemValue($key, $default);
  139. }
  140. /**
  141. * Looks up a string system wide defined value
  142. *
  143. * @param string $key the key of the value, under which it was saved
  144. * @param mixed $default the default value to be returned if the value isn't set
  145. * @return mixed the value or $default
  146. * @since 16.0.0
  147. */
  148. public function getSystemValueString(string $key, string $default = ''): string {
  149. return (string) $this->getSystemValue($key, $default);
  150. }
  151. /**
  152. * Looks up a system wide defined value and filters out sensitive data
  153. *
  154. * @param string $key the key of the value, under which it was saved
  155. * @param mixed $default the default value to be returned if the value isn't set
  156. * @return mixed the value or $default
  157. */
  158. public function getFilteredSystemValue($key, $default = '') {
  159. return $this->systemConfig->getFilteredValue($key, $default);
  160. }
  161. /**
  162. * Delete a system wide defined value
  163. *
  164. * @param string $key the key of the value, under which it was saved
  165. */
  166. public function deleteSystemValue($key) {
  167. $this->systemConfig->deleteValue($key);
  168. }
  169. /**
  170. * Get all keys stored for an app
  171. *
  172. * @param string $appName the appName that we stored the value under
  173. * @return string[] the keys stored for the app
  174. */
  175. public function getAppKeys($appName) {
  176. return \OC::$server->query(\OC\AppConfig::class)->getKeys($appName);
  177. }
  178. /**
  179. * Writes a new app wide value
  180. *
  181. * @param string $appName the appName that we want to store the value under
  182. * @param string $key the key of the value, under which will be saved
  183. * @param string|float|int $value the value that should be stored
  184. */
  185. public function setAppValue($appName, $key, $value) {
  186. \OC::$server->query(\OC\AppConfig::class)->setValue($appName, $key, $value);
  187. }
  188. /**
  189. * Looks up an app wide defined value
  190. *
  191. * @param string $appName the appName that we stored the value under
  192. * @param string $key the key of the value, under which it was saved
  193. * @param string $default the default value to be returned if the value isn't set
  194. * @return string the saved value
  195. */
  196. public function getAppValue($appName, $key, $default = '') {
  197. return \OC::$server->query(\OC\AppConfig::class)->getValue($appName, $key, $default);
  198. }
  199. /**
  200. * Delete an app wide defined value
  201. *
  202. * @param string $appName the appName that we stored the value under
  203. * @param string $key the key of the value, under which it was saved
  204. */
  205. public function deleteAppValue($appName, $key) {
  206. \OC::$server->query(\OC\AppConfig::class)->deleteKey($appName, $key);
  207. }
  208. /**
  209. * Removes all keys in appconfig belonging to the app
  210. *
  211. * @param string $appName the appName the configs are stored under
  212. */
  213. public function deleteAppValues($appName) {
  214. \OC::$server->query(\OC\AppConfig::class)->deleteApp($appName);
  215. }
  216. /**
  217. * Set a user defined value
  218. *
  219. * @param string $userId the userId of the user that we want to store the value under
  220. * @param string $appName the appName that we want to store the value under
  221. * @param string $key the key under which the value is being stored
  222. * @param string|float|int $value the value that you want to store
  223. * @param string $preCondition only update if the config value was previously the value passed as $preCondition
  224. * @throws \OCP\PreConditionNotMetException if a precondition is specified and is not met
  225. * @throws \UnexpectedValueException when trying to store an unexpected value
  226. */
  227. public function setUserValue($userId, $appName, $key, $value, $preCondition = null) {
  228. if (!is_int($value) && !is_float($value) && !is_string($value)) {
  229. throw new \UnexpectedValueException('Only integers, floats and strings are allowed as value');
  230. }
  231. // TODO - FIXME
  232. $this->fixDIInit();
  233. $prevValue = $this->getUserValue($userId, $appName, $key, null);
  234. if ($prevValue !== null) {
  235. if ($prevValue === (string)$value) {
  236. return;
  237. } else if ($preCondition !== null && $prevValue !== (string)$preCondition) {
  238. throw new PreConditionNotMetException();
  239. } else {
  240. $qb = $this->connection->getQueryBuilder();
  241. $qb->update('preferences')
  242. ->set('configvalue', $qb->createNamedParameter($value))
  243. ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId)))
  244. ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter($appName)))
  245. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key)));
  246. $qb->execute();
  247. $this->userCache[$userId][$appName][$key] = (string)$value;
  248. return;
  249. }
  250. }
  251. $preconditionArray = [];
  252. if (isset($preCondition)) {
  253. $preconditionArray = [
  254. 'configvalue' => $preCondition,
  255. ];
  256. }
  257. $this->connection->setValues('preferences', [
  258. 'userid' => $userId,
  259. 'appid' => $appName,
  260. 'configkey' => $key,
  261. ], [
  262. 'configvalue' => $value,
  263. ], $preconditionArray);
  264. // only add to the cache if we already loaded data for the user
  265. if (isset($this->userCache[$userId])) {
  266. if (!isset($this->userCache[$userId][$appName])) {
  267. $this->userCache[$userId][$appName] = array();
  268. }
  269. $this->userCache[$userId][$appName][$key] = (string)$value;
  270. }
  271. }
  272. /**
  273. * Getting a user defined value
  274. *
  275. * @param string $userId the userId of the user that we want to store the value under
  276. * @param string $appName the appName that we stored the value under
  277. * @param string $key the key under which the value is being stored
  278. * @param mixed $default the default value to be returned if the value isn't set
  279. * @return string
  280. */
  281. public function getUserValue($userId, $appName, $key, $default = '') {
  282. $data = $this->getUserValues($userId);
  283. if (isset($data[$appName]) and isset($data[$appName][$key])) {
  284. return $data[$appName][$key];
  285. } else {
  286. return $default;
  287. }
  288. }
  289. /**
  290. * Get the keys of all stored by an app for the user
  291. *
  292. * @param string $userId the userId of the user that we want to store the value under
  293. * @param string $appName the appName that we stored the value under
  294. * @return string[]
  295. */
  296. public function getUserKeys($userId, $appName) {
  297. $data = $this->getUserValues($userId);
  298. if (isset($data[$appName])) {
  299. return array_keys($data[$appName]);
  300. } else {
  301. return array();
  302. }
  303. }
  304. /**
  305. * Delete a user value
  306. *
  307. * @param string $userId the userId of the user that we want to store the value under
  308. * @param string $appName the appName that we stored the value under
  309. * @param string $key the key under which the value is being stored
  310. */
  311. public function deleteUserValue($userId, $appName, $key) {
  312. // TODO - FIXME
  313. $this->fixDIInit();
  314. $sql = 'DELETE FROM `*PREFIX*preferences` '.
  315. 'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?';
  316. $this->connection->executeUpdate($sql, array($userId, $appName, $key));
  317. if (isset($this->userCache[$userId]) and isset($this->userCache[$userId][$appName])) {
  318. unset($this->userCache[$userId][$appName][$key]);
  319. }
  320. }
  321. /**
  322. * Delete all user values
  323. *
  324. * @param string $userId the userId of the user that we want to remove all values from
  325. */
  326. public function deleteAllUserValues($userId) {
  327. // TODO - FIXME
  328. $this->fixDIInit();
  329. $sql = 'DELETE FROM `*PREFIX*preferences` '.
  330. 'WHERE `userid` = ?';
  331. $this->connection->executeUpdate($sql, array($userId));
  332. unset($this->userCache[$userId]);
  333. }
  334. /**
  335. * Delete all user related values of one app
  336. *
  337. * @param string $appName the appName of the app that we want to remove all values from
  338. */
  339. public function deleteAppFromAllUsers($appName) {
  340. // TODO - FIXME
  341. $this->fixDIInit();
  342. $sql = 'DELETE FROM `*PREFIX*preferences` '.
  343. 'WHERE `appid` = ?';
  344. $this->connection->executeUpdate($sql, array($appName));
  345. foreach ($this->userCache as &$userCache) {
  346. unset($userCache[$appName]);
  347. }
  348. }
  349. /**
  350. * Returns all user configs sorted by app of one user
  351. *
  352. * @param string $userId the user ID to get the app configs from
  353. * @return array[] - 2 dimensional array with the following structure:
  354. * [ $appId =>
  355. * [ $key => $value ]
  356. * ]
  357. */
  358. private function getUserValues($userId) {
  359. if (isset($this->userCache[$userId])) {
  360. return $this->userCache[$userId];
  361. }
  362. if ($userId === null || $userId === '') {
  363. $this->userCache[$userId]=array();
  364. return $this->userCache[$userId];
  365. }
  366. // TODO - FIXME
  367. $this->fixDIInit();
  368. $data = array();
  369. $query = 'SELECT `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?';
  370. $result = $this->connection->executeQuery($query, array($userId));
  371. while ($row = $result->fetch()) {
  372. $appId = $row['appid'];
  373. if (!isset($data[$appId])) {
  374. $data[$appId] = array();
  375. }
  376. $data[$appId][$row['configkey']] = $row['configvalue'];
  377. }
  378. $this->userCache[$userId] = $data;
  379. return $data;
  380. }
  381. /**
  382. * Fetches a mapped list of userId -> value, for a specified app and key and a list of user IDs.
  383. *
  384. * @param string $appName app to get the value for
  385. * @param string $key the key to get the value for
  386. * @param array $userIds the user IDs to fetch the values for
  387. * @return array Mapped values: userId => value
  388. */
  389. public function getUserValueForUsers($appName, $key, $userIds) {
  390. // TODO - FIXME
  391. $this->fixDIInit();
  392. if (empty($userIds) || !is_array($userIds)) {
  393. return array();
  394. }
  395. $chunkedUsers = array_chunk($userIds, 50, true);
  396. $placeholders50 = implode(',', array_fill(0, 50, '?'));
  397. $userValues = array();
  398. foreach ($chunkedUsers as $chunk) {
  399. $queryParams = $chunk;
  400. // create [$app, $key, $chunkedUsers]
  401. array_unshift($queryParams, $key);
  402. array_unshift($queryParams, $appName);
  403. $placeholders = (count($chunk) === 50) ? $placeholders50 : implode(',', array_fill(0, count($chunk), '?'));
  404. $query = 'SELECT `userid`, `configvalue` ' .
  405. 'FROM `*PREFIX*preferences` ' .
  406. 'WHERE `appid` = ? AND `configkey` = ? ' .
  407. 'AND `userid` IN (' . $placeholders . ')';
  408. $result = $this->connection->executeQuery($query, $queryParams);
  409. while ($row = $result->fetch()) {
  410. $userValues[$row['userid']] = $row['configvalue'];
  411. }
  412. }
  413. return $userValues;
  414. }
  415. /**
  416. * Determines the users that have the given value set for a specific app-key-pair
  417. *
  418. * @param string $appName the app to get the user for
  419. * @param string $key the key to get the user for
  420. * @param string $value the value to get the user for
  421. * @return array of user IDs
  422. */
  423. public function getUsersForUserValue($appName, $key, $value) {
  424. // TODO - FIXME
  425. $this->fixDIInit();
  426. $sql = 'SELECT `userid` FROM `*PREFIX*preferences` ' .
  427. 'WHERE `appid` = ? AND `configkey` = ? ';
  428. if($this->getSystemValue('dbtype', 'sqlite') === 'oci') {
  429. //oracle hack: need to explicitly cast CLOB to CHAR for comparison
  430. $sql .= 'AND to_char(`configvalue`) = ?';
  431. } else {
  432. $sql .= 'AND `configvalue` = ?';
  433. }
  434. $result = $this->connection->executeQuery($sql, array($appName, $key, $value));
  435. $userIDs = array();
  436. while ($row = $result->fetch()) {
  437. $userIDs[] = $row['userid'];
  438. }
  439. return $userIDs;
  440. }
  441. /**
  442. * Determines the users that have the given value set for a specific app-key-pair
  443. *
  444. * @param string $appName the app to get the user for
  445. * @param string $key the key to get the user for
  446. * @param string $value the value to get the user for
  447. * @return array of user IDs
  448. */
  449. public function getUsersForUserValueCaseInsensitive($appName, $key, $value) {
  450. // TODO - FIXME
  451. $this->fixDIInit();
  452. $sql = 'SELECT `userid` FROM `*PREFIX*preferences` ' .
  453. 'WHERE `appid` = ? AND `configkey` = ? ';
  454. if($this->getSystemValue('dbtype', 'sqlite') === 'oci') {
  455. //oracle hack: need to explicitly cast CLOB to CHAR for comparison
  456. $sql .= 'AND LOWER(to_char(`configvalue`)) = LOWER(?)';
  457. } else {
  458. $sql .= 'AND LOWER(`configvalue`) = LOWER(?)';
  459. }
  460. $result = $this->connection->executeQuery($sql, array($appName, $key, $value));
  461. $userIDs = array();
  462. while ($row = $result->fetch()) {
  463. $userIDs[] = $row['userid'];
  464. }
  465. return $userIDs;
  466. }
  467. public function getSystemConfig() {
  468. return $this->systemConfig;
  469. }
  470. }