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 @@ +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 @@ This book at Wikipedia

- SourceUrls as $source){ ?> + Sources as $source){ ?>
  • - Transcription at Project Gutenberg - Transcription at Wikisource - Page scans at the Internet Archive - Page scans at HathiTrust - Page scans at Google Books - + Type == SOURCE_PROJECT_GUTENBERG){ ?>Transcription at Project Gutenberg + Type == SOURCE_WIKISOURCE){ ?>Transcription at Wikisource + Type == SOURCE_INTERNET_ARCHIVE){ ?>Page scans at the Internet Archive + Type == SOURCE_HATHI_TRUST){ ?>Page scans at HathiTrust + Type == SOURCE_GOOGLE_BOOKS){ ?>Page scans at Google Books + Type == SOURCE_OTHER){ ?>Url) ?? '') ?>

  • @@ -171,7 +171,7 @@ catch(\Exception $ex){

    More free ebooks

    diff --git a/www/ebooks/index.php b/www/ebooks/index.php index d0d246ad..f4b627e1 100644 --- a/www/ebooks/index.php +++ b/www/ebooks/index.php @@ -5,6 +5,8 @@ try{ $page = HttpInput::GetInt('page') ?? 1; $query = HttpInput::GetString('query', false); $sort = HttpInput::GetString('sort', false) ?? SORT_NEWEST; + $pages = 0; + $totalEbooks = 0; if($page <= 0){ $page = 1; @@ -14,11 +16,7 @@ try{ $sort = SORT_NEWEST; } - if($query !== null){ - $ebooks = Library::Search($query); - $pageDescription = 'Search results'; - } - else{ + if($query === null){ $ebooks = Library::GetEbooks($sort); $pages = ceil(sizeof($ebooks) / EBOOKS_PER_PAGE); @@ -37,6 +35,10 @@ try{ break; } } + else{ + $ebooks = Library::Search($query); + $pageDescription = 'Search results'; + } $pageTitle = 'Browse Standard Ebooks'; if($query !== null){ diff --git a/www/webhooks/github.php b/www/webhooks/github.php index db998f68..a601c75d 100644 --- a/www/webhooks/github.php +++ b/www/webhooks/github.php @@ -18,7 +18,7 @@ try{ $hashAlgorithm = $splitHash[0]; $hash = $splitHash[1]; - if(!hash_equals($hash, hash_hmac($hashAlgorithm, $post, preg_replace("/[\r\n]/ius", '', file_get_contents(GITHUB_SECRET_FILE_PATH) ?: '')))){ + if(!hash_equals($hash, hash_hmac($hashAlgorithm, $post, preg_replace("/[\r\n]/ius", '', file_get_contents(GITHUB_SECRET_FILE_PATH) ?: '') ?? ''))){ throw new WebhookException('Invalid GitHub webhook secret.', $post); } @@ -63,7 +63,7 @@ try{ Logger::WriteGithubWebhookLogEntry($requestId, 'Processing ebook "' . $repoName . '" located at "' . $dir . '".'); // Check the local repo's last commit. If it matches this push, then don't do anything; we're already up to date. - $lastCommitSha1 = trim(shell_exec('git -C ' . escapeshellarg($dir) . ' rev-parse HEAD 2>&1; echo $?')); + $lastCommitSha1 = trim(shell_exec('git -C ' . escapeshellarg($dir) . ' rev-parse HEAD 2>&1; echo $?') ?? ''); if($lastCommitSha1 == ''){ Logger::WriteGithubWebhookLogEntry($requestId, 'Error getting last local commit. Output: ' . $lastCommitSha1);