Compare commits

...

3 Commits

2 changed files with 44 additions and 32 deletions

View File

@ -10,28 +10,40 @@ $data["message"] = "";
$data["validated"] = $validated;
$data["filename"] = "";
if ( !$validated ) {
$data["error"] = true;
$data["message"] = "Clients must validate accounts";
header('Content-Type: application/json');
echo json_encode($data);
exit();
}
if ( isset($_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();
$comic = substr($comicfull, strlen(COMICSDIR));
$comicoutputurl = "comics" . str_replace("#", "", $comic) . "/";
$comicoutputfull = "../" . EXTRACTSDIR . str_replace("#", "", $comic) . "/";
} else {
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']) ) {
$data["error"] = true;
$data["message"] = "Malformed request";
sendResponse();
}
$comicreq = base64_decode($_REQUEST['comic']);
$comicfile = basename($comicreq);
$comicpath = dirname($comicreq);
$comicfull = realpath(COMICSDIR . $comicreq);
if ( $comicfull === false ) {
$data["error"] = true;
$data["message"] = "Requested comic not found";
sendResponse();
}
$data["filename"] = $comicfile;
$comic = safeFileName(substr($comicfull, strlen(COMICSDIR)));
$comicoutputurl = "comics" . $comicpath . "/" . safeFileName($comicfile) . "/";
$comicoutputfull = "../" . EXTRACTSDIR . $comicpath . "/" . safeFileName($comicfile) . "/";
$ext = strtolower(substr($comicfull, -3));
$_SESSION['comfile'] = basename($comicfull);
$_SESSION['comfile'] = $comicfile;
session_write_close();
// Get the current page for this comic or 0 (zero) if never opened
@ -48,20 +60,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";
@ -81,7 +94,7 @@ if ( $extracted ) {
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);
$imgname = safeFileName($imgname);
$imgcontents = $zip->getFromIndex($i);
$written = file_put_contents($comicoutputfull . $imgname, $imgcontents);
if ( $written ) $files[] = $imgname;
@ -96,7 +109,7 @@ if ( $extracted ) {
} else {
$entries = $rar->getEntries();
foreach ( $entries as $entry ) {
$outfile = str_replace("#", "", basename($entry->getName()));
$outfile = safeFileName(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']);
@ -109,7 +122,8 @@ if ( $extracted ) {
}
} else {
$data["error"] = true;
$data["message"] = "Error invalid comic file format";
$data["message"] = "Invalid comic file format";
sendResponse();
}
}
@ -128,8 +142,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:

View File

@ -15,8 +15,8 @@ function getAdultComics() {
}
function safeFileName($file = "") {
$to_remove = array("<", ">", "#", "\"", "/", "\\", "*", "?", "|");
return str_replace($to_remove, '', $file);
$to_remove = array("<", ">", "#", "\"", "/", "\\", "*", "?", "|", "'");
return str_replace($to_remove, "", $file);
}
function makeThumb($item = "") {