forked from mikespub-org/intity-epubjs-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzipfs.php
71 lines (66 loc) · 1.82 KB
/
zipfs.php
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
<?php
/**
* COPS (Calibre OPDS PHP Server) class file
*
* @author mikespub
* @deprecated 3.1.0 use index.php/zipfs instead
*/
use SebLucas\Cops\Input\Request;
use SebLucas\Cops\Calibre\Book;
if (!empty($_SERVER['SCRIPT_NAME']) && str_contains($_SERVER['SCRIPT_NAME'], '/vendor/')) {
echo "Unable to run in /vendor/ directory";
return;
}
if (php_sapi_name() === 'cli') {
return;
}
require_once __DIR__ . '/config.php';
// don't try to match path params here
$request = new Request(false);
$path = $request->path();
if (empty($path) || $path == '/') {
Request::notFound();
}
$path = substr($path, 1);
$matches = [];
if (!preg_match('/^(\d+)\/(.+)$/', $path, $matches)) {
Request::notFound();
}
$idData = $matches[1];
if (empty($idData)) {
// this will call exit()
$request->notFound();
}
$component = $matches[2];
if (empty($component)) {
// this will call exit()
$request->notFound();
}
try {
$book = Book::getBookByDataId($idData, $request->database());
if (!$book) {
throw new Exception('Unknown data ' . $idData);
}
$epub = $book->getFilePath('EPUB', $idData);
if (!$epub || !file_exists($epub)) {
throw new Exception('Unknown file ' . $epub);
}
$zip = new ZipArchive();
$res = $zip->open($epub, ZipArchive::RDONLY);
if ($res !== true) {
throw new Exception('Invalid file ' . $epub);
}
$res = $zip->locateName($component);
if ($res === false) {
throw new Exception('Unknown component ' . $component);
}
$expires = 60 * 60 * 24 * 14;
header('Pragma: public');
header('Cache-Control: maxage=' . $expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
echo $zip->getFromName($component);
$zip->close();
} catch (Exception $e) {
error_log($e);
$request->notFound();
}