Import.php 5.6 KB

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