More refactoring for static analysis

This commit is contained in:
Alex Cabal 2019-01-18 17:49:13 -06:00
parent 5939195955
commit ba53958596
9 changed files with 55 additions and 36 deletions

View file

@ -27,7 +27,7 @@ function vds($var){
spl_autoload_register(function($class){
try{
include_once($class . '.php');
include($class . '.php');
}
catch(\Exception $ex){
}

View file

@ -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);
}
}

11
lib/EbookSource.php Normal file
View file

@ -0,0 +1,11 @@
<?
class EbookSource{
public $Type;
public $Url;
public function __construct(int $type, string $url){
$this->Type = $type;
$this->Url = $url;
}
}
?>

View file

@ -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){

View file

@ -1,5 +1,4 @@
<?
if(!isset($ebooks)){
$ebooks = [];
}

View file

@ -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)){

View file

@ -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){
<p><a href="<?= Formatter::ToPlainText($ebook->WikipediaUrl) ?>" class="wikipedia">This book at Wikipedia</a></p>
</li>
<? } ?>
<? foreach($ebook->SourceUrls as $source){ ?>
<? foreach($ebook->Sources as $source){ ?>
<li>
<p>
<? if($source['source'] == 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['source'] == 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['source'] == 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_PROJECT_GUTENBERG){ ?><a href="<?= Formatter::ToPlainText($source->Url) ?>" class="project-gutenberg">Transcription at Project Gutenberg</a><? } ?>
<? if($source->Type == SOURCE_WIKISOURCE){ ?><a href="<?= Formatter::ToPlainText($source->Url) ?>" class="wikisource">Transcription at Wikisource</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->Type == SOURCE_HATHI_TRUST){ ?><a href="<?= Formatter::ToPlainText($source->Url) ?>" class="hathitrust">Page scans at HathiTrust</a><? } ?>
<? if($source->Type == SOURCE_GOOGLE_BOOKS){ ?><a href="<?= Formatter::ToPlainText($source->Url) ?>" class="google">Page scans at Google Books</a><? } ?>
<? if($source->Type == SOURCE_OTHER){ ?><a href="<?= Formatter::ToPlainText($source->Url) ?>" class="globe"><?= Formatter::ToPlainText(preg_replace(['|https?://(en\.)?|', '|/.+$|'], '', $source->Url) ?? '') ?></a><? } ?>
</p>
</li>
<? } ?>
@ -171,7 +171,7 @@ catch(\Exception $ex){
<h2>More free ebooks</h2>
<ul>
<? 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>
</aside>

View file

@ -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){

View file

@ -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);