AppConfig.php 8.1 KB

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