3 * COPS (Calibre OPDS PHP Server) class file
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Sébastien Lucas <sebastien@slucas.fr>
9 require_once('base.php');
11 class Data extends Base {
19 public static $mimetypes = array(
20 'azw' => 'application/x-mobipocket-ebook',
21 'azw3' => 'application/x-mobipocket-ebook',
22 'cbz' => 'application/x-cbz',
23 'cbr' => 'application/x-cbr',
24 'doc' => 'application/msword',
25 'epub' => 'application/epub+zip',
26 'fb2' => 'text/fb2+xml',
27 'kobo' => 'application/x-koboreader-ebook',
28 'mobi' => 'application/x-mobipocket-ebook',
29 'lit' => 'application/x-ms-reader',
30 'lrs' => 'text/x-sony-bbeb+xml',
31 'lrf' => 'application/x-sony-bbeb',
32 'lrx' => 'application/x-sony-bbeb',
33 'ncx' => 'application/x-dtbncx+xml',
34 'opf' => 'application/oebps-package+xml',
35 'otf' => 'application/x-font-opentype',
36 'pdb' => 'application/vnd.palm',
37 'pdf' => 'application/pdf',
38 'prc' => 'application/x-mobipocket-ebook',
39 'rtf' => 'application/rtf',
40 'svg' => 'image/svg+xml',
41 'ttf' => 'application/x-font-truetype',
43 'xhtml' => 'application/xhtml+xml',
44 'xpgt' => 'application/adobe-page-template+xml',
45 'zip' => 'application/zip',
46 'fb2.zip' => 'application/fb2+zip'
49 public function __construct($post, $book = null) {
50 $this->id = $post->id;
51 $this->name = $post->name;
52 $this->format = $post->format;
53 $this->realFormat = str_replace ("ORIGINAL_", "", $post->format);
54 $this->extension = strtolower ($this->realFormat);
58 public function isKnownType () {
59 return array_key_exists ($this->extension, self::$mimetypes);
62 public function getMimeType () {
63 if ($this->isKnownType ()) {
64 return self::$mimetypes [$this->extension];
66 return "application/octet-stream";
70 public function getFilename () {
71 return $this->name . "." . strtolower ($this->format);
74 public function getUpdatedFilename () {
75 return $this->book->getAuthorsName () . " - " . $this->book->title;
78 public function getUpdatedFilenameEpub () {
79 return $this->getUpdatedFilename () . ".epub";
82 public function getUpdatedFilenameKepub () {
83 return $this->getUpdatedFilename () . ".kepub.epub";
86 public function getDataLink ($rel, $title = NULL) {
87 return self::getBookLink ($this->book, $this->extension, $this->getMimeType (), $rel, $this->getFilename (), $this->id, $title);
90 public function getLocalPath () {
91 return $this->book->path . "/" . $this->getFilename ();
94 public function getHtmlLink () {
96 return self::getBookLink ($this->book, $this->extension, $this->getMimeType (), NULL, $this->getFilename (), $this->id, $this->getFilename())->href;
99 public static function getBookLink ($book, $type, $mime, $rel, $filename, $idData, $title)
102 $encoded_name=rawurlencode($filename);
103 $url = "fetch/$idData/$type/$id/$encoded_name";
104 return new Link ($url,$mime,$rel,$filename);
107 public static function getLink ($book, $type, $mime, $rel, $filename, $idData, $title = NULL, $height = NULL)
111 $urlParam = addURLParameter("", "data", $idData);
113 if (preg_match ('/^\//', Base::getFileDirectory ()) || // Linux /
114 preg_match ('/^\w\:/', Base::getFileDirectory ()) || // Windows X:
115 $rel == Link::OPDS_THUMBNAIL_TYPE ||
116 ($type == "epub" && $config['cops_update_epub-metadata']))
118 if ($type != "jpg") $urlParam = addURLParameter($urlParam, "type", $type);
119 if ($rel == Link::OPDS_THUMBNAIL_TYPE) {
120 if (is_null ($height)) {
121 if (preg_match ('/feed.php/', $_SERVER["SCRIPT_NAME"])) {
122 $height = $config['cops_opds_thumbnail_height'];
126 $height = $config['cops_html_thumbnail_height'];
129 $urlParam = addURLParameter($urlParam, "height", $height);
131 $urlParam = addURLParameter($urlParam, "id", $book->id);
132 return new Link ("fetch.php?" . $urlParam, $mime, $rel, $title);
136 return new Link (str_replace('%2F','/',rawurlencode ($book->path."/".$filename)), $mime, $rel, $title);