about summary refs log tree commit diff stats
path: root/backend.php
diff options
context:
space:
mode:
authorAndrew Dolgov <noreply@fakecake.org>2017-02-25 15:37:22 +0300
committerAndrew Dolgov <noreply@fakecake.org>2017-02-25 15:37:32 +0300
commitbc6e20d282e0cc6152fb3b7a99ef79aef08388d3 (patch)
treefec469e63bb6b6e03475e8faf1c019b9e8467626 /backend.php
parentf37c9975eeec34562c93f9a0b25ace14294d089f (diff)
downloadthe-epube-bc6e20d282e0cc6152fb3b7a99ef79aef08388d3.tar.gz
the-epube-bc6e20d282e0cc6152fb3b7a99ef79aef08388d3.tar.xz
use sqlite as a scratch db
store lastread pointer as a CFI
Diffstat (limited to 'backend.php')
-rw-r--r--backend.php60
1 files changed, 31 insertions, 29 deletions
diff --git a/backend.php b/backend.php
index 83977b7..4a19916 100644
--- a/backend.php
+++ b/backend.php
@@ -1,16 +1,15 @@
 <?php
 
 	require_once "config.php";
-	require_once "include/functions.php";
 
 	$op = $_REQUEST["op"];
 
 	header("Content-type: text/json");
 
-	$link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
-	init_connection($link);
+	$ldb = new SQLite3(SCRATCH_DB);
+	$ldb->busyTimeout(10*1000);
 
-	$owner = db_escape_string($_SERVER["PHP_AUTH_USER"]);
+	$owner = SQLite3::escapeString($_SERVER["PHP_AUTH_USER"]);
 
 	if (!$owner) {
 		print json_encode(["error" => "NOT_AUTHENTICATED"]);
@@ -72,10 +71,10 @@
 		$bookid = (int) $_REQUEST["id"];
 
 		if ($bookid) {
-			$result = db_query($link, "SELECT pagination FROM epube_pagination WHERE bookid = '$bookid' LIMIT 1");
+			$result = $ldb->query("SELECT pagination FROM epube_pagination WHERE bookid = '$bookid' LIMIT 1");
 
-			if (db_num_rows($result) != 0) {
-				print db_fetch_result($result, 0, "pagination");
+			if ($line = $result->fetchArray()) {
+				print $line["pagination"];
 			} else {
 				print json_encode(["error" => "NOT_FOUND"]);
 			}
@@ -84,80 +83,83 @@
 		break;
 	case "storepagination":
 		$bookid = (int) $_REQUEST["id"];
-		$payload = db_escape_string($_REQUEST["payload"]);
+		$payload = SQLite3::escapeString($_REQUEST["payload"]);
 		$total_pages = (int) $_REQUEST["total"];
 
 		if ($bookid && $payload && $total_pages) {
 
-			db_query($link, "BEGIN");
+			$ldb->query("BEGIN");
 
-			$result = db_query($link, "SELECT id FROM epube_pagination WHERE bookid = '$bookid' LIMIT 1");
+			$result = $ldb->query("SELECT id FROM epube_pagination WHERE bookid = '$bookid' LIMIT 1");
 
-			if (db_num_rows($result) != 0) {
-				$id = db_fetch_result($result, 0, "id");
+			if ($line = $result->fetchArray()) {
+				$id = $line["id"];
 
-				db_query($link, "UPDATE epube_pagination SET pagination = '$payload',
+				$ldb->query("UPDATE epube_pagination SET pagination = '$payload',
 					total_pages = '$total_pages' WHERE id = '$id'");
 
 			} else {
-				db_query($link, "INSERT INTO epube_pagination (bookid, pagination, total_pages) VALUES
+				$ldb->query("INSERT INTO epube_pagination (bookid, pagination, total_pages) VALUES
 					('$bookid', '$payload', '$total_pages')");
 
 			}
 
-			db_query($link, "COMMIT");
+			$ldb->query("COMMIT");
 		}
 
 		break;
 	case "getlastread":
 		$bookid = (int) $_REQUEST["id"];
 		$lastread = 0;
+		$lastcfi = "";
 
 		if ($bookid) {
 
-			$result = db_query($link, "SELECT id, lastread FROM epube_books
+			$result = $ldb->query("SELECT id, lastread, lastcfi FROM epube_books
 				WHERE bookid = '$bookid' AND owner = '$owner' LIMIT 1");
 
-			if (db_num_rows($result) != 0) {
-				$lastread = (int) db_fetch_result($result, 0, "lastread");
+			if ($line = $result->fetchArray()) {
+				$lastread = (int) $line["lastread"];
+				$lastcfi = $line["lastcfi"];
 			}
 		}
 
-		print json_encode(["lastread" => $lastread]);
+		print json_encode(["page" => $lastread, "cfi" => $lastcfi]);
 
 		break;
 
 	case "storelastread":
 		$page = (int) $_REQUEST["page"];
 		$bookid = (int) $_REQUEST["id"];
+		$cfi = SQLite3::escapeString($_REQUEST["cfi"]);
 
 		if ($page && $bookid) {
 
-			db_query($link, "BEGIN");
+			$ldb->query("BEGIN");
 
-			$result = db_query($link, "SELECT id, lastread FROM epube_books
+			$result = $ldb->query("SELECT id, lastread, lastcfi FROM epube_books
 				WHERE bookid = '$bookid' AND owner = '$owner' LIMIT 1");
 
-			if (db_num_rows($result) != 0) {
-				$id = db_fetch_result($result, 0, "id");
-				$lastread = (int) db_fetch_result($result, 0, "lastread");
+			if ($line = $result->fetchArray()) {
+				$id = $line["id"];
+				$lastread = (int) $line["lastread"];
 
 				if ($lastread < $page || $page == -1) {
 
 					if ($page == -1) $page = 0;
 
-					db_query($link, "UPDATE epube_books SET lastread = '$page' WHERE id = '$id'");
+					$ldb->query("UPDATE epube_books SET lastread = '$page', lastcfi = '$cfi' WHERE id = '$id'");
 				}
 			} else {
-				db_query($link, "INSERT INTO epube_books (bookid, owner, lastread) VALUES
-					('$bookid', '$owner', '$page')");
+				$ldb->query("INSERT INTO epube_books (bookid, owner, lastread, lastcfi) VALUES
+					('$bookid', '$owner', '$page', '$cfi')");
 
 			}
 
-			db_query($link, "COMMIT");
+			$ldb->query("COMMIT");
 		}
 
-		print json_encode(["lastread" => $page]);
+		print json_encode(["page" => $page, "cfi" => $cfi]);
 
 		break;