From eb4ac7bb61498185c91e163aff893994b8990470 Mon Sep 17 00:00:00 2001 From: Junior Date: Mon, 21 Aug 2023 20:55:09 -0400 Subject: [PATCH] Switch to jQuery/AJAX folder navigation --- ajax/getfoldercontents.php | 101 ++++++++++++++++++++++++++ ajax/setpath.php | 25 +++++++ htmlfooter.php | 4 ++ htmlheader.php | 35 +++++++++ index.php | 173 ++------------------------------------------- js/comics.js | 53 +++++++++++--- 6 files changed, 217 insertions(+), 174 deletions(-) create mode 100644 ajax/getfoldercontents.php create mode 100644 ajax/setpath.php create mode 100644 htmlfooter.php create mode 100644 htmlheader.php diff --git a/ajax/getfoldercontents.php b/ajax/getfoldercontents.php new file mode 100644 index 0000000..e5fd11b --- /dev/null +++ b/ajax/getfoldercontents.php @@ -0,0 +1,101 @@ +prepare($query); +$sth->execute($fields); +$comics_read = array(); +$issues_read = array(); +while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) { + if ( !in_array($row['comic'], $comics_read) ) $comics_read[] = $row['comic']; + if ( !in_array($row['issue'], $issues_read) ) $issues_read[] = $row['issue']; +} + +$data = array(); +$folders = array(); +$issues = array(); +$entries = scandir($fullcompath); +foreach ( $entries as $entry ) { + if ( ($entry == ".") || ($entry == "..") ) continue; + $info = array(); + $info["name"] = $entry; + $info["namesafe"] = htmlspecialchars($entry, ENT_QUOTES); + if ( is_dir($fullcompath . $entry) ) { + $info["comname"] = (strpos($entry, " - ") !== false) ? str_replace(" - ", "
", $entry) : $entry; + $info["newpath"] = $compath . (($compath == "/") ? "" : "/") . $entry; + $info["mtime"] = filemtime($fullcompath . $entry); + //$info["thumburl"] = htmlentities(THUMBSDIR . $compath . (($compath == "/") ? "" : "/") . $entry, ENT_QUOTES) . ".jpg"; + $info["thumburl"] = THUMBSDIR . $compath . (($compath == "/") ? "" : "/") . $entry . ".jpg"; + if ( in_array($entry, $comics_read) ) { + $info["read"] = true; + $readclass = " readborder"; + } else { + $info["read"] = false; + $readclass = ""; + } + $info["div"] = "
"; + $info["div"] .= "
{$info["comname"]}
"; + $folders[] = $info; + } else { + if ( (substr($entry, -3) == "cbz") || (substr($entry, -3) == "cbr") ) { + $info["comname"] = substr(((strpos($entry, " - ") !== false) ? str_replace(" - ", "
", $entry) : $entry), 0, -4); + $info["relcomic"] = base64_encode($compath . (($compath == "/") ? "" : "/") . $entry); + $info["mtime"] = filemtime($fullcompath . $entry); + $info["thumburl"] = htmlentities(THUMBSDIR . $compath . (($compath == "/") ? "" : "/") . substr($entry, 0, -4), ENT_QUOTES) . ".jpg"; + if ( in_array($entry, $issues_read) ) { + $info["read"] = true; + $readclass = " readborder"; + } else { + $info["read"] = false; + $readclass = ""; + } + $info["div"] = "
"; + $info["div"] .= ""; + $info["div"] .= "
{$info["comname"]}
"; + $issues[] = $info; + } + } + if ( isset($info["thumburl"]) && !file_exists("../" . $info["thumburl"]) ) { + $havethumb = makeThumb($fullcompath . $entry); + } +} + +if ( $_SESSION["sortorder"] == SORTBYNAME ) { + usort($folders, "compareNames"); + usort($issues, "compareNames"); +} else { + usort($folders, "compareDates"); + usort($issues, "compareDates"); +} + +$data["compath"] = $compath; +$data["contents"] = array_merge($folders, $issues); + +header('Content-Type: application/json'); +echo json_encode($data); +exit(); + +// vim: set ts=4 sw=4 et: diff --git a/ajax/setpath.php b/ajax/setpath.php new file mode 100644 index 0000000..6987203 --- /dev/null +++ b/ajax/setpath.php @@ -0,0 +1,25 @@ + + + + diff --git a/htmlheader.php b/htmlheader.php new file mode 100644 index 0000000..01527db --- /dev/null +++ b/htmlheader.php @@ -0,0 +1,35 @@ + + + + + + + + + + + +Comics Fancy + + + + + + + + + + + + diff --git a/index.php b/index.php index 6cc24c3..63356a5 100644 --- a/index.php +++ b/index.php @@ -10,101 +10,8 @@ if ( isset($_REQUEST['sortorder']) ) { } } -$validext = array('cbr', 'cbz'); - -if ( $_SESSION['compath'] == "" ) $_SESSION['compath'] = "/"; -if ( isset($_REQUEST['newpath']) ) { - $comicfull = realpath(COMICSDIR . urldecode($_REQUEST['newpath'])); - if ( ($comicfull === false) || (substr($comicfull, 0, strlen(COMICSDIR)) != COMICSDIR) ) { - $_SESSION['compath'] = "/"; - } else { - $_SESSION['compath'] = substr($comicfull, strlen(COMICSDIR)); - } -} - -$compath = $_SESSION['compath']; - -$fullcompath = COMICSDIR . $compath; -$header = basename($compath); -if ( $header == "" ) { $header = "Comics"; } - -$fltime = microtime_float(); -$folders = array(); -$files = array(); -if ( $fh = opendir($fullcompath) ) { - while ( false !== ($entry = readdir($fh)) ) { - $mtime = filemtime($fullcompath . "/" . $entry); - if ( ($entry != ".") && ($entry != "..") && is_dir($fullcompath . "/" . $entry) ) { - $folders[$mtime] = $entry; - } - if ( is_file($fullcompath . "/" . $entry) - && in_array(substr($entry, -3), $validext) ) { - $files[] = $entry; - } - } - closedir($fh); - if ( $_SESSION['sortorder'] == SORTBYNAME ) { - natsort($folders); - } else { - ksort($folders); - $folders = array_reverse($folders); - } - sort($files); -} -$items = array_merge($folders, $files); -$fltime = microtime_float() - $fltime; - -$updir = dirname($compath); -$updirlink = urlencode($updir); - -// build up a list of comics and issues which have been -$query = "SELECT comic, issue FROM pagetracker WHERE username=:username"; -$fields = array(); -$fields[":username"] = $_SESSION['username']; -$sth = $globaldbh->prepare($query); -$sth->execute($fields); -$comics = array(); -$issues = array(); -while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) { - if ( !in_array($row['comic'], $comics) ) $comics[] = $row['comic']; - if ( !in_array($row['issue'], $issues) ) $issues[] = $row['issue']; -} +require_once 'htmlheader.php'; ?> - - - - - - - - - - - -Comics Fancy - - - - - - - - - - - -
Sort:
- - -
$updir"; - if ( dirname($compath) == "/" ) { echo "Comics"; } - echo "/"; -} -?>
-
+ +
+
+ +
"; - $newthumbs++; - $st = microtime_float(); - $havethumb = makeThumb($fullcompath . "/" . $items[$i-1]); - $thumbtime += microtime_float() - $st; - if ( !$havethumb ) { - $havethumb = true; - $thumb = "img/nothumb.png"; - } - } - $thumb = "thumbs" . $compath . "/" .basename($thumbfile); - $comname = pathinfo($items[$i-1], PATHINFO_FILENAME); - if ( strpos($comname, " - ") !== false ) $comname = str_replace(" - ", "
", $comname); - $classmod = ""; - if ( $i <= count($folders) ) { - if ( in_array($items[$i-1], $comics) ) $classmod=" readborder"; - echo "
"; - $thumburl = htmlentities($thumb, ENT_QUOTES); - echo "
{$comname}
\n"; - } else { - if ( in_array($items[$i-1], $issues) ) $classmod=" readborder"; - $relcomic = trim(base64_encode($compath . "/" . $items[$i-1]), "="); - $thumburl = htmlentities($thumb, ENT_QUOTES); - echo "
"; - echo "
"; - echo "{$comname}
\n"; - } -} -$htime = microtime_float() - $htime; -?> - -
-
- - -If this is the first time it may take a while to generate thumbnails."); + $.ajax({ + url : 'ajax/setpath.php', + data : {path: path}, + dataType : 'json', + success : function(data, stat, jqo) { + console.log(data.message); + curpath = path; + getFolderContents(); + } + }); +} + function updateCurrentPage() { var currentPage = lightbox.currentPosition; $.ajax({ @@ -18,11 +61,6 @@ function updateCurrentPage() { }); } -function changeFolder(name, path) { - toastr.success("Opening comic \"" + name + "\"
If this is the first time it may take a while to generate thumbnails."); - window.location.replace("index.php?newpath=" + path); -} - // // This JS function is called when a user clicks on a comic. // "showcomic.php" is called with the comic to view as a parameter. @@ -31,7 +69,6 @@ function changeFolder(name, path) { // If not in debug mode a lightbox object is created to display the comic. // function showComic(comic, name) { - $("#" + comic).addClass("readborder"); toastr.success("Extracting and showing the comic \"" + name + "\"...", "Showing Comic"); $.ajax({ url : 'ajax/showcomic.php',