#!/usr/bin/perl

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

# This script manages the user accounts of a password protected directory.

use CGI ':standard';

require "pw_variables.pm";

$section = param('section');
$user = param('username');
$password = param('password');

if ( $user eq "" )
{
	print "No user entered - exiting ...<BR>";
	exit;
}

print<<HEADER;
<HTML>
<HEAD>
<TITLE>
IE Internet Password Protected Area Management
</TITLE>
<link rel="stylesheet" type="text/css" href="http://www.ieinternet.com/home2.css">
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1"></head>
</HEAD>
<BODY>
<TABLE WIDTH="750" BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR VALIGN=TOP>
<TD COLSPAN=3>
        <A HREF="http://ieinternet.com/index.htm"><IMG SRC="http://ieinternet.com/images/logo_top.gif" WIDTH="291" HEIGHT="58" BORDER="0"
        ALT="Irelands leader for affordable web hosting, website hosting, email hosting, ecommerce hosting,
        windows hosting, secure hosting, full anti-virus email screening, full anti-spam email screening, ieinternet.com
        shared hosting, ieinternet.com sme hosting, irelands leading cost effective web hosting provider.ieinternet.com"></A>
</TD>

<TD>
        <IMG SRC="http://ieinternet.com/images/dot.gif" WIDTH="289" HEIGHT="51" BORDER="0">
</TD>
<TD colspan=2>
        <A HREF="http://ieinternet.com/mycontrolpanel.htm"><img src="http://www.ieinternet.com/images/control_panel.gif"
        WIDTH="170" HEIGHT="51" ALT="Access your control panel here" BORDER="0"></A>

<TD>
        <A HREF="http://ieinternet.com/webmail"><img src="http://ieinternet.com/images/my_webmail.gif"
        WIDTH="131" HEIGHT="51" ALT="Access your webmail here" BORDER="0"></A>
</TD>
</table>
<P CLASS=TEXT>
<BR><BR>
HEADER

if ( $section eq "add_user" )
{
	$heading = "<b>Addition of a user</b>";
	$comment = "The following user has been added to the list of users allowed to access your password protected directory:";
} elsif ( $section eq "delete_user" ) {
	$heading = "<b>Deletion of a user</b>";
	$comment = "The following user has been deleted from the list of users allowed to access your password protected directory:";
} elsif ( $section eq "change_password" ) {
	$heading = "<b>Change of a user's password</b>";
	$comment = "The following user's password has been changed, details below:";
} else {
	print "An error has occured - exiting...<br><br>";
	exit;
}

print "$heading<BR><BR>$comment<BR><BR>";
print "<b>User:</b> $user<br>";

if ( $password ne "" )
{
	# this means that we are adding a user or changing its password - either way we run the same command
	system "htpasswd -m -b $htpasswdfile $user $password";
	print "<b>Password:</b> $password<br>";
} else {
	# this means we delete the user
	@ammended_user_list = ();
	$searchstring = "^$user\:";
	open (HTPASS, $htpasswdfile) || die "Couldn't open the file $htpasswdfile: $!";
	while (<HTPASS>)
	{
		if ( !(/$searchstring/) )
		{
			push @ammended_user_list, $_;
		}
	}
	close (HTPASS);

	$arraysize = @ammended_user_list;
	print "opening the file $tmphtpasswdfile";
	open (NEWFILE, ">$tmphtpasswdfile") || die "Couldn't open the file $tmphtpasswdfile: $!";
	for ( $i = 0 ; $i < $arraysize ; $i++ )
	{
		print "Keep this user: $ammended_user_list[$i]<BR>";
		print NEWFILE $ammended_user_list[$i];
	}
	close (NEWFILE);

	system "mv $tmphtpasswdfile $htpasswdfile";
}		
	


print<<FOOTER;

<br><br>
<TABLE WIDTH="750" BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR VALIGN=TOP><TD><IMG SRC="http://www.ieinternet.com/images/dot.gif"
HEIGHT="3" BORDER="0"></TD>
</TR><TR VALIGN=TOP><TD BGCOLOR="#CC0000"><IMG SRC="http://www.ieinternet.com/images/dot.gif" HEIGHT="7" BORDER="0"></TD>
</TR><TR VALIGN=TOP><TD class="copyright"><IMG SRC="http://www.ieinternet.com/images/home_logos.gif" HEIGHT="46" WIDTH="750" BORDER="0">
<center> Copyright 1998 -  2004 IE Internet.com Ltd. All Rights Reserved. </center></TD></TR></TABLE>

</BODY>
</HTML>

FOOTER
