From f2b5bc8ab8fadaee3db3a03e6a4748fdb28a01b1 Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Thu, 7 Feb 2013 14:47:56 +0100 Subject: [PATCH 01/10] adding autotesting for mssql server - windows only --- autotest.cmd | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/autotest.cmd b/autotest.cmd index 053860db54..e5ea0cf4c3 100644 --- a/autotest.cmd +++ b/autotest.cmd @@ -4,14 +4,14 @@ :: @author Thomas Müller :: @author Tobias Ramforth (translated into Windows batch file) :: -:: @copyright 2012 Thomas Müller thomas.mueller@tmit.eu +:: @copyright 2012, 2013 Thomas Müller thomas.mueller@tmit.eu :: @echo off set DATADIR=data-autotest set BASEDIR=%~dp0 -:: create autoconfig for sqlite, mysql and postgresql +:: create autoconfig for sqlite, mysql, postgresql and mssql echo ^ .\tests\autoconfig-sqlite.php echo $AUTOCONFIG ^= array ^( >> .\tests\autoconfig-sqlite.php echo 'installed' ^=^> false^, >> .\tests\autoconfig-sqlite.php @@ -50,16 +50,35 @@ echo 'dbhost' ^=^> 'localhost'^, >> .\tests\autoconfig-pgsql.php echo 'dbpass' ^=^> 'owncloud'^, >> .\tests\autoconfig-pgsql.php echo ^)^; >> .\tests\autoconfig-pgsql.php +echo ^ .\tests\autoconfig-mssql.php +echo $AUTOCONFIG ^= array ^( >> .\tests\autoconfig-mssql.php +echo 'installed' ^=^> false^, >> .\tests\autoconfig-mssql.php +echo 'dbtype' ^=^> 'mssql'^, >> .\tests\autoconfig-mssql.php +echo 'dbtableprefix' ^=^> 'oc_'^, >> .\tests\autoconfig-mssql.php +echo 'adminlogin' ^=^> 'admin'^, >> .\tests\autoconfig-mssql.php +echo 'adminpass' ^=^> 'admin'^, >> .\tests\autoconfig-mssql.php +echo 'directory' ^=^> '%BASEDIR%%DATADIR%'^, >> .\tests\autoconfig-mssql.php +echo 'dbuser' ^=^> 'oc_autotest'^, >> .\tests\autoconfig-mssql.php +echo 'dbname' ^=^> 'oc_autotest'^, >> .\tests\autoconfig-mssql.php +echo 'dbhost' ^=^> 'localhost\sqlexpress'^, >> .\tests\autoconfig-mssql.php +echo 'dbpass' ^=^> 'owncloud'^, >> .\tests\autoconfig-mssql.php +echo ^)^; >> .\tests\autoconfig-mssql.php + echo localhost:5432:*:oc_autotest:owncloud > %APPDATA%\postgresql\pgpass.conf :: :: start test execution :: -::call:execute_tests "sqlite" -call:execute_tests "mysql" -::call:execute_tests "mssql" -::call:execute_tests "ora" -::call:execute_tests "pgsql" +if [%1] == [] ( + echo "Running on all database backends" + call:execute_tests "sqlite" + call:execute_tests "mysql" + call:execute_tests "mssql" + ::call:execute_tests "ora" + call:execute_tests "pgsql" +) else ( + call:execute_tests "%1" +) goto:eof @@ -83,6 +102,9 @@ goto:eof if "%~1" == "mysql" mysql -u oc_autotest -powncloud -e "DROP DATABASE oc_autotest" if "%~1" == "pgsql" dropdb -h localhost -p 5432 -U oc_autotest -w oc_autotest + + :: we assume a sqlexpress installation + if "%~1" == "mssql" sqlcmd -S localhost\sqlexpress -U oc_autotext -P owncloud -Q "IF EXISTS (SELECT name FROM sys.databases WHERE name=N'oc_autotest') DROP DATABASE [oc_autotest]" :: copy autoconfig copy /y %BASEDIR%\tests\autoconfig-%~1.php %BASEDIR%\config\autoconfig.php @@ -96,9 +118,8 @@ goto:eof rmdir /s /q coverage-html-%~1 md coverage-html-%~1 php -f enable_all.php - ::phpunit --log-junit autotest-results-%~1.xml --coverage-clover autotest-clover-%~1.xml --coverage-html coverage-html-%~1 - ::phpunit --bootstrap bootstrap.php --configuration phpunit.xml - php win32-phpunit.php --bootstrap bootstrap.php --configuration phpunit.xml --log-junit autotest-results-%~1.xml --coverage-clover autotest-clover-%~1.xml --coverage-html coverage-html-%~1 + + php win32-phpunit.php --bootstrap bootstrap.php --configuration phpunit-autotest.xml --log-junit autotest-results-%~1.xml --coverage-clover autotest-clover-%~1.xml --coverage-html coverage-html-%~1 echo "Done with testing %~1 ..." cd %BASEDIR% goto:eof @@ -114,4 +135,10 @@ goto:eof :: - to enable dropdb I decided to add following line to pg_hba.conf (this is not the safest way but I don't care for the testing machine): :: local all all trust :: +:: NOTES on mssql: +:: we assume the usage of a local installed sqlexpress +:: create a user 'oc_autotest' with password 'owncloud' and assign the server role 'dbcreator' +:: make sure the sqlserver is configured to allow sql authentication +:: + From d577f790c8cbcf4f8dce74f9991e4bd62e21f949 Mon Sep 17 00:00:00 2001 From: Tobias Ramforth Date: Fri, 8 Feb 2013 00:00:51 +0100 Subject: [PATCH 02/10] Added MS SQL Server support --- core/js/setup.js | 7 ++ core/setup.php | 2 + core/templates/installation.php | 20 +++- lib/db.php | 180 +++++++++++++++++++++++++++++++- lib/setup.php | 148 +++++++++++++++++++++++++- 5 files changed, 346 insertions(+), 11 deletions(-) diff --git a/core/js/setup.js b/core/js/setup.js index 9aded6591c..fb6e7c5097 100644 --- a/core/js/setup.js +++ b/core/js/setup.js @@ -5,6 +5,7 @@ $(document).ready(function() { mysql:!!$('#hasMySQL').val(), postgresql:!!$('#hasPostgreSQL').val(), oracle:!!$('#hasOracle').val(), + mssql:!!$('#hasMSSQL').val() }; $('#selectDbType').buttonset(); @@ -41,6 +42,12 @@ $(document).ready(function() { $('#dbhost').show(250); $('#dbhostlabel').show(250); }); + + $('#mssql').click(function() { + $('#use_other_db').slideDown(250); + $('#dbhost').show(250); + $('#dbhostlabel').show(250); + }); $('input[checked]').trigger('click'); diff --git a/core/setup.php b/core/setup.php index 66b8cf378b..0da9b35a35 100644 --- a/core/setup.php +++ b/core/setup.php @@ -16,6 +16,7 @@ $hasSQLite = class_exists('SQLite3'); $hasMySQL = is_callable('mysql_connect'); $hasPostgreSQL = is_callable('pg_connect'); $hasOracle = is_callable('oci_connect'); +$hasMSSQL = is_callable('sqlsrv_connect'); $datadir = OC_Config::getValue('datadirectory', OC::$SERVERROOT.'/data'); // Protect data directory here, so we can test if the protection is working @@ -26,6 +27,7 @@ $opts = array( 'hasMySQL' => $hasMySQL, 'hasPostgreSQL' => $hasPostgreSQL, 'hasOracle' => $hasOracle, + 'hasMSSQLServer' => $hasMSSQL, 'directory' => $datadir, 'secureRNG' => OC_Util::secureRNG_available(), 'htaccessWorking' => OC_Util::ishtaccessworking(), diff --git a/core/templates/installation.php b/core/templates/installation.php index 03c580c9b0..6a6370785d 100644 --- a/core/templates/installation.php +++ b/core/templates/installation.php @@ -2,6 +2,7 @@ '> '> '> +'>
0): ?> @@ -55,7 +56,7 @@
- + t( 'Configure the database' ); ?>
@@ -71,7 +72,7 @@ - +

