summaryrefslogtreecommitdiff
path: root/index.php
blob: 078846480bca962dc181907f347d84f1c38c3720 (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
	set_include_path(__DIR__ ."/include" . PATH_SEPARATOR .
		get_include_path());

	if (!isset($_COOKIE['epube_sid'])) {
		header("Location: login.php");
		exit;
	}

	require_once "common.php";
	require_once "sessions.php";

	Config::sanity_check();

	if (!validate_session()) {
		header("Location: login.php");
		exit;
	}

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

	if (basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)) != 'index.php') {
		header('Location: index.php');
		exit;
	}

	setcookie("epube_csrf_token", $_SESSION["csrf_token"], time() + Config::get(Config::SESSION_LIFETIME),
		"/", "", Config::is_server_https());

    // TODO: this should be unified with the service worker cache list
    $check_files_mtime = [
		'manifest.json',
		'worker.js',
		'img/ic_launcher_web.png',
		'img/favicon.png',
		'read.html',
		'dist/app.min.js',
		'dist/app.min.css',
		'dist/app-libs.min.js',
		'dist/reader_iframe.min.js',
		'dist/reader_iframe.min.css',
		'offline.html',
		'lib/bootstrap/v3/css/bootstrap-theme.min.css',
		'lib/bootstrap/v3/css/bootstrap.min.css',
		'lib/bootstrap/v3/css/theme-dark.min.css',
		'lib/bootstrap/v3/fonts/glyphicons-halflings-regular.woff2'
	];

	$check_files_mtime = array_merge($check_files_mtime, glob('lib/fonts/*'));
	$check_files_mtime = array_filter($check_files_mtime, "file_exists");

	$mode = htmlspecialchars($_REQUEST["mode"] ?? "");

	$ldb = Db::pdo();
?>
<!DOCTYPE html>
<html>
<head>
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link href="lib/bootstrap/v3/css/bootstrap.min.css" rel="stylesheet" media="screen">
	<link id="theme_css" href="lib/bootstrap/v3/css/theme-dark.min.css" rel="stylesheet" media="screen">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<script src="dist/app-libs.min.js"></script>
	<title>The Epube</title>
	<link type="text/css" rel="stylesheet" media="screen" href="dist/app.min.css" />
	<link rel="shortcut icon" type="image/png" href="img/favicon.png" />
	<link rel="manifest" href="manifest.json">
	<meta name="mobile-web-app-capable" content="yes">
	<script src="dist/app.min.js"></script>
	<script type="text/javascript">
			'use strict';

			if ('serviceWorker' in navigator) {
            navigator.serviceWorker
                .register('worker.js')
                .then(function() {
                    console.log("service worker registered");

                    $(document).ready(function() {
							   App.cached_urls = JSON.parse('<?= json_encode($check_files_mtime) ?>');
                        App.index_mode = "<?= $mode ?>";
                        App.version = "<?= Config::get_version(true) ?>";
                        App.init();
                    });
                });
        } else {
            alert("Service worker support missing in browser (are you using plain HTTP?).");
        }
    </script>
</head>
<body class="epube-index">

<?php
	$query = $_REQUEST["query"] ?? "";
?>

<div class="navbar navbar-default navbar-static-top">
<div class="container">
	<div class="navbar-header">
		<span class="navbar-brand"><a title="v<?= Config::get_version(true) ?>" href="?">The Epube</a></span>

		<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#nav-collapse" aria-expanded="false">
			<span class="sr-only">Toggle navigation</span>
			<span class="icon-bar"></span>
			<span class="icon-bar"></span>
			<span class="icon-bar"></span>
		</button>

	</div>

	<?php
		$fav_active = $mode == "favorites" ? "active" : "";
		$index_active = $mode != "favorites" ? "active" : "";
	?>

	<div class="collapse navbar-collapse" id="nav-collapse">

		<ul class="nav navbar-nav">
			<li class="<?= $index_active ?>"><a href="index.php">All</a></li>
			<li class="<?= $fav_active ?>"><a href="index.php?mode=favorites">Favorites</a></li>
			<li><a href="offline.html">Local</a></li>
		</ul>

		<form class="navbar-form navbar-right">
			<div class="input-group">
				<input type="text" name="query" class="form-control" placeholder="Search for..."
					value="<?= htmlspecialchars($query) ?>">
				<input type="hidden" name="mode" value="<?= $mode ?>">
				<span class="input-group-btn">
					<button type="submit" class="btn btn-default">Go</button>
				</span>
			</div>
		</form>

		<ul class="nav navbar-nav navbar-right">
			<li><a href="#" title="Refresh script cache" onclick="return App.refreshCache(true)">
				<span class="glyphicon glyphicon-refresh"></span> <span class='hidden-sm hidden-md hidden-lg'>Refresh script cache</span></a></li>
			</li>
			<?php if ($mode !== "favorites") { ?>
				<li><a href="#" onclick="App.logout()" title="Log out">
					<span class="glyphicon glyphicon-log-out"></span> <span class='hidden-sm hidden-md hidden-lg'>Log out</span>
				</a></li>
			<?php } ?>
			<?php if ($mode == "favorites") { ?>
				<li><a href="#" onclick="App.Offline.getAll()" title="Download all">
					<span class="glyphicon glyphicon-download-alt text-primary"></span> <span class='hidden-sm hidden-md hidden-lg'>Download all</span>
				</a></li>
			<?php } ?>
		</ul>
	</div>

</div>
</div>

<div class="epube-app-filler"></div>

<div class="container container-main">

