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.

244 lines
11KB

  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. $database = GetUrlParam (DB);
  63. $out ["coverurl"] = Data::getLink ($book, "jpg", "image/jpeg", Link::OPDS_IMAGE_TYPE, "cover.jpg", NULL)->hrefXhtml ();
  64. $out ["thumbnailurl"] = Data::getLink ($book, "jpg", "image/jpeg", Link::OPDS_THUMBNAIL_TYPE, "cover.jpg", NULL, NULL, $config['cops_html_thumbnail_height'] * 2)->hrefXhtml ();
  65. $out ["content"] = $book->getComment (false);
  66. $out ["datas"] = array ();
  67. $dataKindle = $book->GetMostInterestingDataToSendToKindle ();
  68. foreach ($book->getDatas() as $data) {
  69. $tab = array ("id" => $data->id, "format" => $data->format, "url" => $data->getHtmlLink (), "mail" => 0, "readerUrl" => "");
  70. if (!empty ($config['cops_mail_configuration']) && !is_null ($dataKindle) && $data->id == $dataKindle->id) {
  71. $tab ["mail"] = 1;
  72. }
  73. if ($data->format == "EPUB") {
  74. $tab ["readerUrl"] = "epubreader.php?data={$data->id}&db={$database}";
  75. }
  76. array_push ($out ["datas"], $tab);
  77. }
  78. $out ["authors"] = array ();
  79. foreach ($book->getAuthors () as $author) {
  80. $link = new LinkNavigation ($author->getUri ());
  81. array_push ($out ["authors"], array ("name" => $author->name, "url" => $link->hrefXhtml ()));
  82. }
  83. $out ["tags"] = array ();
  84. foreach ($book->getTags () as $tag) {
  85. $link = new LinkNavigation ($tag->getUri ());
  86. array_push ($out ["tags"], array ("name" => $tag->name, "url" => $link->hrefXhtml ()));
  87. }
  88. ;
  89. return $out;
  90. }
  91. public static function getContentArray ($entry) {
  92. if ($entry instanceof EntryBook) {
  93. $out = array ( "title" => $entry->title);
  94. $out ["book"] = self::getBookContentArray ($entry->book);
  95. return $out;
  96. }
  97. return array ( "title" => $entry->title, "content" => $entry->content, "navlink" => $entry->getNavLink (), "number" => $entry->numberOfElement );
  98. }
  99. public static function getContentArrayTypeahead ($page) {
  100. $out = array ();
  101. foreach ($page->entryArray as $entry) {
  102. if ($entry instanceof EntryBook) {
  103. array_push ($out, array ("class" => $entry->className, "title" => $entry->title, "navlink" => $entry->book->getDetailUrl ()));
  104. } else {
  105. if (empty ($entry->className) xor Base::noDatabaseSelected ()) {
  106. array_push ($out, array ("class" => $entry->className, "title" => $entry->title, "navlink" => $entry->getNavLink ()));
  107. } else {
  108. array_push ($out, array ("class" => $entry->className, "title" => $entry->content, "navlink" => $entry->getNavLink ()));
  109. }
  110. }
  111. }
  112. return $out;
  113. }
  114. public static function addCompleteArray ($in) {
  115. global $config;
  116. $out = $in;
  117. $out ["c"] = array ("version" => VERSION, "i18n" => array (
  118. "coverAlt" => localize("i18n.coversection"),
  119. "authorsTitle" => localize("authors.title"),
  120. "bookwordTitle" => localize("bookword.title"),
  121. "tagsTitle" => localize("tags.title"),
  122. "seriesTitle" => localize("series.title"),
  123. "customizeTitle" => localize ("customize.title"),
  124. "aboutTitle" => localize ("about.title"),
  125. "previousAlt" => localize ("paging.previous.alternate"),
  126. "nextAlt" => localize ("paging.next.alternate"),
  127. "searchAlt" => localize ("search.alternate"),
  128. "sortAlt" => localize ("sort.alternate"),
  129. "homeAlt" => localize ("home.alternate"),
  130. "cogAlt" => localize ("cog.alternate"),
  131. "permalinkAlt" => localize ("permalink.alternate"),
  132. "publisherName" => localize("publisher.name"),
  133. "pubdateTitle" => localize("pubdate.title"),
  134. "languagesTitle" => localize("language.title"),
  135. "contentTitle" => localize("content.summary"),
  136. "sortorderAsc" => localize("search.sortorder.asc"),
  137. "sortorderDesc" => localize("search.sortorder.desc"),
  138. "customizeEmail" => localize("customize.email")),
  139. "url" => array (
  140. "detailUrl" => "index.php?page=13&id={0}&db={1}",
  141. "coverUrl" => "fetch.php?id={0}&db={1}",
  142. "thumbnailUrl" => "fetch.php?height=" . $config['cops_html_thumbnail_height'] . "&id={0}&db={1}"),
  143. "config" => array (
  144. "use_fancyapps" => $config ["cops_use_fancyapps"],
  145. "max_item_per_page" => $config['cops_max_item_per_page'],
  146. "kindleHack" => "",
  147. "server_side_rendering" => useServerSideRendering (),
  148. "html_tag_filter" => $config['cops_html_tag_filter']));
  149. if ($config['cops_thumbnail_handling'] == "1") {
  150. $out ["c"]["url"]["thumbnailUrl"] = $out ["c"]["url"]["coverUrl"];
  151. } else if (!empty ($config['cops_thumbnail_handling'])) {
  152. $out ["c"]["url"]["thumbnailUrl"] = $config['cops_thumbnail_handling'];
  153. }
  154. if (preg_match("/./", $_SERVER['HTTP_USER_AGENT'])) {
  155. $out ["c"]["config"]["kindleHack"] = 'style="text-decoration: none !important;"';
  156. }
  157. return $out;
  158. }
  159. public static function getJson ($complete = false) {
  160. global $config;
  161. $page = getURLParam ("page", Base::PAGE_INDEX);
  162. $query = getURLParam ("query");
  163. $search = getURLParam ("search");
  164. $qid = getURLParam ("id");
  165. $n = getURLParam ("n", "1");
  166. $database = GetUrlParam (DB);
  167. $currentPage = Page::getPage ($page, $qid, $query, $n);
  168. $currentPage->InitializeContent ();
  169. if ($search) {
  170. return self::getContentArrayTypeahead ($currentPage);
  171. }
  172. $out = array ( "title" => $currentPage->title);
  173. $entries = array ();
  174. foreach ($currentPage->entryArray as $entry) {
  175. array_push ($entries, self::getContentArray ($entry));
  176. }
  177. if (!is_null ($currentPage->book)) {
  178. $out ["book"] = self::getFullBookContentArray ($currentPage->book);
  179. }
  180. $out ["databaseId"] = GetUrlParam (DB, "");
  181. $out ["databaseName"] = Base::getDbName ();
  182. if ($out ["databaseId"] == "") {
  183. $out ["databaseName"] = "";
  184. }
  185. $out ["fullTitle"] = $out ["title"];
  186. if ($out ["databaseId"] != "" && $out ["databaseName"] != $out ["fullTitle"]) {
  187. $out ["fullTitle"] = $out ["databaseName"] . " > " . $out ["fullTitle"];
  188. }
  189. $out ["page"] = $page;
  190. $out ["multipleDatabase"] = Base::isMultipleDatabaseEnabled () ? 1 : 0;
  191. $out ["entries"] = $entries;
  192. $out ["isPaginated"] = 0;
  193. if ($currentPage->isPaginated ()) {
  194. $prevLink = $currentPage->getPrevLink ();
  195. $nextLink = $currentPage->getNextLink ();
  196. $out ["isPaginated"] = 1;
  197. $out ["prevLink"] = "";
  198. if (!is_null ($prevLink)) {
  199. $out ["prevLink"] = $prevLink->hrefXhtml ();
  200. }
  201. $out ["nextLink"] = "";
  202. if (!is_null ($nextLink)) {
  203. $out ["nextLink"] = $nextLink->hrefXhtml ();
  204. }
  205. $out ["maxPage"] = $currentPage->getMaxPage ();
  206. $out ["currentPage"] = $currentPage->n;
  207. }
  208. if (!is_null (getURLParam ("complete")) || $complete) {
  209. $out = self::addCompleteArray ($out);
  210. }
  211. $out ["containsBook"] = 0;
  212. if ($currentPage->containsBook ()) {
  213. $out ["containsBook"] = 1;
  214. }
  215. $out["abouturl"] = "index.php" . addURLParameter ("?page=" . Base::PAGE_ABOUT, DB, $database);
  216. if ($page == Base::PAGE_ABOUT) {
  217. $temp = preg_replace ("/\<h1\>About COPS\<\/h1\>/", "<h1>About COPS " . VERSION . "</h1>", file_get_contents('about.html'));
  218. $out ["fullhtml"] = $temp;
  219. }
  220. $out ["homeurl"] = "index.php";
  221. if ($page != Base::PAGE_INDEX && !is_null ($database)) $out ["homeurl"] = $out ["homeurl"] . "?" . addURLParameter ("", DB, $database);
  222. return $out;
  223. }
  224. }