2010-03-10 00:58:36 +01:00
|
|
|
<?php
|
2013-04-03 06:49:59 +02:00
|
|
|
|
2010-03-10 00:58:36 +01:00
|
|
|
/**
|
|
|
|
* This is a helper class for the SS installer.
|
|
|
|
*
|
|
|
|
* It does all the specific checking for SQLiteDatabase
|
|
|
|
* to ensure that the configuration is setup correctly.
|
|
|
|
*
|
2013-04-03 06:49:59 +02:00
|
|
|
* @package SQLite3
|
2010-03-10 00:58:36 +01:00
|
|
|
*/
|
|
|
|
class SQLiteDatabaseConfigurationHelper implements DatabaseConfigurationHelper {
|
2013-04-03 06:49:59 +02:00
|
|
|
|
2010-03-10 00:58:36 +01:00
|
|
|
/**
|
2013-04-03 06:49:59 +02:00
|
|
|
* Create a connection of the appropriate type
|
2010-03-10 00:58:36 +01:00
|
|
|
*
|
2013-04-03 06:49:59 +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-03-10 00:58:36 +01:00
|
|
|
*/
|
2013-04-03 06:49:59 +02:00
|
|
|
protected function createConnection($databaseConfig, &$error) {
|
|
|
|
$error = null;
|
|
|
|
try {
|
|
|
|
if(!file_exists($databaseConfig['path'])) {
|
|
|
|
self::create_db_dir($databaseConfig['path']);
|
|
|
|
self::secure_db_dir($databaseConfig['path']);
|
|
|
|
}
|
|
|
|
$file = $databaseConfig['path'] . '/' . $databaseConfig['database'];
|
|
|
|
$conn = null;
|
|
|
|
|
|
|
|
switch($databaseConfig['type']) {
|
|
|
|
case 'SQLite3Database':
|
|
|
|
if(empty($databaseConfig['key'])) {
|
|
|
|
$conn = @new SQLite3($file, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
|
|
|
|
} else {
|
|
|
|
$conn = @new SQLite3($file, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $databaseConfig['key']);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'SQLite3PDODatabase':
|
|
|
|
// May throw a PDOException if fails
|
|
|
|
$conn = @new PDO("sqlite:$file");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$error = 'Invalid connection type';
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($conn) {
|
|
|
|
return $conn;
|
|
|
|
} else {
|
|
|
|
$error = 'Unknown connection error';
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} catch(Exception $ex) {
|
|
|
|
$error = $ex->getMessage();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-10 00:58:36 +01:00
|
|
|
public function requireDatabaseFunctions($databaseConfig) {
|
2013-04-03 06:49:59 +02:00
|
|
|
$data = DatabaseAdapterRegistry::get_adapter($databaseConfig['type']);
|
|
|
|
return !empty($data['supported']);
|
2010-03-10 00:58:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function requireDatabaseServer($databaseConfig) {
|
2010-03-15 07:43:49 +01:00
|
|
|
$path = $databaseConfig['path'];
|
2012-04-30 01:18:52 +02:00
|
|
|
$error = '';
|
2013-04-03 06:49:59 +02:00
|
|
|
$success = false;
|
2012-04-30 01:18:52 +02:00
|
|
|
|
2010-03-15 07:43:49 +01:00
|
|
|
if(!$path) {
|
|
|
|
$error = 'No database path provided';
|
2013-04-03 06:49:59 +02:00
|
|
|
} elseif(is_writable($path) || (!file_exists($path) && is_writable(dirname($path)))) {
|
|
|
|
// check if folder is writeable
|
2010-03-10 00:58:36 +01:00
|
|
|
$success = true;
|
|
|
|
} else {
|
2013-04-03 06:49:59 +02:00
|
|
|
$error = "Permission denied";
|
2010-03-10 00:58:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'success' => $success,
|
2013-04-03 06:49:59 +02:00
|
|
|
'error' => $error,
|
|
|
|
'path' => $path
|
2010-03-10 00:58:36 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure a database connection is possible using credentials provided.
|
2010-03-15 07:43:49 +01:00
|
|
|
*
|
|
|
|
* @todo Validate path
|
|
|
|
*
|
2010-03-10 00:58:36 +01:00
|
|
|
* @param array $databaseConfig Associative array of db configuration, e.g. "type", "path" etc
|
|
|
|
* @return array Result - e.g. array('success' => true, 'error' => 'details of error')
|
|
|
|
*/
|
|
|
|
public function requireDatabaseConnection($databaseConfig) {
|
2013-04-03 06:49:59 +02:00
|
|
|
// Do additional validation around file paths
|
|
|
|
if(empty($databaseConfig['path'])) return array(
|
2010-03-15 08:01:54 +01:00
|
|
|
'success' => false,
|
2013-04-03 06:49:59 +02:00
|
|
|
'error' => "Missing directory path"
|
2010-03-15 08:01:54 +01:00
|
|
|
);
|
2013-04-03 06:49:59 +02:00
|
|
|
if(empty($databaseConfig['database'])) return array(
|
2010-03-15 08:01:54 +01:00
|
|
|
'success' => false,
|
2013-04-03 06:49:59 +02:00
|
|
|
'error' => "Missing database filename"
|
2010-03-15 08:01:54 +01:00
|
|
|
);
|
|
|
|
|
2013-04-03 06:49:59 +02:00
|
|
|
// Create and secure db directory
|
|
|
|
$path = $databaseConfig['path'];
|
2010-03-15 08:01:54 +01:00
|
|
|
$dirCreated = self::create_db_dir($path);
|
|
|
|
if(!$dirCreated) return array(
|
|
|
|
'success' => false,
|
|
|
|
'error' => sprintf('Cannot create path: "%s"', $path)
|
|
|
|
);
|
|
|
|
$dirSecured = self::secure_db_dir($path);
|
|
|
|
if(!$dirSecured) return array(
|
|
|
|
'success' => false,
|
|
|
|
'error' => sprintf('Cannot secure path through .htaccess: "%s"', $path)
|
|
|
|
);
|
2010-03-15 07:43:49 +01:00
|
|
|
|
2013-04-03 06:49:59 +02:00
|
|
|
$conn = $this->createConnection($databaseConfig, $error);
|
|
|
|
$success = !empty($conn);
|
2010-03-10 00:58:36 +01:00
|
|
|
|
|
|
|
return array(
|
|
|
|
'success' => $success,
|
|
|
|
'connection' => $conn,
|
|
|
|
'error' => $error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-05-26 07:12:39 +02:00
|
|
|
public function getDatabaseVersion($databaseConfig) {
|
2010-05-15 06:59:18 +02:00
|
|
|
$version = 0;
|
2013-04-03 06:49:59 +02:00
|
|
|
|
|
|
|
switch($databaseConfig['type']) {
|
|
|
|
case 'SQLite3Database':
|
|
|
|
$info = SQLite3::version();
|
2010-05-15 06:59:18 +02:00
|
|
|
$version = trim($info['versionString']);
|
2013-04-03 06:49:59 +02:00
|
|
|
break;
|
|
|
|
case 'SQLite3PDODatabase':
|
|
|
|
// Fallback to using sqlite_version() query
|
|
|
|
$conn = $this->createConnection($databaseConfig, $error);
|
|
|
|
if($conn) {
|
|
|
|
$version = $conn->getAttribute(PDO::ATTR_SERVER_VERSION);
|
|
|
|
}
|
|
|
|
break;
|
2010-05-15 06:59:18 +02:00
|
|
|
}
|
|
|
|
|
2010-05-26 07:12:39 +02:00
|
|
|
return $version;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requireDatabaseVersion($databaseConfig) {
|
|
|
|
$success = false;
|
|
|
|
$error = '';
|
|
|
|
$version = $this->getDatabaseVersion($databaseConfig);
|
|
|
|
|
2010-05-15 06:59:18 +02:00
|
|
|
if($version) {
|
|
|
|
$success = version_compare($version, '3.3', '>=');
|
|
|
|
if(!$success) {
|
|
|
|
$error = "Your SQLite3 library version is $version. It's recommended you use at least 3.3.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-15 06:08:12 +02:00
|
|
|
return array(
|
2010-05-15 06:59:18 +02:00
|
|
|
'success' => $success,
|
|
|
|
'error' => $error
|
2010-05-15 06:08:12 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-03-10 00:58:36 +01:00
|
|
|
public function requireDatabaseOrCreatePermissions($databaseConfig) {
|
2013-04-03 06:49:59 +02:00
|
|
|
$conn = $this->createConnection($databaseConfig, $error);
|
|
|
|
$success = $alreadyExists = !empty($conn);
|
2010-03-10 00:58:36 +01:00
|
|
|
return array(
|
|
|
|
'success' => $success,
|
|
|
|
'alreadyExists' => $alreadyExists,
|
|
|
|
);
|
|
|
|
}
|
2010-03-15 07:43:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the provided directory and prepares it for
|
|
|
|
* storing SQLlite. Use {@link secure_db_dir()} to
|
|
|
|
* secure it against unauthorized access.
|
|
|
|
*
|
|
|
|
* @param String $path Absolute path, usually with a hidden folder.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function create_db_dir($path) {
|
2013-04-03 06:49:59 +02:00
|
|
|
return file_exists($path) || mkdir($path);
|
2010-03-15 07:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Secure the provided directory via web-access
|
|
|
|
* by placing a .htaccess file in it.
|
|
|
|
* This is just required if the database directory
|
|
|
|
* is placed within a publically accessible webroot (the
|
|
|
|
* default path is in a hidden folder within assets/).
|
|
|
|
*
|
|
|
|
* @param String $path Absolute path, containing a SQLite datatbase
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function secure_db_dir($path) {
|
2010-03-15 08:01:54 +01:00
|
|
|
return (is_writeable($path)) ? file_put_contents($path . '/.htaccess', 'deny from all') : false;
|
2010-03-15 07:43:49 +01:00
|
|
|
}
|
2013-03-23 15:31:09 +01:00
|
|
|
|
|
|
|
public function requireDatabaseAlterPermissions($databaseConfig) {
|
2013-04-03 06:49:59 +02:00
|
|
|
// no concept of table-specific permissions; If you can connect you can alter schema
|
|
|
|
return array(
|
|
|
|
'success' => true,
|
|
|
|
'applies' => false
|
|
|
|
);
|
2013-03-23 15:31:09 +01:00
|
|
|
}
|
2010-03-10 00:58:36 +01:00
|
|
|
}
|