Import.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Core\Command\Config;
  25. use OCP\IConfig;
  26. use Stecman\Component\Symfony\Console\BashCompletion\Completion;
  27. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  28. use Stecman\Component\Symfony\Console\BashCompletion\Completion\ShellPathCompletion;
  29. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  30. use Symfony\Component\Console\Command\Command;
  31. use Symfony\Component\Console\Input\InputArgument;
  32. use Symfony\Component\Console\Input\InputInterface;
  33. use Symfony\Component\Console\Output\OutputInterface;
  34. class Import extends Command implements CompletionAwareInterface {
  35. protected array $validRootKeys = ['system', 'apps'];
  36. public function __construct(
  37. protected IConfig $config,
  38. ) {
  39. parent::__construct();
  40. }
  41. protected function configure() {
  42. $this
  43. ->setName('config:import')
  44. ->setDescription('Import a list of configs')
  45. ->addArgument(
  46. 'file',
  47. InputArgument::OPTIONAL,
  48. 'File with the json array to import'
  49. )
  50. ;
  51. }
  52. protected function execute(InputInterface $input, OutputInterface $output): int {
  53. $importFile = $input->getArgument('file');
  54. if ($importFile !== null) {
  55. $content = $this->getArrayFromFile($importFile);
  56. } else {
  57. $content = $this->getArrayFromStdin();
  58. }
  59. try {
  60. $configs = $this->validateFileContent($content);
  61. } catch (\UnexpectedValueException $e) {
  62. $output->writeln('<error>' . $e->getMessage(). '</error>');
  63. return 1;
  64. }
  65. if (!empty($configs['system'])) {
  66. $this->config->setSystemValues($configs['system']);
  67. }
  68. if (!empty($configs['apps'])) {
  69. foreach ($configs['apps'] as $app => $appConfigs) {
  70. foreach ($appConfigs as $key => $value) {
  71. if ($value === null) {
  72. $this->config->deleteAppValue($app, $key);
  73. } else {
  74. $this->config->setAppValue($app, $key, $value);
  75. }
  76. }
  77. }
  78. }
  79. $output->writeln('<info>Config successfully imported from: ' . $importFile . '</info>');
  80. return 0;
  81. }
  82. /**
  83. * Get the content from stdin ("config:import < file.json")
  84. *
  85. * @return string
  86. */
  87. protected function getArrayFromStdin() {
  88. // Read from stdin. stream_set_blocking is used to prevent blocking
  89. // when nothing is passed via stdin.
  90. stream_set_blocking(STDIN, 0);
  91. $content = file_get_contents('php://stdin');
  92. stream_set_blocking(STDIN, 1);
  93. return $content;
  94. }
  95. /**
  96. * Get the content of the specified file ("config:import file.json")
  97. *
  98. * @param string $importFile
  99. * @return string
  100. */
  101. protected function getArrayFromFile($importFile) {
  102. return file_get_contents($importFile);
  103. }
  104. /**
  105. * @param string $content
  106. * @return array
  107. * @throws \UnexpectedValueException when the array is invalid
  108. */
  109. protected function validateFileContent($content) {
  110. $decodedContent = json_decode($content, true);
  111. if (!is_array($decodedContent) || empty($decodedContent)) {
  112. throw new \UnexpectedValueException('The file must contain a valid json array');
  113. }
  114. $this->validateArray($decodedContent);
  115. return $decodedContent;
  116. }
  117. /**
  118. * Validates that the array only contains `system` and `apps`
  119. *
  120. * @param array $array
  121. */
  122. protected function validateArray($array) {
  123. $arrayKeys = array_keys($array);
  124. $additionalKeys = array_diff($arrayKeys, $this->validRootKeys);
  125. $commonKeys = array_intersect($arrayKeys, $this->validRootKeys);
  126. if (!empty($additionalKeys)) {
  127. throw new \UnexpectedValueException('Found invalid entries in root: ' . implode(', ', $additionalKeys));
  128. }
  129. if (empty($commonKeys)) {
  130. throw new \UnexpectedValueException('At least one key of the following is expected: ' . implode(', ', $this->validRootKeys));
  131. }
  132. if (isset($array['system'])) {
  133. if (is_array($array['system'])) {
  134. foreach ($array['system'] as $name => $value) {
  135. $this->checkTypeRecursively($value, $name);
  136. }
  137. } else {
  138. throw new \UnexpectedValueException('The system config array is not an array');
  139. }
  140. }
  141. if (isset($array['apps'])) {
  142. if (is_array($array['apps'])) {
  143. $this->validateAppsArray($array['apps']);
  144. } else {
  145. throw new \UnexpectedValueException('The apps config array is not an array');
  146. }
  147. }
  148. }
  149. /**
  150. * @param mixed $configValue
  151. * @param string $configName
  152. */
  153. protected function checkTypeRecursively($configValue, $configName) {
  154. if (!is_array($configValue) && !is_bool($configValue) && !is_int($configValue) && !is_string($configValue) && !is_null($configValue) && !is_float($configValue)) {
  155. throw new \UnexpectedValueException('Invalid system config value for "' . $configName . '". Only arrays, bools, integers, floats, strings and null (delete) are allowed.');
  156. }
  157. if (is_array($configValue)) {
  158. foreach ($configValue as $key => $value) {
  159. $this->checkTypeRecursively($value, $configName);
  160. }
  161. }
  162. }
  163. /**
  164. * Validates that app configs are only integers and strings
  165. *
  166. * @param array $array
  167. */
  168. protected function validateAppsArray($array) {
  169. foreach ($array as $app => $configs) {
  170. foreach ($configs as $name => $value) {
  171. if (!is_int($value) && !is_string($value) && !is_null($value)) {
  172. throw new \UnexpectedValueException('Invalid app config value for "' . $app . '":"' . $name . '". Only integers, strings and null (delete) are allowed.');
  173. }
  174. }
  175. }
  176. }
  177. /**
  178. * @param string $optionName
  179. * @param CompletionContext $context
  180. * @return string[]
  181. */
  182. public function completeOptionValues($optionName, CompletionContext $context) {
  183. return [];
  184. }
  185. /**
  186. * @param string $argumentName
  187. * @param CompletionContext $context
  188. * @return string[]
  189. */
  190. public function completeArgumentValues($argumentName, CompletionContext $context) {
  191. if ($argumentName === 'file') {
  192. $helper = new ShellPathCompletion(
  193. $this->getName(),
  194. 'file',
  195. Completion::TYPE_ARGUMENT
  196. );
  197. return $helper->run();
  198. }
  199. return [];
  200. }
  201. }