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.

207 lines
8.6KB

  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. require_once ("book.php");
  10. class JSONRenderer
  11. {
  12. public static function getBookContentArray ($book) {
  13. global $config;
  14. $i = 0;
  15. $preferedData = array ();
  16. foreach ($config['cops_prefered_format'] as $format)
  17. {
  18. if ($i == 2) { break; }
  19. if ($data = $book->getDataFormat ($format)) {
  20. $i++;
  21. array_push ($preferedData, array ("url" => $data->getHtmlLink (), "name" => $format));
  22. }
  23. }
  24. $publisher = $book->getPublisher();
  25. if (is_null ($publisher)) {
  26. $pn = "";
  27. $pu = "";
  28. } else {
  29. $pn = $publisher->name;
  30. $link = new LinkNavigation ($publisher->getUri ());
  31. $pu = $link->hrefXhtml ();
  32. }
  33. $serie = $book->getSerie ();
  34. if (is_null ($serie)) {
  35. $sn = "";
  36. $scn = "";
  37. $su = "";
  38. } else {
  39. $sn = $serie->name;
  40. $scn = str_format (localize ("content.series.data"), $book->seriesIndex, $serie->name);
  41. $link = new LinkNavigation ($serie->getUri ());
  42. $su = $link->hrefXhtml ();
  43. }
  44. return array ("id" => $book->id,
  45. "hasCover" => $book->hasCover,
  46. "preferedData" => $preferedData,
  47. "rating" => $book->getRating (),
  48. "publisherName" => $pn,
  49. "publisherurl" => $pu,
  50. "pubDate" => $book->getPubDate (),
  51. "languagesName" => $book->getLanguages (),
  52. "authorsName" => $book->getAuthorsName (),
  53. "tagsName" => $book->getTagsName (),
  54. "seriesName" => $sn,
  55. "seriesIndex" => $book->seriesIndex,
  56. "seriesCompleteName" => $scn,
  57. "seriesurl" => $su);
  58. }
  59. public static function getFullBookContentArray ($book) {
  60. global $config;
  61. $out = self::getBookContentArray ($book);
  62. $out ["coverurl"] = Data::getLink ($book, "jpg", "image/jpeg", Link::OPDS_IMAGE_TYPE, "cover.jpg", NULL)->hrefXhtml ();
  63. $out ["thumbnailurl"] = Data::getLink ($book, "jpg", "image/jpeg", Link::OPDS_THUMBNAIL_TYPE, "cover.jpg", NULL, NULL, $config['cops_html_thumbnail_height'] * 2)->hrefXhtml ();
  64. $out ["content"] = $book->getComment (false);
  65. $out ["datas"] = array ();
  66. $dataKindle = $book->GetMostInterestingDataToSendToKindle ();
  67. foreach ($book->getDatas() as $data) {
  68. $tab = array ("id" => $data->id, "format" => $data->format, "url" => $data->getHtmlLink (), "mail" => 0);
  69. if (!empty ($config['cops_mail_configuration']) && !is_null ($dataKindle) && $data->id == $dataKindle->id) {
  70. $tab ["mail"] = 1;
  71. }
  72. array_push ($out ["datas"], $tab);
  73. }
  74. $out ["authors"] = array ();
  75. foreach ($book->getAuthors () as $author) {
  76. $link = new LinkNavigation ($author->getUri ());
  77. array_push ($out ["authors"], array ("name" => $author->name, "url" => $link->hrefXhtml ()));
  78. }
  79. $out ["tags"] = array ();
  80. foreach ($book->getTags () as $tag) {
  81. $link = new LinkNavigation ($tag->getUri ());
  82. array_push ($out ["tags"], array ("name" => $tag->name, "url" => $link->hrefXhtml ()));
  83. }
  84. ;
  85. return $out;
  86. }
  87. public static function getContentArray ($entry) {
  88. if ($entry instanceof EntryBook) {
  89. $out = array ( "title" => $entry->title);
  90. $out ["book"] = self::getBookContentArray ($entry->book);
  91. return $out;
  92. }
  93. return array ( "title" => $entry->title, "content" => $entry->content, "navlink" => $entry->getNavLink () );
  94. }
  95. public static function getJson ($complete = false) {
  96. global $config;
  97. $page = getURLParam ("page", Base::PAGE_INDEX);
  98. $query = getURLParam ("query");
  99. $search = getURLParam ("search");
  100. $qid = getURLParam ("id");
  101. $n = getURLParam ("n", "1");
  102. $database = GetUrlParam (DB);
  103. $currentPage = Page::getPage ($page, $qid, $query, $n);
  104. $currentPage->InitializeContent ();
  105. if ($search) {
  106. return $currentPage->getContentArrayTypeahead ();
  107. }
  108. $out = array ( "title" => $currentPage->title);
  109. $entries = array ();
  110. foreach ($currentPage->entryArray as $entry) {
  111. array_push ($entries, self::getContentArray ($entry));
  112. }
  113. if (!is_null ($currentPage->book)) {
  114. $out ["book"] = self::getFullBookContentArray ($currentPage->book);
  115. }
  116. $out ["databaseId"] = GetUrlParam (DB, "");
  117. $out ["databaseName"] = Base::getDbName ();
  118. $out ["page"] = $page;
  119. $out ["multipleDatabase"] = Base::isMultipleDatabaseEnabled () ? 1 : 0;
  120. $out ["entries"] = $entries;
  121. $out ["isPaginated"] = 0;
  122. if ($currentPage->isPaginated ()) {
  123. $prevLink = $currentPage->getPrevLink ();
  124. $nextLink = $currentPage->getNextLink ();
  125. $out ["isPaginated"] = 1;
  126. $out ["prevLink"] = "";
  127. if (!is_null ($prevLink)) {
  128. $out ["prevLink"] = $prevLink->hrefXhtml ();
  129. }
  130. $out ["nextLink"] = "";
  131. if (!is_null ($nextLink)) {
  132. $out ["nextLink"] = $nextLink->hrefXhtml ();
  133. }
  134. $out ["maxPage"] = $currentPage->getMaxPage ();
  135. $out ["currentPage"] = $currentPage->n;
  136. }
  137. if (!is_null (getURLParam ("complete")) || $complete) {
  138. $out ["c"] = array ("version" => VERSION, "i18n" => array (
  139. "coverAlt" => localize("i18n.coversection"),
  140. "authorsTitle" => localize("authors.title"),
  141. "bookwordTitle" => localize("bookword.title"),
  142. "tagsTitle" => localize("tags.title"),
  143. "seriesTitle" => localize("series.title"),
  144. "customizeTitle" => localize ("customize.title"),
  145. "aboutTitle" => localize ("about.title"),
  146. "previousAlt" => localize ("paging.previous.alternate"),
  147. "nextAlt" => localize ("paging.next.alternate"),
  148. "searchAlt" => localize ("search.alternate"),
  149. "sortAlt" => localize ("sort.alternate"),
  150. "homeAlt" => localize ("home.alternate"),
  151. "cogAlt" => localize ("cog.alternate"),
  152. "permalinkAlt" => localize ("permalink.alternate"),
  153. "publisherName" => localize("publisher.name"),
  154. "pubdateTitle" => localize("pubdate.title"),
  155. "languagesTitle" => localize("language.title"),
  156. "contentTitle" => localize("content.summary"),
  157. "sortorderAsc" => localize("search.sortorder.asc"),
  158. "sortorderDesc" => localize("search.sortorder.desc"),
  159. "customizeEmail" => localize("customize.email")),
  160. "url" => array (
  161. "detailUrl" => "index.php?page=13&id={0}&db={1}",
  162. "coverUrl" => "fetch.php?id={0}&db={1}",
  163. "thumbnailUrl" => "fetch.php?height=" . $config['cops_html_thumbnail_height'] . "&id={0}&db={1}"),
  164. "config" => array (
  165. "use_fancyapps" => $config ["cops_use_fancyapps"],
  166. "max_item_per_page" => $config['cops_max_item_per_page'],
  167. "server_side_rendering" => useServerSideRendering (),
  168. "html_tag_filter" => $config['cops_html_tag_filter']));
  169. if ($config['cops_thumbnail_handling'] == "1") {
  170. $out ["c"]["url"]["thumbnailUrl"] = $out ["c"]["url"]["coverUrl"];
  171. } else if (!empty ($config['cops_thumbnail_handling'])) {
  172. $out ["c"]["url"]["thumbnailUrl"] = $config['cops_thumbnail_handling'];
  173. }
  174. }
  175. $out ["containsBook"] = 0;
  176. if ($currentPage->containsBook ()) {
  177. $out ["containsBook"] = 1;
  178. }
  179. $out["abouturl"] = "index.php" . addURLParameter ("?page=16", DB, $database);
  180. if ($page == Base::PAGE_ABOUT) {
  181. $temp = preg_replace ("/\<h1\>About COPS\<\/h1\>/", "<h1>About COPS " . VERSION . "</h1>", file_get_contents('about.html'));
  182. $out ["fullhtml"] = $temp;
  183. }
  184. $out ["homeurl"] = "index.php";
  185. if ($page != Base::PAGE_INDEX && !is_null ($database)) $out ["homeurl"] = $out ["homeurl"] . "?" . addURLParameter ("", DB, $database);
  186. return $out;
  187. }
  188. }