2013-03-29 19:28:48 +04:00
|
|
|
<?php
|
2015-02-23 13:28:53 +03:00
|
|
|
/**
|
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Joas Schilling <nickvergessen@gmx.de>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
2013-03-29 19:28:48 +04:00
|
|
|
namespace OC\Setup;
|
|
|
|
|
2013-04-03 00:01:23 +04:00
|
|
|
class MSSQL extends AbstractDatabase {
|
2013-04-03 19:52:18 +04:00
|
|
|
public $dbprettyname = 'MS SQL Server';
|
|
|
|
|
2015-01-26 14:59:25 +03:00
|
|
|
public function setupDatabase($username) {
|
2013-03-29 19:28:48 +04:00
|
|
|
//check if the database user has admin right
|
2013-04-03 00:01:23 +04:00
|
|
|
$masterConnectionInfo = array( "Database" => "master", "UID" => $this->dbuser, "PWD" => $this->dbpassword);
|
2013-03-29 19:28:48 +04:00
|
|
|
|
2013-04-03 00:01:23 +04:00
|
|
|
$masterConnection = @sqlsrv_connect($this->dbhost, $masterConnectionInfo);
|
2013-03-29 19:28:48 +04:00
|
|
|
if(!$masterConnection) {
|
|
|
|
$entry = null;
|
|
|
|
if( ($errors = sqlsrv_errors() ) != null) {
|
|
|
|
$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
|
|
|
|
} else {
|
|
|
|
$entry = '';
|
|
|
|
}
|
2014-11-26 14:30:07 +03:00
|
|
|
throw new \OC\DatabaseSetupException($this->trans->t('MS SQL username and/or password not valid: %s', array($entry)),
|
2013-04-03 00:01:23 +04:00
|
|
|
$this->trans->t('You need to enter either an existing account or the administrator.'));
|
2013-03-29 19:28:48 +04:00
|
|
|
}
|
|
|
|
|
2015-01-23 13:13:47 +03:00
|
|
|
\OC_Config::setValues([
|
|
|
|
'dbuser' => $this->dbuser,
|
|
|
|
'dbpassword' => $this->dbpassword,
|
|
|
|
]);
|
2013-03-29 19:28:48 +04:00
|
|
|
|
2013-04-03 00:01:23 +04:00
|
|
|
$this->createDBLogin($masterConnection);
|
2013-03-29 19:28:48 +04:00
|
|
|
|
2013-04-03 00:01:23 +04:00
|
|
|
$this->createDatabase($masterConnection);
|
2013-03-29 19:28:48 +04:00
|
|
|
|
2013-04-03 00:01:23 +04:00
|
|
|
$this->createDBUser($masterConnection);
|
2013-03-29 19:28:48 +04:00
|
|
|
|
|
|
|
sqlsrv_close($masterConnection);
|
|
|
|
|
2013-04-03 00:01:23 +04:00
|
|
|
$this->createDatabaseStructure();
|
2013-03-29 19:28:48 +04:00
|
|
|
}
|
|
|
|
|
2013-04-03 00:01:23 +04:00
|
|
|
private function createDBLogin($connection) {
|
|
|
|
$query = "SELECT * FROM master.sys.server_principals WHERE name = '".$this->dbuser."';";
|
2013-03-29 19:28:48 +04:00
|
|
|
$result = sqlsrv_query($connection, $query);
|
|
|
|
if ($result === false) {
|
|
|
|
if ( ($errors = sqlsrv_errors() ) != null) {
|
|
|
|
$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
|
|
|
|
} else {
|
|
|
|
$entry = '';
|
|
|
|
}
|
|
|
|
$entry.='Offending command was: '.$query.'<br />';
|
|
|
|
\OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
|
|
|
|
} else {
|
|
|
|
$row = sqlsrv_fetch_array($result);
|
|
|
|
|
|
|
|
if ($row === false) {
|
|
|
|
if ( ($errors = sqlsrv_errors() ) != null) {
|
|
|
|
$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
|
|
|
|
} else {
|
|
|
|
$entry = '';
|
|
|
|
}
|
|
|
|
$entry.='Offending command was: '.$query.'<br />';
|
|
|
|
\OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
|
|
|
|
} else {
|
|
|
|
if ($row == null) {
|
2013-04-03 00:01:23 +04:00
|
|
|
$query = "CREATE LOGIN [".$this->dbuser."] WITH PASSWORD = '".$this->dbpassword."';";
|
2013-03-29 19:28:48 +04:00
|
|
|
$result = sqlsrv_query($connection, $query);
|
|
|
|
if (!$result or $result === false) {
|
|
|
|
if ( ($errors = sqlsrv_errors() ) != null) {
|
|
|
|
$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
|
|
|
|
} else {
|
|
|
|
$entry = '';
|
|
|
|
}
|
|
|
|
$entry.='Offending command was: '.$query.'<br />';
|
|
|
|
\OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-03 00:01:23 +04:00
|
|
|
private function createDBUser($connection) {
|
|
|
|
$query = "SELECT * FROM [".$this->dbname."].sys.database_principals WHERE name = '".$this->dbuser."';";
|
2013-03-29 19:28:48 +04:00
|
|
|
$result = sqlsrv_query($connection, $query);
|
|
|
|
if ($result === false) {
|
|
|
|
if ( ($errors = sqlsrv_errors() ) != null) {
|
|
|
|
$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
|
|
|
|
} else {
|
|
|
|
$entry = '';
|
|
|
|
}
|
|
|
|
$entry.='Offending command was: '.$query.'<br />';
|
|
|
|
\OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
|
|
|
|
} else {
|
|
|
|
$row = sqlsrv_fetch_array($result);
|
|
|
|
|
|
|
|
if ($row === false) {
|
|
|
|
if ( ($errors = sqlsrv_errors() ) != null) {
|
|
|
|
$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
|
|
|
|
} else {
|
|
|
|
$entry = '';
|
|
|
|
}
|
|
|
|
$entry.='Offending command was: '.$query.'<br />';
|
|
|
|
\OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
|
|
|
|
} else {
|
|
|
|
if ($row == null) {
|
2013-04-03 00:01:23 +04:00
|
|
|
$query = "USE [".$this->dbname."]; CREATE USER [".$this->dbuser."] FOR LOGIN [".$this->dbuser."];";
|
2013-03-29 19:28:48 +04:00
|
|
|
$result = sqlsrv_query($connection, $query);
|
|
|
|
if (!$result || $result === false) {
|
|
|
|
if ( ($errors = sqlsrv_errors() ) != null) {
|
|
|
|
$entry = 'DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
|
|
|
|
} else {
|
|
|
|
$entry = '';
|
|
|
|
}
|
|
|
|
$entry.='Offending command was: '.$query.'<br />';
|
|
|
|
\OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-03 00:01:23 +04:00
|
|
|
$query = "USE [".$this->dbname."]; EXEC sp_addrolemember 'db_owner', '".$this->dbuser."';";
|
2013-03-29 19:28:48 +04:00
|
|
|
$result = sqlsrv_query($connection, $query);
|
|
|
|
if (!$result || $result === false) {
|
|
|
|
if ( ($errors = sqlsrv_errors() ) != null) {
|
|
|
|
$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
|
|
|
|
} else {
|
|
|
|
$entry = '';
|
|
|
|
}
|
|
|
|
$entry.='Offending command was: '.$query.'<br />';
|
|
|
|
\OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-03 00:01:23 +04:00
|
|
|
private function createDatabase($connection) {
|
|
|
|
$query = "CREATE DATABASE [".$this->dbname."];";
|
2013-03-29 19:28:48 +04:00
|
|
|
$result = sqlsrv_query($connection, $query);
|
|
|
|
if (!$result || $result === false) {
|
|
|
|
if ( ($errors = sqlsrv_errors() ) != null) {
|
|
|
|
$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
|
|
|
|
} else {
|
|
|
|
$entry = '';
|
|
|
|
}
|
|
|
|
$entry.='Offending command was: '.$query.'<br />';
|
|
|
|
\OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-03 00:01:23 +04:00
|
|
|
private function createDatabaseStructure() {
|
|
|
|
$connectionInfo = array( "Database" => $this->dbname, "UID" => $this->dbuser, "PWD" => $this->dbpassword);
|
2013-03-29 19:28:48 +04:00
|
|
|
|
2013-04-03 00:01:23 +04:00
|
|
|
$connection = @sqlsrv_connect($this->dbhost, $connectionInfo);
|
2013-03-29 19:28:48 +04:00
|
|
|
|
|
|
|
//fill the database if needed
|
2013-04-03 00:01:23 +04:00
|
|
|
$query = "SELECT * FROM INFORMATION_SCHEMA.TABLES"
|
|
|
|
." WHERE TABLE_SCHEMA = '".$this->dbname."'"
|
2013-04-11 02:08:53 +04:00
|
|
|
." AND TABLE_NAME = '".$this->tableprefix."users'";
|
2013-03-29 19:28:48 +04:00
|
|
|
$result = sqlsrv_query($connection, $query);
|
|
|
|
if ($result === false) {
|
|
|
|
if ( ($errors = sqlsrv_errors() ) != null) {
|
|
|
|
$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
|
|
|
|
} else {
|
|
|
|
$entry = '';
|
|
|
|
}
|
|
|
|
$entry.='Offending command was: '.$query.'<br />';
|
|
|
|
\OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
|
|
|
|
} else {
|
|
|
|
$row = sqlsrv_fetch_array($result);
|
|
|
|
|
|
|
|
if ($row === false) {
|
|
|
|
if ( ($errors = sqlsrv_errors() ) != null) {
|
|
|
|
$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
|
|
|
|
} else {
|
|
|
|
$entry = '';
|
|
|
|
}
|
|
|
|
$entry.='Offending command was: '.$query.'<br />';
|
|
|
|
\OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
|
|
|
|
} else {
|
|
|
|
if ($row == null) {
|
2013-06-27 22:19:51 +04:00
|
|
|
\OC_DB::createDbFromStructure($this->dbDefinitionFile);
|
2013-03-29 19:28:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlsrv_close($connection);
|
|
|
|
}
|
|
|
|
}
|