<?php

require '../header.php';

require_login();

# usort function for sorting by array's "mtime"
# Return 1 or -1 in reverse order so newest is first
function compareDates($a, $b) {
    if ( $a["mtime"] == $b["mtime"] ) return 0;
    return ($a["mtime"] < $b["mtime"]) ? 1 : -1;
}

# usort function for sorting by array's "name"
# Does "natural" sorting so "28 Days Later" comes before "2099 Hulk"
function compareNames($a, $b) {
    if ( $a["name"] == $b["name"] ) return 0;
    return strnatcmp($a["name"], $b["name"]);
}

$compath = $_SESSION['compath'];
$fullcompath = COMICSDIR . (($compath == "/") ? "" : $compath) . "/";

// 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_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(" - ", "<br />", $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"] = "<div class='item folder{$readclass}' data-path=\"{$info["newpath"]}\" data-name=\"{$info["comname"]}\">";
        $info["div"] .= "<img src=\"{$info["thumburl"]}\"><br clear=all />{$info["comname"]}</div>";
        $folders[] = $info;
    } else {
        if ( (substr($entry, -3) == "cbz") || (substr($entry, -3) == "cbr") ) {
            $info["comname"] = substr(((strpos($entry, " - ") !== false) ? str_replace(" - ", "<br />", $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"] = "<div class='item{$readclass}'>";
            $info["div"] .= "<img class='issue' data-relcomic=\"{$info["relcomic"]}\" data-comname=\"{$info["comname"]}\" border=0 src=\"{$info["thumburl"]}\">";
            $info["div"] .= "<br clear=all /><a href=\"downloadcomic.php?comic={$info["relcomic"]}\">{$info["comname"]}</a></div>";
            $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: