<?php
// Set path settings

$localFolderPath '/home/knigga5/public_html/gallery/';
$homeURL 'http://www.knigga.com/';
$numbCol 3;
$rowsPerPage 3;
$width 200;
$height 200;

// End of user settings








// What gallery do we want?
$gallery addslashes($_GET['g']);
if(
$gallery == "")
{
    
//  Get listing
    
$sqlstring "SELECT name, title FROM galleries WHERE public = 1 ORDER BY name ASC;";
    
$resultarray db_query($sqlstring);
    
    
?>
    
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<?php include("../meta.html"); ?>
<title> www.KNIGGA.com - my pictures </title>
</head>
<body>
<p ALIGN="center">
<font SIZE="5">www.KNIGGA.com - my pictures</font>
</p>
<hr NOSHADE WIDTH="80%">
<?php include("../header.html"); ?>
<table align="center" width="80%">
 <tr>
  <td>
   <ul>
<?php
$entries 
count($resultarray);

// figure total number of rows
$numbRow = (int)($entries 3);
if((
$entries 3) != 0)
{
    
$numbRow++;
}

for(
$i 0$i $numbRow$i++)
{
    if(isset(
$resultarray[$i])){?><li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?g=<?php echo $resultarray[$i]['name']; ?>"><?php echo $resultarray[$i]['title']; ?></a></li><?php }
?>
   </ul>
  </td>
  <td>
   <ul>
<?php
for($i $numbRow$i < ($numbRow); $i++)
{
    if(isset(
$resultarray[$i])){?><li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?g=<?php echo $resultarray[$i]['name']; ?>"><?php echo $resultarray[$i]['title']; ?></a></li><?php }
?>
   </ul>
  </td>
  <td>
   <ul>
<?php
for($i = ($numbRow); $i < ($numbRow); $i++)
{
    if(isset(
$resultarray[$i])){?><li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?g=<?php echo $resultarray[$i]['name']; ?>"><?php echo $resultarray[$i]['title']; ?></a></li><?php }
?>
   </ul>
  </td>
 </tr>
</table>
<?php include("../footer.html"); ?>
</body>
</html>
    
    <?php
    
exit();
}

// What page is this?
$pg $_GET['pg'];
if(
$pg == "")
{
    
$pg 0;
}

// Get page title
$sqlstring "SELECT title, name FROM galleries WHERE name = '$gallery';";
$resultarray db_query($sqlstring);

if(
$resultarray[0]['name'] == "")
{
    exit(
"Specified gallery does not exist");
}

// Rewrite path vars to include gallery info
$localFolderPath $localFolderPath.$resultarray[0]['name'];

// Error out if a non-existant gallery is specified
if(!file_exists($localFolderPath))
{
    exit(
"Specified gallery does not exist");
}

// Get directory listing and sort files
$dh  opendir($localFolderPath);
$fileCount 0;
while (
false !== ($filename readdir($dh))) {
   
$fileArray[] = $filename;
   
$fileCount++;
}
closedir($dh);
sort($fileArray);

// Make thumbnail directory if it isn't there
if(!file_exists($localFolderPath."/thumbs"))
{
    
mkdir($localFolderPath."/thumbs"0755);
}

// Weed out the directories
$fileCountClean 0;
for(
$i 0$i $fileCount$i++)
{
    
$absFileLoc $localFolderPath."/".$fileArray[$i]."";
    if(!
is_dir($absFileLoc))
    {
        
$fileArrayClean[] = $fileArray[$i];
        
$fileCountClean++;
    }
}

// figure total number of rows
$numbRow = (int)($fileCountClean $numbCol);
if((
$fileCountClean $numbCol) != 0)
{
    
$numbRow++;
}

// figure total number of pages
$numbPg = (int)($fileCountClean / ($numbCol $rowsPerPage));
if((
$fileCountClean % ($numbCol $rowsPerPage)) != 0)
{
    
$numbPg++;
}

// Error out if a non-existant page is specified
if($pg >= $numbPg)
{
    exit(
"Silly goose!  There aren't that many pages in this particular gallery.");
}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<?php include("../meta.html"); ?>
<title> www.KNIGGA.com - my pictures - <?php echo $resultarray[0]['title']; ?> </title>
</head>
<body>
<!--

gallery name: <?php echo $resultarray[0]['name']."\n"?>
number of files in gallery: <?php echo $fileCountClean."\n"?>
number of pages: <?php echo $numbPg."\n"?>

-->
<p ALIGN="center">
<font SIZE="5">www.KNIGGA.com - my pictures - <?php echo $resultarray[0]['title']; ?></font>
</p>
<hr NOSHADE WIDTH="80%">
<?php include("../header.html"); ?>
<table align="center" cellspacing="4">
<?php

$pos 
$pg $rowsPerPage $numbCol;

for(
$y 0$y $rowsPerPage$y++)
{
    echo 
"<tr>\n";
    
$rowID $pos $numbCol;
    for(
$x 0$x $numbCol$x++)
    {
        echo 
"<td align='center' valign='middle'>\n";
        echo 
"<!--\nx: ".$x."\ny: ".$y."\npos: ".$pos."\n-->\n";
        if(
$pos $fileCountClean)
        {
            if(!
file_exists($localFolderPath."/thumbs/".$fileArrayClean[$pos]))
            {
                echo 
"<a href='".$resultarray[0]['name']."/".$fileArrayClean[$pos]."'><img border='0' src='thumb.php?f=".base64_encode($localFolderPath."/".$fileArrayClean[$pos])."&amp;h=".$height."&amp;w=".$width."' alt='generating thumbnail - ".$fileArrayClean[$pos]."'></a>\n";
            }
            else
            {
                echo 
"<a href='".$resultarray[0]['name']."/".$fileArrayClean[$pos]."'><img border='0' src='".$resultarray[0]['name']."/thumbs/".$fileArrayClean[$pos]."' alt='thumbnail - ".$fileArrayClean[$pos]."'></a>\n";
            }
        }
        echo 
"</td>\n";
        
$pos++;
    }
    echo 
"</tr>\n";
    
}

?>
</table>
<p align="center">
<?php
echo "page ".($pg 1)." of ".$numbPg."<br>";
if(
$numbPg 1)
{
    switch(
$pg)
    {
        case 
0:
            
?>
            
    <a href="<?php echo $_SERVER['PHP_SELF']."?g=".$resultarray[0]['name']."&amp;pg=".($pg 1); ?>">next</a>
            
            <?php
                
break;
        case (
$numbPg 1):
            
?>
            
    <a href="<?php echo $_SERVER['PHP_SELF']."?g=".$resultarray[0]['name']."&amp;pg=".($pg 1); ?>">prev</a>
            
            <?php
            
break;
        default:
            
?>
            
    <a href="<?php echo $_SERVER['PHP_SELF']."?g=".$resultarray[0]['name']."&amp;pg=".($pg 1); ?>">prev</a> &nbsp;|&nbsp; <a href="<?php echo $_SERVER['PHP_SELF']."?g=".$resultarray[0]['name']."&amp;pg=".($pg 1); ?>">next</a>
            
            <?php
            
break;
    }
}
?>
</p>
<?php include("../footer.html"); ?>
</body>
</html>

<?php
function db_query($sqlstring)
{
        include_once(
"../db.php");
        
        
//  let's make a connection and select the db
        
mysql_connect($DBserver$DBuser$DBpass) or die(mysql_error());
        
mysql_select_db("knigga5_news") or die(mysql_error());
        
//  run query
        
$sqlqueryresult mysql_query($sqlstring) or die(mysql_error());
        
//  generate output array if SELECT
        
if(substr_count(strtolower($sqlstring), "select") > 0)
        {
            
$resultarray = array();
            while (
$row mysql_fetch_array($sqlqueryresult)) {
                 
$resultarray[] = $row;
             };
         }
         
//  close db connection
         
mysql_close();
         
//  return the output array
         
return $resultarray;
}
?>