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.

96 lines
2.6KB

  1. <?php
  2. /**
  3. * COPS (Calibre OPDS PHP Server)
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Sébastien Lucas <sebastien@slucas.fr>
  7. */
  8. require_once ("config.php");
  9. require_once ("book.php");
  10. require_once ("data.php");
  11. global $config;
  12. if ($config ['cops_fetch_protect'] == "1") {
  13. session_start();
  14. if (!isset($_SESSION['connected'])) {
  15. notFound ();
  16. return;
  17. }
  18. }
  19. $expires = 60*60*24*14;
  20. header("Pragma: public");
  21. header("Cache-Control: maxage=".$expires);
  22. header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
  23. $bookId = getURLParam ("id", NULL);
  24. $type = getURLParam ("type", "jpg");
  25. $idData = getURLParam ("data", NULL);
  26. if (is_null ($bookId))
  27. {
  28. $book = Book::getBookByDataId($idData);
  29. }
  30. else
  31. {
  32. $book = Book::getBookById($bookId);
  33. }
  34. if (!$book) {
  35. notFound ();
  36. return;
  37. }
  38. if ($book && ($type == "jpg" || empty ($config['calibre_internal_directory']))) {
  39. if ($type == "jpg") {
  40. $file = $book->getFilePath ($type);
  41. } else {
  42. $file = $book->getFilePath ($type, $idData);
  43. }
  44. if (!$file || !file_exists ($file)) {
  45. notFound ();
  46. return;
  47. }
  48. }
  49. switch ($type)
  50. {
  51. case "jpg":
  52. header("Content-Type: image/jpeg");
  53. if ($book->getThumbnail (getURLParam ("width"), getURLParam ("height"))) {
  54. // The cover had to be resized
  55. return;
  56. }
  57. break;
  58. default:
  59. $data = $book->getDataById ($idData);
  60. header("Content-Type: " . $data->getMimeType ());
  61. break;
  62. }
  63. $file = $book->getFilePath ($type, $idData, true);
  64. if ($type == "epub" && $config['cops_update_epub-metadata'])
  65. {
  66. $book->getUpdatedEpub ($idData);
  67. return;
  68. }
  69. if ($type == "jpg") {
  70. header('Content-Disposition: filename="' . basename ($file) . '"');
  71. } else {
  72. header('Content-Disposition: attachment; filename="' . basename ($file) . '"');
  73. }
  74. $dir = $config['calibre_internal_directory'];
  75. if (empty ($config['calibre_internal_directory'])) {
  76. $dir = Base::getDbDirectory ();
  77. }
  78. if (empty ($config['cops_x_accel_redirect'])) {
  79. $filename = $dir . $file;
  80. $fp = fopen($filename, 'rb');
  81. header("Content-Length: " . filesize($filename));
  82. fpassthru($fp);
  83. }
  84. else {
  85. header ($config['cops_x_accel_redirect'] . ": " . $dir . $file);
  86. }