1
0

SystemConfig.php 4.0 KB

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