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 {
|
2013-04-03 06:27:11 +02:00
|
|
|
|
2010-02-11 08:38:51 +01:00
|
|
|
/**
|
2013-04-03 06:27:11 +02:00
|
|
|
* Create a connection of the appropriate type
|
2010-02-11 08:38:51 +01:00
|
|
|
*
|
2013-04-03 06:27:11 +02:00
|
|
|
* @param array $databaseConfig
|
|
|
|
* @param string $error Error message passed by value
|
|
|
|
* @return mixed|null Either the connection object, or null if error
|
2010-02-11 08:38:51 +01:00
|
|
|
*/
|
2013-04-03 06:27:11 +02:00
|
|
|
protected function createConnection($databaseConfig, &$error) {
|
|
|
|
$error = null;
|
|
|
|
$username = empty($databaseConfig['username']) ? '' : $databaseConfig['username'];
|
|
|
|
$password = empty($databaseConfig['password']) ? '' : $databaseConfig['password'];
|
2010-02-11 08:38:51 +01:00
|
|
|
$server = $databaseConfig['server'];
|
2013-04-03 06:27:11 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
switch($databaseConfig['type']) {
|
|
|
|
case 'PostgreSQLDatabase':
|
|
|
|
$userPart = $username ? " user=$username" : '';
|
|
|
|
$passwordPart = $password ? " password=$password" : '';
|
|
|
|
$connstring = "host=$server port=5432 dbname=postgres{$userPart}{$passwordPart}";
|
|
|
|
$conn = pg_connect($connstring);
|
|
|
|
break;
|
|
|
|
case 'PostgrePDODatabase':
|
|
|
|
// May throw a PDOException if fails
|
|
|
|
$conn = @new PDO('postgresql:host='.$server.';dbname=postgres;port=5432', $username, $password);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$error = 'Invalid connection type';
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} catch(Exception $ex) {
|
|
|
|
$error = $ex->getMessage();
|
|
|
|
return null;
|
|
|
|
}
|
2010-02-11 08:38:51 +01:00
|
|
|
if($conn) {
|
2013-04-03 06:27:11 +02:00
|
|
|
return $conn;
|
2010-02-11 08:38:51 +01:00
|
|
|
} else {
|
|
|
|
$error = 'PostgreSQL requires a valid username and password to determine if the server exists.';
|
2013-04-03 06:27:11 +02:00
|
|
|
return null;
|
2010-02-11 08:38:51 +01:00
|
|
|
}
|
2013-04-03 06:27:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function requireDatabaseFunctions($databaseConfig) {
|
|
|
|
$data = DatabaseAdapterRegistry::get_adapter($databaseConfig['type']);
|
|
|
|
return !empty($data['supported']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requireDatabaseServer($databaseConfig) {
|
|
|
|
$conn = $this->createConnection($databaseConfig, $error);
|
|
|
|
$success = !empty($conn);
|
2010-02-11 08:38:51 +01:00
|
|
|
return array(
|
2010-02-11 11:00:13 +01:00
|
|
|
'success' => $success,
|
2010-02-11 08:38:51 +01:00
|
|
|
'error' => $error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requireDatabaseConnection($databaseConfig) {
|
2013-04-03 06:27:11 +02:00
|
|
|
$conn = $this->createConnection($databaseConfig, $error);
|
|
|
|
$success = !empty($conn);
|
2010-02-11 08:38:51 +01:00
|
|
|
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) {
|
2013-04-03 06:27:11 +02:00
|
|
|
$conn = $this->createConnection($databaseConfig, $error);
|
|
|
|
if(!$conn) {
|
|
|
|
return false;
|
|
|
|
} elseif($conn instanceof PDO) {
|
|
|
|
return $conn->getAttribute(PDO::ATTR_SERVER_VERSION);
|
|
|
|
} elseif(is_resource($conn)) {
|
|
|
|
$info = pg_version($conn);
|
|
|
|
return $info['server'];
|
|
|
|
} else {
|
|
|
|
return false;
|
2010-05-15 06:03:25 +02:00
|
|
|
}
|
2010-05-26 07:14:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that the PostgreSQL version is at least 8.3.
|
2013-04-03 06:27:11 +02:00
|
|
|
*
|
2010-05-26 07:14:05 +02:00
|
|
|
* @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
|
|
|
);
|
|
|
|
}
|
2013-04-03 06:27:11 +02:00
|
|
|
|
2010-02-11 08:38:51 +01:00
|
|
|
/**
|
2013-04-03 06:27:11 +02:00
|
|
|
* Helper function to quote a string value
|
2010-02-11 08:38:51 +01:00
|
|
|
*
|
2013-04-03 06:27:11 +02:00
|
|
|
* @param mixed $conn Connection object/resource
|
|
|
|
* @param string $value Value to quote
|
|
|
|
* @return string Quoted strieng
|
|
|
|
*/
|
|
|
|
protected function quote($conn, $value) {
|
|
|
|
if($conn instanceof PDO) {
|
|
|
|
return $conn->quote($value);
|
|
|
|
} elseif(is_resource($conn)) {
|
|
|
|
return "'".pg_escape_string($conn, $value)."'";
|
|
|
|
} else {
|
|
|
|
user_error('Invalid database connection', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to execute a query
|
|
|
|
*
|
|
|
|
* @param mixed $conn Connection object/resource
|
|
|
|
* @param string $sql SQL string to execute
|
|
|
|
* @return array List of first value from each resulting row
|
2010-02-11 08:38:51 +01:00
|
|
|
*/
|
2013-04-03 06:27:11 +02:00
|
|
|
protected function query($conn, $sql) {
|
|
|
|
$items = array();
|
|
|
|
if($conn instanceof PDO) {
|
|
|
|
foreach($conn->query($sql) as $row) {
|
|
|
|
$items[] = $row[0];
|
|
|
|
}
|
|
|
|
} elseif(is_resource($conn)) {
|
|
|
|
$result = pg_query($conn, $sql);
|
|
|
|
while ($row = pg_fetch_row($result)) {
|
|
|
|
$items[] = $row[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
2010-02-11 08:38:51 +01:00
|
|
|
public function requireDatabaseOrCreatePermissions($databaseConfig) {
|
2010-02-11 11:00:13 +01:00
|
|
|
$success = false;
|
|
|
|
$alreadyExists = false;
|
2013-04-03 06:27:11 +02:00
|
|
|
$conn = $this->createConnection($databaseConfig, $error);
|
|
|
|
if($conn) {
|
|
|
|
// Check if db already exists
|
|
|
|
$existingDatabases = $this->query($conn, "SELECT datname FROM pg_database");
|
|
|
|
$alreadyExists = in_array($databaseConfig['database'], $existingDatabases);
|
|
|
|
if($alreadyExists) {
|
2010-03-08 23:33:50 +01:00
|
|
|
$success = true;
|
2013-04-03 06:27:11 +02:00
|
|
|
} else {
|
|
|
|
// Check if this user has create privileges
|
|
|
|
$allowedUsers = $this->query($conn, "select rolname from pg_authid where rolcreatedb = true;");
|
|
|
|
$success = in_array($databaseConfig['username'], $allowedUsers);
|
2010-03-08 23:33:50 +01:00
|
|
|
}
|
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
|
|
|
);
|
|
|
|
}
|
2013-04-03 06:27:11 +02:00
|
|
|
|
2013-03-23 14:59:34 +01:00
|
|
|
public function requireDatabaseAlterPermissions($databaseConfig) {
|
2013-04-03 06:27:11 +02:00
|
|
|
$conn = $this->createConnection($databaseConfig, $error);
|
|
|
|
if($conn) {
|
2015-03-14 00:14:54 +01:00
|
|
|
// if the account can even log in, it can alter tables
|
|
|
|
return array(
|
|
|
|
'success' => true,
|
|
|
|
'applies' => true
|
|
|
|
);
|
2013-04-03 06:27:11 +02:00
|
|
|
}
|
|
|
|
return array(
|
2015-03-14 00:14:54 +01:00
|
|
|
'success' => false,
|
2013-04-03 06:27:11 +02:00
|
|
|
'applies' => true
|
|
|
|
);
|
2013-03-23 14:59:34 +01:00
|
|
|
}
|
2010-02-11 11:00:13 +01:00
|
|
|
}
|