allconfig.php 13 KB

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