allconfig.php 12 KB

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