109 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			109 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
require '../header.php';
 | 
						|
 | 
						|
$validated = require_login(NOREDIRECT);
 | 
						|
 | 
						|
$data = array();
 | 
						|
$data["error"] = false;
 | 
						|
$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(urldecode($_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 {
 | 
						|
   exit();
 | 
						|
}
 | 
						|
 | 
						|
$ext = strtolower(substr($comicfull, -3));
 | 
						|
$_SESSION['comfile'] = basename($comicfull);
 | 
						|
 | 
						|
// Get the current page for this comic or 0 (zero) if never opened
 | 
						|
$query = "SELECT currentpage FROM pagetracker WHERE username=:username AND issue=:issue";
 | 
						|
$fields = array();
 | 
						|
$fields[':username'] = $_SESSION['username'];
 | 
						|
$fields[':issue'] = $_SESSION['comfile'];
 | 
						|
$sth = $globaldbh->prepare($query);
 | 
						|
$sth->execute($fields);
 | 
						|
if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
 | 
						|
   $currentpage = intval($row['currentpage']);
 | 
						|
} else {
 | 
						|
   $currentpage = 0;
 | 
						|
}
 | 
						|
$data["startindex"] = $currentpage;
 | 
						|
 | 
						|
if ( !is_dir($comicoutputfull) ) {
 | 
						|
   mkdir($comicoutputfull, 0755, true);
 | 
						|
}
 | 
						|
 | 
						|
$files = array();
 | 
						|
if ( $ext == "cbz" ) {
 | 
						|
   $zip = new ZipArchive();
 | 
						|
   $opened = $zip->open($comicfull);
 | 
						|
   for ( $i=0; $i<$zip->numFiles; $i++ ) {
 | 
						|
      $imgname = basename($zip->getNameIndex($i));
 | 
						|
      //$myext = substr($imgname, -3);
 | 
						|
      if ( ! isset(pathinfo($imgname)['extension']) ) continue;
 | 
						|
      $myext = strtolower(pathinfo($imgname)['extension']);
 | 
						|
      if ( ($myext == "jpeg") || ($myext == "jpg") || ($myext == "png") || ($myext == "webp") ) {
 | 
						|
         $imgname = str_replace("#", "", $imgname);
 | 
						|
         $imgcontents = $zip->getFromIndex($i);
 | 
						|
         $written = file_put_contents($comicoutputfull . $imgname, $imgcontents);
 | 
						|
         if ( $written ) $files[] = $imgname;
 | 
						|
      }
 | 
						|
   }
 | 
						|
} elseif ( $ext == "cbr" ) {
 | 
						|
   $rar = RarArchive::open($comicfull);
 | 
						|
   $entries = $rar->getEntries();
 | 
						|
   foreach ( $entries as $entry ) {
 | 
						|
      $outfile = str_replace("#", "", basename($entry->getName()));
 | 
						|
      $entry->extract(false, $comicoutputfull . $outfile);
 | 
						|
      if ( ! isset(pathinfo($outfile)['extension']) ) continue;
 | 
						|
      $myext = strtolower(pathinfo($outfile)['extension']);
 | 
						|
      if ( ($myext == "jpeg") || ($myext == "jpg") || ($myext == "png") || ($myext == "webp") ) {
 | 
						|
         $files[] = basename($outfile);
 | 
						|
      }
 | 
						|
   }
 | 
						|
   unset($entry);
 | 
						|
} else {
 | 
						|
   exit();
 | 
						|
}
 | 
						|
 | 
						|
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}";
 | 
						|
}
 | 
						|
$debug = (DEBUG_SHOWCOMICAJAX || DEBUG_ALL);
 | 
						|
 | 
						|
$data["debug"] = $debug;
 | 
						|
$data["images"] = $images;
 | 
						|
$data["captions"] = $captions;
 | 
						|
header('Content-Type: application/json');
 | 
						|
echo json_encode($data);
 | 
						|
exit();
 | 
						|
 | 
						|
// vim: set ts=3 sw=3 et:
 |