#!/usr/local/bin/perl
##-----------------------------------------------------------------##
##                                                                 ##
##                                                                 ##
## © Copyright Mr Lyle R Hopkins 1999. All rights reserved. No part##
## of this or any of the attached documents shall be               ##
## reproduced/stored in any way whatsoever without written         ##
## permission from the Copyright holder.                           ##
##   The Copyright holder holds no responsibility for errors or    ##
## omissions. No liability is assumed in any way for damages       ##
## resulting from the use of this document/program.                ##
##                                                                 ##
## Have a nice day.                                                ##
##                                                                 ##
##                                                                 ##
##-----------------------------------------------------------------##

## By Lyle Hopkins ##

print "Content-type: text/html\n\n";

##################################################
######################## Define variables
##################################################

$programurl = "http://ukzone.hypermart.net/scripts/demos/cgi-bin"; ## The folder where you put the files



##################################################
######################## See what to do
##################################################

$WHATWANT = $ENV{'QUERY_STRING'};

if ($WHATWANT eq "admin") {
      &admin();
   }
if ($WHATWANT eq "enteradmin") {
	## Get user input
   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
   @pairs = split(/&/, $buffer);
   foreach $pair (@pairs) {
   ($name, $value) = split(/=/, $pair);
   $value =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $FORM{$name} = $value;
   }
	## Get file Username and password
   open(INF,"adminpass.txt"); ## Open read file
   $userpass = <INF>; ## Put into an array
   close(INF); ## Close file
   chomp($userpass);
   ($username, $password) = split(/:/, $userpass);
   $enterpasscrypt = crypt($FORM{'password'}, substr($password, 0, 2));
	## If they match
   if ($FORM{'username'} eq $username && $enterpasscrypt eq $password){
      &enteradmin();
   } ## End if
   }
if ($WHATWANT eq "changeadmin") {
	## Get user input
   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
   @pairs = split(/&/, $buffer);
   foreach $pair (@pairs) {
   ($name, $value) = split(/=/, $pair);
   $value =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $FORM{$name} = $value;
   }
	## Get file Username and password
   open(INF,"adminpass.txt"); ## Open read file
   $userpass = <INF>; ## Put into an array
   close(INF); ## Close file
   chomp($userpass);
   ($username, $password) = split(/:/, $userpass);
   $newpasscrypt = crypt($FORM{'password'}, substr($password, 0, 2));
	## Save new data
  open(OUTF,">adminpass.txt"); ##  Admin file for appending
  print OUTF
  "$FORM{'username'}:$newpasscrypt"; ## Write user fields
  close(OUTF); ## Close file

  print "<p>New data saved\n<br>";
  print "<p>It is recommended that you now Chmod the file read-only (444)\n<br>";
   }


##################################################
######################## Enter Admin
##################################################

sub admin {

print <<EndHTML;

<p align="center"><font size="6">Enter password to enter Admin
area</font></p>

<p align="center">&nbsp;</p>

<form action="$programurl/makeadmin.cgi?enteradmin" method="POST">
    <p align="center">Username <input type="text" size="20"
    name="username"></p>
    <p align="center">Password <input type="text" size="20"
    name="password"></p>
    <p align="center"><input type="submit" value="Enter"></p>
</form>

EndHTML
;

exit;
} ## End sub


##################################################
######################## The page
##################################################

sub enteradmin {

print <<EndHTML;

<p align="center"><font size="6">Enter new Username and Password</font></p>

<p align="center">&nbsp;</p>

<form action="$programurl/makeadmin.cgi?changeadmin" method="POST">
    <p align="center">New Username <input type="text" size="20"
    name="username" value="$FORM{'username'}"></p>
    <p align="center">New Password <input type="text" size="20"
    name="password" value="$FORM{'password'}"></p>
    <p align="center"><input type="submit" value="Enter"></p>
</form>

EndHTML
;

} ## End sub