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.

appconfig.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Jakob Sack <mail@jakobsack.de>
  6. * @author Joas Schilling <nickvergessen@owncloud.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Lukas Reschke <lukas@owncloud.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <icewind@owncloud.com>
  11. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  12. * @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @copyright Copyright (c) 2015, ownCloud, Inc.
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OC;
  32. use OCP\IAppConfig;
  33. use OCP\IDBConnection;
  34. /**
  35. * This class provides an easy way for apps to store config values in the
  36. * database.
  37. */
  38. class AppConfig implements IAppConfig {
  39. /**
  40. * @var \OCP\IDBConnection $conn
  41. */
  42. protected $conn;
  43. private $cache = array();
  44. /**
  45. * @param IDBConnection $conn
  46. */
  47. public function __construct(IDBConnection $conn) {
  48. $this->conn = $conn;
  49. $this->configLoaded = false;
  50. }
  51. /**
  52. * @param string $app
  53. * @return array
  54. */
  55. private function getAppValues($app) {
  56. $this->loadConfigValues();
  57. if (isset($this->cache[$app])) {
  58. return $this->cache[$app];
  59. }
  60. return [];
  61. }
  62. /**
  63. * Get all apps using the config
  64. *
  65. * @return array an array of app ids
  66. *
  67. * This function returns a list of all apps that have at least one
  68. * entry in the appconfig table.
  69. */
  70. public function getApps() {
  71. $this->loadConfigValues();
  72. return $this->getSortedKeys($this->cache);
  73. }
  74. /**
  75. * Get the available keys for an app
  76. *
  77. * @param string $app the app we are looking for
  78. * @return array an array of key names
  79. *
  80. * This function gets all keys of an app. Please note that the values are
  81. * not returned.
  82. */
  83. public function getKeys($app) {
  84. $this->loadConfigValues();
  85. if (isset($this->cache[$app])) {
  86. return $this->getSortedKeys($this->cache[$app]);
  87. }
  88. return [];
  89. }
  90. public function getSortedKeys($data) {
  91. $keys = array_keys($data);
  92. sort($keys);
  93. return $keys;
  94. }
  95. /**
  96. * Gets the config value
  97. *
  98. * @param string $app app
  99. * @param string $key key
  100. * @param string $default = null, default value if the key does not exist
  101. * @return string the value or $default
  102. *
  103. * This function gets a value from the appconfig table. If the key does
  104. * not exist the default value will be returned
  105. */
  106. public function getValue($app, $key, $default = null) {
  107. $this->loadConfigValues();
  108. if ($this->hasKey($app, $key)) {
  109. return $this->cache[$app][$key];
  110. }
  111. return $default;
  112. }
  113. /**
  114. * check if a key is set in the appconfig
  115. *
  116. * @param string $app
  117. * @param string $key
  118. * @return bool
  119. */
  120. public function hasKey($app, $key) {
  121. $this->loadConfigValues();
  122. return isset($this->cache[$app][$key]);
  123. }
  124. /**
  125. * Sets a value. If the key did not exist before it will be created.
  126. *
  127. * @param string $app app
  128. * @param string $key key
  129. * @param string $value value
  130. * @return bool True if the value was inserted or updated, false if the value was the same
  131. */
  132. public function setValue($app, $key, $value) {
  133. if (!$this->hasKey($app, $key)) {
  134. $inserted = (bool) $this->conn->insertIfNotExist('*PREFIX*appconfig', [
  135. 'appid' => $app,
  136. 'configkey' => $key,
  137. 'configvalue' => $value,
  138. ], [
  139. 'appid',
  140. 'configkey',
  141. ]);
  142. if ($inserted) {
  143. if (!isset($this->cache[$app])) {
  144. $this->cache[$app] = [];
  145. }
  146. $this->cache[$app][$key] = $value;
  147. return true;
  148. }
  149. }
  150. $sql = $this->conn->getQueryBuilder();
  151. $sql->update('appconfig')
  152. ->set('configvalue', $sql->createParameter('configvalue'))
  153. ->where($sql->expr()->eq('appid', $sql->createParameter('app')))
  154. ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey')))
  155. ->andWhere($sql->expr()->neq('configvalue', $sql->createParameter('configvalue')))
  156. ->setParameter('configvalue', $value)
  157. ->setParameter('app', $app)
  158. ->setParameter('configkey', $key)
  159. ->setParameter('configvalue', $value);
  160. $changedRow = (bool) $sql->execute();
  161. $this->cache[$app][$key] = $value;
  162. return $changedRow;
  163. }
  164. /**
  165. * Deletes a key
  166. *
  167. * @param string $app app
  168. * @param string $key key
  169. * @return boolean|null
  170. */
  171. public function deleteKey($app, $key) {
  172. $this->loadConfigValues();
  173. $sql = $this->conn->getQueryBuilder();
  174. $sql->delete('appconfig')
  175. ->where($sql->expr()->eq('appid', $sql->createParameter('app')))
  176. ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey')))
  177. ->setParameter('app', $app)
  178. ->setParameter('configkey', $key);
  179. $sql->execute();
  180. unset($this->cache[$app][$key]);
  181. }
  182. /**
  183. * Remove app from appconfig
  184. *
  185. * @param string $app app
  186. * @return boolean|null
  187. *
  188. * Removes all keys in appconfig belonging to the app.
  189. */
  190. public function deleteApp($app) {
  191. $this->loadConfigValues();
  192. $sql = $this->conn->getQueryBuilder();
  193. $sql->delete('appconfig')
  194. ->where($sql->expr()->eq('appid', $sql->createParameter('app')))
  195. ->setParameter('app', $app);
  196. $sql->execute();
  197. unset($this->cache[$app]);
  198. }
  199. /**
  200. * get multiple values, either the app or key can be used as wildcard by setting it to false
  201. *
  202. * @param string|false $app
  203. * @param string|false $key
  204. * @return array|false
  205. */
  206. public function getValues($app, $key) {
  207. if (($app !== false) === ($key !== false)) {
  208. return false;
  209. }
  210. if ($key === false) {
  211. return $this->getAppValues($app);
  212. } else {
  213. $configs = [];
  214. foreach ($this->getApps() as $appId) {
  215. if ($this->hasKey($appId, $key)) {
  216. $configs[$appId] = $this->getValue($appId, $key);
  217. }
  218. }
  219. return $configs;
  220. }
  221. }
  222. /**
  223. * Load all the app config values
  224. */
  225. protected function loadConfigValues() {
  226. if ($this->configLoaded) return;
  227. $this->cache = [];
  228. $sql = $this->conn->getQueryBuilder();
  229. $sql->select('*')
  230. ->from('appconfig');
  231. $result = $sql->execute();
  232. while ($row = $result->fetch()) {
  233. if (!isset($this->cache[$row['appid']])) {
  234. $this->cache[$row['appid']] = [];
  235. }
  236. $this->cache[$row['appid']][$row['configkey']] = $row['configvalue'];
  237. }
  238. $result->closeCursor();
  239. $this->configLoaded = true;
  240. }
  241. }