You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AllConfig.php 13KB

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