#!/usr/bin/perl -w

# frame.cgi
#
# This script is part of Birch. It displays the photo and caption for a given date.
#
# Call with /cgi-bin/frame?date=date_to_display.
#
# Copyright (c) 2006, Ross Harmes
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without modification, 
# are permitted provided that the following conditions are met:
# 
#	*	Redistributions of source code must retain the above copyright notice, 
#		this list of conditions and the following disclaimer.
#	*	Redistributions in binary form must reproduce the above copyright notice, 
#		this list of conditions and the following disclaimer in the documentation 
#		and/or other materials provided with the distribution.
#	*	Neither the name of View Software nor the names of its contributors may be 
#		used to endorse or promote products derived from this software without 
#		specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
# SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 
# WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Changelog: 
#	2004.05.21: File created.
#	2004.06.26: Added the ability to have a vertically oriented photo.
#	2005.03.19: Added navigation arrows overlaying the current photo.
#	2005.12.11: Added custom photo sizes.

use POSIX;
use strict;
use CGI qw(:standard);

my $photo_list = "photos.xml"; # This is the XML file with the dates & captions of the posted photos.

my $date = param("date");
my ($current_photo, $current_caption, $working_line, $arrow_left, $arrow_right, $photo_arrow_left, $photo_arrow_right, $orientation, $class, $width_working, $height_working);
my $dimensions;
my $dimensions_thumb;
my @archive;
my @line;
my %current_date;

my $found = 0;

my ($page_title, $sidebar, $file_line);
my $title_file = "../includes/title.shtml";
my $sidebar_file = "../includes/sidebar.shtml";

# Initialize.

$line[0] = '';
$line[1] = '';
$line[2] = '';

for (my $i = 0; $i < 6; $i++) {
    $archive[$i] = '';
}

$photo_arrow_left = $photo_arrow_right = $arrow_left = $arrow_right = '';

# Open the photo list and read the first 9 lines into an array.

open(LIST, "< $photo_list");
while($working_line = <LIST>) {
	if($working_line =~ /<date>(.*?)<\/date>/) {
		$line[0] = $line[1];
		$line[1] = $line[2];
		$line[2] = $working_line;
		
		if($date eq $1) { $found = 1; last; }
		}
	}
	
if($found and $working_line = <LIST>) { $line[3] = $working_line; }
else { $line[3] = ''; }

if($found and $working_line = <LIST>) { $line[4] = $working_line; }
else { $line[4] = ''; }

close LIST;

# If the date is not found, redirect to a 404 error page.

if(!$found) { print redirect("/404.html"); }

# Else the date was found.

if($line[0] eq '') { $archive[4] = "\t\t<div class=\"spacer\"></div>"; }
else { 
	if($line[0] =~ /<date>(.*?)<\/date><caption>.*?<\/caption><orientation>(.*?)<\/orientation><width>(.*?)<\/width><height>(.*?)<\/height>/) {
		if($2 eq 'horizontal') { 
			$height_working = ceil((66 * $4) / $3);
			$dimensions_thumb = "width=\"66\" height=\"" . $height_working . "\""; 
			}
		else { 
			$width_working = ceil((50 * $3) / $4);
			$dimensions_thumb = "width=\"" . $width_working . "\" height=\"50\" class=\"vert\""; 
			}
		$archive[4] = "\t\t<a href=\"../cgi-bin/frame.cgi?date=" . $1 . "\"><img src=\"../thumbnails/" . $1 . ".jpg\" $dimensions_thumb alt=\"\" /></a>";
		}
	}
	
if($line[1] eq '') { 
	$archive[3] = "\t\t<div class=\"spacer\"></div>"; 
	$arrow_right = "\t\t<div id=\"arrow-right-blank\"></div>";
	}
else { 
	if($line[1] =~ /<date>(.*?)<\/date><caption>.*?<\/caption><orientation>(.*?)<\/orientation><width>(.*?)<\/width><height>(.*?)<\/height>/) {
		if($2 eq 'horizontal') { 
			$height_working = ceil((66 * $4) / $3);
			$dimensions_thumb = "width=\"66\" height=\"" . $height_working . "\""; 
			}
		else { 
			$width_working = ceil((50 * $3) / $4);
			$dimensions_thumb = "width=\"" . $width_working . "\" height=\"50\" class=\"vert\""; 
			}
		$archive[3] = "\t\t<a href=\"../cgi-bin/frame.cgi?date=" . $1 . "\"><img src=\"../thumbnails/" . $1 . ".jpg\" $dimensions_thumb alt=\"\" /></a>";
		$arrow_right = "\t\t<a href=\"../cgi-bin/frame.cgi?date=" . $1 . "\" id=\"arrow-right\"></a>";
		$photo_arrow_right = "<a href=\"../cgi-bin/frame.cgi?date=" . $1 . "\" id=\"photo-arrow-right\"></a>";
		}
	}
	
