2013-03-29 15:28:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace OC\Setup;
|
|
|
|
|
2013-04-02 20:01:23 +00:00
|
|
|
class PostgreSQL extends AbstractDatabase {
|
2013-04-03 15:52:18 +00:00
|
|
|
public $dbprettyname = 'PostgreSQL';
|
|
|
|
|
2013-04-02 20:01:23 +00:00
|
|
|
public function setupDatabase($username) {
|
|
|
|
$e_host = addslashes($this->dbhost);
|
|
|
|
$e_user = addslashes($this->dbuser);
|
|
|
|
$e_password = addslashes($this->dbpassword);
|
2013-03-29 15:28:48 +00:00
|
|
|
|
|
|
|
//check if the database user has admin rights
|
|
|
|
$connection_string = "host='$e_host' dbname=postgres user='$e_user' password='$e_password'";
|
|
|
|
$connection = @pg_connect($connection_string);
|
|
|
|
if(!$connection) {
|
|
|
|
// Try if we can connect to the DB with the specified name
|
2013-04-02 20:01:23 +00:00
|
|
|
$e_dbname = addslashes($this->dbname);
|
2013-03-29 15:28:48 +00:00
|
|
|
$connection_string = "host='$e_host' dbname='$e_dbname' user='$e_user' password='$e_password'";
|
|
|
|
$connection = @pg_connect($connection_string);
|
|
|
|
|
|
|
|
if(!$connection)
|
2013-04-02 20:01:23 +00:00
|
|
|
throw new \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.'));
|
2013-03-29 15:28:48 +00:00
|
|
|
}
|
2013-04-02 20:01:23 +00:00
|
|
|
$e_user = pg_escape_string($this->dbuser);
|
2013-03-29 15:28:48 +00: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
|
2013-04-02 20:01:23 +00:00
|
|
|
$this->dbuser='oc_'.$username;
|
2013-03-29 15:28:48 +00:00
|
|
|
//create a new password so we don't need to store the admin config in the config file
|
2013-08-15 06:49:19 +00:00
|
|
|
$this->dbpassword=\OC_Util::generateRandomBytes(30);
|
2013-03-29 15:28:48 +00:00
|
|
|
|
2013-04-02 20:01:23 +00:00
|
|
|
$this->createDBUser($connection);
|
2013-03-29 15:28:48 +00:00
|
|
|
|
2013-04-02 20:01:23 +00:00
|
|
|
\OC_Config::setValue('dbuser', $this->dbuser);
|
|
|
|
\OC_Config::setValue('dbpassword', $this->dbpassword);
|
2013-03-29 15:28:48 +00:00
|
|
|
|
|
|
|
//create the database
|
2013-04-02 20:01:23 +00:00
|
|
|
$this->createDatabase($connection);
|
2013-03-29 15:28:48 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-04-02 20:01:23 +00:00
|
|
|
\OC_Config::setValue('dbuser', $this->dbuser);
|
|
|
|
\OC_Config::setValue('dbpassword', $this->dbpassword);
|
2013-03-29 15:28:48 +00:00
|
|
|
|
|
|
|
//create the database
|
2013-04-02 20:01:23 +00:00
|
|
|
$this->createDatabase($connection);
|
2013-03-29 15:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// the connection to dbname=postgres is not needed anymore
|
|
|
|
pg_close($connection);
|
|
|
|
|
2013-04-02 20:01:23 +00:00
|
|
|
// connect to the ownCloud database (dbname=$this->dbname) and check if it needs to be filled
|
|
|
|
$this->dbuser = \OC_Config::getValue('dbuser');
|
|
|
|
$this->dbpassword = \OC_Config::getValue('dbpassword');
|
2013-03-29 15:28:48 +00:00
|
|
|
|
2013-04-02 20:01:23 +00:00
|
|
|
$e_host = addslashes($this->dbhost);
|
|
|
|
$e_dbname = addslashes($this->dbname);
|
|
|
|
$e_user = addslashes($this->dbuser);
|
|
|
|
$e_password = addslashes($this->dbpassword);
|
2013-03-29 15:28:48 +00:00
|
|
|
|
|
|
|
$connection_string = "host='$e_host' dbname='$e_dbname' user='$e_user' password='$e_password'";
|
|
|
|
$connection = @pg_connect($connection_string);
|
|
|
|
if(!$connection) {
|
2013-04-02 20:01:23 +00:00
|
|
|
throw new \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.'));
|
2013-03-29 15:28:48 +00:00
|
|
|
}
|
2013-04-10 22:08:53 +00:00
|
|
|
$query = "select count(*) FROM pg_class WHERE relname='".$this->tableprefix."users' limit 1";
|
2013-03-29 15:28:48 +00:00
|
|
|
$result = pg_query($connection, $query);
|
|
|
|
if($result) {
|
|
|
|
$row = pg_fetch_row($result);
|
|
|
|
}
|
|
|
|
if(!$result or $row[0]==0) {
|
2013-06-27 18:19:51 +00:00
|
|
|
\OC_DB::createDbFromStructure($this->dbDefinitionFile);
|
2013-03-29 15:28:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-02 20:01:23 +00:00
|
|
|
private function createDatabase($connection) {
|
2013-03-29 15:28:48 +00:00
|
|
|
//we cant use OC_BD functions here because we need to connect as the administrative user.
|
2013-04-02 20:01:23 +00:00
|
|
|
$e_name = pg_escape_string($this->dbname);
|
|
|
|
$e_user = pg_escape_string($this->dbuser);
|
2013-03-29 15:28:48 +00:00
|
|
|
$query = "select datname from pg_database where datname = '$e_name'";
|
|
|
|
$result = pg_query($connection, $query);
|
|
|
|
if(!$result) {
|
2013-04-02 20:01:23 +00: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 />';
|
2013-03-29 15:28:48 +00:00
|
|
|
\OC_Log::write('setup.pg', $entry, \OC_Log::WARN);
|
|
|
|
}
|
|
|
|
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-02 20:01:23 +00: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 />';
|
2013-03-29 15:28:48 +00:00
|
|
|
\OC_Log::write('setup.pg', $entry, \OC_Log::WARN);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$query = "REVOKE ALL PRIVILEGES ON DATABASE \"$e_name\" FROM PUBLIC";
|
|
|
|
pg_query($connection, $query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-02 20:01:23 +00:00
|
|
|
private function createDBUser($connection) {
|
|
|
|
$e_name = pg_escape_string($this->dbuser);
|
|
|
|
$e_password = pg_escape_string($this->dbpassword);
|
2013-03-29 15:28:48 +00:00
|
|
|
$query = "select * from pg_roles where rolname='$e_name';";
|
|
|
|
$result = pg_query($connection, $query);
|
|
|
|
if(!$result) {
|
2013-04-02 20:01:23 +00: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 />';
|
2013-03-29 15:28:48 +00:00
|
|
|
\OC_Log::write('setup.pg', $entry, \OC_Log::WARN);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02 20:01:23 +00: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 />';
|
2013-03-29 15:28:48 +00:00
|
|
|
\OC_Log::write('setup.pg', $entry, \OC_Log::WARN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-02 20:01:23 +00: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 />';
|
2013-03-29 15:28:48 +00:00
|
|
|
\OC_Log::write('setup.pg', $entry, \OC_Log::WARN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|