Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

allconfig.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 a new system wide value
  67. *
  68. * @param string $key the key of the value, under which will be saved
  69. * @param mixed $value the value that should be stored
  70. */
  71. public function setSystemValue($key, $value) {
  72. $this->systemConfig->setValue($key, $value);
  73. }
  74. /**
  75. * Looks up a system wide defined value
  76. *
  77. * @param string $key the key of the value, under which it was saved
  78. * @param mixed $default the default value to be returned if the value isn't set
  79. * @return mixed the value or $default
  80. */
  81. public function getSystemValue($key, $default = '') {
  82. return $this->systemConfig->getValue($key, $default);
  83. }
  84. /**
  85. * Delete a system wide defined value
  86. *
  87. * @param string $key the key of the value, under which it was saved
  88. */
  89. public function deleteSystemValue($key) {
  90. $this->systemConfig->deleteValue($key);
  91. }
  92. /**
  93. * Get all keys stored for an app
  94. *
  95. * @param string $appName the appName that we stored the value under
  96. * @return string[] the keys stored for the app
  97. */
  98. public function getAppKeys($appName) {
  99. return \OC::$server->getAppConfig()->getKeys($appName);
  100. }
  101. /**
  102. * Writes a new app wide value
  103. *
  104. * @param string $appName the appName that we want to store the value under
  105. * @param string $key the key of the value, under which will be saved
  106. * @param string $value the value that should be stored
  107. */
  108. public function setAppValue($appName, $key, $value) {
  109. \OC::$server->getAppConfig()->setValue($appName, $key, $value);
  110. }
  111. /**
  112. * Looks up an app wide defined value
  113. *
  114. * @param string $appName the appName that we stored the value under
  115. * @param string $key the key of the value, under which it was saved
  116. * @param string $default the default value to be returned if the value isn't set
  117. * @return string the saved value
  118. */
  119. public function getAppValue($appName, $key, $default = '') {
  120. return \OC::$server->getAppConfig()->getValue($appName, $key, $default);
  121. }
  122. /**
  123. * Delete an app wide defined value
  124. *
  125. * @param string $appName the appName that we stored the value under
  126. * @param string $key the key of the value, under which it was saved
  127. */
  128. public function deleteAppValue($appName, $key) {
  129. \OC::$server->getAppConfig()->deleteKey($appName, $key);
  130. }
  131. /**
  132. * Removes all keys in appconfig belonging to the app
  133. *
  134. * @param string $appName the appName the configs are stored under
  135. */
  136. public function deleteAppValues($appName) {
  137. \OC::$server->getAppConfig()->deleteApp($appName);
  138. }
  139. /**
  140. * Set a user defined value
  141. *
  142. * @param string $userId the userId of the user that we want to store the value under
  143. * @param string $appName the appName that we want to store the value under
  144. * @param string $key the key under which the value is being stored
  145. * @param string $value the value that you want to store
  146. * @param string $preCondition only update if the config value was previously the value passed as $preCondition
  147. * @throws \OCP\PreConditionNotMetException if a precondition is specified and is not met
  148. */
  149. public function setUserValue($userId, $appName, $key, $value, $preCondition = null) {
  150. // TODO - FIXME
  151. $this->fixDIInit();
  152. // Check if the key does exist
  153. $sql = 'SELECT `configvalue` FROM `*PREFIX*preferences` '.
  154. 'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?';
  155. $result = $this->connection->executeQuery($sql, array($userId, $appName, $key));
  156. $oldValue = $result->fetchColumn();
  157. $exists = $oldValue !== false;
  158. if($oldValue === strval($value)) {
  159. // no changes
  160. return;
  161. }
  162. $data = array($value, $userId, $appName, $key);
  163. if (!$exists && $preCondition === null) {
  164. $sql = 'INSERT INTO `*PREFIX*preferences` (`configvalue`, `userid`, `appid`, `configkey`)'.
  165. 'VALUES (?, ?, ?, ?)';
  166. } elseif ($exists) {
  167. $sql = 'UPDATE `*PREFIX*preferences` SET `configvalue` = ? '.
  168. 'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ? ';
  169. if($preCondition !== null) {
  170. if($this->getSystemValue('dbtype', 'sqlite') === 'oci') {
  171. //oracle hack: need to explicitly cast CLOB to CHAR for comparison
  172. $sql .= 'AND to_char(`configvalue`) = ?';
  173. } else {
  174. $sql .= 'AND `configvalue` = ?';
  175. }
  176. $data[] = $preCondition;
  177. }
  178. }
  179. $affectedRows = $this->connection->executeUpdate($sql, $data);
  180. // only add to the cache if we already loaded data for the user
  181. if ($affectedRows > 0 && isset($this->userCache[$userId])) {
  182. if (!isset($this->userCache[$userId][$appName])) {
  183. $this->userCache[$userId][$appName] = array();
  184. }
  185. $this->userCache[$userId][$appName][$key] = $value;
  186. }
  187. if ($preCondition !== null && $affectedRows === 0) {
  188. throw new PreConditionNotMetException;
  189. }
  190. }
  191. /**
  192. * Getting a user defined value
  193. *
  194. * @param string $userId the userId of the user that we want to store the value under
  195. * @param string $appName the appName that we stored the value under
  196. * @param string $key the key under which the value is being stored
  197. * @param string $default the default value to be returned if the value isn't set
  198. * @return string
  199. */
  200. public function getUserValue($userId, $appName, $key, $default = '') {
  201. $data = $this->getUserValues($userId);
  202. if (isset($data[$appName]) and isset($data[$appName][$key])) {
  203. return $data[$appName][$key];
  204. } else {
  205. return $default;
  206. }
  207. }
  208. /**
  209. * Get the keys of all stored by an app for the user
  210. *
  211. * @param string $userId the userId of the user that we want to store the value under
  212. * @param string $appName the appName that we stored the value under
  213. * @return string[]
  214. */
  215. public function getUserKeys($userId, $appName) {
  216. $data = $this->getUserValues($userId);
  217. if (isset($data[$appName])) {
  218. return array_keys($data[$appName]);
  219. } else {
  220. return array();
  221. }
  222. }
  223. /**
  224. * Delete a user value
  225. *
  226. * @param string $userId the userId of the user that we want to store the value under
  227. * @param string $appName the appName that we stored the value under
  228. * @param string $key the key under which the value is being stored
  229. */
  230. public function deleteUserValue($userId, $appName, $key) {
  231. // TODO - FIXME
  232. $this->fixDIInit();
  233. $sql = 'DELETE FROM `*PREFIX*preferences` '.
  234. 'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?';
  235. $this->connection->executeUpdate($sql, array($userId, $appName, $key));
  236. if (isset($this->userCache[$userId]) and isset($this->userCache[$userId][$appName])) {
  237. unset($this->userCache[$userId][$appName][$key]);
  238. }
  239. }
  240. /**
  241. * Delete all user values
  242. *
  243. * @param string $userId the userId of the user that we want to remove all values from
  244. */
  245. public function deleteAllUserValues($userId) {
  246. // TODO - FIXME
  247. $this->fixDIInit();
  248. $sql = 'DELETE FROM `*PREFIX*preferences` '.
  249. 'WHERE `userid` = ?';
  250. $this->connection->executeUpdate($sql, array($userId));
  251. unset($this->userCache[$userId]);
  252. }
  253. /**
  254. * Delete all user related values of one app
  255. *
  256. * @param string $appName the appName of the app that we want to remove all values from
  257. */
  258. public function deleteAppFromAllUsers($appName) {
  259. // TODO - FIXME
  260. $this->fixDIInit();
  261. $sql = 'DELETE FROM `*PREFIX*preferences` '.
  262. 'WHERE `appid` = ?';
  263. $this->connection->executeUpdate($sql, array($appName));
  264. foreach ($this->userCache as &$userCache) {
  265. unset($userCache[$appName]);
  266. }
  267. }
  268. /**
  269. * Returns all user configs sorted by app of one user
  270. *
  271. * @param string $userId the user ID to get the app configs from
  272. * @return array[] - 2 dimensional array with the following structure:
  273. * [ $appId =>
  274. * [ $key => $value ]
  275. * ]
  276. */
  277. private function getUserValues($userId) {
  278. // TODO - FIXME
  279. $this->fixDIInit();
  280. if (isset($this->userCache[$userId])) {
  281. return $this->userCache[$userId];
  282. }
  283. $data = array();
  284. $query = 'SELECT `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?';
  285. $result = $this->connection->executeQuery($query, array($userId));
  286. while ($row = $result->fetch()) {
  287. $appId = $row['appid'];
  288. if (!isset($data[$appId])) {
  289. $data[$appId] = array();
  290. }
  291. $data[$appId][$row['configkey']] = $row['configvalue'];
  292. }
  293. $this->userCache[$userId] = $data;
  294. return $data;
  295. }
  296. /**
  297. * Fetches a mapped list of userId -> value, for a specified app and key and a list of user IDs.
  298. *
  299. * @param $appName app to get the value for
  300. * @param $key the key to get the value for
  301. * @param $userIds the user IDs to fetch the values for
  302. * @return array Mapped values: userId => value
  303. */
  304. public function getUserValueForUsers($appName, $key, $userIds) {
  305. // TODO - FIXME
  306. $this->fixDIInit();
  307. if (empty($userIds) || !is_array($userIds)) {
  308. return array();
  309. }
  310. $chunkedUsers = array_chunk($userIds, 50, true);
  311. $placeholders50 = implode(',', array_fill(0, 50, '?'));
  312. $userValues = array();
  313. foreach ($chunkedUsers as $chunk) {
  314. $queryParams = $chunk;
  315. // create [$app, $key, $chunkedUsers]
  316. array_unshift($queryParams, $key);
  317. array_unshift($queryParams, $appName);
  318. $placeholders = (sizeof($chunk) == 50) ? $placeholders50 : implode(',', array_fill(0, sizeof($chunk), '?'));
  319. $query = 'SELECT `userid`, `configvalue` ' .
  320. 'FROM `*PREFIX*preferences` ' .
  321. 'WHERE `appid` = ? AND `configkey` = ? ' .
  322. 'AND `userid` IN (' . $placeholders . ')';
  323. $result = $this->connection->executeQuery($query, $queryParams);
  324. while ($row = $result->fetch()) {
  325. $userValues[$row['userid']] = $row['configvalue'];
  326. }
  327. }
  328. return $userValues;
  329. }
  330. /**
  331. * Determines the users that have the given value set for a specific app-key-pair
  332. *
  333. * @param string $appName the app to get the user for
  334. * @param string $key the key to get the user for
  335. * @param string $value the value to get the user for
  336. * @return array of user IDs
  337. */
  338. public function getUsersForUserValue($appName, $key, $value) {
  339. // TODO - FIXME
  340. $this->fixDIInit();
  341. $sql = 'SELECT `userid` FROM `*PREFIX*preferences` ' .
  342. 'WHERE `appid` = ? AND `configkey` = ? ';
  343. if($this->getSystemValue('dbtype', 'sqlite') === 'oci') {
  344. //oracle hack: need to explicitly cast CLOB to CHAR for comparison
  345. $sql .= 'AND to_char(`configvalue`) = ?';
  346. } else {
  347. $sql .= 'AND `configvalue` = ?';
  348. }
  349. $result = $this->connection->executeQuery($sql, array($appName, $key, $value));
  350. $userIDs = array();
  351. while ($row = $result->fetch()) {
  352. $userIDs[] = $row['userid'];
  353. }
  354. return $userIDs;
  355. }
  356. }