true, 'error' => 'details of error') */ public function requireDatabaseServer($databaseConfig) { $success = false; $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'] )); $errors = sqlsrv_errors(); if($errors) { $error .= "\n"; foreach($errors as $detail) { $error .= "\n" . @$detail['message'] . "\n"; } } } if($conn) { $success = true; } else { $success = false; if(!$error) $error = 'SQL Server requires a valid username and password to determine if the server exists.'; } return array( 'success' => $success, 'error' => $error ); } /** * Ensure a database connection is possible using credentials provided. * @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 requireDatabaseConnection($databaseConfig) { $success = false; $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'] )); $errors = sqlsrv_errors(); if($errors) { $error .= "\n"; foreach($errors as $detail) { $error .= "\n" . @$detail['message'] . "\n"; } } } if($conn) { $success = true; } else { $success = false; } return array( 'success' => $success, 'connection' => $conn, 'error' => $error ); } public function getDatabaseVersion($databaseConfig) { $version = 0; // Get the version using SERVERPROPERTY() function if(function_exists('mssql_connect')) { $conn = @mssql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password'], true); $result = @mssql_query("SELECT CONVERT(char(15), SERVERPROPERTY('ProductVersion'))", $conn); $row = @mssql_fetch_array($result); } else { $conn = @sqlsrv_connect($databaseConfig['server'], array( 'UID' => $databaseConfig['username'], 'PWD' => $databaseConfig['password'] )); $result = @sqlsrv_query($conn, "SELECT CONVERT(char(15), SERVERPROPERTY('ProductVersion'))"); $row = @sqlsrv_fetch_array($result); } if($row && isset($row[0])) { $version = trim($row[0]); } 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); if($version) { $success = version_compare($version, '10.00.2531', '>='); 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 ); } /** * 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 * @return array Result - e.g. array('success' => true, 'alreadyExists' => 'true') */ public function requireDatabaseOrCreatePermissions($databaseConfig) { $success = false; $alreadyExists = false; $check = $this->requireDatabaseConnection($databaseConfig); $conn = $check['connection']; if(function_exists('mssql_select_db')) { $exists = @mssql_select_db($databaseConfig['database'], $conn); } else { $exists = @sqlsrv_query($conn, "USE \"$databaseConfig[database]\""); } if($exists) { $success = true; $alreadyExists = true; } else { if(function_exists('mssql_query') && mssql_query("CREATE DATABASE testing123", $conn)) { mssql_query("DROP DATABASE testing123", $conn); $success = true; $alreadyExists = false; } elseif(function_exists('sqlsrv_query') && @sqlsrv_query($conn, "CREATE DATABASE testing123")) { sqlsrv_query($conn, "DROP DATABASE testing123"); $success = true; $alreadyExists = false; } } return array( 'success' => $success, 'alreadyExists' => $alreadyExists ); } }