Improve handling of improperly extracted comic folders

This commit is contained in:
Junior 2026-11-21 13:50:20 -05:00
parent b02d2ac1c1
commit ab0dd508dc

View File

@ -10,24 +10,34 @@ $data["message"] = "";
$data["validated"] = $validated; $data["validated"] = $validated;
$data["filename"] = ""; $data["filename"] = "";
function sendResponse() {
global $data;
header('Content-Type: application/json');
echo json_encode($data);
exit();
}
if ( !$validated ) { if ( !$validated ) {
$data["error"] = true; $data["error"] = true;
$data["message"] = "Clients must validate accounts"; $data["message"] = "Clients must validate accounts";
header('Content-Type: application/json'); sendResponse();
echo json_encode($data);
exit();
} }
if ( isset($_REQUEST['comic']) ) { if ( isset($_REQUEST['comic']) ) {
$comicfull = realpath(COMICSDIR . base64_decode($_REQUEST['comic'])); $comicfull = realpath(COMICSDIR . base64_decode($_REQUEST['comic']));
if ( $comicfull === false ) {
$data["error"] = true;
$data["message"] = "Requested comic not found";
sendResponse();
}
$data["filename"] = basename($comicfull); $data["filename"] = basename($comicfull);
if ( $comicfull === false ) exit();
if ( substr($comicfull, 0, strlen(COMICSDIR)) != COMICSDIR ) exit();
$comic = substr($comicfull, strlen(COMICSDIR)); $comic = substr($comicfull, strlen(COMICSDIR));
$comicoutputurl = "comics" . str_replace("#", "", $comic) . "/"; $comicoutputurl = "comics" . str_replace("#", "", $comic) . "/";
$comicoutputfull = "../" . EXTRACTSDIR . str_replace("#", "", $comic) . "/"; $comicoutputfull = "../" . EXTRACTSDIR . str_replace("#", "", $comic) . "/";
} else { } else {
exit(); $data["error"] = true;
$data["message"] = "Malformed request";
sendResponse();
} }
$ext = strtolower(substr($comicfull, -3)); $ext = strtolower(substr($comicfull, -3));
@ -48,20 +58,21 @@ if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
$data["startindex"] = $currentpage; $data["startindex"] = $currentpage;
$extracted = false; $extracted = false;
$files = array();
if ( !is_dir($comicoutputfull) ) { if ( !is_dir($comicoutputfull) ) {
mkdir($comicoutputfull, 0755, true); mkdir($comicoutputfull, 0755, true);
} else { } else {
touch($comicoutputfull); touch($comicoutputfull);
$extracted = true;
}
$files = array();
if ( $extracted ) {
$contents = glob($comicoutputfull . "*"); $contents = glob($comicoutputfull . "*");
foreach ( $contents as $i ) { foreach ( $contents as $i ) {
$files[] = basename($i); $files[] = basename($i);
} }
} else { if ( count($files) != 0 ) {
$extracted = true;
}
}
if ( !$extracted ) {
$magic = file_get_contents($comicfull, false, null, 0, 4); $magic = file_get_contents($comicfull, false, null, 0, 4);
if ( substr($magic, 0, 2) === "PK" ) { if ( substr($magic, 0, 2) === "PK" ) {
$comtype = "cbz"; $comtype = "cbz";
@ -109,7 +120,8 @@ if ( $extracted ) {
} }
} else { } else {
$data["error"] = true; $data["error"] = true;
$data["message"] = "Error invalid comic file format"; $data["message"] = "Invalid comic file format";
sendResponse();
} }
} }
@ -128,8 +140,6 @@ foreach ( $files as $file ) {
$data["images"] = $images; $data["images"] = $images;
$data["captions"] = $captions; $data["captions"] = $captions;
header('Content-Type: application/json'); sendResponse();
echo json_encode($data);
exit();
// vim: set ts=3 sw=3 et: // vim: set ts=3 sw=3 et: