Import.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Core\Command\Config;
  8. use OCP\IConfig;
  9. use Stecman\Component\Symfony\Console\BashCompletion\Completion;
  10. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  11. use Stecman\Component\Symfony\Console\BashCompletion\Completion\ShellPathCompletion;
  12. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  13. use Symfony\Component\Console\Command\Command;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. class Import extends Command implements CompletionAwareInterface {
  18. protected array $validRootKeys = ['system', 'apps'];
  19. public function __construct(
  20. protected IConfig $config,
  21. ) {
  22. parent::__construct();
  23. }
  24. protected function configure() {
  25. $this
  26. ->setName('config:import')
  27. ->setDescription('Import a list of configs')
  28. ->addArgument(
  29. 'file',
  30. InputArgument::OPTIONAL,
  31. 'File with the json array to import'
  32. )
  33. ;
  34. }
  35. protected function execute(InputInterface $input, OutputInterface $output): int {
  36. $importFile = $input->getArgument('file');
  37. if ($importFile !== null) {
  38. $content = $this->getArrayFromFile($importFile);
  39. } else {
  40. $content = $this->getArrayFromStdin();
  41. }
  42. try {
  43. $configs = $this->validateFileContent($content);
  44. } catch (\UnexpectedValueException $e) {
  45. $output->writeln('<error>' . $e->getMessage() . '</error>');
  46. return 1;
  47. }
  48. if (!empty($configs['system'])) {
  49. $this->config->setSystemValues($configs['system']);
  50. }
  51. if (!empty($configs['apps'])) {
  52. foreach ($configs['apps'] as $app => $appConfigs) {
  53. foreach ($appConfigs as $key => $value) {
  54. if ($value === null) {
  55. $this->config->deleteAppValue($app, $key);
  56. } else {
  57. $this->config->setAppValue($app, $key, $value);
  58. }
  59. }
  60. }
  61. }
  62. $output->writeln('<info>Config successfully imported from: ' . $importFile . '</info>');
  63. return 0;
  64. }
  65. /**
  66. * Get the content from stdin ("config:import < file.json")
  67. *
  68. * @return string
  69. */
  70. protected function getArrayFromStdin() {
  71. // Read from stdin. stream_set_blocking is used to prevent blocking
  72. // when nothing is passed via stdin.
  73. stream_set_blocking(STDIN, 0);
  74. $content = file_get_contents('php://stdin');
  75. stream_set_blocking(STDIN, 1);
  76. return $content;
  77. }
  78. /**
  79. * Get the content of the specified file ("config:import file.json")
  80. *
  81. * @param string $importFile
  82. * @return string
  83. */
  84. protected function getArrayFromFile($importFile) {
  85. return file_get_contents($importFile);
  86. }
  87. /**
  88. * @param string $content
  89. * @return array
  90. * @throws \UnexpectedValueException when the array is invalid
  91. */
  92. protected function validateFileContent($content) {
  93. $decodedContent = json_decode($content, true);
  94. if (!is_array($decodedContent) || empty($decodedContent)) {
  95. throw new \UnexpectedValueException('The file must contain a valid json array');
  96. }
  97. $this->validateArray($decodedContent);
  98. return $decodedContent;
  99. }
  100. /**
  101. * Validates that the array only contains `system` and `apps`
  102. *
  103. * @param array $array
  104. */
  105. protected function validateArray($array) {
  106. $arrayKeys = array_keys($array);
  107. $additionalKeys = array_diff($arrayKeys, $this->validRootKeys);
  108. $commonKeys = array_intersect($arrayKeys, $this->validRootKeys);
  109. if (!empty($additionalKeys)) {
  110. throw new \UnexpectedValueException('Found invalid entries in root: ' . implode(', ', $additionalKeys));
  111. }
  112. if (empty($commonKeys)) {
  113. throw new \UnexpectedValueException('At least one key of the following is expected: ' . implode(', ', $this->validRootKeys));
  114. }
  115. if (isset($array['system'])) {
  116. if (is_array($array['system'])) {
  117. foreach ($array['system'] as $name => $value) {
  118. $this->checkTypeRecursively($value, $name);
  119. }
  120. } else {
  121. throw new \UnexpectedValueException('The system config array is not an array');
  122. }
  123. }
  124. if (isset($array['apps'])) {
  125. if (is_array($array['apps'])) {
  126. $this->validateAppsArray($array['apps']);
  127. } else {
  128. throw new \UnexpectedValueException('The apps config array is not an array');
  129. }
  130. }
  131. }
  132. /**
  133. * @param mixed $configValue
  134. * @param string $configName
  135. */
  136. protected function checkTypeRecursively($configValue, $configName) {
  137. if (!is_array($configValue) && !is_bool($configValue) && !is_int($configValue) && !is_string($configValue) && !is_null($configValue) && !is_float($configValue)) {
  138. throw new \UnexpectedValueException('Invalid system config value for "' . $configName . '". Only arrays, bools, integers, floats, strings and null (delete) are allowed.');
  139. }
  140. if (is_array($configValue)) {
  141. foreach ($configValue as $key => $value) {
  142. $this->checkTypeRecursively($value, $configName);
  143. }
  144. }
  145. }
  146. /**
  147. * Validates that app configs are only integers and strings
  148. *
  149. * @param array $array
  150. */
  151. protected function validateAppsArray($array) {
  152. foreach ($array as $app => $configs) {
  153. foreach ($configs as $name => $value) {
  154. if (!is_int($value) && !is_string($value) && !is_null($value)) {
  155. throw new \UnexpectedValueException('Invalid app config value for "' . $app . '":"' . $name . '". Only integers, strings and null (delete) are allowed.');
  156. }
  157. }
  158. }
  159. }
  160. /**
  161. * @param string $optionName
  162. * @param CompletionContext $context
  163. * @return string[]
  164. */
  165. public function completeOptionValues($optionName, CompletionContext $context) {
  166. return [];
  167. }
  168. /**
  169. * @param string $argumentName
  170. * @param CompletionContext $context
  171. * @return string[]
  172. */
  173. public function completeArgumentValues($argumentName, CompletionContext $context) {
  174. if ($argumentName === 'file') {
  175. $helper = new ShellPathCompletion(
  176. $this->getName(),
  177. 'file',
  178. Completion::TYPE_ARGUMENT
  179. );
  180. return $helper->run();
  181. }
  182. return [];
  183. }
  184. }