Import.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 $validRootKeys = ['system', 'apps'];
  36. /** @var IConfig */
  37. protected $config;
  38. /**
  39. * @param IConfig $config
  40. */
  41. public function __construct(IConfig $config) {
  42. parent::__construct();
  43. $this->config = $config;
  44. }
  45. protected function configure() {
  46. $this
  47. ->setName('config:import')
  48. ->setDescription('Import a list of configs')
  49. ->addArgument(
  50. 'file',
  51. InputArgument::OPTIONAL,
  52. 'File with the json array to import'
  53. )
  54. ;
  55. }
  56. protected function execute(InputInterface $input, OutputInterface $output): int {
  57. $importFile = $input->getArgument('file');
  58. if ($importFile !== null) {
  59. $content = $this->getArrayFromFile($importFile);
  60. } else {
  61. $content = $this->getArrayFromStdin();
  62. }
  63. try {
  64. $configs = $this->validateFileContent($content);
  65. } catch (\UnexpectedValueException $e) {
  66. $output->writeln('<error>' . $e->getMessage(). '</error>');
  67. return 1;
  68. }
  69. if (!empty($configs['system'])) {
  70. $this->config->setSystemValues($configs['system']);
  71. }
  72. if (!empty($configs['apps'])) {
  73. foreach ($configs['apps'] as $app => $appConfigs) {
  74. foreach ($appConfigs as $key => $value) {
  75. if ($value === null) {
  76. $this->config->deleteAppValue($app, $key);
  77. } else {
  78. $this->config->setAppValue($app, $key, $value);
  79. }
  80. }
  81. }
  82. }
  83. $output->writeln('<info>Config successfully imported from: ' . $importFile . '</info>');
  84. return 0;
  85. }
  86. /**
  87. * Get the content from stdin ("config:import < file.json")
  88. *
  89. * @return string
  90. */
  91. protected function getArrayFromStdin() {
  92. // Read from stdin. stream_set_blocking is used to prevent blocking
  93. // when nothing is passed via stdin.
  94. stream_set_blocking(STDIN, 0);
  95. $content = file_get_contents('php://stdin');
  96. stream_set_blocking(STDIN, 1);
  97. return $content;
  98. }
  99. /**
  100. * Get the content of the specified file ("config:import file.json")
  101. *
  102. * @param string $importFile
  103. * @return string
  104. */
  105. protected function getArrayFromFile($importFile) {
  106. return file_get_contents($importFile);
  107. }
  108. /**
  109. * @param string $content
  110. * @return array
  111. * @throws \UnexpectedValueException when the array is invalid
  112. */
  113. protected function validateFileContent($content) {
  114. $decodedContent = json_decode($content, true);
  115. if (!is_array($decodedContent) || empty($decodedContent)) {
  116. throw new \UnexpectedValueException('The file must contain a valid json array');
  117. }
  118. $this->validateArray($decodedContent);
  119. return $decodedContent;
  120. }
  121. /**
  122. * Validates that the array only contains `system` and `apps`
  123. *
  124. * @param array $array
  125. */
  126. protected function validateArray($array) {
  127. $arrayKeys = array_keys($array);
  128. $additionalKeys = array_diff($arrayKeys, $this->validRootKeys);
  129. $commonKeys = array_intersect($arrayKeys, $this->validRootKeys);
  130. if (!empty($additionalKeys)) {
  131. throw new \UnexpectedValueException('Found invalid entries in root: ' . implode(', ', $additionalKeys));
  132. }
  133. if (empty($commonKeys)) {
  134. throw new \UnexpectedValueException('At least one key of the following is expected: ' . implode(', ', $this->validRootKeys));
  135. }
  136. if (isset($array['system'])) {
  137. if (is_array($array['system'])) {
  138. foreach ($array['system'] as $name => $value) {
  139. $this->checkTypeRecursively($value, $name);
  140. }
  141. } else {
  142. throw new \UnexpectedValueException('The system config array is not an array');
  143. }
  144. }
  145. if (isset($array['apps'])) {
  146. if (is_array($array['apps'])) {
  147. $this->validateAppsArray($array['apps']);
  148. } else {
  149. throw new \UnexpectedValueException('The apps config array is not an array');
  150. }
  151. }
  152. }
  153. /**
  154. * @param mixed $configValue
  155. * @param string $configName
  156. */
  157. protected function checkTypeRecursively($configValue, $configName) {
  158. if (!is_array($configValue) && !is_bool($configValue) && !is_int($configValue) && !is_string($configValue) && !is_null($configValue)) {
  159. throw new \UnexpectedValueException('Invalid system config value for "' . $configName . '". Only arrays, bools, integers, strings and null (delete) are allowed.');
  160. }
  161. if (is_array($configValue)) {
  162. foreach ($configValue as $key => $value) {
  163. $this->checkTypeRecursively($value, $configName);
  164. }
  165. }
  166. }
  167. /**
  168. * Validates that app configs are only integers and strings
  169. *
  170. * @param array $array
  171. */
  172. protected function validateAppsArray($array) {
  173. foreach ($array as $app => $configs) {
  174. foreach ($configs as $name => $value) {
  175. if (!is_int($value) && !is_string($value) && !is_null($value)) {
  176. throw new \UnexpectedValueException('Invalid app config value for "' . $app . '":"' . $name . '". Only integers, strings and null (delete) are allowed.');
  177. }
  178. }
  179. }
  180. }
  181. /**
  182. * @param string $optionName
  183. * @param CompletionContext $context
  184. * @return string[]
  185. */
  186. public function completeOptionValues($optionName, CompletionContext $context) {
  187. return [];
  188. }
  189. /**
  190. * @param string $argumentName
  191. * @param CompletionContext $context
  192. * @return string[]
  193. */
  194. public function completeArgumentValues($argumentName, CompletionContext $context) {
  195. if ($argumentName === 'file') {
  196. $helper = new ShellPathCompletion(
  197. $this->getName(),
  198. 'file',
  199. Completion::TYPE_ARGUMENT
  200. );
  201. return $helper->run();
  202. }
  203. return [];
  204. }
  205. }