sub admin
{

# Define all our variables as local to this subroutine:
my ($password,$encpass,$cookie,$c,$return,@lines,@keys,%values);

$password = param("password") || '';						# Get the admin password sent from the form.
$return   = param("return")   || shift || '';				# Do we want to return to another page after authentication ?
$encpass  = crypt($password,"p7");							# Encrypted version of the above password.
$cookie   = retrieve_cookie("gwad") || '';					# Get any existing admin cookie.

if (param("x")){											# If we have a value for the parameter 'x' ('x' means exit or logout):
 $c = setcookie('gwad','','-1d');							# Prepare a cookie with an expire time yesterday (this will delete the cookie). 
 print "Set-Cookie: $c\n";									# Send the cookie.
 print "location: http://$host$scripturl\n\n";				# Return to the welcome page for Guestwho.
 exit;
}

if ($encpass ne "$adminpass" && $cookie ne "$adminpass"){	# If we have neither a valid admin cookie or a valid password:
print "Content-type: text/html\n\n";						# Tell it we're gonna output HTML.

print <<END;												# Print the following HTML (for the password entry page) untill we reach the keyword "END".
<HTML>
<head>
<title>Guestwho Administration: Enter Password</title>
</head>
<body bgcolor="#FFFFFF" text="#285080">
<FORM ACTION="http://$host$scripturl" METHOD="POST">
<input type="hidden" name="action" value="admin">
<input type="hidden" name="return" value="$return">
<font face="arial" size="2">
<b>Please Enter Admin Password:</b></font>
<input type="password" name="password">
<input type="submit" value="Submit">
</FORM>
</body>
</HTML>

END
exit;														# Tell it we're done executing, and it can finish now.
}															# Close the 'if' loop we started earlier.

if ($cookie eq $adminpass || $encpass eq $adminpass){		# If we do have either a valid admin cookie or a valid password:

 $c = setcookie('gwad',$adminpass,'+30m');					# Prepare a new admin cookie.
 @lines = getfile($adminfile,0);							# Get the HTML to print for the main admin page.
 %values = %{get_data()};									# Read the list of entries into a 2D hash (%values).
 
 
	# Loop over the list of entries from %values, 
	# and round the entry number of each to 3 digits:
 
 foreach (keys %values){
  $values{$_}{'entry'} = sprintf("%.3d",$values{$_}{'entry'});
 }
 
 @keys = return_sorted(\%values,'entry','desc');			# Get a sorted list of keys from %values.
  
 print "Set-Cookie: $c\n";									# Send the new admin cookie.
 
 
	# If we have a value for the 'return' parameter, send us back to that page:
	# This will automatically stop the script for executing any further.
 
 if ($return){print "Location: http://$host$scripturl?action=$return\n\n";}
 
 	# If there's no 'return' parameter, we continue on as follows..
 	
 
 print "Content-type: text/html\n\n";						# Tell it we're gonna output HTML.
 
 
 # Loop over each line of HTML from the main admin template, doing the following:
 
 # 1. If the line contains the special content tag '<!--%%ENTRYLIST-->' do the following:
 #	a. Start a drop down (select) box ready for a list of entries.
 #	b. Loop over the entry list, printing an option tag for each one.
 
 #	N.B.
 #	The value of each option is the entry number (e.g. 012).
 #	The description for each option is in the format "Entry # - Name".
 #	(e.g. "001 - Tom Smith").
 #	A sample option tag looks like this: <option value="123">123 - Jack Daniels</option>
 
 # 2. If the line doesn't contain the above content tag:
 #	a. Filter it through the expand_vars() function, which handles other miscellaneous content tags.
 #	b. Print the line out with the rest of the content.
 
 #	N.B.
 # The miscellaneous content tags are discussed in detail in the manual.
 
 
 foreach my $line (@lines){
  if ($line =~ /\Q<!--%%ENTRYLIST%%-->\E/)
  {
   print "<select name=\"entrynum\" align=\"center\">\n";
   print "<option value='' selected>Select Entry Number:</option>\n";
   
   foreach my $key (@keys){
   print "<option value=\"$values{$key}{'entry'}\">$values{$key}{'entry'} - $values{$key}{'name'}</option>\n";
   }
   
   print "</select>\n";
   
  }else{
   $line = expand_vars($line,'');
   print "$line";
  }
 }
}
exit;														# Tell it we're done for now.
}															# Close the subroutine.
1;