Import.php 6.5 KB

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