Improve file type detection. Improve error handling.
This commit is contained in:
parent
5e58c8e28f
commit
7b95cd8923
|
|
@ -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,12 +62,23 @@ 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);
|
||||
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);
|
||||
|
|
@ -75,20 +86,30 @@ if ( $extracted ) {
|
|||
$written = file_put_contents($comicoutputfull . $imgname, $imgcontents);
|
||||
if ( $written ) $files[] = $imgname;
|
||||
}
|
||||
} elseif ( $ext == "cbr" ) {
|
||||
$zip->close();
|
||||
}
|
||||
} elseif ( $comtype == "cbr" ) {
|
||||
$rar = RarArchive::open($comicfull);
|
||||
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();
|
||||
}
|
||||
} else {
|
||||
exit();
|
||||
$data["error"] = true;
|
||||
$data["message"] = "Error invalid comic file format";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user