2010-04-14 05:32:30 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This is a helper class for the SS installer.
|
2012-01-11 16:05:19 +01:00
|
|
|
*
|
2010-04-14 05:32:30 +02:00
|
|
|
* It does all the specific checking for MySQLDatabase
|
|
|
|
* to ensure that the configuration is setup correctly.
|
2012-01-11 16:05:19 +01:00
|
|
|
*
|
2012-06-20 23:59:16 +02:00
|
|
|
* @package framework
|
2010-04-23 05:26:32 +02:00
|
|
|
* @subpackage model
|
2010-04-14 05:32:30 +02:00
|
|
|
*/
|
|
|
|
class MySQLDatabaseConfigurationHelper 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.
|
2012-01-11 16:05:19 +01:00
|
|
|
*
|
2010-04-14 05:32:30 +02:00
|
|
|
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function requireDatabaseFunctions($databaseConfig) {
|
2012-04-28 02:13:58 +02:00
|
|
|
return class_exists('MySQLi');
|
2010-04-14 05:32:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that the database server exists.
|
|
|
|
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
|
2010-04-14 05:44:38 +02:00
|
|
|
* @return array Result - e.g. array('success' => true, 'error' => 'details of error')
|
2010-04-14 05:32:30 +02:00
|
|
|
*/
|
|
|
|
public function requireDatabaseServer($databaseConfig) {
|
2010-04-14 05:44:38 +02:00
|
|
|
$success = false;
|
2010-05-25 06:17:35 +02:00
|
|
|
$error = '';
|
2012-04-30 01:15:21 +02:00
|
|
|
$conn = @new MySQLi($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']);
|
2012-05-19 01:29:25 +02:00
|
|
|
if($conn && $conn->connect_errno) {
|
2010-04-14 05:44:38 +02:00
|
|
|
$success = false;
|
2012-05-19 01:29:25 +02:00
|
|
|
$error = $conn->connect_error;
|
|
|
|
} else {
|
|
|
|
$success = true;
|
2010-04-14 05:32:30 +02:00
|
|
|
}
|
2012-05-19 01:29:25 +02:00
|
|
|
|
2010-04-14 05:32:30 +02:00
|
|
|
return array(
|
2010-04-14 05:44:38 +02:00
|
|
|
'success' => $success,
|
2010-04-14 05:32:30 +02:00
|
|
|
'error' => $error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-10-15 04:54:18 +02:00
|
|
|
/**
|
|
|
|
* Get the database version for the MySQL connection, given the
|
|
|
|
* database parameters.
|
|
|
|
* @return mixed string Version number as string | boolean FALSE on failure
|
|
|
|
*/
|
2010-10-15 04:50:06 +02:00
|
|
|
public function getDatabaseVersion($databaseConfig) {
|
2012-04-28 02:13:58 +02:00
|
|
|
$conn = new MySQLi($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']);
|
2010-10-15 04:54:18 +02:00
|
|
|
if(!$conn) return false;
|
2012-04-28 02:13:58 +02:00
|
|
|
$version = $conn->server_info;
|
2010-10-15 04:54:18 +02:00
|
|
|
if(!$version) {
|
|
|
|
// fallback to trying a query
|
2012-04-28 02:13:58 +02:00
|
|
|
$result = $conn->query('SELECT VERSION()');
|
|
|
|
$row = $result->fetch_array();
|
2010-10-15 04:54:18 +02:00
|
|
|
if($row && isset($row[0])) {
|
|
|
|
$version = trim($row[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $version;
|
2010-10-15 04:50:06 +02:00
|
|
|
}
|
|
|
|
|
2010-10-15 03:10:10 +02:00
|
|
|
/**
|
|
|
|
* Ensure that the MySQL server version is at least 5.0.
|
|
|
|
* @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) {
|
2010-10-15 04:50:06 +02:00
|
|
|
$version = $this->getDatabaseVersion($databaseConfig);
|
2010-10-15 03:10:10 +02:00
|
|
|
$success = false;
|
|
|
|
$error = '';
|
|
|
|
if($version) {
|
|
|
|
$success = version_compare($version, '5.0', '>=');
|
|
|
|
if(!$success) {
|
|
|
|
$error = "Your MySQL server version is $version. It's recommended you use at least MySQL 5.0.";
|
|
|
|
}
|
2010-10-15 04:49:35 +02:00
|
|
|
} else {
|
|
|
|
$error = "Could not determine your MySQL version.";
|
2010-10-15 03:10:10 +02:00
|
|
|
}
|
|
|
|
return array(
|
|
|
|
'success' => $success,
|
|
|
|
'error' => $error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-04-14 05:32:30 +02:00
|
|
|
/**
|
|
|
|
* Ensure a database connection is possible using credentials provided.
|
|
|
|
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
|
2010-04-14 05:44:38 +02:00
|
|
|
* @return array Result - e.g. array('success' => true, 'error' => 'details of error')
|
2010-04-14 05:32:30 +02:00
|
|
|
*/
|
|
|
|
public function requireDatabaseConnection($databaseConfig) {
|
2010-04-14 05:44:38 +02:00
|
|
|
$success = false;
|
2010-05-25 06:17:35 +02:00
|
|
|
$error = '';
|
2012-04-28 02:13:58 +02:00
|
|
|
$conn = new MySQLi($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']);
|
2010-04-14 05:32:30 +02:00
|
|
|
if($conn) {
|
2010-04-14 05:44:38 +02:00
|
|
|
$success = true;
|
2010-04-14 05:32:30 +02:00
|
|
|
} else {
|
2010-04-14 05:44:38 +02:00
|
|
|
$success = false;
|
2012-04-28 02:13:58 +02:00
|
|
|
$error = ($conn) ? $conn->connect_error : '';
|
2010-04-14 05:32:30 +02:00
|
|
|
}
|
|
|
|
return array(
|
2010-04-14 05:44:38 +02:00
|
|
|
'success' => $success,
|
2010-04-14 05:32:30 +02:00
|
|
|
'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.
|
2012-01-11 16:05:19 +01:00
|
|
|
*
|
2010-04-14 05:32:30 +02:00
|
|
|
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
|
2010-04-14 05:44:38 +02:00
|
|
|
* @return array Result - e.g. array('success' => true, 'alreadyExists' => 'true')
|
2010-04-14 05:32:30 +02:00
|
|
|
*/
|
|
|
|
public function requireDatabaseOrCreatePermissions($databaseConfig) {
|
2010-04-14 05:44:38 +02:00
|
|
|
$success = false;
|
|
|
|
$alreadyExists = false;
|
2012-04-28 02:13:58 +02:00
|
|
|
$conn = new MySQLi($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']);
|
|
|
|
if($conn && $conn->select_db($databaseConfig['database'])) {
|
2010-04-14 05:44:38 +02:00
|
|
|
$success = true;
|
|
|
|
$alreadyExists = true;
|
2010-04-14 05:32:30 +02:00
|
|
|
} else {
|
2012-04-28 02:13:58 +02:00
|
|
|
if($conn && $conn->query('CREATE DATABASE testing123')) {
|
2012-05-05 01:22:28 +02:00
|
|
|
$conn->query('DROP DATABASE testing123');
|
2010-04-14 05:44:38 +02:00
|
|
|
$success = true;
|
|
|
|
$alreadyExists = false;
|
2010-04-14 05:32:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return array(
|
2010-04-14 05:44:38 +02:00
|
|
|
'success' => $success,
|
|
|
|
'alreadyExists' => $alreadyExists
|
2010-04-14 05:32:30 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-03-21 15:04:51 +01:00
|
|
|
/**
|
|
|
|
* Ensure we have permissions to alter tables.
|
|
|
|
*
|
|
|
|
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
|
|
|
|
* @return array Result - e.g. array('okay' => true, 'applies' => true), where applies is whether
|
|
|
|
* the test is relevant for the database
|
|
|
|
*/
|
|
|
|
public function requireDatabaseAlterPermissions($databaseConfig) {
|
|
|
|
$success = false;
|
|
|
|
$conn = new MySQLi($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']);
|
|
|
|
if($conn) {
|
|
|
|
if ($res = $conn->query('SHOW GRANTS')) {
|
|
|
|
// Annoyingly, MySQL 'escapes' the database, so we need to do it too.
|
|
|
|
$db = str_replace(array('%', '_', '`'), array('\%', '\_', '``'), $databaseConfig['database']);
|
|
|
|
while ($row = $res->fetch_array()) {
|
2013-03-24 19:53:42 +01:00
|
|
|
if (preg_match('/^GRANT (.+) ON (.+) TO/', $row[0], $matches)) {
|
2013-03-21 15:04:51 +01:00
|
|
|
// Need to change to an array of permissions, because ALTER is contained in ALTER ROUTINES.
|
|
|
|
$permission = array_map('trim', explode(',', $matches[1]));
|
|
|
|
$on_database = $matches[2];
|
|
|
|
// The use of both ` and " is because of ANSI mode.
|
|
|
|
if (in_array('ALL PRIVILEGES', $permission) and (
|
|
|
|
($on_database == '*.*') or ($on_database == '`' . $db . '`.*')
|
|
|
|
or ($on_database == '"' . $db . '".*'))) {
|
|
|
|
$success = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (in_array('ALTER', $permission) and (
|
|
|
|
($on_database == '*.*') or ($on_database == '`' . $db . '`.*')
|
|
|
|
or ($on_database == '"' . $db . '".*'))) {
|
|
|
|
$success = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array(
|
|
|
|
'success' => $success,
|
|
|
|
'applies' => true
|
|
|
|
);
|
|
|
|
}
|
2010-04-14 05:44:38 +02:00
|
|
|
}
|