AppConfig.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. * @deprecated 8.0.0 use method getAppKeys of \OCP\IConfig
  91. *
  92. * This function gets all keys of an app. Please note that the values are
  93. * not returned.
  94. */
  95. public function getKeys($app) {
  96. $this->loadConfigValues();
  97. if (isset($this->cache[$app])) {
  98. return $this->getSortedKeys($this->cache[$app]);
  99. }
  100. return [];
  101. }
  102. public function getSortedKeys($data) {
  103. $keys = array_keys($data);
  104. sort($keys);
  105. return $keys;
  106. }
  107. /**
  108. * Gets the config value
  109. *
  110. * @param string $app app
  111. * @param string $key key
  112. * @param string $default = null, default value if the key does not exist
  113. * @return string the value or $default
  114. * @deprecated 8.0.0 use method getAppValue of \OCP\IConfig
  115. *
  116. * This function gets a value from the appconfig table. If the key does
  117. * not exist the default value will be returned
  118. */
  119. public function getValue($app, $key, $default = null) {
  120. $this->loadConfigValues();
  121. if ($this->hasKey($app, $key)) {
  122. return $this->cache[$app][$key];
  123. }
  124. return $default;
  125. }
  126. /**
  127. * check if a key is set in the appconfig
  128. *
  129. * @param string $app
  130. * @param string $key
  131. * @return bool
  132. */
  133. public function hasKey($app, $key) {
  134. $this->loadConfigValues();
  135. return isset($this->cache[$app][$key]);
  136. }
  137. /**
  138. * Sets a value. If the key did not exist before it will be created.
  139. *
  140. * @param string $app app
  141. * @param string $key key
  142. * @param string|float|int $value value
  143. * @return bool True if the value was inserted or updated, false if the value was the same
  144. * @deprecated 8.0.0 use method setAppValue of \OCP\IConfig
  145. */
  146. public function setValue($app, $key, $value) {
  147. if (!$this->hasKey($app, $key)) {
  148. $inserted = (bool) $this->conn->insertIfNotExist('*PREFIX*appconfig', [
  149. 'appid' => $app,
  150. 'configkey' => $key,
  151. 'configvalue' => $value,
  152. ], [
  153. 'appid',
  154. 'configkey',
  155. ]);
  156. if ($inserted) {
  157. if (!isset($this->cache[$app])) {
  158. $this->cache[$app] = [];
  159. }
  160. $this->cache[$app][$key] = $value;
  161. return true;
  162. }
  163. }
  164. $sql = $this->conn->getQueryBuilder();
  165. $sql->update('appconfig')
  166. ->set('configvalue', $sql->createParameter('configvalue'))
  167. ->where($sql->expr()->eq('appid', $sql->createParameter('app')))
  168. ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey')))
  169. ->setParameter('configvalue', $value)
  170. ->setParameter('app', $app)
  171. ->setParameter('configkey', $key);
  172. /*
  173. * Only limit to the existing value for non-Oracle DBs:
  174. * http://docs.oracle.com/cd/E11882_01/server.112/e26088/conditions002.htm#i1033286
  175. * > Large objects (LOBs) are not supported in comparison conditions.
  176. */
  177. if (!($this->conn instanceof OracleConnection)) {
  178. // Only update the value when it is not the same
  179. $sql->andWhere($sql->expr()->neq('configvalue', $sql->createParameter('configvalue')))
  180. ->setParameter('configvalue', $value);
  181. }
  182. $changedRow = (bool) $sql->execute();
  183. $this->cache[$app][$key] = $value;
  184. return $changedRow;
  185. }
  186. /**
  187. * Deletes a key
  188. *
  189. * @param string $app app
  190. * @param string $key key
  191. * @return boolean
  192. * @deprecated 8.0.0 use method deleteAppValue of \OCP\IConfig
  193. */
  194. public function deleteKey($app, $key) {
  195. $this->loadConfigValues();
  196. $sql = $this->conn->getQueryBuilder();
  197. $sql->delete('appconfig')
  198. ->where($sql->expr()->eq('appid', $sql->createParameter('app')))
  199. ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey')))
  200. ->setParameter('app', $app)
  201. ->setParameter('configkey', $key);
  202. $sql->execute();
  203. unset($this->cache[$app][$key]);
  204. return false;
  205. }
  206. /**
  207. * Remove app from appconfig
  208. *
  209. * @param string $app app
  210. * @return boolean
  211. * @deprecated 8.0.0 use method deleteAppValue of \OCP\IConfig
  212. *
  213. * Removes all keys in appconfig belonging to the app.
  214. */
  215. public function deleteApp($app) {
  216. $this->loadConfigValues();
  217. $sql = $this->conn->getQueryBuilder();
  218. $sql->delete('appconfig')
  219. ->where($sql->expr()->eq('appid', $sql->createParameter('app')))
  220. ->setParameter('app', $app);
  221. $sql->execute();
  222. unset($this->cache[$app]);
  223. return false;
  224. }
  225. /**
  226. * get multiple values, either the app or key can be used as wildcard by setting it to false
  227. *
  228. * @param string|false $app
  229. * @param string|false $key
  230. * @return array|false
  231. */
  232. public function getValues($app, $key) {
  233. if (($app !== false) === ($key !== false)) {
  234. return false;
  235. }
  236. if ($key === false) {
  237. return $this->getAppValues($app);
  238. } else {
  239. $appIds = $this->getApps();
  240. $values = array_map(function($appId) use ($key) {
  241. return isset($this->cache[$appId][$key]) ? $this->cache[$appId][$key] : null;
  242. }, $appIds);
  243. $result = array_combine($appIds, $values);
  244. return array_filter($result);
  245. }
  246. }
  247. /**
  248. * get all values of the app or and filters out sensitive data
  249. *
  250. * @param string $app
  251. * @return array
  252. */
  253. public function getFilteredValues($app) {
  254. $values = $this->getValues($app, false);
  255. foreach ($this->sensitiveValues[$app] as $sensitiveKey) {
  256. if (isset($values[$sensitiveKey])) {
  257. $values[$sensitiveKey] = IConfig::SENSITIVE_VALUE;
  258. }
  259. }
  260. return $values;
  261. }
  262. /**
  263. * Load all the app config values
  264. */
  265. protected function loadConfigValues() {
  266. if ($this->configLoaded) {
  267. return;
  268. }
  269. $this->cache = [];
  270. $sql = $this->conn->getQueryBuilder();
  271. $sql->select('*')
  272. ->from('appconfig');
  273. $result = $sql->execute();
  274. // we are going to store the result in memory anyway
  275. $rows = $result->fetchAll();
  276. foreach ($rows as $row) {
  277. if (!isset($this->cache[$row['appid']])) {
  278. $this->cache[$row['appid']] = [];
  279. }
  280. $this->cache[$row['appid']][$row['configkey']] = $row['configvalue'];
  281. }
  282. $result->closeCursor();
  283. $this->configLoaded = true;
  284. }
  285. }