<?php
// Folderblog 3.0
// http://folderblog.org/

// This work is hereby released into the Public Domain.
// http://creativecommons.org/licenses/publicdomain/

// Server settings
error_reporting(E_ALL);
ini_set("magic_quotes_runtime", 0);
ini_set("url_rewriter.tags", "");
umask(0);

// Import user settings
require_once("fb_settings.php");
require_once("{$template_name}_settings.php");

// Path
$fbself = $_SERVER["REQUEST_URI"];
if($use_alt_url) {
    $path[1] = isset($_GET["p"]) ? $_GET["p"] : "";
    $path[0] = $fbfile = str_replace($path[1], "", $_SERVER["REQUEST_URI"]) . (isset($_GET["p"]) ? "" : "?p=");
} else {
    if (!strpos($_SERVER["REQUEST_URI"], ".php")) {
        $_SERVER["REQUEST_URI"] .= "index.php";
    }
    $path = explode(".php", urldecode($_SERVER["REQUEST_URI"]));
    $fbfile = $path[0] . ".php/";
}
$fbdir = dirname($path[0] . "!") . "/";
$fbdir = str_replace(array("\\","//"), array("/","/"), $fbdir);

// Folderblog 2.x > 3.x redirect
if (isset($_GET["cat"]) || isset($_GET["image"]) || isset($_GET["menu"]) || isset($_GET["comments"])) {
    if (isset($_GET["cat"])) {
        $newcat = $_GET["cat"] . "/";
    } else {
        $newcat = "image/";
    }
    if (isset($_GET["image"])) {
        $newimg = $_GET["image"] . "/";
    } elseif (isset($_GET["comments"])) {
        $newimg = $_GET["comments"] . "/comments/";
    } elseif (isset($_GET["menu"])) {
        $newimg = "archives/" . $_GET["menu"] . "/";
    } else {
        $newimg = $newcom = "";
    }
    header("HTTP/1.1 301 Moved Permanently");
    header("Status: 301", TRUE, 301);
    header("Location: http://" . getenv("SERVER_NAME") . $fbfile . "$newcat$newimg");   
}

// Functions
function get($key) {
    global $_POST;
    if (array_key_exists($key, $_POST)) {
        return (ini_get("magic_quotes_gpc")==1) ? stripslashes(trim(htmlspecialchars($_POST[$key]))) : trim(htmlspecialchars($_POST[$key]));
    }
    return false;
}

function createThumb($filename,$altname="") {
    global $photo_directory, $thumb_directory, $thumb_maxsize, $square_thumbs, $file_extension;
    $original = imagecreatefromjpeg($filename);
    $x = imagesx($original);
    $y = imagesy($original);
    if ($square_thumbs==0) {
        $scale = $thumb_maxsize/max($x, $y);
        $newx = $x*$scale;
        $newy = $y*$scale;
        $thumb = imagecreatetruecolor($newx, $newy);
        imagecopyresampled($thumb, $original, 0, 0, 0, 0, $newx, $newy, $x, $y);
    } elseif ($square_thumbs==1) {
        $scale = $thumb_maxsize/min($x, $y);
        $newx = $x*$scale;
        $newy = $y*$scale;
        $thumb = imagecreatetruecolor($thumb_maxsize, $thumb_maxsize);
        imagecopyresampled($thumb, $original, ($newx-$thumb_maxsize)/-2, ($newy-$thumb_maxsize)/-2, 0, 0, $newx, $newy, $x, $y);
    }
    imagejpeg($thumb, $thumb_directory . basename($filename), 95);
    if ($altname) {
        imagejpeg($thumb, $thumb_directory . $altname, 95);
    }
    return array($newx, $newy);
}

