diff --git a/dev/install/DatabaseConfigurationHelper.php b/dev/install/DatabaseConfigurationHelper.php new file mode 100644 index 000000000..70f7029ea --- /dev/null +++ b/dev/install/DatabaseConfigurationHelper.php @@ -0,0 +1,42 @@ + true, 'error' => 'details of error') + */ + public function requireDatabaseServer($databaseConfig); + + /** + * Ensure a database connection is possible using credentials provided. + * The established connection resource is returned with the results as well. + * + * @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc + * @return array Result - e.g. array('okay' => true, 'connection' => mysql link, 'error' => 'details of error') + */ + public function requireDatabaseConnection($databaseConfig); + + /** + * 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('okay' => true, 'existsAlready' => 'true') + */ + public function requireDatabaseOrCreatePermissions($databaseConfig); + +} \ No newline at end of file diff --git a/dev/install/MySQLDatabaseConfigurationHelper.php b/dev/install/MySQLDatabaseConfigurationHelper.php new file mode 100644 index 000000000..4a4bea65a --- /dev/null +++ b/dev/install/MySQLDatabaseConfigurationHelper.php @@ -0,0 +1,93 @@ + true, 'error' => 'details of error') + */ + public function requireDatabaseServer($databaseConfig) { + $okay = false; + $conn = @mysql_connect($databaseConfig['server'], null, null); + if($conn || mysql_errno() < 2000) { + $okay = true; + } else { + $okay = false; + $error = mysql_error(); + } + return array( + 'okay' => $okay, + 'error' => $error + ); + } + + /** + * Ensure a database connection is possible using credentials provided. + * The established connection resource is returned with the results as well. + * + * @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc + * @return array Result - e.g. array('okay' => true, 'connection' => mysql link, 'error' => 'details of error') + */ + public function requireDatabaseConnection($databaseConfig) { + $okay = false; + $conn = @mysql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']); + if($conn) { + $okay = true; + } else { + $okay = false; + $error = mysql_error(); + } + return array( + 'okay' => $okay, + 'connection' => $conn, + '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('okay' => true, 'existsAlready' => 'true') + */ + public function requireDatabaseOrCreatePermissions($databaseConfig) { + $okay = false; + $existsAlready = false; + $conn = @mysql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']); + if(@mysql_select_db($databaseConfig['database'], $conn)) { + $okay = true; + $existsAlready = true; + } else { + if(@mysql_query("CREATE DATABASE testing123", $conn)) { + mysql_query("DROP DATABASE testing123", $conn); + $okay = true; + $existsAlready = false; + } + } + return array( + 'okay' => $okay, + 'existsAlready' => $existsAlready + ); + } + +} \ No newline at end of file