diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-05-24 21:23:59 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-05-24 21:23:59 +0300 |
| commit | 4af3c3398db3c214d2216d56a1154a00ba079923 (patch) | |
| tree | a20f4b71605d8e3667b51c4b7e43f1afe3dbc521 /classes | |
| parent | ce40334f1a4708958b38c8565635019e4fbeefb9 (diff) | |
| download | the-epube-4af3c3398db3c214d2216d56a1154a00ba079923.tar.gz the-epube-4af3c3398db3c214d2216d56a1154a00ba079923.tar.xz | |
add a listhandler to show return a list of books as JSON for index
Diffstat (limited to 'classes')
| -rw-r--r-- | classes/CsrfMiddleware.php | 2 | ||||
| -rw-r--r-- | classes/Handler/ListHandler.php | 163 |
2 files changed, 164 insertions, 1 deletions
diff --git a/classes/CsrfMiddleware.php b/classes/CsrfMiddleware.php index 2361957..9693f99 100644 --- a/classes/CsrfMiddleware.php +++ b/classes/CsrfMiddleware.php @@ -4,7 +4,7 @@ class CsrfMiddleware implements MiddlewareInterface { /** @var array<string> */ private array $csrfIgnore = [ - "cover", "download", "getpagination", "getlastread", "getinfo" + "cover", "download", "getpagination", "getlastread", "getinfo", "list" ]; public function process(Request $request, callable $next): Response diff --git a/classes/Handler/ListHandler.php b/classes/Handler/ListHandler.php new file mode 100644 index 0000000..c8c912f --- /dev/null +++ b/classes/Handler/ListHandler.php @@ -0,0 +1,163 @@ +<?php + +namespace Handler; + +use Config; +use Request; +use RequestHandlerInterface; +use Response; +use PDO; + +class ListHandler implements RequestHandlerInterface +{ + public function handle(Request $request): Response + { + $owner = $_SESSION["owner"] ?? ""; + $mode = $request->getAttribute('mode', ''); + $query = $request->getAttribute('query', ''); + $limit = (int) $request->getAttribute('limit', 60); + $offset = (int) $request->getAttribute('offset', 0); + + $ldb = \Db::pdo(); + $caldb = new PDO('sqlite:' . Config::get(Config::CALIBRE_DB)); + + $ids_qpart = "1"; + + if ($mode == "favorites") { + $fav_sth = $ldb->prepare("SELECT bookid FROM epube_favorites WHERE owner = ?"); + $fav_sth->execute([$owner]); + + $fav_ids = []; + while ($line = $fav_sth->fetch()) { + array_push($fav_ids, $line["bookid"]); + } + + if (empty($fav_ids)) { + return (new Response())->withBody(json_encode(["books" => [], "total" => 0])); + } + + $ids_qpart = "books.id IN (" . implode(",", $fav_ids) . ")"; + } + + $order_by = $query ? "author_sort, series_name, series_index, title, books.id" : "books.id DESC"; + + $sth = $caldb->prepare("SELECT books.*, s.name AS series_name, + (SELECT id FROM data WHERE book = books.id AND format = 'EPUB' LIMIT 1) AS epub_id FROM books + LEFT JOIN books_series_link AS bsl ON (bsl.book = books.id) + LEFT JOIN series AS s ON (bsl.series = s.id) + WHERE + ((:query = '') OR + ( + LOWER(books.author_sort) LIKE LOWER(:query) OR + LOWER(books.title) LIKE LOWER(:query) OR + LOWER(series_name) LIKE LOWER(:query) + )) + AND $ids_qpart + ORDER BY $order_by LIMIT :limit OFFSET :offset"); + + $sth->execute([':limit' => $limit, ':offset' => $offset, ':query' => '%' . $query . '%']); + + $books = []; + $total = 0; + + while ($line = $sth->fetch()) { + $cover_link = ""; + if ($line['has_cover']) { + $cover_filename = Config::get(Config::BOOKS_DIR) . "/" . $line["path"] . "/" . "cover.jpg"; + if (file_exists($cover_filename)) { + $cover_mtime = filemtime($cover_filename); + $cover_link = "backend.php?" . http_build_query(["op" => "cover", "id" => $line["id"], "ts" => $cover_mtime]); + } + } + + $read_link = ""; + $in_progress = false; + $is_read = false; + + if ($line["epub_id"]) { + $read_link = "read.html?" . http_build_query(["id" => $line["epub_id"], "rt" => $mode, "b" => $line["id"], "ts" => time()]); + + $lastread_sth = $ldb->prepare("SELECT lastread, total_pages FROM epube_books, epube_pagination + WHERE epube_pagination.bookid = epube_books.bookid AND + epube_books.bookid = ? AND owner = ?"); + $lastread_sth->execute([$line['epub_id'], $owner]); + + if ($lastread_line = $lastread_sth->fetch()) { + $lastread = $lastread_line["lastread"]; + $total_pages = $lastread_line["total_pages"]; + + $is_read = $total_pages - $lastread < 5; + $in_progress = $lastread > 1; + } + } + + $series_link = ""; + $series_full = ""; + + if ($line["series_name"]) { + $series_link = "?" . http_build_query(["query" => $line["series_name"]]); + $series_full = $line["series_name"] . " [" . $line["series_index"] . "]"; + } + + $data_sth = $caldb->prepare("SELECT * FROM data WHERE book = ? LIMIT 3"); + $data_sth->execute([$line['id']]); + + $download_formats = []; + while ($data_line = $data_sth->fetch()) { + if ($data_line["format"] != "ORIGINAL_EPUB") { + $download_formats[] = [ + "format" => $data_line["format"], + "id" => $data_line["id"], + ]; + } + } + + $is_fav = false; + $fav_sth = $ldb->prepare("SELECT id FROM epube_favorites + WHERE bookid = ? AND owner = ? LIMIT 1"); + $fav_sth->execute([$line['id'], $owner]); + if ($fav_sth->fetch()) { + $is_fav = true; + } + + $books[] = [ + "id" => $line["id"], + "title" => $line["title"], + "author_sort" => $line["author_sort"], + "series_name" => $line["series_name"] ?? "", + "series_index" => $line["series_index"] ?? 0, + "series_link" => $series_link, + "series_full" => $series_full, + "cover_link" => $cover_link, + "read_link" => $read_link, + "in_progress" => $in_progress, + "is_read" => $is_read, + "is_fav" => $is_fav, + "epub_id" => $line["epub_id"] ?? 0, + "download_formats" => $download_formats, + ]; + } + + // count total for pagination + $count_sth = $caldb->prepare("SELECT COUNT(*) FROM books + LEFT JOIN books_series_link AS bsl ON (bsl.book = books.id) + LEFT JOIN series AS s ON (bsl.series = s.id) + WHERE + ((:query = '%%') OR + ( + LOWER(books.author_sort) LIKE LOWER(:query) OR + LOWER(books.title) LIKE LOWER(:query) OR + LOWER(s.name) LIKE LOWER(:query) + )) + AND $ids_qpart"); + $count_sth->execute([':query' => '%' . $query . '%']); + $total = (int) $count_sth->fetchColumn(); + + return (new Response())->withBody(json_encode([ + "books" => $books, + "total" => $total, + "offset" => $offset, + "limit" => $limit, + ])); + } +} |
