diff options
Diffstat (limited to 'lib/setup/oci.php')
-rw-r--r-- | lib/setup/oci.php | 117 |
1 files changed, 64 insertions, 53 deletions
diff --git a/lib/setup/oci.php b/lib/setup/oci.php index 9694d460dcd..3bb625c557d 100644 --- a/lib/setup/oci.php +++ b/lib/setup/oci.php @@ -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); } |