#!/usr/bin/perl
use strict;
use CGI;
print "Content-type: TEXT/HTML\n\n";

open(INPUT_FILE, "photos.fet");
my ($target, $line, $image, $title, $blurb, $done, $newest, $commentfile, $commentdate, $commentname, $commenttext, $addcomment, $commentip, $commenthost);

my @params = split(/&/, $ENV{'QUERY_STRING'});
$target = $params[0];
$addcomment = $params[1];
$done = 0;
$line = "";

while(!($done == 1) && !($line eq "end"))
{
	do
	{
		$line = <INPUT_FILE>;
		chomp($line);
	}while(!($line));
	if($newest == undef)
	{
		$newest = $line;
	}
	if($target == $line || $target == "")
	{
		$target = $line;
		while(!($done == 1))
		{
			$image = <INPUT_FILE>;
			chomp($image);

			$title = <INPUT_FILE>;
			chomp($title);
			$blurb = <INPUT_FILE>;
			chomp($blurb);
			$done = 1;
		}
	}
	else
	{
		do
		{
		$line = <INPUT_FILE>;
		chomp($line);
		}while($line);
	}
}
close(INPUT_FILE);


$commentfile = 'itsstupid/' . $target . '.fet';

if (!(-e $commentfile))
{
	open (CREATE_COMMENT_FILE, '>' . $commentfile) || die "cannot create comment file: $!";
	close CREATE_COMMENT_FILE;
} 

open (COMMENT_FILE, '<' . $commentfile) || die "cannot open comment file: $!";
my @comments = <COMMENT_FILE>;
my @commentbits;
my @checklastpost = split(/\|/, @comments[$#comments]);
close COMMENT_FILE;

if ($addcomment == 1)
{
	my ($data, $writedate, $writename, $writetext, $writeip, $writehost, $blah);
	my $length = $ENV{'CONTENT_LENGTH'};
	read(STDIN, $data, $length);

	my ($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOfYear, $IsDST) = localtime(time + 3600);
	my $RealYear = $Year + 1900;
	my $RealMonth = $Month + 1;
	if ($Minute < 10)
	{
		$Minute = '0' . $Minute;
	}
	my $writedate = $RealYear . '.' . $RealMonth . '.' . $Day . ' at ' . $Hour . ':' . $Minute . ' EST';

	my @pairs = split(/&/, $data);
	($blah, $writename) = split(/=/, $pairs[0]);
	($blah, $writetext) = split(/=/, $pairs[1]);

	$writename =~ tr/+/ /;
	$writetext =~ tr/+/ /;

##        $writename =~ s/%([0-9a-fA-F]{2,2})/(26=>'&amp;',3C=>'&lt;',3E=>'&gt;'){uc($1)}/eg
	$writename =~ s'%26'&amp;'g;
	$writename =~ s'%3C'&lt;'g;
	$writename =~ s'%3E'&gt;'g;
	$writetext =~ s'%26'&amp;'g;
	$writetext =~ s'%3C'&lt;'g;
	$writetext =~ s'%3E'&gt;'g;
	$writename =~ s'%0D%0A'<br>'g;
	$writetext =~ s'%0D%0A'<br>'g;
	$writename =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
	$writetext =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;

	$writeip = $ENV{'REMOTE_ADDR'};
	$writehost = $ENV{'REMOTE_HOST'};

	if ($writename eq '')
	{
		$writename = "anonymous";
	}

	if (($writetext ne $checklastpost[2]) && ($writetext ne ''))
	{

		open (WRITE_COMMENT_FILE, '>>' . $commentfile);
		flock(WRITE_COMMENT_FILE, 2) or die "cannot lock file exclusively: $!";
		my $linetowrite = $writedate . '|' . $writename . '|' . $writetext . '|' . $writeip . '|' . $writehost . "\n";
		print WRITE_COMMENT_FILE $linetowrite;
		push(@comments, $linetowrite);
		close WRITE_COMMENT_FILE;
	}
}

print <<"EOT";
<html>
<!--hand coded by william van hecke in apple textedit and hacked by LiK :) -->
<head>
<title>LiK's Photo Thingy</title>
<meta name="description" content="LiK's random photos">
<meta name="keywords" content="photos, photolog, blog, weblog, journal, log, LiK, Lik Way Chan, thingy">
</head>
<body bgcolor="#999999" text="#000000" link="#CCCCCC" vlink="#CCCCCC" alink="#CCCCCC">
<div align="center">

<td align="center">
		<a href="info.htm"><img src="title.gif" border="0"></a>
<br><font size="2">last updated February 17, 2003</font></br>

<!--start big table>
<table border="0" cellpadding="5" cellspacing="0" width="750">
	<tr><td align="center">
		<table bgcolor="#999999" border="0" cellpadding="10" cellspacing="1" width="100%">
			<tr><td bgcolor="#666666" align="center">
EOT

if($done == 1)
{
	print '<img src="photos/' . $image . '">
		</table>
	<tr><td align="center">
		<table bgcolor="#999999" border="0" cellpadding="10" cellspacing="1" width="100%">
			<tr><td bgcolor="#666666">	
				<table border="0" cellpadding="5" cellspacing="0" width="100%">
					<tr><td align="center" colspan="2">
						<p><b>', $title, '</b>
						<p>' . $blurb . '
						<br>
					<tr><td align="left">
';
	if(!($target == 1))
	{
		print '<a href="index.pl?1">first photo</a>&nbsp;&nbsp;<a href="index.pl?' . ($target-1) . '">previous photo</a>';
	}
print '
					<td align="right">';
	if (!($target == $newest))
	{
		print '<a href="index.pl?' . ($target+1) . '">next photo</a>&nbsp;&nbsp;<a href="index.pl">last photo</a>';
	}
}
else
{
	print $target . ": no such record.\n";
}


print '
				</table>
		</table>
	<tr><td align="center">
		<table bgcolor="#999999" border="0" cellpadding="10" cellspacing="1" width="100%">
			<tr><td bgcolor="#666666" align="center">
			<p><b>comments on this photo</b>
';


if (@comments)
{
	foreach my $commentline (@comments)
	{
		($commentdate, $commentname, $commenttext, $commentip, $commenthost) = split(/\|/, $commentline);
		print '<p><b>' . $commentdate . ' by ' . $commentname . '</b><br>';
		print $commenttext . '</p>';
	}
}
else
{
print '<p>no comments yet';
}

print <<"EOT";
		</table>
	<tr><td align="center">
		<table bgcolor="#999999" border="0" cellpadding="10" cellspacing="1" width="100%">
			<tr><td bgcolor="#666666" align="center">
			<p><b>add a comment</b>
			<p>
			<form method="post" action="index.pl?$target&1">
			name:<br><input type="text" name="name" size="20"><br><br>
			comment:<br><textarea name="comment" cols="40" rows="8"></textarea><br>
			<input type="submit">
			</form>
		</table>
</table>
<!--end big table>

<font size="-1">LiKs Photo Thingy &copy;2002 <a href="http://www.liksketchpad.com">Lik Way Chan</a></font>
<br><font size="-1">Original code by <a href="http://context.metalbat.com">Jetfuel</a> and edited by <a href="http://www.kuiki.net">hachi</a></font>
</div>
</body>

</html>
EOT
