AppConfig.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. 'external' => [
  46. '/^sites$/',
  47. ],
  48. 'spreed' => [
  49. '/^bridge_bot_password/',
  50. '/^signaling_servers$/',
  51. '/^signaling_ticket_secret$/',
  52. '/^stun_servers$/',
  53. '/^turn_servers$/',
  54. '/^turn_server_secret$/',
  55. ],
  56. 'theming' => [
  57. '/^imprintUrl$/',
  58. '/^privacyUrl$/',
  59. '/^slogan$/',
  60. '/^url$/',
  61. ],
  62. 'user_ldap' => [
  63. '/^(s..)?ldap_agent_password$/',
  64. ],
  65. ];
  66. /** @var Connection */
  67. protected $conn;
  68. /** @var array[] */
  69. private $cache = [];
  70. /** @var bool */
  71. private $configLoaded = false;
  72. /**
  73. * @param Connection $conn
  74. */
  75. public function __construct(Connection $conn) {
  76. $this->conn = $conn;
  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->createNamedParameter($value))
  180. ->where($sql->expr()->eq('appid', $sql->createNamedParameter($app)))
  181. ->andWhere($sql->expr()->eq('configkey', $sql->createNamedParameter($key)));
  182. /*
  183. * Only limit to the existing value for non-Oracle DBs:
  184. * http://docs.oracle.com/cd/E11882_01/server.112/e26088/conditions002.htm#i1033286
  185. * > Large objects (LOBs) are not supported in comparison conditions.
  186. */
  187. if (!($this->conn instanceof OracleConnection)) {
  188. /*
  189. * Only update the value when it is not the same
  190. * Note that NULL requires some special handling. Since comparing
  191. * against null can have special results.
  192. */
  193. if ($value === null) {
  194. $sql->andWhere(
  195. $sql->expr()->isNotNull('configvalue')
  196. );
  197. } else {
  198. $sql->andWhere(
  199. $sql->expr()->orX(
  200. $sql->expr()->isNull('configvalue'),
  201. $sql->expr()->neq('configvalue', $sql->createNamedParameter($value))
  202. )
  203. );
  204. }
  205. }
  206. $changedRow = (bool) $sql->execute();
  207. $this->cache[$app][$key] = $value;
  208. return $changedRow;
  209. }
  210. /**
  211. * Deletes a key
  212. *
  213. * @param string $app app
  214. * @param string $key key
  215. * @return boolean
  216. */
  217. public function deleteKey($app, $key) {
  218. $this->loadConfigValues();
  219. $sql = $this->conn->getQueryBuilder();
  220. $sql->delete('appconfig')
  221. ->where($sql->expr()->eq('appid', $sql->createParameter('app')))
  222. ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey')))
  223. ->setParameter('app', $app)
  224. ->setParameter('configkey', $key);
  225. $sql->execute();
  226. unset($this->cache[$app][$key]);
  227. return false;
  228. }
  229. /**
  230. * Remove app from appconfig
  231. *
  232. * @param string $app app
  233. * @return boolean
  234. *
  235. * Removes all keys in appconfig belonging to the app.
  236. */
  237. public function deleteApp($app) {
  238. $this->loadConfigValues();
  239. $sql = $this->conn->getQueryBuilder();
  240. $sql->delete('appconfig')
  241. ->where($sql->expr()->eq('appid', $sql->createParameter('app')))
  242. ->setParameter('app', $app);
  243. $sql->execute();
  244. unset($this->cache[$app]);
  245. return false;
  246. }
  247. /**
  248. * get multiple values, either the app or key can be used as wildcard by setting it to false
  249. *
  250. * @param string|false $app
  251. * @param string|false $key
  252. * @return array|false
  253. */
  254. public function getValues($app, $key) {
  255. if (($app !== false) === ($key !== false)) {
  256. return false;
  257. }
  258. if ($key === false) {
  259. return $this->getAppValues($app);
  260. } else {
  261. $appIds = $this->getApps();
  262. $values = array_map(function ($appId) use ($key) {
  263. return isset($this->cache[$appId][$key]) ? $this->cache[$appId][$key] : null;
  264. }, $appIds);
  265. $result = array_combine($appIds, $values);
  266. return array_filter($result);
  267. }
  268. }
  269. /**
  270. * get all values of the app or and filters out sensitive data
  271. *
  272. * @param string $app
  273. * @return array
  274. */
  275. public function getFilteredValues($app) {
  276. $values = $this->getValues($app, false);
  277. if (isset($this->sensitiveValues[$app])) {
  278. foreach ($this->sensitiveValues[$app] as $sensitiveKeyExp) {
  279. $sensitiveKeys = preg_grep($sensitiveKeyExp, array_keys($values));
  280. foreach ($sensitiveKeys as $sensitiveKey) {
  281. $values[$sensitiveKey] = IConfig::SENSITIVE_VALUE;
  282. }
  283. }
  284. }
  285. return $values;
  286. }
  287. /**
  288. * Load all the app config values
  289. */
  290. protected function loadConfigValues() {
  291. if ($this->configLoaded) {
  292. return;
  293. }
  294. $this->cache = [];
  295. $sql = $this->conn->getQueryBuilder();
  296. $sql->select('*')
  297. ->from('appconfig');
  298. $result = $sql->execute();
  299. // we are going to store the result in memory anyway
  300. $rows = $result->fetchAll();
  301. foreach ($rows as $row) {
  302. if (!isset($this->cache[$row['appid']])) {
  303. $this->cache[(string)$row['appid']] = [];
  304. }
  305. $this->cache[(string)$row['appid']][(string)$row['configkey']] = (string)$row['configvalue'];
  306. }
  307. $result->closeCursor();
  308. $this->configLoaded = true;
  309. }
  310. /**
  311. * Clear all the cached app config values
  312. *
  313. * WARNING: do not use this - this is only for usage with the SCSSCacher to
  314. * clear the memory cache of the app config
  315. */
  316. public function clearCachedConfig() {
  317. $this->configLoaded = false;
  318. }
  319. }