public function ajax_check_sqlite_credentials()
{
$db_file = $_POST['file'];
$xml = new \SimpleXMLElement('<response></response>');
// Missing anything?
if (!isset($db_file)) {
$xml->addChild('status', 0);
$xml_error = $xml->addChild('error');
$xml_error->addChild('id', '#databasefile');
$xml_error->addChild('message', _t('The database file was left empty.'));
}
if (!isset($xml_error)) {
if ($db_file == basename($db_file)) {
// The filename was given without a path
$db_file = Site::get_path('user', true) . $db_file;
}
if (!is_writable(dirname($db_file))) {
$xml->addChild('status', 0);
$xml_error = $xml->addChild('error');
$xml_error->addChild('id', '#databasefile');
$xml_error->addChild('message', _t('Cannot write to %s directory. SQLite requires that the directory that holds the DB file be writable by the web server.', array(dirname($db_file))));
} elseif (file_exists(Site::get_path('user', true) . $db_file) && !is_writable(Site::get_path('user', true) . $db_file)) {
$xml->addChild('status', 0);
$xml_error = $xml->addChild('error');
$xml_error->addChild('id', '#databasefile');
$xml_error->addChild('message', _t('Cannot write to %s. The SQLite data file is not writable by the web server.', array($db_file)));
} else {
// Can we connect to the DB?
$pdo = 'sqlite:' . $db_file;
$connect = DB::connect($pdo, null, null);
// Disconnect, but no longer delete the file - it could already have contents!
DB::disconnect();
switch ($connect) {
case true:
// We were able to connect to an existing database file.
$xml->addChild('status', 1);
break;
default:
// We can't create the database file, send an error message.
$xml->addChild('status', 0);
$xml_error = $xml->addChild('error');
// TODO: Add error codes handling for user-friendly messages
$xml_error->addChild('id', '#databasefile');
$xml_error->addChild('message', $connect->getMessage());
}
}
}
$xml = $xml->asXML();
ob_clean();
header("Content-type: application/xml");
header("Cache-Control: no-cache");
print $xml;
}