<form class="form-inline separate-search" style="display : none">
    <div class="input-group">
        <input type="text" name="query" class="form-control" placeholder="Search for..."
               value="<?= htmlspecialchars($query) ?>">
        <input type="hidden" name="mode" value="<?= $mode ?>">
        <span class="input-group-btn">
                <button type="submit" class="btn btn-default">Go</button>
        </span>
    </div>
</form>

<div class="modal fade" id="summary-modal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Summary</h4>
      </div>
      <div class="modal-body">
			<div class="book-summary"> </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

<div style="display : none" class="alert alert-info dl-progress"></div>

<?php
	$db = 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"]);
		}

		$ids_qpart = "books.id IN (" . implode(",", $fav_ids) . ")";
	}

	$limit = 60;
	$offset = (int) ($_REQUEST["offset"] ?? 0);

	$order_by = $query ? "author_sort, series_name, series_index, title, books.id" : "books.id DESC";

	$sth = $db->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 . '%']);

	print "<div class='row display-flex'>";

	$rows = 0;

	while ($line = $sth->fetch()) {
		++$rows;

		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);
			else
				$cover_mtime = 0;

			$cover_link = "backend.php?" . http_build_query(["op" => "cover", "id" => $line["id"], "ts" => $cover_mtime]);
		} else {
			$cover_link = "";
		}

		$author_link = "?" . http_build_query(["query" => $line["author_sort"]]);

		$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;
			}

		} else {
			$read_link = "";
		}

		$cover_class = $is_read ? "read" : "";
		$title_class = $in_progress ? "in_progress" : "";

		if ($line["series_name"]) {
			$series_link = "?" . http_build_query(["query" => $line["series_name"]]);
			$series_full = $line["series_name"] . " [" . $line["series_index"] . "]";
		} else {
			$series_link = "";
			$series_full = "";
		}

		$data_sth = $db->prepare("SELECT * FROM data WHERE book = ? LIMIT 3");
		$data_sth->execute([$line['id']]);

		?>
			<div class="col-xxs-6 col-xs-3 col-sm-3 col-md-2" id="cell-<?= $line["id"] ?>">
				<?php if ($read_link) { ?>
					<a class="thumbnail <?= $cover_class ?>" href="<?= $read_link ?>">
				<?php } else { ?>
					<span class="thumbnail <?= $cover_class ?>">
				<?php } ?>

					<img style="display : none" data-book-id="<?= $line['id'] ?>" data-cover-link="<?= $cover_link ?>">

				<?php if ($read_link) { ?>
					</a>
				<?php } else { ?>
					</span>
				<?php } ?>

				<div class="caption">
					<div title="<?= htmlspecialchars($line['title']) ?>" class="<?= $title_class ?>">
						<?php if ($read_link) { ?>
							<a href="<?= $read_link ?>"><?= $line['title'] ?></a>
						<?php } else { ?>
							<?= $line['title'] ?>
						<?php } ?>
					</div>

					<div><a title="<?= htmlspecialchars($series_full) ?>" href="<?= $series_link ?>">
							<?= $series_full ?></a></div>

 					<div><a title="<?= htmlspecialchars($line["author_sort"]) ?>" href="<?= $author_link ?>">
						<?= $line["author_sort"] ?></a></div>

				</div>

				<div class="dropdown" style="white-space : nowrap">
				  <a href="#" data-toggle="dropdown" role="button">
				  		More...
						<span class="caret"></span>
		  			</a>

					<ul class="dropdown-menu" aria-labelledby="dLabel">
						<?php

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

							$found_id = false;

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

							if ($found_id) {
								$toggle_fav_prompt = "Remove from favorites";
								$fav_attr = "1";
							} else {
								$toggle_fav_prompt = "Add to favorites";
								$fav_attr = "0";
							}
						?>

						<li><a href="#" onclick="return App.showSummary(this)"
							data-book-id="<?= $line["id"] ?>">Summary</a></li>

						<li><a href="#" onclick="return App.toggleFavorite(this)"
							data-is-fav="<?= $fav_attr ?>"
							class="fav_item" data-book-id="<?= $line["id"] ?>">
							<?= $toggle_fav_prompt ?></a></li>

						<?php if ($line["epub_id"]) { ?>
						<li><a href="#" onclick=""
							data-book-id="<?= $line["id"] ?>" class="offline_dropitem"></a></li>
						<li class="divider"></li>
						<?php } ?>

						<?php while ($data_line = $data_sth->fetch()) {
							if ($data_line["format"] != "ORIGINAL_EPUB") {
								$label_class = $data_line["format"] == "EPUB" ? "label-success" : "label-primary";

								$download_link = "backend.php?op=download&id=" . $data_line["id"];

								print "<li><a target=\"_blank\" href=\"$download_link\">Download: <span class=\"label $label_class\">" .
									$data_line["format"] . "</span></a></li>";
							}
						} ?>
					</ul>
				</div>
			</div>
		<?php } /* while */ ?>

	</div>

	<?php
		$prev_link = http_build_query(["mode" => $mode, "query" => $query, "offset" => $offset - $limit]);
		$next_link = http_build_query(["mode" => $mode, "query" => $query, "offset" => $offset + $limit]);
	?>

	<ul class="pager">
		<?php if ($offset > 0) { ?>
		<li class="previous"><a href="?<?= $prev_link ?>">&larr; Previous</a></li>
		<?php } else { ?>
		<li class="previous disabled"><a href="#">&larr; Previous</a></li>
		<?php } ?>

		<?php if ($rows == $limit) { ?>
			<li class="next"><a href="?<?= $next_link ?>">Next&rarr;</a></li>
		<?php } else { ?>
			<li class="next disabled"><a href="#">Next&rarr;</a></li>
		<?php } ?>
	</ul>
</div>
</body>
</html>