SystemConfig.php 4.3 KB

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