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.

141 lines
4.4KB

  1. <?php
  2. /**
  3. * COPS (Calibre OPDS PHP Server)
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Gordon Page <gordon@incero.com> with integration/modification by Sébastien Lucas <sebastien@slucas.fr>
  7. */
  8. require_once ("config.php");
  9. require_once ("book.php");
  10. require_once ("data.php");
  11. function notFound () {
  12. header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
  13. header("Status: 404 Not Found");
  14. $_SERVER['REDIRECT_STATUS'] = 404;
  15. }
  16. global $config;
  17. $expires = 60*60*24*14;
  18. header("Pragma: public");
  19. header("Cache-Control: maxage=".$expires);
  20. header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
  21. $bookId = getURLParam ("id", NULL);
  22. $type = getURLParam ("type", "jpg");
  23. $idData = getURLParam ("data", NULL);
  24. if (is_null ($bookId))
  25. {
  26. $book = Book::getBookByDataId($idData);
  27. }
  28. else
  29. {
  30. $book = Book::getBookById($bookId);
  31. }
  32. if (!$book) {
  33. notFound ();
  34. return;
  35. }
  36. if ($book && ($type == "jpg" || empty ($config['calibre_internal_directory']))) {
  37. if ($type == "jpg") {
  38. $file = $book->getFilePath ($type);
  39. } else {
  40. $file = $book->getFilePath ($type, $idData);
  41. }
  42. if (!$file || !file_exists ($file)) {
  43. notFound ();
  44. return;
  45. }
  46. }
  47. switch ($type)
  48. {
  49. case "jpg":
  50. header("Content-Type: image/jpeg");
  51. if (isset($_GET["width"]))
  52. {
  53. $file = $book->getFilePath ($type);
  54. // get image size
  55. if($size = GetImageSize($file)){
  56. $w = $size[0];
  57. $h = $size[1];
  58. //set new size
  59. $nw = $_GET["width"];
  60. if ($nw > $w) { break; }
  61. $nh = ($nw*$h)/$w;
  62. }
  63. else{
  64. //set new size
  65. $nw = "160";
  66. $nh = "120";
  67. }
  68. //draw the image
  69. $src_img = imagecreatefromjpeg($file);
  70. $dst_img = imagecreatetruecolor($nw,$nh);
  71. imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h);//resizing the image
  72. imagejpeg($dst_img,null,80);
  73. imagedestroy($src_img);
  74. imagedestroy($dst_img);
  75. return;
  76. }
  77. if (isset($_GET["height"]))
  78. {
  79. $file = $book->getFilePath ($type);
  80. // get image size
  81. if($size = GetImageSize($file)){
  82. $w = $size[0];
  83. $h = $size[1];
  84. //set new size
  85. $nh = $_GET["height"];
  86. if ($nh > $h) { break; }
  87. $nw = ($nh*$w)/$h;
  88. }
  89. else{
  90. //set new size
  91. $nw = "160";
  92. $nh = "120";
  93. }
  94. //draw the image
  95. $src_img = imagecreatefromjpeg($file);
  96. $dst_img = imagecreatetruecolor($nw,$nh);
  97. imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h);//resizing the image
  98. imagejpeg($dst_img,null,80);
  99. imagedestroy($src_img);
  100. imagedestroy($dst_img);
  101. return;
  102. }
  103. break;
  104. default:
  105. header("Content-Type: " . Data::$mimetypes[$type]);
  106. break;
  107. }
  108. $file = $book->getFilePath ($type, $idData, true);
  109. if ($type == "epub" && $config['cops_update_epub-metadata'])
  110. {
  111. $book->getUpdatedEpub ($idData);
  112. return;
  113. }
  114. if ($type == "jpg") {
  115. header('Content-Disposition: filename="' . basename ($file) . '"');
  116. } else {
  117. header('Content-Disposition: attachment; filename="' . basename ($file) . '"');
  118. }
  119. $dir = $config['calibre_internal_directory'];
  120. if (empty ($config['calibre_internal_directory'])) {
  121. $dir = Base::getDbDirectory ();
  122. }
  123. if (empty ($config['cops_x_accel_redirect'])) {
  124. $filename = $dir . $file;
  125. $fp = fopen($filename, 'rb');
  126. header("Content-Length: " . filesize($filename));
  127. fpassthru($fp);
  128. }
  129. else {
  130. header ($config['cops_x_accel_redirect'] . ": " . $dir . $file);
  131. }