-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.php
More file actions
416 lines (333 loc) Β· 12.2 KB
/
Copy pathreader.php
File metadata and controls
416 lines (333 loc) Β· 12.2 KB
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
session_start();
$ebookFolder = 'ebooks/';
$extractPath = 'ebooks/extracted/';
$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
// Reset stored file when a new book is loaded
if (isset($_GET['file'])) {
$_SESSION['original_file'] = $_GET['file']; // Store new file
}
// Retrieve the correct file for reading
$file = isset($_SESSION['original_file']) ? $_SESSION['original_file'] : '';
//retrieve the correct theme
$theme = isset($_COOKIE['theme']) ? $_COOKIE['theme'] : (isset($_SESSION['theme']) ? $_SESSION['theme'] : 'light-mode');
$_SESSION['theme'] = $theme; // Keep theme across pages
echo "<body class='$theme' onload='applyTheme();'>";
if (!$file) {
die("No file selected!");
}
// Function to sanitize filenames
function sanitizeFilename($filename) {
// Remove special characters, apostrophes, spaces, and accents
$filename = iconv("UTF-8", "ASCII//TRANSLIT", $filename); // Convert special characters
$filename = preg_replace("/[^a-zA-Z0-9._-]/", "_", $filename); // Keep only safe characters
return $filename;
}
// Rename file to a clean format
$originalFile = $ebookFolder . $file;
$sanitizedFile = $ebookFolder . sanitizeFilename($file);
if ($originalFile !== $sanitizedFile) {
rename($originalFile, $sanitizedFile);
$file = basename($sanitizedFile);
}
// Ensure extraction folder exists
$bookFolder = $extractPath . pathinfo($file, PATHINFO_FILENAME) . '/';
if (!is_dir($bookFolder)) {
mkdir($bookFolder, 0755, true);
}
// Extract EPUB using system command
$epubFile = escapeshellarg(realpath($ebookFolder . $file));
$destination = escapeshellarg(realpath($bookFolder));
exec("unzip -o $epubFile -d $destination 2>&1", $output, $returnCode);
if ($returnCode !== 0) {
die("Failed to extract EPUB file! Error:\n" . implode("\n", $output));
}
// Search for HTML files in multiple locations, including /text folder
// Search inside OEBPS, Text, or text folders (including subdirectories)
$htmlFiles = glob("$bookFolder/{OEBPS,Text,text}/**/*.{html,xhtml,htm}", GLOB_BRACE);
// If no files are found, check directly inside all subdirectories
if (empty($htmlFiles)) {
$htmlFiles = glob("$bookFolder/**/*.{html,xhtml,htm}", GLOB_BRACE);
}
// If still empty, check the root folder of the extracted EPUB
if (empty($htmlFiles)) {
$htmlFiles = glob("$bookFolder/*.{html,xhtml,htm}", GLOB_BRACE);
}
if (empty($htmlFiles)) {
die("No readable HTML content found");
}
// Sort files in natural order
natsort($htmlFiles);
$htmlFiles = array_values($htmlFiles);
if (!isset($htmlFiles[$page])) {
die("Page not found!");
}
// Save progress
$_SESSION['last_page'] = $page;
$content = file_get_contents($htmlFiles[$page]);
// Automatically store bookmark for this book
$_SESSION["bookmark_$file"] = json_encode([
'page' => $page,
'scroll' => $_GET['scroll'] ?? $_POST['scroll'] ?? $_SESSION["bookmark_$file"]['scroll'] ?? 0
]);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>EPUB Reader</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: white;
color: black;
transition: background 0.3s, color 0.3s;
overflow: hidden; /* Prevent unwanted scrolling */
}
.book-container {
max-width: 800px;
margin: auto;
padding: 20px;
border: 1px solid #ddd;
text-align: justify; /* Justify text */
line-height: 1.6;
margin-top: 80px; /* Push content below toolbar */
height: 90vh; /* Keep container viewable within screen */
overflow: hidden;
/* overflow-y: auto; Enable natural scrolling */
user-select: none; /* Prevent text selection */
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
}
.nav-buttons {
position: fixed;
top: 50%;
transform: translateY(-50%);
width: 100%;
display: flex;
justify-content: space-between;
padding: 0 -5px;
}
.nav-button {
padding: 20px;
font-size: 22px;
background-color: rgba(0, 0, 0, 0.3);
color: white;
border-radius: 50%;
cursor: pointer;
text-decoration: none;
border: none;
width: 60px;
height: 60px;
display: flex;
align-items: center;
justify-content: center;
}
.nav-button:hover {
background-color: rgba(0, 0, 0, 0.8);
}
.light-mode { background: white; color: black; }
.dark-mode { background: black; color: white; }
.sepia-mode { background: #f4ecd8; color: #5f4b32; }
/* menu icons */
.top-buttons {
display: flex;
justify-content: center;
gap: 10px;
padding: 10px;
position: fixed;
top: 0;
width: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 1000;
height: 45px; /* 60px; */
align-items: center;
}
.button-icon {
padding: 10px; /* 15px */
font-size: 20px; /* Slightly smaller icons, was 22 */
background-color: transparent;
color: white;
border-radius: 50%;
cursor: pointer;
text-decoration: none;
border: none;
width: 40px; /* 50px; */
height: 40px; /* 50px; */
display: flex;
align-items: center;
justify-content: center;
transition: background 0.3s;
}
.button-icon:hover {
background-color: rgba(255, 255, 255, 0.2);
}
.progress-container {
position: fixed;
bottom: 0;
width: 100%;
height: 6px;
background: #ccc;
}
.progress-bar {
height: 100%;
width: 0%;
background: #4CAF50;
transition: width 0.3s;
}
.page-counter {
position: fixed;
bottom: 8px;
right: 20px;
font-size: 8px;
color: white;
background: rgba(0, 0, 0, 0.4);
padding: 5px;
border-radius: 5px;
}
</style>
</head>
<body onload="applyTheme();">
<div class="top-buttons">
<select id="toc-dropdown" onchange="jumpToChapter(this.value)">
<?php foreach ($htmlFiles as $index => $file) { ?>
<option value="<?php echo $index; ?>">
Pag.<?php echo $index + 1; ?>
</option>
<?php } ?>
</select>
<button class="button-icon" onclick="setTheme('light-mode')">π¨</button>
<button class="button-icon" onclick="setTheme('dark-mode')">π</button>
<button class="button-icon" onclick="setTheme('sepia-mode')">π</button>
<button class="button-icon" onclick="readAloud()">π</button>-
<button class="button-icon" id="increaseFont">β</button>
<button class="button-icon" id="decreaseFont">β</button>
<button class="button-icon" onclick="window.location.href='index_reader.php'">π </button>
</div>
<div class="book-container" id="book-content">
<?php echo $content; ?>
</div>
<div class="nav-buttons">
<button class="nav-button" onclick="scrollUp()">β¬
</button>
<button class="nav-button" onclick="scrollDown()">β‘</button>
</div>
<div class="progress-container">
<div class="progress-bar" id="progress-bar"></div>
</div>
<div class="page-counter" id="page-counter"></div>
<script>
function storeScrollPosition() {
let bookTitle = "<?php echo urlencode($_SESSION['original_file']); ?>";
let currentScroll = document.getElementById("book-content").scrollTop;
let bookmarkData = localStorage.getItem("bookmark_" + bookTitle);
bookmarkData = bookmarkData ? JSON.parse(bookmarkData) : { page: <?php echo $page; ?>, scroll: 0 };
bookmarkData.scroll = currentScroll; // Store updated scroll position
localStorage.setItem("bookmark_" + bookTitle, JSON.stringify(bookmarkData));
}
// Save scroll position before leaving the page
window.addEventListener("beforeunload", storeScrollPosition);
document.getElementById("book-content").addEventListener("scroll", storeScrollPosition);
function saveBookmark() {
let currentPage = <?php echo $page; ?>;
let bookTitle = "<?php echo urlencode($_SESSION['original_file']); ?>";
let scrollY = document.getElementById("book-content").scrollTop;
localStorage.setItem("bookmark_" + bookTitle, JSON.stringify({ page: currentPage, scroll: scrollY }));
alert("π Bookmark saved!");
}
function loadBookmark() {
let bookTitle = "<?php echo urlencode($_SESSION['original_file']); ?>";
let savedBookmark = localStorage.getItem("bookmark_" + bookTitle);
if (savedBookmark) {
let bookmarkData = JSON.parse(savedBookmark);
window.location.href = "reader.php?file=" + bookTitle + "&page=" + bookmarkData.page + "&scroll=" + bookmarkData.scroll;
} else {
alert("β No bookmark found!");
}
}
//restaure saved bookmark position
document.addEventListener("DOMContentLoaded", () => {
let bookTitle = "<?php echo urlencode($_SESSION['original_file']); ?>";
let savedBookmark = localStorage.getItem("bookmark_" + bookTitle);
if (savedBookmark) {
let bookmarkData = JSON.parse(savedBookmark);
let savedScroll = bookmarkData.scroll || 0;
document.getElementById("book-content").scrollTop = savedScroll;
}
});
//table of content jumping
function jumpToChapter(pageIndex) {
window.location.href = "reader.php?file=" + "<?php echo urlencode($_SESSION['original_file']); ?>" + "&page=" + pageIndex;
}
// progress bar
function updatePageProgress() {
let currentPage = <?php echo $page; ?> + 1;
let totalPages = <?php echo count($htmlFiles); ?>;
let progress = (currentPage / totalPages) * 100;
document.getElementById("progress-bar").style.width = progress + "%";
document.getElementById("page-counter").innerText = `π ${currentPage} / ${totalPages}`;
}
document.addEventListener("DOMContentLoaded", updatePageProgress);
function readAloud() {
const text = document.getElementById("book-content").innerText;
const speech = new SpeechSynthesisUtterance(text);
speech.lang = "en-US";
window.speechSynthesis.speak(speech);
}
function setTheme(theme) {
document.body.className = theme;
localStorage.setItem("theme", theme);
document.cookie = "theme=" + theme + "; path=/"; // Store theme in cookie for persistence
applyTheme(); // Refresh theme immediately
}
function applyTheme() {
const savedTheme = localStorage.getItem("theme") || "light-mode";
document.body.className = savedTheme;
}
document.addEventListener("DOMContentLoaded", applyTheme);
function scrollUp() {
let bookContent = document.getElementById('book-content');
if (bookContent.scrollTop > 10) {
bookContent.scrollBy({
top: -window.innerHeight * 0.9,
behavior: 'smooth'
});
} else {
window.location.href = "reader.php?file=" + "<?php echo urlencode($_SESSION['original_file']); ?>" + "&page=<?php echo max($page - 1, 0); ?>";
}
}
function scrollDown() {
let bookContent = document.getElementById('book-content');
let maxScroll = bookContent.scrollHeight - bookContent.clientHeight;
let currentScroll = bookContent.scrollTop;
if (currentScroll < maxScroll - 10) {
bookContent.scrollBy({
top: window.innerHeight * 0.9,
behavior: 'smooth'
});
} else {
// Use the correct stored filename for navigation
window.location.href = "reader.php?file=" + "<?php echo urlencode($_SESSION['original_file']); ?>" + "&page=<?php echo min($page + 1, count($htmlFiles) - 1); ?>";
}
}
//font size
const increaseFontButton = document.getElementById("increaseFont");
const decreaseFontButton = document.getElementById("decreaseFont");
const readerContent = document.getElementById("book-content"); // Change from "reader" to "book-content"
// Load stored font size or set default
let fontSize = localStorage.getItem("fontSize") ? parseInt(localStorage.getItem("fontSize")) : 16;
readerContent.style.fontSize = fontSize + "px";
increaseFontButton.addEventListener("click", () => {
fontSize += 2;
readerContent.style.fontSize = fontSize + "px";
localStorage.setItem("fontSize", fontSize); // Save preference
});
decreaseFontButton.addEventListener("click", () => {
fontSize -= 2;
readerContent.style.fontSize = fontSize + "px";
localStorage.setItem("fontSize", fontSize); // Save preference
});
</script>
</body>
</html>