#!/usr/bin/perl
use strict;
use CGI;
use CGI::Carp;
use Pg;
my $db = 'cattleweb';
my $bgcolor = '#ffffff';
my $defbanner = "http://www.cattleweb.net/images/cattlewebtitle.gif";
my $expire = "28"; # Number of days before an ad expires
my $q = new CGI;
my $conn;
print $q->header('text/html');
if( ($q->path_info() eq '/verify') || ($q->path_info() eq 'verify') )
{
&Verify;
} elsif( ($q->path_info() eq '/submit') || ($q->path_info() eq 'submit') )
{
&Finish;
} else {
&ShowForm;
}
############################################################################
#----------------------------------------------------------------
# ShowForm
#
# Just show the form
#----------------------------------------------------------------
sub ShowForm
{
my $script = $q->script_name();
print $q->start_html(-title=>'Classified Advertisement Form',
-BGCOLOR=>$bgcolor);
my $id = 0;
my $query;
my $banner;
if(length($ENV{'QUERY_STRING'}) > 0)
{
$id = $ENV{'QUERY_STRING'};
$conn = Pg::new('','','','',$db);
$query = "SELECT bannerurl FROM classified_owners WHERE id=$id";
my $r = $conn->exec($query);
$banner = $r->getvalue(0,0);
}
if(length($banner) > 0)
{
print "
\n"
} else {
print "
\n"
}
print <
Category:
Company:
Name:
Phone:
City:
State:
Country:
Email:
URL:
Advertisement:
END
if($id > 0)
{
print "\n";
}
print "";
print $q->end_html();
}
#------------------------------------------------------------------
# Verify
#
# Verify data and show it to the user
#------------------------------------------------------------------
sub Verify
{
print $q->start_html(-title=>'Classified Advertisement Form',
-BGCOLOR=>$bgcolor);
my %in = ();
for ($q->param())
{
$in{$_} = $q->escapeHTML($q->param($_));
}
my $banner;
my $pid;
if($q->param('pid'))
{
$pid = $q->param('pid');
$conn = Pg::new('','','','',$db);
my $query = "SELECT bannerurl FROM classified_owners WHERE id=$pid";
my $r = $conn->exec($query);
$banner = $r->getvalue(0,0);
}
if(length($banner) > 0)
{
print "
\n"
} else {
print "
\n"
}
my $script = $q->script_name();
print <
Category:
$in{'category'}
Company:
$in{'company'}
Name:
$in{'name'}
Phone:
$in{'phone'}
City:
$in{'city'}
State:
$in{'state'}
Country:
$in{'country'}
Email:
$in{'email'}
URL:
$in{'url'}
Advertisement:
$in{'text'}
END
for ($q->param())
{
print "\n";
}
print "\n";
print "
\n";
print $q->end_html();
}
sub Finish
{
print $q->start_html(-title=>'Database Submission Complete',
-BGCOLOR=>$bgcolor);
$conn = Pg::new('','','','',$db);
my %in = ();
for ($q->param())
{
$in{$_} = $q->param($_);
$in{$_} =~ s/'/\\'/g;
$in{$_} =~ s/^/'/g;
$in{$_} =~ s/$/'/g;
}
$conn->exec("BEGIN");
# create the classified
my $query = "INSERT INTO classified (category, company, name, phone, ".
" city, state, country, email, url, text, expire) ".
" VALUES ( ".
" $in{'category'}, $in{'company'}, $in{'name'}, ".
" $in{'phone'}, $in{'city'}, $in{'state'}, ".
" $in{'country'}, $in{'email'}, $in{'url'}, ".
" $in{'text'}, 'now'::date+$expire) ";
my $r = $conn->exec($query);
# get id number of the ad
$query = "SELECT currval('classifiedid')";
$r = $conn->exec($query);
my $adid = $r->getvalue(0,0);
# make an entry in the ad owners table to link it up to an owner
if($q->param('pid'))
{
$query = "INSERT INTO ad_owners (adid, ownerid) VALUES ($adid, ".
$q->param('pid') . ")";
$conn->exec($query);
}
$conn->exec("END");
my $pid;
my $banner;
if($q->param('pid'))
{
$pid = $q->param('pid');
$query = "SELECT bannerurl from classified_owners WHERE id=$pid";
$r = $conn->exec($query);
$banner = $r->getvalue(0,0);
}
print "