2010-02-11 08:38:51 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This is a helper class for the SS installer.
|
|
|
|
*
|
|
|
|
* It does all the specific checking for PostgreSQLDatabase
|
|
|
|
* to ensure that the configuration is setup correctly.
|
|
|
|
*
|
2010-02-11 12:04:53 +01:00
|
|
|
* @package postgresql
|
2010-02-11 08:38:51 +01:00
|
|
|
*/
|
|
|
|
class PostgreSQLDatabaseConfigurationHelper implements DatabaseConfigurationHelper {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that the database function pg_connect
|
|
|
|
* 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('pg_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 11:00:13 +01:00
|
|
|
* @return array Result - e.g. array('success' => true, 'error' => 'details of error')
|
2010-02-11 08:38:51 +01:00
|
|
|
*/
|
|
|
|
public function requireDatabaseServer($databaseConfig) {
|
2010-02-11 11:00:13 +01:00
|
|
|
$success = false;
|
2010-02-11 08:38:51 +01:00
|
|
|
$error = '';
|
|
|
|
$username = $databaseConfig['username'] ? $databaseConfig['username'] : '';
|
|
|
|
$password = $databaseConfig['password'] ? $databaseConfig['password'] : '';
|
|
|
|
$server = $databaseConfig['server'];
|
|
|
|
$userPart = $username ? " user=$username" : '';
|
|
|
|
$passwordPart = $password ? " password=$password" : '';
|
2010-03-26 03:32:04 +01:00
|
|
|
$connstring = "host=$server port=5432 dbname=postgres {$userPart}{$passwordPart}";
|
2010-02-11 08:38:51 +01:00
|
|
|
|
|
|
|
$conn = @pg_connect($connstring);
|
|
|
|
if($conn) {
|
2010-02-11 11:00:13 +01:00
|
|
|
$success = true;
|
2010-02-11 08:38:51 +01:00
|
|
|
} else {
|
2010-02-11 11:00:13 +01:00
|
|
|
$success = false;
|
2010-02-11 08:38:51 +01:00
|
|
|
$error = 'PostgreSQL requires a valid username and password to determine if the server exists.';
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
2010-02-11 11:00:13 +01:00
|
|
|
'success' => $success,
|
2010-02-11 08:38:51 +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:42 +01:00
|
|
|
* @return array Result - e.g. array('success' => true, 'error' => 'details of error')
|
2010-02-11 08:38:51 +01:00
|
|
|
*/
|
|
|
|
public function requireDatabaseConnection($databaseConfig) {
|
2010-02-11 11:00:13 +01:00
|
|
|
$success = false;
|
2010-02-11 08:38:51 +01:00
|
|
|
$error = '';
|
|
|
|
$username = $databaseConfig['username'] ? $databaseConfig['username'] : '';
|
|
|
|
$password = $databaseConfig['password'] ? $databaseConfig['password'] : '';
|
|
|
|
$server = $databaseConfig['server'];
|
|
|
|
$userPart = $username ? " user=$username" : '';
|
|
|
|
$passwordPart = $password ? " password=$password" : '';
|
2010-03-26 03:32:04 +01:00
|
|
|
$connstring = "host=$server port=5432 dbname=postgres {$userPart}{$passwordPart}";
|
2010-02-11 08:38:51 +01:00
|
|
|
|
|
|
|
$conn = @pg_connect($connstring);
|
|
|
|
if($conn) {
|
2010-02-11 11:00:13 +01:00
|
|
|
$success = true;
|
2010-02-11 08:38:51 +01:00
|
|
|
} else {
|
2010-02-11 11:00:13 +01:00
|
|
|
$success = false;
|
2010-02-11 08:38:51 +01:00
|
|
|
$error = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
2010-02-11 11:00:13 +01:00
|
|
|
'success' => $success,
|
2010-02-11 12:54:54 +01:00
|
|
|
'connection' => $conn,
|
2010-02-11 08:38:51 +01:00
|
|
|
'error' => $error
|
2010-05-15 06:03:25 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-05-26 07:14:05 +02:00
|
|
|
public function getDatabaseVersion($databaseConfig) {
|
|
|
|
$version = 0;
|
2010-05-15 06:03:25 +02:00
|
|
|
$username = $databaseConfig['username'] ? $databaseConfig['username'] : '';
|
|
|
|
$password = $databaseConfig['password'] ? $databaseConfig['password'] : '';
|
|
|
|
$server = $databaseConfig['server'];
|
|
|
|
$userPart = $username ? " user=$username" : '';
|
|
|
|
$passwordPart = $password ? " password=$password" : '';
|
|
|
|
$connstring = "host=$server port=5432 dbname=postgres {$userPart}{$passwordPart}";
|
|
|
|
$conn = @pg_connect($connstring);
|
2010-05-15 06:06:15 +02:00
|
|
|
$info = @pg_version($conn);
|
|
|
|
$version = ($info && isset($info['server'])) ? $info['server'] : null;
|
2010-05-15 06:03:25 +02:00
|
|
|
if(!$version) {
|
|
|
|
// fallback to using the version() function
|
|
|
|
$result = @pg_query($conn, "SELECT version()");
|
|
|
|
$row = @pg_fetch_array($result);
|
|
|
|
|
|
|
|
if($row && isset($row[0])) {
|
|
|
|
$parts = explode(' ', trim($row[0]));
|
|
|
|
// ASSUMPTION version number is the second part e.g. "PostgreSQL 8.4.3"
|
|
|
|
$version = trim($parts[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-26 07:14:05 +02:00
|
|
|
return $version;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that the PostgreSQL version is at least 8.3.
|
|
|
|
* @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-15 06:03:25 +02:00
|
|
|
if($version) {
|
|
|
|
$success = version_compare($version, '8.3', '>=');
|
|
|
|
if(!$success) {
|
|
|
|
$error = "Your PostgreSQL version is $version. It's recommended you use at least 8.3.";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$error = "Your PostgreSQL version could not be determined.";
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'success' => $success,
|
|
|
|
'error' => $error
|
2010-02-11 08:38:51 +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 11:00:13 +01:00
|
|
|
* @return array Result - e.g. array('success' => true, 'alreadyExists' => 'true')
|
2010-02-11 08:38:51 +01:00
|
|
|
*/
|
|
|
|
public function requireDatabaseOrCreatePermissions($databaseConfig) {
|
2010-02-11 11:00:13 +01:00
|
|
|
$success = false;
|
|
|
|
$alreadyExists = false;
|
2010-02-11 08:38:51 +01:00
|
|
|
$check = $this->requireDatabaseConnection($databaseConfig);
|
|
|
|
$conn = $check['connection'];
|
|
|
|
|
|
|
|
$result = pg_query($conn, "SELECT datname FROM pg_database WHERE datname = '$databaseConfig[database]'");
|
|
|
|
if(pg_fetch_array($result)) {
|
2010-02-11 11:00:13 +01:00
|
|
|
$success = true;
|
|
|
|
$alreadyExists = true;
|
2010-03-08 23:33:50 +01:00
|
|
|
} else {
|
2010-03-09 01:25:42 +01:00
|
|
|
if(@pg_query($conn, "CREATE DATABASE testing123")) {
|
|
|
|
pg_query($conn, "DROP DATABASE testing123");
|
2010-03-08 23:33:50 +01:00
|
|
|
$success = true;
|
|
|
|
$alreadyExists = false;
|
|
|
|
}
|
2010-02-11 08:38:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
2010-02-11 11:00:13 +01:00
|
|
|
'success' => $success,
|
2010-03-08 23:33:50 +01:00
|
|
|
'alreadyExists' => $alreadyExists
|
2010-02-11 08:38:51 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-02-11 11:00:13 +01:00
|
|
|
}
|