1
0

AllConfig.php 14 KB

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