AppConfig.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. * @author Roeland Jago Douma <roeland@famdouma.nl>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OC;
  34. use OC\DB\Connection;
  35. use OC\DB\OracleConnection;
  36. use OCP\IAppConfig;
  37. use OCP\IConfig;
  38. /**
  39. * This class provides an easy way for apps to store config values in the
  40. * database.
  41. */
  42. class AppConfig implements IAppConfig {
  43. /** @var array[] */
  44. protected $sensitiveValues = [
  45. 'circles' => [
  46. '/^key_pairs$/',
  47. '/^local_gskey$/',
  48. ],
  49. 'external' => [
  50. '/^sites$/',
  51. ],
  52. 'integration_discourse' => [
  53. '/^private_key$/',
  54. '/^public_key$/',
  55. ],
  56. 'integration_dropbox' => [
  57. '/^client_id$/',
  58. '/^client_secret$/',
  59. ],
  60. 'integration_github' => [
  61. '/^client_id$/',
  62. '/^client_secret$/',
  63. ],
  64. 'integration_gitlab' => [
  65. '/^client_id$/',
  66. '/^client_secret$/',
  67. '/^oauth_instance_url$/',
  68. ],
  69. 'integration_google' => [
  70. '/^client_id$/',
  71. '/^client_secret$/',
  72. ],
  73. 'integration_jira' => [
  74. '/^client_id$/',
  75. '/^client_secret$/',
  76. '/^forced_instance_url$/',
  77. ],
  78. 'integration_onedrive' => [
  79. '/^client_id$/',
  80. '/^client_secret$/',
  81. ],
  82. 'integration_openproject' => [
  83. '/^client_id$/',
  84. '/^client_secret$/',
  85. '/^oauth_instance_url$/',
  86. ],
  87. 'integration_reddit' => [
  88. '/^client_id$/',
  89. '/^client_secret$/',
  90. ],
  91. 'integration_suitecrm' => [
  92. '/^client_id$/',
  93. '/^client_secret$/',
  94. '/^oauth_instance_url$/',
  95. ],
  96. 'integration_twitter' => [
  97. '/^consumer_key$/',
  98. '/^consumer_secret$/',
  99. '/^followed_user$/',
  100. ],
  101. 'integration_zammad' => [
  102. '/^client_id$/',
  103. '/^client_secret$/',
  104. '/^oauth_instance_url$/',
  105. ],
  106. 'notify_push' => [
  107. '/^cookie$/',
  108. ],
  109. 'spreed' => [
  110. '/^bridge_bot_password$/',
  111. '/^hosted-signaling-server-(.*)$/',
  112. '/^signaling_servers$/',
  113. '/^signaling_ticket_secret$/',
  114. '/^signaling_token_privkey_(.*)$/',
  115. '/^signaling_token_pubkey_(.*)$/',
  116. '/^sip_bridge_dialin_info$/',
  117. '/^sip_bridge_shared_secret$/',
  118. '/^stun_servers$/',
  119. '/^turn_servers$/',
  120. '/^turn_server_secret$/',
  121. ],
  122. 'support' => [
  123. '/^last_response$/',
  124. '/^potential_subscription_key$/',
  125. '/^subscription_key$/',
  126. ],
  127. 'theming' => [
  128. '/^imprintUrl$/',
  129. '/^privacyUrl$/',
  130. '/^slogan$/',
  131. '/^url$/',
  132. ],
  133. 'user_ldap' => [
  134. '/^(s..)?ldap_agent_password$/',
  135. ],
  136. 'user_saml' => [
  137. '/^idp-x509cert$/',
  138. ],
  139. ];
  140. /** @var Connection */
  141. protected $conn;
  142. /** @var array[] */
  143. private $cache = [];
  144. /** @var bool */
  145. private $configLoaded = false;
  146. /**
  147. * @param Connection $conn
  148. */
  149. public function __construct(Connection $conn) {
  150. $this->conn = $conn;
  151. }
  152. /**
  153. * @param string $app
  154. * @return array
  155. */
  156. private function getAppValues($app) {
  157. $this->loadConfigValues();
  158. if (isset($this->cache[$app])) {
  159. return $this->cache[$app];
  160. }
  161. return [];
  162. }
  163. /**
  164. * Get all apps using the config
  165. *
  166. * @return array an array of app ids
  167. *
  168. * This function returns a list of all apps that have at least one
  169. * entry in the appconfig table.
  170. */
  171. public function getApps() {
  172. $this->loadConfigValues();
  173. return $this->getSortedKeys($this->cache);
  174. }
  175. /**
  176. * Get the available keys for an app
  177. *
  178. * @param string $app the app we are looking for
  179. * @return array an array of key names
  180. *
  181. * This function gets all keys of an app. Please note that the values are
  182. * not returned.
  183. */
  184. public function getKeys($app) {
  185. $this->loadConfigValues();
  186. if (isset($this->cache[$app])) {
  187. return $this->getSortedKeys($this->cache[$app]);
  188. }
  189. return [];
  190. }
  191. public function getSortedKeys($data) {
  192. $keys = array_keys($data);
  193. sort($keys);
  194. return $keys;
  195. }
  196. /**
  197. * Gets the config value
  198. *
  199. * @param string $app app
  200. * @param string $key key
  201. * @param string $default = null, default value if the key does not exist
  202. * @return string the value or $default
  203. *
  204. * This function gets a value from the appconfig table. If the key does
  205. * not exist the default value will be returned
  206. */
  207. public function getValue($app, $key, $default = null) {
  208. $this->loadConfigValues();
  209. if ($this->hasKey($app, $key)) {
  210. return $this->cache[$app][$key];
  211. }
  212. return $default;
  213. }
  214. /**
  215. * check if a key is set in the appconfig
  216. *
  217. * @param string $app
  218. * @param string $key
  219. * @return bool
  220. */
  221. public function hasKey($app, $key) {
  222. $this->loadConfigValues();
  223. return isset($this->cache[$app][$key]);
  224. }
  225. /**
  226. * Sets a value. If the key did not exist before it will be created.
  227. *
  228. * @param string $app app
  229. * @param string $key key
  230. * @param string|float|int $value value
  231. * @return bool True if the value was inserted or updated, false if the value was the same
  232. */
  233. public function setValue($app, $key, $value) {
  234. if (!$this->hasKey($app, $key)) {
  235. $inserted = (bool) $this->conn->insertIfNotExist('*PREFIX*appconfig', [
  236. 'appid' => $app,
  237. 'configkey' => $key,
  238. 'configvalue' => $value,
  239. ], [
  240. 'appid',
  241. 'configkey',
  242. ]);
  243. if ($inserted) {
  244. if (!isset($this->cache[$app])) {
  245. $this->cache[$app] = [];
  246. }
  247. $this->cache[$app][$key] = $value;
  248. return true;
  249. }
  250. }
  251. $sql = $this->conn->getQueryBuilder();
  252. $sql->update('appconfig')
  253. ->set('configvalue', $sql->createNamedParameter($value))
  254. ->where($sql->expr()->eq('appid', $sql->createNamedParameter($app)))
  255. ->andWhere($sql->expr()->eq('configkey', $sql->createNamedParameter($key)));
  256. /*
  257. * Only limit to the existing value for non-Oracle DBs:
  258. * http://docs.oracle.com/cd/E11882_01/server.112/e26088/conditions002.htm#i1033286
  259. * > Large objects (LOBs) are not supported in comparison conditions.
  260. */
  261. if (!($this->conn instanceof OracleConnection)) {
  262. /*
  263. * Only update the value when it is not the same
  264. * Note that NULL requires some special handling. Since comparing
  265. * against null can have special results.
  266. */
  267. if ($value === null) {
  268. $sql->andWhere(
  269. $sql->expr()->isNotNull('configvalue')
  270. );
  271. } else {
  272. $sql->andWhere(
  273. $sql->expr()->orX(
  274. $sql->expr()->isNull('configvalue'),
  275. $sql->expr()->neq('configvalue', $sql->createNamedParameter($value))
  276. )
  277. );
  278. }
  279. }
  280. $changedRow = (bool) $sql->execute();
  281. $this->cache[$app][$key] = $value;
  282. return $changedRow;
  283. }
  284. /**
  285. * Deletes a key
  286. *
  287. * @param string $app app
  288. * @param string $key key
  289. * @return boolean
  290. */
  291. public function deleteKey($app, $key) {
  292. $this->loadConfigValues();
  293. $sql = $this->conn->getQueryBuilder();
  294. $sql->delete('appconfig')
  295. ->where($sql->expr()->eq('appid', $sql->createParameter('app')))
  296. ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey')))
  297. ->setParameter('app', $app)
  298. ->setParameter('configkey', $key);
  299. $sql->execute();
  300. unset($this->cache[$app][$key]);
  301. return false;
  302. }
  303. /**
  304. * Remove app from appconfig
  305. *
  306. * @param string $app app
  307. * @return boolean
  308. *
  309. * Removes all keys in appconfig belonging to the app.
  310. */
  311. public function deleteApp($app) {
  312. $this->loadConfigValues();
  313. $sql = $this->conn->getQueryBuilder();
  314. $sql->delete('appconfig')
  315. ->where($sql->expr()->eq('appid', $sql->createParameter('app')))
  316. ->setParameter('app', $app);
  317. $sql->execute();
  318. unset($this->cache[$app]);
  319. return false;
  320. }
  321. /**
  322. * get multiple values, either the app or key can be used as wildcard by setting it to false
  323. *
  324. * @param string|false $app
  325. * @param string|false $key
  326. * @return array|false
  327. */
  328. public function getValues($app, $key) {
  329. if (($app !== false) === ($key !== false)) {
  330. return false;
  331. }
  332. if ($key === false) {
  333. return $this->getAppValues($app);
  334. } else {
  335. $appIds = $this->getApps();
  336. $values = array_map(function ($appId) use ($key) {
  337. return isset($this->cache[$appId][$key]) ? $this->cache[$appId][$key] : null;
  338. }, $appIds);
  339. $result = array_combine($appIds, $values);
  340. return array_filter($result);
  341. }
  342. }
  343. /**
  344. * get all values of the app or and filters out sensitive data
  345. *
  346. * @param string $app
  347. * @return array
  348. */
  349. public function getFilteredValues($app) {
  350. $values = $this->getValues($app, false);
  351. if (isset($this->sensitiveValues[$app])) {
  352. foreach ($this->sensitiveValues[$app] as $sensitiveKeyExp) {
  353. $sensitiveKeys = preg_grep($sensitiveKeyExp, array_keys($values));
  354. foreach ($sensitiveKeys as $sensitiveKey) {
  355. $values[$sensitiveKey] = IConfig::SENSITIVE_VALUE;
  356. }
  357. }
  358. }
  359. return $values;
  360. }
  361. /**
  362. * Load all the app config values
  363. */
  364. protected function loadConfigValues() {
  365. if ($this->configLoaded) {
  366. return;
  367. }
  368. $this->cache = [];
  369. $sql = $this->conn->getQueryBuilder();
  370. $sql->select('*')
  371. ->from('appconfig');
  372. $result = $sql->execute();
  373. // we are going to store the result in memory anyway
  374. $rows = $result->fetchAll();
  375. foreach ($rows as $row) {
  376. if (!isset($this->cache[$row['appid']])) {
  377. $this->cache[(string)$row['appid']] = [];
  378. }
  379. $this->cache[(string)$row['appid']][(string)$row['configkey']] = (string)$row['configvalue'];
  380. }
  381. $result->closeCursor();
  382. $this->configLoaded = true;
  383. }
  384. /**
  385. * Clear all the cached app config values
  386. * New cache will be generated next time a config value is retrieved
  387. */
  388. public function clearCachedConfig(): void {
  389. $this->configLoaded = false;
  390. }
  391. }