#!/usr/bin/perl
use DBI;
require 'cgi-lib.pl';
$dbh=DBI->connect("DBI:mysql:ukshopsearch", "admin", "purple");
&ReadParse;
require 'menu.pl';
require 'adserver.pl';
$menu = &PrintMenu;
$ad = &PrintAd;
if ($in{display} eq 'thread'){
$sql = "select count(*) from gossip_messages where type='child' and message_status in ('website','list') and parent_id=$in{id} order by date_posted desc";
#$sql = "select ID,subject,date_format(date_posted,'%a %D %b %Y'),from_address from gossip_messages where type='parent' and message_status in ('website','list') order by date_posted desc";
$sth=$dbh->prepare($sql);
$sth->execute();
$children=$sth->fetchrow_array;
$sth->finish;
# If thread has children, then diplay them
if ($children > 0){
&PrintThread;
}
# Otherwise show the message
else {
&PrintMessage;
}
}
elsif ($in{display} eq 'message'){
&PrintMessage;
}
elsif ($in{display} eq 'new'){
&PrintNew;
}
elsif ($in{display} eq 'shop'){
&PrintShop;
}
elsif ($in{display} eq 'post'){
if ($in{email} eq ''){
$errors .= 'Please enter your email address (anonymous posting is not allowed)
';
}
if (!($in{email} =~ /[^@]+@[^\.]+\.[^\.]+/)){
$errors .= 'Please enter a valid email address (anonymous posting is not allowed)
';
}
if ($in{name} eq ''){
$errors .= 'Please enter your name (anonymous posting is not allowed)
';
}
if ($in{subject} eq ''){
$errors .= 'Please enter a subject line
';
}
if ($in{body} eq ''){
$errors .= 'Please enter a message
';
}
if (defined $errors){
&PrintErrors($errors);
}
else {
&PostMessage;
}
}
else {
print "Location:/gossip/index.html\n\n";
}
$dbh->disconnect;
exit 0;
# Subs only beneath this point
##############################
# Print Thread
###############
sub PrintThread{
print "Content-type:text/html\n\n";
print <
UKShopSearch.com - the directory for UK based online shops and uk friendly global stores
Original Message(first of thread)
HTML
# Get the parent
$sql = "select id,subject,date_format(date_posted,'%a %D %b %Y'),from_address from gossip_messages where id=$in{id}";
$sth=$dbh->prepare($sql);
$sth->execute();
@row=$sth->fetchrow_array;
print "$row[1] posted by $row[3] on $row[2] \n";
$sth->finish;
print "Responses(newest message first) ";
# Get the children
$sql = "select id,subject,date_format(date_posted,'%a %D %b %Y'),from_address from gossip_messages where type='child' and message_status in ('website','list') and parent_id=$in{id} order by date_posted desc";
$sth=$dbh->prepare($sql);
$sth->execute();
while(@row=$sth->fetchrow_array){
print "$row[1] posted by $row[3] on $row[2] \n";
}
$sth->finish;
print <
HTML
}
# Print Shop
###############
sub PrintShop{
my $results_per_page = 20;
my $start = $in{start} =~ /^\d+$/ ? $in{start} : 0;
my $total = 0;
my $shop,$url,$affiliate_code,$link_code;
$sql = "select title,url,affiliate_code from shops where id=$in{shop_id}";
$sth=$dbh->prepare($sql);
$sth->execute();
($shop,$url,$affiliate_code) = $sth->fetchrow_array;
$sth->finish;
if ($affiliate_code){
$link_code = $affiliate_code;
}
else{
$link_code = "$shop";
}
print "Content-type:text/html\n\n";
print <
UKShopSearch.com - the directory for UK based online shops and uk friendly global stores
The following thread(s) contain one or more messages related to $link_code
HTML
# Get the parent ids
$sql = "select distinct if(type='parent',id,parent_id) from gossip_messages where shop_id=$in{shop_id} and message_status in ('website','list')";
$sth=$dbh->prepare($sql);
$sth->execute();
my $in;
while (my $id=$sth->fetchrow_array){
$in .= $id . ',';
$total ++;
}
$sth->finish;
$in =~ s/,$//;
$sql = "select ID,subject,date_format(date_posted,'%a %D %b %Y'),from_address from gossip_messages where id in ($in) order by date_posted desc limit $start,$results_per_page";
$sth=$dbh->prepare($sql);
$sth->execute();
while(@row=$sth->fetchrow_array){
print "$row[1] posted by $row[3] on $row[2] \n";
}
$sth->finish;
if ($start > 0){
$previous_start = $start - $results_per_page;
$previous = "<< previous $results_per_page threads";
}
else {
$previous = "<< previous $results_per_page threads";
}
if ($total > $start + $results_per_page){
$next_start = $start + $results_per_page;
$next = "next $results_per_page threads >>";
}
else {
$next = "next $results_per_page threads >>";
}
print <
$previous | $next
HTML
}
# Print Message
###############
sub PrintMessage{
$sql = "select id,subject,date_format(date_posted,'%a %D %b %Y'),from_address, from_field, message_body, type, parent_id, date_posted, shop_id from gossip_messages where id=$in{id}";
$sth=$dbh->prepare($sql);
$sth->execute();
my ($id,$subject,$date_posted,$from_address,$from_field,$message_body,$type,$parent_id,$timestamp,$shop_id)=$sth->fetchrow_array;
$sth->finish;
$subject =~ s/</g;
$subject =~ s/>/>/g;
$from_field =~ s/</g;
$from_field =~ s/>/>/g;
$message_body =~ s/</g;
$message_body =~ s/>/>/g;
my $parent_timestamp;
if ($type eq 'parent'){
$parent_id = $id;
$parent_timestamp = $timestamp;
}
else {
$sql = "select date_posted from gossip_messages where id=$parent_id";
$sth=$dbh->prepare($sql);
$sth->execute();
$parent_timestamp = $sth->fetchrow_array;
$sth->finish;
}
# Is there a previous thread?
$sql = "select id from gossip_messages where type='parent' and message_status in ('website','list') and date_posted < $parent_timestamp order by date_posted desc limit 0,1";
$sth=$dbh->prepare($sql);
$sth->execute();
my $back_thread = $sth->fetchrow_array;
$sth->finish;
if ($back_thread){
$back = "back";
}
else {
$back = 'back';
}
# Is there a next thread?
$sql = "select id from gossip_messages where type='parent' and message_status in ('website','list') and date_posted > $parent_timestamp order by date_posted limit 0,1";
$sth=$dbh->prepare($sql);
$sth->execute();
my $next_thread = $sth->fetchrow_array;
$sth->finish;
if ($next_thread){
$next = "next";
}
else {
$next = 'next';
}
# Is there a previous message?
$sql = "select id from gossip_messages where ((id=$parent_id and type='parent') or (parent_id=$parent_id and type='child')) and message_status in ('website','list') and date_posted < $timestamp order by date_posted desc limit 0,1";
$sth=$dbh->prepare($sql);
$sth->execute();
my $back_message = $sth->fetchrow_array;
$sth->finish;
if ($back_message){
$msg_back = "back";
}
else {
$msg_back = 'back';
}
# Is there a next message?
$sql = "select id from gossip_messages where ((id=$parent_id and type='parent') or (parent_id=$parent_id and type='child')) and message_status in ('website','list') and date_posted > $timestamp order by date_posted limit 0,1";
$sth=$dbh->prepare($sql);
$sth->execute();
my $next_message = $sth->fetchrow_array;
$sth->finish;
if ($next_message){
$msg_next = "next";
}
else {
$msg_next = 'next';
}
# Is this message about a particular shop, and if so are there any other messages about the shop in question?
if ($shop_id != 0){
$sql = "select count(*) from gossip_messages where message_status in ('website','list') and shop_id = $shop_id";
$sth=$dbh->prepare($sql);
$sth->execute();
my $shop_messages = $sth->fetchrow_array;
$sth->finish;
if ($shop_messages > 1){
$more_on_this_shop = "more on this shop";
}
else {
$more_on_this_shop = 'more on this shop';
}
}
else {
$more_on_this_shop = 'more on this shop';
}
print "Content-type:text/html\n\n";
print <
UKShopSearch.com - the directory for UK based online shops and uk friendly global stores
HTML
}
# Print Form for New Message
############################
sub PrintNew{
my ($id,$subject,$message_body,$type,$parent_id,$thread,$shop_id,$title_snippet);
if ($in{id} != 0){
$sql = "select id,subject,message_body,type,parent_id,shop_id from gossip_messages where id=$in{id}";
$sth=$dbh->prepare($sql);
$sth->execute();
($id,$subject,$message_body,$type,$parent_id,$shop_id)=$sth->fetchrow_array;
$sth->finish;
$subject =~ s/</g;
$subject =~ s/>/>/g;
if (!($subject =~ /^RE:/i)){
$subject = 'Re: ' . $subject;
}
$subject_element = "$subject";
$subject_hidden = "";
$message_body = '> ' . $message_body;
$message_body =~ s/\n/\n> /g;
$message_body =~ s/> $//g;
$message_body =~ s/</g;
$message_body =~ s/>/>/g;
$message_body = "\n\n\n" . $message_body;
if ($type eq 'parent'){
$thread = $id;
}
elsif ($type eq 'child'){
$thread = $parent_id;
}
}
else {
$subject_element = '';
$thread = 0;
$shop_id = $in{shop};
if ($shop_id){
$sql = "select title from shops where id=$shop_id";
$sth=$dbh->prepare($sql);
$sth->execute();
my $title=$sth->fetchrow_array;
$sth->finish;
$title_snippet=<
Shop
$title
HTML
}
}
print "Content-type:text/html\n\n";
print <
UKShopSearch.com - the directory for UK based online shops and uk friendly global stores
HTML
}
# Print Errors
##############
sub PrintErrors{
my $errors = $_[0];
print "Content-type:text/html\n\n";
print <
UKShopSearch.com - the directory for UK based online shops and uk friendly global stores
$menu
ShopGossip Forum - Message Error!
There were problems with your message.
Please use your browser's back button to return to the form, and follow the instructions below:
$errors
HTML
}
# Post Message
##############
sub PostMessage{
$in{email} =~ s/'/\\'/g;
$in{name} =~ s/'/\\'/g;
$in{subject} =~ s/'/\\'/g;
$in{shop_id} =~ s/'/\\'/g;
$in{body} =~ s/'/\\'/g;
$in{body} =~ s/>/>/g;
$in{body} =~ s/</";
if ($in{thread} == 0){
$type = 'parent';
$parent_id = 0;
}
else {
$type = 'child';
$parent_id = $in{thread};
}
$sql = "insert into gossip_messages set
type = '$type',
parent_id = $parent_id,
message_status = 'posted',
message_body = '$in{body}',
date_posted=NULL,
from_field = '$from_field',
from_address = '$in{email}',
subject = '$in{subject}',
shop_id = $in{shop_id},
sender_status = 'website',
ip = '$ENV{REMOTE_ADDR}'";
$sth=$dbh->prepare($sql);
$sth->execute();
$sth->finish;
if ($in{subscribe} eq 'yes'){
$sql = "select count(*) from gossip_email where email='$in{email}'";
$sth=$dbh->prepare($sql);
$sth->execute();
$subscribed = $sth->fetchrow_array;
$sth->finish;
if ($subscribed){
# If they are subscribed, then activate them
$sql = "update gossip_email set active=1 where email='$in{email}'";
$sth=$dbh->prepare($sql);
$sth->execute();
$sth->finish;
}
else {
# add this email address
$sql = "insert into gossip_email set email='$in{email}', active=1, mode=0";
$sth=$dbh->prepare($sql);
$sth->execute();
$sth->finish;
}
}
print "Content-type:text/html\n\n";
print <
UKShopSearch.com - the directory for UK based online shops and uk friendly global stores