Merge pull request #323 from Faldon/database_port

Added occ install option for database-port
This commit is contained in:
Morris Jobke 2016-07-07 10:20:08 +02:00 committed by GitHub
commit ed28885d73
5 changed files with 26 additions and 7 deletions

View File

@ -50,6 +50,7 @@ class Install extends Command {
->addOption('database', null, InputOption::VALUE_REQUIRED, 'Supported database type', 'sqlite') ->addOption('database', null, InputOption::VALUE_REQUIRED, 'Supported database type', 'sqlite')
->addOption('database-name', null, InputOption::VALUE_REQUIRED, 'Name of the database') ->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-host', null, InputOption::VALUE_REQUIRED, 'Hostname of the database', 'localhost')
->addOption('database-port', null, InputOption::VALUE_REQUIRED, 'Port the database is listening on')
->addOption('database-user', null, InputOption::VALUE_REQUIRED, 'User name to connect to the database') ->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-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) ->addOption('database-table-prefix', null, InputOption::VALUE_OPTIONAL, 'Prefix for all tables (default: oc_)', null)
@ -106,6 +107,7 @@ class Install extends Command {
$dbUser = $input->getOption('database-user'); $dbUser = $input->getOption('database-user');
$dbPass = $input->getOption('database-pass'); $dbPass = $input->getOption('database-pass');
$dbName = $input->getOption('database-name'); $dbName = $input->getOption('database-name');
$dbPort = $input->getOption('database-port');
if ($db === 'oci') { if ($db === 'oci') {
// an empty hostname needs to be read from the raw parameters // an empty hostname needs to be read from the raw parameters
$dbHost = $input->getParameterOption('--database-host', ''); $dbHost = $input->getParameterOption('--database-host', '');
@ -158,6 +160,7 @@ class Install extends Command {
'dbpass' => $dbPass, 'dbpass' => $dbPass,
'dbname' => $dbName, 'dbname' => $dbName,
'dbhost' => $dbHost, 'dbhost' => $dbHost,
'dbport' => $dbPort,
'dbtableprefix' => $dbTablePrefix, 'dbtableprefix' => $dbTablePrefix,
'adminlogin' => $adminLogin, 'adminlogin' => $adminLogin,
'adminpass' => $adminPassword, 'adminpass' => $adminPassword,

View File

@ -42,6 +42,8 @@ abstract class AbstractDatabase {
/** @var string */ /** @var string */
protected $dbHost; protected $dbHost;
/** @var string */ /** @var string */
protected $dbPort;
/** @var string */
protected $tablePrefix; protected $tablePrefix;
/** @var IConfig */ /** @var IConfig */
protected $config; protected $config;
@ -78,11 +80,13 @@ abstract class AbstractDatabase {
$dbPass = $config['dbpass']; $dbPass = $config['dbpass'];
$dbName = $config['dbname']; $dbName = $config['dbname'];
$dbHost = !empty($config['dbhost']) ? $config['dbhost'] : 'localhost'; $dbHost = !empty($config['dbhost']) ? $config['dbhost'] : 'localhost';
$dbPort = !empty($config['dbport']) ? $config['dbport'] : '';
$dbTablePrefix = isset($config['dbtableprefix']) ? $config['dbtableprefix'] : 'oc_'; $dbTablePrefix = isset($config['dbtableprefix']) ? $config['dbtableprefix'] : 'oc_';
$this->config->setSystemValues([ $this->config->setSystemValues([
'dbname' => $dbName, 'dbname' => $dbName,
'dbhost' => $dbHost, 'dbhost' => $dbHost,
'dbport' => $dbPort,
'dbtableprefix' => $dbTablePrefix, 'dbtableprefix' => $dbTablePrefix,
]); ]);
@ -90,6 +94,7 @@ abstract class AbstractDatabase {
$this->dbPassword = $dbPass; $this->dbPassword = $dbPass;
$this->dbName = $dbName; $this->dbName = $dbName;
$this->dbHost = $dbHost; $this->dbHost = $dbHost;
$this->dbPort = $dbPort;
$this->tablePrefix = $dbTablePrefix; $this->tablePrefix = $dbTablePrefix;
} }

View File

@ -100,8 +100,14 @@ class MySQL extends AbstractDatabase {
'tablePrefix' => $this->tablePrefix, 'tablePrefix' => $this->tablePrefix,
); );
// adding port support // adding port support through installer
if (strpos($this->dbHost, ':')) { 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. // Host variable may carry a port or socket.
list($host, $portOrSocket) = explode(':', $this->dbHost, 2); list($host, $portOrSocket) = explode(':', $this->dbHost, 2);
if (ctype_digit($portOrSocket)) { if (ctype_digit($portOrSocket)) {

View File

@ -63,12 +63,14 @@ class OCI extends AbstractDatabase {
public function setupDatabase($username) { public function setupDatabase($username) {
$e_host = addslashes($this->dbHost); $e_host = addslashes($this->dbHost);
// casting to int to avoid malicious input
$e_port = (int)$this->dbPort;
$e_dbname = addslashes($this->dbName); $e_dbname = addslashes($this->dbName);
//check if the database user has admin right //check if the database user has admin right
if ($e_host == '') { if ($e_host == '') {
$easy_connect_string = $e_dbname; // use dbname as easy connect name $easy_connect_string = $e_dbname; // use dbname as easy connect name
} else { } 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']); $this->logger->debug('connect string: ' . $easy_connect_string, ['app' => 'setup.oci']);
$connection = @oci_connect($this->dbUser, $this->dbPassword, $easy_connect_string); $connection = @oci_connect($this->dbUser, $this->dbPassword, $easy_connect_string);

View File

@ -34,8 +34,11 @@ class PostgreSQL extends AbstractDatabase {
$e_user = addslashes($this->dbUser); $e_user = addslashes($this->dbUser);
$e_password = addslashes($this->dbPassword); $e_password = addslashes($this->dbPassword);
// Fix database with port connection // adding port support through installer
if(strpos($e_host, ':')) { if(!empty($this->dbPort)) {
// casting to int to avoid malicious input
$port = (int)$this->dbPort;
} else if(strpos($e_host, ':')) {
list($e_host, $port)=explode(':', $e_host, 2); list($e_host, $port)=explode(':', $e_host, 2);
} else { } else {
$port=false; $port=false;
@ -51,8 +54,8 @@ class PostgreSQL extends AbstractDatabase {
$connection = @pg_connect($connection_string); $connection = @pg_connect($connection_string);
if(!$connection) if(!$connection)
throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'), throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL connection failed'),
$this->trans->t('You need to enter either an existing account or the administrator.')); $this->trans->t('Please check your connection details.'));
} }
$e_user = pg_escape_string($this->dbUser); $e_user = pg_escape_string($this->dbUser);
//check for roles creation rights in postgresql //check for roles creation rights in postgresql