2010-04-14 05:32:30 +02:00
|
|
|
<?php
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2010-04-14 05:32:30 +02:00
|
|
|
/**
|
|
|
|
* 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 {
|
|
|
|
|
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Create a connection of the appropriate type
|
2012-01-11 16:05:19 +01:00
|
|
|
*
|
2013-06-21 00:32:08 +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-04-14 05:32:30 +02:00
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
protected function createConnection($databaseConfig, &$error) {
|
|
|
|
$error = null;
|
|
|
|
try {
|
|
|
|
switch($databaseConfig['type']) {
|
|
|
|
case 'MySQLDatabase':
|
|
|
|
$conn = @new MySQLi($databaseConfig['server'], $databaseConfig['username'],
|
|
|
|
$databaseConfig['password']);
|
|
|
|
if($conn && empty($conn->connect_errno)) {
|
|
|
|
$conn->query("SET sql_mode = 'ANSI'");
|
|
|
|
return $conn;
|
|
|
|
} else {
|
|
|
|
$error = ($conn->connect_errno)
|
|
|
|
? $conn->connect_error
|
|
|
|
: 'Unknown connection error';
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
case 'MySQLPDODatabase':
|
|
|
|
// May throw a PDOException if fails
|
|
|
|
$conn = @new PDO('mysql:host='.$databaseConfig['server'], $databaseConfig['username'],
|
|
|
|
$databaseConfig['password']);
|
|
|
|
if($conn) {
|
|
|
|
$conn->query("SET sql_mode = 'ANSI'");
|
|
|
|
return $conn;
|
|
|
|
} else {
|
|
|
|
$error = 'Unknown connection error';
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
$error = 'Invalid connection type';
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} catch(Exception $ex) {
|
|
|
|
$error = $ex->getMessage();
|
|
|
|
return null;
|
|
|
|
}
|
2010-04-14 05:32:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Helper function to quickly extract a column from a mysqi_result
|
|
|
|
*
|
|
|
|
* @param mixed $results mysqli_result or enumerable list of rows
|
|
|
|
* @return array Resulting data
|
2010-04-14 05:32:30 +02:00
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
protected function column($results) {
|
|
|
|
$array = array();
|
|
|
|
if($results instanceof mysqli_result) {
|
|
|
|
while($row = $results->fetch_array()) {
|
|
|
|
$array[] = $row[0];
|
|
|
|
}
|
2012-05-19 01:29:25 +02:00
|
|
|
} else {
|
2013-06-21 00:32:08 +02:00
|
|
|
foreach($results as $row) {
|
|
|
|
$array[] = $row[0];
|
|
|
|
}
|
2010-04-14 05:32:30 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requireDatabaseFunctions($databaseConfig) {
|
|
|
|
$data = DatabaseAdapterRegistry::get_adapter($databaseConfig['type']);
|
|
|
|
return !empty($data['supported']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requireDatabaseServer($databaseConfig) {
|
|
|
|
$connection = $this->createConnection($databaseConfig, $error);
|
|
|
|
$success = !empty($connection);
|
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:50:06 +02:00
|
|
|
public function getDatabaseVersion($databaseConfig) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$conn = $this->createConnection($databaseConfig, $error);
|
|
|
|
if(!$conn) {
|
|
|
|
return false;
|
|
|
|
} elseif($conn instanceof MySQLi) {
|
|
|
|
return $conn->server_info;
|
|
|
|
} elseif($conn instanceof PDO) {
|
|
|
|
return $conn->getAttribute(PDO::ATTR_SERVER_VERSION);
|
2010-10-15 04:54:18 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
return false;
|
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.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2010-10-15 03:10:10 +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) {
|
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
|
|
|
public function requireDatabaseConnection($databaseConfig) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$conn = $this->createConnection($databaseConfig, $error);
|
|
|
|
$success = !empty($conn);
|
|
|
|
|
|
|
|
// Check database name only uses valid characters
|
|
|
|
if($success && !$this->checkValidDatabaseName($databaseConfig['database'])) {
|
2010-04-14 05:44:38 +02:00
|
|
|
$success = false;
|
2013-06-21 00:32:08 +02:00
|
|
|
$error = 'Invalid characters in database name.';
|
2010-04-14 05:32:30 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Determines if a given database name is a valid Silverstripe name.
|
2012-01-11 16:05:19 +01:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string $database Candidate database name
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function checkValidDatabaseName($database) {
|
|
|
|
|
|
|
|
// Reject filename unsafe characters (cross platform)
|
|
|
|
if(preg_match('/[\\\\\/\?%\*\:\|"\<\>\.]+/', $database)) return false;
|
|
|
|
|
|
|
|
// Restricted to characters in the ASCII and Extended ASCII range
|
|
|
|
// @see http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
|
|
|
|
return preg_match('/^[\x{0001}-\x{FFFF}]+$/u', $database);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a specified grant proves that the current user has the specified
|
|
|
|
* permission on the specified database
|
|
|
|
*
|
|
|
|
* @param string $database Database name
|
|
|
|
* @param string $permission Permission to check for
|
|
|
|
* @param string $grant MySQL syntax grant to check within
|
|
|
|
* @return boolean
|
2010-04-14 05:32:30 +02:00
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public function checkDatabasePermissionGrant($database, $permission, $grant) {
|
|
|
|
// Filter out invalid database names
|
|
|
|
if(!$this->checkValidDatabaseName($database)) return false;
|
|
|
|
|
|
|
|
// Escape all valid database patterns (permission must exist on all tables)
|
|
|
|
$dbPattern = sprintf(
|
|
|
|
'((%s)|(%s)|(%s))',
|
|
|
|
preg_quote("\"$database\".*"),
|
|
|
|
preg_quote('"%".*'),
|
|
|
|
preg_quote('*.*')
|
|
|
|
);
|
|
|
|
$expression = '/GRANT[ ,\w]+((ALL PRIVILEGES)|('.$permission.'(?! ((VIEW)|(ROUTINE)))))[ ,\w]+ON '.
|
|
|
|
$dbPattern.'/i';
|
|
|
|
return preg_match($expression, $grant);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the current user has the specified permission on the specified database
|
|
|
|
*
|
|
|
|
* @param mixed $conn Connection object
|
|
|
|
* @param string $database Database name
|
|
|
|
* @param string $permission Permission to check
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function checkDatabasePermission($conn, $database, $permission) {
|
|
|
|
$grants = $this->column($conn->query("SHOW GRANTS FOR CURRENT_USER"));
|
|
|
|
foreach($grants as $grant) {
|
|
|
|
if($this->checkDatabasePermissionGrant($database, $permission, $grant)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-14 05:32:30 +02:00
|
|
|
public function requireDatabaseOrCreatePermissions($databaseConfig) {
|
2010-04-14 05:44:38 +02:00
|
|
|
$success = false;
|
|
|
|
$alreadyExists = false;
|
2013-06-21 00:32:08 +02:00
|
|
|
$conn = $this->createConnection($databaseConfig, $error);
|
|
|
|
if($conn) {
|
|
|
|
$list = $this->column($conn->query("SHOW DATABASES"));
|
|
|
|
if(in_array($databaseConfig['database'], $list)) {
|
2010-04-14 05:44:38 +02:00
|
|
|
$success = true;
|
2013-06-21 00:32:08 +02:00
|
|
|
$alreadyExists = true;
|
|
|
|
} else{
|
|
|
|
// If no database exists then check DDL permissions
|
2010-04-14 05:44:38 +02:00
|
|
|
$alreadyExists = false;
|
2013-06-21 00:32:08 +02:00
|
|
|
$success = $this->checkDatabasePermission($conn, $databaseConfig['database'], 'CREATE');
|
2010-04-14 05:32:30 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
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
|
|
|
public function requireDatabaseAlterPermissions($databaseConfig) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$conn = $this->createConnection($databaseConfig, $error);
|
|
|
|
$success = $this->checkDatabasePermission($conn, $databaseConfig['database'], 'ALTER');
|
2013-03-21 15:04:51 +01:00
|
|
|
return array(
|
|
|
|
'success' => $success,
|
|
|
|
'applies' => true
|
|
|
|
);
|
|
|
|
}
|
2010-04-14 05:44:38 +02:00
|
|
|
}
|