From 7c8463d297cb76d1c7c2575e32dca5877e58c6a4 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Wed, 8 May 2024 11:01:18 -0500 Subject: [PATCH] Add to some pages --- lib/Formatter.php | 4 -- templates/Header.php | 10 ++- www/artworks/index.php | 27 +++++-- www/collections/get.php | 2 +- www/ebooks/author.php | 8 +-- www/ebooks/ebook.php | 2 +- www/ebooks/index.php | 155 ++++++++++++++++++++++++---------------- www/index.php | 2 +- 8 files changed, 128 insertions(+), 82 deletions(-) diff --git a/lib/Formatter.php b/lib/Formatter.php index e3b9c490..6f92abb7 100644 --- a/lib/Formatter.php +++ b/lib/Formatter.php @@ -36,10 +36,6 @@ class Formatter{ return htmlspecialchars(trim($text ?? ''), ENT_QUOTES, 'utf-8'); } - public static function EscapeXhtmlQueryString(?string $text): string{ - return str_replace('&', '&', trim($text ?? '')); - } - public static function EscapeXml(?string $text): string{ // Accepts a query string that has already been url-encoded. For example, // ?foo=bar+baz&x=y diff --git a/templates/Header.php b/templates/Header.php index 6563087d..6c6abec5 100644 --- a/templates/Header.php +++ b/templates/Header.php @@ -11,6 +11,7 @@ $feedUrl = $feedUrl ?? null; $feedTitle = $feedTitle ?? ''; $isErrorPage = $isErrorPage ?? false; $downloadUrl = $downloadUrl ?? null; +$canonicalUrl = $canonicalUrl ?? 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. @@ -55,6 +56,9 @@ if(!$isXslt){ + + + @@ -66,9 +70,9 @@ if(!$isXslt){ - - - + + + diff --git a/www/artworks/index.php b/www/artworks/index.php index e02144a9..fefeaa8f 100644 --- a/www/artworks/index.php +++ b/www/artworks/index.php @@ -97,8 +97,23 @@ try{ $queryStringParams['per-page'] = $perPage; } + if($page > 1){ + $queryStringParams['page'] = $page; + } + + ksort($queryStringParams); + $queryString = http_build_query($queryStringParams); + unset($queryStringParams['page']); + $queryStringWithoutPage = http_build_query($queryStringParams); + + $canonicalUrl = SITE_URL . '/artworks'; + + if($queryString != ''){ + $canonicalUrl .= '?' . $queryString; + } + $pages = ceil($totalArtworkCount / $perPage); if($pages > 0 && $page > $pages){ throw new Exceptions\PageOutOfBoundsException(); @@ -106,14 +121,14 @@ try{ } catch(Exceptions\PageOutOfBoundsException){ $url = '/artworks?page=' . $pages; - if($queryString != ''){ - $url .= '&' . $queryString; + if($queryStringWithoutPage != ''){ + $url .= '&' . $queryStringWithoutPage; } header('Location: ' . $url); exit(); } -?> $pageTitle, 'artwork' => true, 'description' => $pageDescription]) ?> +?> $pageTitle, 'artwork' => true, 'description' => $pageDescription, 'canonicalUrl' => $canonicalUrl]) ?>

Browse U.S. Public Domain Artwork

@@ -167,13 +182,13 @@ catch(Exceptions\PageOutOfBoundsException){ 0){ ?>
diff --git a/www/collections/get.php b/www/collections/get.php index 7abb19af..ecee2588 100644 --- a/www/collections/get.php +++ b/www/collections/get.php @@ -28,7 +28,7 @@ try{ $pageDescription = 'A list of free ebooks in the ' . Formatter::EscapeHtml($collectionName) . ' ' . $collectionType; $pageHeader = 'Free Ebooks in the ' . Formatter::EscapeHtml($collectionName) . ' ' . ucfirst($collectionType); - $feedUrl = '/collections/' . Formatter::EscapeHtml($collection); + $feedUrl = '/collections/' . $collection; $feedTitle = 'Standard Ebooks - Ebooks in the ' . Formatter::EscapeHtml($collectionName) . ' ' . $collectionType; } catch(Exceptions\CollectionNotFoundException){ diff --git a/www/ebooks/author.php b/www/ebooks/author.php index 879e9793..b7401244 100644 --- a/www/ebooks/author.php +++ b/www/ebooks/author.php @@ -19,17 +19,17 @@ try{ } $author = strip_tags($ebooks[0]->AuthorsHtml); - $authorUrl = Formatter::EscapeHtml($ebooks[0]->AuthorsUrl); + $authorUrl = $ebooks[0]->AuthorsUrl; } catch(Exceptions\AuthorNotFoundException){ Template::Emit404(); } -?> 'Ebooks by ' . $author, 'feedUrl' => str_replace('/ebooks/', '/authors/', $authorUrl), 'feedTitle' => 'Standard Ebooks - Ebooks by ' . $author, 'highlight' => 'ebooks', 'description' => 'All of the Standard Ebooks ebooks by ' . $author]) ?> +?> 'Ebooks by ' . $author, 'feedUrl' => str_replace('/ebooks/', '/authors/', $authorUrl), 'feedTitle' => 'Standard Ebooks - Ebooks by ' . $author, 'highlight' => 'ebooks', 'description' => 'All of the Standard Ebooks ebooks by ' . $author, 'canonicalUrl' => SITE_URL . $authorUrl]) ?>

