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 7.0KB

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