function getThumb($file, $format) {
    global $fbdir, $photo_directory, $thumb_directory, $comments_directory, $captions_directory, $date_format, $time_difference, $file_extension;
    $fbtitle = $fbcaption = $fbcomcount = ""; // Initialize empty variables
    if ($file[0]) {
        $thumbfile = "$thumb_directory{$file[0]}$file_extension";
        if ((!file_exists($thumbfile)) && (function_exists("imagejpeg"))) {
            createThumb($file[1]);
        }
        if (strpos($format, "<#fbtitle#>") || strpos($format, "<#fbcaption#>")) {
            @list($fbtitle, $fbcaption) = explode("\n", file_get_contents("$captions_directory{$file[0]}.txt"), 2);
        }
        $fbcomcount = 0;
        if (strpos($format, "<#fbcomcount#>") && $comments = @file("$comments_directory$image_com.txt")) {
            $fbcomcount=count($comments);
        }
        $size = file_exists($thumbfile) ? getimagesize($thumbfile) : array(3 => "");
        $tags = array("<#fblink#>" => linkPrefix() . "$file[0]/", 
                "<#fbimage#>" => $file[0] ? "<img src=\"$fbdir$thumbfile\" $size[3] alt=\"" . strip_tags($fbtitle) . "\" />" : "", 
                "<#fbcomcount#>" => $fbcomcount, 
                "<#fbtitle#>" => $fbtitle, 
                "<#fbdate#>" => strftime($date_format,filemtime($file[1])+$time_difference), 
                "<#fbcaption#>" => $fbcaption);
    }
    else {
        $tags = array("<#fblink#>" => "", "<#fbimage#>" => "", "<#fbcomcount#>" => "", "<#fbtitle#>" => "", "<#fbdate#>" => "", "<#fbcaption#>" => "");
    }
    return str_replace(array_keys($tags), $tags, $format);
}

function getDirList($index) {
    global $fbfile, $include_cats, $categories, $photo_directory, $file_extension, $post, $sort;
    if ($index=="image") {
        $index = "";
    } else {
        $index .= "/";
    }
    $index_directory = $photo_directory . $index;
    $filelist = glob("$index_directory*$file_extension", GLOB_NOSORT);
    if (($include_cats==1) && (!$index) && ($categories)) {
        foreach($categories as $subdir) {
            if ($content = glob("$subdir/*$file_extension", GLOB_NOSORT)) {
                $filelist = array_merge($filelist, $content);
            }
        }
    }
    isset($filelist[0]) or die("<html><body><strong><a href=\"$fbfile\">folderblog</a> error</strong><br /></h1>No image files found in <i>$index_directory</i>. Please upload files or confirm directory location and permissions.</body></html>");
    while($filename = array_pop($filelist)) {
        $base = basename($filename, $file_extension);
        if (filesize($filename)==0) {
            $thiscat = "";
            $folders = array_merge(array(rtrim($photo_directory, "/")), $categories);
            while(!@filesize("$thiscat/$base$file_extension") && ($folders)) {
                $thiscat = array_shift($folders);
            }
            $filename = "$thiscat/$base$file_extension";
        }
        if ($post||filemtime($filename)<=time()) {
            $files[$filename] = $base;       
        }
    }
    if ($sort==1) {
        natcasesort($files);
    }
    else {
        uksort($files,"cmpmtime");
    }
    $files = array_unique($files);
    foreach($files as $key => $thisfile) {
        $clean[] = array($thisfile, $key); 
    }
    return $clean;
}

function cmpmtime($a, $b) {
    if (filemtime($a)==filemtime($b)) {
        return 0;
    }
    return filemtime($a) < filemtime($b) ? -1 : 1;
}

function linkPrefix() {
    global $fbfile, $post, $category; 
    return "$fbfile$post" . rawurlencode($category) . "/";
}

// Start
@session_start();
setlocale(LC_TIME, $locale);
$time_difference = 3600 * $time_difference;
$post = $image = $category = $fbthumbs = $fbrands = $fbmemberof = $fbcat = $fbmenu = $fbcomments = $fbcomval = $fbpostform = $message = ""; // Initialize empty variables
$page = "main";
if (strlen($path[1])) { // Get URI variables
    $vars = explode("/", trim($path[1], "/"));
    $vars = array_pad($vars, 4, "");
    if ($vars[0]=="post") {
        $post = array_shift($vars) . "/";
    }
    $category = array_shift($vars);
    if ($vars[0]=="archives") {
        $page = array_shift($vars);
        $pageno = array_shift($vars);
    } else {
        $image = rawurldecode(array_shift($vars));
    }
    if ((isset($vars[0])) && ($vars[0]=="comments")) {
       $page = array_shift($vars);
    }
}
if(!$category) {
    $category = "image";
}
$tags = array();
$template_file = "{$template_name}_$page.php";

