diff --git a/lib/Core.php b/lib/Core.php index 74e5916f..8e428d5e 100644 --- a/lib/Core.php +++ b/lib/Core.php @@ -27,7 +27,7 @@ function vds($var){ spl_autoload_register(function($class){ try{ - include_once($class . '.php'); + include($class . '.php'); } catch(\Exception $ex){ } diff --git a/lib/Ebook.php b/lib/Ebook.php index 711a2fb5..621d523d 100644 --- a/lib/Ebook.php +++ b/lib/Ebook.php @@ -30,7 +30,7 @@ class Ebook{ public $ReadingTime; public $GitHubUrl; public $WikipediaUrl; - public $SourceUrls = []; + public $Sources = []; public $Authors = []; // Array of Contributors public $AuthorsHtml; public $AuthorsUrl; // This is a single URL even if there are multiple authors; for example, /ebooks/karl-marx_friedrich-engels/ @@ -47,7 +47,7 @@ class Ebook{ $this->RepoFilesystemPath = SITE_ROOT . '/ebooks/' . str_replace('/', '_', $this->RepoFilesystemPath) . '.git'; if(!is_dir($this->RepoFilesystemPath)){ // On dev systems we might not have the bare repos, so make an adjustment - $this->RepoFilesystemPath = preg_replace('/\.git$/ius', '', $this->RepoFilesystemPath); + $this->RepoFilesystemPath = preg_replace('/\.git$/ius', '', $this->RepoFilesystemPath) ?? ''; } if(!is_dir($wwwFilesystemPath)){ @@ -159,7 +159,10 @@ class Ebook{ // Figure out authors and contributors. foreach($xml->xpath('/package/metadata/dc:creator') ?: [] as $author){ - $id = $author->attributes()->id; + $id = ''; + if($author->attributes() !== null){ + $id = $author->attributes()->id; + } $this->Authors[] = new Contributor( (string)$author, (string)$xml->xpath('/package/metadata/meta[@property="file-as"][@refines="#' . $id . '"]')[0], $this->NullIfEmpty($xml->xpath('/package/metadata/meta[@property="se:name.person.full-name"][@refines="#' . $id . '"]')), @@ -174,7 +177,10 @@ class Ebook{ $this->AuthorsUrl = preg_replace('|url:https://standardebooks.org/ebooks/([^/]+)/.*|ius', '/ebooks/\1/', $this->Identifier); foreach($xml->xpath('/package/metadata/dc:contributor') ?: [] as $contributor){ - $id = $contributor->attributes()->id; + $id = ''; + if($contributor->attributes() !== null){ + $id = $contributor->attributes()->id; + } foreach($xml->xpath('/package/metadata/meta[@property="role"][@refines="#' . $id . '"]') ?: [] as $role){ $c = new Contributor( (string)$contributor, @@ -267,23 +273,24 @@ class Ebook{ // Next the page scan source URLs. foreach($xml->xpath('/package/metadata/dc:source') ?: [] as $element){ - if(mb_stripos((string)$element, '//www.gutenberg.org/') !== false){ - $this->SourceUrls[] = ['source' => SOURCE_PROJECT_GUTENBERG, 'url' => (string)$element]; + $e = (string)$element; + if(mb_stripos($e, '//www.gutenberg.org/') !== false){ + $this->Sources[] = new EbookSource(SOURCE_PROJECT_GUTENBERG, $e); } - elseif(mb_stripos((string)$element, '//archive.org/') !== false){ - $this->SourceUrls[] = ['source' => SOURCE_INTERNET_ARCHIVE, 'url' => (string)$element]; + elseif(mb_stripos($e, '//archive.org/') !== false){ + $this->Sources[] = new EbookSource(SOURCE_INTERNET_ARCHIVE, $e); } - elseif(mb_stripos((string)$element, 'hathitrust.org/') !== false){ - $this->SourceUrls[] = ['source' => SOURCE_HATHI_TRUST, 'url' => (string)$element]; + elseif(mb_stripos($e, 'hathitrust.org/') !== false){ + $this->Sources[] = new EbookSource(SOURCE_HATHI_TRUST, $e); } - elseif(mb_stripos((string)$element, 'wikisource.org/') !== false){ - $this->SourceUrls[] = ['source' => SOURCE_WIKISOURCE, 'url' => (string)$element]; + elseif(mb_stripos($e, 'wikisource.org/') !== false){ + $this->Sources[] = new EbookSource(SOURCE_WIKISOURCE, $e); } - elseif(mb_stripos((string)$element, 'books.google.com/') !== false){ - $this->SourceUrls[] = ['source' => SOURCE_GOOGLE_BOOKS, 'url' => (string)$element]; + elseif(mb_stripos($e, 'books.google.com/') !== false){ + $this->Sources[] = new EbookSource(SOURCE_GOOGLE_BOOKS, $e); } else{ - $otherUrls[] = [SOURCE_OTHER, (string)$element]; + $this->Sources[] = new EbookSource(SOURCE_OTHER, $e); } } diff --git a/lib/EbookSource.php b/lib/EbookSource.php new file mode 100644 index 00000000..1d656856 --- /dev/null +++ b/lib/EbookSource.php @@ -0,0 +1,11 @@ + +class EbookSource{ + public $Type; + public $Url; + + public function __construct(int $type, string $url){ + $this->Type = $type; + $this->Url = $url; + } +} +?> diff --git a/lib/Library.php b/lib/Library.php index 8b4ffdb1..74bc83f1 100644 --- a/lib/Library.php +++ b/lib/Library.php @@ -50,7 +50,7 @@ class Library{ if(!$success){ foreach(explode("\n", trim(shell_exec('find ' . SITE_ROOT . '/www/ebooks/ -name "content.opf"') ?? '')) as $filename){ - $ebookWwwFilesystemPath = preg_replace('|/src/.+|ius', '', $filename); + $ebookWwwFilesystemPath = preg_replace('|/src/.+|ius', '', $filename) ?: ''; $ebook = apcu_fetch('ebook-' . $ebookWwwFilesystemPath, $success); if(!$success){ @@ -78,7 +78,7 @@ class Library{ foreach(explode("\n", trim(shell_exec('find ' . escapeshellarg($wwwFilesystemPath) . ' -name "content.opf"') ?? '')) as $filename){ try{ - $ebookWwwFilesystemPath = preg_replace('|/src/.+|ius', '', $filename); + $ebookWwwFilesystemPath = preg_replace('|/src/.+|ius', '', $filename) ?? ''; $ebook = apcu_fetch('ebook-' . $ebookWwwFilesystemPath, $success); if(!$success){ diff --git a/templates/EbookGrid.php b/templates/EbookGrid.php index 5525c840..46307ff9 100644 --- a/templates/EbookGrid.php +++ b/templates/EbookGrid.php @@ -1,5 +1,4 @@ - if(!isset($ebooks)){ $ebooks = []; } diff --git a/www/ebooks/author.php b/www/ebooks/author.php index 2f46252b..e0efca14 100644 --- a/www/ebooks/author.php +++ b/www/ebooks/author.php @@ -2,7 +2,7 @@ require_once('Core.php'); try{ - $urlPath = trim(str_replace('.', '', HttpInput::GetString('url-path')), '/'); // Contains the portion of the URL (without query string) that comes after https://standardebooks.org/ebooks/ + $urlPath = trim(str_replace('.', '', HttpInput::GetString('url-path') ?? ''), '/'); // Contains the portion of the URL (without query string) that comes after https://standardebooks.org/ebooks/ $wwwFilesystemPath = SITE_ROOT . '/www/ebooks/' . $urlPath; // Path to the deployed WWW files for this ebook if($urlPath == '' || mb_stripos($wwwFilesystemPath, SITE_ROOT . '/www/ebooks/') !== 0 || !is_dir($wwwFilesystemPath)){ diff --git a/www/ebooks/ebook.php b/www/ebooks/ebook.php index fe3a3e04..cb99d309 100644 --- a/www/ebooks/ebook.php +++ b/www/ebooks/ebook.php @@ -2,7 +2,7 @@ require_once('Core.php'); try{ - $urlPath = trim(str_replace('.', '', HttpInput::GetString('url-path')), '/'); // Contains the portion of the URL (without query string) that comes after https://standardebooks.org/ebooks/ + $urlPath = trim(str_replace('.', '', HttpInput::GetString('url-path') ?? ''), '/'); // Contains the portion of the URL (without query string) that comes after https://standardebooks.org/ebooks/ $wwwFilesystemPath = SITE_ROOT . '/www/ebooks/' . $urlPath; // Path to the deployed WWW files for this ebook if($urlPath == '' || mb_stripos($wwwFilesystemPath, SITE_ROOT . '/www/ebooks/') !== 0){ @@ -145,15 +145,15 @@ catch(\Exception $ex){
} ?> - foreach($ebook->SourceUrls as $source){ ?> + foreach($ebook->Sources as $source){ ?>- if($source['source'] == SOURCE_PROJECT_GUTENBERG){ ?>Transcription at Project Gutenberg } ?> - if($source['source'] == SOURCE_WIKISOURCE){ ?>Transcription at Wikisource } ?> - if($source['source'] == SOURCE_INTERNET_ARCHIVE){ ?>Page scans at the Internet Archive } ?> - if($source['source'] == SOURCE_HATHI_TRUST){ ?>Page scans at HathiTrust } ?> - if($source['source'] == SOURCE_GOOGLE_BOOKS){ ?>Page scans at Google Books } ?> - if($source['source'] == SOURCE_OTHER){ ?>= Formatter::ToPlainText(preg_replace(['|https?://(en\.)?|', '|/.+$|'], '', $source['url']) ?? '') ?> } ?> + if($source->Type == SOURCE_PROJECT_GUTENBERG){ ?>Transcription at Project Gutenberg } ?> + if($source->Type == SOURCE_WIKISOURCE){ ?>Transcription at Wikisource } ?> + if($source->Type == SOURCE_INTERNET_ARCHIVE){ ?>Page scans at the Internet Archive } ?> + if($source->Type == SOURCE_HATHI_TRUST){ ?>Page scans at HathiTrust } ?> + if($source->Type == SOURCE_GOOGLE_BOOKS){ ?>Page scans at Google Books } ?> + if($source->Type == SOURCE_OTHER){ ?>= Formatter::ToPlainText(preg_replace(['|https?://(en\.)?|', '|/.+$|'], '', $source->Url) ?? '') ?> } ?>