#!/usr/bin/perl

$top = '..';

require "$top/site.pl";
require 'cgi-util.pl';
require 'db.pl';
use Mail::Sendmail;

my $siteEmail = "Minnesota Club DSM <$email>";

#%form = CGIReadGet();
$q = $ENV{'QUERY_STRING'};

$noMake = 12;
$defaultState = 24;

sub DefineIfPresent {
	my ($v) = @_;
	my $r = 'NULL';
	$r = $dbh->quote($v) if($v ne '');
	return $r;
}

sub DefineTextBlock {
	my ($v) = @_;

	$v =~ s/<br>/\n/g;
	$v =~ s/&/&amp;/g;
	$v =~ s/>/&gt;/g;
	$v =~ s/</&lt;/g;
	$v =~ s/[\n\r]+/<br>/g;
	$v = $dbh->quote($v);

	return $v;
}

sub GetDefaults {
	my ($username) = @_;

	my %d;

	my $query = <<"EOF";
SELECT
	*
FROM
	users,
	user_info
WHERE
	users.user_id=user_info.user_id AND
	users.user_name='$username'
EOF
	my $sth = $dbh->prepare($query);
	$sth->execute;

	return %d if($sth->rows < 1);
	my $ref = $sth->fetchrow_hashref();
	foreach $key (keys %$ref) {
		$data{$key} = $ref->{$key};
	}
	$sth->finish;


	$d{'first'} = $data{'first_name'};
	$d{'last'} = $data{'last_name'};
	$d{'city'} = $data{'city'};
	$d{'state'} = $data{'state_id'};
	$d{'email'} = $data{'email'};
	$d{'hide_email'} = !$data{'disclose_email'};
	$d{'url'} = $data{'url'};
	$d{'aim'} = $data{'aim'};
	$d{'icq'} = $data{'icq'};
	$d{'irc'} = $data{'irc'};
	$d{'yahoo'} = $data{'yahoo'};

	$query = "SELECT * FROM vehicle WHERE user_id=$data{'user_id'}";
	my $sth = $dbh->prepare($query);
	$sth->execute;

	if($sth->rows > 0) {
		my $ref = $sth->fetchrow_hashref();
		my $value = $ref->{'description'};
		$sth->finish;
		$value =~ s/<br>/\n/g;
		$value =~ s/&amp;/&/g;
		$value =~ s/&gt;/>/g;
		$value =~ s/&lt;/</g;
		$d{'vehicles'} = $value;
	} else {
		$d{'vehicles'} = '';
	}

	$query = "SELECT * FROM description WHERE user_id=$data{'user_id'}";
	my $sth = $dbh->prepare($query);
	$sth->execute;

	if($sth->rows > 0) {
		my $ref = $sth->fetchrow_hashref();
		my $value = $ref->{'description'};
		$sth->finish;
		$value =~ s/<br>/\n/g;
		$value =~ s/&amp;/&/g;
		$value =~ s/&gt;/>/g;
		$value =~ s/&lt;/</g;
		$d{'desc'} = $value;
	} else {
		$d{'desc'} = '';
	}

	$query = "SELECT * FROM modifications WHERE user_id=$data{'user_id'}";
	my $sth = $dbh->prepare($query);
	$sth->execute;

	if($sth->rows > 0) {
		my $ref = $sth->fetchrow_hashref();
		my $value = $ref->{'mods'};
		$sth->finish;
		$value =~ s/<br>/\n/g;
		$value =~ s/&amp;/&/g;
		$value =~ s/&gt;/>/g;
		$value =~ s/&lt;/</g;
		$d{'mods'} = $value;
	} else {
		$d{'mods'} = '';
	}

	$query = "SELECT * FROM dsm WHERE user_id=$data{'user_id'}";
	my $sth = $dbh->prepare($query);
	$sth->execute;

	if($sth->rows > 0) {
		my $ref = $sth->fetchrow_hashref();
		$sth->finish;
		$d{'color'} = $ref->{'color'};
		$d{'make'} = $ref->{'make_id'};
		$d{'year'} = $ref->{'year'};
		$d{'year'} = '' if($d{'year'} == 0);
	} else {
		$d{'color'} = '';
		$d{'year'} = '';
		$d{'make'} = $noMake;
	}

	return %d;
}

