You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Import.php 6.5KB

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