From 9437beaee9784dacb6aff9ee8523dae11aeab722 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Sun, 25 Feb 2024 20:12:20 -0600 Subject: [PATCH] Add thank-you page to ebook downloads --- .gitignore | 2 +- config/apache/standardebooks.org.conf | 20 +++- config/apache/standardebooks.test.conf | 20 +++- lib/EbookFormat.php | 15 +++ templates/Header.php | 4 + www/css/core.css | 24 ++++- www/ebooks/download.php | 127 +++++++++++++++++++++++++ www/ebooks/ebook.php | 8 +- www/ebooks/opensearch.php | 18 ++++ 9 files changed, 222 insertions(+), 16 deletions(-) create mode 100644 lib/EbookFormat.php create mode 100644 www/ebooks/download.php create mode 100644 www/ebooks/opensearch.php diff --git a/.gitignore b/.gitignore index add824c1..817c5458 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ ebooks/* -www/ebooks/* +www/ebooks/*/ www/images/covers/* www/feeds/opds/*.xml www/feeds/opds/*/*.xml diff --git a/config/apache/standardebooks.org.conf b/config/apache/standardebooks.org.conf index c999b287..d5ad085d 100644 --- a/config/apache/standardebooks.org.conf +++ b/config/apache/standardebooks.org.conf @@ -218,6 +218,9 @@ Define webroot /standardebooks.org/web # Redirect latest version of the manual RewriteRule ^/manual/latest(.*) /manual/index.php?url=$1 [L] + # Rewrite ebook downloads + RewriteRule ^/ebooks/(.+?)/download$ /ebooks/download.php?url-path=$1 [QSA] + # List of specific URL rewrites RewriteRule ^/contribute/accepted-ebooks/? /contribute/collections-policy [R=301,L] RewriteRule ^/ebooks/aristotle/the-nicomachean-ethics(/?$|/.+?$) /ebooks/aristotle/nicomachean-ethics$1 [R=301,L] @@ -307,18 +310,25 @@ Define webroot /standardebooks.org/web RewriteRule ^/artworks/([^/\.]+)/([^/\.]+)/edit$ /artworks/edit.php?artist-url-name=$1&artwork-url-name=$2 [L] - # Specific config for /bulk-downloads - + # Specific config for /ebooks///downloads + # Both directives are required XSendFile on - XSendFilePath /standardebooks.org/web/www/bulk-downloads + XSendFilePath ${webroot}/www/ebooks + + + # Specific config for /bulk-downloads + + # Both directives are required + XSendFile on + XSendFilePath ${webroot}/www/bulk-downloads # Specific config for /feeds - + # This must be defined at the top level /feeds/ directory # Both directives are required XSendFile on - XSendFilePath /standardebooks.org/web/www/feeds + XSendFilePath ${webroot}/www/feeds diff --git a/config/apache/standardebooks.test.conf b/config/apache/standardebooks.test.conf index 39a4bd38..7e89ca62 100644 --- a/config/apache/standardebooks.test.conf +++ b/config/apache/standardebooks.test.conf @@ -200,6 +200,9 @@ Define webroot /standardebooks.org/web # Redirect latest version of the manual RewriteRule ^/manual/latest(.*) /manual/index.php?url=$1 [L] + # Rewrite ebook downloads + RewriteRule ^/ebooks/(.+?)/download$ /ebooks/download.php?url-path=$1 [QSA] + # List of specific URL rewrites RewriteRule ^/contribute/accepted-ebooks/? /contribute/collections-policy [R=301,L] RewriteRule ^/ebooks/aristotle/the-nicomachean-ethics(/?$|/.+?$) /ebooks/aristotle/nicomachean-ethics$1 [R=301,L] @@ -289,18 +292,25 @@ Define webroot /standardebooks.org/web RewriteRule ^/artworks/([^/\.]+)/([^/\.]+)/edit$ /artworks/edit.php?artist-url-name=$1&artwork-url-name=$2 [L] - # Specific config for /bulk-downloads - + # Specific config for /ebooks///downloads + # Both directives are required XSendFile on - XSendFilePath /standardebooks.org/web/www/bulk-downloads + XSendFilePath ${webroot}/www/ebooks + + + # Specific config for /bulk-downloads + + # Both directives are required + XSendFile on + XSendFilePath ${webroot}/www/bulk-downloads # Specific config for /feeds - + # This must be defined at the top level /feeds/ directory # Both directives are required XSendFile on - XSendFilePath /standardebooks.org/web/www/feeds + XSendFilePath ${webroot}/www/feeds diff --git a/lib/EbookFormat.php b/lib/EbookFormat.php new file mode 100644 index 00000000..fef29f90 --- /dev/null +++ b/lib/EbookFormat.php @@ -0,0 +1,15 @@ + 'application/x-mobi8-ebook', + default => 'application/epub+zip' + }; + } +} diff --git a/templates/Header.php b/templates/Header.php index ef5f2d33..6563087d 100644 --- a/templates/Header.php +++ b/templates/Header.php @@ -10,6 +10,7 @@ $isXslt = $isXslt ?? false; $feedUrl = $feedUrl ?? null; $feedTitle = $feedTitle ?? ''; $isErrorPage = $isErrorPage ?? false; +$downloadUrl = $downloadUrl ?? null; // As of Sep 2022, all versions of Safari have a bug where if the page is served as XHTML, // then elements download all s instead of the first supported match. @@ -81,6 +82,9 @@ if(!$isXslt){ + + +
diff --git a/www/css/core.css b/www/css/core.css index 0db916d8..034d6694 100644 --- a/www/css/core.css +++ b/www/css/core.css @@ -1490,7 +1490,8 @@ ol.ebooks-list > li p{ hyphens: none; } -ol.ebooks-list > li img{ +ol.ebooks-list > li img, +img.ebook{ box-sizing: border-box; max-width: 100%; height: auto; @@ -2905,6 +2906,21 @@ ul.feed p{ padding-bottom: 0; } +.stinger{ + font-family: "Crimson Pro", serif; + font-style: italic; + font-size: 2rem; +} + +.thank-you-container{ + display: flex; + gap: 2rem; +} + +.thank-you-container picture{ + flex-shrink: 0; +} + @media (hover: none) and (pointer: coarse){ /* target ipads and smartphones without a mouse */ /* For iPad, unset the height so it matches the other elements */ select[multiple]{ @@ -3157,6 +3173,12 @@ ul.feed p{ } } +@media(max-width: 860px){ + .thank-you-container picture{ + display: none; + } +} + @media(max-width: 730px){ form[action="/ebooks"]{ grid-template-columns: auto auto 1fr 1fr; diff --git a/www/ebooks/download.php b/www/ebooks/download.php new file mode 100644 index 00000000..f3cfcc56 --- /dev/null +++ b/www/ebooks/download.php @@ -0,0 +1,127 @@ +KepubUrl; + break; + + case EbookFormat::Azw3: + $downloadUrl = $ebook->Azw3Url; + break; + + case EbookFormat::AdvancedEpub: + $downloadUrl = $ebook->AdvancedEpubUrl; + break; + + case EbookFormat::Epub: + default: + $downloadUrl = $ebook->EpubUrl; + break; + } + + if(!$showThankYouPage){ + // Download the file directly, without showing the thank you page + $downloadPath = WEB_ROOT . $downloadUrl; + + if(!is_file($downloadPath)){ + throw new Exceptions\InvalidFileException(); + } + + // Everything OK, serve the file using Apache. + // The xsendfile Apache module tells Apache to serve the file, including not-modified or etag headers. + // Much more efficient than reading it in PHP and outputting it that way. + header('X-Sendfile: ' . $downloadPath); + header('Content-Type: ' . $format->GetMimeType()); + header('Content-Disposition: attachment; filename="' . basename($downloadPath) . '"'); + exit(); + } + + // Increment local download count, expires in 2 weeks + $downloadCount++; + setcookie('download-count', (string)$downloadCount, ['expires' => strtotime('+2 week'), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => false, 'samesite' => 'Lax']); + + if(!$showThankYouPage){ + exit(); + } +} +catch(Exceptions\InvalidFileException){ + Template::Emit404(); +} +?> $downloadUrl]) ?> +
+

Your download has started!

+
+ + CoverImage2xAvifUrl !== null){ ?> + + The cover for the Standard Ebooks edition of <?= Formatter::EscapeHtml(strip_tags($ebook->TitleWithCreditsHtml)) ?> + +
+

Before you go...

+

Will you make a donation to help us further our mission of creating beautiful, free ebooks?

+

It takes a team of highly-skilled volunteers hours to create and proof each of our ebooks. We couldn’t do it without the financial support of literature lovers like you. Any amount helps further our mission!

+

+ Make a donation towards free literature +

+

Join the Patrons Circle

+

Joining our Patrons Circle gives you access to some awesome book-lover benefits:

+
    +
  • +

    Your name listed on our masthead. (You can also remain anonymous if you prefer.)

    +
  • +
  • +

    Access to our various ebook feeds:

    +
      +
    • +

      Browse and download from the entire Standard Ebooks catalog directly in your ereading app using our OPDS feed.

      +
    • +
    • +

      Get notified of new ebooks in your news client with our Atom or RSS feeds.

      +
    • +
    • +

      Parse and process the feeds to use our ebooks in your personal software projects.

      +
    • +
    +
  • +
  • +

    Access to bulk ebook downloads to easily download whole collections of ebooks at once.

    +
  • +
  • +

    The ability to submit a book for inclusion on our Wanted Ebooks list, once per quarter. (Submissions must conform to our collections policy and are subject to approval.)

    +
  • +
  • +

    The right to periodically vote on a selection from our Wanted Ebooks list to choose an ebook for immediate production. The resulting ebook will be a permanent addition to our online catalog of free digital literature.

    +
  • +
+

+ Join the S.E. Patrons Circle +

+
+
+
+ diff --git a/www/ebooks/ebook.php b/www/ebooks/ebook.php index a8bd4c26..4901e8b2 100644 --- a/www/ebooks/ebook.php +++ b/www/ebooks/ebook.php @@ -228,7 +228,7 @@ catch(Exceptions\EbookNotFoundException){

- Compatible epub All devices and apps except Kindles and Kobos. + Compatible epub All devices and apps except Kindles and Kobos.

@@ -237,7 +237,7 @@ catch(Exceptions\EbookNotFoundException){
  • - azw3 Kindle devices and apps.KindleCoverUrl !== null){ ?> Also download the Kindle cover thumbnail to see the cover in your Kindle’s library. You may be interested in our Kindle FAQ. Also see our Kindle FAQ. + azw3 Kindle devices and apps.KindleCoverUrl !== null){ ?> Also download the Kindle cover thumbnail to see the cover in your Kindle’s library. You may be interested in our Kindle FAQ. Also see our Kindle FAQ.

  • @@ -246,7 +246,7 @@ catch(Exceptions\EbookNotFoundException){
  • - kepub Kobo devices and apps. You may also be interested in our Kobo FAQ. + kepub Kobo devices and apps. You may also be interested in our Kobo FAQ.

  • @@ -255,7 +255,7 @@ catch(Exceptions\EbookNotFoundException){
  • - Advanced epub An advanced format that uses the latest technology not yet fully supported by most ereaders. + Advanced epub An advanced format that uses the latest technology not yet fully supported by most ereaders.

  • diff --git a/www/ebooks/opensearch.php b/www/ebooks/opensearch.php new file mode 100644 index 00000000..3cea6b05 --- /dev/null +++ b/www/ebooks/opensearch.php @@ -0,0 +1,18 @@ +'); +?> + + Standard Ebooks + Search the Standard Ebooks catalog. + Standard Ebooks + en-US + open + UTF-8 + UTF-8 + + + + + +