about summary refs log tree commit diff stats
path: root/classes/Handler/GetlastreadHandler.php
blob: 8d0a1f2dd98daee72cd29d69c2a77af0f60a46d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php

namespace Handler;

use AuthRequired;
use ORM;
use Request;
use RequestHandlerInterface;
use Response;

class GetlastreadHandler implements RequestHandlerInterface, AuthRequired
{
    public function handle(Request $request): Response
    {
        $bookid = (int) $request->getAttribute('id');
        $owner = $_SESSION["owner"] ?? "";

        if ($bookid) {
            $book = ORM::for_table('epube_books')
                ->where('bookid', $bookid)
                ->where('owner', $owner)
                ->find_one();

            if ($book) {
                return (new Response())->withBody(json_encode([
                    "page" => (int)$book->lastread,
                    "cfi" => $book->lastcfi,
                    "total" => 100,
                    "timestamp" => (int)$book->lastts
                ]));
            }
        }

        return (new Response())
            ->withStatus(404)
            ->withBody("Not found.");
    }
}