forked from seblucas/cops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.php
203 lines (172 loc) · 6.83 KB
/
data.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
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
<?php
/**
* COPS (Calibre OPDS PHP Server) class file
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Sébastien Lucas <[email protected]>
*/
require_once('base.php');
class Data extends Base {
public $id;
public $name;
public $format;
public $realFormat;
public $extension;
public $book;
public static $mimetypes = array(
'azw' => 'application/x-mobipocket-ebook',
'azw1' => 'application/x-topaz-ebook',
'azw2' => 'application/x-kindle-application',
'azw3' => 'application/x-mobi8-ebook',
'cbz' => 'application/x-cbz',
'cbr' => 'application/x-cbr',
'djv' => 'image/vnd.djvu',
'djvu' => 'image/vnd.djvu',
'doc' => 'application/msword',
'epub' => 'application/epub+zip',
'fb2' => 'text/fb2+xml',
'ibooks'=> 'application/x-ibooks+zip',
'kepub' => 'application/epub+zip',
'kobo' => 'application/x-koboreader-ebook',
'mobi' => 'application/x-mobipocket-ebook',
'lit' => 'application/x-ms-reader',
'lrs' => 'text/x-sony-bbeb+xml',
'lrf' => 'application/x-sony-bbeb',
'lrx' => 'application/x-sony-bbeb',
'ncx' => 'application/x-dtbncx+xml',
'opf' => 'application/oebps-package+xml',
'otf' => 'application/x-font-opentype',
'pdb' => 'application/vnd.palm',
'pdf' => 'application/pdf',
'prc' => 'application/x-mobipocket-ebook',
'rtf' => 'application/rtf',
'svg' => 'image/svg+xml',
'ttf' => 'application/x-font-truetype',
'tpz' => 'application/x-topaz-ebook',
'wmf' => 'image/wmf',
'xhtml' => 'application/xhtml+xml',
'xpgt' => 'application/adobe-page-template+xml',
'zip' => 'application/zip'
);
public function __construct($post, $book = null) {
$this->id = $post->id;
$this->name = $post->name;
$this->format = $post->format;
$this->realFormat = str_replace ("ORIGINAL_", "", $post->format);
$this->extension = strtolower ($this->realFormat);
$this->book = $book;
}
public function isKnownType () {
return array_key_exists ($this->extension, self::$mimetypes);
}
public function getMimeType () {
$result = "application/octet-stream";
if ($this->isKnownType ()) {
return self::$mimetypes [$this->extension];
} elseif (function_exists('finfo_open') === true) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
if (is_resource($finfo) === true)
{
$result = finfo_file($finfo, $this->getLocalPath ());
}
finfo_close($finfo);
}
return $result;
}
public function isEpubValidOnKobo () {
return $this->format == "EPUB" || $this->format == "KEPUB";
}
public function getFilename () {
return $this->name . "." . strtolower ($this->format);
}
public function getUpdatedFilename () {
return $this->book->getAuthorsSort () . " - " . $this->book->title;
}
public function getUpdatedFilenameEpub () {
return $this->getUpdatedFilename () . ".epub";
}
public function getUpdatedFilenameKepub () {
return $this->getUpdatedFilename () . ".kepub.epub";
}
public function getDataLink ($rel, $title = NULL) {
global $config;
if ($rel == Link::OPDS_ACQUISITION_TYPE && $config['cops_use_url_rewriting'] == "1") {
return $this->getHtmlLinkWithRewriting($title);
}
return self::getLink ($this->book, $this->extension, $this->getMimeType (), $rel, $this->getFilename (), $this->id, $title);
}
public function getHtmlLink () {
return $this->getDataLink(Link::OPDS_ACQUISITION_TYPE)->href;
}
public function getLocalPath () {
return $this->book->path . "/" . $this->getFilename ();
}
public function getHtmlLinkWithRewriting ($title = NULL) {
global $config;
$database = "";
if (!is_null (GetUrlParam (DB))) $database = GetUrlParam (DB) . "/";
$href = "download/" . $this->id . "/" . $database;
if ($config['cops_provide_kepub'] == "1" &&
$this->isEpubValidOnKobo () &&
preg_match("/Kobo/", $_SERVER['HTTP_USER_AGENT'])) {
$href .= urlencode ($this->getUpdatedFilenameKepub ());
} else {
$href .= urlencode ($this->getFilename ());
}
return new Link ($href, $this->getMimeType (), Link::OPDS_ACQUISITION_TYPE, $title);
}
public static function getDataByBook ($book) {
$out = array ();
$result = parent::getDb ()->prepare('select id, format, name
from data where book = ?');
$result->execute (array ($book->id));
while ($post = $result->fetchObject ())
{
array_push ($out, new Data ($post, $book));
}
return $out;
}
public static function handleThumbnailLink ($urlParam, $height) {
global $config;
if (is_null ($height)) {
if (preg_match ('/feed.php/', $_SERVER["SCRIPT_NAME"])) {
$height = $config['cops_opds_thumbnail_height'];
}
else
{
$height = $config['cops_html_thumbnail_height'];
}
}
if ($config['cops_thumbnail_handling'] != "1") {
$urlParam = addURLParameter($urlParam, "height", $height);
}
return $urlParam;
}
public static function getLink ($book, $type, $mime, $rel, $filename, $idData, $title = NULL, $height = NULL)
{
global $config;
$urlParam = addURLParameter("", "data", $idData);
if (Base::useAbsolutePath () ||
$rel == Link::OPDS_THUMBNAIL_TYPE ||
($type == "epub" && $config['cops_update_epub-metadata']))
{
if ($type != "jpg") $urlParam = addURLParameter($urlParam, "type", $type);
if ($rel == Link::OPDS_THUMBNAIL_TYPE) {
$urlParam = self::handleThumbnailLink($urlParam, $height);
}
$urlParam = addURLParameter($urlParam, "id", $book->id);
if (!is_null (GetUrlParam (DB))) $urlParam = addURLParameter ($urlParam, DB, GetUrlParam (DB));
if ($config['cops_thumbnail_handling'] != "1" &&
!empty ($config['cops_thumbnail_handling']) &&
$rel == Link::OPDS_THUMBNAIL_TYPE) {
return new Link ($config['cops_thumbnail_handling'], $mime, $rel, $title);
} else {
return new Link ("fetch.php?" . $urlParam, $mime, $rel, $title);
}
}
else
{
return new Link (str_replace('%2F','/',rawurlencode ($book->path."/".$filename)), $mime, $rel, $title);
}
}
}