Import.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Core\Command\Config;
  23. use OCP\IConfig;
  24. use Stecman\Component\Symfony\Console\BashCompletion\Completion;
  25. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  26. use Stecman\Component\Symfony\Console\BashCompletion\Completion\ShellPathCompletion;
  27. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  28. use Symfony\Component\Console\Command\Command;
  29. use Symfony\Component\Console\Input\InputArgument;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class Import extends Command implements CompletionAwareInterface {
  33. protected $validRootKeys = ['system', 'apps'];
  34. /** @var IConfig */
  35. protected $config;
  36. /**
  37. * @param IConfig $config
  38. */
  39. public function __construct(IConfig $config) {
  40. parent::__construct();
  41. $this->config = $config;
  42. }
  43. protected function configure() {
  44. $this
  45. ->setName('config:import')
  46. ->setDescription('Import a list of configs')
  47. ->addArgument(
  48. 'file',
  49. InputArgument::OPTIONAL,
  50. 'File with the json array to import'
  51. )
  52. ;
  53. }
  54. protected function execute(InputInterface $input, OutputInterface $output) {
  55. $importFile = $input->getArgument('file');
  56. if ($importFile !== null) {
  57. $content = $this->getArrayFromFile($importFile);
  58. } else {
  59. $content = $this->getArrayFromStdin();
  60. }
  61. try {
  62. $configs = $this->validateFileContent($content);
  63. } catch (\UnexpectedValueException $e) {
  64. $output->writeln('<error>' . $e->getMessage(). '</error>');
  65. return;
  66. }
  67. if (!empty($configs['system'])) {
  68. $this->config->setSystemValues($configs['system']);
  69. }
  70. if (!empty($configs['apps'])) {
  71. foreach ($configs['apps'] as $app => $appConfigs) {
  72. foreach ($appConfigs as $key => $value) {
  73. if ($value === null) {
  74. $this->config->deleteAppValue($app, $key);
  75. } else {
  76. $this->config->setAppValue($app, $key, $value);
  77. }
  78. }
  79. }
  80. }
  81. $output->writeln('<info>Config successfully imported from: ' . $importFile . '</info>');
  82. }
  83. /**
  84. * Get the content from stdin ("config:import < file.json")
  85. *
  86. * @return string
  87. */
  88. protected function getArrayFromStdin() {
  89. // Read from stdin. stream_set_blocking is used to prevent blocking
  90. // when nothing is passed via stdin.
  91. stream_set_blocking(STDIN, 0);
  92. $content = file_get_contents('php://stdin');
  93. stream_set_blocking(STDIN, 1);
  94. return $content;
  95. }
  96. /**
  97. * Get the content of the specified file ("config:import file.json")
  98. *
  99. * @param string $importFile
  100. * @return string
  101. */
  102. protected function getArrayFromFile($importFile) {
  103. $content = file_get_contents($importFile);
  104. return $content;
  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. }