AppConfig.php 10 KB

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