// Categories
$fbcats = "<ul class=\"fbcats\">" . ($category!="image" ? "<li class=\"fbshowall\">(<a href=\"$fbfile{$post}image/" . ($page=="archives" ? "archives/" : "") .  "\">show all</a>)</li>" : "");
if ($categories = glob("$photo_directory*", GLOB_ONLYDIR)) {
    foreach($categories as $thiscat) {
        $thiscat = basename($thiscat);
        if (!($cat_name = @file_get_contents("$photo_directory$thiscat/category.txt"))) {
            $cat_name = str_replace("_"," ",$thiscat);
        }
        $fbcats .= "<li><a href=\"$fbfile$post$thiscat/" . ($page=="archives" ? "archives/" : "") . "\" class=\"" . ($category==$thiscat ? "fbthiscat" : "fbcat") . "\">$cat_name</a></li>\n";
        if ($category==$thiscat) {
            $fbcat = $cat_name;
        }
    }
}
$fbcats .= "</ul>";

// Comment
if (get("doComment")) {
    $comment = get("comment");
    $fbnameval = $name = get("name");
    $fburlval = $url = get("url");
    $fbemailval = $email = get("email");
    $image = get("image");
    $captcha = get("captcha");
    if ((strlen($comment)) && (strlen($name)) && (!$require_captcha||(isset($_SESSION["md5"])&&$_SESSION["md5"]==md5(strtoupper($captcha))))) {
        $comment = str_replace(array("\n","\r\n"),"<br />",$comment);
        $url = str_replace("http://", "", $url);
        if (strlen(trim($url))) {
            $name = "<a href=\"http://$url\" rel=\"nofollow\" onclick=\"this.target='_blank'\">$name</a>";
        }
        if (!eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z.]{2,5}$', $email)) {
            $fbemailval = $email = "";
        }
        $date = strftime($comdate_format,time()+$time_difference);
        $file = fopen("$comments_directory{$image}_com.txt", "a");
        fwrite($file, stripslashes("$comment<>$name<>$date<>" . str_rot13($email) . "\n"));
        fclose($file);
        if ($email_comments_to) {
            $ip = $_SERVER["REMOTE_ADDR"];
            mail($email_comments_to, $fbblogtitle . " comment",
                "This is an automated message to notify you of the following comment on your image $image:\n\n$comment\n  (posted by $fbnameval <$fbemailval> on $date)\n\nTo go to the image, visit the link below:\n\nhttp://" . getenv("SERVER_NAME") . linkPrefix() .  rawurlencode($image) . "\n\n-- \nComment posted by $ip.\n\nYou may delete the comment via the following link:\n\nhttp://" . getenv("SERVER_NAME") . "{$fbfile}post/" . rawurlencode($category) . "/" .  rawurlencode($image) . "/\n\n(You can disable this feature in your folderblog's fb_settings.php.)",
                "From: folderblog <$email>\r\nReply-To: $email\r\nMIME-Version: 1.0\r\nContent-type: text/plain; charset=iso-8859-1\r\nX-Priority: 3\r\nX-MSmail-Priority: Normal");
        }
    } else {
        $fbcomval = $comment;
    }
    $cookie = get("cookie");
    if ($cookie==1) {
        setcookie("folderblog", "$fbnameval::$fburlval::$fbemailval", time() + 31536000,"/");
    } else {
        setcookie("folderblog", "", time(),"/");
        unset($fbnameval,$_COOKIE["folderblog"]);
    }
}
// Delete Comments
if ((get("delComment")) && isset($_POST["com"]) && (get("pass")==$pass) && ($pass)) {
    $image = get("image");
    $comments = @file("{$comments_directory}{$image}_com.txt");
    foreach($_POST["com"] as $id) {
        unset($comments[$id]);
    }
    $file = fopen("$comments_directory{$image}_com.txt", "w");
    fwrite($file, implode("",$comments));
    fclose($file);
}
    
