---------
<?php
################
# Harkey's Gallery 0.0.2
################
function readfilecomm($filename)
{
//This function is largely unnecessary, and
// can be replaced with readfile() if all captions are named .html
    
if(file_exists($filename))
    {
        
$text =  file_get_contents($filename); 
        echo
"$text";
    }
    else
    echo
"";
}

function 
makefilecomm($filename,$rawfilename,$printcomment)
{
    
$printcomment $printcomment;
    
$filename $filename;
 
    if(!
file_exists($filename) && !file_exists("$rawfilename.txt"))
    {
        
$file fopen($filename"x+");
    
fclose($file);
    }
       
}
function 
captionEdit($caption_file$dir$thumb$password){
if(!isset(
$_GET['submit']))
{
    echo
"
    <br/><em>You must have a valid password to edit captions.</em>
    <form action=\"$PHP_SELF?i=$dir\" method=\"get\">
    Password:<br/>
    <input type=\"text\" name=\"password\" size=\"20\" /><br/>
    Caption:<br/>
    <textarea name=\"save\" cols=\"30\" rows=\"7\">"
;

    require_once(
"$caption_file"); 
    
    echo
"
    </textarea>
    <input type=\"hidden\" name=\"edit\" value=\"1\" />
    <input type=\"hidden\" name=\"i\" value=\"$dir\" />
    <input type=\"hidden\" name=\"file\" value=\"$caption_file\" />
    <input type=\"hidden\" name=\"thumb\" value=\"$thumb\" />
    <input type=\"submit\" name=\"submit\" value=\"&Delta;\" />
    </form>"
;
}
elseif(isset(
$_GET['submit']) && md5($_GET['password']) == "$password")
{
    
$f fopen("$caption_file"'w'); 
    
fwrite($f$_GET['save']); 
    
fclose($f);
    echo
"Caption Edited!";
}
}

function 
validimages($imgs)
{
    foreach(
$imgs as $file)
    {
        
$typename substr($file, -33);//get type of file for resizer(last 3 chars)
        
$length strlen($file);//get file length to remove chars for comments
        
$name substr($file0$length-4);//remove extension(last 4 chars)
        
$check_for_resize spliti("_"$name);
        
$cfr_count count($check_for_resize);
        
        if(
$check_for_resize[$cfr_count-1] != "small")
        {
            
$tf++;
        }
    }
    return(
$tf);   
}

function 
thumb($imgfile$destination$type$percent$maxwidth$maxheight)
{
    list(
$width$height) = getimagesize($imgfile);
    
$type strtolower($type);
    if(
$type == "jpg")
    
$type "jpeg";
    
$imgcreatefromx "imagecreatefrom$type";
    
$imgx "image$type";
    
    if(!
file_exists($destination) && file_exists($imgfile))
    {
        if(
$width $maxwidth || $height $maxheight)
        {
            
$newwidth $width $percent;
            
$newheight $height $percent;
            
$thumb ImageCreateTrueColor($newwidth,$newheight);
            
$source $imgcreatefromx($imgfile);
            
imagecopyresampled($thumb$source0000$newwidth$newheight$width$height);
            
$imgx($thumb,$destination);
        }
    }
}

function 
readpath($dir,$level,$last,&$dirs,&$files){
    if(
is_dir($dir))
    
$dp=opendir($dir);
    elseif(
$dir == "./Timeline")
    {
    
$dir "./Art History";
        
$dp=opendir($dir);    
    }
    else
    {
        echo
"Error:directory not found. Root was selected as default.";
        
$dir ".";
        
$dp=opendir($dir);
    }
    
    while (
false!=($file=readdir($dp)) && $level == $last){
        
        if (
$file!="." && $file!=".."){
            
            if (
is_dir($dir."/".$file) && (!ereg("\.$",$file)))
            {
                
readpath($dir."/".$file,$level+1,$last,$dirs,$files);
                
$dirs[] = "$dir/$file";
            }
            
            else{
                
//print "<a href=\"$PHP_SELF?level+1\">$dir/$file</a><br/>\n";
                //print "<a href=\"$dir/$file\">$dir/$file</a><br/>\n";
                
if(eregi('\.jpg$'$file) || eregi('\.gif$'$file) || eregi('\.png$'$file) || eregi('\.bmp$'$file))
                
$files[] = "$dir/$file";
            }
            
        }
        
    }
}
?>
<<? ?>?xml version="1.0" encoding="ISO-8859-1"?<? ?>>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Harkey's Image Gallery</title>
<link rel="stylesheet" type="text/css" media="screen" href="style.css" />
</head>
<body>
<?php
        
########################################
        ############ Main Display ##############
        ########################################

