#!/usr/bin/perl
#  Affiliate Manager v1.1.1 - Members Script
#  Created By Infinity Systems
#  http://www.infinitysys.net/
#  xxagentxx@infinitysys.net  (XxagentxX)
#  mace@infinitysys.net       (Mace)
#
#############################################################
#				 		  Copyright
#############################################################
# Copyright 2001 Infinity Systems  All Rights Reserved.
#
# Selling or redistributing the code for this program
# without prior written consent is expressly forbidden.
# In all cases copyright and header must remain intact.
#############################################################
#				 		  Disclaimer
#############################################################
#  Infinity Systems, its employees, third parties, or any
#  affiliations with Infinity Systems is not responsible for any and
#  all demands, losses, liabilities, claims, including attorney
#  fees asserted against Infinity Systems, its employees, thrid
#  parties or any affiliation with Infinity Systems.  Customers
#  agree not to hold Infinity Systems, its employees, thrid parties
#  or any affiliation with Infinity Systems against liabilities
#  from:
#
#  1)Any injury to a person or property caused by a sold
#  and provided product by Infinity Systems
#
#  2)Copyright infringement 
#
#  3)any defective products sold to customer from Infinity Systems
#  products. These programs may not be distributed in whole
#  or part, freely, for pay, or any other form of compensation
#############################################################
#############################################################

### Variables ###

# This is the URL to affmembers.cgi
$affmembers = "http://www.infinitysys.net/cgi-test/affmembers.cgi";

# This is the Full Unix Path to your data directory (this MUST be same as in affadmin.cgi and affmembers.cgi)
$datadir = "/home/sites/site62/web/datatest";

### Uneditable Variables ###
$title = "Affiliate Manager (Members Script)";
$version = "1.1.1";

### Parse form and Parse Query ###
&parse_form;
&parse_query;

### If Statements ###
$referer = $ENV{HTTP_REFERER};
if (($action eq "checklogin") && ($referer =~ "$scripturl")) {
	&check_login;
}
elsif (($action eq "update") && ($referer =~ "$scripturl")) {
	&update;
}
else {
	&login;
}

### Subroutines ###
sub update {


	if ($FORM{'username'} eq ""){
		print "You forgot to fill out part of the form.  Please go back and change it.\n";
		exit;
	}
	else {  
		$username = $FORM{'username'};
		$username =~ s/\|//g;    
	}
	if ($FORM{'siteurl'} eq ""){
		print "You forgot to fill out part of the form.  Please go back and change it.\n";
		exit;
	}
	else {  
		$siteurl = $FORM{'siteurl'};
		$siteurl =~ s/\|//g;    
	}
	if ($FORM{'buttonurl'} eq ""){
		print "You forgot to fill out part of the form.  Please go back and change it.\n";
		exit;
	}
	else {  
		$buttonurl = $FORM{'buttonurl'};
		$buttonurl =~ s/\|//g;    
	}
	if ($FORM{'email'} eq ""){
		print "You forgot to fill out part of the form.  Please go back and change it.\n";
		exit;
	}
	else {  
		$email = $FORM{'email'};
		$email =~ s/\|//g;    
	}
	if ($FORM{'type'} eq ""){
		print "You forgot to fill out part of the form.  Please go back and change it.\n";
		exit;
	}
	else {  
		$type = $FORM{'type'};
		$type =~ s/\|//g;    
	}
	if ($FORM{'comments'} eq ""){
		print "You forgot to fill out part of the form.  Please go back and change it.\n";
		exit;
	}
	else {  
		$comments = $FORM{'comments'};
		$comments =~ s/\|//g;    
	}

# Mace|hiSKt3fRvjZMI|http://www.infinitysys.net/3|http://www.infinitysys.net/images/isbutton.gif3|mace@infinitysys.net3|My comments here|PERL
# $username, $passwd2, $siteurl, $buttonurl, $email, $comments, $type
	
open(DIR,"$datadir/members.iddb");
	flock(DIR, 2); # Lock the file so nobody else uses it.   	
		@members = <DIR>;
flock(DIR, 8);
close(DIR);
open(DIR,">$datadir/members.iddb");
foreach $line (@members) {
chomp($line);
($username2, $passwd2, $siteurl2, $buttonurl2, $email2, $comments2, $type2) = split(/\|/, $line);
if ($FORM{'username'} eq "$username2") {
if ($FORM{'apassword2'} eq "") {
$newpass = $passwd2;
}
else {

$newpass = crypt($FORM{'apassword2'}, substr($FORM{'apassword2'}, 0, 2));

}
print DIR "$username|$newpass|$siteurl|$buttonurl|$email|$comments|$type";
$var = "$username|$newpass|$siteurl|$buttonurl|$email|$comments|$type";
}
else {
print DIR "$line\n";
}
}
close(DIR);

$message = "Your information has been updated";

&main;


}

