diff --git a/base.php b/base.php
index 46b7a77..350ca34 100644
--- a/base.php
+++ b/base.php
@@ -889,15 +889,23 @@ abstract class Base
public static function getDbFileName ($database = NULL) {
return self::getDbDirectory ($database) .'metadata.db';
}
+
+ private static function error () {
+ header("location: checkconfig.php?err=1");
+ exit();
+ }
public static function getDb ($database = NULL) {
global $config;
if (is_null (self::$db)) {
try {
- self::$db = new PDO('sqlite:'. self::getDbFileName ($database));
+ if (file_exists (self::getDbFileName ($database))) {
+ self::$db = new PDO('sqlite:'. self::getDbFileName ($database));
+ } else {
+ self::error ();
+ }
} catch (Exception $e) {
- header("location: checkconfig.php?err=1");
- exit();
+ self::error ();
}
}
return self::$db;
diff --git a/checkconfig.php b/checkconfig.php
index 57f97a7..aac84bc 100644
--- a/checkconfig.php
+++ b/checkconfig.php
@@ -129,6 +129,7 @@ Please check
}
?>
+
Check if Calibre database file can be opened with PHP
@@ -182,6 +183,7 @@ Please check
+
diff --git a/index.php b/index.php
index f9070d0..4dcea57 100644
--- a/index.php
+++ b/index.php
@@ -23,6 +23,10 @@
exit ();
}
+ // Access the database ASAP to be sure it's readable, redirect if that's not the case.
+ // It has to be done before any header is sent.
+ $test = Base::getDb ();
+
header ("Content-Type:text/html;charset=utf-8");
$page = getURLParam ("page", Base::PAGE_INDEX);
$query = getURLParam ("query");