1
0

allconfig.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. 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 $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 $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. */
  191. public function setUserValue($userId, $appName, $key, $value, $preCondition = null) {
  192. // TODO - FIXME
  193. $this->fixDIInit();
  194. $preconditionArray = [];
  195. if (isset($preCondition)) {
  196. $preconditionArray = [
  197. 'configvalue' => $preCondition,
  198. ];
  199. }
  200. $this->connection->setValues('preferences', [
  201. 'userid' => $userId,
  202. 'appid' => $appName,
  203. 'configkey' => $key,
  204. ], [
  205. 'configvalue' => $value,
  206. ], $preconditionArray);
  207. // only add to the cache if we already loaded data for the user
  208. if (isset($this->userCache[$userId])) {
  209. if (!isset($this->userCache[$userId][$appName])) {
  210. $this->userCache[$userId][$appName] = array();
  211. }
  212. $this->userCache[$userId][$appName][$key] = $value;
  213. }
  214. }
  215. /**
  216. * Getting a user defined value
  217. *
  218. * @param string $userId the userId of the user that we want to store the value under
  219. * @param string $appName the appName that we stored the value under
  220. * @param string $key the key under which the value is being stored
  221. * @param mixed $default the default value to be returned if the value isn't set
  222. * @return string
  223. */
  224. public function getUserValue($userId, $appName, $key, $default = '') {
  225. $data = $this->getUserValues($userId);
  226. if (isset($data[$appName]) and isset($data[$appName][$key])) {
  227. return $data[$appName][$key];
  228. } else {
  229. return $default;
  230. }
  231. }
  232. /**
  233. * Get the keys of all stored by an app for the user
  234. *
  235. * @param string $userId the userId of the user that we want to store the value under
  236. * @param string $appName the appName that we stored the value under
  237. * @return string[]
  238. */
  239. public function getUserKeys($userId, $appName) {
  240. $data = $this->getUserValues($userId);
  241. if (isset($data[$appName])) {
  242. return array_keys($data[$appName]);
  243. } else {
  244. return array();
  245. }
  246. }
  247. /**
  248. * Delete a user value
  249. *
  250. * @param string $userId the userId of the user that we want to store the value under
  251. * @param string $appName the appName that we stored the value under
  252. * @param string $key the key under which the value is being stored
  253. */
  254. public function deleteUserValue($userId, $appName, $key) {
  255. // TODO - FIXME
  256. $this->fixDIInit();
  257. $sql = 'DELETE FROM `*PREFIX*preferences` '.
  258. 'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?';
  259. $this->connection->executeUpdate($sql, array($userId, $appName, $key));
  260. if (isset($this->userCache[$userId]) and isset($this->userCache[$userId][$appName])) {
  261. unset($this->userCache[$userId][$appName][$key]);
  262. }
  263. }
  264. /**
  265. * Delete all user values
  266. *
  267. * @param string $userId the userId of the user that we want to remove all values from
  268. */
  269. public function deleteAllUserValues($userId) {
  270. // TODO - FIXME
  271. $this->fixDIInit();
  272. $sql = 'DELETE FROM `*PREFIX*preferences` '.
  273. 'WHERE `userid` = ?';
  274. $this->connection->executeUpdate($sql, array($userId));
  275. unset($this->userCache[$userId]);
  276. }
  277. /**
  278. * Delete all user related values of one app
  279. *
  280. * @param string $appName the appName of the app that we want to remove all values from
  281. */
  282. public function deleteAppFromAllUsers($appName) {
  283. // TODO - FIXME
  284. $this->fixDIInit();
  285. $sql = 'DELETE FROM `*PREFIX*preferences` '.
  286. 'WHERE `appid` = ?';
  287. $this->connection->executeUpdate($sql, array($appName));
  288. foreach ($this->userCache as &$userCache) {
  289. unset($userCache[$appName]);
  290. }
  291. }
  292. /**
  293. * Returns all user configs sorted by app of one user
  294. *
  295. * @param string $userId the user ID to get the app configs from
  296. * @return array[] - 2 dimensional array with the following structure:
  297. * [ $appId =>
  298. * [ $key => $value ]
  299. * ]
  300. */
  301. private function getUserValues($userId) {
  302. // TODO - FIXME
  303. $this->fixDIInit();
  304. if (isset($this->userCache[$userId])) {
  305. return $this->userCache[$userId];
  306. }
  307. $data = array();
  308. $query = 'SELECT `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?';
  309. $result = $this->connection->executeQuery($query, array($userId));
  310. while ($row = $result->fetch()) {
  311. $appId = $row['appid'];
  312. if (!isset($data[$appId])) {
  313. $data[$appId] = array();
  314. }
  315. $data[$appId][$row['configkey']] = $row['configvalue'];
  316. }
  317. $this->userCache[$userId] = $data;
  318. return $data;
  319. }
  320. /**
  321. * Fetches a mapped list of userId -> value, for a specified app and key and a list of user IDs.
  322. *
  323. * @param string $appName app to get the value for
  324. * @param string $key the key to get the value for
  325. * @param array $userIds the user IDs to fetch the values for
  326. * @return array Mapped values: userId => value
  327. */
  328. public function getUserValueForUsers($appName, $key, $userIds) {
  329. // TODO - FIXME
  330. $this->fixDIInit();
  331. if (empty($userIds) || !is_array($userIds)) {
  332. return array();
  333. }
  334. $chunkedUsers = array_chunk($userIds, 50, true);
  335. $placeholders50 = implode(',', array_fill(0, 50, '?'));
  336. $userValues = array();
  337. foreach ($chunkedUsers as $chunk) {
  338. $queryParams = $chunk;
  339. // create [$app, $key, $chunkedUsers]
  340. array_unshift($queryParams, $key);
  341. array_unshift($queryParams, $appName);
  342. $placeholders = (sizeof($chunk) == 50) ? $placeholders50 : implode(',', array_fill(0, sizeof($chunk), '?'));
  343. $query = 'SELECT `userid`, `configvalue` ' .
  344. 'FROM `*PREFIX*preferences` ' .
  345. 'WHERE `appid` = ? AND `configkey` = ? ' .
  346. 'AND `userid` IN (' . $placeholders . ')';
  347. $result = $this->connection->executeQuery($query, $queryParams);
  348. while ($row = $result->fetch()) {
  349. $userValues[$row['userid']] = $row['configvalue'];
  350. }
  351. }
  352. return $userValues;
  353. }
  354. /**
  355. * Determines the users that have the given value set for a specific app-key-pair
  356. *
  357. * @param string $appName the app to get the user for
  358. * @param string $key the key to get the user for
  359. * @param string $value the value to get the user for
  360. * @return array of user IDs
  361. */
  362. public function getUsersForUserValue($appName, $key, $value) {
  363. // TODO - FIXME
  364. $this->fixDIInit();
  365. $sql = 'SELECT `userid` FROM `*PREFIX*preferences` ' .
  366. 'WHERE `appid` = ? AND `configkey` = ? ';
  367. if($this->getSystemValue('dbtype', 'sqlite') === 'oci') {
  368. //oracle hack: need to explicitly cast CLOB to CHAR for comparison
  369. $sql .= 'AND to_char(`configvalue`) = ?';
  370. } else {
  371. $sql .= 'AND `configvalue` = ?';
  372. }
  373. $result = $this->connection->executeQuery($sql, array($appName, $key, $value));
  374. $userIDs = array();
  375. while ($row = $result->fetch()) {
  376. $userIDs[] = $row['userid'];
  377. }
  378. return $userIDs;
  379. }
  380. }