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