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