if($line[2] =~ /<date>(.*?)<\/date><caption>(.*?)<\/caption><orientation>(.*?)<\/orientation><width>(.*?)<\/width><height>(.*?)<\/height>/) {
	$current_photo = $1;
	$current_caption = $2;
	$orientation = $3;
	$width_working = $4;
	$height_working = $5;
	
	$dimensions = "width=\"" . $width_working . "\" height=\"" . $height_working . "\"";

	if($orientation eq 'horizontal') { 
		$height_working = ceil((66 * $height_working) / $width_working);
		$dimensions_thumb = "width=\"66\" height=\"" . $height_working . "\"";
		$class = "selected";
		}	
	else { 
		$width_working = ceil((50 * $width_working) / $height_working);
		$dimensions_thumb = "width=\"" . $width_working . "\" height=\"50\"";
		$dimensions .= " class=\"vert\""; 
		$class = "vert selected";
		}
	
	$archive[2] = "\t\t<a href=\"../cgi-bin/frame.cgi?date=" . $1 . "\"><img src=\"../thumbnails/" . $1 . ".jpg\" $dimensions_thumb alt=\"\" class=\"$class\" /></a>";
	}
	
if($line[3] eq '' or $line[3] eq '</photo_list>') { 
	$archive[1] = "\t\t<div class=\"spacer\"></div>"; 
	$arrow_left = "\t\t<div id=\"arrow-left-blank\"></div>"; 
	}
else { 
	if($line[3] =~ /<date>(.*?)<\/date><caption>.*?<\/caption><orientation>(.*?)<\/orientation><width>(.*?)<\/width><height>(.*?)<\/height>/) {
		if($2 eq 'horizontal') { 
			$height_working = ceil((66 * $4) / $3);
			$dimensions_thumb = "width=\"66\" height=\"" . $height_working . "\""; 
			}
		else { 
			$width_working = ceil((50 * $3) / $4);
			$dimensions_thumb = "width=\"" . $width_working . "\" height=\"50\" class=\"vert\""; 
			}
		$archive[1] = "\t\t<a href=\"../cgi-bin/frame.cgi?date=" . $1 . "\"><img src=\"../thumbnails/" . $1 . ".jpg\" $dimensions_thumb alt=\"\" /></a>";
		$arrow_left = "\t\t<a href=\"../cgi-bin/frame.cgi?date=" . $1 . "\" id=\"arrow-left\"></a>";		
		$photo_arrow_left = "<a href=\"../cgi-bin/frame.cgi?date=" . $1 . "\" id=\"photo-arrow-left\"></a>";
		}
	}
	
if($line[4] eq '' or $line[4] eq '</photo_list>') { $archive[0] = "\t\t<div class=\"spacer\"></div>"; }
else { 
	if($line[4] =~ /<date>(.*?)<\/date><caption>.*?<\/caption><orientation>(.*?)<\/orientation><width>(.*?)<\/width><height>(.*?)<\/height>/) {
		if($2 eq 'horizontal') { 
			$height_working = ceil((66 * $4) / $3);
			$dimensions_thumb = "width=\"66\" height=\"" . $height_working . "\""; 
			}
		else { 
			$width_working = ceil((50 * $3) / $4);
			$dimensions_thumb = "width=\"" . $width_working . "\" height=\"50\" class=\"vert\""; 
			}
		$archive[0] = "\t\t<a href=\"../cgi-bin/frame.cgi?date=" . $1 . "\"><img src=\"../thumbnails/" . $1 . ".jpg\" $dimensions_thumb alt=\"\" /></a>";
		}
	}
	
# Filter out the date and time.

if($current_photo =~ /(\d\d\d\d)_(\d\d)_(\d\d)_(\d\d)_(\d\d)_(\d\d)/) {
	$current_date{'year'} = 	$1;
	$current_date{'month'} = 	$2;
	$current_date{'day'} = 		$3;
	$current_date{'hour'} = 	$4;
	$current_date{'minute'} = 	$5;
	$current_date{'second'} = 	$6;
	
	$current_date{'serial'} = $1 . $2 . $3 . $4 . $5 . $6;
	}

# Get the contents of the title and sidebar fragments.

open TITLE, $title_file or die "Can't open $title_file for reading: $!";
while ($file_line = <TITLE>) { $page_title .= $file_line; }
close TITLE;

open SIDEBAR, $sidebar_file or die "Can't open $sidebar_file for reading: $!";
while ($file_line = <SIDEBAR>) { $sidebar .= $file_line; }
close SIDEBAR;

# Print the page.

#print STDERR "$arrow_left";
#print STDERR "$archive[0]";
#print STDERR "$archive[1]";
#print STDERR "$archive[2]";
#print STDERR "$archive[3]";
#print STDERR "$archive[4]";
#print STDERR "$arrow_right";
#print STDERR "$page_title";
#print STDERR "$page_title";


print <<END_OF_HTML
Content-type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd  ">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head>
	<title>$page_title :: Previous Photo :: $current_date{'serial'}</title>

	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="author" content="" />
	<meta name="keywords" content="" />
	<meta name="description" content="" />
	<meta name="robots" content="index" />
		
	<link rel="stylesheet" type="text/css" href="../includes/styles.css" />
	
</head>
<body>

<div id="content">
<div id="sidebar">

$sidebar

</div>

<div id="main">

	<div id="photo">
		$photo_arrow_left
		$photo_arrow_right
		<img src="../photos/$current_photo.jpg" $dimensions alt="" />
	</div>
		
	<div id="caption">
		<div id="caption-title">$current_date{'serial'} <a href="../cgi-bin/frame.cgi?date=$current_photo">Permanent Link</a></div>
		$current_caption
	</div>

	<div id="previous-photos">
$arrow_left
$archive[0]
$archive[1]
$archive[2]
$archive[3]
$archive[4]
$arrow_right
	</div>
	
</div>
</div>

</body>
</html>

END_OF_HTML
;
