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.

SystemConfig.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Johannes Schlichenmaier <johannes@schlichenmaier.info>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC;
  28. use OCP\IConfig;
  29. /**
  30. * Class which provides access to the system config values stored in config.php
  31. * Internal class for bootstrap only.
  32. * fixes cyclic DI: AllConfig needs AppConfig needs Database needs AllConfig
  33. */
  34. class SystemConfig {
  35. /** @var array */
  36. protected $sensitiveValues = [
  37. 'instanceid' => true,
  38. 'datadirectory' => true,
  39. 'dbname' => true,
  40. 'dbhost' => true,
  41. 'dbpassword' => true,
  42. 'dbuser' => true,
  43. 'mail_from_address' => true,
  44. 'mail_domain' => true,
  45. 'mail_smtphost' => true,
  46. 'mail_smtpname' => true,
  47. 'mail_smtppassword' => true,
  48. 'passwordsalt' => true,
  49. 'secret' => true,
  50. 'updater.secret' => true,
  51. 'trusted_proxies' => true,
  52. 'proxyuserpwd' => true,
  53. 'log.condition' => [
  54. 'shared_secret' => true,
  55. ],
  56. 'license-key' => true,
  57. 'redis' => [
  58. 'host' => true,
  59. 'password' => true,
  60. ],
  61. 'objectstore' => [
  62. 'arguments' => [
  63. // Legacy Swift (https://github.com/nextcloud/server/pull/17696#discussion_r341302207)
  64. 'options' => [
  65. 'credentials' => [
  66. 'key' => true,
  67. 'secret' => true,
  68. ]
  69. ],
  70. // S3
  71. 'key' => true,
  72. 'secret' => true,
  73. // Swift v2
  74. 'username' => true,
  75. 'password' => true,
  76. // Swift v3
  77. 'user' => [
  78. 'name' => true,
  79. 'password' => true,
  80. ],
  81. ],
  82. ],
  83. ];
  84. /** @var Config */
  85. private $config;
  86. public function __construct(Config $config) {
  87. $this->config = $config;
  88. }
  89. /**
  90. * Lists all available config keys
  91. * @return array an array of key names
  92. */
  93. public function getKeys() {
  94. return $this->config->getKeys();
  95. }
  96. /**
  97. * Sets a new system wide value
  98. *
  99. * @param string $key the key of the value, under which will be saved
  100. * @param mixed $value the value that should be stored
  101. */
  102. public function setValue($key, $value) {
  103. $this->config->setValue($key, $value);
  104. }
  105. /**
  106. * Sets and deletes values and writes the config.php
  107. *
  108. * @param array $configs Associative array with `key => value` pairs
  109. * If value is null, the config key will be deleted
  110. */
  111. public function setValues(array $configs) {
  112. $this->config->setValues($configs);
  113. }
  114. /**
  115. * Looks up a system wide defined value
  116. *
  117. * @param string $key the key of the value, under which it was saved
  118. * @param mixed $default the default value to be returned if the value isn't set
  119. * @return mixed the value or $default
  120. */
  121. public function getValue($key, $default = '') {
  122. return $this->config->getValue($key, $default);
  123. }
  124. /**
  125. * Looks up a system wide defined value and filters out sensitive data
  126. *
  127. * @param string $key the key of the value, under which it was saved
  128. * @param mixed $default the default value to be returned if the value isn't set
  129. * @return mixed the value or $default
  130. */
  131. public function getFilteredValue($key, $default = '') {
  132. $value = $this->getValue($key, $default);
  133. if (isset($this->sensitiveValues[$key])) {
  134. $value = $this->removeSensitiveValue($this->sensitiveValues[$key], $value);
  135. }
  136. return $value;
  137. }
  138. /**
  139. * Delete a system wide defined value
  140. *
  141. * @param string $key the key of the value, under which it was saved
  142. */
  143. public function deleteValue($key) {
  144. $this->config->deleteKey($key);
  145. }
  146. /**
  147. * @param bool|array $keysToRemove
  148. * @param mixed $value
  149. * @return mixed
  150. */
  151. protected function removeSensitiveValue($keysToRemove, $value) {
  152. if ($keysToRemove === true) {
  153. return IConfig::SENSITIVE_VALUE;
  154. }
  155. if (is_array($value)) {
  156. foreach ($keysToRemove as $keyToRemove => $valueToRemove) {
  157. if (isset($value[$keyToRemove])) {
  158. $value[$keyToRemove] = $this->removeSensitiveValue($valueToRemove, $value[$keyToRemove]);
  159. }
  160. }
  161. }
  162. return $value;
  163. }
  164. }