server/lib/private/setup/mssql.php

185 lines
5.8 KiB
PHP
Raw Normal View History

<?php
namespace OC\Setup;
2013-04-02 20:01:23 +00:00
class MSSQL extends AbstractDatabase {
public $dbprettyname = 'MS SQL Server';
2013-04-02 20:01:23 +00:00
public function setupDatabase() {
//check if the database user has admin right
2013-04-02 20:01:23 +00:00
$masterConnectionInfo = array( "Database" => "master", "UID" => $this->dbuser, "PWD" => $this->dbpassword);
2013-04-02 20:01:23 +00:00
$masterConnection = @sqlsrv_connect($this->dbhost, $masterConnectionInfo);
if(!$masterConnection) {
$entry = null;
if( ($errors = sqlsrv_errors() ) != null) {
$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
} else {
$entry = '';
}
throw new \OC\DatabaseSetupException($this->trans->t('MS SQL username and/or password not valid: %s', array($entry)),
2013-04-02 20:01:23 +00:00
$this->trans->t('You need to enter either an existing account or the administrator.'));
}
\OC_Config::setValues([
'dbuser' => $this->dbuser,
'dbpassword' => $this->dbpassword,
]);
2013-04-02 20:01:23 +00:00
$this->createDBLogin($masterConnection);
2013-04-02 20:01:23 +00:00
$this->createDatabase($masterConnection);
2013-04-02 20:01:23 +00:00
$this->createDBUser($masterConnection);
sqlsrv_close($masterConnection);
2013-04-02 20:01:23 +00:00
$this->createDatabaseStructure();
}
2013-04-02 20:01:23 +00:00
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) {
$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-02 20:01:23 +00:00
$query = "CREATE LOGIN [".$this->dbuser."] WITH PASSWORD = '".$this->dbpassword."';";
$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-02 20:01:23 +00:00
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) {
$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-02 20:01:23 +00:00
$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) {
$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-02 20:01:23 +00:00
$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) {
$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-02 20:01:23 +00:00
private function createDatabase($connection) {
$query = "CREATE DATABASE [".$this->dbname."];";
$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-02 20:01:23 +00:00
private function createDatabaseStructure() {
$connectionInfo = array( "Database" => $this->dbname, "UID" => $this->dbuser, "PWD" => $this->dbpassword);
2013-04-02 20:01:23 +00:00
$connection = @sqlsrv_connect($this->dbhost, $connectionInfo);
//fill the database if needed
2013-04-02 20:01:23 +00:00
$query = "SELECT * FROM INFORMATION_SCHEMA.TABLES"
." WHERE TABLE_SCHEMA = '".$this->dbname."'"
2013-04-10 22:08:53 +00:00
." AND TABLE_NAME = '".$this->tableprefix."users'";
$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 18:19:51 +00:00
\OC_DB::createDbFromStructure($this->dbDefinitionFile);
}
}
}
sqlsrv_close($connection);
}
}