sub main {
&header;

($username, $passwd2, $siteurl, $buttonurl, $email, $comments, $type) = split(/\|/, $var);

print <<EOF ;
<html>
<head><title>$title - $version</title></head>
<body link="blue" vlink="blue" alink="blue">
<center>
<font face="arial" size="3" color="black">
<b>$title $version Administration Area</b>
<br>
</font>
<font face="arial" size="2" color="blue">
$message
</font>
<br>
<hr>
</center>
<form action="$affmembers?action=update" method="post">
<table border="0" align="center">
<tr><td><font face="arial" size="2" color="black">UserName</font></td><td><font face="arial" size="2" color="black">$username<input type="hidden" name="username" value="$username"></font></td></tr>
<tr><td><font face="arial" size="2" color="black">Password</font></td><td><input type="text" size="35" maxlength="256" name="apassword2"></td></tr>
<tr><td><font face="arial" size="2" color="black">Website URL</font></td><td><input type="text" size="35" maxlength="256" name="siteurl" value="$siteurl"></td></tr>
<tr><td><font face="arial" size="2" color="black">Button Image URL</font></td><td><input type="text" size="35" maxlength="256" name="buttonurl" value="$buttonurl"></td></tr>
<tr><td><font face="arial" size="2" color="black">Your E-mail</font></td><td><input type="text" size="35" maxlength="256" name="email" value="$email"><input type="hidden" name="comments" value="$comments"><input type="hidden" name="type" value="$type"></td></tr>
</table>
<P>
<table align="center" border="0">
<tr>
<td><input type="submit" value="Update"></td>
<td><input type="reset" value="Reset Form"><br></td>
</form>
</tr>
</table>
<P>
<center>
<table width="50%" border="1">
<tr><td>
<font face="arial" size="2" color="red">NOTE:</font> <font face="arial" size="2" color="black">If you want to change your password, type in a new one.  If you DO NOT
want to change your password, then leave the password field blank.</font>
</td></tr>
</table>
</center>
</body>
</html>

EOF

}

sub check_login {
	
$passwd3 = crypt($FORM{'passwd'}, substr($FORM{'passwd'}, 0, 2));

open(FILE, "$datadir/members.iddb");
      		@data = <FILE>;
	close(FILE);
foreach $line (@data) {
($username, $passwd2, $siteurl, $buttonurl, $email, $comments, $type) = split(/\|/, $line);

if (($FORM{'login_id'} eq "$username") && ("$passwd2" eq "$passwd3")) {
$var = $line;
$enter = 1;
}
}
if ($enter == "1") {

$message = "Welcome";

	&main;
}
else {
	&login;
}
}

sub login {
&header;

	open(DIR,"$datadir/header.iddb");
	@header = <DIR>;
	close(DIR);

	open(DIR,"$datadir/footer.iddb");
	@footer = <DIR>;
	close(DIR);
	
print "@header\n";
	
print <<EOF ;
<form action="$affmembers?action=checklogin" method=post>
<table cellspacing=1 bgcolor=#000000 border=0 cellpadding=3 valign=center>
<TR bgcolor=#E4E4E4 align=center>
<TD colspan=2>
<FONT SIZE=-1 FACE=Arial color="#000000"><B>Login</B><font size=-1>
</TD></TR>
<tr>
<td bgcolor=#cecece>
<font size=-1>Username</td><td bgcolor=white><input type="Text" name="login_id" size=30>
</td>
</tr>
<tr>
<td bgcolor=#cecece>
<font size=-1>Password</td><td bgcolor=white><input type=password name="passwd" size=30>
</td>
</tr>
<TR bgcolor=#E4E4E4 align=center>
<TD height=23 colspan=2>
<FONT SIZE=-1 FACE=Arial><font size=-1><input type=submit value=Login>
</TD>
</TR>
</table>
</form>
EOF

print "@footer\n";

}

sub header {

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

}

sub parse_query {
$_ = <STDIN>;
$_ = "$_&$ENV{'QUERY_STRING'}";
tr/+/ /;
@keys = split(/&/, $_);
foreach $key (@keys) {
  ($tmp, $value) = split(/=/, $key);
  $_ = $tmp;
  $action = $value if m/action/i;
  $user = $value if m/user/i;
  $email = $value if m/email/i;
  $level = $value if m/level/i;
  $passwd = $value if m/passwd/i;
  $beginat = $value if m/beginat/i;
  $endat = $value if m/endat/i;
}
}

sub parse_form {

	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;
          	$value =~ s/<!--(.|\n)*-->//g;

          	$FORM{$name} = $value;

       }
}