1
0

AppConfig.php 7.8 KB

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