You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AppConfig.php 10KB

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