From ab0dd508dc379991cf553e154957ca71747e4245 Mon Sep 17 00:00:00 2001 From: Junior Date: Sat, 21 Nov 2026 13:50:20 -0500 Subject: [PATCH] Improve handling of improperly extracted comic folders --- ajax/showcomic.php | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/ajax/showcomic.php b/ajax/showcomic.php index 62fa116..f973b2c 100644 --- a/ajax/showcomic.php +++ b/ajax/showcomic.php @@ -10,24 +10,34 @@ $data["message"] = ""; $data["validated"] = $validated; $data["filename"] = ""; +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"; - header('Content-Type: application/json'); - echo json_encode($data); - exit(); + $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: