1
0

AppConfig.php 10 KB

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