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.

357 lines
8.5 KiB

3 years ago
  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. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OC;
  33. use OC\DB\OracleConnection;
  34. use OCP\IAppConfig;
  35. use OCP\IConfig;
  36. use OCP\IDBConnection;
  37. /**
  38. * This class provides an easy way for apps to store config values in the
  39. * database.
  40. */
  41. class AppConfig implements IAppConfig {
  42. /** @var array[] */
  43. protected $sensitiveValues = [
  44. 'external' => [
  45. '/^sites$/',
  46. ],
  47. 'spreed' => [
  48. '/^bridge_bot_password/',
  49. '/^signaling_servers$/',
  50. '/^signaling_ticket_secret$/',
  51. '/^stun_servers$/',
  52. '/^turn_servers$/',
  53. '/^turn_server_secret$/',
  54. ],
  55. 'theming' => [
  56. '/^imprintUrl$/',
  57. '/^privacyUrl$/',
  58. '/^slogan$/',
  59. '/^url$/',
  60. ],
  61. 'user_ldap' => [
  62. '/^(s..)?ldap_agent_password$/',
  63. ],
  64. ];
  65. /** @var \OCP\IDBConnection */
  66. protected $conn;
  67. /** @var array[] */
  68. private $cache = [];
  69. /** @var bool */
  70. private $configLoaded = false;
  71. /**
  72. * @param IDBConnection $conn
  73. */
  74. public function __construct(IDBConnection $conn) {
  75. $this->conn = $conn;
  76. $this->configLoaded = false;
  77. }
  78. /**
  79. * @param string $app
  80. * @return array
  81. */
  82. private function getAppValues($app) {
  83. $this->loadConfigValues();
  84. if (isset($this->cache[$app])) {
  85. return $this->cache[$app];
  86. }
  87. return [];
  88. }
  89. /**
  90. * Get all apps using the config
  91. *
  92. * @return array an array of app ids
  93. *
  94. * This function returns a list of all apps that have at least one
  95. * entry in the appconfig table.
  96. */
  97. public function getApps() {
  98. $this->loadConfigValues();
  99. return $this->getSortedKeys($this->cache);
  100. }
  101. /**
  102. * Get the available keys for an app
  103. *
  104. * @param string $app the app we are looking for
  105. * @return array an array of key names
  106. *
  107. * This function gets all keys of an app. Please note that the values are
  108. * not returned.
  109. */
  110. public function getKeys($app) {
  111. $this->loadConfigValues();
  112. if (isset($this->cache[$app])) {
  113. return $this->getSortedKeys($this->cache[$app]);
  114. }
  115. return [];
  116. }
  117. public function getSortedKeys($data) {
  118. $keys = array_keys($data);
  119. sort($keys);
  120. return $keys;
  121. }
  122. /**
  123. * Gets the config value
  124. *
  125. * @param string $app app
  126. * @param string $key key
  127. * @param string $default = null, default value if the key does not exist
  128. * @return string the value or $default
  129. *
  130. * This function gets a value from the appconfig table. If the key does
  131. * not exist the default value will be returned
  132. */
  133. public function getValue($app, $key, $default = null) {
  134. $this->loadConfigValues();
  135. if ($this->hasKey($app, $key)) {
  136. return $this->cache[$app][$key];
  137. }
  138. return $default;
  139. }
  140. /**
  141. * check if a key is set in the appconfig
  142. *
  143. * @param string $app
  144. * @param string $key
  145. * @return bool
  146. */
  147. public function hasKey($app, $key) {
  148. $this->loadConfigValues();
  149. return isset($this->cache[$app][$key]);
  150. }
  151. /**
  152. * Sets a value. If the key did not exist before it will be created.
  153. *
  154. * @param string $app app
  155. * @param string $key key
  156. * @param string|float|int $value value
  157. * @return bool True if the value was inserted or updated, false if the value was the same
  158. */
  159. public function setValue($app, $key, $value) {
  160. if (!$this->hasKey($app, $key)) {
  161. $inserted = (bool) $this->conn->insertIfNotExist('*PREFIX*appconfig', [
  162. 'appid' => $app,
  163. 'configkey' => $key,
  164. 'configvalue' => $value,
  165. ], [
  166. 'appid',
  167. 'configkey',
  168. ]);
  169. if ($inserted) {
  170. if (!isset($this->cache[$app])) {
  171. $this->cache[$app] = [];
  172. }
  173. $this->cache[$app][$key] = $value;
  174. return true;
  175. }
  176. }
  177. $sql = $this->conn->getQueryBuilder();
  178. $sql->update('appconfig')
  179. ->set('configvalue', $sql->createParameter('configvalue'))
  180. ->where($sql->expr()->eq('appid', $sql->createParameter('app')))
  181. ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey')))
  182. ->setParameter('configvalue', $value)
  183. ->setParameter('app', $app)
  184. ->setParameter('configkey', $key);
  185. /*
  186. * Only limit to the existing value for non-Oracle DBs:
  187. * http://docs.oracle.com/cd/E11882_01/server.112/e26088/conditions002.htm#i1033286
  188. * > Large objects (LOBs) are not supported in comparison conditions.
  189. */
  190. if (!($this->conn instanceof OracleConnection)) {
  191. // Only update the value when it is not the same
  192. $sql->andWhere($sql->expr()->neq('configvalue', $sql->createParameter('configvalue')))
  193. ->setParameter('configvalue', $value);
  194. }
  195. $changedRow = (bool) $sql->execute();
  196. $this->cache[$app][$key] = $value;
  197. return $changedRow;
  198. }
  199. /**
  200. * Deletes a key
  201. *
  202. * @param string $app app
  203. * @param string $key key
  204. * @return boolean
  205. */
  206. public function deleteKey($app, $key) {
  207. $this->loadConfigValues();
  208. $sql = $this->conn->getQueryBuilder();
  209. $sql->delete('appconfig')
  210. ->where($sql->expr()->eq('appid', $sql->createParameter('app')))
  211. ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey')))
  212. ->setParameter('app', $app)
  213. ->setParameter('configkey', $key);
  214. $sql->execute();
  215. unset($this->cache[$app][$key]);
  216. return false;
  217. }
  218. /**
  219. * Remove app from appconfig
  220. *
  221. * @param string $app app
  222. * @return boolean
  223. *
  224. * Removes all keys in appconfig belonging to the app.
  225. */
  226. public function deleteApp($app) {
  227. $this->loadConfigValues();
  228. $sql = $this->conn->getQueryBuilder();
  229. $sql->delete('appconfig')
  230. ->where($sql->expr()->eq('appid', $sql->createParameter('app')))
  231. ->setParameter('app', $app);
  232. $sql->execute();
  233. unset($this->cache[$app]);
  234. return false;
  235. }
  236. /**
  237. * get multiple values, either the app or key can be used as wildcard by setting it to false
  238. *
  239. * @param string|false $app
  240. * @param string|false $key
  241. * @return array|false
  242. */
  243. public function getValues($app, $key) {
  244. if (($app !== false) === ($key !== false)) {
  245. return false;
  246. }
  247. if ($key === false) {
  248. return $this->getAppValues($app);
  249. } else {
  250. $appIds = $this->getApps();
  251. $values = array_map(function ($appId) use ($key) {
  252. return isset($this->cache[$appId][$key]) ? $this->cache[$appId][$key] : null;
  253. }, $appIds);
  254. $result = array_combine($appIds, $values);
  255. return array_filter($result);
  256. }
  257. }
  258. /**
  259. * get all values of the app or and filters out sensitive data
  260. *
  261. * @param string $app
  262. * @return array
  263. */
  264. public function getFilteredValues($app) {
  265. $values = $this->getValues($app, false);
  266. if (isset($this->sensitiveValues[$app])) {
  267. foreach ($this->sensitiveValues[$app] as $sensitiveKeyExp) {
  268. $sensitiveKeys = preg_grep($sensitiveKeyExp, array_keys($values));
  269. foreach ($sensitiveKeys as $sensitiveKey) {
  270. $values[$sensitiveKey] = IConfig::SENSITIVE_VALUE;
  271. }
  272. }
  273. }
  274. return $values;
  275. }
  276. /**
  277. * Load all the app config values
  278. */
  279. protected function loadConfigValues() {
  280. if ($this->configLoaded) {
  281. return;
  282. }
  283. $this->cache = [];
  284. $sql = $this->conn->getQueryBuilder();
  285. $sql->select('*')
  286. ->from('appconfig');
  287. $result = $sql->execute();
  288. // we are going to store the result in memory anyway
  289. $rows = $result->fetchAll();
  290. foreach ($rows as $row) {
  291. if (!isset($this->cache[$row['appid']])) {
  292. $this->cache[$row['appid']] = [];
  293. }
  294. $this->cache[$row['appid']][$row['configkey']] = $row['configvalue'];
  295. }
  296. $result->closeCursor();
  297. $this->configLoaded = true;
  298. }
  299. /**
  300. * Clear all the cached app config values
  301. *
  302. * WARNING: do not use this - this is only for usage with the SCSSCacher to
  303. * clear the memory cache of the app config
  304. */
  305. public function clearCachedConfig() {
  306. $this->configLoaded = false;
  307. }
  308. }