#!/usr/bin/perl
print "Content-type: text/html\n\n";
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;
      $cgi{$name} = $value;
}
# load in the form contents
$htaccess = "/usr/local/etc/httpd/cgi-bin/kims/chk/up/.htaccess";
$htpasswd = "/usr/local/etc/httpd/cgi-bin/kims/chk/up/.htpasswd";
$hthead = "/usr/local/etc/httpd/cgi-bin/kims/chk/head";
$passlog = "/usr/local/etc/httpd/cgi-bin/kims/chk/passlog";
$real = $cgi{'realname'};
$email = $cgi{'email'};
$nick = $cgi{'username'};
if((length($real)) == 0){
	print "<BLINK>Please go back and fill the name...";
}elsif(length($email) == 0){
	print "<BLINK>Please go back and fill the email...";
}elsif(length($nick) == 0){
	print "<BLINK>Please go back and fill the nickname...";
}
open(FC,$htpasswd);
while(<FC>){
	@get = split(/:/,$_);
	if( (length($nick) == length(@get[0])) && ($nick =~ /@get[0]/) ){
		print "Sorry nickname already exists\n";
		close(FG);
		exit;
	}
}		
close(FG);
$time = localtime(time);
@rad = split(/ /,$time);
@time = split(/:/,@rad[3]);
$rad = @time[2]*7;
open(F,"rad.dat");
while(<F>){
	$rad = eval($rad) + $_;
}
close(F);
open(F,">rad.dat");
print F $rad;
close(F);
$pass = sprintf("AA%s",$rad);
$command = sprintf("/usr/local/etc/httpd/cgi-bin/kims/chk/k_pass %s %s %s",$htpasswd,$nick,$pass);
system($command);
open(FF,">$htaccess");
#open(FF,">testa");
open(FR,"/usr/local/etc/httpd/cgi-bin/kims/chk/head");
while(<FR>){
	print FF $_;
}
close(FR);
open(FA,"$htpasswd");
while(<FA>){
	@aa = split(/:/,$_);
	print FF "require user @aa[0]\n";
}
close(FA);	
print FF "</Limit>\n";
close(FF);
open(FL,">>$passlog");
print FL "$real:$email:$nick\n";
close(FL);
open(MAIL,"| /bin/mail $email");
print MAIL "From: root\@ezenet.com\n";
print MAIL "Subject: Your Login name and Password\n";
print MAIL "Your Login name is $nick\n";
print MAIL "Your Passwd is $pass\n";
close(MAIL);
print "<CENTER>Thanks</CENTER><BR>Please check your email to get your username and passwd";
print "<A HREF=/cgi-bin/kims/chk/up/ccc>Enter the program</A>\n";
