AppConfig.php 8.7 KB

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