ENHANCEMENT Better validation checks in SQLiteDatabaseConfigurationHelper

This commit is contained in:
Ingo Schommer 2010-03-15 07:01:54 +00:00
parent 4443df5171
commit 204e3efd38

View File

@ -59,32 +59,45 @@ class SQLiteDatabaseConfigurationHelper implements DatabaseConfigurationHelper {
public function requireDatabaseConnection($databaseConfig) { public function requireDatabaseConnection($databaseConfig) {
$success = false; $success = false;
$error = ''; $error = '';
// arg validation
if(!isset($databaseConfig['path']) || !$databaseConfig['path']) return array(
'success' => false,
'error' => sprintf('Invalid path: "%s"', $databaseConfig['path'])
);
$path = $databaseConfig['path']; $path = $databaseConfig['path'];
if($path && $databaseConfig['database']) {
// create and secure db directory if(!isset($databaseConfig['database']) || !$databaseConfig['database']) return array(
if(!file_exists($path)) { 'success' => false,
self::create_db_dir($path); 'error' => sprintf('Invalid database name: "%s"', $databaseConfig['database'])
} );
self::secure_db_dir($path);
// create and secure db directory
$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)
);
$file = $path . '/' . $databaseConfig['database']; $file = $path . '/' . $databaseConfig['database'];
$file = preg_replace('/\/$/', '', $file); $file = preg_replace('/\/$/', '', $file);
if($databaseConfig['type'] == 'SQLitePDODatabase' || version_compare(phpversion(), '5.3.0', '<')) { if($databaseConfig['type'] == 'SQLitePDODatabase' || version_compare(phpversion(), '5.3.0', '<')) {
$conn = @(new PDO("sqlite:$file")); $conn = @(new PDO("sqlite:$file"));
} else { } else {
$conn = @(new SQLite3($file, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE)); $conn = @(new SQLite3($file, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE));
} }
if($conn) { if($conn) {
$success = true; $success = true;
} else {
$success = false;
$error = '';
}
} else { } else {
$success = false; $success = false;
$error = '';
} }
@ -150,6 +163,6 @@ class SQLiteDatabaseConfigurationHelper implements DatabaseConfigurationHelper {
* @return boolean * @return boolean
*/ */
public static function secure_db_dir($path) { public static function secure_db_dir($path) {
file_put_contents($path . '/.htaccess', 'deny from all'); return (is_writeable($path)) ? file_put_contents($path . '/.htaccess', 'deny from all') : false;
} }
} }