1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* COPS (Calibre OPDS PHP Server) class file |
4
|
|
|
* |
5
|
|
|
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html) |
6
|
|
|
* @author Sébastien Lucas <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
class Data extends Base |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
public $id; |
12
|
|
|
public $name; |
13
|
|
|
public $format; |
14
|
|
|
public $realFormat; |
15
|
|
|
public $extension; |
16
|
|
|
public $book; |
17
|
|
|
|
18
|
|
|
public static $mimetypes = array( |
19
|
|
|
'aac' => 'audio/aac', |
20
|
|
|
'azw' => 'application/x-mobipocket-ebook', |
21
|
|
|
'azw1' => 'application/x-topaz-ebook', |
22
|
|
|
'azw2' => 'application/x-kindle-application', |
23
|
|
|
'azw3' => 'application/x-mobi8-ebook', |
24
|
|
|
'cbz' => 'application/x-cbz', |
25
|
|
|
'cbr' => 'application/x-cbr', |
26
|
|
|
'djv' => 'image/vnd.djvu', |
27
|
|
|
'djvu' => 'image/vnd.djvu', |
28
|
|
|
'doc' => 'application/msword', |
29
|
|
|
'epub' => 'application/epub+zip', |
30
|
|
|
'fb2' => 'text/fb2+xml', |
31
|
|
|
'ibooks'=> 'application/x-ibooks+zip', |
32
|
|
|
'kepub' => 'application/epub+zip', |
33
|
|
|
'kobo' => 'application/x-koboreader-ebook', |
34
|
|
|
'm4a' => 'audio/mp4', |
35
|
|
|
'mobi' => 'application/x-mobipocket-ebook', |
36
|
|
|
'mp3' => 'audio/mpeg', |
37
|
|
|
'lit' => 'application/x-ms-reader', |
38
|
|
|
'lrs' => 'text/x-sony-bbeb+xml', |
39
|
|
|
'lrf' => 'application/x-sony-bbeb', |
40
|
|
|
'lrx' => 'application/x-sony-bbeb', |
41
|
|
|
'ncx' => 'application/x-dtbncx+xml', |
42
|
|
|
'opf' => 'application/oebps-package+xml', |
43
|
|
|
'otf' => 'application/x-font-opentype', |
44
|
|
|
'pdb' => 'application/vnd.palm', |
45
|
|
|
'pdf' => 'application/pdf', |
46
|
|
|
'prc' => 'application/x-mobipocket-ebook', |
47
|
|
|
'rtf' => 'application/rtf', |
48
|
|
|
'svg' => 'image/svg+xml', |
49
|
|
|
'ttf' => 'application/x-font-truetype', |
50
|
|
|
'tpz' => 'application/x-topaz-ebook', |
51
|
|
|
'wav' => 'audio/wav', |
52
|
|
|
'wmf' => 'image/wmf', |
53
|
|
|
'xhtml' => 'application/xhtml+xml', |
54
|
|
|
'xpgt' => 'application/adobe-page-template+xml', |
55
|
|
|
'zip' => 'application/zip' |
56
|
|
|
); |
57
|
|
|
|
58
|
63 |
|
public function __construct($post, $book = null) { |
59
|
63 |
|
$this->id = $post->id; |
60
|
63 |
|
$this->name = $post->name; |
61
|
63 |
|
$this->format = $post->format; |
62
|
63 |
|
$this->realFormat = str_replace ("ORIGINAL_", "", $post->format); |
|
|
|
|
63
|
63 |
|
$this->extension = strtolower ($this->realFormat); |
64
|
63 |
|
$this->book = $book; |
65
|
63 |
|
} |
66
|
|
|
|
67
|
53 |
|
public function isKnownType () { |
68
|
53 |
|
return array_key_exists ($this->extension, self::$mimetypes); |
69
|
|
|
} |
70
|
|
|
|
71
|
53 |
|
public function getMimeType () { |
72
|
53 |
|
$result = "application/octet-stream"; |
|
|
|
|
73
|
53 |
|
if ($this->isKnownType ()) { |
74
|
53 |
|
return self::$mimetypes [$this->extension]; |
75
|
1 |
|
} elseif (function_exists('finfo_open') === true) { |
76
|
1 |
|
$finfo = finfo_open(FILEINFO_MIME_TYPE); |
77
|
|
|
|
78
|
1 |
|
if (is_resource($finfo) === true) |
79
|
|
|
{ |
80
|
1 |
|
$result = finfo_file($finfo, $this->getLocalPath ()); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
finfo_close($finfo); |
84
|
|
|
|
85
|
|
|
} |
86
|
1 |
|
return $result; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
public function isEpubValidOnKobo () { |
90
|
1 |
|
return $this->format == "EPUB" || $this->format == "KEPUB"; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
50 |
|
public function getFilename () { |
94
|
50 |
|
return $this->name . "." . strtolower ($this->format); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
public function getUpdatedFilename () { |
98
|
1 |
|
return $this->book->getAuthorsSort () . " - " . $this->book->title; |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
public function getUpdatedFilenameEpub () { |
102
|
1 |
|
return $this->getUpdatedFilename () . ".epub"; |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
public function getUpdatedFilenameKepub () { |
106
|
1 |
|
$str = $this->getUpdatedFilename () . ".kepub.epub"; |
|
|
|
|
107
|
1 |
|
return str_replace(array(':', '#', '&'), |
108
|
1 |
|
array('-', '-', ' '), $str ); |
109
|
|
|
} |
110
|
|
|
|
111
|
49 |
|
public function getDataLink ($rel, $title = NULL, $view = false) { |
112
|
49 |
|
global $config; |
|
|
|
|
113
|
|
|
|
114
|
49 |
|
if ($rel == Link::OPDS_ACQUISITION_TYPE && $config['cops_use_url_rewriting'] == "1") { |
|
|
|
|
115
|
2 |
|
return $this->getHtmlLinkWithRewriting($title, $view); |
116
|
|
|
} |
117
|
|
|
|
118
|
48 |
|
return self::getLink ($this->book, $this->extension, $this->getMimeType (), $rel, $this->getFilename (), $this->id, $title, NULL, $view); |
119
|
|
|
} |
120
|
|
|
|
121
|
5 |
|
public function getHtmlLink () { |
122
|
5 |
|
return $this->getDataLink(Link::OPDS_ACQUISITION_TYPE)->href; |
123
|
|
|
} |
124
|
|
|
|
125
|
4 |
|
public function getViewHtmlLink () { |
126
|
4 |
|
return $this->getDataLink(Link::OPDS_ACQUISITION_TYPE, NULL, true)->href; |
127
|
|
|
} |
128
|
|
|
|
129
|
2 |
|
public function getLocalPath () { |
130
|
2 |
|
return $this->book->path . "/" . $this->getFilename (); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
2 |
|
public function getHtmlLinkWithRewriting ($title = NULL, $view = false) { |
134
|
2 |
|
global $config; |
|
|
|
|
135
|
|
|
|
136
|
2 |
|
$database = ""; |
|
|
|
|
137
|
2 |
View Code Duplication |
if (!is_null (GetUrlParam (DB))) $database = GetUrlParam (DB) . "/"; |
|
|
|
|
138
|
|
|
|
139
|
2 |
|
$prefix = "download"; |
|
|
|
|
140
|
2 |
|
if ($view) { |
141
|
|
|
$prefix = "view"; |
|
|
|
|
142
|
|
|
} |
143
|
2 |
|
$href = $prefix . "/" . $this->id . "/" . $database; |
|
|
|
|
144
|
|
|
|
145
|
2 |
|
if ($config['cops_provide_kepub'] == "1" && |
|
|
|
|
146
|
2 |
|
$this->isEpubValidOnKobo () && |
147
|
2 |
|
preg_match("/Kobo/", $_SERVER['HTTP_USER_AGENT'])) { |
|
|
|
|
148
|
1 |
|
$href .= rawurlencode ($this->getUpdatedFilenameKepub ()); |
149
|
|
|
} else { |
150
|
2 |
|
$href .= rawurlencode ($this->getFilename ()); |
151
|
|
|
} |
152
|
2 |
|
return new Link ($href, $this->getMimeType (), Link::OPDS_ACQUISITION_TYPE, $title); |
153
|
|
|
} |
154
|
|
|
|
155
|
62 |
View Code Duplication |
public static function getDataByBook ($book) { |
|
|
|
|
156
|
62 |
|
$out = array (); |
157
|
62 |
|
$result = parent::getDb ()->prepare('select id, format, name |
|
|
|
|
158
|
|
|
from data where book = ?'); |
159
|
62 |
|
$result->execute (array ($book->id)); |
160
|
|
|
|
161
|
62 |
|
while ($post = $result->fetchObject ()) |
162
|
|
|
{ |
163
|
62 |
|
array_push ($out, new Data ($post, $book)); |
164
|
|
|
} |
165
|
62 |
|
return $out; |
166
|
|
|
} |
167
|
|
|
|
168
|
19 |
|
public static function handleThumbnailLink ($urlParam, $height) { |
169
|
19 |
|
global $config; |
|
|
|
|
170
|
|
|
|
171
|
19 |
|
if (is_null ($height)) { |
172
|
18 |
|
if (preg_match ('/feed.php/', $_SERVER["SCRIPT_NAME"])) { |
|
|
|
|
173
|
|
|
$height = $config['cops_opds_thumbnail_height']; |
174
|
|
|
} |
175
|
|
|
else |
176
|
|
|
{ |
177
|
18 |
|
$height = $config['cops_html_thumbnail_height']; |
178
|
|
|
} |
179
|
|
|
} |
180
|
19 |
|
if ($config['cops_thumbnail_handling'] != "1") { |
|
|
|
|
181
|
19 |
|
$urlParam = addURLParameter($urlParam, "height", $height); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
19 |
|
return $urlParam; |
185
|
|
|
} |
186
|
|
|
|
187
|
49 |
|
public static function getLink ($book, $type, $mime, $rel, $filename, $idData, $title = NULL, $height = NULL, $view = false) |
188
|
|
|
{ |
189
|
49 |
|
global $config; |
|
|
|
|
190
|
|
|
|
191
|
49 |
|
$urlParam = addURLParameter("", "data", $idData); |
|
|
|
|
192
|
49 |
|
if ($view) $urlParam = addURLParameter($urlParam, "view", 1); |
|
|
|
|
193
|
|
|
|
194
|
49 |
|
if (Base::useAbsolutePath () || |
195
|
|
|
$rel == Link::OPDS_THUMBNAIL_TYPE || |
196
|
49 |
|
($type == "epub" && $config['cops_update_epub-metadata'])) |
|
|
|
|
197
|
|
|
{ |
198
|
49 |
|
if ($type != "jpg") $urlParam = addURLParameter($urlParam, "type", $type); |
|
|
|
|
199
|
49 |
|
if ($rel == Link::OPDS_THUMBNAIL_TYPE) { |
200
|
19 |
|
$urlParam = self::handleThumbnailLink($urlParam, $height); |
201
|
|
|
} |
202
|
49 |
|
$urlParam = addURLParameter($urlParam, "id", $book->id); |
|
|
|
|
203
|
49 |
View Code Duplication |
if (!is_null (GetUrlParam (DB))) $urlParam = addURLParameter ($urlParam, DB, GetUrlParam (DB)); |
|
|
|
|
204
|
49 |
|
if ($config['cops_thumbnail_handling'] != "1" && |
|
|
|
|
205
|
49 |
|
!empty ($config['cops_thumbnail_handling']) && |
206
|
49 |
|
$rel == Link::OPDS_THUMBNAIL_TYPE) { |
207
|
|
|
return new Link ($config['cops_thumbnail_handling'], $mime, $rel, $title); |
208
|
|
|
} else { |
209
|
49 |
|
return new Link ("fetch.php?" . $urlParam, $mime, $rel, $title); |
|
|
|
|
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
else |
213
|
|
|
{ |
214
|
|
|
return new Link (str_replace('%2F','/',rawurlencode ($book->path."/".$filename)), $mime, $rel, $title); |
|
|
|
|
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.