AppConfig.php 11 KB

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