forked from seblucas/cops
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch.php
126 lines (114 loc) · 4.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* COPS (Calibre OPDS PHP Server)
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Sébastien Lucas <[email protected]>
*/
require_once dirname(__FILE__) . '/config.php';
require_once dirname(__FILE__) . '/base.php';
global $config;
if ($config['cops_fetch_protect'] == '1') {
session_start();
if (!isset($_SESSION['connected'])) {
notFound();
return;
}
}
$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');
$bookId = getURLParam('id', NULL);
$type = getURLParam('type', 'jpg');
$idData = getURLParam('data', NULL);
$viewOnly = getURLParam('view', FALSE);
if (is_null($bookId)) {
$book = Book::getBookByDataId($idData);
} else {
$book = Book::getBookById($bookId);
}
if (!$book) {
notFound ();
return;
}
if ($book && ($type == 'jpg' || empty ($config['calibre_internal_directory']))) {
if ($type == 'jpg') {
$file = $book->getFilePath($type);
} else {
$file = $book->getFilePath($type, $idData);
}
if (is_null($file) || !file_exists($file)) {
notFound();
return;
}
}
switch ($type)
{
case 'jpg':
header('Content-Type: image/jpeg');
//by default, we don't cache
$thumbnailCacheFullpath = null;
if ( isset($config['cops_thumbnail_cache_directory']) && $config['cops_thumbnail_cache_directory'] !== '' ) {
$thumbnailCacheFullpath = $config['cops_thumbnail_cache_directory'];
//if multiple databases, add a subfolder with the database ID
$thumbnailCacheFullpath .= !is_null(GetUrlParam (DB)) ? 'db-' . GetUrlParam (DB) . DIRECTORY_SEPARATOR : '';
//when there are lots of thumbnails, it's better to save files in subfolders, so if the book's uuid is
//"01234567-89ab-cdef-0123-456789abcdef", we will save the thumbnail in .../0/12/34567-89ab-cdef-0123-456789abcdef-...
$thumbnailCacheFullpath .= substr($book->uuid, 0, 1) . DIRECTORY_SEPARATOR . substr($book->uuid, 1, 2) . DIRECTORY_SEPARATOR;
//check if cache folder exists or create it
if ( file_exists($thumbnailCacheFullpath) || mkdir($thumbnailCacheFullpath, 0700, true) ) {
//we name the thumbnail from the book's uuid and it's dimensions (width and/or height)
$thumbnailCacheName = substr($book->uuid, 3) . '-' . getURLParam('width') . 'x' . getURLParam('height') . '.jpg';
$thumbnailCacheFullpath = $thumbnailCacheFullpath . $thumbnailCacheName;
} else {
//error creating the folder, so we don't cache
$thumbnailCacheFullpath = null;
}
}
if ( $thumbnailCacheFullpath !== null && file_exists($thumbnailCacheFullpath) ) {
//return the already cached thumbnail
readfile( $thumbnailCacheFullpath );
return;
}
if ($book->getThumbnail (getURLParam('width'), getURLParam('height'), $thumbnailCacheFullpath)) {
//if we don't cache the thumbnail, imagejpeg() in $book->getThumbnail() already return the image data
if ( $thumbnailCacheFullpath === null ) {
// The cover had to be resized
return;
} else {
//return the just cached thumbnail
readfile( $thumbnailCacheFullpath );
return;
}
}
break;
default:
$data = $book->getDataById($idData);
header('Content-Type: ' . $data->getMimeType());
break;
}
$file = $book->getFilePath($type, $idData, true);
if (!$viewOnly && $type == 'epub' && $config['cops_update_epub-metadata']) {
$book->getUpdatedEpub($idData);
return;
}
if ($type == 'jpg') {
header('Content-Disposition: filename="' . basename($file) . '"');
} elseif ($viewOnly) {
header('Content-Disposition: inline');
} else {
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
}
$dir = $config['calibre_internal_directory'];
if (empty($config['calibre_internal_directory'])) {
$dir = Base::getDbDirectory();
}
if (empty($config['cops_x_accel_redirect'])) {
$filename = $dir . $file;
$fp = fopen($filename, 'rb');
header('Content-Length: ' . filesize($filename));
fpassthru($fp);
} else {
header($config['cops_x_accel_redirect'] . ': ' . $dir . $file);
}