Fix the redirect in case the database is not found. I also handle better the case were no file are actually found. Thanks to At Libitum. fix #116

This commit is contained in:
Sébastien Lucas 2013-11-25 17:10:43 +01:00
parent 26f5b36563
commit 4d9b3a4925
3 changed files with 17 additions and 3 deletions

View file

@ -889,15 +889,23 @@ abstract class Base
public static function getDbFileName ($database = NULL) { public static function getDbFileName ($database = NULL) {
return self::getDbDirectory ($database) .'metadata.db'; return self::getDbDirectory ($database) .'metadata.db';
} }
private static function error () {
header("location: checkconfig.php?err=1");
exit();
}
public static function getDb ($database = NULL) { public static function getDb ($database = NULL) {
global $config; global $config;
if (is_null (self::$db)) { if (is_null (self::$db)) {
try { 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) { } catch (Exception $e) {
header("location: checkconfig.php?err=1"); self::error ();
exit();
} }
} }
return self::$db; return self::$db;

View file

@ -129,6 +129,7 @@ Please check
} }
?> ?>
</article> </article>
<?php if (is_readable (Base::getDbFileName ($i))) { ?>
<article class="frontpage"> <article class="frontpage">
<h2>Check if Calibre database file can be opened with PHP</h2> <h2>Check if Calibre database file can be opened with PHP</h2>
<h4> <h4>
@ -182,6 +183,7 @@ Please check
</h4> </h4>
</article> </article>
<?php } ?> <?php } ?>
<?php } ?>
<?php $i++; } ?> <?php $i++; } ?>
</section> </section>
<footer></footer> <footer></footer>

View file

@ -23,6 +23,10 @@
exit (); 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"); header ("Content-Type:text/html;charset=utf-8");
$page = getURLParam ("page", Base::PAGE_INDEX); $page = getURLParam ("page", Base::PAGE_INDEX);
$query = getURLParam ("query"); $query = getURLParam ("query");