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