// Post
if (get("doPost")) {
    @chmod("$photo_directory",0755);
    @chmod("$thumb_directory",0755);
    @chmod("$captions_directory",0755);
    @chmod("$comments_directory",0755);
    if ((get("pass")==$pass) && ($pass)) {
        $_SESSION["passval"] = $pass;
        $image = get("image");
        if ((isset($_FILES['uploaded'])) && (getimagesize($_FILES['uploaded']['tmp_name']))) {
            move_uploaded_file($_FILES['uploaded']['tmp_name'], $photo_directory . $_FILES['uploaded']['name']);
            header("Location: http://" . $_SERVER['HTTP_HOST'] . "{$fbfile}post/image/" . basename($_FILES['uploaded']['name'], $file_extension) . "/");
        } elseif ($_FILES['uploaded']['size']) {
            $message = "Not an image file.";
        } else {
            $timestamp = strtotime(get("timestamp"));
            $title = str_replace(array("<?","?>","\""), array("","","&quot;"), stripslashes($_POST["title"]));
            $caption = str_replace(array("<?","?>"), "", nl2br(stripslashes($_POST["caption"])));
            $file = fopen("$captions_directory$image.txt", "w");
	        fwrite($file, "$title\n$caption");
            fclose($file);
            $cat = isset($_POST["cat"]) ? $_POST["cat"] : array("");
            if ($categories) {
                $folders = array_merge(array(rtrim($photo_directory, "/")), $categories);
            } else {
                $folders = array(rtrim($photo_directory, "/"));
            }
            $original = $folders[0];
            while(!file_exists("$original/$image$file_extension")) {
                $original = next($folders);
            }
            if (($cat[0]) && ($cat[0])!=$original) {
                copy("$original/$image$file_extension", "{$cat[0]}/$image$file_extension");
            }
            foreach($folders as $thiscat) {
                if (in_array($thiscat, $cat)) {
                    if ($contents = @file_get_contents("$thiscat/$image$file_extension")) {
                        unlink("$thiscat/$image$file_extension");
                    }
                    $file = fopen("$thiscat/$image$file_extension", "w");
                    fwrite($file, $contents);
                    fclose($file);
                    touch("$thiscat/$image$file_extension", $timestamp);
                } elseif (file_exists("$thiscat/$image$file_extension")) {
                    unlink("$thiscat/$image$file_extension");
                }
            }
            $message = "Update successful!";
        }
    } elseif ($pass) {
        $message = "Password incorrect.";
    } else {
        $message = "Please set a password in fb_settings.php.";
    }
}

$files = getDirList($category); // Get Files
$total = count($files);

if (get("doPost")) { // RSS, current thumb
    if ($fbrss) {
        $rss = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n<rss version=\"2.0\">\n<channel>\n<generator>Folderblog 3</generator>\n<title>$fbblogtitle</title>\n<link>http://" . $_SERVER["SERVER_NAME"] . "$fbfile</link>\n<description>$rss_desc</description>\n<language>$rss_lang</language>\n<lastBuildDate>" . date("r") . "</lastBuildDate>\n<webMaster>$email_comments_to</webMaster>\n<docs>http://blogs.law.harvard.edu/tech/rss</docs>\n";
        for($i=count($files)-1; ($i>count($files)-11) && ($i>0); $i--) {
            @list($itemtitle,$itemcaption) = explode("\n",file_get_contents("$captions_directory{$files[$i][0]}.txt"), 2);
            $itemthumb = "$thumb_directory{$files[$i][0]}$file_extension";
            $rss .= "<item>\n<title>$itemtitle</title>\n<link>http://" . $_SERVER["SERVER_NAME"] . "{$fbfile}image/" . rawurlencode($files[$i][0]) . "/</link>\n<description><![CDATA[\n" . ($rss_show_thumbs ? "<img src=\"http://" . $_SERVER["SERVER_NAME"] . "$fbdir$itemthumb\" /><br />" : "") . "$itemcaption\n]]></description><pubDate>" . date("r",filemtime($files[$i][1])) . "</pubDate>\n</item>\n";
        }
        $rss .= "</channel>\n</rss>";
        $file = fopen($fbrss, "w");
	    fwrite($file, $rss);
        fclose($file);
    }
    if ((function_exists("imagejpeg"))) {
        createThumb($files[count($files)-1][1],"current.jpg");
        createThumb($files[array_rand($files)][1],"random.jpg");
    }
}

