Import.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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) {
  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;
  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. }
  85. /**
  86. * Get the content from stdin ("config:import < file.json")
  87. *
  88. * @return string
  89. */
  90. protected function getArrayFromStdin() {
  91. // Read from stdin. stream_set_blocking is used to prevent blocking
  92. // when nothing is passed via stdin.
  93. stream_set_blocking(STDIN, 0);
  94. $content = file_get_contents('php://stdin');
  95. stream_set_blocking(STDIN, 1);
  96. return $content;
  97. }
  98. /**
  99. * Get the content of the specified file ("config:import file.json")
  100. *
  101. * @param string $importFile
  102. * @return string
  103. */
  104. protected function getArrayFromFile($importFile) {
  105. return file_get_contents($importFile);
  106. }
  107. /**
  108. * @param string $content
  109. * @return array
  110. * @throws \UnexpectedValueException when the array is invalid
  111. */
  112. protected function validateFileContent($content) {
  113. $decodedContent = json_decode($content, true);
  114. if (!is_array($decodedContent) || empty($decodedContent)) {
  115. throw new \UnexpectedValueException('The file must contain a valid json array');
  116. }
  117. $this->validateArray($decodedContent);
  118. return $decodedContent;
  119. }
  120. /**
  121. * Validates that the array only contains `system` and `apps`
  122. *
  123. * @param array $array
  124. */
  125. protected function validateArray($array) {
  126. $arrayKeys = array_keys($array);
  127. $additionalKeys = array_diff($arrayKeys, $this->validRootKeys);
  128. $commonKeys = array_intersect($arrayKeys, $this->validRootKeys);
  129. if (!empty($additionalKeys)) {
  130. throw new \UnexpectedValueException('Found invalid entries in root: ' . implode(', ', $additionalKeys));
  131. }
  132. if (empty($commonKeys)) {
  133. throw new \UnexpectedValueException('At least one key of the following is expected: ' . implode(', ', $this->validRootKeys));
  134. }
  135. if (isset($array['system'])) {
  136. if (is_array($array['system'])) {
  137. foreach ($array['system'] as $name => $value) {
  138. $this->checkTypeRecursively($value, $name);
  139. }
  140. } else {
  141. throw new \UnexpectedValueException('The system config array is not an array');
  142. }
  143. }
  144. if (isset($array['apps'])) {
  145. if (is_array($array['apps'])) {
  146. $this->validateAppsArray($array['apps']);
  147. } else {
  148. throw new \UnexpectedValueException('The apps config array is not an array');
  149. }
  150. }
  151. }
  152. /**
  153. * @param mixed $configValue
  154. * @param string $configName
  155. */
  156. protected function checkTypeRecursively($configValue, $configName) {
  157. if (!is_array($configValue) && !is_bool($configValue) && !is_int($configValue) && !is_string($configValue) && !is_null($configValue)) {
  158. throw new \UnexpectedValueException('Invalid system config value for "' . $configName . '". Only arrays, bools, integers, strings and null (delete) are allowed.');
  159. }
  160. if (is_array($configValue)) {
  161. foreach ($configValue as $key => $value) {
  162. $this->checkTypeRecursively($value, $configName);
  163. }
  164. }
  165. }
  166. /**
  167. * Validates that app configs are only integers and strings
  168. *
  169. * @param array $array
  170. */
  171. protected function validateAppsArray($array) {
  172. foreach ($array as $app => $configs) {
  173. foreach ($configs as $name => $value) {
  174. if (!is_int($value) && !is_string($value) && !is_null($value)) {
  175. throw new \UnexpectedValueException('Invalid app config value for "' . $app . '":"' . $name . '". Only integers, strings and null (delete) are allowed.');
  176. }
  177. }
  178. }
  179. }
  180. /**
  181. * @param string $optionName
  182. * @param CompletionContext $context
  183. * @return string[]
  184. */
  185. public function completeOptionValues($optionName, CompletionContext $context) {
  186. return [];
  187. }
  188. /**
  189. * @param string $argumentName
  190. * @param CompletionContext $context
  191. * @return string[]
  192. */
  193. public function completeArgumentValues($argumentName, CompletionContext $context) {
  194. if ($argumentName === 'file') {
  195. $helper = new ShellPathCompletion(
  196. $this->getName(),
  197. 'file',
  198. Completion::TYPE_ARGUMENT
  199. );
  200. return $helper->run();
  201. }
  202. return [];
  203. }
  204. }