2010-02-11 08:38:10 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This is a helper class for the SS installer.
|
|
|
|
*
|
|
|
|
* It does all the specific checking for MSSQLDatabase
|
|
|
|
* to ensure that the configuration is setup correctly.
|
|
|
|
*
|
|
|
|
* @package mssql
|
|
|
|
*/
|
|
|
|
class MSSQLDatabaseConfigurationHelper implements DatabaseConfigurationHelper {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that the database function for connectivity is available.
|
|
|
|
* If it is, we assume the PHP module for this database has been setup correctly.
|
|
|
|
*
|
|
|
|
* @param array $databaseConfig Associative array of database configuration, e.g. "server", "username" etc
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function requireDatabaseFunctions($databaseConfig) {
|
|
|
|
return (function_exists('mssql_connect') || function_exists('sqlsrv_connect')) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that the database server exists.
|
|
|
|
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
|
2010-02-11 10:59:52 +01:00
|
|
|
* @return array Result - e.g. array('success' => true, 'error' => 'details of error')
|
2010-02-11 08:38:10 +01:00
|
|
|
*/
|
|
|
|
public function requireDatabaseServer($databaseConfig) {
|
2010-02-11 10:59:52 +01:00
|
|
|
$success = false;
|
2010-02-11 08:38:10 +01:00
|
|
|
$error = '';
|
|
|
|
|
|
|
|
if(function_exists('mssql_connect')) {
|
|
|
|
$conn = @mssql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password'], true);
|
|
|
|
} else {
|
|
|
|
$conn = @sqlsrv_connect($databaseConfig['server'], array(
|
|
|
|
'UID' => $databaseConfig['username'],
|
|
|
|
'PWD' => $databaseConfig['password']
|
|
|
|
));
|
2010-03-05 05:24:53 +01:00
|
|
|
|
|
|
|
$errors = sqlsrv_errors();
|
|
|
|
if($errors) {
|
|
|
|
$error .= "\n";
|
|
|
|
foreach($errors as $detail) {
|
|
|
|
$error .= "\n" . @$detail['message'] . "\n";
|
|
|
|
}
|
|
|
|
}
|
2010-02-11 08:38:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if($conn) {
|
2010-02-11 10:59:52 +01:00
|
|
|
$success = true;
|
2010-02-11 08:38:10 +01:00
|
|
|
} else {
|
2010-02-11 10:59:52 +01:00
|
|
|
$success = false;
|
2010-03-05 05:24:53 +01:00
|
|
|
if(!$error) $error = 'SQL Server requires a valid username and password to determine if the server exists.';
|
2010-02-11 08:38:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
2010-02-11 10:59:52 +01:00
|
|
|
'success' => $success,
|
2010-02-11 08:38:10 +01:00
|
|
|
'error' => $error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure a database connection is possible using credentials provided.
|
|
|
|
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
|
2010-02-11 11:08:21 +01:00
|
|
|
* @return array Result - e.g. array('success' => true, 'error' => 'details of error')
|
2010-02-11 08:38:10 +01:00
|
|
|
*/
|
|
|
|
public function requireDatabaseConnection($databaseConfig) {
|
2010-02-11 10:59:52 +01:00
|
|
|
$success = false;
|
2010-02-11 08:38:10 +01:00
|
|
|
$error = '';
|
2010-05-13 14:25:54 +02:00
|
|
|
|
2010-02-11 08:38:10 +01:00
|
|
|
if(function_exists('mssql_connect')) {
|
|
|
|
$conn = @mssql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password'], true);
|
|
|
|
} else {
|
|
|
|
$conn = @sqlsrv_connect($databaseConfig['server'], array(
|
|
|
|
'UID' => $databaseConfig['username'],
|
|
|
|
'PWD' => $databaseConfig['password']
|
|
|
|
));
|
2010-03-05 22:22:54 +01:00
|
|
|
|
|
|
|
$errors = sqlsrv_errors();
|
|
|
|
if($errors) {
|
|
|
|
$error .= "\n";
|
|
|
|
foreach($errors as $detail) {
|
|
|
|
$error .= "\n" . @$detail['message'] . "\n";
|
|
|
|
}
|
|
|
|
}
|
2010-02-11 08:38:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if($conn) {
|
2010-02-11 10:59:52 +01:00
|
|
|
$success = true;
|
2010-02-11 08:38:10 +01:00
|
|
|
} else {
|
2010-02-11 10:59:52 +01:00
|
|
|
$success = false;
|
2010-02-11 08:38:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
2010-02-11 10:59:52 +01:00
|
|
|
'success' => $success,
|
2010-02-11 12:52:12 +01:00
|
|
|
'connection' => $conn,
|
2010-02-11 08:38:10 +01:00
|
|
|
'error' => $error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-05-26 07:11:14 +02:00
|
|
|
public function getDatabaseVersion($databaseConfig) {
|
2010-05-13 14:25:54 +02:00
|
|
|
$version = 0;
|
|
|
|
|
|
|
|
// Get the version using SERVERPROPERTY() function
|
|
|
|
if(function_exists('mssql_connect')) {
|
|
|
|
$conn = @mssql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password'], true);
|
2010-05-13 23:52:57 +02:00
|
|
|
$result = @mssql_query("SELECT CONVERT(char(15), SERVERPROPERTY('ProductVersion'))", $conn);
|
2010-05-13 14:25:54 +02:00
|
|
|
$row = @mssql_fetch_array($result);
|
|
|
|
} else {
|
|
|
|
$conn = @sqlsrv_connect($databaseConfig['server'], array(
|
|
|
|
'UID' => $databaseConfig['username'],
|
|
|
|
'PWD' => $databaseConfig['password']
|
|
|
|
));
|
2010-05-13 23:52:57 +02:00
|
|
|
$result = @sqlsrv_query($conn, "SELECT CONVERT(char(15), SERVERPROPERTY('ProductVersion'))");
|
2010-05-13 14:25:54 +02:00
|
|
|
$row = @sqlsrv_fetch_array($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($row && isset($row[0])) {
|
2010-05-13 23:52:57 +02:00
|
|
|
$version = trim($row[0]);
|
2010-05-13 14:25:54 +02:00
|
|
|
}
|
|
|
|
|
2010-05-26 07:11:14 +02:00
|
|
|
return $version;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that the SQL Server version is at least 10.00.2531 (SQL Server 2008 SP1).
|
|
|
|
* @see http://www.sqlteam.com/article/sql-server-versions
|
|
|
|
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
|
|
|
|
* @return array Result - e.g. array('success' => true, 'error' => 'details of error')
|
|
|
|
*/
|
|
|
|
public function requireDatabaseVersion($databaseConfig) {
|
|
|
|
$success = false;
|
|
|
|
$error = '';
|
|
|
|
$version = $this->getDatabaseVersion($databaseConfig);
|
|
|
|
|
2010-05-13 14:25:54 +02:00
|
|
|
if($version) {
|
2010-05-13 14:27:25 +02:00
|
|
|
$success = version_compare($version, '10.00.2531', '>=');
|
2010-05-13 14:25:54 +02:00
|
|
|
if(!$success) {
|
|
|
|
$error = "Your SQL Server version is $version. It's recommended you use at least 10.00.2531 (SQL Server 2008 SP1).";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$error = "Your SQL Server version could not be determined.";
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'success' => $success,
|
|
|
|
'error' => $error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-02-11 08:38:10 +01:00
|
|
|
/**
|
|
|
|
* Ensure that the database connection is able to use an existing database,
|
|
|
|
* or be able to create one if it doesn't exist.
|
|
|
|
*
|
|
|
|
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
|
2010-02-11 10:59:52 +01:00
|
|
|
* @return array Result - e.g. array('success' => true, 'alreadyExists' => 'true')
|
2010-02-11 08:38:10 +01:00
|
|
|
*/
|
|
|
|
public function requireDatabaseOrCreatePermissions($databaseConfig) {
|
2010-02-11 10:59:52 +01:00
|
|
|
$success = false;
|
|
|
|
$alreadyExists = false;
|
2010-02-11 08:38:10 +01:00
|
|
|
|
|
|
|
$check = $this->requireDatabaseConnection($databaseConfig);
|
|
|
|
$conn = $check['connection'];
|
2010-03-11 21:09:00 +01:00
|
|
|
|
|
|
|
if(function_exists('mssql_select_db')) {
|
|
|
|
$exists = @mssql_select_db($databaseConfig['database'], $conn);
|
|
|
|
} else {
|
|
|
|
$exists = @sqlsrv_query($conn, "USE \"$databaseConfig[database]\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
if($exists) {
|
2010-02-11 10:59:52 +01:00
|
|
|
$success = true;
|
|
|
|
$alreadyExists = true;
|
2010-02-11 08:38:10 +01:00
|
|
|
} else {
|
|
|
|
if(function_exists('mssql_query') && mssql_query("CREATE DATABASE testing123", $conn)) {
|
|
|
|
mssql_query("DROP DATABASE testing123", $conn);
|
2010-02-11 10:59:52 +01:00
|
|
|
$success = true;
|
|
|
|
$alreadyExists = false;
|
2010-02-11 08:38:10 +01:00
|
|
|
} elseif(function_exists('sqlsrv_query') && @sqlsrv_query($conn, "CREATE DATABASE testing123")) {
|
|
|
|
sqlsrv_query($conn, "DROP DATABASE testing123");
|
2010-02-11 10:59:52 +01:00
|
|
|
$success = true;
|
|
|
|
$alreadyExists = false;
|
2010-02-11 08:38:10 +01:00
|
|
|
}
|
|
|
|
}
|
2010-03-11 21:09:00 +01:00
|
|
|
|
2010-02-11 08:38:10 +01:00
|
|
|
return array(
|
2010-02-11 10:59:52 +01:00
|
|
|
'success' => $success,
|
|
|
|
'alreadyExists' => $alreadyExists
|
2010-02-11 08:38:10 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-05-13 14:25:54 +02:00
|
|
|
}
|