Ebooks by AuthorsHtml ?>

- Download collection - Feeds for this author + Download collection + Feeds for this author

$ebooks, 'view' => VIEW_GRID]) ?>

We also have bulk ebook downloads and a list of collections available, as well as ebook catalog feeds for use directly in your ereader app or RSS reader.

diff --git a/www/ebooks/ebook.php b/www/ebooks/ebook.php index 618050bd..a1eab509 100644 --- a/www/ebooks/ebook.php +++ b/www/ebooks/ebook.php @@ -107,7 +107,7 @@ catch(Exceptions\SeeOtherEbookException $ex){ catch(Exceptions\EbookNotFoundException){ Template::Emit404(); } -?> strip_tags($ebook->TitleWithCreditsHtml) . ' - Free ebook download', 'ogType' => 'book', 'coverUrl' => $ebook->DistCoverUrl, 'highlight' => 'ebooks', 'description' => 'Free epub ebook download of the Standard Ebooks edition of ' . $ebook->Title . ': ' . $ebook->Description]) ?> +?> strip_tags($ebook->TitleWithCreditsHtml) . ' - Free ebook download', 'ogType' => 'book', 'coverUrl' => $ebook->DistCoverUrl, 'highlight' => 'ebooks', 'description' => 'Free epub ebook download of the Standard Ebooks edition of ' . $ebook->Title . ': ' . $ebook->Description, 'canonicalUrl' => SITE_URL . $ebook->Url]) ?>
diff --git a/www/ebooks/index.php b/www/ebooks/index.php index 1ba89595..6ce9f8cf 100644 --- a/www/ebooks/index.php +++ b/www/ebooks/index.php @@ -8,69 +8,100 @@ $tags = HttpInput::GetArray('tags') ?? []; $view = HttpInput::Str(GET, 'view'); $sort = EbookSort::tryFrom(HttpInput::Str(GET, 'sort') ?? ''); $queryString = ''; +$queryStringParams = []; -if($page <= 0){ - $page = 1; +try{ + if($page <= 0){ + $page = 1; + } + + if($perPage != EBOOKS_PER_PAGE && $perPage != 24 && $perPage != 48){ + $perPage = EBOOKS_PER_PAGE; + } + + // If we're passed string values that are the same as the defaults, + // set them to null so that we can have cleaner query strings in the navigation footer + if($view !== null){ + $view = mb_strtolower($view); + } + + if($view === 'grid'){ + $view = null; + } + + if($sort == EbookSort::Newest){ + $sort = null; + } + + if(sizeof($tags) == 1 && mb_strtolower($tags[0]) == 'all'){ + $tags = []; + } + + $ebooks = Library::FilterEbooks($query != '' ? $query : null, $tags, $sort); + $pageTitle = 'Browse Standard Ebooks'; + $pageHeader = 'Browse Ebooks'; + $pages = ceil(sizeof($ebooks) / $perPage); + $totalEbooks = sizeof($ebooks); + $ebooks = array_slice($ebooks, ($page - 1) * $perPage, $perPage); + + + if($page > 1){ + $pageTitle .= ', page ' . $page; + } + + $pageDescription = 'Page ' . $page . ' of the Standard Ebooks free ebook library'; + + if($query != ''){ + $queryStringParams['query'] = $query; + } + + if(sizeof($tags) > 0){ + $queryStringParams['tags'] = $tags; + } + + if($view !== null){ + $queryStringParams['view'] = $view; + } + + if($sort !== null){ + $queryStringParams['sort'] = $sort->value; + } + + if($perPage !== EBOOKS_PER_PAGE){ + $queryStringParams['per-page'] = $perPage; + } + + if($page > 1){ + $queryStringParams['page'] = $page; + } + + ksort($queryStringParams); + + $queryString = http_build_query($queryStringParams); + + unset($queryStringParams['page']); + $queryStringWithoutPage = http_build_query($queryStringParams); + + $canonicalUrl = SITE_URL . '/ebooks'; + + if($queryString != ''){ + $canonicalUrl .= '?' . $queryString; + } + + if($pages > 0 && $page > $pages){ + throw new Exceptions\PageOutOfBoundsException(); + } } +catch(Exceptions\PageOutOfBoundsException){ + $url = '/ebooks?page=' . $pages; + if($queryStringWithoutPage != ''){ + $url .= '&' . $queryStringWithoutPage; + } -if($perPage != EBOOKS_PER_PAGE && $perPage != 24 && $perPage != 48){ - $perPage = EBOOKS_PER_PAGE; + header('Location: ' . $url); + exit(); } - -// If we're passed string values that are the same as the defaults, -// set them to null so that we can have cleaner query strings in the navigation footer -if($view !== null){ - $view = mb_strtolower($view); -} - -if($view === 'grid'){ - $view = null; -} - -if($sort == EbookSort::Newest){ - $sort = null; -} - -if(sizeof($tags) == 1 && mb_strtolower($tags[0]) == 'all'){ - $tags = []; -} - -$ebooks = Library::FilterEbooks($query != '' ? $query : null, $tags, $sort); -$pageTitle = 'Browse Standard Ebooks'; -$pageHeader = 'Browse Ebooks'; -$pages = ceil(sizeof($ebooks) / $perPage); -$totalEbooks = sizeof($ebooks); -$ebooks = array_slice($ebooks, ($page - 1) * $perPage, $perPage); - -if($page > 1){ - $pageTitle .= ', page ' . $page; -} - -$pageDescription = 'Page ' . $page . ' of the Standard Ebooks free ebook library'; - -if($query != ''){ - $queryString .= '&query=' . urlencode($query); -} - -foreach($tags as $tag){ - $queryString .= '&tags[]=' . urlencode($tag); -} - -if($view !== null){ - $queryString .= '&view=' . urlencode($view); -} - -if($sort !== null){ - $queryString .= '&sort=' . urlencode($sort->value); -} - -if($perPage !== EBOOKS_PER_PAGE){ - $queryString .= '&per-page=' . urlencode((string)$perPage); -} - -$queryString = preg_replace('/^&/ius', '', $queryString); - -?> $pageTitle, 'highlight' => 'ebooks', 'description' => $pageDescription]) ?> +?> $pageTitle, 'highlight' => 'ebooks', 'description' => $pageDescription, 'canonicalUrl' => $canonicalUrl]) ?>

@@ -86,13 +117,13 @@ $queryString = preg_replace('/^&/ius', '', $queryString); 0){ ?> diff --git a/www/index.php b/www/index.php index e1624a85..20d85ab3 100644 --- a/www/index.php +++ b/www/index.php @@ -1,4 +1,4 @@ - 'Free and liberated ebooks, carefully produced for the true book lover. Download free ebooks with professional-quality formatting and typography, in formats compatible with your ereader.']) ?> + 'Free and liberated ebooks, carefully produced for the true book lover. Download free ebooks with professional-quality formatting and typography, in formats compatible with your ereader.', 'canonicalUrl' => SITE_URL]) ?>

Free and liberated ebooks,
carefully produced for the true book lover.