#!/usr/bin/perl
# ^ Set this to your location of perl ^
############################################################
#
# Count 'Em - Version 1.1
# Count.cgi
# This script allows users to sign up and modify their counters.
#
# Written by jumpworld.net
# All questions should be directed to webmaster@jumpworld.net
#
# To Do in next version:
# More Counter Styles
# Got a request: Email webmaster@jumpworld.net
#
##############################################################
#
# This script is NOT freeware. Registration is US $35 which
# includes free updates, support and one liscence.
# 'We don't believe in charging for the same script twice'
#
# By using this script you agree to exclude us from any liability           
# that might arise from its use.
#                                                                            
# Redistributing\selling the code for this program without prior      
# written consent is expressly forbidden.
############################################################### 

# Name of this script
$scriptname = 'count.cgi';

# Url to counter file.
$url = 'http://www.mom.to/counter/img_counter.cgi';

# Path to datafile
$datafile ='/home/momto/ftp/counter/datafile.txt';

# Path to counter file
$path = '/home/momto/ftp/counter';

# Site url
$site= 'http://www.mom.to/counter/count.cgi';

# Site title
$title ='MOM';

# Location of header file
$header = 'header.txt';

# Location of footer file
$footer = 'footer.txt';

# Location of footer file
$counts = 'counts.txt';

# FILE LOCKING --  0 = off   1 = on #
 # Not recommended to be turned off #
 # Only turn off on NT systems #
  $use_flock = "1";

# Send Email Confirmations of sign ups --  0 = off   1 = on #
# Turn off to make sign up process quicker # 
  $use_email = "1";

# If above is set to 1 put location of sendmail
$sendmail="/usr/lib/sendmail";

# Your email address (if usemail = 1)
$youremail ="MOM\@MOM.to";


##### No more editing Required #####

open (FILE,"$header");
@header=<FILE>;
close(FILE);
open (FILE,"$footer");

@footer=<FILE>;
close(FILE);
# HTML Output
print "Content-type: text/html\n\n";

################################
# Get all the form info
################################
sub form_info  {
 read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
 if ($ENV{'QUERY_STRING'})
 	{$buffer = "$buffer\&$ENV{'QUERY_STRING'}";}
 @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;}
}
# CALL THE SUB ABOVE 
&form_info;

# Do some defining
$name2 = $FORM{'name'};
$password = $FORM{'password'};
$cvalue = $FORM{'cvalue'};
$uurl = $FORM{'url'};
$email = $FORM{'email'};
$success = "";
############## Figure out what the user wants to do ##############



if ($FORM{'action'} eq "Create new counter") {
    &create;
    }

elsif ($FORM{'action'} eq "Create Counter") {
    &newreal;
    }
elsif ($FORM{'action'} eq "Modify Counter") {
    &modify;
    }
elsif ($FORM{'action'} eq "Modify") {
    &modreal;
    }
elsif ($FORM{'action'} eq "Change") {
    &uchange;
    }
elsif ($FORM{'action'} eq "Delete Counter") {
&deletehtm;
    }
elsif ($FORM{'action'} eq "Delete") {
&delete;
    }
else {
&panel;
}

