Calibre OPDS (and HTML) PHP Server : web-based light alternative to Calibre content server / Calibre2OPDS to serve ebooks (epub, mobi, pdf, ...) http://blog.slucas.fr/en/oss/calibre-opds-php-server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
6.8KB

  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 <sebastien@slucas.fr>
  7. */
  8. require_once('base.php');
  9. class Data extends Base {
  10. public $id;
  11. public $name;
  12. public $format;
  13. public $realFormat;
  14. public $extension;
  15. public $book;
  16. public static $mimetypes = array(
  17. 'azw' => 'application/x-mobipocket-ebook',
  18. 'azw1' => 'application/x-topaz-ebook',
  19. 'azw2' => 'application/x-kindle-application',
  20. 'azw3' => 'application/x-mobi8-ebook',
  21. 'cbz' => 'application/x-cbz',
  22. 'cbr' => 'application/x-cbr',
  23. 'djv' => 'image/vnd.djvu',
  24. 'djvu' => 'image/vnd.djvu',
  25. 'doc' => 'application/msword',
  26. 'epub' => 'application/epub+zip',
  27. 'fb2' => 'text/fb2+xml',
  28. 'ibooks'=> 'application/x-ibooks+zip',
  29. 'kepub' => 'application/epub+zip',
  30. 'kobo' => 'application/x-koboreader-ebook',
  31. 'mobi' => 'application/x-mobipocket-ebook',
  32. 'lit' => 'application/x-ms-reader',
  33. 'lrs' => 'text/x-sony-bbeb+xml',
  34. 'lrf' => 'application/x-sony-bbeb',
  35. 'lrx' => 'application/x-sony-bbeb',
  36. 'ncx' => 'application/x-dtbncx+xml',
  37. 'opf' => 'application/oebps-package+xml',
  38. 'otf' => 'application/x-font-opentype',
  39. 'pdb' => 'application/vnd.palm',
  40. 'pdf' => 'application/pdf',
  41. 'prc' => 'application/x-mobipocket-ebook',
  42. 'rtf' => 'application/rtf',
  43. 'svg' => 'image/svg+xml',
  44. 'ttf' => 'application/x-font-truetype',
  45. 'tpz' => 'application/x-topaz-ebook',
  46. 'wmf' => 'image/wmf',
  47. 'xhtml' => 'application/xhtml+xml',
  48. 'xpgt' => 'application/adobe-page-template+xml',
  49. 'zip' => 'application/zip'
  50. );
  51. public function __construct($post, $book = null) {
  52. $this->id = $post->id;
  53. $this->name = $post->name;
  54. $this->format = $post->format;
  55. $this->realFormat = str_replace ("ORIGINAL_", "", $post->format);
  56. $this->extension = strtolower ($this->realFormat);
  57. $this->book = $book;
  58. }
  59. public function isKnownType () {
  60. return array_key_exists ($this->extension, self::$mimetypes);
  61. }
  62. public function getMimeType () {
  63. $result = "application/octet-stream";
  64. if ($this->isKnownType ()) {
  65. return self::$mimetypes [$this->extension];
  66. } elseif (function_exists('finfo_open') === true) {
  67. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  68. if (is_resource($finfo) === true)
  69. {
  70. $result = finfo_file($finfo, $this->getLocalPath ());
  71. }
  72. finfo_close($finfo);
  73. }
  74. return $result;
  75. }
  76. public function isEpubValidOnKobo () {
  77. return $this->format == "EPUB" || $this->format == "KEPUB";
  78. }
  79. public function getFilename () {
  80. return $this->name . "." . strtolower ($this->format);
  81. }
  82. public function getUpdatedFilename () {
  83. return $this->book->getAuthorsSort () . " - " . $this->book->title;
  84. }
  85. public function getUpdatedFilenameEpub () {
  86. return $this->getUpdatedFilename () . ".epub";
  87. }
  88. public function getUpdatedFilenameKepub () {
  89. return $this->getUpdatedFilename () . ".kepub.epub";
  90. }
  91. public function getDataLink ($rel, $title = NULL) {
  92. global $config;
  93. if ($rel == Link::OPDS_ACQUISITION_TYPE && $config['cops_use_url_rewriting'] == "1") {
  94. return $this->getHtmlLinkWithRewriting($title);
  95. }
  96. return self::getLink ($this->book, $this->extension, $this->getMimeType (), $rel, $this->getFilename (), $this->id, $title);
  97. }
  98. public function getHtmlLink () {
  99. return $this->getDataLink(Link::OPDS_ACQUISITION_TYPE)->href;
  100. }
  101. public function getLocalPath () {
  102. return $this->book->path . "/" . $this->getFilename ();
  103. }
  104. public function getHtmlLinkWithRewriting ($title = NULL) {
  105. global $config;
  106. $database = "";
  107. if (!is_null (GetUrlParam (DB))) $database = GetUrlParam (DB) . "/";
  108. $href = "download/" . $this->id . "/" . $database;
  109. if ($config['cops_provide_kepub'] == "1" &&
  110. $this->isEpubValidOnKobo () &&
  111. preg_match("/Kobo/", $_SERVER['HTTP_USER_AGENT'])) {
  112. $href .= rawurlencode ($this->getUpdatedFilenameKepub ());
  113. } else {
  114. $href .= rawurlencode ($this->getFilename ());
  115. }
  116. return new Link ($href, $this->getMimeType (), Link::OPDS_ACQUISITION_TYPE, $title);
  117. }
  118. public static function getDataByBook ($book) {
  119. $out = array ();
  120. $result = parent::getDb ()->prepare('select id, format, name
  121. from data where book = ?');
  122. $result->execute (array ($book->id));
  123. while ($post = $result->fetchObject ())
  124. {
  125. array_push ($out, new Data ($post, $book));
  126. }
  127. return $out;
  128. }
  129. public static function handleThumbnailLink ($urlParam, $height) {
  130. global $config;
  131. if (is_null ($height)) {
  132. if (preg_match ('/feed.php/', $_SERVER["SCRIPT_NAME"])) {
  133. $height = $config['cops_opds_thumbnail_height'];
  134. }
  135. else
  136. {
  137. $height = $config['cops_html_thumbnail_height'];
  138. }
  139. }
  140. if ($config['cops_thumbnail_handling'] != "1") {
  141. $urlParam = addURLParameter($urlParam, "height", $height);
  142. }
  143. return $urlParam;
  144. }
  145. public static function getLink ($book, $type, $mime, $rel, $filename, $idData, $title = NULL, $height = NULL)
  146. {
  147. global $config;
  148. $urlParam = addURLParameter("", "data", $idData);
  149. if (Base::useAbsolutePath () ||
  150. $rel == Link::OPDS_THUMBNAIL_TYPE ||
  151. ($type == "epub" && $config['cops_update_epub-metadata']))
  152. {
  153. if ($type != "jpg") $urlParam = addURLParameter($urlParam, "type", $type);
  154. if ($rel == Link::OPDS_THUMBNAIL_TYPE) {
  155. $urlParam = self::handleThumbnailLink($urlParam, $height);
  156. }
  157. $urlParam = addURLParameter($urlParam, "id", $book->id);
  158. if (!is_null (GetUrlParam (DB))) $urlParam = addURLParameter ($urlParam, DB, GetUrlParam (DB));
  159. if ($config['cops_thumbnail_handling'] != "1" &&
  160. !empty ($config['cops_thumbnail_handling']) &&
  161. $rel == Link::OPDS_THUMBNAIL_TYPE) {
  162. return new Link ($config['cops_thumbnail_handling'], $mime, $rel, $title);
  163. } else {
  164. return new Link ("fetch.php?" . $urlParam, $mime, $rel, $title);
  165. }
  166. }
  167. else
  168. {
  169. return new Link (str_replace('%2F','/',rawurlencode ($book->path."/".$filename)), $mime, $rel, $title);
  170. }
  171. }
  172. }