MySQL t( 'will be used' ); ?>.

@@ -81,7 +82,7 @@ - +

PostgreSQL t( 'will be used' ); ?>.

@@ -91,7 +92,7 @@ - +

Oracle t( 'will be used' ); ?>.

@@ -99,6 +100,17 @@ /> + + + + +

MS SQL t( 'will be used' ); ?>.

+ + + + /> + +
diff --git a/lib/db.php b/lib/db.php index 51f7c7679d..58f46c1171 100644 --- a/lib/db.php +++ b/lib/db.php @@ -178,6 +178,13 @@ class OC_DB { $dsn = 'oci:dbname=//' . $host . '/' . $name; } break; + case 'mssql': + if ($port) { + $dsn='sqlsrv:Server='.$host.','.$port.';Database='.$name; + } else { + $dsn='sqlsrv:Server='.$host.';Database='.$name; + } + break; default: return false; } @@ -277,6 +284,15 @@ class OC_DB { $dsn['database'] = $user; } break; + case 'mssql': + $dsn = array( + 'phptype' => 'sqlsrv', + 'username' => $user, + 'password' => $pass, + 'hostspec' => $host, + 'database' => $name + ); + break; default: return false; } @@ -540,7 +556,7 @@ class OC_DB { * http://www.sqlite.org/lang_createtable.html * http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions037.htm */ - if( $CONFIG_DBTYPE == 'pgsql' ) { //mysql support it too but sqlite doesn't + if( $CONFIG_DBTYPE == 'pgsql' || 'mssql') { //mysql support it too but sqlite doesn't $content = str_replace( '0000-00-00 00:00:00', 'CURRENT_TIMESTAMP', $content ); } file_put_contents( $file2, $content ); @@ -624,7 +640,7 @@ class OC_DB { } else { return true; } - } elseif( $type == 'pgsql' || $type == 'oci' || $type == 'mysql') { + } elseif( $type == 'pgsql' || $type == 'oci' || $type == 'mysql' || $type == 'mssql') { $query = 'INSERT INTO `' .$table . '` (' . implode(',', array_keys($input)) . ') SELECT \'' . implode('\',\'', array_values($input)) . '\' FROM ' . $table . ' WHERE '; @@ -683,7 +699,15 @@ class OC_DB { }elseif( $type == 'oci' ) { $query = str_replace( '`', '"', $query ); $query = str_ireplace( 'NOW()', 'CURRENT_TIMESTAMP', $query ); - } + }elseif( $type == 'mssql' ) { + $query = preg_replace( "/\`(.*?)`/", "[$1]", $query ); + $query = str_replace( 'NOW()', 'CURRENT_TIMESTAMP', $query ); + $query = str_replace( 'now()', 'CURRENT_TIMESTAMP', $query ); + $query = str_replace( 'LENGTH(', 'LEN(', $query ); + $query = str_replace( 'SUBSTR(', 'SUBSTRING(', $query ); + + $query = self::fixLimitClauseForMSSQL($query); + } // replace table name prefix $query = str_replace( '*PREFIX*', $prefix, $query ); @@ -691,6 +715,60 @@ class OC_DB { return $query; } + private static function fixLimitClauseForMSSQL($query) { + $limitLocation = stripos ($query, "LIMIT"); + + if ( $limitLocation === false ) { + return $query; + } + + // total == 0 means all results - not zero results + // + // First number is either total or offset, locate it by first space + // + $offset = substr ($query, $limitLocation + 5); + $offset = substr ($offset, 0, stripos ($offset, ' ')); + $offset = trim ($offset); + + // check for another parameter + if (stripos ($offset, ',') === false) { + // no more parameters + $offset = 0; + $total = intval ($offset); + } else { + // found another parameter + $offset = intval ($offset); + + $total = substr ($query, $limitLocation + 5); + $total = substr ($total, stripos ($total, ',')); + + $total = substr ($total, 0, stripos ($total, ' ')); + $total = intval ($total); + } + + $query = trim (substr ($query, 0, $limitLocation)); + + if ($offset == 0 && $total !== 0) { + if (strpos($query, "SELECT") === false) { + $query = "TOP {$total} " . $query; + } else { + $query = preg_replace('/SELECT(\s*DISTINCT)?/Dsi', 'SELECT$1 TOP '.$total, $query); + } + } else if ($offset > 0) { + $query = preg_replace('/SELECT(\s*DISTINCT)?/Dsi', 'SELECT$1 TOP(10000000) ', $query); + $query = 'SELECT * + FROM (SELECT sub2.*, ROW_NUMBER() OVER(ORDER BY sub2.line2) AS line3 + FROM (SELECT 1 AS line2, sub1.* FROM (' . $query . ') AS sub1) as sub2) AS sub3'; + + if ($total > 0) { + $query .= ' WHERE line3 BETWEEN ' . ($offset + 1) . ' AND ' . ($offset + $total); + } else { + $query .= ' WHERE line3 > ' . $offset; + } + } + return $query; + } + /** * @brief drop a table * @param string $tableName the table to drop @@ -844,6 +922,14 @@ class PDOStatementWrapper{ public function execute($input=array()) { $this->lastArguments=$input; if(count($input)>0) { + if (!isset($type)) { + $type = OC_Config::getValue( "dbtype", "sqlite" ); + } + + if ($type == 'mssql') { + $this->tryFixSubstringLastArgumentDataForMSSQL($input); + } + $result=$this->statement->execute($input); }else{ $result=$this->statement->execute(); @@ -855,6 +941,94 @@ class PDOStatementWrapper{ } } + private function tryFixSubstringLastArgumentDataForMSSQL(&$input) { + $query = $this->statement->queryString; + $pos = stripos ($query, 'SUBSTRING'); + + if ( $pos === false) { + return; + } + + try { + $newQuery = ''; + + $cArg = 0; + + $inSubstring = false; + + // Create new query + for ($i = 0; $i < strlen ($query); $i++) { + if ($inSubstring == false) { + // Defines when we should start inserting values + if (substr ($query, $i, 9) == 'SUBSTRING') { + $inSubstring = true; + } + } else { + // Defines when we should stop inserting values + if (substr ($query, $i, 1) == ')') { + $inSubstring = false; + } + } + + if (substr ($query, $i, 1) == '?') { + // We found a question mark + if ($inSubstring) { + $newQuery .= $input[$cArg]; + + // + // Remove from input array + // + array_splice ($input, $cArg, 1); + } else { + $newQuery .= substr ($query, $i, 1); + $cArg++; + } + } else { + $newQuery .= substr ($query, $i, 1); + } + } + + // The global data we need + $name = OC_Config::getValue( "dbname", "owncloud" ); + $host = OC_Config::getValue( "dbhost", "" ); + $user = OC_Config::getValue( "dbuser", "" ); + $pass = OC_Config::getValue( "dbpassword", "" ); + if (strpos($host,':')) { + list($host, $port) = explode(':', $host, 2); + } else { + $port = false; + } + $opts = array(); + + if ($port) { + $dsn = 'sqlsrv:Server='.$host.','.$port.';Database='.$name; + } else { + $dsn = 'sqlsrv:Server='.$host.';Database='.$name; + } + + $PDO = new PDO($dsn, $user, $pass, $opts); + $PDO->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); + $PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + $this->statement = $PDO->prepare($newQuery); + + $this->lastArguments = $input; + } catch (PDOException $e){ + $entry = 'PDO DB Error: "'.$e->getMessage().'"
'; + $entry .= 'Offending command was: '.$this->statement->queryString .'
'; + $entry .= 'Input parameters: ' .print_r($input, true).'
'; + $entry .= 'Stack trace: ' .$e->getTraceAsString().'
'; + OC_Log::write('core', $entry, OC_Log::FATAL); + OC_User::setUserId(null); + + // send http status 503 + header('HTTP/1.1 503 Service Temporarily Unavailable'); + header('Status: 503 Service Temporarily Unavailable'); + OC_Template::printErrorPage('Failed to connect to database'); + die ($entry); + } + } + /** * provide numRows */ diff --git a/lib/setup.php b/lib/setup.php index 4dd190b99f..d4ea26354e 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -33,12 +33,14 @@ class OC_Setup { $error[] = 'Specify a data folder.'; } - if($dbtype=='mysql' or $dbtype == 'pgsql' or $dbtype == 'oci') { //mysql and postgresql needs more config options - if($dbtype=='mysql') + if($dbtype == 'mysql' or $dbtype == 'pgsql' or $dbtype == 'oci' or $dbtype == 'mssql') { //mysql and postgresql needs more config options + if($dbtype == 'mysql') $dbprettyname = 'MySQL'; - else if($dbtype=='pgsql') + else if($dbtype == 'pgsql') $dbprettyname = 'PostgreSQL'; - else + else if ($dbtype == 'mssql') + $dbprettyname = 'MS SQL Server'; + else $dbprettyname = 'Oracle'; @@ -145,6 +147,29 @@ class OC_Setup { return $error; } } + elseif ($dbtype == 'mssql') { + $dbuser = $options['dbuser']; + $dbpass = $options['dbpass']; + $dbname = $options['dbname']; + $dbhost = $options['dbhost']; + $dbtableprefix = isset($options['dbtableprefix']) ? $options['dbtableprefix'] : 'oc_'; + + OC_Config::setValue('dbname', $dbname); + OC_Config::setValue('dbhost', $dbhost); + OC_Config::setValue('dbuser', $dbuser); + OC_Config::setValue('dbpassword', $dbpass); + OC_Config::setValue('dbtableprefix', $dbtableprefix); + + try { + self::setupMSSQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix); + } catch (Exception $e) { + $error[] = array( + 'error' => 'MS SQL username and/or password not valid', + 'hint' => 'You need to enter either an existing account or the administrator.' + ); + return $error; + } + } else { //delete the old sqlite database first, might cause infinte loops otherwise if(file_exists("$datadir/owncloud.db")) { @@ -563,6 +588,121 @@ class OC_Setup { } } + private static function setupMSSQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix) { + //check if the database user has admin right + $masterConnectionInfo = array( "Database" => "master", "UID" => $dbuser, "PWD" => $dbpass); + + $masterConnection = @sqlsrv_connect($dbhost, $masterConnectionInfo); + if(!$masterConnection) { + $entry = null; + if( ($errors = sqlsrv_errors() ) != null) { + $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; + } else { + $entry = ''; + } + throw new Exception('MS SQL username and/or password not valid: '.$entry); + } + + OC_Config::setValue('dbuser', $dbuser); + OC_Config::setValue('dbpassword', $dbpass); + + self::mssql_createDBLogin($dbuser, $dbpass, $masterConnection); + + self::mssql_createDatabase($dbname, $masterConnection); + + self::mssql_createDBUser($dbuser, $dbpass, $masterConnection); + + sqlsrv_close($masterConnection); + + $connectionInfo = array( "Database" => $dbname, "UID" => $dbuser, "PWD" => $dbpass); + + $connection = @sqlsrv_connect($dbhost, $connectionInfo); + + //fill the database if needed + $query="SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{$dbname}' AND TABLE_NAME = '{$dbtableprefix}users'"; + $result = sqlsrv_query($connection, $query); + if($result) { + $row=sqlsrv_fetch_array($result); + } + + if(!$result or $row[0] == 0) { + OC_DB::createDbFromStructure('db_structure.xml'); + } + + sqlsrv_close($connection); + } + + private static function mssql_createDBLogin($name, $password, $connection) { + $query = "SELECT * FROM master.sys.server_principals WHERE name = '".$name."';"; + $result = sqlsrv_query($connection, $query); + if ($result) { + $row = sqlsrv_fetch_array($result); + } + + if (!$result or $row[0] == 0) { + $query = "CREATE LOGIN [".$name."] WITH PASSWORD = '".$password."';"; + $result = sqlsrv_query($connection, $query); + if (!$result or $result === false) { + if ( ($errors = sqlsrv_errors() ) != null) { + $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; + } else { + $entry = ''; + } + $entry.='Offending command was: '.$query.'
'; + echo($entry); + } + } + } + + private static function mssql_createDBUser($name, $dbname, $connection) { + $query = "SELECT * FROM [".$dbname."].sys.database_principals WHERE name = '".$name."';"; + $result = sqlsrv_query($connection, $query); + if($result) { + $row=sqlsrv_fetch_array($result); + } + + if (!$result or $row[0] == 0) { + $query = "USE [".$dbname."]; CREATE USER [".$name."] FOR LOGIN [".$name."];"; + $result = sqlsrv_query($connection, $query); + if (!$result or $result === false) { + if ( ($errors = sqlsrv_errors() ) != null) { + $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; + } else { + $entry = ''; + } + $entry.='Offending command was: '.$query.'
'; + echo($entry); + } + } + + $query = "USE [".$dbname."]; EXEC sp_addrolemember 'db_owner', '".$name."';"; + $result = sqlsrv_query($connection, $query); + if (!$result or $result === false) { + if ( ($errors = sqlsrv_errors() ) != null) { + $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; + } else { + $entry = ''; + } + $entry.='Offending command was: '.$query.'
'; + echo($entry); + } + } + + private static function mssql_createDatabase($dbname, $connection) { + $query = "CREATE DATABASE [".$dbname."];"; + $result = sqlsrv_query($connection, $query); + if (!$result or $result === false) { + if ( ($errors = sqlsrv_errors() ) != null) { + $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; + } else { + $entry = ''; + } + $entry.='Offending command was: '.$query.'
'; + echo($entry); + } + } + + /** * create .htaccess files for apache hosts */ From 41ec976fd74320c379f95d05d3a490310f06402e Mon Sep 17 00:00:00 2001 From: Administrator Date: Sun, 10 Feb 2013 13:07:59 +0100 Subject: [PATCH 03/10] Bugfixes and cleanup MS SQL Server installation --- core/js/setup.js | 4 +- core/setup.php | 2 +- core/templates/installation.php | 2 +- lib/db.php | 36 +++--- lib/setup.php | 191 +++++++++++++++++++++----------- 5 files changed, 148 insertions(+), 87 deletions(-) diff --git a/core/js/setup.js b/core/js/setup.js index fb6e7c5097..cb8392d0a3 100644 --- a/core/js/setup.js +++ b/core/js/setup.js @@ -5,7 +5,7 @@ $(document).ready(function() { mysql:!!$('#hasMySQL').val(), postgresql:!!$('#hasPostgreSQL').val(), oracle:!!$('#hasOracle').val(), - mssql:!!$('#hasMSSQL').val() + mssql:!!$('#hasMSSQL').val() }; $('#selectDbType').buttonset(); @@ -43,7 +43,7 @@ $(document).ready(function() { $('#dbhostlabel').show(250); }); - $('#mssql').click(function() { + $('#mssql').click(function() { $('#use_other_db').slideDown(250); $('#dbhost').show(250); $('#dbhostlabel').show(250); diff --git a/core/setup.php b/core/setup.php index 0da9b35a35..6ea16cdcc4 100644 --- a/core/setup.php +++ b/core/setup.php @@ -27,7 +27,7 @@ $opts = array( 'hasMySQL' => $hasMySQL, 'hasPostgreSQL' => $hasPostgreSQL, 'hasOracle' => $hasOracle, - 'hasMSSQLServer' => $hasMSSQL, + 'hasMSSQL' => $hasMSSQL, 'directory' => $datadir, 'secureRNG' => OC_Util::secureRNG_available(), 'htaccessWorking' => OC_Util::ishtaccessworking(), diff --git a/core/templates/installation.php b/core/templates/installation.php index 6a6370785d..4a41527c90 100644 --- a/core/templates/installation.php +++ b/core/templates/installation.php @@ -107,7 +107,7 @@

MS SQL t( 'will be used' ); ?>.

- + /> diff --git a/lib/db.php b/lib/db.php index 58f46c1171..c3ff481e19 100644 --- a/lib/db.php +++ b/lib/db.php @@ -284,7 +284,7 @@ class OC_DB { $dsn['database'] = $user; } break; - case 'mssql': + case 'mssql': $dsn = array( 'phptype' => 'sqlsrv', 'username' => $user, @@ -292,7 +292,7 @@ class OC_DB { 'hostspec' => $host, 'database' => $name ); - break; + break; default: return false; } @@ -920,28 +920,30 @@ class PDOStatementWrapper{ * make execute return the result instead of a bool */ public function execute($input=array()) { - $this->lastArguments=$input; - if(count($input)>0) { - if (!isset($type)) { - $type = OC_Config::getValue( "dbtype", "sqlite" ); - } - - if ($type == 'mssql') { - $this->tryFixSubstringLastArgumentDataForMSSQL($input); - } - + $this->lastArguments = $input; + if (count($input) > 0) { + + if (!isset($type)) { + $type = OC_Config::getValue( "dbtype", "sqlite" ); + } + + if ($type == 'mssql') { + $input = $this->tryFixSubstringLastArgumentDataForMSSQL($input); + } + $result=$this->statement->execute($input); - }else{ + } else { $result=$this->statement->execute(); } - if($result) { + + if ($result) { return $this; - }else{ + } else { return false; } } - private function tryFixSubstringLastArgumentDataForMSSQL(&$input) { + private function tryFixSubstringLastArgumentDataForMSSQL($input) { $query = $this->statement->queryString; $pos = stripos ($query, 'SUBSTRING'); @@ -1013,6 +1015,8 @@ class PDOStatementWrapper{ $this->statement = $PDO->prepare($newQuery); $this->lastArguments = $input; + + return $input; } catch (PDOException $e){ $entry = 'PDO DB Error: "'.$e->getMessage().'"
'; $entry .= 'Offending command was: '.$this->statement->queryString .'
'; diff --git a/lib/setup.php b/lib/setup.php index d4ea26354e..3efad79cfa 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -610,98 +610,155 @@ class OC_Setup { self::mssql_createDatabase($dbname, $masterConnection); - self::mssql_createDBUser($dbuser, $dbpass, $masterConnection); + self::mssql_createDBUser($dbuser, $dbname, $masterConnection); sqlsrv_close($masterConnection); - $connectionInfo = array( "Database" => $dbname, "UID" => $dbuser, "PWD" => $dbpass); - - $connection = @sqlsrv_connect($dbhost, $connectionInfo); - - //fill the database if needed - $query="SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{$dbname}' AND TABLE_NAME = '{$dbtableprefix}users'"; - $result = sqlsrv_query($connection, $query); - if($result) { - $row=sqlsrv_fetch_array($result); - } - - if(!$result or $row[0] == 0) { - OC_DB::createDbFromStructure('db_structure.xml'); - } - - sqlsrv_close($connection); + self::mssql_createDatabaseStructure($dbname, $dbuser, $dbpass); } private static function mssql_createDBLogin($name, $password, $connection) { $query = "SELECT * FROM master.sys.server_principals WHERE name = '".$name."';"; $result = sqlsrv_query($connection, $query); - if ($result) { + if ($result === false) { + if ( ($errors = sqlsrv_errors() ) != null) { + $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; + } else { + $entry = ''; + } + $entry.='Offending command was: '.$query.'
'; + echo($entry); + } else { $row = sqlsrv_fetch_array($result); - } - if (!$result or $row[0] == 0) { - $query = "CREATE LOGIN [".$name."] WITH PASSWORD = '".$password."';"; - $result = sqlsrv_query($connection, $query); - if (!$result or $result === false) { - if ( ($errors = sqlsrv_errors() ) != null) { - $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; - } else { - $entry = ''; - } - $entry.='Offending command was: '.$query.'
'; - echo($entry); - } + if ($row === false) { + if ( ($errors = sqlsrv_errors() ) != null) { + $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; + } else { + $entry = ''; + } + $entry.='Offending command was: '.$query.'
'; + echo($entry); + } else { + if ($row == null) { + $query = "CREATE LOGIN [".$name."] WITH PASSWORD = '".$password."';"; + $result = sqlsrv_query($connection, $query); + if (!$result or $result === false) { + if ( ($errors = sqlsrv_errors() ) != null) { + $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; + } else { + $entry = ''; + } + $entry.='Offending command was: '.$query.'
'; + echo($entry); + } + } + } } } private static function mssql_createDBUser($name, $dbname, $connection) { $query = "SELECT * FROM [".$dbname."].sys.database_principals WHERE name = '".$name."';"; $result = sqlsrv_query($connection, $query); - if($result) { - $row=sqlsrv_fetch_array($result); + if ($result === false) { + if ( ($errors = sqlsrv_errors() ) != null) { + $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; + } else { + $entry = ''; + } + $entry.='Offending command was: '.$query.'
'; + echo($entry); + } else { + $row = sqlsrv_fetch_array($result); + + if ($row === false) { + if ( ($errors = sqlsrv_errors() ) != null) { + $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; + } else { + $entry = ''; + } + $entry.='Offending command was: '.$query.'
'; + echo($entry); + } else { + if ($row == null) { + $query = "USE [".$dbname."]; CREATE USER [".$name."] FOR LOGIN [".$name."];"; + $result = sqlsrv_query($connection, $query); + if (!$result || $result === false) { + if ( ($errors = sqlsrv_errors() ) != null) { + $entry = 'DB Error: "'.print_r(sqlsrv_errors()).'"
'; + } else { + $entry = ''; + } + $entry.='Offending command was: '.$query.'
'; + echo($entry); + } + } + + $query = "USE [".$dbname."]; EXEC sp_addrolemember 'db_owner', '".$name."';"; + $result = sqlsrv_query($connection, $query); + if (!$result || $result === false) { + if ( ($errors = sqlsrv_errors() ) != null) { + $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; + } else { + $entry = ''; + } + $entry.='Offending command was: '.$query.'
'; + echo($entry); + } + } } - - if (!$result or $row[0] == 0) { - $query = "USE [".$dbname."]; CREATE USER [".$name."] FOR LOGIN [".$name."];"; - $result = sqlsrv_query($connection, $query); - if (!$result or $result === false) { - if ( ($errors = sqlsrv_errors() ) != null) { - $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; - } else { - $entry = ''; - } - $entry.='Offending command was: '.$query.'
'; - echo($entry); - } - } - - $query = "USE [".$dbname."]; EXEC sp_addrolemember 'db_owner', '".$name."';"; - $result = sqlsrv_query($connection, $query); - if (!$result or $result === false) { - if ( ($errors = sqlsrv_errors() ) != null) { - $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; - } else { - $entry = ''; - } - $entry.='Offending command was: '.$query.'
'; - echo($entry); - } } private static function mssql_createDatabase($dbname, $connection) { $query = "CREATE DATABASE [".$dbname."];"; $result = sqlsrv_query($connection, $query); - if (!$result or $result === false) { - if ( ($errors = sqlsrv_errors() ) != null) { - $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; - } else { - $entry = ''; - } - $entry.='Offending command was: '.$query.'
'; - echo($entry); + if (!$result || $result === false) { + if ( ($errors = sqlsrv_errors() ) != null) { + $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; + } else { + $entry = ''; + } + $entry.='Offending command was: '.$query.'
'; + echo($entry); } } + private static function mssql_createDatabaseStructure($dbname, $dbuser, $dbpass) { + $connectionInfo = array( "Database" => $dbname, "UID" => $dbuser, "PWD" => $dbpass); + + $connection = @sqlsrv_connect($dbhost, $connectionInfo); + + //fill the database if needed + $query = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{$dbname}' AND TABLE_NAME = '{$dbtableprefix}users'"; + $result = sqlsrv_query($connection, $query); + if ($result === false) { + if ( ($errors = sqlsrv_errors() ) != null) { + $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; + } else { + $entry = ''; + } + $entry.='Offending command was: '.$query.'
'; + echo($entry); + } else { + $row = sqlsrv_fetch_array($result); + + if ($row === false) { + if ( ($errors = sqlsrv_errors() ) != null) { + $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; + } else { + $entry = ''; + } + $entry.='Offending command was: '.$query.'
'; + echo($entry); + } else { + if ($row == null) { + OC_DB::createDbFromStructure('db_structure.xml'); + } + } + } + + sqlsrv_close($connection); + } /** * create .htaccess files for apache hosts From 4e5a3fbcafcaa0cdaba351dfb307b0000179c832 Mon Sep 17 00:00:00 2001 From: Administrator Date: Sun, 10 Feb 2013 14:08:00 +0100 Subject: [PATCH 04/10] - Fixed indentations. - Fixed a bug in legacy.php: there was an error that was not checked for if the table 'fscache' did not exist in the database. --- lib/db.php | 144 ++++++++++++++++++------------------- lib/files/cache/legacy.php | 6 ++ 2 files changed, 78 insertions(+), 72 deletions(-) diff --git a/lib/db.php b/lib/db.php index c3ff481e19..3ccd51737a 100644 --- a/lib/db.php +++ b/lib/db.php @@ -943,95 +943,95 @@ class PDOStatementWrapper{ } } - private function tryFixSubstringLastArgumentDataForMSSQL($input) { - $query = $this->statement->queryString; - $pos = stripos ($query, 'SUBSTRING'); - - if ( $pos === false) { - return; - } - - try { - $newQuery = ''; + private function tryFixSubstringLastArgumentDataForMSSQL($input) { + $query = $this->statement->queryString; + $pos = stripos ($query, 'SUBSTRING'); - $cArg = 0; + if ( $pos === false) { + return; + } - $inSubstring = false; + try { + $newQuery = ''; - // Create new query - for ($i = 0; $i < strlen ($query); $i++) { - if ($inSubstring == false) { - // Defines when we should start inserting values - if (substr ($query, $i, 9) == 'SUBSTRING') { - $inSubstring = true; - } - } else { - // Defines when we should stop inserting values - if (substr ($query, $i, 1) == ')') { - $inSubstring = false; - } - } + $cArg = 0; - if (substr ($query, $i, 1) == '?') { - // We found a question mark - if ($inSubstring) { - $newQuery .= $input[$cArg]; + $inSubstring = false; - // - // Remove from input array - // - array_splice ($input, $cArg, 1); - } else { - $newQuery .= substr ($query, $i, 1); - $cArg++; - } - } else { - $newQuery .= substr ($query, $i, 1); - } - } + // Create new query + for ($i = 0; $i < strlen ($query); $i++) { + if ($inSubstring == false) { + // Defines when we should start inserting values + if (substr ($query, $i, 9) == 'SUBSTRING') { + $inSubstring = true; + } + } else { + // Defines when we should stop inserting values + if (substr ($query, $i, 1) == ')') { + $inSubstring = false; + } + } - // The global data we need - $name = OC_Config::getValue( "dbname", "owncloud" ); - $host = OC_Config::getValue( "dbhost", "" ); - $user = OC_Config::getValue( "dbuser", "" ); - $pass = OC_Config::getValue( "dbpassword", "" ); - if (strpos($host,':')) { - list($host, $port) = explode(':', $host, 2); - } else { - $port = false; - } - $opts = array(); + if (substr ($query, $i, 1) == '?') { + // We found a question mark + if ($inSubstring) { + $newQuery .= $input[$cArg]; - if ($port) { - $dsn = 'sqlsrv:Server='.$host.','.$port.';Database='.$name; - } else { - $dsn = 'sqlsrv:Server='.$host.';Database='.$name; - } + // + // Remove from input array + // + array_splice ($input, $cArg, 1); + } else { + $newQuery .= substr ($query, $i, 1); + $cArg++; + } + } else { + $newQuery .= substr ($query, $i, 1); + } + } - $PDO = new PDO($dsn, $user, $pass, $opts); - $PDO->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); - $PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + // The global data we need + $name = OC_Config::getValue( "dbname", "owncloud" ); + $host = OC_Config::getValue( "dbhost", "" ); + $user = OC_Config::getValue( "dbuser", "" ); + $pass = OC_Config::getValue( "dbpassword", "" ); + if (strpos($host,':')) { + list($host, $port) = explode(':', $host, 2); + } else { + $port = false; + } + $opts = array(); - $this->statement = $PDO->prepare($newQuery); + if ($port) { + $dsn = 'sqlsrv:Server='.$host.','.$port.';Database='.$name; + } else { + $dsn = 'sqlsrv:Server='.$host.';Database='.$name; + } - $this->lastArguments = $input; - - return $input; - } catch (PDOException $e){ + $PDO = new PDO($dsn, $user, $pass, $opts); + $PDO->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); + $PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + $this->statement = $PDO->prepare($newQuery); + + $this->lastArguments = $input; + + return $input; + } catch (PDOException $e){ $entry = 'PDO DB Error: "'.$e->getMessage().'"
'; $entry .= 'Offending command was: '.$this->statement->queryString .'
'; $entry .= 'Input parameters: ' .print_r($input, true).'
'; $entry .= 'Stack trace: ' .$e->getTraceAsString().'
'; OC_Log::write('core', $entry, OC_Log::FATAL); - OC_User::setUserId(null); + OC_User::setUserId(null); - // send http status 503 - header('HTTP/1.1 503 Service Temporarily Unavailable'); - header('Status: 503 Service Temporarily Unavailable'); - OC_Template::printErrorPage('Failed to connect to database'); - die ($entry); + // send http status 503 + header('HTTP/1.1 503 Service Temporarily Unavailable'); + header('Status: 503 Service Temporarily Unavailable'); + OC_Template::printErrorPage('Failed to connect to database'); + die ($entry); } - } + } /** * provide numRows diff --git a/lib/files/cache/legacy.php b/lib/files/cache/legacy.php index 33d4b8e7c9..bdc3cbf00b 100644 --- a/lib/files/cache/legacy.php +++ b/lib/files/cache/legacy.php @@ -51,6 +51,12 @@ class Legacy { $this->cacheHasItems = false; return false; } + + if ($result === false || property_exists($result, 'error_message_prefix')) { + $this->cacheHasItems = false; + return false; + } + $this->cacheHasItems = (bool)$result->fetchRow(); return $this->cacheHasItems; } From 0c31c3cc3a928e8ca9272d32d68163c0700697ea Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Mon, 11 Feb 2013 23:46:32 +0100 Subject: [PATCH 05/10] fixing user for autotesting mssql --- autotest.cmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autotest.cmd b/autotest.cmd index e5ea0cf4c3..a511faef9c 100644 --- a/autotest.cmd +++ b/autotest.cmd @@ -104,7 +104,7 @@ goto:eof if "%~1" == "pgsql" dropdb -h localhost -p 5432 -U oc_autotest -w oc_autotest :: we assume a sqlexpress installation - if "%~1" == "mssql" sqlcmd -S localhost\sqlexpress -U oc_autotext -P owncloud -Q "IF EXISTS (SELECT name FROM sys.databases WHERE name=N'oc_autotest') DROP DATABASE [oc_autotest]" + if "%~1" == "mssql" sqlcmd -S localhost\sqlexpress -U oc_autotest -P owncloud -Q "IF EXISTS (SELECT name FROM sys.databases WHERE name=N'oc_autotest') DROP DATABASE [oc_autotest]" :: copy autoconfig copy /y %BASEDIR%\tests\autoconfig-%~1.php %BASEDIR%\config\autoconfig.php From b2dfdacaa1cb6d7839aed3b4d39adc6504a4beef Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Tue, 12 Feb 2013 01:04:35 +0100 Subject: [PATCH 06/10] fixing undefined variables --- lib/setup.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/setup.php b/lib/setup.php index 893e0121ff..6f2899ed86 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -591,7 +591,7 @@ class OC_Setup { echo($entry); } } - // grant neccessary roles + // grant necessary roles $query = 'GRANT CREATE SESSION, CREATE TABLE, CREATE SEQUENCE, CREATE TRIGGER, UNLIMITED TABLESPACE TO '.$name; $stmt = oci_parse($connection, $query); if (!$stmt) { @@ -634,7 +634,7 @@ class OC_Setup { sqlsrv_close($masterConnection); - self::mssql_createDatabaseStructure($dbname, $dbuser, $dbpass); + self::mssql_createDatabaseStructure($dbhost, $dbname, $dbuser, $dbpass, $dbtableprefix); } private static function mssql_createDBLogin($name, $password, $connection) { @@ -730,7 +730,7 @@ class OC_Setup { } private static function mssql_createDatabase($dbname, $connection) { - $query = "CREATE DATABASE [".$dbname."];"; + $query = "CREATE DATABASE IF NOT EXISTS [".$dbname."];"; $result = sqlsrv_query($connection, $query); if (!$result || $result === false) { if ( ($errors = sqlsrv_errors() ) != null) { @@ -742,8 +742,10 @@ class OC_Setup { echo($entry); } } - - private static function mssql_createDatabaseStructure($dbname, $dbuser, $dbpass) { + + // private static function setupMSSQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $username) { + + private static function mssql_createDatabaseStructure($dbhost, $dbname, $dbuser, $dbpass, $dbtableprefix) { $connectionInfo = array( "Database" => $dbname, "UID" => $dbuser, "PWD" => $dbpass); $connection = @sqlsrv_connect($dbhost, $connectionInfo); From 81368510105c06be21c52a699f77da4764ae6a03 Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Tue, 12 Feb 2013 01:05:47 +0100 Subject: [PATCH 07/10] fixing indent --- lib/setup.php | 70 +++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/lib/setup.php b/lib/setup.php index 6f2899ed86..a71e906a85 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -46,8 +46,8 @@ class OC_Setup { else if($dbtype == 'pgsql') $dbprettyname = 'PostgreSQL'; else if ($dbtype == 'mssql') - $dbprettyname = 'MS SQL Server'; - else + $dbprettyname = 'MS SQL Server'; + else $dbprettyname = 'Oracle'; @@ -100,7 +100,7 @@ class OC_Setup { $error[] = array( 'error' => $e->getMessage(), 'hint' => $e->getHint() - ); + ); return($error); } catch (Exception $e) { $error[] = array( @@ -154,13 +154,13 @@ class OC_Setup { return $error; } } - elseif ($dbtype == 'mssql') { + elseif ($dbtype == 'mssql') { $dbuser = $options['dbuser']; $dbpass = $options['dbpass']; $dbname = $options['dbname']; $dbhost = $options['dbhost']; $dbtableprefix = isset($options['dbtableprefix']) ? $options['dbtableprefix'] : 'oc_'; - + OC_Config::setValue('dbname', $dbname); OC_Config::setValue('dbhost', $dbhost); OC_Config::setValue('dbuser', $dbuser); @@ -176,7 +176,7 @@ class OC_Setup { ); return $error; } - } + } else { //delete the old sqlite database first, might cause infinte loops otherwise if(file_exists("$datadir/owncloud.db")) { @@ -199,7 +199,7 @@ class OC_Setup { OC_Appconfig::setValue('core', 'lastupdatedat', microtime(true)); OC_AppConfig::setValue('core', 'remote_core.css', '/core/minimizer.php'); OC_AppConfig::setValue('core', 'remote_core.js', '/core/minimizer.php'); - + OC_Group::createGroup('admin'); OC_Group::addToGroup($username, 'admin'); OC_User::login($username, $password); @@ -298,7 +298,7 @@ class OC_Setup { $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)), + throw new DatabaseSetupException($l->t("MySQL user '%s'@'%%' already exists", array($name)), $l->t("Drop this user from MySQL.")); } } @@ -570,7 +570,7 @@ class OC_Setup { $result = oci_execute($stmt); if(!$result) { $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '
'; - $entry .= $l->t('Offending command was: "%s", name: %s, password: %s', + $entry .= $l->t('Offending command was: "%s", name: %s, password: %s', array($query, $name, $password)) . '
'; echo($entry); } @@ -602,7 +602,7 @@ class OC_Setup { $result = oci_execute($stmt); if(!$result) { $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '
'; - $entry .= $l->t('Offending command was: "%s", name: %s, password: %s', + $entry .= $l->t('Offending command was: "%s", name: %s, password: %s', array($query, $name, $password)) . '
'; echo($entry); } @@ -611,15 +611,15 @@ class OC_Setup { private static function setupMSSQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix) { //check if the database user has admin right $masterConnectionInfo = array( "Database" => "master", "UID" => $dbuser, "PWD" => $dbpass); - + $masterConnection = @sqlsrv_connect($dbhost, $masterConnectionInfo); if(!$masterConnection) { - $entry = null; - if( ($errors = sqlsrv_errors() ) != null) { - $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; - } else { - $entry = ''; - } + $entry = null; + if( ($errors = sqlsrv_errors() ) != null) { + $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; + } else { + $entry = ''; + } throw new Exception('MS SQL username and/or password not valid: '.$entry); } @@ -627,17 +627,17 @@ class OC_Setup { OC_Config::setValue('dbpassword', $dbpass); self::mssql_createDBLogin($dbuser, $dbpass, $masterConnection); - + self::mssql_createDatabase($dbname, $masterConnection); - + self::mssql_createDBUser($dbuser, $dbname, $masterConnection); sqlsrv_close($masterConnection); self::mssql_createDatabaseStructure($dbhost, $dbname, $dbuser, $dbpass, $dbtableprefix); - } + } - private static function mssql_createDBLogin($name, $password, $connection) { + private static function mssql_createDBLogin($name, $password, $connection) { $query = "SELECT * FROM master.sys.server_principals WHERE name = '".$name."';"; $result = sqlsrv_query($connection, $query); if ($result === false) { @@ -650,7 +650,7 @@ class OC_Setup { echo($entry); } else { $row = sqlsrv_fetch_array($result); - + if ($row === false) { if ( ($errors = sqlsrv_errors() ) != null) { $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; @@ -671,7 +671,7 @@ class OC_Setup { } $entry.='Offending command was: '.$query.'
'; echo($entry); - } + } } } } @@ -690,7 +690,7 @@ class OC_Setup { echo($entry); } else { $row = sqlsrv_fetch_array($result); - + if ($row === false) { if ( ($errors = sqlsrv_errors() ) != null) { $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; @@ -698,7 +698,7 @@ class OC_Setup { $entry = ''; } $entry.='Offending command was: '.$query.'
'; - echo($entry); + echo($entry); } else { if ($row == null) { $query = "USE [".$dbname."]; CREATE USER [".$name."] FOR LOGIN [".$name."];"; @@ -711,7 +711,7 @@ class OC_Setup { } $entry.='Offending command was: '.$query.'
'; echo($entry); - } + } } $query = "USE [".$dbname."]; EXEC sp_addrolemember 'db_owner', '".$name."';"; @@ -726,9 +726,9 @@ class OC_Setup { echo($entry); } } - } + } } - + private static function mssql_createDatabase($dbname, $connection) { $query = "CREATE DATABASE IF NOT EXISTS [".$dbname."];"; $result = sqlsrv_query($connection, $query); @@ -736,12 +736,12 @@ class OC_Setup { if ( ($errors = sqlsrv_errors() ) != null) { $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; } else { - $entry = ''; + $entry = ''; } $entry.='Offending command was: '.$query.'
'; echo($entry); } - } + } // private static function setupMSSQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $username) { @@ -760,10 +760,10 @@ class OC_Setup { $entry = ''; } $entry.='Offending command was: '.$query.'
'; - echo($entry); + echo($entry); } else { $row = sqlsrv_fetch_array($result); - + if ($row === false) { if ( ($errors = sqlsrv_errors() ) != null) { $entry='DB Error: "'.print_r(sqlsrv_errors()).'"
'; @@ -771,17 +771,17 @@ class OC_Setup { $entry = ''; } $entry.='Offending command was: '.$query.'
'; - echo($entry); + echo($entry); } else { if ($row == null) { OC_DB::createDbFromStructure('db_structure.xml'); } } } - + sqlsrv_close($connection); } - + /** * create .htaccess files for apache hosts */ From 98d7e0f7cdaa53b3a8906d1a8f497f237130b44d Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Tue, 12 Feb 2013 01:14:53 +0100 Subject: [PATCH 08/10] write any database messages / non critical errors to the log - don't pollute the browser --- lib/setup.php | 65 ++++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/lib/setup.php b/lib/setup.php index a71e906a85..128c53269e 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -279,10 +279,11 @@ class OC_Setup { if(!$result) { $entry = $l->t('DB Error: "%s"', array(mysql_error($connection))) . '
'; $entry .= $l->t('Offending command was: "%s"', array($query)) . '
'; - echo($entry); + \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN); } $query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'"; - $result = mysql_query($query, $connection); //this query will fail if there aren't the right permissons, ignore the error + //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) { @@ -381,7 +382,7 @@ class OC_Setup { if(!$result) { $entry = $l->t('DB Error: "%s"', array(pg_last_error($connection))) . '
'; $entry .= $l->t('Offending command was: "%s"', array($query)) . '
'; - echo($entry); + \OC_Log::write('setup.pg', $entry, \OC_Log::WARN); } if(! pg_fetch_row($result)) { //The database does not exists... let's create it @@ -390,11 +391,11 @@ class OC_Setup { if(!$result) { $entry = $l->t('DB Error: "%s"', array(pg_last_error($connection))) . '
'; $entry .= $l->t('Offending command was: "%s"', array($query)) . '
'; - echo($entry); + \OC_Log::write('setup.pg', $entry, \OC_Log::WARN); } else { $query = "REVOKE ALL PRIVILEGES ON DATABASE \"$e_name\" FROM PUBLIC"; - $result = pg_query($connection, $query); + pg_query($connection, $query); } } } @@ -408,7 +409,7 @@ class OC_Setup { if(!$result) { $entry = $l->t('DB Error: "%s"', array(pg_last_error($connection))) . '
'; $entry .= $l->t('Offending command was: "%s"', array($query)) . '
'; - echo($entry); + \OC_Log::write('setup.pg', $entry, \OC_Log::WARN); } if(! pg_fetch_row($result)) { @@ -418,7 +419,7 @@ class OC_Setup { if(!$result) { $entry = $l->t('DB Error: "%s"', array(pg_last_error($connection))) . '
'; $entry .= $l->t('Offending command was: "%s"', array($query)) . '
'; - echo($entry); + \OC_Log::write('setup.pg', $entry, \OC_Log::WARN); } } else { // change password of the existing role @@ -427,7 +428,7 @@ class OC_Setup { if(!$result) { $entry = $l->t('DB Error: "%s"', array(pg_last_error($connection))) . '
'; $entry .= $l->t('Offending command was: "%s"', array($query)) . '
'; - echo($entry); + \OC_Log::write('setup.pg', $entry, \OC_Log::WARN); } } } @@ -454,7 +455,7 @@ class OC_Setup { if (!$stmt) { $entry = $l->t('DB Error: "%s"', array(oci_last_error($connection))) . '
'; $entry .= $l->t('Offending command was: "%s"', array($query)) . '
'; - echo($entry); + \OC_Log::write('setup.oci', $entry, \OC_Log::WARN); } $result = oci_execute($stmt); if($result) { @@ -518,9 +519,9 @@ class OC_Setup { $un = $dbtableprefix.'users'; oci_bind_by_name($stmt, ':un', $un); if (!$stmt) { - $entry = $l->t('DB Error: "%s"', array(oci_last_error($connection))) . '
'; + $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '
'; $entry .= $l->t('Offending command was: "%s"', array($query)) . '
'; - echo($entry); + \OC_Log::write('setup.oci', $entry, \OC_Log::WARN); } $result = oci_execute($stmt); @@ -546,14 +547,14 @@ class OC_Setup { if (!$stmt) { $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '
'; $entry .= $l->t('Offending command was: "%s"', array($query)) . '
'; - echo($entry); + \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))) . '
'; $entry .= $l->t('Offending command was: "%s"', array($query)) . '
'; - echo($entry); + \OC_Log::write('setup.oci', $entry, \OC_Log::WARN); } if(! oci_fetch_row($stmt)) { @@ -564,7 +565,7 @@ class OC_Setup { if (!$stmt) { $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '
'; $entry .= $l->t('Offending command was: "%s"', array($query)) . '
'; - echo($entry); + \OC_Log::write('setup.oci', $entry, \OC_Log::WARN); } //oci_bind_by_name($stmt, ':un', $name); $result = oci_execute($stmt); @@ -572,7 +573,7 @@ class OC_Setup { $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '
'; $entry .= $l->t('Offending command was: "%s", name: %s, password: %s', array($query, $name, $password)) . '
'; - echo($entry); + \OC_Log::write('setup.oci', $entry, \OC_Log::WARN); } } else { // change password of the existing role $query = "ALTER USER :un IDENTIFIED BY :pw"; @@ -580,7 +581,7 @@ class OC_Setup { if (!$stmt) { $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '
'; $entry .= $l->t('Offending command was: "%s"', array($query)) . '
'; - echo($entry); + \OC_Log::write('setup.oci', $entry, \OC_Log::WARN); } oci_bind_by_name($stmt, ':un', $name); oci_bind_by_name($stmt, ':pw', $password); @@ -588,7 +589,7 @@ class OC_Setup { if(!$result) { $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '
'; $entry .= $l->t('Offending command was: "%s"', array($query)) . '
'; - echo($entry); + \OC_Log::write('setup.oci', $entry, \OC_Log::WARN); } } // grant necessary roles @@ -597,18 +598,20 @@ class OC_Setup { if (!$stmt) { $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '
'; $entry .= $l->t('Offending command was: "%s"', array($query)) . '
'; - echo($entry); + \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))) . '
'; $entry .= $l->t('Offending command was: "%s", name: %s, password: %s', array($query, $name, $password)) . '
'; - echo($entry); + \OC_Log::write('setup.oci', $entry, \OC_Log::WARN); } } private static function setupMSSQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix) { + $l = self::getTrans(); + //check if the database user has admin right $masterConnectionInfo = array( "Database" => "master", "UID" => $dbuser, "PWD" => $dbpass); @@ -620,7 +623,7 @@ class OC_Setup { } else { $entry = ''; } - throw new Exception('MS SQL username and/or password not valid: '.$entry); + throw new Exception($l->t('MS SQL username and/or password not valid: $s', array($entry))); } OC_Config::setValue('dbuser', $dbuser); @@ -647,7 +650,7 @@ class OC_Setup { $entry = ''; } $entry.='Offending command was: '.$query.'
'; - echo($entry); + \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN); } else { $row = sqlsrv_fetch_array($result); @@ -658,7 +661,7 @@ class OC_Setup { $entry = ''; } $entry.='Offending command was: '.$query.'
'; - echo($entry); + \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN); } else { if ($row == null) { $query = "CREATE LOGIN [".$name."] WITH PASSWORD = '".$password."';"; @@ -670,7 +673,7 @@ class OC_Setup { $entry = ''; } $entry.='Offending command was: '.$query.'
'; - echo($entry); + \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN); } } } @@ -687,7 +690,7 @@ class OC_Setup { $entry = ''; } $entry.='Offending command was: '.$query.'
'; - echo($entry); + \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN); } else { $row = sqlsrv_fetch_array($result); @@ -698,7 +701,7 @@ class OC_Setup { $entry = ''; } $entry.='Offending command was: '.$query.'
'; - echo($entry); + \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN); } else { if ($row == null) { $query = "USE [".$dbname."]; CREATE USER [".$name."] FOR LOGIN [".$name."];"; @@ -710,7 +713,7 @@ class OC_Setup { $entry = ''; } $entry.='Offending command was: '.$query.'
'; - echo($entry); + \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN); } } @@ -723,7 +726,7 @@ class OC_Setup { $entry = ''; } $entry.='Offending command was: '.$query.'
'; - echo($entry); + \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN); } } } @@ -739,12 +742,10 @@ class OC_Setup { $entry = ''; } $entry.='Offending command was: '.$query.'
'; - echo($entry); + \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN); } } - // private static function setupMSSQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $username) { - private static function mssql_createDatabaseStructure($dbhost, $dbname, $dbuser, $dbpass, $dbtableprefix) { $connectionInfo = array( "Database" => $dbname, "UID" => $dbuser, "PWD" => $dbpass); @@ -760,7 +761,7 @@ class OC_Setup { $entry = ''; } $entry.='Offending command was: '.$query.'
'; - echo($entry); + \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN); } else { $row = sqlsrv_fetch_array($result); @@ -771,7 +772,7 @@ class OC_Setup { $entry = ''; } $entry.='Offending command was: '.$query.'
'; - echo($entry); + \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN); } else { if ($row == null) { OC_DB::createDbFromStructure('db_structure.xml'); From bcbf2e667badaae382911f9304d0811b17167af6 Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Tue, 12 Feb 2013 01:17:01 +0100 Subject: [PATCH 09/10] fixing sql server syntax for database creation --- lib/setup.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/setup.php b/lib/setup.php index 128c53269e..4a195c3450 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -733,7 +733,7 @@ class OC_Setup { } private static function mssql_createDatabase($dbname, $connection) { - $query = "CREATE DATABASE IF NOT EXISTS [".$dbname."];"; + $query = "CREATE DATABASE [".$dbname."];"; $result = sqlsrv_query($connection, $query); if (!$result || $result === false) { if ( ($errors = sqlsrv_errors() ) != null) { From 78a3625ddfc67e7e6743a2ff6fd31e1566b174c8 Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Thu, 14 Feb 2013 21:59:24 +0100 Subject: [PATCH 10/10] final adoptions for mssql connectivity --- lib/db.php | 7 +++++++ lib/files/cache/cache.php | 6 +++--- tests/lib/dbschema.php | 12 +++++++++--- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/db.php b/lib/db.php index 3ccd51737a..903f76c8c0 100644 --- a/lib/db.php +++ b/lib/db.php @@ -404,6 +404,13 @@ class OC_DB { $query = self::prepare('SELECT lastval() AS id'); $row = $query->execute()->fetchRow(); return $row['id']; + } + if( $type == 'mssql' ) { + if($table !== null) { + $prefix = OC_Config::getValue( "dbtableprefix", "oc_" ); + $table = str_replace( '*PREFIX*', $prefix, $table ); + } + return self::$connection->lastInsertId($table); }else{ if($table !== null) { $prefix = OC_Config::getValue( "dbtableprefix", "oc_" ); diff --git a/lib/files/cache/cache.php b/lib/files/cache/cache.php index dcb6e8fd39..b61f1de448 100644 --- a/lib/files/cache/cache.php +++ b/lib/files/cache/cache.php @@ -56,7 +56,7 @@ class Cache { } else { $query = \OC_DB::prepare('INSERT INTO `*PREFIX*storages`(`id`) VALUES(?)'); $query->execute(array($this->storageId)); - $this->numericId = \OC_DB::insertid('*PREFIX*filecache'); + $this->numericId = \OC_DB::insertid('*PREFIX*storages'); } } @@ -493,8 +493,8 @@ class Cache { */ public function getIncomplete() { $query = \OC_DB::prepare('SELECT `path` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC LIMIT 1'); - $query->execute(array($this->numericId)); - if ($row = $query->fetchRow()) { + $result = $query->execute(array($this->numericId)); + if ($row = $result->fetchRow()) { return $row['path']; } else { return false; diff --git a/tests/lib/dbschema.php b/tests/lib/dbschema.php index fb60ce7dbb..e20a04ef7f 100644 --- a/tests/lib/dbschema.php +++ b/tests/lib/dbschema.php @@ -91,9 +91,15 @@ class Test_DBSchema extends PHPUnit_Framework_TestCase { break; case 'pgsql': $sql = "SELECT tablename AS table_name, schemaname AS schema_name " - . "FROM pg_tables WHERE schemaname NOT LIKE 'pg_%' " - . "AND schemaname != 'information_schema' " - . "AND tablename = '".$table."'"; + . "FROM pg_tables WHERE schemaname NOT LIKE 'pg_%' " + . "AND schemaname != 'information_schema' " + . "AND tablename = '".$table."'"; + $query = OC_DB::prepare($sql); + $result = $query->execute(array()); + $exists = $result && $result->fetchOne(); + break; + case 'mssql': + $sql = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{$table}'"; $query = OC_DB::prepare($sql); $result = $query->execute(array()); $exists = $result && $result->fetchOne();