#!/usr/local/bin/perl

# Slide Show
# Version 1.0
# auto_slide_show.pl
# By Mike Calabrese
# http://www.mikecalabrese.com
# 11-01-2003

# This program is available for free on an AS-IS basis.  I am not
# responsible for consequential damages, loss of income or whatever
# else that may happen as a result of the use of this program.

# Receive & Parse out the data
$theinput = <>; # Read the data into an array
chomp $theinput; # Chomp the return character
my @value_pairs = split (/&/,$theinput);
my %formdata = ();
foreach $pair (@value_pairs) {
($field, $value) = split (/=/,$pair);
$value =~ tr/+/ /;
$value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
$formdata{$field} = $value;
}

# Assign variable names to the input

# HTML Output
print "Content-type: text/html\n\n";
print <<"HTMLDOC";
<HTML><HEAD>
<META HTTP-EQUIV="refresh"
CONTENT="5;URL=http://www.yourdomain.com/cgi-bin/slide_show/auto_slide_show.pl">
<TITLE>Auto Slide Show Viewer
</TITLE></HEAD>
<BODY BGCOLOR="#ffffff" LINK="#008000" ALINK="#000080" VLINK="#400000">
<CENTER>
<P>
<H3>Auto Slide Show Viewer</H3>
<P>
HTMLDOC

# Connect to the Database
use DBI;
my ($dsn) = "DBI:mysql:yourdatabasename:localhost";
my ($user_name) = "yourlogin";
my ($password) = "yourpassword";
my ($dbh, $sth);
my (@ary);
$dbh = DBI->connect ($dsn, $user_name, $password, {RaiseError => 1});

# Issue the select statement that retrieves the max number of records
$sth = $dbh->prepare ("SELECT count(*) from slide_show");
$sth->execute ();
$max_reference_number = $sth->fetchrow_array ();

# Generate a random number between 1 and the max reference number.
$rand_ref_num = int(rand($max_reference_number)) + 1;

# Issue the select statement that gets the image information
$sth = $dbh->prepare ("select image_url, "
   . "description from slide_show where reference_number = '$rand_ref_num'");
$sth->execute ();

# Build the image info from the array @ary
while (@ary = $sth->fetchrow_array ())
{
$image_url = $ary[0];
$description = $ary[1];
}

print "$image_url<BR>\n";
print "<I><SMALL>$rand_ref_num of $max_reference_number\n";
print "<BR>$description</SMALL></I><P>\n";

# Disconnect from the database
$sth->finish ();
$dbh->disconnect ();

print <<"HTMLDOC";
</TD><TD VALIGN="Top">
HTMLDOC
if ($reference_number eq "$max_reference_number") {
$reference_number = 0;
}
print <<"HTMLDOC";
<B>This page will refresh every 5 seconds and show a new image.</B>
<P>
<FORM METHOD="POST" ACTION="manual_slide_show.pl">
<INPUT TYPE="Submit" VALUE="Manual Slide Show Viewer">
</FORM>
</CENTER>
</BODY></HTML>
HTMLDOC