Skip to content

Commit

Permalink
Add overwrite.cli.url to occ maintenance:install
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepDiver1975 committed Mar 19, 2018
1 parent f8ad334 commit 2c07097
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 44 deletions.
30 changes: 20 additions & 10 deletions core/Command/Maintenance/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ protected function configure() {
->addOption('database-table-prefix', null, InputOption::VALUE_OPTIONAL, 'Prefix for all tables (default: oc_).', null)
->addOption('admin-user', null, InputOption::VALUE_REQUIRED, 'User name of the admin account.', 'admin')
->addOption('admin-pass', null, InputOption::VALUE_REQUIRED, 'Password of the admin account.')
->addOption('data-dir', null, InputOption::VALUE_REQUIRED, 'Path to the data directory.', \OC::$SERVERROOT."/data");
->addOption('data-dir', null, InputOption::VALUE_REQUIRED, 'Path to the data directory.', \OC::$SERVERROOT. '/data')
->addOption('overwrite.cli.url', null, InputOption::VALUE_REQUIRED, 'URL the ownCloud instance is primarily accessible.');
}

protected function execute(InputInterface $input, OutputInterface $output) {
Expand All @@ -74,7 +75,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {

// ignore the OS X setup warning
if(count($errors) !== 1 ||
(string)($errors[0]['error']) !== 'Mac OS X is not supported and ownCloud will not work properly on this platform. Use it at your own risk! ') {
(string)$errors[0]['error'] !== 'Mac OS X is not supported and ownCloud will not work properly on this platform. Use it at your own risk! ') {
return 1;
}
}
Expand All @@ -88,7 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$this->printErrors($output, $errors);
return 1;
}
$output->writeln("ownCloud was successfully installed");
$output->writeln('ownCloud was successfully installed');
return 0;
}

