Passed
Push — master ( 730af0...3b14ce )
by Roeland
12:25 queued 11s
created

Install   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
eloc 104
dl 0
loc 167
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 36 5
A __construct() 0 3 1
A configure() 0 16 1
A printErrors() 0 7 3
F validateInput() 0 80 14
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bernhard Posselt <[email protected]>
6
 * @author Christian Kampka <[email protected]>
7
 * @author Daniel Hansson <[email protected]>
8
 * @author Daniel Kesselberg <[email protected]>
9
 * @author Joas Schilling <[email protected]>
10
 * @author Jörn Friedrich Dreyer <[email protected]>
11
 * @author Morris Jobke <[email protected]>
12
 * @author Roeland Jago Douma <[email protected]>
13
 * @author Thomas Müller <[email protected]>
14
 * @author Thomas Pulzer <[email protected]>
15
 *
16
 * @license AGPL-3.0
17
 *
18
 * This code is free software: you can redistribute it and/or modify
19
 * it under the terms of the GNU Affero General Public License, version 3,
20
 * as published by the Free Software Foundation.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
 * GNU Affero General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU Affero General Public License, version 3,
28
 * along with this program. If not, see <http://www.gnu.org/licenses/>
29
 *
30
 */
31
32
namespace OC\Core\Command\Maintenance;
33
34
use InvalidArgumentException;
35
use OC\Installer;
36
use OC\Setup;
37
use OC\SystemConfig;
38
use OCP\Defaults;
39
use Symfony\Component\Console\Command\Command;
40
use Symfony\Component\Console\Helper\QuestionHelper;
41
use Symfony\Component\Console\Input\InputInterface;
42
use Symfony\Component\Console\Input\InputOption;
43
use Symfony\Component\Console\Output\OutputInterface;
44
use Symfony\Component\Console\Question\Question;
45
46
class Install extends Command {
47
48
	/**
49
	 * @var SystemConfig
50
	 */
51
	private $config;
52
53
	public function __construct(SystemConfig $config) {
54
		parent::__construct();
55
		$this->config = $config;
56
	}
57
58
	protected function configure() {
59
		$this
60
			->setName('maintenance:install')
61
			->setDescription('install Nextcloud')
62
			->addOption('database', null, InputOption::VALUE_REQUIRED, 'Supported database type', 'sqlite')
63
			->addOption('database-name', null, InputOption::VALUE_REQUIRED, 'Name of the database')
64
			->addOption('database-host', null, InputOption::VALUE_REQUIRED, 'Hostname of the database', 'localhost')
65
			->addOption('database-port', null, InputOption::VALUE_REQUIRED, 'Port the database is listening on')
66
			->addOption('database-user', null, InputOption::VALUE_REQUIRED, 'User name to connect to the database')
67
			->addOption('database-pass', null, InputOption::VALUE_OPTIONAL, 'Password of the database user', null)
68
			->addOption('database-table-prefix', null, InputOption::VALUE_OPTIONAL, 'Prefix for all tables (default: oc_)', null)
69
			->addOption('database-table-space', null, InputOption::VALUE_OPTIONAL, 'Table space of the database (oci only)', null)
70
			->addOption('admin-user', null, InputOption::VALUE_REQUIRED, 'User name of the admin account', 'admin')
71
			->addOption('admin-pass', null, InputOption::VALUE_REQUIRED, 'Password of the admin account')
72
			->addOption('admin-email', null, InputOption::VALUE_OPTIONAL, 'E-Mail of the admin account')
73
			->addOption('data-dir', null, InputOption::VALUE_REQUIRED, 'Path to data directory', \OC::$SERVERROOT."/data");
74
	}
75
76
	protected function execute(InputInterface $input, OutputInterface $output) {
77
78
		// validate the environment
79
		$server = \OC::$server;
80
		$setupHelper = new Setup(
81
			$this->config,
82
			$server->getIniWrapper(),
83
			$server->getL10N('lib'),
84
			$server->query(Defaults::class),
85
			$server->getLogger(),
86
			$server->getSecureRandom(),
87
			\OC::$server->query(Installer::class)
88
		);
89
		$sysInfo = $setupHelper->getSystemInfo(true);
90
		$errors = $sysInfo['errors'];
91
		if (count($errors) > 0) {
92
			$this->printErrors($output, $errors);
93
94
			// ignore the OS X setup warning
95
			if(count($errors) !== 1 ||
96
				(string)$errors[0]['error'] !== 'Mac OS X is not supported and Nextcloud will not work properly on this platform. Use it at your own risk! ') {
97
				return 1;
98
			}
99
		}
100
101
		// validate user input
102
		$options = $this->validateInput($input, $output, array_keys($sysInfo['databases']));
103
104
		// perform installation
105
		$errors = $setupHelper->install($options);
106
		if (count($errors) > 0) {
107
			$this->printErrors($output, $errors);
108
			return 1;
109
		}
110
		$output->writeln("Nextcloud was successfully installed");
111
		return 0;
112
	}
113
114
	/**
115
	 * @param InputInterface $input
116
	 * @param OutputInterface $output
117
	 * @param string[] $supportedDatabases
118
	 * @return array
119
	 */
120
	protected function validateInput(InputInterface $input, OutputInterface $output, $supportedDatabases) {
121
		$db = strtolower($input->getOption('database'));
122
123
		if (!in_array($db, $supportedDatabases)) {
124
			throw new InvalidArgumentException("Database <$db> is not supported.");
125
		}
126
127
		$dbUser = $input->getOption('database-user');
128
		$dbPass = $input->getOption('database-pass');
129
		$dbName = $input->getOption('database-name');
130
		$dbPort = $input->getOption('database-port');
131
		if ($db === 'oci') {
132
			// an empty hostname needs to be read from the raw parameters
133
			$dbHost = $input->getParameterOption('--database-host', '');
134
		} else {
135
			$dbHost = $input->getOption('database-host');
136
		}
137
		if ($dbPort) {
138
			// Append the port to the host so it is the same as in the config (there is no dbport config)
139
			$dbHost .= ':' . $dbPort;
0 ignored issues
show
Bug introduced by
Are you sure $dbPort of type string|string[]|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

139
			$dbHost .= ':' . /** @scrutinizer ignore-type */ $dbPort;
Loading history...
140
		}
141
		$dbTablePrefix = 'oc_';
142
		if ($input->hasParameterOption('--database-table-prefix')) {
143
			$dbTablePrefix = (string) $input->getOption('database-table-prefix');
144
			$dbTablePrefix = trim($dbTablePrefix);
145
		}
146
		if ($input->hasParameterOption('--database-pass')) {
147
			$dbPass = (string) $input->getOption('database-pass');
148
		}
149
		$adminLogin = $input->getOption('admin-user');
150
		$adminPassword = $input->getOption('admin-pass');
151
		$adminEmail = $input->getOption('admin-email');
152
		$dataDir = $input->getOption('data-dir');
153
154
		if ($db !== 'sqlite') {
155
			if (is_null($dbUser)) {
156
				throw new InvalidArgumentException("Database user not provided.");
157
			}
158
			if (is_null($dbName)) {
159
				throw new InvalidArgumentException("Database name not provided.");
160
			}
161
			if (is_null($dbPass)) {
162
				/** @var QuestionHelper $helper */
163
				$helper = $this->getHelper('question');
164
				$question = new Question('What is the password to access the database with user <'.$dbUser.'>?');
0 ignored issues
show
Bug introduced by
Are you sure $dbUser of type boolean|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

164
				$question = new Question('What is the password to access the database with user <'./** @scrutinizer ignore-type */ $dbUser.'>?');
Loading history...
165
				$question->setHidden(true);
166
				$question->setHiddenFallback(false);
167
				$dbPass = $helper->ask($input, $output, $question);
168
			}
169
		}
170
171
		if (is_null($adminPassword)) {
172
			/** @var QuestionHelper $helper */
173
			$helper = $this->getHelper('question');
174
			$question = new Question('What is the password you like to use for the admin account <'.$adminLogin.'>?');
175
			$question->setHidden(true);
176
			$question->setHiddenFallback(false);
177
			$adminPassword = $helper->ask($input, $output, $question);
178
		}
179
180
		if ($adminEmail !== null && !filter_var($adminEmail, FILTER_VALIDATE_EMAIL)) {
181
			throw new InvalidArgumentException('Invalid e-mail-address <' . $adminEmail . '> for <' . $adminLogin . '>.');
0 ignored issues
show
Bug introduced by
Are you sure $adminEmail of type boolean|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

181
			throw new InvalidArgumentException('Invalid e-mail-address <' . /** @scrutinizer ignore-type */ $adminEmail . '> for <' . $adminLogin . '>.');
Loading history...
182
		}
183
184
		$options = [
185
			'dbtype' => $db,
186
			'dbuser' => $dbUser,
187
			'dbpass' => $dbPass,
188
			'dbname' => $dbName,
189
			'dbhost' => $dbHost,
190
			'dbtableprefix' => $dbTablePrefix,
191
			'adminlogin' => $adminLogin,
192
			'adminpass' => $adminPassword,
193
			'adminemail' => $adminEmail,
194
			'directory' => $dataDir
195
		];
196
		if ($db === 'oci') {
197
			$options['dbtablespace'] = $input->getParameterOption('--database-table-space', '');
198
		}
199
		return $options;
200
	}
201
202
	/**
203
	 * @param OutputInterface $output
204
	 * @param $errors
205
	 */
206
	protected function printErrors(OutputInterface $output, $errors) {
207
		foreach ($errors as $error) {
208
			if (is_array($error)) {
209
				$output->writeln('<error>' . (string)$error['error'] . '</error>');
210
				$output->writeln('<info> -> ' . (string)$error['hint'] . '</info>');
211
			} else {
212
				$output->writeln('<error>' . (string)$error . '</error>');
213
			}
214
		}
215
	}
216
}
217