#!/usr/bin/perl
#
# password protect.

use Apache::Htpasswd;

$header = "<HTML><HEAD><TITLE>Password Protect</TITLE></HEAD>
<BODY background=http://planetmind.net/images/hunabku_bkg3.gif>
<IMG SRC=http://planetmind.net/images/planetwomanlogo.gif WIDTH=212 HEIGHT=54><BR>";

# Get the form input
  read(STDIN, $input, $ENV{'CONTENT_LENGTH'});

	@pairs = split(/&/, $input);

    foreach $pair (@pairs) {
	($name, $value) = split(/=/, $pair);
	
	$name =~ tr/+/ /;
	$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

   $FORM{$name} = $value;
		}

$username = $FORM{'username'};
$password = $FORM{'password'};
$root_dir = $ENV{'SERVER_NAME'};
$root_dir =~ s/\.com|\.net|\.org//i;
$root_dir =~ s/www.//i;
$docroot = $root_dir;
$root_dir = "/www/htdocs/".$root_dir;
$secure_dir = $FORM{'dir'};
$url = "http://".$ENV{'SERVER_NAME'}.$secure_dir;
$secure_dir = $root_dir.$secure_dir;

$uid = (stat($secure_dir))[4];
$owner = (getpwuid($uid))[0];

$script_url = "http://".$ENV{'SERVER_NAME'}."/cgi-bin/cgiwrap/$owner/password_protect.cgi";

$mailprog = "/usr/sbin/sendmail";
$admin_email = $owner."\@".$ENV{'SERVER_NAME'};
$subject = "Planetmind Admin: Password Protection info for $docroot";
$from = "synapse\@planetmind.net";

if ($FORM{'submit'}) {

if (!(-e "../admin/.htpasswd")) {$ex = `touch ../admin/.htpasswd`;}
$foo = new Apache::Htpasswd("../admin/.htpasswd");


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

$htaccess = $secure_dir."/.htaccess";
open(DBASE, ">$htaccess") || print "I can't open $htaccess\n";

print DBASE "AuthUserFile /www/htdocs/$docroot/admin/.htpasswd
AuthGroupFile /dev/null
AuthName \"Private Location\"
AuthType Basic
<Limit GET PUT POST>
require user $username
</Limit>";
close(DBASE);

$call = $foo->htpasswd("$username", "$password");

if ($call == 1) {
print "<FONT SIZE=+1 COLOR=\"#DD0000\"><P>The URL <A HREF=\"$url\">$url</A> is now protected. </FONT><P> Use this information to access it: <P><B>Username:</B> $username <P><B>Password:</B> $password<P>\n";
} else {
  if ($foo->error() =~ /exists/) {
          print "The user $username exists - please go back and choose another.<P>";
  } else {
    print "An error occured: $foo->error(). Please go back and try again. <P>";
  }
}

print "<HR><P>[<A HREF=\"http://$ENV{'SERVER_NAME'}/admin/\">Back to Admin Page</A>]";
print "</body></html>";

open (MAIL, "|$mailprog $admin_email") || die "Can't open $mailprog!\n";
      print MAIL "From: $from\n";
      print MAIL "To: $admin_email\n";
	print MAIL "Subject: $subject\n\n";
      print MAIL "Here is the password protection information you recently set up. Please retain this 
email for your records.

Protected URL: $url
Username: $username
Password: $password

Thanks for using the Planetmind Site Admin Tools!

";

close(MAIL);

exit;
}

if ($FORM{'unprotect'}) {
$call = system("rm -f $secure_dir/.htaccess");
# print "<P>Result = ".$call;

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

print "<FONT SIZE=+1 COLOR=\"#DD0000\">The URL $url is now un-protected.</FONT> ";
print "<HR><P>[<A HREF=\"http://$ENV{'SERVER_NAME'}/admin/\">back to Admin Page</A>]";
print "</body></html>";

exit;

}


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

# print $uid." secure dir = $secure_dir";
# print "root = $root_dir";
print qq!
<H2>Password Protection</H2>
Use this form to password protect or unprotect everything in a directory.<P>

<FORM ACTION=$script_url METHOD=Post>
Enter path to directory: <INPUT NAME="dir" TYPE="text"> 
<input type=button onClick='ifield = document.forms[0].dir; chooser = window.open("/cgi-bin/chooser.cgi?type=1&chroot=$root_dir&file="+ifield.value, "chooser", "toolbar=no,menubar=no,scrollbar=no,width=400,height=300"); chooser.ifield = ifield' value="Browse...">
<P>
Enter Username: <INPUT NAME="username" TYPE="text"><P>
Enter Password: <INPUT NAME="password" TYPE="text">
<P>
<INPUT NAME="submit" TYPE="submit" VALUE="Protect">
<P>
<INPUT NAME="unprotect" TYPE="submit" VALUE="UN-Protect">

</FORM>





<A HREF="http://ecomail.org/">Click here for Ecomail</A> 
</BODY></HTML>!




