]> source.dussan.org Git - nextcloud-server.git/commitdiff
Convert database setup code to objects
authorBart Visscher <bartv@thisnet.nl>
Tue, 2 Apr 2013 20:01:23 +0000 (22:01 +0200)
committerBart Visscher <bartv@thisnet.nl>
Sat, 29 Jun 2013 19:07:11 +0000 (21:07 +0200)
lib/setup.php
lib/setup/abstractdatabase.php [new file with mode: 0644]
lib/setup/mssql.php
lib/setup/mysql.php
lib/setup/oci.php
lib/setup/postgresql.php

index d58dece36567d4ca36ce097562ccda18f5e418e3..30975ae8ff6c593505147d8e5e243243beeb3d93 100644 (file)
@@ -91,34 +91,22 @@ class OC_Setup {
                OC_Config::setValue('datadirectory', $datadir);
                OC_Config::setValue('dbtype', $dbtype);
                OC_Config::setValue('version', implode('.', OC_Util::getVersion()));
-               if ($dbtype == 'mysql' or $dbtype == 'pgsql' or $dbtype == 'oci' or $dbtype == 'mssql') { // these needs more config options
-                       $dbuser = $options['dbuser'];
-                       $dbpass = $options['dbpass'];
-                       $dbname = $options['dbname'];
-                       $dbhost = isset($options['dbhost']) ? $options['dbhost'] : ''; // dbhost contents is checked above
-                       $dbtableprefix = isset($options['dbtableprefix']) ? $options['dbtableprefix'] : 'oc_';
-
-                       OC_Config::setValue('dbname', $dbname);
-                       OC_Config::setValue('dbhost', $dbhost);
-                       OC_Config::setValue('dbtableprefix', $dbtableprefix);
-               }
                try {
                        if ($dbtype == 'mysql') {
-                               \OC\Setup\MySQL::setupDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $username);
+                               $db_setup = new \OC\Setup\MySQL(self::getTrans(), $options);
+                               $db_setup->setupDatabase($username);
                        }
                        elseif($dbtype == 'pgsql') {
-                               \OC\Setup\PostgreSQL::setupDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $username);
+                               $db_setup = new \OC\Setup\PostgreSQL(self::getTrans(), $options);
+                               $db_setup->setupDatabase($username);
                        }
                        elseif($dbtype == 'oci') {
-                               if (array_key_exists('dbtablespace', $options)) {
-                                       $dbtablespace = $options['dbtablespace'];
-                               } else {
-                                       $dbtablespace = 'USERS';
-                               }
-                               \OC\Setup\OCI::setupDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $dbtablespace, $username);
+                               $db_setup = new \OC\Setup\OCI(self::getTrans(), $options);
+                               $db_setup->setupDatabase($username);
                        }
                        elseif ($dbtype == 'mssql') {
-                               \OC\Setup\MSSQL::setupDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix);
+                               $db_setup = new \OC\Setup\MSSQL(self::getTrans(), $options);
+                               $db_setup->setupDatabase($username);
                        }
                        else { // sqlite
                                //delete the old sqlite database first, might cause infinte loops otherwise
diff --git a/lib/setup/abstractdatabase.php b/lib/setup/abstractdatabase.php
new file mode 100644 (file)
index 0000000..cef64ee
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+
+namespace OC\Setup;
+
+abstract class AbstractDatabase {
+       protected $trans;
+       protected $dbuser;
+       protected $dbpassword;
+       protected $dbname;
+       protected $dbhost;
+       protected $tableprefix;
+
+       public function __construct($trans, $config) {
+               $this->trans = $trans;
+               $this->initialize($config);
+       }
+
+       public function initialize($config) {
+               $dbuser = $config['dbuser'];
+               $dbpass = $config['dbpass'];
+               $dbname = $config['dbname'];
+               $dbhost = isset($config['dbhost']) ? $config['dbhost'] : ''; // dbhost contents is checked earlier
+               $dbtableprefix = isset($config['dbtableprefix']) ? $config['dbtableprefix'] : 'oc_';
+
+               \OC_Config::setValue('dbname', $dbname);
+               \OC_Config::setValue('dbhost', $dbhost);
+               \OC_Config::setValue('dbtableprefix', $dbtableprefix);
+
+               $this->dbuser = $dbuser;
+               $this->dbpassword = $dbpass;
+               $this->dbname = $dbname;
+               $this->dbhost = $dbhost;
+               $this->tableprefix = $tableprefix;
+       }
+}
index b3e08fb4fa5e81c4ed60d1c69a6f88c6ac74e7db..a1414a9ac573fd2a6d56090fd2e613bf9e2f92b1 100644 (file)
@@ -2,14 +2,12 @@
 
 namespace OC\Setup;
 
-class MSSQL {
-       public static function setupDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix) {
-               $l = \OC_Setup::getTrans();
-
+class MSSQL extends AbstractDatabase {
+       public function setupDatabase() {
                //check if the database user has admin right
-               $masterConnectionInfo = array( "Database" => "master", "UID" => $dbuser, "PWD" => $dbpass);
+               $masterConnectionInfo = array( "Database" => "master", "UID" => $this->dbuser, "PWD" => $this->dbpassword);
 
-               $masterConnection = @sqlsrv_connect($dbhost, $masterConnectionInfo);
+               $masterConnection = @sqlsrv_connect($this->dbhost, $masterConnectionInfo);
                if(!$masterConnection) {
                        $entry = null;
                        if( ($errors = sqlsrv_errors() ) != null) {
@@ -17,26 +15,26 @@ class MSSQL {
                        } else {
                                $entry = '';
                        }
-                       throw new DatabaseSetupException($l->t('MS SQL username and/or password not valid: %s', array($entry)),
-                                       $l->t('You need to enter either an existing account or the administrator.'));
+                       throw new \DatabaseSetupException($this->trans->t('MS SQL username and/or password not valid: %s', array($entry)),
+                                       $this->trans->t('You need to enter either an existing account or the administrator.'));
                }
 
-               \OC_Config::setValue('dbuser', $dbuser);
-               \OC_Config::setValue('dbpassword', $dbpass);
+               \OC_Config::setValue('dbuser', $this->dbuser);
+               \OC_Config::setValue('dbpassword', $this->dbpassword);
 
-               self::createDBLogin($dbuser, $dbpass, $masterConnection);
+               $this->createDBLogin($masterConnection);
 
-               self::createDatabase($dbname, $masterConnection);
+               $this->createDatabase($masterConnection);
 
-               self::createDBUser($dbuser, $dbname, $masterConnection);
+               $this->createDBUser($masterConnection);
 
                sqlsrv_close($masterConnection);
 
-               self::createDatabaseStructure($dbhost, $dbname, $dbuser, $dbpass, $dbtableprefix);
+               $this->createDatabaseStructure();
        }
 
-       private static function createDBLogin($name, $password, $connection) {
-               $query = "SELECT * FROM master.sys.server_principals WHERE name = '".$name."';";
+       private function createDBLogin($connection) {
+               $query = "SELECT * FROM master.sys.server_principals WHERE name = '".$this->dbuser."';";
                $result = sqlsrv_query($connection, $query);
                if ($result === false) {
                        if ( ($errors = sqlsrv_errors() ) != null) {
@@ -59,7 +57,7 @@ class MSSQL {
                                \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
                        } else {
                                if ($row == null) {
-                                       $query = "CREATE LOGIN [".$name."] WITH PASSWORD = '".$password."';";
+                                       $query = "CREATE LOGIN [".$this->dbuser."] WITH PASSWORD = '".$this->dbpassword."';";
                                        $result = sqlsrv_query($connection, $query);
                                        if (!$result or $result === false) {
                                                if ( ($errors = sqlsrv_errors() ) != null) {
@@ -75,8 +73,8 @@ class MSSQL {
                }
        }
 
-       private static function createDBUser($name, $dbname, $connection) {
-               $query = "SELECT * FROM [".$dbname."].sys.database_principals WHERE name = '".$name."';";
+       private function createDBUser($connection) {
+               $query = "SELECT * FROM [".$this->dbname."].sys.database_principals WHERE name = '".$this->dbuser."';";
                $result = sqlsrv_query($connection, $query);
                if ($result === false) {
                        if ( ($errors = sqlsrv_errors() ) != null) {
@@ -99,7 +97,7 @@ class MSSQL {
                                \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
                        } else {
                                if ($row == null) {
-                                       $query = "USE [".$dbname."]; CREATE USER [".$name."] FOR LOGIN [".$name."];";
+                                       $query = "USE [".$this->dbname."]; CREATE USER [".$this->dbuser."] FOR LOGIN [".$this->dbuser."];";
                                        $result = sqlsrv_query($connection, $query);
                                        if (!$result || $result === false) {
                                                if ( ($errors = sqlsrv_errors() ) != null) {
@@ -112,7 +110,7 @@ class MSSQL {
                                        }
                                }
 
-                               $query = "USE [".$dbname."]; EXEC sp_addrolemember 'db_owner', '".$name."';";
+                               $query = "USE [".$this->dbname."]; EXEC sp_addrolemember 'db_owner', '".$this->dbuser."';";
                                $result = sqlsrv_query($connection, $query);
                                if (!$result || $result === false) {
                                        if ( ($errors = sqlsrv_errors() ) != null) {
@@ -127,8 +125,8 @@ class MSSQL {
                }
        }
 
-       private static function createDatabase($dbname, $connection) {
-               $query = "CREATE DATABASE [".$dbname."];";
+       private function createDatabase($connection) {
+               $query = "CREATE DATABASE [".$this->dbname."];";
                $result = sqlsrv_query($connection, $query);
                if (!$result || $result === false) {
                        if ( ($errors = sqlsrv_errors() ) != null) {
@@ -141,13 +139,15 @@ class MSSQL {
                }
        }
 
-       private static function createDatabaseStructure($dbhost, $dbname, $dbuser, $dbpass, $dbtableprefix) {
-               $connectionInfo = array( "Database" => $dbname, "UID" => $dbuser, "PWD" => $dbpass);
+       private function createDatabaseStructure() {
+               $connectionInfo = array( "Database" => $this->dbname, "UID" => $this->dbuser, "PWD" => $this->dbpassword);
 
-               $connection = @sqlsrv_connect($dbhost, $connectionInfo);
+               $connection = @sqlsrv_connect($this->dbhost, $connectionInfo);
 
                //fill the database if needed
-               $query = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{$dbname}' AND TABLE_NAME = '{$dbtableprefix}users'";
+               $query = "SELECT * FROM INFORMATION_SCHEMA.TABLES"
+                       ." WHERE TABLE_SCHEMA = '".$this->dbname."'"
+                       ." AND TABLE_NAME = '".$this->dbtableprefix."users'";
                $result = sqlsrv_query($connection, $query);
                if ($result === false) {
                        if ( ($errors = sqlsrv_errors() ) != null) {
index 92ed5b425247cc886e6f52d1f09b3bb2ac2fd231..790b8e82257d9d6b8e1914fbdfaa006fcd1958cf 100644 (file)
@@ -2,50 +2,49 @@
 
 namespace OC\Setup;
 
-class MySQL {
-       public static function setupDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $username) {
+class MySQL extends AbstractDatabase {
+       public function setupDatabase($username) {
                //check if the database user has admin right
-               $l = \OC_Setup::getTrans();
-               $connection = @mysql_connect($dbhost, $dbuser, $dbpass);
+               $connection = @mysql_connect($this->dbhost, $this->dbuser, $this->dbpassword);
                if(!$connection) {
-                       throw new DatabaseSetupException($l->t('MySQL username and/or password not valid'),
-                               $l->t('You need to enter either an existing account or the administrator.'));
+                       throw new \DatabaseSetupException($this->trans->t('MySQL username and/or password not valid'),
+                               $this->trans->t('You need to enter either an existing account or the administrator.'));
                }
                $oldUser=\OC_Config::getValue('dbuser', false);
 
                //this should be enough to check for admin rights in mysql
-               $query="SELECT user FROM mysql.user WHERE user='$dbuser'";
+               $query="SELECT user FROM mysql.user WHERE user='$this->dbuser'";
                if(mysql_query($query, $connection)) {
                        //use the admin login data for the new database user
 
                        //add prefix to the mysql user name to prevent collisions
-                       $dbusername=substr('oc_'.$username, 0, 16);
-                       if($dbusername!=$oldUser) {
+                       $this->dbuser=substr('oc_'.$username, 0, 16);
+                       if($this->dbuser!=$oldUser) {
                                //hash the password so we don't need to store the admin config in the config file
-                               $dbpassword=OC_Util::generate_random_bytes(30);
+                               $this->dbpassword=OC_Util::generate_random_bytes(30);
 
-                               self::createDBUser($dbusername, $dbpassword, $connection);
+                               $this->createDBUser($connection);
 
-                               \OC_Config::setValue('dbuser', $dbusername);
-                               \OC_Config::setValue('dbpassword', $dbpassword);
+                               \OC_Config::setValue('dbuser', $this->dbuser);
+                               \OC_Config::setValue('dbpassword', $this->dbpassword);
                        }
 
                        //create the database
-                       self::createDatabase($dbname, $dbusername, $connection);
+                       $this->createDatabase($connection);
                }
                else {
-                       if($dbuser!=$oldUser) {
-                               \OC_Config::setValue('dbuser', $dbuser);
-                               \OC_Config::setValue('dbpassword', $dbpass);
+                       if($this->dbuser!=$oldUser) {
+                               \OC_Config::setValue('dbuser', $this->dbuser);
+                               \OC_Config::setValue('dbpassword', $this->dbpassword);
                        }
 
                        //create the database
-                       self::createDatabase($dbname, $dbuser, $connection);
+                       $this->createDatabase($connection);
                }
 
                //fill the database if needed
                $query='select count(*) from information_schema.tables'
-                       ." where table_schema='$dbname' AND table_name = '{$dbtableprefix}users';";
+                       ." where table_schema='".$this->dbname."' AND table_name = '".$this->dbtableprefix."users';";
                $result = mysql_query($query, $connection);
                if($result) {
                        $row=mysql_fetch_row($result);
@@ -56,37 +55,39 @@ class MySQL {
                mysql_close($connection);
        }
 
-       private static function createDatabase($name, $user, $connection) {
+       private function createDatabase($connection) {
+               $name = $this->dbname;
+               $user = $this->dbuser;
                //we cant use OC_BD functions here because we need to connect as the administrative user.
-               $l = \OC_Setup::getTrans();
-               $query = "CREATE DATABASE IF NOT EXISTS  `$name`";
+               $query = "CREATE DATABASE IF NOT EXISTS `$name`";
                $result = mysql_query($query, $connection);
                if(!$result) {
-                       $entry = $l->t('DB Error: "%s"', array(mysql_error($connection))) . '<br />';
-                       $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
+                       $entry = $this->trans->t('DB Error: "%s"', array(mysql_error($connection))) . '<br />';
+                       $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
                        \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
                }
-               $query="GRANT ALL PRIVILEGES ON  `$name` . * TO  '$user'";
+               $query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'";
 
                //this query will fail if there aren't the right permissions, ignore the error
                mysql_query($query, $connection);
        }
 
-       private static function createDBUser($name, $password, $connection) {
+       private function createDBUser($connection) {
+               $name = $this->dbuser;
+               $password = $this->dbpassword;
                // we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one,
                // the anonymous user would take precedence when there is one.
-               $l = \OC_Setup::getTrans();
                $query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'";
                $result = mysql_query($query, $connection);
                if (!$result) {
-                       throw new DatabaseSetupException($l->t("MySQL user '%s'@'localhost' exists already.", array($name)),
-                               $l->t("Drop this user from MySQL", array($name)));
+                       throw new \DatabaseSetupException($this->trans->t("MySQL user '%s'@'localhost' exists already.", array($name)),
+                               $this->trans->t("Drop this user from MySQL", array($name)));
                }
                $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'";
                $result = mysql_query($query, $connection);
                if (!$result) {
-                       throw new DatabaseSetupException($l->t("MySQL user '%s'@'%%' already exists", array($name)),
-                               $l->t("Drop this user from MySQL."));
+                       throw new \DatabaseSetupException($this->trans->t("MySQL user '%s'@'%%' already exists", array($name)),
+                               $this->trans->t("Drop this user from MySQL."));
                }
        }
 }
index 9694d460dcd7ef90b51af9f2e89562b72bfac004..3bb625c557d3f426b843a04166c5809b6deb3b4b 100644 (file)
@@ -2,12 +2,22 @@
 
 namespace OC\Setup;
 
-class OCI {
-       public static function setupDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $dbtablespace,
-               $username) {
-               $l = \OC_Setup::getTrans();
-               $e_host = addslashes($dbhost);
-               $e_dbname = addslashes($dbname);
+class OCI extends AbstractDatabase {
+       protected $dbtablespace;
+
+       public function initialize($config) {
+               parent::initialize($config);
+               if (array_key_exists('dbtablespace', $options)) {
+                       $this->dbtablespace = $options['dbtablespace'];
+               } else {
+                       $this->dbtablespace = 'USERS';
+               }
+               \OC_Config::setValue('dbtablespace', $this->dbtablespace);
+       }
+
+       public function setupDatabase($dbtablespace, $username) {
+               $e_host = addslashes($this->dbhost);
+               $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
@@ -15,7 +25,7 @@ class OCI {
                        $easy_connect_string = '//'.$e_host.'/'.$e_dbname;
                }
                \OC_Log::write('setup oracle', 'connect string: ' . $easy_connect_string, \OC_Log::DEBUG);
-               $connection = @oci_connect($dbuser, $dbpass, $easy_connect_string);
+               $connection = @oci_connect($this->dbuser, $this->dbpassword, $easy_connect_string);
                if(!$connection) {
                        $e = oci_error();
                        if (is_array ($e) && isset ($e['message'])) {
@@ -26,7 +36,7 @@ class OCI {
                                                        .' NLS_LANG='.getenv('NLS_LANG')
                                                        .' tnsnames.ora is '.(is_readable(getenv('ORACLE_HOME').'/network/admin/tnsnames.ora')?'':'not ').'readable');
                        }
-                       throw new DatabaseSetupException($l->t($l->t('Oracle username and/or password not valid'),
+                       throw new DatabaseSetupException($l->t('Oracle username and/or password not valid'),
                                        'Check environment: ORACLE_HOME='.getenv('ORACLE_HOME')
                                                        .' ORACLE_SID='.getenv('ORACLE_SID')
                                                        .' LD_LIBRARY_PATH='.getenv('LD_LIBRARY_PATH')
@@ -39,8 +49,8 @@ class OCI {
                        ." WHERE user_role_privs.granted_role = role_sys_privs.role AND privilege = 'CREATE ROLE'";
                $stmt = oci_parse($connection, $query);
                if (!$stmt) {
-                       $entry = $l->t('DB Error: "%s"', array(oci_last_error($connection))) . '<br />';
-                       $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
+                       $entry = $this->trans->t('DB Error: "%s"', array(oci_last_error($connection))) . '<br />';
+                       $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
                        \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
                }
                $result = oci_execute($stmt);
@@ -51,31 +61,31 @@ class OCI {
                        //use the admin login data for the new database user
 
                        //add prefix to the oracle user name to prevent collisions
-                       $dbusername='oc_'.$username;
+                       $this->dbuser='oc_'.$username;
                        //create a new password so we don't need to store the admin config in the config file
-                       $dbpassword=OC_Util::generate_random_bytes(30);
+                       $this->dbpassword=OC_Util::generate_random_bytes(30);
 
                        //oracle passwords are treated as identifiers:
                        //  must start with aphanumeric char
                        //  needs to be shortened to 30 bytes, as the two " needed to escape the identifier count towards the identifier length.
-                       $dbpassword=substr($dbpassword, 0, 30);
+                       $this->dbpassword=substr($this->dbpassword, 0, 30);
 
-                       self::createDBUser($dbusername, $dbpassword, $dbtablespace, $connection);
+                       $this->createDBUser($dbtablespace, $connection);
 
-                       \OC_Config::setValue('dbuser', $dbusername);
-                       \OC_Config::setValue('dbname', $dbusername);
-                       \OC_Config::setValue('dbpassword', $dbpassword);
+                       \OC_Config::setValue('dbuser', $this->dbusername);
+                       \OC_Config::setValue('dbname', $this->dbusername);
+                       \OC_Config::setValue('dbpassword', $this->dbpassword);
 
                        //create the database not neccessary, oracle implies user = schema
-                       //self::createDatabase($dbname, $dbusername, $connection);
+                       //$this->createDatabase($this->dbname, $this->dbusername, $connection);
                } else {
 
-                       \OC_Config::setValue('dbuser', $dbuser);
-                       \OC_Config::setValue('dbname', $dbname);
-                       \OC_Config::setValue('dbpassword', $dbpass);
+                       \OC_Config::setValue('dbuser', $this->dbuser);
+                       \OC_Config::setValue('dbname', $this->dbname);
+                       \OC_Config::setValue('dbpassword', $this->dbpassword);
 
                        //create the database not neccessary, oracle implies user = schema
-                       //self::createDatabase($dbname, $dbuser, $connection);
+                       //$this->createDatabase($this->dbname, $this->dbuser, $connection);
                }
 
                //FIXME check tablespace exists: select * from user_tablespaces
@@ -83,31 +93,31 @@ class OCI {
                // the connection to dbname=oracle is not needed anymore
                oci_close($connection);
 
-               // connect to the oracle database (schema=$dbuser) an check if the schema needs to be filled
-               $dbuser = \OC_Config::getValue('dbuser');
-               //$dbname = \OC_Config::getValue('dbname');
-               $dbpass = \OC_Config::getValue('dbpassword');
+               // connect to the oracle database (schema=$this->dbuser) an check if the schema needs to be filled
+               $this->dbuser = \OC_Config::getValue('dbuser');
+               //$this->dbname = \OC_Config::getValue('dbname');
+               $this->dbpassword = \OC_Config::getValue('dbpassword');
 
-               $e_host = addslashes($dbhost);
-               $e_dbname = addslashes($dbname);
+               $e_host = addslashes($this->dbhost);
+               $e_dbname = addslashes($this->dbname);
 
                if ($e_host == '') {
                        $easy_connect_string = $e_dbname; // use dbname as easy connect name
                } else {
                        $easy_connect_string = '//'.$e_host.'/'.$e_dbname;
                }
-               $connection = @oci_connect($dbuser, $dbpass, $easy_connect_string);
+               $connection = @oci_connect($this->dbuser, $this->dbpassword, $easy_connect_string);
                if(!$connection) {
-                       throw new DatabaseSetupException($l->t('Oracle username and/or password not valid'),
-                                       $l->t('You need to enter either an existing account or the administrator.'));
+                       throw new \DatabaseSetupException($this->trans->t('Oracle username and/or password not valid'),
+                                       $this->trans->t('You need to enter either an existing account or the administrator.'));
                }
                $query = "SELECT count(*) FROM user_tables WHERE table_name = :un";
                $stmt = oci_parse($connection, $query);
-               $un = $dbtableprefix.'users';
+               $un = $this->dbtableprefix.'users';
                oci_bind_by_name($stmt, ':un', $un);
                if (!$stmt) {
-                       $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
-                       $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
+                       $entry = $this->trans->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
+                       $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
                        \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
                }
                $result = oci_execute($stmt);
@@ -127,38 +137,39 @@ class OCI {
         * @param String $tablespace
         * @param resource $connection
         */
-       private static function createDBUser($name, $password, $tablespace, $connection) {
-               $l = \OC_Setup::getTrans();
+       private function createDBUser($tablespace, $connection) {
+               $name = $this->dbuser;
+               $password = $this->password;
                $query = "SELECT * FROM all_users WHERE USERNAME = :un";
                $stmt = oci_parse($connection, $query);
                if (!$stmt) {
-                       $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
-                       $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
+                       $entry = $this->trans->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
+                       $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
                        \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
                }
                oci_bind_by_name($stmt, ':un', $name);
                $result = oci_execute($stmt);
                if(!$result) {
-                       $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
-                       $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
+                       $entry = $this->trans->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
+                       $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
                        \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
                }
 
                if(! oci_fetch_row($stmt)) {
                        //user does not exists let's create it :)
                        //password must start with alphabetic character in oracle
-                       $query = 'CREATE USER '.$name.' IDENTIFIED BY "'.$password.'" DEFAULT TABLESPACE '.$tablespace; //TODO set default tablespace
+                       $query = 'CREATE USER '.$name.' IDENTIFIED BY "'.$password.'" DEFAULT TABLESPACE '.$this->dbtablespace;
                        $stmt = oci_parse($connection, $query);
                        if (!$stmt) {
-                               $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
-                               $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
+                               $entry = $this->trans->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
+                               $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
                                \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
                        }
                        //oci_bind_by_name($stmt, ':un', $name);
                        $result = oci_execute($stmt);
                        if(!$result) {
-                               $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
-                               $entry .= $l->t('Offending command was: "%s", name: %s, password: %s',
+                               $entry = $this->trans->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
+                               $entry .= $this->trans->t('Offending command was: "%s", name: %s, password: %s',
                                        array($query, $name, $password)) . '<br />';
                                \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
                        }
@@ -166,16 +177,16 @@ class OCI {
                        $query = "ALTER USER :un IDENTIFIED BY :pw";
                        $stmt = oci_parse($connection, $query);
                        if (!$stmt) {
-                               $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
-                               $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
+                               $entry = $this->trans->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
+                               $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
                                \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
                        }
                        oci_bind_by_name($stmt, ':un', $name);
                        oci_bind_by_name($stmt, ':pw', $password);
                        $result = oci_execute($stmt);
                        if(!$result) {
-                               $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
-                               $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
+                               $entry = $this->trans->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
+                               $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
                                \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
                        }
                }
@@ -183,14 +194,14 @@ class OCI {
                $query = 'GRANT CREATE SESSION, CREATE TABLE, CREATE SEQUENCE, CREATE TRIGGER, UNLIMITED TABLESPACE TO '.$name;
                $stmt = oci_parse($connection, $query);
                if (!$stmt) {
-                       $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
-                       $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
+                       $entry = $this->trans->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
+                       $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
                        \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
                }
                $result = oci_execute($stmt);
                if(!$result) {
-                       $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
-                       $entry .= $l->t('Offending command was: "%s", name: %s, password: %s',
+                       $entry = $this->trans->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
+                       $entry .= $this->trans->t('Offending command was: "%s", name: %s, password: %s',
                                array($query, $name, $password)) . '<br />';
                        \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
                }
index a86039989e5061daf8e0c4950416cc375f45daa6..3fb1e6b878768da05cadcc5d271c139c8eb425b3 100644 (file)
@@ -2,27 +2,26 @@
 
 namespace OC\Setup;
 
-class PostgreSQL {
-       public static function setupDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $username) {
-               $e_host = addslashes($dbhost);
-               $e_user = addslashes($dbuser);
-               $e_password = addslashes($dbpass);
-               $l = \OC_Setup::getTrans();
+class PostgreSQL extends AbstractDatabase {
+       public function setupDatabase($username) {
+               $e_host = addslashes($this->dbhost);
+               $e_user = addslashes($this->dbuser);
+               $e_password = addslashes($this->dbpassword);
 
                //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
-                       $e_dbname = addslashes($dbname);
+                       $e_dbname = addslashes($this->dbname);
                        $connection_string = "host='$e_host' dbname='$e_dbname' user='$e_user' password='$e_password'";
                        $connection = @pg_connect($connection_string);
 
                        if(!$connection)
-                               throw new DatabaseSetupException($l->t('PostgreSQL username and/or password not valid'),
-                                               $l->t('You need to enter either an existing account or the administrator.'));
+                               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.'));
                }
-               $e_user = pg_escape_string($dbuser);
+               $e_user = pg_escape_string($this->dbuser);
                //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);
@@ -30,45 +29,45 @@ class PostgreSQL {
                        //use the admin login data for the new database user
 
                        //add prefix to the postgresql user name to prevent collisions
-                       $dbusername='oc_'.$username;
+                       $this->dbuser='oc_'.$username;
                        //create a new password so we don't need to store the admin config in the config file
-                       $dbpassword=OC_Util::generate_random_bytes(30);
+                       $this->dbpassword=OC_Util::generate_random_bytes(30);
 
-                       self::createDBUser($dbusername, $dbpassword, $connection);
+                       $this->createDBUser($connection);
 
-                       \OC_Config::setValue('dbuser', $dbusername);
-                       \OC_Config::setValue('dbpassword', $dbpassword);
+                       \OC_Config::setValue('dbuser', $this->dbuser);
+                       \OC_Config::setValue('dbpassword', $this->dbpassword);
 
                        //create the database
-                       self::createDatabase($dbname, $dbusername, $connection);
+                       $this->createDatabase($connection);
                }
                else {
-                       \OC_Config::setValue('dbuser', $dbuser);
-                       \OC_Config::setValue('dbpassword', $dbpass);
+                       \OC_Config::setValue('dbuser', $this->dbuser);
+                       \OC_Config::setValue('dbpassword', $this->dbpassword);
 
                        //create the database
-                       self::createDatabase($dbname, $dbuser, $connection);
+                       $this->createDatabase($connection);
                }
 
                // the connection to dbname=postgres is not needed anymore
                pg_close($connection);
 
-               // connect to the ownCloud database (dbname=$dbname) and check if it needs to be filled
-               $dbuser = \OC_Config::getValue('dbuser');
-               $dbpass = \OC_Config::getValue('dbpassword');
+               // 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');
 
-               $e_host = addslashes($dbhost);
-               $e_dbname = addslashes($dbname);
-               $e_user = addslashes($dbuser);
-               $e_password = addslashes($dbpass);
+               $e_host = addslashes($this->dbhost);
+               $e_dbname = addslashes($this->dbname);
+               $e_user = addslashes($this->dbuser);
+               $e_password = addslashes($this->dbpassword);
 
                $connection_string = "host='$e_host' dbname='$e_dbname' user='$e_user' password='$e_password'";
                $connection = @pg_connect($connection_string);
                if(!$connection) {
-                       throw new DatabaseSetupException($l->t('PostgreSQL username and/or password not valid'),
-                                       $l->t('You need to enter either an existing account or the administrator.'));
+                       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.'));
                }
-               $query = "select count(*) FROM pg_class WHERE relname='{$dbtableprefix}users' limit 1";
+               $query = "select count(*) FROM pg_class WHERE relname='".$this->dbtableprefix."users' limit 1";
                $result = pg_query($connection, $query);
                if($result) {
                        $row = pg_fetch_row($result);
@@ -78,17 +77,15 @@ class PostgreSQL {
                }
        }
 
-       private static function createDatabase($name, $user, $connection) {
-
+       private function createDatabase($connection) {
                //we cant use OC_BD functions here because we need to connect as the administrative user.
-               $l = \OC_Setup::getTrans();
-               $e_name = pg_escape_string($name);
-               $e_user = pg_escape_string($user);
+               $e_name = pg_escape_string($this->dbname);
+               $e_user = pg_escape_string($this->dbuser);
                $query = "select datname from pg_database where datname = '$e_name'";
                $result = pg_query($connection, $query);
                if(!$result) {
-                       $entry = $l->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
-                       $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
+                       $entry = $this->trans->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
+                       $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
                        \OC_Log::write('setup.pg', $entry, \OC_Log::WARN);
                }
                if(! pg_fetch_row($result)) {
@@ -96,8 +93,8 @@ class PostgreSQL {
                        $query = "CREATE DATABASE \"$e_name\" OWNER \"$e_user\"";
                        $result = pg_query($connection, $query);
                        if(!$result) {
-                               $entry = $l->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
-                               $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
+                               $entry = $this->trans->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
+                               $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
                                \OC_Log::write('setup.pg', $entry, \OC_Log::WARN);
                        }
                        else {
@@ -107,15 +104,14 @@ class PostgreSQL {
                }
        }
 
-       private static function createDBUser($name, $password, $connection) {
-               $l = \OC_Setup::getTrans();
-               $e_name = pg_escape_string($name);
-               $e_password = pg_escape_string($password);
+       private function createDBUser($connection) {
+               $e_name = pg_escape_string($this->dbuser);
+               $e_password = pg_escape_string($this->dbpassword);
                $query = "select * from pg_roles where rolname='$e_name';";
                $result = pg_query($connection, $query);
                if(!$result) {
-                       $entry = $l->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
-                       $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
+                       $entry = $this->trans->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
+                       $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
                        \OC_Log::write('setup.pg', $entry, \OC_Log::WARN);
                }
 
@@ -124,8 +120,8 @@ class PostgreSQL {
                        $query = "CREATE USER \"$e_name\" CREATEDB PASSWORD '$e_password';";
                        $result = pg_query($connection, $query);
                        if(!$result) {
-                               $entry = $l->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
-                               $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
+                               $entry = $this->trans->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
+                               $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
                                \OC_Log::write('setup.pg', $entry, \OC_Log::WARN);
                        }
                }
@@ -133,8 +129,8 @@ class PostgreSQL {
                        $query = "ALTER ROLE \"$e_name\" WITH PASSWORD '$e_password';";
                        $result = pg_query($connection, $query);
                        if(!$result) {
-                               $entry = $l->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
-                               $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
+                               $entry = $this->trans->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
+                               $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
                                \OC_Log::write('setup.pg', $entry, \OC_Log::WARN);
                        }
                }