aboutsummaryrefslogtreecommitdiffstats
path: root/core/Command/Config/Import.php
blob: 7f1e09d2c95dd5a0c043865a1184bf333b9ddbbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
 * @author Joas Schilling <nickvergessen@owncloud.com>
 *
 * @copyright Copyright (c) 2016, ownCloud, Inc.
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

namespace OC\Core\Command\Config;

use OCP\IConfig;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Import extends Command {
	protected $validRootKeys = ['system', 'apps'];

	/** @var IConfig */
	protected $config;

	/**
	 * @param IConfig $config
	 */
	public function __construct(IConfig $config) {
		parent::__construct();
		$this->config = $config;
	}

	protected function configure() {
		$this
			->setName('config:import')
			->setDescription('Import a list of configs')
			->addArgument(
				'file',
				InputArgument::OPTIONAL,
				'File with the json array to import'
			)
		;
	}

	protected function execute(InputInterface $input, OutputInterface $output) {
		$importFile = $input->getArgument('file');
		if ($importFile !== null) {
			$content = $this->getArrayFromFile($importFile);
		} else {
			$content = $this->getArrayFromStdin();
		}

		try {
			$configs = $this->validateFileContent($content);
		} catch (\UnexpectedValueException $e) {
			$output->writeln('<error>' . $e->getMessage(). '</error>');
			return;
		}

		if (!empty($configs['system'])) {
			$this->config->setSystemValues($configs['system']);
		}

		if (!empty($configs['apps'])) {
			foreach ($configs['apps'] as $app => $appConfigs) {
				foreach ($appConfigs as $key => $value) {
					if ($value === null) {
						$this->config->deleteAppValue($app, $key);
					} else {
						$this->config->setAppValue($app, $key, $value);
					}
				}
			}
		}

		$output->writeln('<info>Config successfully imported from: ' . $importFile . '</info>');
	}

	/**
	 * Get the content from stdin ("config:import < file.json")
	 *
	 * @return string
	 */
	protected function getArrayFromStdin() {
		// Read from stdin. stream_set_blocking is used to prevent blocking
		// when nothing is passed via stdin.
		stream_set_blocking(STDIN, 0);
		$content = file_get_contents('php://stdin');
		stream_set_blocking(STDIN, 1);
		return $content;
	}

	/**
	 * Get the content of the specified file ("config:import file.json")
	 *
	 * @param string $importFile
	 * @return string
	 */
	protected function getArrayFromFile($importFile) {
		$content = file_get_contents($importFile);
		return $content;
	}

	/**
	 * @param string $content
	 * @return array
	 * @throws \UnexpectedValueException when the array is invalid
	 */
	protected function validateFileContent($content) {
		$decodedContent = json_decode($content, true);
		if (!is_array($decodedContent) || empty($decodedContent)) {
			throw new \UnexpectedValueException('The file must contain a valid json array');
		}

		$this->validateArray($decodedContent);

		return $decodedContent;
	}

	/**
	 * Validates that the array only contains `system` and `apps`
	 *
	 * @param array $array
	 */
	protected function validateArray($array) {
		$arrayKeys = array_keys($array);
		$additionalKeys = array_diff($arrayKeys, $this->validRootKeys);
		$commonKeys = array_intersect($arrayKeys, $this->validRootKeys);
		if (!empty($additionalKeys)) {
			throw new \UnexpectedValueException('Found invalid entries in root: ' . implode(', ', $additionalKeys));
		}
		if (empty($commonKeys)) {
			throw new \UnexpectedValueException('At least one key of the following is expected: ' . implode(', ', $this->validRootKeys));
		}

		if (isset($array['system'])) {
			if (is_array($array['system'])) {
				foreach ($array['system'] as $name => $value) {
					$this->checkTypeRecursively($value, $name);
				}
			} else {
				throw new \UnexpectedValueException('The system config array is not an array');
			}
		}

		if (isset($array['apps'])) {
			if (is_array($array['apps'])) {
				$this->validateAppsArray($array['apps']);
			} else {
				throw new \UnexpectedValueException('The apps config array is not an array');
			}
		}
	}

	/**
	 * @param mixed $configValue
	 * @param string $configName
	 */
	protected function checkTypeRecursively($configValue, $configName) {
		if (!is_array($configValue) && !is_bool($configValue) && !is_int($configValue) && !is_string($configValue) && !is_null($configValue)) {
			throw new \UnexpectedValueException('Invalid system config value for "' . $configName . '". Only arrays, bools, integers, strings and null (delete) are allowed.');
		}
		if (is_array($configValue)) {
			foreach ($configValue as $key => $value) {
				$this->checkTypeRecursively($value, $configName);
			}
		}
	}

	/**
	 * Validates that app configs are only integers and strings
	 *
	 * @param array $array
	 */
	protected function validateAppsArray($array) {
		foreach ($array as $app => $configs) {
			foreach ($configs as $name => $value) {
				if (!is_int($value) && !is_string($value) && !is_null($value)) {
					throw new \UnexpectedValueException('Invalid app config value for "' . $app . '":"' . $name . '". Only integers, strings and null (delete) are allowed.');
				}
			}
		}
	}
}