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.

74 lines
2.2KB

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