sub create {
print<<EOF;
<html><head><title>Create Counter</title></head>
@header<br>
<center>Add a record</center>
<form method="POST" action="$scriptname">
UserName: <input type="text" size="20" name="name"><br>
Password: <input type="text" size="20" name="password"><br>
Site Url: <input type="text" size="20" name="url"><br>
Email: <input type="text" size="20" name="email"><br>
<input type="submit" name="action" value="Create Counter"></form><br>
@footer
</body></html>
EOF
exit;
}
sub panel {
print<<EOF;
<html>
<head>
<title>Free Counters</title>
</head>
@header<br>
<body bgcolor="#446F6A">
<div align="center"><center>

<table border="0" width="76%" bgcolor="#FFFFFF" cellspacing="0" cellpadding="0">
  <tr>
    <td width="100%"><div align="center"><center><table border="1" width="73%" cellspacing="0"
    cellpadding="0" bgcolor="#657C96">
      <tr>
        <td width="100%"><p align="center"><strong><font face="Courier New" color="#000000"><big><big><strong>$title</strong></big></big><br>
        </font></strong></td>
      </tr>
    </table>
    </center></div></td>
  </tr>
  <tr>
    <td width="100%"><br>
    <br>
    <div align="center"><center><table border="0" width="60%" height="7" cellspacing="0"
    cellpadding="0">
      <tr>
        <td width="100%" height="62"><form method="POST" action="$scriptname">
          <div align="center"><center><p><input type="submit" name="action"
          value="Create new counter"
          style="background-color: rgb(0,0,128); color: rgb(255,255,255); font-family: Arial; border-left: medium none"><br>
          <font face="Arial">Add a counter to your home page for FREE no programming required.</font></p>
          </center></div>
        </form>
        </td>
      </tr>
      <tr>
        <td width="100%" height="1"><hr width="72%" size="1" color="#008080">
        </td>
      </tr>
      <tr>
        <td width="100%" height="1"><form method="POST" action="$scriptname">
          <div align="center"><center><p><input type="submit" name="action" value="Modify Counter"
          style="background-color: rgb(0,0,128); color: rgb(255,255,255); font-family: Arial"><br>
          <font face="Arial">If you already have an account this is the place to make changes to
          your account.</font></p>
          </center></div>
        </form>
        </td>
      </tr>
    </table>
    </center></div><p><br>
    </p>
    <hr width="72%" size="1" color="#008080">
    <p align="center"><small><font face="Arial Narrow">© 1998-1999 John Quaglieri<br>
    Count 'Em provided by<br>
    <a href="$site">$title</a></font></small><br>
    </td>
  </tr>
</table>
</center></div>

<p>&nbsp;</p>
</body>
</html>
EOF
exit;
}

sub newreal {
 $message = "" ;
 $found_err = "" ;
 
 # Start checking form for errors
 $errmsg = "<p>Field 'url' must be filled in.</p>\n" ;
 
 if ($uurl eq "") {
 	$message = $message.$errmsg ;
 	$found_err = 1 ; }
 
 
 $errmsg = "<p>Field 'username' must be filled in with only the characters a-z, 0-9, and dashes.</p>\n" ;
  if ($name2 eq "") {
 	$message = $message.$errmsg ;
 	$found_err = 1 ; }
 
$errmsg = "<p>Please enter a valid email address</p>\n" ;
 
 if ($email !~ /.+\@.+\..+/) {
 	$message = $message.$errmsg ;
 	$found_err = 1 ; }
 
 
 $errmsg = "<p>Field 'password' must be filled in.</p>\n" ;
 
 if ($password eq "") {
 	$message = $message.$errmsg ;
 	$found_err = 1 ; }
 	
 $errmsg = "<p>Sorry, that username is taken. Pick another.</p>\n" ;
 
 open(DATA,"<$datafile");
 	@subs=<DATA>;
 close(DATA);
 
 foreach $sub(@subs){
 	@subdata = split(/\|/,$sub);
 	if(@subdata[0] eq $name2){
 		$message = $message.$errmsg ;
 		$found_err = 1 ;
 	}	
 }
 
 if ($found_err) {
 	&PrintError; } else {
	open(DATA,">>$datafile");
 		print DATA "$name2|$password|$uurl|$email\n";
 	close(DATA);
open (NEWID,">$name2.dat");
if ($use_flock) {
	flock NEWID, 2; 
}
print NEWID "0";
close (NEWID);

open (ID,"$counts");
if ($use_flock) {
	flock ID, 2; 
}
$new = <ID> ;
$new++;
close (ID);

open (NEW,">$counts");
if ($use_flock) {
	flock NEW, 2; 
}
print NEW "$new";
close (NEW);

if ($use_email) {
    open (MAIL, "| $sendmail -t") || die "I can't open sendmail\n";
    print MAIL "To: $email\n";
    print MAIL "From: $youremail\n";
    print MAIL "Subject: $title Free Counters\n";
    print MAIL "Thanks for signing up for the Our Free Counters:\n";
    print MAIL "Your information is as follows.\n";
    print MAIL "Username: $name2 <br>Password: $password<br>\n";
    print MAIL "You can log in and modify your account at any time.\n";
    print MAIL "$url\n";
    close (MAIL);

}

print<<EOF;
<html><head><title>Counter Created</title></head>
@header<br>
Your new counter has been created.<br>
The code is:<br>
&lt;a href="$site"&gt;&lt;img src="$url?$name2.dat:1"&gt;&lt;/a&gt;<br>
<small>White digits and black background</small><br>
Or<br>
&lt;a href="$site"&gt;&lt;img src="$url?$name2.dat:0"&gt;&lt;/a&gt;<br>
<small>Black digits and whit background</small><br>
<br>
You can modify your account at anytime by logging in.<br>
<form method=POST action="$scriptname">
<input type="submit" name="action" value="Modify Counter"></form><br>
@footer
</body></html>
EOF
exit;
}
}
sub modify {
print<<EOF;
<html><head><title>Modify</title></head>
@header<br>
<form method="POST" action="$scriptname">
UserName: <input type="text" size="20" name="name"><br>
Password: <input type="text" size="20" name="password"><br>
<input type="submit" name="action" value="Modify"></form><br>
@footer
</body></html>
EOF
exit;
}