//For all other installation delete the next three lines and change "elseif" to "if"
if(($_GET['i'] == "./Timeline") || (eregi("timeline",$_GET['i'])))
{
    
$start_dir "./Art History";
}
elseif((isset(
$_GET['i'])) && (!ereg("\.\.",$_GET['i'])))
{
    
$start_dir trim(htmlentities(strip_tags($_GET['i'])));
}
else{
    
$start_dir ".";
}
$last=1;
$level=1;
$edit 0;
$image_count 0;    
$dirs = array();
$files = array();
readpath($start_dir,$level$last$dirs,$files);
$gallery substr($start_dirstrrpos($start_dir'/') + 1strlen($start_dir));
if(
$gallery == NULL)
$gallery "root";
?>
<div class="floatleft">
<strong>Galleries in <?=$gallery?></strong><br/>
<ul><li><a href="<?=$PHP_SELF?>?i=.">Home</a></li>
<?php
// CREATE PREVIOUS LINK THROUGH ARRAY
$menudirs explode("/"trim($start_dir));
$countdirs count($menudirs);
for(
$x 0;$x<$countdirs-1;$x++)
{
    if(
$x>0$previous .= "/".$menudirs[$x];
    else 
$previous .= $menudirs[$x];
}
if(
$previous != "")
echo 
"<li><a href=\"$PHP_SELF?i=".$previous."\">&lt;-Previous</a></li>\n";

sort($dirs);
if(empty(
$dirs))
echo
"";

// display links to sub directories
//$allow = $_GET['allow'];

foreach($dirs as $dir)
{
    echo 
"<li><a href=\"$PHP_SELF?i=$dir\">".
    
substr($dirstrrpos($dir'/') + 1strlen($dir))."</a></li>\n";
    
$count++;
}

print 
"</ul>";

?>
</div>
<div class="files">
<?php
if(file_exists("$start_dir/message.txt"))
readfile("$start_dir/message.txt","r");

echo 
"\n<br/>
<div style=\"border:1px solid black;margin:5px;padding:5px;color:red;\"><strong>Notice:</strong>Please do not hotlink these images! Your cooperation is appreciated.</div><br/>[Total Images:"
.validimages($files)."]<br/>";
sort($files);


    if(
$_GET['edit'] == && isset($_GET['file']) && isset($_GET['thumb']))
    {
        
$editing $_GET['file'];
        
$thumbnail $_GET['thumb'];
        echo
"<div style=\"border:1px solid black;padding:20px;\"><strong>Editing Caption File:</strong> $editing<br/><img src=\"$thumbnail\" alt=\"$editing\" /><br/>";
        
captionEdit($_GET['file'], $start_dir$thumbnail"89a5bf8e1fa69273efd040658ec81156");
        echo
"</div>";
    }

// display all images in the files array.
foreach($files as $file)
{
    
    
$typename substr($file, -33);//get type of file for resizer(last 3 chars)
    
$length strlen($file);//get file length to remove chars for comments
    
$name substr($file0$length-4);//remove extension(last 4 chars)
    
$thumbdest "$name"."_small.$typename";


    
$check_for_resize spliti("_"$name);
    
$cfr_count count($check_for_resize);
    
$length strlen($file);//length to remove file extension
    
$namenoext substr($file0$length-4);//remove image extension(last 4 chars)

  
    //scale the image based on if statement
    
list($width$height) = getimagesize($file);
    
$scaled false;
    if(
$width 400 || $height 400)
    {
        
$subwidth round($width 0.3);
        
$scaled true;
    
    }
    else
    {
        
$subwidth $width;
        
$thumbdest $file;
    }
    
    if(
$check_for_resize[$cfr_count-1] != "small")
    {
    if(
$scaled == true)        
        
thumb($file,$thumbdest,$typename,"0.3","400","400");
        
/* comment file creation */
        
makefilecomm("$namenoext.txt",$namenoext$file);//create comment file
        /*display the image & caption*/
        
echo"<div class=\"sub\" style=\"width:$subwidth"."px\">\n";
        echo 
"<a href=\"$file\"><img class=\"floatimg\" src=\"$thumbdest\" alt=\"$file\" /></a>";
    
$editfn "$name.txt";
        echo 
"<br/><a href=\"$PHP_SELF?i=$start_dir&edit=1&file=$editfn&thumb=$thumbdest\" title=\"Edit Captions\"><img src=\"file_small.gif\" style=\"border:0\" alt=\"caption\" /></a>\n";
        
//PRINT CAPTIONS
        
readfilecomm("$name.txt");
        if(
$scaled == true) echo"\n<br/><a href=\"$file\">[fullsize ".$width."x$height"."]</a>";
        else
        echo
"[".$width."x$height"."]";
        echo
"</div>\n";
    
$image_count++;
    }
}
?>
</div>
</body>
</html>