Improve file type detection. Improve error handling.

This commit is contained in:
Junior 2026-11-09 18:43:44 -05:00
parent 5e58c8e28f
commit 7b95cd8923

View File

@ -19,7 +19,7 @@ if ( !$validated ) {
} }
if ( isset($_REQUEST['comic']) ) { if ( isset($_REQUEST['comic']) ) {
$comicfull = realpath(COMICSDIR . base64_decode(urldecode($_REQUEST['comic']))); $comicfull = realpath(COMICSDIR . base64_decode($_REQUEST['comic']));
$data["filename"] = basename($comicfull); $data["filename"] = basename($comicfull);
if ( $comicfull === false ) exit(); if ( $comicfull === false ) exit();
if ( substr($comicfull, 0, strlen(COMICSDIR)) != COMICSDIR ) exit(); if ( substr($comicfull, 0, strlen(COMICSDIR)) != COMICSDIR ) exit();
@ -62,12 +62,23 @@ if ( $extracted ) {
$files[] = basename($i); $files[] = basename($i);
} }
} else { } else {
if ( $ext == "cbz" ) { $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" ) {
$zip = new ZipArchive(); $zip = new ZipArchive();
$opened = $zip->open($comicfull); $opened = $zip->open($comicfull);
if ( !$opened ) {
$data["error"] = true;
$data["message"] = "Error opening comic file";
} else {
for ( $i=0; $i<$zip->numFiles; $i++ ) { for ( $i=0; $i<$zip->numFiles; $i++ ) {
$imgname = basename($zip->getNameIndex($i)); $imgname = basename($zip->getNameIndex($i));
if ( ! isset(pathinfo($imgname)['extension']) ) continue; if ( ! isset(pathinfo($imgname)['extension']) ) continue;
if ( (substr($imgname, 0, 8) == "__MACOSX") || (substr($imgname, 0, 2) == "._") ) continue;
$myext = strtolower(pathinfo($imgname)['extension']); $myext = strtolower(pathinfo($imgname)['extension']);
if ( ! in_array($myext, VALIDIMAGETYPES) ) continue; if ( ! in_array($myext, VALIDIMAGETYPES) ) continue;
$imgname = str_replace("#", "", $imgname); $imgname = str_replace("#", "", $imgname);
@ -75,20 +86,30 @@ if ( $extracted ) {
$written = file_put_contents($comicoutputfull . $imgname, $imgcontents); $written = file_put_contents($comicoutputfull . $imgname, $imgcontents);
if ( $written ) $files[] = $imgname; if ( $written ) $files[] = $imgname;
} }
} elseif ( $ext == "cbr" ) { $zip->close();
}
} elseif ( $comtype == "cbr" ) {
$rar = RarArchive::open($comicfull); $rar = RarArchive::open($comicfull);
if ( $rar === false ) {
$data["error"] = true;
$data["message"] = "Error opening comic file";
} else {
$entries = $rar->getEntries(); $entries = $rar->getEntries();
foreach ( $entries as $entry ) { foreach ( $entries as $entry ) {
$outfile = str_replace("#", "", basename($entry->getName())); $outfile = str_replace("#", "", basename($entry->getName()));
if ( ! isset(pathinfo($outfile)['extension']) ) continue; if ( ! isset(pathinfo($outfile)['extension']) ) continue;
if ( (substr($outfile, 0, 8) == "__MACOSX") || (substr($outfile, 0, 2) == "._") ) continue;
$myext = strtolower(pathinfo($outfile)['extension']); $myext = strtolower(pathinfo($outfile)['extension']);
if ( ! in_array($myext, VALIDIMAGETYPES) ) continue; if ( ! in_array($myext, VALIDIMAGETYPES) ) continue;
$entry->extract(false, $comicoutputfull . $outfile); $entry->extract(false, $comicoutputfull . $outfile);
$files[] = basename($outfile); $files[] = basename($outfile);
} }
unset($entry); unset($entry);
$rar->close();
}
} else { } else {
exit(); $data["error"] = true;
$data["message"] = "Error invalid comic file format";
} }
} }