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.

146 lines
5.2KB

  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. 'azw3' => 'application/x-mobipocket-ebook',
  19. 'cbz' => 'application/x-cbz',
  20. 'cbr' => 'application/x-cbr',
  21. 'doc' => 'application/msword',
  22. 'epub' => 'application/epub+zip',
  23. 'fb2' => 'text/fb2+xml',
  24. 'kobo' => 'application/x-koboreader-ebook',
  25. 'mobi' => 'application/x-mobipocket-ebook',
  26. 'lit' => 'application/x-ms-reader',
  27. 'lrs' => 'text/x-sony-bbeb+xml',
  28. 'lrf' => 'application/x-sony-bbeb',
  29. 'lrx' => 'application/x-sony-bbeb',
  30. 'ncx' => 'application/x-dtbncx+xml',
  31. 'opf' => 'application/oebps-package+xml',
  32. 'otf' => 'application/x-font-opentype',
  33. 'pdb' => 'application/vnd.palm',
  34. 'pdf' => 'application/pdf',
  35. 'prc' => 'application/x-mobipocket-ebook',
  36. 'rtf' => 'application/rtf',
  37. 'svg' => 'image/svg+xml',
  38. 'ttf' => 'application/x-font-truetype',
  39. 'wmf' => 'image/wmf',
  40. 'xhtml' => 'application/xhtml+xml',
  41. 'xpgt' => 'application/adobe-page-template+xml',
  42. 'zip' => 'application/zip'
  43. );
  44. public function __construct($post, $book = null) {
  45. $this->id = $post->id;
  46. $this->name = $post->name;
  47. $this->format = $post->format;
  48. $this->realFormat = str_replace ("ORIGINAL_", "", $post->format);
  49. $this->extension = strtolower ($this->realFormat);
  50. $this->book = $book;
  51. }
  52. public function isKnownType () {
  53. return array_key_exists ($this->extension, self::$mimetypes);
  54. }
  55. public function getMimeType () {
  56. if ($this->isKnownType ()) {
  57. return self::$mimetypes [$this->extension];
  58. } else {
  59. return "application/octet-stream";
  60. }
  61. }
  62. public function getFilename () {
  63. return $this->name . "." . strtolower ($this->format);
  64. }
  65. public function getUpdatedFilename () {
  66. return $this->book->getAuthorsName () . " - " . $this->book->title;
  67. }
  68. public function getUpdatedFilenameEpub () {
  69. return $this->getUpdatedFilename () . ".epub";
  70. }
  71. public function getUpdatedFilenameKepub () {
  72. return $this->getUpdatedFilename () . ".kepub.epub";
  73. }
  74. public function getDataLink ($rel, $title = NULL) {
  75. return self::getLink ($this->book, $this->extension, $this->getMimeType (), $rel, $this->getFilename (), $this->id, $title);
  76. }
  77. public function getLocalPath () {
  78. return $this->book->path . "/" . $this->getFilename ();
  79. }
  80. public function getHtmlLink () {
  81. global $config;
  82. if ($config['cops_use_url_rewriting'] == "1")
  83. {
  84. $database = "";
  85. if (!is_null (GetUrlParam (DB))) $database = GetUrlParam (DB) . "/";
  86. if ($config['cops_provide_kepub'] == "1" && preg_match("/Kobo/", $_SERVER['HTTP_USER_AGENT'])) {
  87. return "download/" . $this->id . "/" . $database . urlencode ($this->getUpdatedFilenameKepub ());
  88. } else {
  89. return "download/" . $this->id . "/" . $database . urlencode ($this->getFilename ());
  90. }
  91. }
  92. else
  93. {
  94. return self::getLink ($this->book, $this->extension, $this->getMimeType (), NULL, $this->getFilename (), $this->id, NULL)->href;
  95. }
  96. }
  97. public static function getLink ($book, $type, $mime, $rel, $filename, $idData, $title = NULL, $height = NULL)
  98. {
  99. global $config;
  100. $urlParam = addURLParameter("", "data", $idData);
  101. if (preg_match ('/^\//', Base::getDbDirectory ()) || // Linux /
  102. preg_match ('/^\w\:/', Base::getDbDirectory ()) || // Windows X:
  103. $rel == Link::OPDS_THUMBNAIL_TYPE ||
  104. ($type == "epub" && $config['cops_update_epub-metadata']))
  105. {
  106. if ($type != "jpg") $urlParam = addURLParameter($urlParam, "type", $type);
  107. if ($rel == Link::OPDS_THUMBNAIL_TYPE) {
  108. if (is_null ($height)) {
  109. if (preg_match ('/feed.php/', $_SERVER["SCRIPT_NAME"])) {
  110. $height = $config['cops_opds_thumbnail_height'];
  111. }
  112. else
  113. {
  114. $height = $config['cops_html_thumbnail_height'];
  115. }
  116. }
  117. $urlParam = addURLParameter($urlParam, "height", $height);
  118. }
  119. $urlParam = addURLParameter($urlParam, "id", $book->id);
  120. if (!is_null (GetUrlParam (DB))) $urlParam = addURLParameter ($urlParam, DB, GetUrlParam (DB));
  121. return new Link ("fetch.php?" . $urlParam, $mime, $rel, $title);
  122. }
  123. else
  124. {
  125. return new Link (str_replace('%2F','/',rawurlencode ($book->path."/".$filename)), $mime, $rel, $title);
  126. }
  127. }
  128. }