sub GetStateID {
	my ($abbr) = @_;

	my $state = '';
	my $sth = $dbh->prepare("SELECT state_id FROM states WHERE abbreviation='$abbr'");
	$sth->execute;
	if($sth->rows > 0) {
		my $ref = $sth->fetchrow_hashref();
		$state = $ref->{'state_id'};
	}
	$sth->finish;

	return $state;
}

sub ReloadMembersIndex {
	my ($username) = @_;
	my $pid = fork();
	if($pid == 0) {
		open(STDOUT, '>/dev/null');
		exec './members.pl', $username;
		exit -1;
	} else {
		wait();
	}
}

sub GetImageList {
	my ($user) = @_;
	my $dir = "$top/$imageDir/$user";
	my @files = ();

	return @files if(!-d $dir);
	opendir DIR, $dir;
	my @tmp = readdir DIR;
	closedir DIR;

	foreach my $file (@tmp) {
		next if($file =~ /^\./);
		next if($file eq $thumbnailsDir);
		push @files, $file if(-f "$dir/$file" && -f "$dir/$thumbnailsDir/$file");
	}
	return reverse @files;
}






CGIHeader();

OpenDB();

BeginPage("Edit Member Database");

BeginContent();

if($q =~ /^add/i) {
	Banner("<img src=\"$top/images/member.gif\"> Add new member");

	print <<"EOF";
<p>
Enter a user name and password for your new account.  The user name
will be required to return to modify your information later.
Choose something that is easy to remember, there is no way to change it.
It must be alpha-numeric, no punctuation, spaces, et c., and it is
case-sensitive.
</p>

<form method="post" action="edit.cgi">
<table>
<tr>
<td class=info>User name:</td>
<td><input type=text name=username maxlength=16></td>
</tr>
<tr>
<td class=info>Password:</td>
<td><input type=password name=password maxlength=16></td>
</tr>
<tr>
<td class=info>Confirm:</td>
<td><input type=password name=confirm maxlength=16></td>
</tr>
<tr>
<td></td>
<td><input type=submit name=submit value="Add"></td>
</tr>
</table>
</form>
EOF
} elsif($q =~ /^edit/i) {
	Banner("<img src=\"$top/images/member.gif\"> Member Login");

	print <<"EOF";
<form method="post" action="edit.cgi">
<table>
<tr>
<td class=info>User name:</td>
<td><input type=text name=username maxlength=16></td>
</tr>
<tr>
<td class=info>Password:</td>
<td><input type=password name=password maxlength=16></td>
</tr>
<tr>
<td></td>
<td><input type=submit name=submit value="Login"></td>
</tr>
</table>
</form>
EOF
} elsif($q =~ /^username/i || $q =~ /^password/i) {
	Banner("<img src=\"$top/images/member.gif\"> Forgotten password");

	%form = CGIReadGet();

	if(!UserExists($form{'username'})) {
		print <<"EOF";
<p>
You don't exist.  <a href="edit.cgi?edit">Try again</a>.
</p>
EOF
		goto END;
	}

	print <<"EOF";
<p>
You can request that your password be mailed to you.
</p>

<form method="post" action="edit.cgi">
EOF

	my $email = 'someone@somewhere.com';
	my $query = <<"EOF";
SELECT
	users.user_name,
	user_info.email
FROM
	users,
	user_info
WHERE
	users.user_id=user_info.user_id AND
	users.user_name='$form{'username'}'
EOF
	my $sth = $dbh->prepare($query);
	$sth->execute;

	if($sth->rows > 0) {
		my $ref = $sth->fetchrow_hashref();
		$email = $ref->{'email'};
	}
	$sth->finish;

	print <<"EOF";
<input type=hidden name=username value="$form{'username'}">
<input type=hidden name=email value="$email">
<table>
<tr>
<td class=info>Email address:</td>
<td><b>$email</b></td>
</tr>
<tr>
<td></td>
<td><input type=submit name=submit value="Send Password"></td>
</tr>
</table>
</form>
EOF
	goto END;
} else {
	%form = CGIReadPost();

	if($form{'submit'} eq 'Add') {
		$badness = 0;
		if($form{'password'} ne $form{'confirm'}) {
			print <<"EOF";
<p>
Your passwords do not match.
</p>
EOF
			$badness++;
		}
		if($form{'username'} eq '') {
			print <<"EOF";
<p>
You entered an empty username.
</p>
EOF
			$badness++;
		}
		if(length $form{'password'} < 4) {
			print <<"EOF";
<p>
That password is too short.
</p>
EOF
			$badness++;
		}
		my $tmp = $form{'username'};
		$tmp =~ s/[^\d\w]*//g;
		if($form{'username'} ne $tmp) {
			print <<"EOF";
<p>
You entered a bad username.
</p>
EOF
			$badness++;
		}
		if(UserExists($form{'username'})) {
			print <<"EOF";
<p>
Sorry, the user name <i>$form{'username'}</i> has already been taken.
</p>
EOF
			$badness++;
		}
		if($badness) {
			print "Please <a href=\"edit.cgi?add\">go back</a> and try again.\n";
			goto END;
		}

		# fall through to edit code below
	}

	if($form{'submit'} eq 'Send Password') {
		my $password = '***';

		my $query = "SELECT password FROM users WHERE user_name='$form{'username'}'";
		my $sth = $dbh->prepare($query);
		$sth->execute;
		if($sth->rows > 0) {
			my $ref = $sth->fetchrow_hashref();
			$password = $ref->{'password'};
		}
		$sth->finish;

		$mail{From} = $siteEmail;
		$mail{To}   = $form{'email'};
		$mail{Subject} = "Your mn-dsm membership";
		$mail{Message} = <<"EndMail";
Hello,

Someone requested that an email be sent to this address to tell you
what your password is for the mn-dsm web site.

Web site:  http://mn.dsm.org/
User name: $form{'username'}
Password:  $password

Bye bye.
EndMail

		sendmail %mail;

		print <<"EOF";
<p>
Your password has been mailed to <b>$form{'email'}</b>.
When you receive the message, you can <a href="edit.cgi?edit">try again</a>.
</p>
EOF
		goto END;
	}

	if($form{'submit'} eq 'Submit Changes') {
		# check input
		$badness = 0;
		if($form{'last'} eq '') {
			print "You must specify a last name<br>\n";
			$badness++;
		}
		if($form{'email'} eq '') {
			print "You must specify an email address<br>\n";
			$badness++;
		}
		if($form{'email'} ne '' && $form{'email'} !~ /.+@.+\..+/) {
			print "You must specify a valid email address<br>\n";
			$badness++;
		}
		my $tmp = $form{'icq'};
		$tmp =~ s/[\d+]*//g;
		if($form{'icq'} ne '' && $tmp ne '') {
			print "An ICQ number must be a number<br>\n";
			$badness++;
		}
		$tmp = $form{'year'};
		$tmp =~ s/[\d+]*//g;
		if($form{'year'} ne '' && ($tmp ne '' || length $form{'year'} != 4)) {
			print "The year <i>$form{'year'}</i> is not valid<br>\n";
			$badness++;
		}

		if($badness) {
			print "<a href=\"javascript:history.back(1)\">Try Again</a>\n";
			goto END;
		}

		my $uid = 0;

		if(defined($form{'newuser'})) {
			$dbh->do("INSERT INTO users VALUES ('$form{'username'}','$form{'password'}',NULL)");
			$uid = GetUserID($form{'username'});
			$dbh->do("INSERT INTO changes VALUES ($uid, CURRENT_DATE, CURRENT_DATE)");
		}

		$uid = GetUserID($form{'username'}) if($uid == 0);

		# add or change all remaining fields as necessary

		my $state_id = GetStateID($form{'state'});
		my $de = 1;
		$de = 0 if(defined($form{'hideemail'}));
		my $ln = DefineIfPresent($form{'last'});
		my $fn = DefineIfPresent($form{'first'});
		my $city = DefineIfPresent($form{'city'});
		my $email = DefineIfPresent($form{'email'});
		my $url = DefineIfPresent($form{'url'});
		my $aim = DefineIfPresent($form{'aim'});
		my $icq = DefineIfPresent($form{'icq'});
		my $irc = DefineIfPresent($form{'irc'});
		my $yahoo = DefineIfPresent($form{'yahoo'});

		$dbh->do("DELETE FROM user_info WHERE user_id=$uid");
		$dbh->do("INSERT INTO user_info (user_id, last_name, first_name, city, state_id, email, disclose_email, url, aim, icq, irc, yahoo) VALUES ($uid,$ln,$fn,$city,$state_id,$email,$de,$url,$aim,$icq,$irc,$yahoo)");

		# always a dsm entry, even if it's make=None
		my $color = DefineIfPresent($form{'color'});
		my $year = DefineIfPresent($form{'year'});
		$color = 'NULL' if($form{'make'} == $noMake);
		$year = 0 if($form{'make'} == $noMake || $year eq 'NULL');
		$dbh->do("DELETE FROM dsm WHERE user_id=$uid");
		$dbh->do("INSERT INTO dsm (user_id, color, year, make_id, mod_id) VALUES ($uid,$color,$year,$form{'make'},NULL)");

		$dbh->do("DELETE FROM description WHERE user_id=$uid");
		if($form{'desc'} ne '') {
			$value = DefineTextBlock($form{'desc'});
			$dbh->do("INSERT INTO description (user_id, description) VALUES ($uid,$value)");
		}
		$dbh->do("DELETE FROM modifications WHERE user_id=$uid");
		if($form{'mods'} ne '') {
			$value = DefineTextBlock($form{'mods'});
			$dbh->do("INSERT INTO modifications (user_id, mods, mod_id) VALUES ($uid,$value,NULL)");
		}
		$dbh->do("DELETE FROM vehicle WHERE user_id=$uid");
		if($form{'vehicles'} ne '') {
			$value = DefineTextBlock($form{'vehicles'});
			$dbh->do("INSERT INTO vehicle (user_id, year, type, description) VALUES ($uid,NULL,NULL,$value)");
		}

		# update changes.modified
		$dbh->do("UPDATE changes SET modified=CURRENT_DATE WHERE user_id=$uid");
		ReloadMembersIndex($form{'username'});

		print "Profile for <b>$form{'username'}</b> has been saved<br>\n";
		print "<a href=\"$top/$profileDir/$form{'username'}.html\">View profile</a><br>\n";
		goto END;
	}

	if($form{'submit'} eq 'Delete Profile') {
		my @images = GetImageList($form{'username'});
		foreach my $image (@images) {
			unlink "images/$form{'username'}/$image";
			unlink "images/$form{'username'}/$thumbnailsDir/$image";
		}
		rmdir "images/$form{'username'}/$thumbnailsDir";
		rmdir "images/$form{'username'}";
		unlink "$top/$profileDir/$form{'username'}.html";

		$uid = GetUserID($form{'username'});
		$dbh->do("DELETE FROM vehicle WHERE user_id=$uid");
		$dbh->do("DELETE FROM modifications WHERE user_id=$uid");
		$dbh->do("DELETE FROM dsm WHERE user_id=$uid");
		$dbh->do("DELETE FROM changes WHERE user_id=$uid");
		$dbh->do("DELETE FROM description WHERE user_id=$uid");
		$dbh->do("DELETE FROM user_info WHERE user_id=$uid");
		$dbh->do("DELETE FROM users WHERE user_id=$uid");
		ReloadMembersIndex();
		
		print "Your profile has been removed.<br>\n";

		print "<a href=\"$top\">Home</a>\n";
		goto END;
	}

	if($form{'submit'} eq 'Change Password') {
		if($form{'newpassword'} ne $form{'confirm'}) {
			print "Your new passwords didn't match.<br>\n";
			print "<a href=\"javascript:history.back(1)\">Try again</a>\n";
			goto END;
		}
		if(length $form{'newpassword'} < 4) {
			print "Your new password is too short.<br>\n";
			print "<a href=\"javascript:history.back(1)\">Try again</a>\n";
			goto END;
		}
		if(!Validate($form{'username'}, $form{'oldpassword'})) {
			print "Your old password was incorrect.<br>\n";
			print "<a href=\"javascript:history.back(1)\">Try again</a>\n";
			goto END;
		}
		$dbh->do("UPDATE users SET password='$form{'newpassword'}' WHERE user_name='$form{'username'}'");
		print "Your password has been changed.<br>\n";

		print "<a href=\"$top/$profileDir/$form{'username'}.html\">View profile</a><br>\n";
		print "<a href=\"javascript:history.back(1)\">Return to editor</a>\n";
		goto END;
	}

	if($form{'submit'} eq 'Delete Images') {
		foreach my $image (keys %form) {
			next if($image eq 'submit');
			next if($image eq 'username');
			unlink "images/$form{'username'}/$image";
			unlink "images/$form{'username'}/$thumbnailsDir/$image";
			print "Deleted <b>$image</b><br>\n";
		}
		ReloadMembersIndex($form{'username'});
		BlockBreak();
		print "<a href=\"$top/$profileDir/$form{'username'}.html\">View profile</a><br>\n";
		print "<a href=\"javascript:history.back(1)\">Return to editor</a> - you may need to reload to display any changes\n";
		goto END;
	}

	if($form{'submit'} eq 'Login') {
		if(!UserExists($form{'username'})) {
			print <<"EOF";
<p>
Sorry, the user name <i>$form{'username'}</i> does not exist.
If you don't know your user name, select your name from the list.
If you aren't there, you will need to first <a href="edit.cgi?add">add</a>
yourself an account before you can change it.
</p>

<form method="post" action="edit.cgi">
<input type=hidden name=password value="$form{'password'}">
<select name=username>
EOF

			my $query = <<"EOF";
SELECT
	users.user_name,
	user_info.first_name,
	user_info.last_name
FROM
	users,
	user_info
WHERE
	users.user_id=user_info.user_id
ORDER BY
	user_info.last_name
EOF
			my $sth = $dbh->prepare($query);
			$sth->execute;

			while (my $ref = $sth->fetchrow_hashref()) {
				foreach $key (keys %$ref) {
					$data{$key} = $ref->{$key};
				}

				if(defined($data{'first_name'})) {
					print "<option value=\"$data{'user_name'}\">$data{'first_name'} $data{'last_name'}\n";
				} else {
				print "<option value=\"$data{'user_name'}\">$data{'last_name'}\n";
				}
			}
			$sth->finish;

			print "</select>\n<br>\n<input type=submit name=submit value=\"Login\">\n</form>\n";
			goto END;
		}
		if(!Validate($form{'username'}, $form{'password'})) {
			print <<"EOF";
<p>
You entered an incorrect password.  You can <a href="edit.cgi?edit">go back</a>
to try again.
</p>
<p>
If you don't remember your password
<a href="edit.cgi?username=$form{'username'}"&password=forgotten>
go here</a>.
</p>
EOF
			goto END;
		}
	}

	# handling adding and editing here
	# the user is all validated and happy

	my $str = "<img src=\"$top/images/construction.gif\"> Editing profile for ";
	if($form{'submit'} eq 'Add') {
		$str .= "<i>$form{'username'}</i>";
	} else {
		$str .= "$form{'username'}";
	}
	Banner($str);
	print "<form method=\"post\" action=\"edit.cgi\">\n";
	if($form{'submit'} eq 'Add') {
		print "<input type=hidden name=newuser value=\"$form{'username'}\'\">\n";

		$default{'first'} = '';
		$default{'last'} = $form{'username'};
		$default{'city'} = '';
		$default{'state'} = $defaultState;
		$default{'email'} = '';
		$default{'hide_email'} = 0;
		$default{'url'} = '';
		$default{'aim'} = '';
		$default{'icq'} = '';
		$default{'irc'} = '';
		$default{'yahoo'} = '';
		$default{'color'} = '';
		$default{'year'} = '';
		$default{'make'} = $noMake;
		$default{'desc'} = '';
		$default{'mods'} = '';
		$default{'vehicles'} = '';
	} else {
		%default = GetDefaults($form{'username'});
	}
	print <<"EOF";
<input type=hidden name=username value="$form{'username'}">
<input type=hidden name=password value="$form{'password'}">

<p>
Enter as much information as you feel comfortable providing.  You
must provide your name at a minimum and an email address in case
you forget your password.  Entering your email here does not
subscribe you to our mailing list.  The list of addresses on this site
will not be willfully disclosed to anyone, however if you wish that your
email not be listed on the site, you can choose <i>Hide Email</i>.
</p>

<table cellspacing=0>
<tr>
<td height=32 class=banner><img src="$top/images/member.gif"></td>
<td class=banner width="100%"><b>Identity</b></td>
</tr>
<tr>
<td class=info>Full Name:</td>
<td>
<table cellspacing=0 cellpadding=0>
<tr><td><i>First</i></td><td><i>Last</i></td></tr>
<tr><td>
<input type=text size=12 name=first value="$default{'first'}"></td>
</td><td>
<input type=text size=12 name=last value="$default{'last'}"></td>
</td></tr>
</table>
</td>
</tr>
<tr>
<td class=info>Location:</td>
<td><input type=text size=24 name=city value="$default{'city'}">,
<select name=state>
EOF
	$query = "SELECT * from states ORDER BY abbreviation";
	my $sth = $dbh->prepare($query);
	$sth->execute;

	while (my $ref = $sth->fetchrow_hashref()) {
		foreach $key (keys %$ref) {
			$data{$key} = $ref->{$key};
		}

		if($data{'state_id'} eq $default{'state'}) {
			print "<option selected";
		} else {
			print "<option";
		}
		print " value=\"$data{'abbreviation'}\">$data{'state'}\n";
	}
	$sth->finish;
	
	print <<"EOF";
</select>
</tr>
<tr>
<td height=32 class=banner><img src="$top/images/globe.gif"></td>
<td class=banner width="100%"><b>Internet</b></td>
</tr>
<tr>
<td class=info>Email:</td>
<td><input type=text size=50 name=email value="$default{'email'}">
EOF

	if($default{'hide_email'}) {
		print "<input type=checkbox name=hideemail value=1 checked>Hide Email</td>\n";
	} else {
		print "<input type=checkbox name=hideemail value=1>Hide Email</td>\n";
	}

	print <<"EOF";
</tr>
<tr>
<td class=info>Home Page:</td>
<td><input type=text size=50 name=url value="$default{'url'}"></td>
</tr>
<tr>
<td class=info>Chat:</td>
<td>
<table>
<tr>
<td class=info>AOL Instant Messenger:</td>
<td><input type=text size=10 name=aim value="$default{'aim'}"></td>
</tr>
<tr>
<td class=info>ICQ #:</td>
<td><input type=text size=10 name=icq value="$default{'icq'}"></td>
</tr>
<tr>
<td class=info>IRC Nickname:</td>
<td><input type=text size=10 name=irc value="$default{'irc'}"></td>
</tr>
<tr>
<td class=info>Yahoo! Instant Messenger:</td>
<td><input type=text size=10 name=yahoo value="$default{'yahoo'}"></td>
</tr>
</table>
</td>
</tr>
<tr>
<td height=32 class=banner><img src="$top/images/bug.gif"></td>
<td class=banner width="100%"><b>Vehicles</b></td>
</tr>
<tr>
<td class=info>DSM:</td>
<td>
<table>
<tr>
<td class=info>Color:</td>
<td><input type=text size=10 name=color value="$default{'color'}"> e.g. Black</td>
</tr>
<tr>
<td class=info>Year:</td>
<td><input type=text size=4 name=year value="$default{'year'}"> e.g. 1992</td>
</tr>
<tr>
<td class=info>Make:</td>
<td>
<select name=make>
EOF

	$query = "SELECT * from makes ORDER BY make";
	my $sth = $dbh->prepare($query);
	$sth->execute;

	while (my $ref = $sth->fetchrow_hashref()) {
		foreach $key (keys %$ref) {
			$data{$key} = $ref->{$key};
		}

		if($data{'make_id'} == $default{'make'}) {
			print "<option selected value=\"$data{'make_id'}\">$data{'make'}\n";
		} else {
			print "<option value=\"$data{'make_id'}\">$data{'make'}\n";
		}
	}
	$sth->finish;

	print <<"EOF";
</select>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class=info>Description:</td>
<td><textarea rows=6 wrap=virtual cols=56 name=desc>$default{'desc'}</textarea></td>
</tr>
<tr>
<td class=info>Modifications:</td>
<td><textarea rows=6 wrap=virtual cols=56 name=mods>$default{'mods'}</textarea></td>
</tr>
<tr>
<td class=info>Other Vehicles:</td>
<td><textarea rows=6 wrap=virtual cols=56 name=vehicles>$default{'vehicles'}</textarea></td>
</tr>
<tr>
<td></td>
<td><input type=submit name=submit value="Submit Changes"></td>
</tr>

</table>
</form>
EOF

	if($form{'submit'} ne 'Add') {
		my @images = GetImageList($form{'username'});
		
		if($#images >= 0) {
			BlockBreak();
			Banner("<img src=\"$top/images/film.gif\"> Delete Images");

			print <<"EOF";
<p>
Choose an image below if you wish to delete it.
</p>

<form method="post" action"edit.cgi">
<input type=hidden name=username value="$form{'username'}">
<table cellpadding=10>
EOF

			$i = 0;
			foreach my $image (@images) {
				print "<tr>\n" if($i == 0);
				$i++;
				print "<td>\n";
				print "<input type=checkbox name=\"$image\" value=1>\n";
				print "<b>$image</b><br>\n";
				print "<img src=\"$top/$imageDir/$form{'username'}/$thumbnailsDir/$image\" alt=\"$image\">\n";
				print "</td>\n";
				if($i == 3) {
					print "</tr>\n";
					$i = 0;
				}
			}
			print <<"EOF";
</table>
<input type=submit name=submit value="Delete Images">
</form>
EOF
		}

		BlockBreak();
		Banner("<img src=\"$top/images/film.gif\"> Image Upload");
		print <<"EOF";
<p>
Choose an image on your local computer to upload to the site.  You may
upload any .jpg, .gif or .png file, and avoid any weird characters
(spaces, punctuation, etc.) in the file name.
</p>

<p>
Do not upload inappropriate images.
Your profile will be deleted and you will not be welcome back if
you violate this.  Period.
</p>

<form method="post" enctype="multipart/form-data" action="image.cgi">
<input type=hidden name=username value="$form{'username'}">
<table>
<tr>
<td class=info>File:</td>
<td><input type="file" name="upload_file"></td>
</tr>
<tr>
<td></td>
<td><input type=submit value="Upload Image"></td>
</tr>
</table>
</form>
EOF

	BlockBreak();
	Banner("<img src=\"$top/images/key.gif\"> Change Password");
	print <<"EOF";
<form method="post" action"edit.cgi">
<input type=hidden name=username value="$form{'username'}">
<table>
<tr>
<td class=info>Old Password:</td>
<td><input type=password name=oldpassword maxlength=16></td>
</tr>
<tr>
<td class=info>New Password:</td>
<td><input type=password name=newpassword maxlength=16></td>
</tr>
<tr>
<td class=info>Again:</td>
<td><input type=password name=confirm maxlength=16></td>
</tr>
<tr>
<td></td>
<td><input type=submit name=submit value="Change Password"></td>
</tr>
</table>
</form>
EOF

	BlockBreak();
	Banner("<img src=\"$top/images/tnt.gif\"> Delete Profile");
	print <<"EOF";
<p>
If you don't want to be listed anymore, delete your profile.  Warning:
this is permanent.
</p>

<form method="post" action"edit.cgi">
<input type=hidden name=username value="$form{'username'}">
<input type=submit name=submit value="Delete Profile">
</form>
EOF

	}
}

END:

EndContent();
EndPage();

