mirror of
https://github.com/standardebooks/web.git
synced 2025-07-05 22:30:30 -04:00
More refactoring for static analysis
This commit is contained in:
parent
5939195955
commit
ba53958596
9 changed files with 55 additions and 36 deletions
|
@ -27,7 +27,7 @@ function vds($var){
|
||||||
|
|
||||||
spl_autoload_register(function($class){
|
spl_autoload_register(function($class){
|
||||||
try{
|
try{
|
||||||
include_once($class . '.php');
|
include($class . '.php');
|
||||||
}
|
}
|
||||||
catch(\Exception $ex){
|
catch(\Exception $ex){
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ class Ebook{
|
||||||
public $ReadingTime;
|
public $ReadingTime;
|
||||||
public $GitHubUrl;
|
public $GitHubUrl;
|
||||||
public $WikipediaUrl;
|
public $WikipediaUrl;
|
||||||
public $SourceUrls = [];
|
public $Sources = [];
|
||||||
public $Authors = []; // Array of Contributors
|
public $Authors = []; // Array of Contributors
|
||||||
public $AuthorsHtml;
|
public $AuthorsHtml;
|
||||||
public $AuthorsUrl; // This is a single URL even if there are multiple authors; for example, /ebooks/karl-marx_friedrich-engels/
|
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';
|
$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
|
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)){
|
if(!is_dir($wwwFilesystemPath)){
|
||||||
|
@ -159,7 +159,10 @@ class Ebook{
|
||||||
|
|
||||||
// Figure out authors and contributors.
|
// Figure out authors and contributors.
|
||||||
foreach($xml->xpath('/package/metadata/dc:creator') ?: [] as $author){
|
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,
|
$this->Authors[] = new Contributor( (string)$author,
|
||||||
(string)$xml->xpath('/package/metadata/meta[@property="file-as"][@refines="#' . $id . '"]')[0],
|
(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 . '"]')),
|
$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);
|
$this->AuthorsUrl = preg_replace('|url:https://standardebooks.org/ebooks/([^/]+)/.*|ius', '/ebooks/\1/', $this->Identifier);
|
||||||
|
|
||||||
foreach($xml->xpath('/package/metadata/dc:contributor') ?: [] as $contributor){
|
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){
|
foreach($xml->xpath('/package/metadata/meta[@property="role"][@refines="#' . $id . '"]') ?: [] as $role){
|
||||||
$c = new Contributor(
|
$c = new Contributor(
|
||||||
(string)$contributor,
|
(string)$contributor,
|
||||||
|
@ -267,23 +273,24 @@ class Ebook{
|
||||||
|
|
||||||
// Next the page scan source URLs.
|
// Next the page scan source URLs.
|
||||||
foreach($xml->xpath('/package/metadata/dc:source') ?: [] as $element){
|
foreach($xml->xpath('/package/metadata/dc:source') ?: [] as $element){
|
||||||
if(mb_stripos((string)$element, '//www.gutenberg.org/') !== false){
|
$e = (string)$element;
|
||||||
$this->SourceUrls[] = ['source' => SOURCE_PROJECT_GUTENBERG, 'url' => (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){
|
elseif(mb_stripos($e, '//archive.org/') !== false){
|
||||||
$this->SourceUrls[] = ['source' => SOURCE_INTERNET_ARCHIVE, 'url' => (string)$element];
|
$this->Sources[] = new EbookSource(SOURCE_INTERNET_ARCHIVE, $e);
|
||||||
}
|
}
|
||||||
elseif(mb_stripos((string)$element, 'hathitrust.org/') !== false){
|
elseif(mb_stripos($e, 'hathitrust.org/') !== false){
|
||||||
$this->SourceUrls[] = ['source' => SOURCE_HATHI_TRUST, 'url' => (string)$element];
|
$this->Sources[] = new EbookSource(SOURCE_HATHI_TRUST, $e);
|
||||||
}
|
}
|
||||||
elseif(mb_stripos((string)$element, 'wikisource.org/') !== false){
|
elseif(mb_stripos($e, 'wikisource.org/') !== false){
|
||||||
$this->SourceUrls[] = ['source' => SOURCE_WIKISOURCE, 'url' => (string)$element];
|
$this->Sources[] = new EbookSource(SOURCE_WIKISOURCE, $e);
|
||||||
}
|
}
|
||||||
elseif(mb_stripos((string)$element, 'books.google.com/') !== false){
|
elseif(mb_stripos($e, 'books.google.com/') !== false){
|
||||||
$this->SourceUrls[] = ['source' => SOURCE_GOOGLE_BOOKS, 'url' => (string)$element];
|
$this->Sources[] = new EbookSource(SOURCE_GOOGLE_BOOKS, $e);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
$otherUrls[] = [SOURCE_OTHER, (string)$element];
|
$this->Sources[] = new EbookSource(SOURCE_OTHER, $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
11
lib/EbookSource.php
Normal file
11
lib/EbookSource.php
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?
|
||||||
|
class EbookSource{
|
||||||
|
public $Type;
|
||||||
|
public $Url;
|
||||||
|
|
||||||
|
public function __construct(int $type, string $url){
|
||||||
|
$this->Type = $type;
|
||||||
|
$this->Url = $url;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
|
@ -50,7 +50,7 @@ class Library{
|
||||||
|
|
||||||
if(!$success){
|
if(!$success){
|
||||||
foreach(explode("\n", trim(shell_exec('find ' . SITE_ROOT . '/www/ebooks/ -name "content.opf"') ?? '')) as $filename){
|
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);
|
$ebook = apcu_fetch('ebook-' . $ebookWwwFilesystemPath, $success);
|
||||||
|
|
||||||
if(!$success){
|
if(!$success){
|
||||||
|
@ -78,7 +78,7 @@ class Library{
|
||||||
|
|
||||||
foreach(explode("\n", trim(shell_exec('find ' . escapeshellarg($wwwFilesystemPath) . ' -name "content.opf"') ?? '')) as $filename){
|
foreach(explode("\n", trim(shell_exec('find ' . escapeshellarg($wwwFilesystemPath) . ' -name "content.opf"') ?? '')) as $filename){
|
||||||
try{
|
try{
|
||||||
$ebookWwwFilesystemPath = preg_replace('|/src/.+|ius', '', $filename);
|
$ebookWwwFilesystemPath = preg_replace('|/src/.+|ius', '', $filename) ?? '';
|
||||||
$ebook = apcu_fetch('ebook-' . $ebookWwwFilesystemPath, $success);
|
$ebook = apcu_fetch('ebook-' . $ebookWwwFilesystemPath, $success);
|
||||||
|
|
||||||
if(!$success){
|
if(!$success){
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
<?
|
<?
|
||||||
|
|
||||||
if(!isset($ebooks)){
|
if(!isset($ebooks)){
|
||||||
$ebooks = [];
|
$ebooks = [];
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
require_once('Core.php');
|
require_once('Core.php');
|
||||||
|
|
||||||
try{
|
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
|
$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)){
|
if($urlPath == '' || mb_stripos($wwwFilesystemPath, SITE_ROOT . '/www/ebooks/') !== 0 || !is_dir($wwwFilesystemPath)){
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
require_once('Core.php');
|
require_once('Core.php');
|
||||||
|
|
||||||
try{
|
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
|
$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){
|
if($urlPath == '' || mb_stripos($wwwFilesystemPath, SITE_ROOT . '/www/ebooks/') !== 0){
|
||||||
|
@ -145,15 +145,15 @@ catch(\Exception $ex){
|
||||||
<p><a href="<?= Formatter::ToPlainText($ebook->WikipediaUrl) ?>" class="wikipedia">This book at Wikipedia</a></p>
|
<p><a href="<?= Formatter::ToPlainText($ebook->WikipediaUrl) ?>" class="wikipedia">This book at Wikipedia</a></p>
|
||||||
</li>
|
</li>
|
||||||
<? } ?>
|
<? } ?>
|
||||||
<? foreach($ebook->SourceUrls as $source){ ?>
|
<? foreach($ebook->Sources as $source){ ?>
|
||||||
<li>
|
<li>
|
||||||
<p>
|
<p>
|
||||||
<? if($source['source'] == SOURCE_PROJECT_GUTENBERG){ ?><a href="<?= Formatter::ToPlainText($source['url']) ?>" class="project-gutenberg">Transcription at Project Gutenberg</a><? } ?>
|
<? if($source->Type == SOURCE_PROJECT_GUTENBERG){ ?><a href="<?= Formatter::ToPlainText($source->Url) ?>" class="project-gutenberg">Transcription at Project Gutenberg</a><? } ?>
|
||||||
<? if($source['source'] == SOURCE_WIKISOURCE){ ?><a href="<?= Formatter::ToPlainText($source['url']) ?>" class="wikisource">Transcription at Wikisource</a><? } ?>
|
<? if($source->Type == SOURCE_WIKISOURCE){ ?><a href="<?= Formatter::ToPlainText($source->Url) ?>" class="wikisource">Transcription at Wikisource</a><? } ?>
|
||||||
<? if($source['source'] == SOURCE_INTERNET_ARCHIVE){ ?><a href="<?= Formatter::ToPlainText($source['url']) ?>" class="internet-archive">Page scans at the Internet Archive</a><? } ?>
|
<? if($source->Type == SOURCE_INTERNET_ARCHIVE){ ?><a href="<?= Formatter::ToPlainText($source->Url) ?>" class="internet-archive">Page scans at the Internet Archive</a><? } ?>
|
||||||
<? if($source['source'] == SOURCE_HATHI_TRUST){ ?><a href="<?= Formatter::ToPlainText($source['url']) ?>" class="hathitrust">Page scans at HathiTrust</a><? } ?>
|
<? if($source->Type == SOURCE_HATHI_TRUST){ ?><a href="<?= Formatter::ToPlainText($source->Url) ?>" class="hathitrust">Page scans at HathiTrust</a><? } ?>
|
||||||
<? if($source['source'] == SOURCE_GOOGLE_BOOKS){ ?><a href="<?= Formatter::ToPlainText($source['url']) ?>" class="google">Page scans at Google Books</a><? } ?>
|
<? if($source->Type == SOURCE_GOOGLE_BOOKS){ ?><a href="<?= Formatter::ToPlainText($source->Url) ?>" class="google">Page scans at Google Books</a><? } ?>
|
||||||
<? if($source['source'] == SOURCE_OTHER){ ?><a href="<?= Formatter::ToPlainText($source['url']) ?>" class="globe"><?= Formatter::ToPlainText(preg_replace(['|https?://(en\.)?|', '|/.+$|'], '', $source['url']) ?? '') ?></a><? } ?>
|
<? if($source->Type == SOURCE_OTHER){ ?><a href="<?= Formatter::ToPlainText($source->Url) ?>" class="globe"><?= Formatter::ToPlainText(preg_replace(['|https?://(en\.)?|', '|/.+$|'], '', $source->Url) ?? '') ?></a><? } ?>
|
||||||
</p>
|
</p>
|
||||||
</li>
|
</li>
|
||||||
<? } ?>
|
<? } ?>
|
||||||
|
@ -171,7 +171,7 @@ catch(\Exception $ex){
|
||||||
<h2>More free ebooks</h2>
|
<h2>More free ebooks</h2>
|
||||||
<ul>
|
<ul>
|
||||||
<? foreach($carousel as $carouselEbook){ ?>
|
<? foreach($carousel as $carouselEbook){ ?>
|
||||||
<li><a href="<?= $carouselEbook->Url ?>"><img src="<?= $carouselEbook->CoverImage2xUrl ?>"<? if(false) { ?> srcset="<?= $carouselEbook->CoverImage2xUrl ?> 2x, <?= $carouselEbook->CoverImageUrl ?> 1x"<? } ?> alt="The cover for the Standard Ebooks edition of <?= Formatter::ToPlainText($carouselEbook->Title) ?>" title="<?= Formatter::ToPlainText($carouselEbook->Title) ?>" /></a></li>
|
<li><a href="<?= $carouselEbook->Url ?>"><img src="<?= $carouselEbook->CoverImage2xUrl ?>"<? /* srcset="<?= $carouselEbook->CoverImage2xUrl ?> 2x, <?= $carouselEbook->CoverImageUrl ?> 1x" */ ?> alt="The cover for the Standard Ebooks edition of <?= Formatter::ToPlainText($carouselEbook->Title) ?>" title="<?= Formatter::ToPlainText($carouselEbook->Title) ?>" /></a></li>
|
||||||
<? } ?>
|
<? } ?>
|
||||||
</ul>
|
</ul>
|
||||||
</aside>
|
</aside>
|
||||||
|
|
|
@ -5,6 +5,8 @@ try{
|
||||||
$page = HttpInput::GetInt('page') ?? 1;
|
$page = HttpInput::GetInt('page') ?? 1;
|
||||||
$query = HttpInput::GetString('query', false);
|
$query = HttpInput::GetString('query', false);
|
||||||
$sort = HttpInput::GetString('sort', false) ?? SORT_NEWEST;
|
$sort = HttpInput::GetString('sort', false) ?? SORT_NEWEST;
|
||||||
|
$pages = 0;
|
||||||
|
$totalEbooks = 0;
|
||||||
|
|
||||||
if($page <= 0){
|
if($page <= 0){
|
||||||
$page = 1;
|
$page = 1;
|
||||||
|
@ -14,11 +16,7 @@ try{
|
||||||
$sort = SORT_NEWEST;
|
$sort = SORT_NEWEST;
|
||||||
}
|
}
|
||||||
|
|
||||||
if($query !== null){
|
if($query === null){
|
||||||
$ebooks = Library::Search($query);
|
|
||||||
$pageDescription = 'Search results';
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
$ebooks = Library::GetEbooks($sort);
|
$ebooks = Library::GetEbooks($sort);
|
||||||
|
|
||||||
$pages = ceil(sizeof($ebooks) / EBOOKS_PER_PAGE);
|
$pages = ceil(sizeof($ebooks) / EBOOKS_PER_PAGE);
|
||||||
|
@ -37,6 +35,10 @@ try{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
$ebooks = Library::Search($query);
|
||||||
|
$pageDescription = 'Search results';
|
||||||
|
}
|
||||||
|
|
||||||
$pageTitle = 'Browse Standard Ebooks';
|
$pageTitle = 'Browse Standard Ebooks';
|
||||||
if($query !== null){
|
if($query !== null){
|
||||||
|
|
|
@ -18,7 +18,7 @@ try{
|
||||||
$hashAlgorithm = $splitHash[0];
|
$hashAlgorithm = $splitHash[0];
|
||||||
$hash = $splitHash[1];
|
$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);
|
throw new WebhookException('Invalid GitHub webhook secret.', $post);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@ try{
|
||||||
Logger::WriteGithubWebhookLogEntry($requestId, 'Processing ebook "' . $repoName . '" located at "' . $dir . '".');
|
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.
|
// 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 == ''){
|
if($lastCommitSha1 == ''){
|
||||||
Logger::WriteGithubWebhookLogEntry($requestId, 'Error getting last local commit. Output: ' . $lastCommitSha1);
|
Logger::WriteGithubWebhookLogEntry($requestId, 'Error getting last local commit. Output: ' . $lastCommitSha1);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue