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.

80 lines
2.4KB

  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 ("config.php");
  9. require_once ("base.php");
  10. require_once ("book.php");
  11. require_once ("resources/php-epub-meta/epub.php");
  12. function getComponentContent ($book, $component, $add) {
  13. $data = $book->component ($component);
  14. $callback = function ($m) use ($book, $component, $add) {
  15. $method = $m[1];
  16. $path = $m[2];
  17. $end = "";
  18. if (preg_match ("/^src\s*:/", $method)) {
  19. $end = ")";
  20. }
  21. if (preg_match ("/^#/", $path)) {
  22. return "{$method}'{$path}'{$end}";
  23. }
  24. $hash = "";
  25. if (preg_match ("/^(.+)#(.+)$/", $path, $matches)) {
  26. $path = $matches [1];
  27. $hash = "#" . $matches [2];
  28. }
  29. $comp = $book->getComponentName ($component, $path);
  30. if (!$comp) return "{$method}'#'{$end}";
  31. $out = "{$method}'epubfs.php?{$add}comp={$comp}{$hash}'{$end}";
  32. if ($end) {
  33. return $out;
  34. }
  35. return str_replace ("&", "&amp;", $out);
  36. };
  37. $data = preg_replace_callback ("/(src=)[\"']([^:]*?)[\"']/", $callback, $data);
  38. $data = preg_replace_callback ("/(href=)[\"']([^:]*?)[\"']/", $callback, $data);
  39. $data = preg_replace_callback ("/(\@import\s+)[\"'](.*?)[\"'];/", $callback, $data);
  40. $data = preg_replace_callback ("/(src\s*:\s*url\()(.*?)\)/", $callback, $data);
  41. return $data;
  42. }
  43. if (php_sapi_name() === 'cli') { return; }
  44. $idData = getURLParam ("data", NULL);
  45. $add = "data=$idData&";
  46. if (!is_null (GetUrlParam (DB))) $add .= DB . "=" . GetUrlParam (DB) . "&";
  47. $myBook = Book::getBookByDataId($idData);
  48. $book = new EPub ($myBook->getFilePath ("EPUB", $idData));
  49. $book->initSpineComponent ();
  50. if (!isset ($_GET["comp"])) {
  51. notFound ();
  52. return;
  53. }
  54. $component = $_GET["comp"];
  55. try {
  56. $data = getComponentContent ($book, $component, $add);
  57. $expires = 60*60*24*14;
  58. header("Pragma: public");
  59. header("Cache-Control: maxage=".$expires);
  60. header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
  61. header ("Content-Type: " . $book->componentContentType($component));
  62. echo $data;
  63. }
  64. catch (Exception $e) {
  65. error_log ($e);
  66. notFound ();
  67. }