Skip to content

Commit

Permalink
Added occ install option for database-port.
Browse files Browse the repository at this point in the history
Extended the database setup to store the database port.
Changed the PostgreSQL connection error message for clarification.
  • Loading branch information
Thomas Pulzer committed Jul 6, 2016
1 parent cf7afab commit d367318
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
3 changes: 3 additions & 0 deletions core/Command/Maintenance/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ protected function configure() {
->addOption('database', null, InputOption::VALUE_REQUIRED, 'Supported database type', 'sqlite')
->addOption('database-name', null, InputOption::VALUE_REQUIRED, 'Name of the database')
->addOption('database-host', null, InputOption::VALUE_REQUIRED, 'Hostname of the database', 'localhost')
->addOption('database-port', null, InputOption::VALUE_OPTIONAL, 'Port the database is listening on')
->addOption('database-user', null, InputOption::VALUE_REQUIRED, 'User name to connect to the database')
->addOption('database-pass', null, InputOption::VALUE_OPTIONAL, 'Password of the database user', null)
->addOption('database-table-prefix', null, InputOption::VALUE_OPTIONAL, 'Prefix for all tables (default: oc_)', null)
Expand Down Expand Up @@ -106,6 +107,7 @@ protected function validateInput(InputInterface $input, OutputInterface $output,
$dbUser = $input->getOption('database-user');
$dbPass = $input->getOption('database-pass');
$dbName = $input->getOption('database-name');
$dbPort = $input->getOption('database-port');
if ($db === 'oci') {
// an empty hostname needs to be read from the raw parameters
$dbHost = $input->getParameterOption('--database-host', '');
Expand Down Expand Up @@ -158,6 +160,7 @@ protected function validateInput(InputInterface $input, OutputInterface $output,
'dbpass' => $dbPass,
'dbname' => $dbName,
'dbhost' => $dbHost,
'dbport' => $dbPort,
'dbtableprefix' => $dbTablePrefix,
'adminlogin' => $adminLogin,
'adminpass' => $adminPassword,
Expand Down
5 changes: 5 additions & 0 deletions lib/private/Setup/AbstractDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ abstract class AbstractDatabase {
/** @var string */
protected $dbHost;
/** @var string */
protected $dbPort;
/** @var string */
protected $tablePrefix;
/** @var IConfig */
protected $config;
Expand Down Expand Up @@ -78,18 +80,21 @@ public function initialize($config) {
$dbPass = $config['dbpass'];
$dbName = $config['dbname'];
$dbHost = !empty($config['dbhost']) ? $config['dbhost'] : 'localhost';
$dbPort = !empty($config['dbport']) ? $config['dbport'] : '';
$dbTablePrefix = isset($config['dbtableprefix']) ? $config['dbtableprefix'] : 'oc_';

$this->config->setSystemValues([
'dbname' => $dbName,
'dbhost' => $dbHost,
'dbport' => $dbPort,
'dbtableprefix' => $dbTablePrefix,
]);

$this->dbUser = $dbUser;
$this->dbPassword = $dbPass;
$this->dbName = $dbName;
$this->dbHost = $dbHost;
$this->dbPort = $dbPort;
$this->tablePrefix = $dbTablePrefix;
}

Expand Down
10 changes: 8 additions & 2 deletions lib/private/Setup/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,14 @@ private function connect() {
'tablePrefix' => $this->tablePrefix,
);

// adding port support
if (strpos($this->dbHost, ':')) {
// adding port support through installer
if(!empty($this->dbPort)) {
if (ctype_digit($this->dbPort)) {
$connectionParams['port'] = $this->dbPort;
} else {
$connectionParams['unix_socket'] = $this->dbPort;
}
} else if (strpos($this->dbHost, ':')) {
// Host variable may carry a port or socket.
list($host, $portOrSocket) = explode(':', $this->dbHost, 2);
if (ctype_digit($portOrSocket)) {
Expand Down
4 changes: 3 additions & 1 deletion lib/private/Setup/OCI.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ public function validate($config) {

public function setupDatabase($username) {
$e_host = addslashes($this->dbHost);
// adding slashes for security reasons
$e_port = addslashes($this->dbPort);
$e_dbname = addslashes($this->dbName);
//check if the database user has admin right
if ($e_host == '') {
$easy_connect_string = $e_dbname; // use dbname as easy connect name
} else {
$easy_connect_string = '//'.$e_host.'/'.$e_dbname;
$easy_connect_string = '//'.$e_host.(!empty($e_port) ? ":{$e_port}" : "").'/'.$e_dbname;
}
$this->logger->debug('connect string: ' . $easy_connect_string, ['app' => 'setup.oci']);
$connection = @oci_connect($this->dbUser, $this->dbPassword, $easy_connect_string);
Expand Down
11 changes: 7 additions & 4 deletions lib/private/Setup/PostgreSQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ public function setupDatabase($username) {
$e_user = addslashes($this->dbUser);
$e_password = addslashes($this->dbPassword);

// Fix database with port connection
if(strpos($e_host, ':')) {
// adding port support through installer
if(!empty($this->dbPort)) {
// adding slashes for security reasons
$port = addslashes($this->dbPort);
} else if(strpos($e_host, ':')) {
list($e_host, $port)=explode(':', $e_host, 2);
} else {
$port=false;
Expand All @@ -51,8 +54,8 @@ public function setupDatabase($username) {
$connection = @pg_connect($connection_string);

if(!$connection)
throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'),
$this->trans->t('You need to enter either an existing account or the administrator.'));
throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL connection failed'),
$this->trans->t('Please check your connection details.'));
}
$e_user = pg_escape_string($this->dbUser);
//check for roles creation rights in postgresql
Expand Down

0 comments on commit d367318

Please sign in to comment.