AllConfig.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 system wide defined value and filters out sensitive data
  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. */
  125. public function getFilteredSystemValue($key, $default = '') {
  126. return $this->systemConfig->getFilteredValue($key, $default);
  127. }
  128. /**
  129. * Delete a system wide defined value
  130. *
  131. * @param string $key the key of the value, under which it was saved
  132. */
  133. public function deleteSystemValue($key) {
  134. $this->systemConfig->deleteValue($key);
  135. }
  136. /**
  137. * Get all keys stored for an app
  138. *
  139. * @param string $appName the appName that we stored the value under
  140. * @return string[] the keys stored for the app
  141. */
  142. public function getAppKeys($appName) {
  143. return \OC::$server->query(\OC\AppConfig::class)->getKeys($appName);
  144. }
  145. /**
  146. * Writes a new app wide value
  147. *
  148. * @param string $appName the appName that we want to store the value under
  149. * @param string $key the key of the value, under which will be saved
  150. * @param string|float|int $value the value that should be stored
  151. */
  152. public function setAppValue($appName, $key, $value) {
  153. \OC::$server->query(\OC\AppConfig::class)->setValue($appName, $key, $value);
  154. }
  155. /**
  156. * Looks up an app wide defined value
  157. *
  158. * @param string $appName the appName that we stored the value under
  159. * @param string $key the key of the value, under which it was saved
  160. * @param string $default the default value to be returned if the value isn't set
  161. * @return string the saved value
  162. */
  163. public function getAppValue($appName, $key, $default = '') {
  164. return \OC::$server->query(\OC\AppConfig::class)->getValue($appName, $key, $default);
  165. }
  166. /**
  167. * Delete an app wide defined value
  168. *
  169. * @param string $appName the appName that we stored the value under
  170. * @param string $key the key of the value, under which it was saved
  171. */
  172. public function deleteAppValue($appName, $key) {
  173. \OC::$server->query(\OC\AppConfig::class)->deleteKey($appName, $key);
  174. }
  175. /**
  176. * Removes all keys in appconfig belonging to the app
  177. *
  178. * @param string $appName the appName the configs are stored under
  179. */
  180. public function deleteAppValues($appName) {
  181. \OC::$server->query(\OC\AppConfig::class)->deleteApp($appName);
  182. }
  183. /**
  184. * Set a user defined value
  185. *
  186. * @param string $userId the userId of the user that we want to store the value under
  187. * @param string $appName the appName that we want to store the value under
  188. * @param string $key the key under which the value is being stored
  189. * @param string|float|int $value the value that you want to store
  190. * @param string $preCondition only update if the config value was previously the value passed as $preCondition
  191. * @throws \OCP\PreConditionNotMetException if a precondition is specified and is not met
  192. * @throws \UnexpectedValueException when trying to store an unexpected value
  193. */
  194. public function setUserValue($userId, $appName, $key, $value, $preCondition = null) {
  195. if (!is_int($value) && !is_float($value) && !is_string($value)) {
  196. throw new \UnexpectedValueException('Only integers, floats and strings are allowed as value');
  197. }
  198. // TODO - FIXME
  199. $this->fixDIInit();
  200. $prevValue = $this->getUserValue($userId, $appName, $key, null);
  201. if ($prevValue !== null) {
  202. if ($prevValue === (string)$value) {
  203. return;
  204. } else if ($preCondition !== null && $prevValue !== (string)$preCondition) {
  205. throw new PreConditionNotMetException();
  206. } else {
  207. $qb = $this->connection->getQueryBuilder();
  208. $qb->update('preferences')
  209. ->set('configvalue', $qb->createNamedParameter($value))
  210. ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId)))
  211. ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter($appName)))
  212. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key)));
  213. $qb->execute();
  214. $this->userCache[$userId][$appName][$key] = (string)$value;
  215. return;
  216. }
  217. }
  218. $preconditionArray = [];
  219. if (isset($preCondition)) {
  220. $preconditionArray = [
  221. 'configvalue' => $preCondition,
  222. ];
  223. }
  224. $this->connection->setValues('preferences', [
  225. 'userid' => $userId,
  226. 'appid' => $appName,
  227. 'configkey' => $key,
  228. ], [
  229. 'configvalue' => $value,
  230. ], $preconditionArray);
  231. // only add to the cache if we already loaded data for the user
  232. if (isset($this->userCache[$userId])) {
  233. if (!isset($this->userCache[$userId][$appName])) {
  234. $this->userCache[$userId][$appName] = array();
  235. }
  236. $this->userCache[$userId][$appName][$key] = (string)$value;
  237. }
  238. }
  239. /**
  240. * Getting a user defined value
  241. *
  242. * @param string $userId the userId of the user that we want to store the value under
  243. * @param string $appName the appName that we stored the value under
  244. * @param string $key the key under which the value is being stored
  245. * @param mixed $default the default value to be returned if the value isn't set
  246. * @return string
  247. */
  248. public function getUserValue($userId, $appName, $key, $default = '') {
  249. $data = $this->getUserValues($userId);
  250. if (isset($data[$appName]) and isset($data[$appName][$key])) {
  251. return $data[$appName][$key];
  252. } else {
  253. return $default;
  254. }
  255. }
  256. /**
  257. * Get the keys of all stored by an app for the user
  258. *
  259. * @param string $userId the userId of the user that we want to store the value under
  260. * @param string $appName the appName that we stored the value under
  261. * @return string[]
  262. */
  263. public function getUserKeys($userId, $appName) {
  264. $data = $this->getUserValues($userId);
  265. if (isset($data[$appName])) {
  266. return array_keys($data[$appName]);
  267. } else {
  268. return array();
  269. }
  270. }
  271. /**
  272. * Delete a user value
  273. *
  274. * @param string $userId the userId of the user that we want to store the value under
  275. * @param string $appName the appName that we stored the value under
  276. * @param string $key the key under which the value is being stored
  277. */
  278. public function deleteUserValue($userId, $appName, $key) {
  279. // TODO - FIXME
  280. $this->fixDIInit();
  281. $sql = 'DELETE FROM `*PREFIX*preferences` '.
  282. 'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?';
  283. $this->connection->executeUpdate($sql, array($userId, $appName, $key));
  284. if (isset($this->userCache[$userId]) and isset($this->userCache[$userId][$appName])) {
  285. unset($this->userCache[$userId][$appName][$key]);
  286. }
  287. }
  288. /**
  289. * Delete all user values
  290. *
  291. * @param string $userId the userId of the user that we want to remove all values from
  292. */
  293. public function deleteAllUserValues($userId) {
  294. // TODO - FIXME
  295. $this->fixDIInit();
  296. $sql = 'DELETE FROM `*PREFIX*preferences` '.
  297. 'WHERE `userid` = ?';
  298. $this->connection->executeUpdate($sql, array($userId));
  299. unset($this->userCache[$userId]);
  300. }
  301. /**
  302. * Delete all user related values of one app
  303. *
  304. * @param string $appName the appName of the app that we want to remove all values from
  305. */
  306. public function deleteAppFromAllUsers($appName) {
  307. // TODO - FIXME
  308. $this->fixDIInit();
  309. $sql = 'DELETE FROM `*PREFIX*preferences` '.
  310. 'WHERE `appid` = ?';
  311. $this->connection->executeUpdate($sql, array($appName));
  312. foreach ($this->userCache as &$userCache) {
  313. unset($userCache[$appName]);
  314. }
  315. }
  316. /**
  317. * Returns all user configs sorted by app of one user
  318. *
  319. * @param string $userId the user ID to get the app configs from
  320. * @return array[] - 2 dimensional array with the following structure:
  321. * [ $appId =>
  322. * [ $key => $value ]
  323. * ]
  324. */
  325. private function getUserValues($userId) {
  326. if (isset($this->userCache[$userId])) {
  327. return $this->userCache[$userId];
  328. }
  329. if ($userId === null || $userId === '') {
  330. $this->userCache[$userId]=array();
  331. return $this->userCache[$userId];
  332. }
  333. // TODO - FIXME
  334. $this->fixDIInit();
  335. $data = array();
  336. $query = 'SELECT `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?';
  337. $result = $this->connection->executeQuery($query, array($userId));
  338. while ($row = $result->fetch()) {
  339. $appId = $row['appid'];
  340. if (!isset($data[$appId])) {
  341. $data[$appId] = array();
  342. }
  343. $data[$appId][$row['configkey']] = $row['configvalue'];
  344. }
  345. $this->userCache[$userId] = $data;
  346. return $data;
  347. }
  348. /**
  349. * Fetches a mapped list of userId -> value, for a specified app and key and a list of user IDs.
  350. *
  351. * @param string $appName app to get the value for
  352. * @param string $key the key to get the value for
  353. * @param array $userIds the user IDs to fetch the values for
  354. * @return array Mapped values: userId => value
  355. */
  356. public function getUserValueForUsers($appName, $key, $userIds) {
  357. // TODO - FIXME
  358. $this->fixDIInit();
  359. if (empty($userIds) || !is_array($userIds)) {
  360. return array();
  361. }
  362. $chunkedUsers = array_chunk($userIds, 50, true);
  363. $placeholders50 = implode(',', array_fill(0, 50, '?'));
  364. $userValues = array();
  365. foreach ($chunkedUsers as $chunk) {
  366. $queryParams = $chunk;
  367. // create [$app, $key, $chunkedUsers]
  368. array_unshift($queryParams, $key);
  369. array_unshift($queryParams, $appName);
  370. $placeholders = (count($chunk) === 50) ? $placeholders50 : implode(',', array_fill(0, count($chunk), '?'));
  371. $query = 'SELECT `userid`, `configvalue` ' .
  372. 'FROM `*PREFIX*preferences` ' .
  373. 'WHERE `appid` = ? AND `configkey` = ? ' .
  374. 'AND `userid` IN (' . $placeholders . ')';
  375. $result = $this->connection->executeQuery($query, $queryParams);
  376. while ($row = $result->fetch()) {
  377. $userValues[$row['userid']] = $row['configvalue'];
  378. }
  379. }
  380. return $userValues;
  381. }
  382. /**
  383. * Determines the users that have the given value set for a specific app-key-pair
  384. *
  385. * @param string $appName the app to get the user for
  386. * @param string $key the key to get the user for
  387. * @param string $value the value to get the user for
  388. * @return array of user IDs
  389. */
  390. public function getUsersForUserValue($appName, $key, $value) {
  391. // TODO - FIXME
  392. $this->fixDIInit();
  393. $sql = 'SELECT `userid` FROM `*PREFIX*preferences` ' .
  394. 'WHERE `appid` = ? AND `configkey` = ? ';
  395. if($this->getSystemValue('dbtype', 'sqlite') === 'oci') {
  396. //oracle hack: need to explicitly cast CLOB to CHAR for comparison
  397. $sql .= 'AND to_char(`configvalue`) = ?';
  398. } else {
  399. $sql .= 'AND `configvalue` = ?';
  400. }
  401. $result = $this->connection->executeQuery($sql, array($appName, $key, $value));
  402. $userIDs = array();
  403. while ($row = $result->fetch()) {
  404. $userIDs[] = $row['userid'];
  405. }
  406. return $userIDs;
  407. }
  408. /**
  409. * Determines the users that have the given value set for a specific app-key-pair
  410. *
  411. * @param string $appName the app to get the user for
  412. * @param string $key the key to get the user for
  413. * @param string $value the value to get the user for
  414. * @return array of user IDs
  415. */
  416. public function getUsersForUserValueCaseInsensitive($appName, $key, $value) {
  417. // TODO - FIXME
  418. $this->fixDIInit();
  419. $sql = 'SELECT `userid` FROM `*PREFIX*preferences` ' .
  420. 'WHERE `appid` = ? AND `configkey` = ? ';
  421. if($this->getSystemValue('dbtype', 'sqlite') === 'oci') {
  422. //oracle hack: need to explicitly cast CLOB to CHAR for comparison
  423. $sql .= 'AND LOWER(to_char(`configvalue`)) = LOWER(?)';
  424. } else {
  425. $sql .= 'AND LOWER(`configvalue`) = LOWER(?)';
  426. }
  427. $result = $this->connection->executeQuery($sql, array($appName, $key, $value));
  428. $userIDs = array();
  429. while ($row = $result->fetch()) {
  430. $userIDs[] = $row['userid'];
  431. }
  432. return $userIDs;
  433. }
  434. public function getSystemConfig() {
  435. return $this->systemConfig;
  436. }
  437. }