if (($page=="main") || ($page=="comments")) { // Main Page or Comments Page
    $number = $show_thumbs<3 ? 1 : floor($show_thumbs/2);
    if ($image) {
        for($i=0; ($i<$total) && ($image!=$files[$i][0]); $i++);
        if ($i<$total) { 
            $image = $i;
        }
    }
    if (!is_int($image)) {
        if ($reverse==0) {
            $image = $total-1;
        } else {
            $image = 0;
        }
    }
    if ($loop_photos) {
        $files = array_merge(array_slice($files,$total-$number), $files, array_slice($files,0,$number));
    } else {
        $files = array_pad($files, ($total+$number)*-1, "");
        $files = array_pad($files, (count($files)+$number), "");
    }
    $image += $number;
    $images = array_slice($files, $image-$number, $show_thumbs<3 ? 3 : $show_thumbs);
    $files = array_diff($files, array(""));
    //Random Thumbs
    if ($rand_thumbs>0) {
        srand($image*16);
        if (count($files)>1) {
            $rands = array_rand($files, ($rand_thumbs<count($files) ? $rand_thumbs : count($files)));
        } elseif (count($files)==1) {
            $rands = array_keys($files);
        } else {
            $rands = array();
        }
        if (!is_array($rands)) {
            $rands = array($rands);
        }
        if (count($rands)<$rand_thumbs) {
            $rands = array_pad($rands, $rand_thumbs, "blank");
        }
        $files["blank"] = "";
        foreach($rands as $thisrand) {
            if ($files[$thisrand]) {
                $fbrands .= getThumb($files[$thisrand], $randthumb_format);
            } elseif ($show_empties) {
                $fbrands .= getThumb(array("", ""), $randthumb_format);
            }
        }
    }
    unset($files);
    // Regular thumbs:
    $thisimage = floor(count($images)/2);    
    if ($show_thumbs>0) {
        for($i=0; $i<$show_thumbs; $i++) {
            if ($images[$i]) {
                $fbthumbs .= getThumb($images[$i], ($i==$thisimage) ? $thisthumb_format : $thumb_format);
            } elseif ($show_empties) {
                $fbthumbs .= getThumb(array("", ""), $thumb_format);
            }
        }
    }
    $fbimage = $images[$thisimage];
    @list($fbtitle, $fbcaption) = explode("\n", file_get_contents("$captions_directory{$fbimage[0]}.txt"), 2);
    $size = getimagesize($fbimage[1]);
    $postcats = "<ul class=\"fbcats\"><li><input type=\"checkbox\" name=\"cat[]\" value=\"" . rtrim($photo_directory, "/") . "\"" . (file_exists("$photo_directory{$fbimage[0]}$file_extension") ? " checked=\"checked\"" : "") . " /><a href=\"{$fbfile}post/image/\" class=\"" . ($category=="image" ? "fbthiscat" : "fbcat") . "\">main directory</a></li>";
    if ($categories) {
        foreach($categories as $thiscat) {
            $postcats .= "<li><input type=\"checkbox\" name=\"cat[]\" value=\"$thiscat\" id=\"" . str_replace(" ","_",basename($thiscat)) . "\"";
            $thiscat = basename($thiscat);
            if (!($cat_name=@file_get_contents("$photo_directory$thiscat/category.txt"))) {
                $cat_name = str_replace("_"," ",$thiscat);
            }
            if (file_exists("$photo_directory$thiscat/{$fbimage[0]}$file_extension")) {            
                $fbmemberof .= "<a href=\"$fbfile$thiscat/\">$cat_name</a> ";
                $postcats .= " checked=\"checked\"";
            }
            $postcats .= " /><a href=\"$fbfile$post$thiscat/\" class=\"" . ($category==$thiscat ? "fbthiscat" : "fbcat") . "\">$cat_name</a></li>\n";
        }
    }
    $postcats .= "</ul>";
    $fbcomcount = 0;
    if ($comments = @file("{$comments_directory}{$fbimage[0]}_com.txt")) {
        $fbcomcount = count($comments);
        foreach($comments as $id=>$thiscomment) {
            $parts = explode("<>", $thiscomment);
            if (isset($parts[3])) {
                $parts[3] = str_rot13(trim($parts[3]));
            }
            if ($post) {
                $parts[0] = "<input type=\"checkbox\" name=\"com[]\" value=\"$id\" />" . $parts[0];
            }
            $fbcomments .= str_replace(array("<#fbcomment#>","<#fbauthor#>","<#fbdate#>","<#fbemail#>"), $parts, $comment_format);
        }
    }
    // load cookie
    $fbchecked = "checked=\"checked\"";
    if (isset($_COOKIE["folderblog"])) {
        $_COOKIE["folderblog"] = stripslashes(htmlspecialchars(strip_tags($_COOKIE["folderblog"])));
        $cookieval = explode("::",$_COOKIE["folderblog"]);
        $fbnameval = $cookieval[0];
        $fburlval = $cookieval[1];
        $fbemailval = isset($cookieval[2]) ? $cookieval[2] : "";
    } elseif (!isset($fbnameval)) {
        $fbnameval = $fburlval = $fbemailval = $fbchecked = "";
        $fburlval = "http://";
    }
    if ($post) {
        if (!isset($_SESSION["passval"])) {
            $_SESSION["passval"] = "";
        }
        $fbpostform = "<form method=\"post\" action=\"$fbself\" enctype=\"multipart/form-data\"><div class=\"fbpostform\"><strong>$message</strong><br />Title: <input name=\"title\" size=\"30\" value=\"$fbtitle\" /><br />Posted: <input name=\"timestamp\" size=\"20\" maxlength=\"19\" value=\"" .  date("Y-m-d H:i:s", filemtime($fbimage[1])+$time_difference) . "\" id=\"timestamp\" title=\"yyyy-mm-dd hh-mm-ss\" /> <input type=\"button\" value=\"Now\" onclick=\"document.getElementById('timestamp').value='" . date("Y-m-d H:i:s",time()+$time_difference) . "'\" /><br />Caption:<br /><textarea cols=\"34\" rows=\"3\" name=\"caption\">" . htmlspecialchars($fbcaption) . "</textarea><br />$postcats<br /><em>Or</em> upload a new image: <input type=\"file\" name=\"uploaded\" /><br />Password: <input name=\"pass\" type=\"password\" size=\"12\" value=\"{$_SESSION["passval"]}\" /><br /><input type=\"submit\" value=\"Update\" name=\"doPost\" /><input type=\"hidden\" name=\"image\" value=\"{$fbimage[0]}\" /></div></form>";
        $fbcomments = "<form method=\"post\" action=\"$fbself\">" . $fbcomments . "<div class=\"fbcomform\"><input type=\"hidden\" name=\"image\" value=\"{$fbimage[0]}\" style=\"display: none;\"/>Password: <input name=\"pass\" type=\"password\" size=\"12\" value=\"{$_SESSION["passval"]}\" /> <input type=\"submit\" value=\"Delete Checked\" name=\"delComment\" /></div></form>";
    } else {
        @session_destroy();
    }
    // Tags:
    $tags += array("<#fbimage#>" => $fbimage[1],
                   "<#fbimagename#>" => $fbimage[0],
                   "<#fbimageurl#>" => "$fbdir{$fbimage[1]}",
                   "<#fbwidth#>" => $size[0], 
                   "<#fbheight#>" => $size[1], 
                   "<#fbtitle#>" => $fbtitle,
                   "<#fbdate#>" => strftime($date_format,filemtime($fbimage[1])+$time_difference),
                   "<#fbcaption#>" => $fbcaption, 
                   "<#fbnumber#>" => $image-$number+1, 
                   "<#fbmemberof#>" => $fbmemberof, 
                   "<#fbthumbs#>" => $fbthumbs, 
                   "<#fbrands#>" => $fbrands, 
                   "<#fbprev#>" => $images[$thisimage-1] ? $fbprev : "", 
                   "<#fbprevlink#>" => $images[$thisimage-1] ? linkPrefix() . $images[$thisimage-1][0] . "/" : $emptylink, 
                   "<#fbnext#>" => $images[$thisimage+1] ? $fbnext : "", 
    			   "<#fbnextlink#>" => $images[$thisimage+1] ? linkPrefix() . $images[$thisimage+1][0] . "/" : $emptylink,               
                   "<#fbarchives#>" => $fbarchives, 
                   "<#fbarchiveslink#>" => linkPrefix() . "archives/" . ceil(($image-$number+1)/($thumbs_perrow*$rows_perpage)) . "/",
                   "<#fbcomments#>" => $fbcomments,
                   "<#fbcomthumb#>" => getThumb($fbimage, $comthumb_format),
                   "<#fbcomlink#>" => $allow_comments ? $fbcomlink : "",
                   "<#fbcommentslink#>" => linkPrefix() . $fbimage[0] . "/comments/",
                   "<#fbcomcount#>" => $fbcomcount,
                   "<#fbnameval#>" => $fbnameval,
                   "<#fburlval#>" => $fburlval,
                   "<#fbemailval#>" => $fbemailval,
                   "<#fbchecked#>" => $fbchecked,
                   "<#fbcaptcha#>" => $require_captcha ? $fbcaptcha : "",
                   "<#fbpostform#>" => $fbpostform,
                   "<#fbcomval#>" => $fbcomval);
} elseif ($page=="archives") { // Menu
    if (!$pageno) {
        $pageno = 1;
    }
    $thumbs_perpage = $thumbs_perrow*$rows_perpage;
    $imax=$pageno*$thumbs_perpage;
    if (($show_empties) && ($imax>$total)) {
        $files = array_pad($files, $imax, array("", ""));
    } elseif ($imax>$total) {
        $imax = $total;
    }
    for($i=($pageno-1)*$thumbs_perpage; $i<$imax; $i++) {
        $fbmenu .= getThumb($files[$i], $menuthumb_format);
        if ((($i+1)==$imax) || (($i+1)%$thumbs_perrow==0)) {
            $fbmenu .= "$row_divider\n";
        }
    }
    unset($files);
    $fbpages = "<div class=\"fbpages\">";
    for($i=1;$i<=ceil($total/$thumbs_perpage);$i++) {
        $fbpages .= ($i==$pageno) ? "<span class=\"fbthispage\">$i</span> " : "<a href=\"" . linkPrefix() . "archives/$i\">$i</a> ";
    }
    $fbpages .= "</div>";
    $tags += array("<#fbmenu#>" => $fbmenu, 
                   "<#fbprev#>" => $pageno>1 ? $fbprev : "", 
                   "<#fbprevlink#>" => $pageno>1 ? linkPrefix() . "archives/" . ($pageno-1) . "/" : $emptylink, 
                   "<#fbnext#>" => $imax<$total-1 ? $fbnext : "", 
    			   "<#fbnextlink#>" => $imax<$total-1 ? linkPrefix() . "archives/" . ($pageno+1) . "/" : $emptylink,
                   "<#fbpages#>" => $fbpages,
                   "<#fbrange#>" =>  (($pageno-1) * $thumbs_perpage)+1 . "-" . min($imax,$total));
} 

$tags += array("<#fbblogtitle#>" => $fbblogtitle, 
               "<#fbdir#>" => $fbdir, 
               "<#fbself#>" => $fbself,
               "<#fbfile#>" => $fbfile, 
               "<#fbtotal#>" => $total,
               "<#fbcats#>" => $fbcats,
               "<#fbcat#>" => $fbcat,
               "<#fbrss#>" => "$fbdir$fbrss",
               "<#fbcss#>" => "$fbdir{$template_name}_style.css");
unset($pass);
if ($extensions = glob("extensions/*.php")) {
    foreach ($extensions as $thisExt) {
	    include_once($thisExt);
    }
}
// Do output
eval('?>' . str_replace(array_keys($tags), $tags, file_get_contents($template_file)) . '<?php ');
?>