Expand Down Expand Up @@ -125,15 +126,16 @@ protected function validateInput(InputInterface $input, OutputInterface $output,
$adminLogin = $input->getOption('admin-user');
$adminPassword = $input->getOption('admin-pass');
$dataDir = $input->getOption('data-dir');
$overwriteCliUrl = $input->getOption('overwrite.cli.url');

if ($db !== 'sqlite') {
if (is_null($dbUser)) {
throw new InvalidArgumentException("Database user not provided.");
if ($dbUser === null) {
throw new InvalidArgumentException('Database user not provided.');
}
if (is_null($dbName)) {
throw new InvalidArgumentException("Database name not provided.");
if ($dbName === null) {
throw new InvalidArgumentException('Database name not provided.');
}
if (is_null($dbPass)) {
if ($dbPass === null) {
/** @var $dialog \Symfony\Component\Console\Helper\QuestionHelper */
$dialog = $this->getHelperSet()->get('question');
$q = new Question("<question>What is the password to access the database with user <$dbUser>?</question>", false);
Expand All @@ -142,14 +144,21 @@ protected function validateInput(InputInterface $input, OutputInterface $output,
}
}

if (is_null($adminPassword)) {
if ($adminPassword === null) {
/** @var $dialog \Symfony\Component\Console\Helper\QuestionHelper */
$dialog = $this->getHelperSet()->get('question');
$q = new Question("<question>What is the password you like to use for the admin account <$adminLogin>?</question>", false);
$q->setHidden(true);
$adminPassword = $dialog->ask($input, $output, $q);
}

if ($overwriteCliUrl === null) {
/** @var $dialog \Symfony\Component\Console\Helper\QuestionHelper */
$dialog = $this->getHelperSet()->get('question');
$q = new Question('<question>What is the public facing URL of this ownCloud instance?</question>', false);
$overwriteCliUrl = $dialog->ask($input, $output, $q);
}

$options = [
'dbtype' => $db,
'dbuser' => $dbUser,
Expand All @@ -159,7 +168,8 @@ protected function validateInput(InputInterface $input, OutputInterface $output,
'dbtableprefix' => $dbTablePrefix,
'adminlogin' => $adminLogin,
'adminpass' => $adminPassword,
'directory' => $dataDir
'directory' => $dataDir,
'overwrite.cli.url' => $overwriteCliUrl
];
return $options;
}
Expand Down
84 changes: 50 additions & 34 deletions lib/private/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@

use bantu\IniGetWrapper\IniGetWrapper;
use Exception;
use OC\DatabaseSetupException;
use OC\User\Database;
use OCP\IConfig;
use OCP\IL10N;
use OCP\ILogger;
use OCP\Security\ISecureRandom;
use OCP\Util;

class Setup {
/** @var \OCP\IConfig */
Expand All @@ -62,16 +65,17 @@ class Setup {
/**
* @param IConfig $config
* @param IniGetWrapper $iniWrapper
* @param IL10N $l10n
* @param \OC_Defaults $defaults
* @param ILogger $logger
* @param ISecureRandom $random
*/
function __construct(IConfig $config,
IniGetWrapper $iniWrapper,
IL10N $l10n,
\OC_Defaults $defaults,
ILogger $logger,
ISecureRandom $random
public function __construct(IConfig $config,
IniGetWrapper $iniWrapper,
IL10N $l10n,
\OC_Defaults $defaults,
ILogger $logger,
ISecureRandom $random
) {
$this->config = $config;
$this->iniWrapper = $iniWrapper;
Expand All @@ -81,12 +85,12 @@ function __construct(IConfig $config,
$this->random = $random;
}

static $dbSetupClasses = [
'mysql' => \OC\Setup\MySQL::class,
'pgsql' => \OC\Setup\PostgreSQL::class,
'oci' => \OC\Setup\OCI::class,
'sqlite' => \OC\Setup\Sqlite::class,
'sqlite3' => \OC\Setup\Sqlite::class,
static private $dbSetupClasses = [
'mysql' => Setup\MySQL::class,
'pgsql' => Setup\PostgreSQL::class,
'oci' => Setup\OCI::class,
'sqlite' => Setup\Sqlite::class,
'sqlite3' => Setup\Sqlite::class,
];

/**
Expand Down Expand Up @@ -184,8 +188,10 @@ public function getSupportedDatabases($allowAllDatabases = false) {
* Gathers system information like database type and does
* a few system checks.
*
* @param bool $allowAllDatabases
* @return array of system info, including an "errors" value
* in case of errors/warnings
* @throws Exception
*/
public function getSystemInfo($allowAllDatabases = false) {
$databases = $this->getSupportedDatabases($allowAllDatabases);
Expand All @@ -202,7 +208,7 @@ public function getSystemInfo($allowAllDatabases = false) {
}
if (is_dir($dataDir) && is_writable($dataDir)) {
// Protect data directory here, so we can test if the protection is working
\OC\Setup::protectDataDirectory();
self::protectDataDirectory();
}

if (!\OC_Util::runningOn('linux')) {
Expand Down Expand Up @@ -260,8 +266,11 @@ public function install($options) {
if(empty($options['adminpass'])) {
$error[] = $l->t('Set an admin password.');
}
if (empty($options['overwrite.cli.url'])) {
$error[] = $l->t('Set public facing ownCloud URL.');
}
if(empty($options['directory'])) {
$options['directory'] = \OC::$SERVERROOT."/data";
$options['directory'] = \OC::$SERVERROOT. '/data';
}

if (!isset(self::$dbSetupClasses[$dbType])) {
Expand All @@ -271,6 +280,7 @@ public function install($options) {
$username = htmlspecialchars_decode($options['adminlogin']);
$password = htmlspecialchars_decode($options['adminpass']);
$dataDir = htmlspecialchars_decode($options['directory']);
$overwriteCliUrl = htmlspecialchars_decode($options['overwrite.cli.url']);

$class = self::$dbSetupClasses[$dbType];
/** @var \OC\Setup\AbstractDatabase $dbSetup */
Expand All @@ -280,29 +290,35 @@ public function install($options) {

// validate the data directory
if (
(!is_dir($dataDir) and !mkdir($dataDir)) or
(!is_dir($dataDir) && !mkdir($dataDir)) ||
!is_writable($dataDir)
) {
$error[] = $l->t("Can't create or write into the data directory %s", [$dataDir]);
}

if(count($error) != 0) {
if(count($error) !== 0) {
return $error;
}

$request = \OC::$server->getRequest();

//no errors, good
if(isset($options['trusted_domains'])
&& is_array($options['trusted_domains'])) {
$trustedDomains = $options['trusted_domains'];
} else {
$trustedDomains = [$request->getInsecureServerHost()];
$parts = parse_url($overwriteCliUrl);
if ($parts === false) {
throw new \InvalidArgumentException('Invalid url "' . $overwriteCliUrl . '"');
}
if (!isset($parts['scheme']) || !isset($parts['host'])) {
throw new \InvalidArgumentException('Invalid url "' . $overwriteCliUrl . '"');
}
$host = strtolower($parts['host']);
$trustedDomains = [$host];
}

//use sqlite3 when available, otherwise sqlite2 will be used.
if($dbType=='sqlite' and $this->IsClassExisting('SQLite3')) {
$dbType='sqlite3';
if($dbType == 'sqlite' && $this->IsClassExisting('SQLite3')) {
$dbType = 'sqlite3';
}

//generate a random salt that is used to salt the local user passwords
Expand All @@ -316,34 +332,34 @@ public function install($options) {
'secret' => $secret,
'trusted_domains' => $trustedDomains,
'datadirectory' => $dataDir,
'overwrite.cli.url' => $request->getServerProtocol() . '://' . $request->getInsecureServerHost() . \OC::$WEBROOT,
'overwrite.cli.url' => $overwriteCliUrl,
'dbtype' => $dbType,
'version' => implode('.', \OCP\Util::getVersion()),
'version' => implode('.', Util::getVersion()),
]);

try {
$dbSetup->initialize($options);
$dbSetup->setupDatabase($username);
// apply necessary migrations
$dbSetup->runMigrations();
} catch (\OC\DatabaseSetupException $e) {
} catch (DatabaseSetupException $e) {
$error[] = [
'error' => $e->getMessage(),
'hint' => $e->getHint()
];
return($error);
return $error;
} catch (Exception $e) {
$error[] = [
'error' => 'Error while trying to create admin user: ' . $e->getMessage(),
'hint' => ''
];
return($error);
return $error;
}

//create the user and group
$user = null;
try {
\OC::$server->getUserManager()->registerBackend(new \OC\User\Database());
\OC::$server->getUserManager()->registerBackend(new Database());
$user = \OC::$server->getUserManager()->createUser($username, $password);
if (!$user) {
$error[] = "User <$username> could not be created.";
Expand All @@ -352,12 +368,12 @@ public function install($options) {
$error[] = $exception->getMessage();
}

if(count($error) == 0) {
if(count($error) === 0) {
$config = \OC::$server->getConfig();
$config->setAppValue('core', 'installedat', microtime(true));
$config->setAppValue('core', 'lastupdatedat', microtime(true));

\OC::$server->getGroupManager()->addBackend(new \OC\Group\Database());
\OC::$server->getGroupManager()->addBackend(new Group\Database());

$group =\OC::$server->getGroupManager()->createGroup('admin');
$group->addUser($user);
Expand All @@ -370,8 +386,8 @@ public function install($options) {
file_put_contents($config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data').'/.ocdata', '');

// Update .htaccess files
Setup::updateHtaccess();
Setup::protectDataDirectory();
self::updateHtaccess();
self::protectDataDirectory();

//try to write logtimezone
if (date_default_timezone_get()) {
Expand Down Expand Up @@ -416,7 +432,7 @@ public static function updateHtaccess() {
$webRoot = !empty(\OC::$WEBROOT) ? \OC::$WEBROOT : '/';
}

$setupHelper = new \OC\Setup($config, \OC::$server->getIniWrapper(),
$setupHelper = new Setup($config, \OC::$server->getIniWrapper(),
\OC::$server->getL10N('lib'), new \OC_Defaults(), \OC::$server->getLogger(),
\OC::$server->getSecureRandom());

Expand All @@ -425,10 +441,10 @@ public static function updateHtaccess() {
$htaccessContent = explode($content, $htaccessContent, 2)[0];

//custom 403 error page
$content.= "\nErrorDocument 403 ".$webRoot."/core/templates/403.php";
$content.= "\nErrorDocument 403 ".$webRoot. '/core/templates/403.php';

//custom 404 error page
$content.= "\nErrorDocument 404 ".$webRoot."/core/templates/404.php";
$content.= "\nErrorDocument 404 ".$webRoot. '/core/templates/404.php';

// Add rewrite rules if the RewriteBase is configured
$rewriteBase = $config->getSystemValue('htaccess.RewriteBase', '');
Expand Down

0 comments on commit 2c07097

Please sign in to comment.