Restructure how incorrect ebook URLs are redirected

This commit is contained in:
Alex Cabal 2024-11-11 13:58:10 -06:00
parent d6a2bdcbc8
commit d4c7703cf0
6 changed files with 62 additions and 72 deletions

View file

@ -15,30 +15,7 @@ $carouselTag = null;
$targetCarouselSize = 5;
try{
$urlPath = trim(str_replace('.', '', HttpInput::Str(GET, 'url-path') ?? ''), '/'); // Contains the portion of the URL (without query string) that comes after `https://standardebooks.org/ebooks/`.
$wwwFilesystemPath = EBOOKS_DIST_PATH . $urlPath; // Path to the deployed WWW files for this ebook.
if($urlPath == '' || mb_stripos($wwwFilesystemPath, EBOOKS_DIST_PATH) !== 0){
// Ensure the path exists and that the root is in our www directory.
throw new Exceptions\EbookNotFoundException();
}
// Were we passed the author and a work but not the translator?
// For example: <https://standardebooks.org/ebooks/omar-khayyam/the-rubaiyat-of-omar-khayyam>
// Instead of: <https://standardebooks.org/ebooks/omar-khayyam/the-rubaiyat-of-omar-khayyam/edward-fitzgerald/edmund-dulac>.
// We can tell because if so, the dir we are passed will exist, but there will be no `src` folder.
if(is_dir($wwwFilesystemPath) && !is_dir($wwwFilesystemPath . '/src')){
/** @var DirectoryIterator $file */
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($wwwFilesystemPath)) as $file){
// This iterator will do a deep scan on the directory. When we hit another directory, the filename will be `.` and the path will contain the directory path.
// We want to find where the `src` directory is, and the directory directly below that will be the final web URL we're looking for.
if($file->getFilename() == '.' && preg_match('|/src$|ius', $file->getPath())){
throw new Exceptions\SeeOtherEbookException(preg_replace(['|' . WEB_ROOT . '|ius', '|/src$|ius'], '', $file->getPath()));
}
}
}
$identifier = EBOOKS_IDENTIFIER_PREFIX . $urlPath;
$identifier = EBOOKS_IDENTIFIER_PREFIX . trim(str_replace('.', '', HttpInput::Str(GET, 'url-path') ?? ''), '/'); // Contains the portion of the URL (without query string) that comes after `https://standardebooks.org/ebooks/`.
$ebook = Ebook::GetByIdentifier($identifier);
@ -68,16 +45,27 @@ try{
if(sizeof($ebook->Tags) > 0){
$carouselTag = $ebook->Tags[rand(0, sizeof($ebook->Tags) - 1)];
}
$carousel = Ebook::GetAllByRelated($ebook, $targetCarouselSize, $carouselTag);
}
catch(Exceptions\SeeOtherEbookException $ex){
http_response_code(301);
header('Location: ' . $ex->Url);
exit();
}
catch(Exceptions\EbookNotFoundException){
// Were we passed the author and a work but not the translator?
// For example: <https://standardebooks.org/ebooks/omar-khayyam/the-rubaiyat-of-omar-khayyam>
// Instead of: <https://standardebooks.org/ebooks/omar-khayyam/the-rubaiyat-of-omar-khayyam/edward-fitzgerald>.
try{
$ebook = Ebook::GetByIdentifierStartingWith($identifier);
// Found, redirect.
http_response_code(301);
header('Location: ' . $ebook->Url);
exit();
}
catch(Exceptions\EbookNotFoundException){
// Still not found, continue.
}
// Are we accessing a placeholder for a Public Domain Day book that is not yet released?
if(array_key_exists($urlPath, PD_DAY_EBOOKS)){
if(array_key_exists($identifier, PD_DAY_EBOOKS)){
require('/standardebooks.org/web/www/ebooks/public-domain-day-placeholder.php');
exit();
}
@ -127,7 +115,7 @@ catch(Exceptions\EbookNotFoundException){
<aside id="reading-ease">
<p><?= number_format($ebook->WordCount) ?> words (<?= $ebook->ReadingTime ?>) with a reading ease of <?= $ebook->ReadingEase ?> (<?= $ebook->ReadingEaseDescription ?>)</p>
<? if($ebook->ContributorsHtml !== null){ ?>
<? if($ebook->ContributorsHtml != ''){ ?>
<p><?= $ebook->ContributorsHtml ?></p>
<? } ?>
<? if(sizeof($ebook->CollectionMemberships) > 0){ ?>

View file

@ -1,33 +1,36 @@
<?
use function Safe\preg_replace;
/** @var string $urlPath Passed from script this is included from. */
/** @var string $identifier Passed from script this is included from. */
$ebook = null;
try{
try{
// Attempt to read a draft ebook repo from the filesystem.
$ebook = Ebook::FromFilesystem(PD_DAY_DRAFT_PATH . '/' . str_replace('/', '_', $urlPath));
$ebook = Ebook::FromFilesystem(PD_DAY_DRAFT_PATH . '/' . str_replace('/', '_', preg_replace('|^' . EBOOKS_IDENTIFIER_PREFIX . '|', '', $identifier)));
}
catch(Exceptions\EbookNotFoundException $ex){
// We may have ebooks listed as in progress, but no actual draft repos yet.
// In that case, fill in the details from `PD_DAY_EBOOKS`.
if(array_key_exists($urlPath, PD_DAY_EBOOKS)){
if(array_key_exists($identifier, PD_DAY_EBOOKS)){
$ebook = new Ebook();
$ebook->EbookId = 0;
$ebook->Description = '';
$ebook->LongDescription = '';
$c = new Contributor();
$c->Name = PD_DAY_EBOOKS[$urlPath]['author'];
$c->Name = PD_DAY_EBOOKS[$identifier]['author'];
$ebook->Authors = [$c];
if(isset(PD_DAY_EBOOKS[$urlPath]['translator'])){
if(isset(PD_DAY_EBOOKS[$identifier]['translator'])){
$c = new Contributor();
$c->Name = PD_DAY_EBOOKS[$urlPath]['translator'];
$c->Name = PD_DAY_EBOOKS[$identifier]['translator'];
$ebook->Translators = [$c];
}
$ebook->Title = PD_DAY_EBOOKS[$urlPath]['title'];
$ebook->Title = PD_DAY_EBOOKS[$identifier]['title'];
$ebook->WwwFilesystemPath = '';
$ebook->Identifier = 'url:https://standardebooks.org/ebooks/' . $urlPath;
$ebook->Identifier = 'url:https://standardebooks.org/ebooks/' . $identifier;
}
else{
throw $ex;
@ -59,7 +62,7 @@ catch(Exceptions\EbookNotFoundException){
<? if(sizeof($ebook->Tags) > 0){ ?>
<aside id="reading-ease">
<? if($ebook->ContributorsHtml !== null){ ?>
<? if($ebook->ContributorsHtml != ''){ ?>
<p><?= $ebook->ContributorsHtml ?></p>
<? } ?>
<? if(sizeof($ebook->CollectionMemberships) > 0){ ?>