2013-03-29 19:28:48 +04:00
|
|
|
<?php
|
2015-03-26 13:44:34 +03:00
|
|
|
/**
|
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author eduardo <eduardo@vnexu.net>
|
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
2015-10-05 21:54:56 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, 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/>
|
|
|
|
*
|
|
|
|
*/
|
2013-03-29 19:28:48 +04:00
|
|
|
namespace OC\Setup;
|
|
|
|
|
2013-04-03 00:01:23 +04:00
|
|
|
class PostgreSQL extends AbstractDatabase {
|
2013-04-03 19:52:18 +04:00
|
|
|
public $dbprettyname = 'PostgreSQL';
|
|
|
|
|
2013-04-03 00:01:23 +04:00
|
|
|
public function setupDatabase($username) {
|
2015-07-29 19:09:23 +03:00
|
|
|
$e_host = addslashes($this->dbHost);
|
|
|
|
$e_user = addslashes($this->dbUser);
|
|
|
|
$e_password = addslashes($this->dbPassword);
|
2013-03-29 19:28:48 +04:00
|
|
|
|
2014-01-05 18:38:54 +04:00
|
|
|
// Fix database with port connection
|
2014-01-05 01:23:25 +04:00
|
|
|
if(strpos($e_host, ':')) {
|
|
|
|
list($e_host, $port)=explode(':', $e_host, 2);
|
|
|
|
} else {
|
|
|
|
$port=false;
|
|
|
|
}
|
|
|
|
|
2013-03-29 19:28:48 +04:00
|
|
|
//check if the database user has admin rights
|
2014-01-05 01:23:25 +04:00
|
|
|
$connection_string = "host='$e_host' dbname=postgres user='$e_user' port='$port' password='$e_password'";
|
2013-03-29 19:28:48 +04:00
|
|
|
$connection = @pg_connect($connection_string);
|
|
|
|
if(!$connection) {
|
|
|
|
// Try if we can connect to the DB with the specified name
|
2015-07-29 19:09:23 +03:00
|
|
|
$e_dbname = addslashes($this->dbName);
|
2014-01-08 06:16:37 +04:00
|
|
|
$connection_string = "host='$e_host' dbname='$e_dbname' user='$e_user' port='$port' password='$e_password'";
|
2013-03-29 19:28:48 +04:00
|
|
|
$connection = @pg_connect($connection_string);
|
|
|
|
|
|
|
|
if(!$connection)
|
2014-11-26 14:30:07 +03:00
|
|
|
throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'),
|
2013-04-03 00:01:23 +04:00
|
|
|
$this->trans->t('You need to enter either an existing account or the administrator.'));
|
2013-03-29 19:28:48 +04:00
|
|
|
}
|
2015-07-29 19:09:23 +03:00
|
|
|
$e_user = pg_escape_string($this->dbUser);
|
2013-03-29 19:28:48 +04:00
|
|
|
//check for roles creation rights in postgresql
|
|
|
|
$query="SELECT 1 FROM pg_roles WHERE rolcreaterole=TRUE AND rolname='$e_user'";
|
|
|
|
$result = pg_query($connection, $query);
|
|
|
|
if($result and pg_num_rows($result) > 0) {
|
|
|
|
//use the admin login data for the new database user
|
|
|
|
|
|
|
|
//add prefix to the postgresql user name to prevent collisions
|
2015-07-29 19:09:23 +03:00
|
|
|
$this->dbUser='oc_'.$username;
|
2013-03-29 19:28:48 +04:00
|
|
|
//create a new password so we don't need to store the admin config in the config file
|
2016-01-11 00:07:33 +03:00
|
|
|
$this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS);
|
2013-03-29 19:28:48 +04:00
|
|
|
|
2013-04-03 00:01:23 +04:00
|
|
|
$this->createDBUser($connection);
|
2013-03-29 19:28:48 +04:00
|
|
|
}
|
|
|
|
|
2015-12-03 16:35:15 +03:00
|
|
|
$systemConfig = \OC::$server->getSystemConfig();
|
|
|
|
$systemConfig->setValues([
|
2015-07-29 19:09:23 +03:00
|
|
|
'dbuser' => $this->dbUser,
|
|
|
|
'dbpassword' => $this->dbPassword,
|
2015-01-23 13:13:47 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
//create the database
|
|
|
|
$this->createDatabase($connection);
|
2013-03-29 19:28:48 +04:00
|
|
|
|
|
|
|
// the connection to dbname=postgres is not needed anymore
|
|
|
|
pg_close($connection);
|
|
|
|
|
2013-04-03 00:01:23 +04:00
|
|
|
// connect to the ownCloud database (dbname=$this->dbname) and check if it needs to be filled
|
2015-12-03 16:35:15 +03:00
|
|
|
$this->dbUser = $systemConfig->getValue('dbuser');
|
|
|
|
$this->dbPassword = $systemConfig->getValue('dbpassword');
|
2013-03-29 19:28:48 +04:00
|
|
|
|
2015-07-29 19:09:23 +03:00
|
|
|
$e_host = addslashes($this->dbHost);
|
|
|
|
$e_dbname = addslashes($this->dbName);
|
|
|
|
$e_user = addslashes($this->dbUser);
|
|
|
|
$e_password = addslashes($this->dbPassword);
|
2013-03-29 19:28:48 +04:00
|
|
|
|
2014-01-05 18:38:54 +04:00
|
|
|
// Fix database with port connection
|
2014-01-05 01:23:25 +04:00
|
|
|
if(strpos($e_host, ':')) {
|
|
|
|
list($e_host, $port)=explode(':', $e_host, 2);
|
|
|
|
} else {
|
|
|
|
$port=false;
|
|
|
|
}
|
|
|
|
|
2014-01-08 06:16:37 +04:00
|
|
|
$connection_string = "host='$e_host' dbname='$e_dbname' user='$e_user' port='$port' password='$e_password'";
|
2013-03-29 19:28:48 +04:00
|
|
|
$connection = @pg_connect($connection_string);
|
|
|
|
if(!$connection) {
|
2014-11-26 14:30:07 +03:00
|
|
|
throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'),
|
2013-04-03 00:01:23 +04:00
|
|
|
$this->trans->t('You need to enter either an existing account or the administrator.'));
|
2013-03-29 19:28:48 +04:00
|
|
|
}
|
2015-07-29 19:09:23 +03:00
|
|
|
$query = "select count(*) FROM pg_class WHERE relname='".$this->tablePrefix."users' limit 1";
|
2013-03-29 19:28:48 +04:00
|
|
|
$result = pg_query($connection, $query);
|
|
|
|
if($result) {
|
|
|
|
$row = pg_fetch_row($result);
|
|
|
|
}
|
|
|
|
if(!$result or $row[0]==0) {
|
2013-06-27 22:19:51 +04:00
|
|
|
\OC_DB::createDbFromStructure($this->dbDefinitionFile);
|
2013-03-29 19:28:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-03 00:01:23 +04:00
|
|
|
private function createDatabase($connection) {
|
2013-03-29 19:28:48 +04:00
|
|
|
//we cant use OC_BD functions here because we need to connect as the administrative user.
|
2015-07-29 19:09:23 +03:00
|
|
|
$e_name = pg_escape_string($this->dbName);
|
|
|
|
$e_user = pg_escape_string($this->dbUser);
|
2013-03-29 19:28:48 +04:00
|
|
|
$query = "select datname from pg_database where datname = '$e_name'";
|
|
|
|
$result = pg_query($connection, $query);
|
|
|
|
if(!$result) {
|
2013-04-03 00:01:23 +04:00
|
|
|
$entry = $this->trans->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
|
|
|
|
$entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
|
2015-07-03 15:06:40 +03:00
|
|
|
\OCP\Util::writeLog('setup.pg', $entry, \OCP\Util::WARN);
|
2013-03-29 19:28:48 +04:00
|
|
|
}
|
|
|
|
if(! pg_fetch_row($result)) {
|
|
|
|
//The database does not exists... let's create it
|
|
|
|
$query = "CREATE DATABASE \"$e_name\" OWNER \"$e_user\"";
|
|
|
|
$result = pg_query($connection, $query);
|
|
|
|
if(!$result) {
|
2013-04-03 00:01:23 +04:00
|
|
|
$entry = $this->trans->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
|
|
|
|
$entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
|
2015-07-03 15:06:40 +03:00
|
|
|
\OCP\Util::writeLog('setup.pg', $entry, \OCP\Util::WARN);
|
2013-03-29 19:28:48 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$query = "REVOKE ALL PRIVILEGES ON DATABASE \"$e_name\" FROM PUBLIC";
|
|
|
|
pg_query($connection, $query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-03 00:01:23 +04:00
|
|
|
private function createDBUser($connection) {
|
2015-07-29 19:09:23 +03:00
|
|
|
$e_name = pg_escape_string($this->dbUser);
|
|
|
|
$e_password = pg_escape_string($this->dbPassword);
|
2013-03-29 19:28:48 +04:00
|
|
|
$query = "select * from pg_roles where rolname='$e_name';";
|
|
|
|
$result = pg_query($connection, $query);
|
|
|
|
if(!$result) {
|
2013-04-03 00:01:23 +04:00
|
|
|
$entry = $this->trans->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
|
|
|
|
$entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
|
2015-07-03 15:06:40 +03:00
|
|
|
\OCP\Util::writeLog('setup.pg', $entry, \OCP\Util::WARN);
|
2013-03-29 19:28:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(! pg_fetch_row($result)) {
|
|
|
|
//user does not exists let's create it :)
|
|
|
|
$query = "CREATE USER \"$e_name\" CREATEDB PASSWORD '$e_password';";
|
|
|
|
$result = pg_query($connection, $query);
|
|
|
|
if(!$result) {
|
2013-04-03 00:01:23 +04:00
|
|
|
$entry = $this->trans->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
|
|
|
|
$entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
|
2015-07-03 15:06:40 +03:00
|
|
|
\OCP\Util::writeLog('setup.pg', $entry, \OCP\Util::WARN);
|
2013-03-29 19:28:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else { // change password of the existing role
|
|
|
|
$query = "ALTER ROLE \"$e_name\" WITH PASSWORD '$e_password';";
|
|
|
|
$result = pg_query($connection, $query);
|
|
|
|
if(!$result) {
|
2013-04-03 00:01:23 +04:00
|
|
|
$entry = $this->trans->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
|
|
|
|
$entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
|
2015-07-03 15:06:40 +03:00
|
|
|
\OCP\Util::writeLog('setup.pg', $entry, \OCP\Util::WARN);
|
2013-03-29 19:28:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|