summaryrefslogtreecommitdiff
path: root/backend.php
blob: a585c1035393afc6fff3d842300545aca50a5e15 (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php

	require_once "config.php";

	header("Content-type: text/json");

	define('STATIC_EXPIRES', 86400*14);
	define('PAGE_RESET_PROGRESS', -1);

	require_once "sessions.php";
	require_once "db.php";

	@$owner = $_SESSION["owner"];

	if (!$owner) {
		header($_SERVER["SERVER_PROTOCOL"]." 401 Unauthorized");
		echo "Unauthorized";
		die;
	}

	$op = $_REQUEST["op"];

	$ldb = Db::get();

	ob_start("ob_gzhandler");

	switch ($op) {
	case "cover":
		$id = (int) $_REQUEST["id"];

		$db = new PDO('sqlite:' . CALIBRE_DB);
		$sth = $db->prepare("SELECT has_cover, path FROM books WHERE id = ?");
		$sth->execute([$id]);

		while ($line = $sth->fetch()) {
			$filename = BOOKS_DIR . "/" . $line["path"] . "/" . "cover.jpg";

			if (file_exists($filename)) {
				$base_filename = basename($filename);

				header("Content-type: " . mime_content_type($filename));
				header('Cache-control: max-age=' . STATIC_EXPIRES);
				header("Expires: " . gmdate("D, d M Y H:i:s \G\M\T", time()+STATIC_EXPIRES));
				header("Last-Modified: " .
					gmdate("D, d M Y H:i:s \G\M\T", filemtime($filename)));

				readfile($filename);
			} else {
				header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
				echo "File not found.";
			}
		}

		break;
	case "getowner":
		print json_encode(["owner" => $owner]);
		break;
	case "getinfo":
		$id = (int) $_REQUEST["id"];

		$db = new PDO('sqlite:' . CALIBRE_DB);

		$sth = $db->prepare("SELECT books.*, s.name AS series_name,
			(SELECT text FROM comments WHERE book = books.id) AS comment,
			(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 books.id = ?");
		$sth->execute([$id]);

		if ($line = $sth->fetch()) {
			print json_encode($line);
		}

		break;

	case "togglefav":
		$id = (int) $_REQUEST["id"];

		$sth = $ldb->prepare("SELECT id FROM epube_favorites WHERE bookid = ?
			AND owner = ? LIMIT 1");
		$sth->execute([$id, $owner]);

		$found_id = false;
		$status = -1;

		while ($line = $sth->fetch()) {
			$found_id = $line["id"];
		}

		if ($found_id) {
			$sth = $ldb->prepare("DELETE FROM epube_favorites WHERE id = ?");
			$sth->execute([$found_id]);

			$status = 0;
		} else {
			$sth = $ldb->prepare("INSERT INTO epube_favorites (bookid, owner) VALUES (?, ?)");
			$sth->execute([$id, $owner]);

			$status = 1;
		}

		print json_encode(["id" => $id, "status" => $status]);

	case "download":
		$id = (int) $_REQUEST["id"];

		$db = new PDO('sqlite:' . CALIBRE_DB);
		$sth = $db->prepare("SELECT path, name, format FROM data LEFT JOIN books ON (data.book = books.id) WHERE data.id = ?");
		$sth->execute([$id]);

		while ($line = $sth->fetch()) {
			$filename = BOOKS_DIR . "/" . $line["path"] . "/" . $line["name"] . "." . strtolower($line["format"]);

			if (file_exists($filename)) {
				$base_filename = basename($filename);

				header("Content-type: " . mime_content_type($filename));
				header("Content-Disposition: attachment; filename=\"$base_filename\"");

				readfile($filename);
			} else {
				header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
				echo "File not found.";
			}
		}

		break;

	case "getpagination":
		$bookid = (int) $_REQUEST["id"];

		if ($bookid) {
			$sth = $ldb->prepare("SELECT pagination FROM epube_pagination WHERE bookid = ? LIMIT 1");
			$sth->execute([$bookid]);

			if ($line = $sth->fetch()) {
				print $line["pagination"];
			} else {
				header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
				echo "File not found.";
			}
		}

		break;
	case "storepagination":
		$bookid = (int) $_REQUEST["id"];
		$payload = $_REQUEST["payload"];
		$total_pages = (int) $_REQUEST["total"];

		if ($bookid && $payload && $total_pages) {

			$ldb->beginTransaction();

			$sth = $ldb->prepare("SELECT id FROM epube_pagination WHERE bookid = ? LIMIT 1");
			$sth->execute([$bookid]);

			if ($line = $sth->fetch()) {
				$id = $line["id"];

				$sth = $ldb->prepare("UPDATE epube_pagination SET pagination = ?,
					total_pages = ? WHERE id = ?");
				$sth->execute([$payload, $total_pages, $id]);

			} else {
				$sth = $ldb->prepare("INSERT INTO epube_pagination (bookid, pagination, total_pages) VALUES
					(?, ?, ?)");
				$sth->execute([$bookid, $payload, $total_pages]);
			}

			$ldb->commit();
		}

		break;
	case "getlastread":
		$bookid = (int) $_REQUEST["id"];
		$lastread = 0;
		$lastcfi = "";
		$lastts = 0;

		if ($bookid) {

			$sth = $ldb->prepare("SELECT b.lastread, b.lastcfi, b.lastts FROM epube_books AS b, epube_pagination AS p
				WHERE b.bookid = p.bookid AND b.bookid = ? AND b.owner = ? LIMIT 1");
			$sth->execute([$bookid, $owner]);

			if ($line = $sth->fetch()) {
				$lastread = (int) $line["lastread"];
				$lastcfi = $line["lastcfi"];
				$lastts = (int) $line["lastts"];
			}
		}

		print json_encode(["page" => $lastread, "cfi" => $lastcfi, "total" => 100, "timestamp" => $lastts]);

		break;

	case "storelastread":
		$page = (int) $_REQUEST["page"];
		$bookid = (int) $_REQUEST["id"];
		$timestamp = (int) $_REQUEST["timestamp"];
		$cfi = $_REQUEST["cfi"];

		if ($bookid) {

			$ldb->beginTransaction();

			$sth = $ldb->prepare("SELECT id, lastread, lastcfi, lastts FROM epube_books
				WHERE bookid = ? AND owner = ? LIMIT 1");
			$sth->execute([$bookid, $owner]);

			if ($line = $sth->fetch()) {
				$id = $line["id"];
				$last_timestamp = (int) $line["lastts"];
				$last_page = (int) $line["lastread"];

				if (($timestamp >= $last_timestamp) && ($page >= $last_page || $page == PAGE_RESET_PROGRESS)) {

					if ($page == PAGE_RESET_PROGRESS)
						$page = 0;

					$sth = $ldb->prepare("UPDATE epube_books SET lastread = ?, lastcfi = ?, lastts = ? WHERE id = ?");
					$sth->execute([$page, $cfi, $timestamp, $id]);
				}
			} else {
				$sth = $ldb->prepare("INSERT INTO epube_books (bookid, owner, lastread, lastcfi, lastts) VALUES
					(?, ?, ?, ?, ?)");
				$sth->execute([$bookid, $owner, $page, $cfi, $timestamp]);
			}

			$ldb->commit();
		}

		print json_encode(["page" => $page, "cfi" => $cfi]);

		break;

	case "wikisearch":
		$query = urlencode(strip_tags($_REQUEST['query']));
		$url = "https://en.wiktionary.org/w/api.php?titles=${query}&action=query&prop=extracts&format=json&exlimit=1";

		if ($resp = file_get_contents($url)) {
			print $resp;
		}

		break;
	case "define":
		if (defined('DICT_ENABLED') && DICT_ENABLED) {
			function parse_dict_reply($reply) {
				$tmp = [];

				foreach (explode("\n", $reply) as $line) {
					list ($code, $message) = explode(" ", $line, 2);

					if (!$code && $message)
						array_push($tmp, $message);
				}

				return $tmp;
			}

			/* strip hyphens */
			$word = strip_tags(str_replace("­", "", $_REQUEST["word"]));
			$orig_word = $word;

			$result = [];

			for ($i = 0; $i < 3; $i++) {
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_URL, sprintf("dict://%s/define:%s", DICT_SERVER, $word));
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				$dict_reply = curl_exec($ch);

				if ($dict_reply) {
					$ret_parsed = parse_dict_reply($dict_reply);

					if (count($ret_parsed) > 0) {
						array_push($result, "<strong>$word</strong>");

						$result = array_merge($result, $ret_parsed);
						break;
					} else {
						$word = mb_substr($word, 0, mb_strlen($word)-1);
					}

				} else {
					array_push($result, curl_error($ch));
				}

				curl_close($ch);
			}

			if (count($result) == 0)
				array_push($result, "No results for: <b>$orig_word</b>");

			print json_encode(["result" => $result]);
		} else {
			print json_encode(["result" => ["Dictionary lookups are disabled."]]);
		}

		break;

	default:
		header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
		echo "Method not found.";
	}


?>