forked from mikespub-org/seblucas-cops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.php
107 lines (94 loc) · 2.84 KB
/
fetch.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
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
<?php
/**
* COPS (Calibre OPDS PHP Server)
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Sébastien Lucas <sebastien@slucas.fr>
*/
use SebLucas\Cops\Input\Config;
use SebLucas\Cops\Input\Request;
use SebLucas\Cops\Calibre\Book;
use SebLucas\Cops\Calibre\Cover;
require_once __DIR__ . '/config.php';
$request = new Request();
if (Config::get('fetch_protect') == '1') {
session_start();
if (!isset($_SESSION['connected'])) {
$request->notFound();
return;
}
}
// clean output buffers before sending the ebook data do avoid high memory usage on big ebooks (ie. comic books)
if (ob_get_length() !== false) {
ob_end_clean();
}
$bookId = $request->get('id', null);
$type = $request->get('type', 'jpg');
$idData = $request->get('data', null);
$viewOnly = $request->get('view', false);
$database = $request->get('db');
if (is_null($bookId)) {
$book = Book::getBookByDataId($idData, $database);
} else {
$book = Book::getBookById($bookId, $database);
}
if (!$book) {
$request->notFound();
return;
}
// -DC- Add png type
if ($type == 'jpg' || $type == 'png' || empty(Config::get('calibre_internal_directory'))) {
if ($type == 'jpg' || $type == 'png') {
$file = $book->getFilePath($type);
} else {
$file = $book->getFilePath($type, $idData);
}
if (is_null($file) || !file_exists($file)) {
$request->notFound();
return;
}
}
switch ($type) {
// -DC- Add png type
case 'jpg':
case 'png':
$cover = new Cover($book);
$cover->sendThumbnail($request);
return;
default:
break;
}
$expires = 60*60*24*14;
header('Pragma: public');
header('Cache-Control: max-age=' . $expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
$data = $book->getDataById($idData);
header('Content-Type: ' . $data->getMimeType());
// absolute path for single DB in PHP app here - cfr. internal dir for X-Accel-Redirect with Nginx
$file = $book->getFilePath($type, $idData);
if (!$viewOnly && $type == 'epub' && Config::get('update_epub-metadata')) {
if (Config::get('provide_kepub') == '1' && preg_match('/Kobo/', $request->agent())) {
$book->updateForKepub = true;
}
$book->getUpdatedEpub($idData);
return;
}
if ($viewOnly) {
header('Content-Disposition: inline');
} else {
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
}
// -DC- File is a full path
//$dir = Config::get('calibre_internal_directory');
//if (empty(Config::get('calibre_internal_directory'))) {
// $dir = Database::getDbDirectory();
//}
$dir = '';
if (empty(Config::get('x_accel_redirect'))) {
$filename = $dir . $file;
header('Content-Length: ' . filesize($filename));
readfile($filename);
} else {
header(Config::get('x_accel_redirect') . ': ' . $dir . $file);
}
exit();