sub modreal {
open(DATA,"<$datafile");
 	@subs=<DATA>;
 close(DATA);
 
 $i=-1;
 foreach $sub(@subs){
 	$i=$i+1;
 	@subdata = split(/\|/,$sub);
 	if(@subdata[0] eq $name2){
        if(@subdata[1] eq $password){
&userpanel
		} else {
 			
 	print "<HTML><HEAD><TITLE>Error!</TITLE></HEAD>\n";
 	print "<B>There was an error.</b>\n";		
 	print "<P>Wrong password.</p>\n";
 	print "<P>Click the 'Back' button to try again.</P>\n\n";
      exit;
        }
 	}	
    }
}

sub userpanel{
open(DATA,"<$path/$name2.dat");
 	@subs=<DATA>;
 close(DATA);
print<<EOF;
<html><head><title>User Logged in</title></head>
@header<br>
<center>Welcome @subdata[2]<br>
$success<br>
Your current counter value is @subs.<br>
<br>
The code for your counter is:<br>
&lt;a href="$site"&gt;&lt;img src="$url?$name2.dat:1"&gt;&lt;/a&gt;<br>
<small>White digits and black background</small><br>
Or<br>
&lt;a href="$site"&gt;&lt;img src="$url?$name2.dat:0"&gt;&lt;/a&gt;<br>
<small>Black digits and whit background</small><br>
<br>
<B>Change Value</b></center>
<form method="POST" action="$scriptname">
<input type="text" size="7" name="cvalue" value="@subs"><br>
<input type="hidden" name="name" value="@subdata[0]">
<input type="submit" name="action" value="Change"></form>
<br>
<form method="POST" action="$scriptname">
<input type="hidden" name="name" value="@subdata[0]">
<input type="submit" name="action" value="Delete Counter"></form><br>
@footer
</center></body></html>
EOF
exit;
}
sub uchange {
open(DATA,">$name2.dat");
if ($use_flock) {
	flock DATA, 2; 
}
      $temp="$cvalue";
 	print DATA "$temp";
close(DATA);
$success="Counter Value Changed";
&userpanel;
}

sub deletehtm {
print<<EOF;
<html><head><title>Delete Counter</title></head>
@header<br>
Are you sure you want to delete?<br>
<form method="POST" action="$scriptname">
<input type="hidden" name="name" value="$name2">
<input type="submit" name="action" value="Delete"></form>
<center><hr width="75"></center><br>
@footer
</body></html>
EOF
exit;
}

sub delete {
 open(DATA,"<$datafile");
 	@subs=<DATA>;
 close(DATA);
 
 $i=-1;
 foreach $sub(@subs){
 	$i=$i+1;
 	@subdata = split(/\|/,$sub);
 	if(@subdata[0] eq $name2){
 			@subs[$i]="";

 		} 
                
 }
$ii=-1;
 open(DATA,">$datafile");
 while($ii<=$i+1){
 	$ii=$ii+1;
 	$temp="@subs[$ii]";
 	print DATA "$temp";
}
close(DATA);
 unlink "$path/$name2.dat";
print<<EOF;
@header<br>
Your counter has been deleted<br><br>.
<a href="$site">$title</a><br>
@footer<br>
EOF
exit;
}

 sub PrintError { 
 print "Content-type: text/html\n\n";
 	print "<HTML><HEAD><TITLE>Error!</TITLE></HEAD>\n";
 	print "<H1>Error!</H1>\n";		
 	print $message ;
 	print "<P>Click the 'Back' button to try again.</P>";
 exit 0 ;
 return 1 ; 
 }
