143 lines
4.3 KiB
PHP
143 lines
4.3 KiB
PHP
<?php
|
|
|
|
require '../header.php';
|
|
|
|
$validated = require_login(NOREDIRECT);
|
|
|
|
$data = array();
|
|
$data["error"] = false;
|
|
$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";
|
|
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'] = $comicfile;
|
|
session_write_close();
|
|
|
|
// Get the current page for this comic or 0 (zero) if never opened
|
|
$query = "SELECT currentpage FROM pagetracker WHERE username=:username AND issue=:issue";
|
|
$sth = $globaldbh->prepare($query);
|
|
$sth->bindValue(":username", $_SESSION['username'], PDO::PARAM_STR);
|
|
$sth->bindValue(":issue", $_SESSION['comfile'], PDO::PARAM_STR);
|
|
$sth->execute();
|
|
if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
|
|
$currentpage = intval($row['currentpage']);
|
|
} else {
|
|
$currentpage = 0;
|
|
}
|
|
$data["startindex"] = $currentpage;
|
|
|
|
$extracted = false;
|
|
$files = array();
|
|
if ( !is_dir($comicoutputfull) ) {
|
|
mkdir($comicoutputfull, 0755, true);
|
|
} else {
|
|
touch($comicoutputfull);
|
|
$contents = glob($comicoutputfull . "*");
|
|
foreach ( $contents as $i ) {
|
|
$files[] = basename($i);
|
|
}
|
|
if ( count($files) != 0 ) {
|
|
$extracted = true;
|
|
}
|
|
}
|
|
|
|
if ( !$extracted ) {
|
|
$comtype = getComicType($comicfull);
|
|
if ( $comtype === COMICZIP ) {
|
|
$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 = safeFileName($imgname);
|
|
$imgcontents = $zip->getFromIndex($i);
|
|
$written = file_put_contents($comicoutputfull . $imgname, $imgcontents);
|
|
if ( $written ) $files[] = $imgname;
|
|
}
|
|
$zip->close();
|
|
}
|
|
} elseif ( $comtype === COMICRAR ) {
|
|
$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 = 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']);
|
|
if ( ! in_array($myext, VALIDIMAGETYPES) ) continue;
|
|
$entry->extract(false, $comicoutputfull . $outfile);
|
|
$files[] = basename($outfile);
|
|
}
|
|
unset($entry);
|
|
$rar->close();
|
|
}
|
|
} else {
|
|
$data["error"] = true;
|
|
$data["message"] = "Invalid comic file format";
|
|
sendResponse();
|
|
}
|
|
}
|
|
|
|
natsort($files);
|
|
|
|
$images = array();
|
|
$captions = array();
|
|
|
|
$last = count($files);
|
|
$count = 0;
|
|
foreach ( $files as $file ) {
|
|
$count++;
|
|
$images[] = $comicoutputurl . $file;
|
|
$captions[] = substr(basename($comicoutputurl), 0, -4) . ": {$count} of {$last}";
|
|
}
|
|
|
|
$data["images"] = $images;
|
|
$data["captions"] = $captions;
|
|
sendResponse();
|
|
|
|
// vim: set ts=3 sw=3 et:
|