#!/usr/bin/perl
###############################################################################
#
# This script allows to EDIT any information contained in the Database
# provided that the user has a login and password assigned.
#
# CGI Script by Piotr Pytlik -> http://www.doublep.com/pytlik/piotr.html
# Copyright (c) 1997, Double P Software -> http://www.doublep.com
# All Rights Reserved
# Permission is granted to Letterhead's to use this script provided that this
# copyright information remains unchanged.
#
# HISTORY:
#  5-Jan-96	PP	Created this script in original form
#
###############################################################################

local $SIG{__DIE__}  = sub { my $msg = shift; print "<H2>ERROR: $msg</H2>\n"; };
#local $SIG{__WARN__} = sub { my $msg = shift; print "<H2>WARNING: $msg</H2>\n"; };

require "cgi/formsign.cgi";	# Must be included before cgi/secure.cgi !!!

require "cgi/secure.cgi";	# Provides Login/Password protection
$secureLoginLabel = 'ShopID';
#$secureApp = "/home/ppytlik/public_html/doublep/sentex/pipe";
$secureFunc = \&ValidatePassword;
&ValidateAccess;

my %superusers = ( 1 => 'Steve', 1552 => 'Piotr' );
my $EditShop = $secureLogin;

require "cgi/counter.cgi";
($countAll, $countUniq, $exclAll, $exclUniq) = Count();
$ImgBack = "images/logos/head2.gif";

$disable = <<EOF;
<HR SIZE=3 WIDTH=90%>
<center>
<TABLE BORDER=2 CELLPADDING=4 BGCOLOR=#aa0000><TR><TD><FONT COLOR=#ffff66>
<H2>Sorry but our database has reached its limits with more than 10000 members</H2>
We are working on it right now and it may take a little while. Please bare with us for few days and come back here to try again...
</FONT></TD></TR></TABLE>
</center>
EOF

undef $disable;		# Comment this one out to enable this script

###############################################################################
# This is a form for Adding New Entries to the Database:

print <<EOF;
Content-type: text/html

<HTML><HEAD><TITLE>Edit Your Entry in The Letterville Database</TITLE></HEAD>
<BODY BACKGROUND="$ImgBack" BGCOLOR="#FFFFFF" TEXT="#016644">
<CENTER>
<CENTER>
<p align="center">
		<a href="http://www.letterville.com/residents">
		<img border="0" src="../meets/support.gif" width="340" height="50" align="center"></a></p>
		</CENTER>
		<CENTER>
EOF

if( $superusers{$secureLogin} )
{
	print "<H2>Hi $superusers{$secureLogin}.\n";
	if( $ARGV[0] )
	{
		print " You are editing Shop $ARGV[0]</H2>\n";
		$EditShop = $ARGV[0];
	}
	else
	{
		print " You can edit any Shop you need to:</H2>\n";
		print "<H3>Just specify the Shop ID like in <U>edit.cgi?1234</U> which will edit Shop 1234</H3>\n";
	}
}

if( OpenDb() )
{
	GetInd( 'ID' );	# Main mapping hash as %map
	if( exists $map{$EditShop} )
	{
		$db->GoTo( $map{$EditShop} );

		#print "{ID}=$in{ID}, {Restore}=$in{Restore}, {_ID}=$in{_ID}, login=$secureLogin<BR>\n";
		if( $in{ID} eq 'Restore' && $in{Restore} && $in{_ID} && $in{rec} && $superusers{$secureLogin} )
		{
			Log( "RESTORED Shop $in{_ID} at Record ${rec} by $superusers{$secureLogin}" );
			$db->RestoreRec( $in{rec} );
			print <<EOF;
<HR WIDTH=80% SIZE=3>
<H2>You have just <BLINK>Restored Shop $in{_ID}</BLINK> in the database!<BR>
You can now modify it further and Submit the changes.</H2>
EOF
		}
		if( $in{ID} eq 'Delete' && $in{Delete} && $in{_ID} && $superusers{$secureLogin} )
		{
			$db->DeleteRec( $map{$in{_ID}} );
			Log( "DELETED Shop $in{_ID} at Record $map{$in{_ID}} by $superusers{$secureLogin}" );
			print <<EOF;
<HR WIDTH=80% SIZE=3>
<FORM ACTION=$ENV{SCRIPT_NAME}?$in{_ID} METHOD=post>
<INPUT TYPE=hidden NAME="ID" VALUE=Restore>
<INPUT TYPE=hidden NAME=_ID VALUE="$in{_ID}">
<INPUT TYPE=hidden NAME=rec VALUE="$map{$in{_ID}}">
<H2>You have just <BLINK>Deleted Shop $in{_ID}</BLINK> from the database!<BR>
If that was a mistake, you can <INPUT TYPE=submit NAME=Restore VALUE="Restore Shop $in{_ID}"> now.</H2>
<H3>Make sure that when you're done with changes, you <A HREF="ind.cgi">Reindex</A> the database</H3>
</FORM>
EOF
		}

		foreach( qw( ID Passwd Joined Updated FName LName SName Born Company Email URL RemoteIP Agent
					WAddress WCity WState WZip WCountry WPhone FPhone
					HAddress HCity HState HZip HPhone
					Magz Platform Software Printer Plotter Info History ) )
		{
			$in{"_$_"} = $db->GetField( $_ );
		}

		$in{_URL} = "http://$in{_URL}" unless $in{_URL} =~ /^http:/;
		$in{BornYY} = substr( $in{_Born}, 0, 4 );
		$in{BornMM} = substr( $in{_Born}, 4, 2 );
		$in{BornDD} = substr( $in{_Born}, 6, 2 );

		if( $disable )
		{
			print $disable;
		}
		else
		{
			DispForm( 'Edit' );
		}
	}
	else
	{
	print <<EOF;
<H1>No entry in our database for Shop ID $EditShop.<BR>
Please try again or contact <A HREF="mailto:steve\@letterhead.com">Steve & Barb</A></H1>
EOF
	}
	CloseDb();
}
else
{
	print <<EOF;
<H1>Sorry but it seems we have a problem with our database at the moment.<BR>
Please try again later or contact <A HREF="steve\@letterhead.com">Steve & Barb</A></H1>
EOF
}

###############################################################################

print <<EOF;
$secureLogoutButton

<H4>[
	<A HREF="../">Letterheads</A> |
	<A HREF="../ubb">Bulletin Board</A> |
	<A HREF="events.html">Events</A> |
	<A HREF="basics/ads.html">Advertising</A> |
	<A HREF="suppliers">Suppliers</A>
]</H4>
<P>

<h4><A HREF="mailto:steve\@letterhead.com">E-Mail Steve & Barb!</A></H4>

<h4>Letterville<BR>
144 Hill St., E.,<BR>
Fergus, Ontario, Canada N1M 1G9<BR>
Phone.....1-519-787-7446</H4>


</BODY>
</HTML>
EOF

###############################################################################

sub ValidatePassword
{
	my( $login, $passwd ) = @_;

	if( OpenDb() )
	{
		GetInd( 'ID' );	# Main mapping hash as %map
		if( exists $map{$login} )
		{
			$db->GoTo( $map{$login} );
			my( $id, $pas ) = $db->GetFields( 'ID', 'Passwd' );
			if( $id eq $login && $pas eq $passwd )
			{
				return 1;	# Validated OK.
			}
			else
			{
				return 0;	# Invalid password or messed up $map table
			}
		}
		else
		{
			return 0;	# No such Shop ID - invalid login
		}
		CloseDb();
	}
	else
	{
		return undef;	# Couldn't validate
	}
}

###############################################################################
