diff --git a/ajax/showcomic.php b/ajax/showcomic.php index 52c6d2d..dfd7ea7 100644 --- a/ajax/showcomic.php +++ b/ajax/showcomic.php @@ -75,13 +75,8 @@ if ( !is_dir($comicoutputfull) ) { } if ( !$extracted ) { - $magic = file_get_contents($comicfull, false, null, 0, 4); - if ( substr($magic, 0, 2) === "PK" ) { - $comtype = "cbz"; - } elseif ( $magic === "Rar!" ) { - $comtype = "cbr"; - } - if ( $comtype == "cbz" ) { + $comtype = getComicType($comicfull); + if ( $comtype === COMICZIP ) { $zip = new ZipArchive(); $opened = $zip->open($comicfull); if ( !$opened ) { @@ -101,7 +96,7 @@ if ( !$extracted ) { } $zip->close(); } - } elseif ( $comtype == "cbr" ) { + } elseif ( $comtype === COMICRAR ) { $rar = RarArchive::open($comicfull); if ( $rar === false ) { $data["error"] = true; diff --git a/functions.php b/functions.php index dea89df..5c6a366 100644 --- a/functions.php +++ b/functions.php @@ -19,6 +19,17 @@ function safeFileName($file = "") { return str_replace($to_remove, "", $file); } +function getComicType(string $file = null): String|false { + $magic = file_get_contents($file, false, null, 0, 4); + $type = false; + if ( substr($magic, 0, 2) === "PK" ) { + $type = "cbz"; + } elseif ( $magic === "Rar!" ) { + $type = "cbr"; + } + return $type; +} + function makeThumb($item = "") { if ( $item == "" ) { return false; } $prefix = (substr(getcwd(), -4) == "ajax") ? "../" : ""; @@ -51,7 +62,8 @@ function makeThumb($item = "") { } else { return false; } - if ( substr($comic, -3) == "cbz" ) { + $comtype = getComicType($comic); + if ( $comtype === COMICZIP ) { $zip = new ZipArchive(); $opened = $zip->open($comic); if ( !$opened ) { return false; } @@ -72,7 +84,7 @@ function makeThumb($item = "") { if ( is_null($imgdata) ) return false; $didwrite = file_put_contents($outfile, $imgdata); if ( !$didwrite ) return false; - } elseif ( substr($comic, -3) == "cbr" ) { + } elseif ( $comtype === COMICRAR ) { $rar_file = RarArchive::open($comic); if ( $rar_file === false ) { return false; } $entries = $rar_file->getEntries();