#!/usr/bin/perl
# Enter the path to perl above

################################################################
#                                                              #
# Erics CGI World - http://www.EricsCGI.com                    #
# Counter Server V1.0                                          #
#                                                              #
# Usage: Signup: counter.cgi?signup                            #
#        Admin:  counter.cgi?admin                             #
#                                                              #
# Installation Instructions:                                   #
# Upload all files & dirs to a directory of your choice        #
# Edit template html files in the html directory if wanted     #
# CHMOD counter.cgi to 755                                     #
# CHMOD data directory to 777                                  #
#                                                              #
# No editing is required in this script                        #
# YOU MAY NOT DISTRIBUTE THIS SCRIPT UNDER ANY CIRCUMSTANCES   #
#                                                              #
################################################################

$url2countercgi = "http://www.sait.lu/cgi-bin/cserver/counter.cgi";
# Enter the FULL URL to the counter.cgi file

$digisets = '7';
# This allows you to add extra sets of counter digits to the counter server
# Create a folder like the 1 - 6 that comes with the script in sequence like adding
# a folder number 7 and putting digits 0-9.gif in that folder and change $digisets
# to 7

$servicename = 'sait.lu Counter Server';
# Enter the name of your counter service

$path = '/var/www/vhosts/sait.lu/httpdocs/cgi-bin/cserver/data';
# Enter the system path to where the server will store all its data
# DO NOT INCUDE A TRAILING /

$url2digits = "http://www.sait.lu/cgi-bin/cserver/digits";
# Enter the FULL URL to where the digits graphics can be found
# DO NOT INCUDE A TRAILING /

$header = '';
# Enter the system path to a html file to be used as a header for each page
# generated by the script

$footer = '';
# Enter the system path to a html file to be used as a footer for each page
# generated by the script

$hpath = '/var/www/vhosts/sait.lu/httpdocs/cgi-bin/cserver/html';
# Enter the system path to the html directory that came with the counter server
# Does NOT have to be in your public html directory
# This is required, will cause a 500 internal server error if incorrect

$link_text = '';
# This text will be placed under the counter image given to the user
# Preferably a short 15-20 character name

$link_url = '';
# This is the page that will open when $link_text (above) is clicked

$allowlogs = '1';
# This will log the page accessed, ip addy of surfer, the time and the page they came from
# This can be used to track surfers through sites and how long they spent on each page
# Does NOT use cookies and cannot be turned off by the surfer!!
# This can be limited to save disk space
# After this limit is passed the oldest entry will be removed
# example:
# 31/06/00 23:37 | 213.55.76.432 | http://theirsite.com/page.html
# http://theirsite.com/page.html will be left out if $allowic is set to 0

$loglimit = "500";
# Enter the ammount of page hits to log
# The log file can grow to about 50kb with 500 logs

#########################################
# No changes are needed below this line #
#########################################

$fullpath = "$0";
(@split) = split(/\//,$fullpath);

foreach $thing (@split){
$ENV{'FILENAME'} = "$thing";
}

$lofp = length($fullpath);
$lofn = length($ENV{'FILENAME'});
$newlen = $lofp - $lofn;
$newlen--;
substr($fullpath, $newlen, 9999999) = $nothing;

$ENV{'CURRENT_DIR'} = "$fullpath";

if ($path eq "./data"){
$path = "$ENV{'CURRENT_DIR'}/data";
}

if ($hpath eq "./html"){
$hpath = "$ENV{'CURRENT_DIR'}/html";
}

unless (-e "$ENV{'CURRENT_DIR'}/cgi-lib.pl"){
print <<end_of_html;
Content-type: text/html

$header

ERROR: Cannot find $ENV{'CURRENT_DIR'}/cgi-lib.pl
<br>
Please make sure you uploaded it to the same directory as $ENV{'FILENAME'}

$footer

end_of_html
exit;
}

require "$ENV{'CURRENT_DIR'}/cgi-lib.pl";
&ReadParse;

$version = "1.0";

sub htmlver {
$htmlversion = "$html[0]";
$html[0] = "";

$lohv = length($htmlversion);
$lohv--;
$lohv--;
substr($htmlversion, $lohv, 9999999) = $nothing;
substr($htmlversion, 0, 2) = $nothing;
}

if ($ENV{'QUERY_STRING'} eq "debug"){
print qq~

<title>Script debug</title>
~;

  print "<table cellpadding=\"0\" cellspacing=\"0\">\n";
  foreach $key (sort keys %ENV) {
    print "<tr>\n";
    print "<td width=\"200\" valign=\"TOP\"><font face=\"Verdana\" size=\"2\"><b>$key</b></font></td>\n";
    print "<td><font face=\"Verdana\" size=\"2\">".$ENV{$key}."</font></td>\n";
    print "</tr>\n";
  }
  print "</table><p>\n";

  print "<table cellpadding=\"0\" cellspacing=\"0\">\n";
    print "<tr>\n";
    print "<td width=\"200\" valign=\"TOP\"><font face=\"Verdana\" size=\"2\"><b>\$version</b></font></td>\n";
    print "<td><font face=\"Verdana\" size=\"2\">".$version."</font></td>\n";
    print "</tr>\n";

    print "<tr>\n";
    print "<td width=\"200\" valign=\"TOP\"><font face=\"Verdana\" size=\"2\"><b>\$url2countercgi</b></font></td>\n";
    print "<td><font face=\"Verdana\" size=\"2\">".$url2countercgi."</font></td>\n";
    print "</tr>\n";

    print "<tr>\n";
    print "<td width=\"200\" valign=\"TOP\"><font face=\"Verdana\" size=\"2\"><b>\$path</b></font></td>\n";
    print "<td><font face=\"Verdana\" size=\"2\">".$path."</font></td>\n";
    print "</tr>\n";

    print "<tr>\n";
    print "<td width=\"200\" valign=\"TOP\"><font face=\"Verdana\" size=\"2\"><b>\$digisets</b></font></td>\n";
    print "<td><font face=\"Verdana\" size=\"2\">".$digisets."</font></td>\n";
    print "</tr>\n";

    print "<tr>\n";
    print "<td width=\"200\" valign=\"TOP\"><font face=\"Verdana\" size=\"2\"><b>\$url2digits</b></font></td>\n";
    print "<td><font face=\"Verdana\" size=\"2\">".$url2digits."</font></td>\n";
    print "</tr>\n";

    print "<tr>\n";
    print "<td width=\"200\" valign=\"TOP\"><font face=\"Verdana\" size=\"2\"><b>\$header</b></font></td>\n";
    print "<td><font face=\"Verdana\" size=\"2\">".$header."</font></td>\n";
    print "</tr>\n";

    print "<tr>\n";
    print "<td width=\"200\" valign=\"TOP\"><font face=\"Verdana\" size=\"2\"><b>\$footer</b></font></td>\n";
    print "<td><font face=\"Verdana\" size=\"2\">".$footer."</font></td>\n";
    print "</tr>\n";

    print "<tr>\n";
    print "<td width=\"200\" valign=\"TOP\"><font face=\"Verdana\" size=\"2\"><b>\$hpath</b></font></td>\n";
    print "<td><font face=\"Verdana\" size=\"2\">".$hpath."</font></td>\n";
    print "</tr>\n";

    print "<tr>\n";
    print "<td width=\"200\" valign=\"TOP\"><font face=\"Verdana\" size=\"2\"><b>\$allowlogs</b></font></td>\n";
    print "<td><font face=\"Verdana\" size=\"2\">".$allowlogs."</font></td>\n";
    print "</tr>\n";

    print "<tr>\n";
    print "<td width=\"200\" valign=\"TOP\"><font face=\"Verdana\" size=\"2\"><b>\$loglimit</b></font></td>\n";
    print "<td><font face=\"Verdana\" size=\"2\">".$loglimit."</font></td>\n";
    print "</tr>\n";
  print "<table cellpadding=\"0\" cellspacing=\"0\">\n";
exit;
}

if ($in{'id'} eq ""){

#Gets the header and footer code if any
if ($header ne ""){
open(HEADER, "<$header") || die("Can't open $header\n");
@header=<HEADER>;
close(HEADER);
$header = '';
foreach $line (@header){

if ($header ne ""){
$temp = "$header$line";
$header = "$temp";
}

if ($header eq ""){
$header = "$line";
}
}
}

if ($footer ne ""){
open(FOOTER, "<$footer") || die("Can't open $footer\n");
@footer=<FOOTER>;
close(FOOTER);
$footer = '';
foreach $line (@footer){

if ($footer ne ""){
$temp = "$footer$line";
$footer = "$temp";
}

if ($footer eq ""){
$footer = "$line";
}
}
}

# End get header and footer code

if ($in{'command'} eq "viewlogs"){
open (DATA,"$path/$in{'username'}.dat");
$userdata=<DATA>;
close (DATA);

($password,$email,$dtype,$img0,$img1,$img2,$img3,$img4,$img5,$img6,$img7,$img8,$img9) = split(/\|/,$userdata);

if ($password ne "$in{'password'}"){
# gets the html code from the html file and fixes script variables
open(HTML, "<$hpath/admin_incorrect_password.html") || die("Can't open $hpath/admin_incorrect_password.html\n");
@html=<HTML>;
close(HTML);

&htmlver;

foreach $line (@html){
$line =~ s/\$ENV{'FILENAME'}/$ENV{'FILENAME'}/g;
$line =~ s/\$servicename/$servicename/g;
$line =~ s/\$header/$header/g;
$line =~ s/\$footer/$footer/g;
$line =~ s/\$in{'username'}/$in{'username'}/g;
$line =~ s/\$in{'password'}/$in{'password'}/g;

# all html resorting shoud go before this line
if ($html ne ""){
$temp = "$html$line";
$html = "$temp";
}

if ($html eq ""){
$html = "$line";
}

} # end html resorting and compilation, ready to output $html to browser

print <<end_of_html;
Content-type: text/html

$html

end_of_html
exit;
}

open (DATA,"$path/$in{'username'}.log");
$logdata=<DATA>;
close (DATA);

(@logs) = split(/\+/,$logdata);
$logdata = "";
$vlog = "0";
foreach $log (@logs){
$vlog ++;
($date,$time,$ip,$host,$browser) = split(/\|/,$log);
$logdata = "$logdata<tr><td valign=\"top\" align=\"left\">$date</td><td valign=\"top\" align=\"left\">$time</td><td valign=\"top\" align=\"left\">$ip</td><td valign=\"top\" align=\"left\">$host</td><td valign=\"top\" align=\"left\">$browser</td></tr>";
}

# gets the html code from the html file and fixes script variables
open(HTML, "<$hpath/admin_controls_logs.html") || die("Can't open $hpath/admin_controls_logs.html\n");
@html=<HTML>;
close(HTML);

&htmlver;

foreach $line (@html){
$line =~ s/\$ENV{'FILENAME'}/$ENV{'FILENAME'}/g;
$line =~ s/\$servicename/$servicename/g;
$line =~ s/\$header/$header/g;
$line =~ s/\$footer/$footer/g;
$line =~ s/\$in{'username'}/$in{'username'}/g;
$line =~ s/\$in{'password'}/$in{'password'}/g;
$line =~ s/\$logdata/$logdata/g;
$line =~ s/\$vlog/$vlog/g;

# all html resorting shoud go before this line
if ($html ne ""){
$temp = "$html$line";
$html = "$temp";
}

if ($html eq ""){
$html = "$line";
}

} # end html resorting and compilation, ready to output $html to browser

print <<end_of_html;
Content-type: text/html

$html

end_of_html
exit;
}

if ($in{'command'} eq "" && $ENV{'QUERY_STRING'} eq ""){
print <<end_of_html;
Content-type: text/html

$header

ERROR: No command specified!

$footer

end_of_html
exit;
}

if ($in{'command'} eq "savedefine"){
open (DATA,"$path/$in{'username'}.dat");
$userdata=<DATA>;
close (DATA);

($password,$email,$dtype,$img0,$img1,$img2,$img3,$img4,$img5,$img6,$img7,$img8,$img9) = split(/\|/,$userdata);

if ($password ne "$in{'password'}"){
print <<end_of_html;
Content-type: text/html

WRONG PASSWORD!

end_of_html
exit;
}

open (DATA,">$path/$in{'username'}.dat");
print DATA qq~$password|$email|$dtype|$in{'img0'}|$in{'img1'}|$in{'img2'}|$in{'img3'}|$in{'img4'}|$in{'img5'}|$in{'img6'}|$in{'img7'}|$in{'img8'}|$in{'img9'}~;
close (DATA);

# gets the html code from the html file and fixes script variables
open(HTML, "<$hpath/done_define_image_locations.html") || die("Can't open $hpath/done_define_image_locations.html\n");
@html=<HTML>;
close(HTML);

&htmlver;

foreach $line (@html){
$line =~ s/\$ENV{'FILENAME'}/$ENV{'FILENAME'}/g;
$line =~ s/\$servicename/$servicename/g;
$line =~ s/\$header/$header/g;
$line =~ s/\$footer/$footer/g;
$line =~ s/\$in{'username'}/$in{'username'}/g;
$line =~ s/\$in{'password'}/$in{'password'}/g;
$line =~ s/\$in{'img0'}/$in{'img0'}/g;
$line =~ s/\$in{'img1'}/$in{'img1'}/g;
$line =~ s/\$in{'img2'}/$in{'img2'}/g;
$line =~ s/\$in{'img3'}/$in{'img3'}/g;
$line =~ s/\$in{'img4'}/$in{'img4'}/g;
$line =~ s/\$in{'img5'}/$in{'img5'}/g;
$line =~ s/\$in{'img6'}/$in{'img6'}/g;
$line =~ s/\$in{'img7'}/$in{'img7'}/g;
$line =~ s/\$in{'img8'}/$in{'img8'}/g;
$line =~ s/\$in{'img9'}/$in{'img9'}/g;

# all html resorting shoud go before this line
if ($html ne ""){
$temp = "$html$line";
$html = "$temp";
}

if ($html eq ""){
$html = "$line";
}

} # end html resorting and compilation, ready to output $html to browser

print <<end_of_html;
Content-type: text/html

$html

end_of_html
exit;
}

if ($in{'command'} eq "defineimg"){
open (DATA,"$path/$in{'username'}.dat");
$userdata=<DATA>;
close (DATA);

($password,$email,$dtype,$img0,$img1,$img2,$img3,$img4,$img5,$img6,$img7,$img8,$img9) = split(/\|/,$userdata);

if ($password ne "$in{'password'}"){
print <<end_of_html;
Content-type: text/html

WRONG PASSWORD!

end_of_html
exit;
}

# gets the html code from the html file and fixes script variables
open(HTML, "<$hpath/define_image_locations.html") || die("Can't open $hpath/define_image_locations.html\n");
@html=<HTML>;
close(HTML);

&htmlver;

foreach $line (@html){
$line =~ s/\$ENV{'FILENAME'}/$ENV{'FILENAME'}/g;
$line =~ s/\$servicename/$servicename/g;
$line =~ s/\$header/$header/g;
$line =~ s/\$footer/$footer/g;
$line =~ s/\$in{'username'}/$in{'username'}/g;
$line =~ s/\$in{'password'}/$in{'password'}/g;
$line =~ s/\$img0/$img0/g;
$line =~ s/\$img1/$img1/g;
$line =~ s/\$img2/$img2/g;
$line =~ s/\$img3/$img3/g;
$line =~ s/\$img4/$img4/g;
$line =~ s/\$img5/$img5/g;
$line =~ s/\$img6/$img6/g;
$line =~ s/\$img7/$img7/g;
$line =~ s/\$img8/$img8/g;
$line =~ s/\$img9/$img9/g;

# all html resorting shoud go before this line
if ($html ne ""){
$temp = "$html$line";
$html = "$temp";
}

if ($html eq ""){
$html = "$line";
}

} # end html resorting and compilation, ready to output $html to browser

print <<end_of_html;
Content-type: text/html

$html

end_of_html
exit;
}

if ($in{'command'} eq "processadmin"){
open (DATA,"$path/$in{'username'}.dat");
$userdata=<DATA>;
close (DATA);

($password,$email,$dtype,$img0,$img1,$img2,$img3,$img4,$img5,$img6,$img7,$img8,$img9) = split(/\|/,$userdata);

if ($password ne "$in{'password'}"){
# gets the html code from the html file and fixes script variables
open(HTML, "<$hpath/admin_incorrect_password.html") || die("Can't open $hpath/admin_incorrect_password.html\n");
@html=<HTML>;
close(HTML);

&htmlver;

foreach $line (@html){
$line =~ s/\$ENV{'FILENAME'}/$ENV{'FILENAME'}/g;
$line =~ s/\$servicename/$servicename/g;
$line =~ s/\$header/$header/g;
$line =~ s/\$footer/$footer/g;
$line =~ s/\$in{'username'}/$in{'username'}/g;
$line =~ s/\$in{'password'}/$in{'password'}/g;

# all html resorting shoud go before this line
if ($html ne ""){
$temp = "$html$line";
$html = "$temp";
}

if ($html eq ""){
$html = "$line";
}

} # end html resorting and compilation, ready to output $html to browser

print <<end_of_html;
Content-type: text/html

$html

end_of_html
exit;
}

if ($in{'sdtype'} eq "1"){
$dtype = "$in{'psdigit'}";
}

if ($in{'sdtype'} eq "2"){
$dtype = "custom";
}

if ($in{'sdtype'} eq "3"){		################################################################
$dtype = "text";				################################################################
}								################################################################

open (DATA,">$path/$in{'username'}.dat");
print DATA qq~$in{'new_password'}|$in{'email'}|$dtype|$img0|$img1|$img2|$img3|$img4|$img5|$img6|$img7|$img8|$img9~;
close (DATA);

if ($in{'newcount'} ne ""){
open (DATA,">$path/$in{'username'}.cnt");
print DATA qq~$in{'newcount'}~;
close (DATA);
}

# gets the html code from the html file and fixes script variables
open(HTML, "<$hpath/admin_saved.html") || die("Can't open $hpath/admin_saved.html\n");
@html=<HTML>;
close(HTML);

&htmlver;

foreach $line (@html){
$line =~ s/\$ENV{'FILENAME'}/$ENV{'FILENAME'}/g;
$line =~ s/\$servicename/$servicename/g;
$line =~ s/\$header/$header/g;
$line =~ s/\$footer/$footer/g;
$line =~ s/\$in{'username'}/$in{'username'}/g;
$line =~ s/\$url2countercgi/$url2countercgi/g;

# all html resorting shoud go before this line
if ($html ne ""){
$temp = "$html$line";
$html = "$temp";
}

if ($html eq ""){
$html = "$line";
}

} # end html resorting and compilation, ready to output $html to browser

print <<end_of_html;
Content-type: text/html

$html

end_of_html
exit;
}

if ($in{'command'} eq "showpreset"){
print <<end_of_html;
Content-type: text/html

<html>

<head>
<title>Pre-Set Digits</title>
</head>

<body>

<table border="1">

end_of_html

$curimg = "0";

GETIMG: {
$curimg ++;
print <<end_of_html;

<tr>
<td>$curimg</td>
<td><img border="0" src="$url2digits/$curimg/0.gif"><img border="0" src="$url2digits/$curimg/1.gif"><img border="0" src="$url2digits/$curimg/2.gif"><img border="0" src="$url2digits/$curimg/3.gif"><img border="0" src="$url2digits/$curimg/4.gif"><img border="0" src="$url2digits/$curimg/5.gif"><img border="0" src="$url2digits/$curimg/6.gif"><img border="0" src="$url2digits/$curimg/7.gif"><img border="0" src="$url2digits/$curimg/8.gif"><img border="0" src="$url2digits/$curimg/9.gif"></td>
</tr>

end_of_html

if ($curimg ne "$digisets"){
redo GETIMG ;
}
}

print <<end_of_html;

</table>
</body>
</html>

end_of_html
exit;

}

if ($in{'command'} eq "login"){
unless (-e "$path/$in{'username'}.dat"){
# gets the html code from the html file and fixes script variables
open(HTML, "<$hpath/not_found.html") || die("Can't open $hpath/not_found.html\n");
@html=<HTML>;
close(HTML);

&htmlver;

foreach $line (@html){
$line =~ s/\$ENV{'FILENAME'}/$ENV{'FILENAME'}/g;
$line =~ s/\$servicename/$servicename/g;
$line =~ s/\$header/$header/g;
$line =~ s/\$footer/$footer/g;
$line =~ s/\$in{'username'}/$in{'username'}/g;

# all html resorting shoud go before this line
if ($html ne ""){
$temp = "$html$line";
$html = "$temp";
}

if ($html eq ""){
$html = "$line";
}

} # end html resorting and compilation, ready to output $html to browser

print <<end_of_html;
Content-type: text/html

$html

end_of_html
exit;
}

open (DATA,"$path/$in{'username'}.dat");
$userdata=<DATA>;
close (DATA);

($password,$email,$dtype,$img0,$img1,$img2,$img3,$img4,$img5,$img6,$img7,$img8,$img9) = split(/\|/,$userdata);

if ($password ne "$in{'password'}"){
# gets the html code from the html file and fixes script variables
open(HTML, "<$hpath/admin_incorrect_password.html") || die("Can't open $hpath/admin_incorrect_password.html\n");
@html=<HTML>;
close(HTML);

&htmlver;

foreach $line (@html){
$line =~ s/\$ENV{'FILENAME'}/$ENV{'FILENAME'}/g;
$line =~ s/\$servicename/$servicename/g;
$line =~ s/\$header/$header/g;
$line =~ s/\$footer/$footer/g;
$line =~ s/\$in{'username'}/$in{'username'}/g;
$line =~ s/\$in{'password'}/$in{'password'}/g;

# all html resorting shoud go before this line
if ($html ne ""){
$temp = "$html$line";
$html = "$temp";
}

if ($html eq ""){
$html = "$line";
}

} # end html resorting and compilation, ready to output $html to browser

print <<end_of_html;
Content-type: text/html

$html

end_of_html
exit;
}

# gets the html code from the html file and fixes script variables
open(HTML, "<$hpath/admin.html") || die("Can't open $hpath/admin.html\n");
@html=<HTML>;
close(HTML);

if ($dtype eq "text"){		################################################################################
$ps3 = " checked";			################################################################################
}							################################################################################
else{						################################################################################
if ($dtype eq "custom"){
$ps2 = " checked";
}

if ($dtype ne "custom"){
$ps1 = " checked";
}

$curopt = "0";
GETOPT: {
$curopt ++;

if ($dtype ne "$curopt"){
$psdigitopt = "$psdigitopt<option>$curopt</option>";
}

if ($dtype eq "$curopt"){
$psdigitopt = "$psdigitopt<option selected>$curopt</option>";
}

if ($curopt ne "$digisets"){
redo GETOPT ;
}
}
}							##################################################################################

&htmlver;

foreach $line (@html){
$line =~ s/\$ENV{'FILENAME'}/$ENV{'FILENAME'}/g;
$line =~ s/\$servicename/$servicename/g;
$line =~ s/\$header/$header/g;
$line =~ s/\$footer/$footer/g;
$line =~ s/\$in{'username'}/$in{'username'}/g;
$line =~ s/\$in{'password'}/$in{'password'}/g;
$line =~ s/\$email/$email/g;
$line =~ s/\$url2countercgi/$url2countercgi/g;
$line =~ s/\$ps1/$ps1/g;
$line =~ s/\$ps2/$ps2/g;
$line =~ s/\$ps3/$ps3/g;	#################################################################################
$line =~ s/\$psdigitopt/$psdigitopt/g;

# all html resorting shoud go before this line
if ($html ne ""){
$temp = "$html$line";
$html = "$temp";
}

if ($html eq ""){
$html = "$line";
}

} # end html resorting and compilation, ready to output $html to browser

if ($allowlogs ne "1"){
($html1,$html2,$html3) = split(/\<!"CounterLogs">/,$html);
$html = "$html1$html3";
}

print <<end_of_html;
Content-type: text/html

$html

end_of_html
exit;
}

if ($ENV{'QUERY_STRING'} eq "admin"){
# gets the html code from the html file and fixes script variables
open(HTML, "<$hpath/pre_admin.html") || die("Can't open $hpath/pre_admin.html\n");
@html=<HTML>;
close(HTML);

&htmlver;

foreach $line (@html){
$line =~ s/\$ENV{'FILENAME'}/$ENV{'FILENAME'}/g;
$line =~ s/\$servicename/$servicename/g;
$line =~ s/\$header/$header/g;
$line =~ s/\$footer/$footer/g;

# all html resorting shoud go before this line
if ($html ne ""){
$temp = "$html$line";
$html = "$temp";
}

if ($html eq ""){
$html = "$line";
}

} # end html resorting and compilation, ready to output $html to browser

print <<end_of_html;
Content-type: text/html

$html

end_of_html
exit;
}

if ($in{'command'} eq "signup"){

# Checks if user already exists
if (-e "$path/$in{'username'}.dat"){
$name_taken = "1";
}

# Checks if email address is in the correct format
($email_split1,$email_split2)=split (/\@/,$in{'email'});

if ($email_split2 eq ""){
$email_error = "1";
}

if ($error ne "1"){
($split1,$split2)=split (/\./,$email_split2);
if ($split2 eq ""){
$email_error = "1";
}
}

if ($in{'username'} !~ /^[a-z0-9\\-\_]*$/i){
$error = "1";
$temp = "Username contains invalid characters<br>$et";
$et = "$temp";
}

if ($name_taken eq "1"){
$error = "1";
$temp = "That username is in use by someone else<br>$et";
$et = "$temp";
}

if ($in{'email'} eq ""){
$error = "1";
$temp = "Your email address is required<br>$et";
$et = "$temp";
}

if ($email_error eq "1"){
$error = "1";
$temp = "Your email address is not in the correct format<br>$et";
$et = "$temp";
}

if ($in{'password'} ne "$in{'confirm_password'}"){
$error = "1";
$temp = "Password & Confirm Password do not match<br>$et";
$et = "$temp";
}

if ($in{'confirm_password'} eq ""){
$error = "1";
$temp = "Confirm Password is a required field<br>$et";
$et = "$temp";
}

if ($in{'password'} eq ""){
$error = "1";
$temp = "Password is a required field<br>$et";
$et = "$temp";
}

if ($in{'username'} eq ""){
$error = "1";
$temp = "Username is a required field<br>$et";
$et = "$temp";
}

if ($error eq "1"){
# An error was found, begin compiling error page
# gets the html code from the html file and fixes script variables
open(HTML, "<$hpath/signup_error.html") || die("Can't open $hpath/signup_error.html\n");
@html=<HTML>;
close(HTML);

&htmlver;

foreach $line (@html){
$line =~ s/\$ENV{'FILENAME'}/$ENV{'FILENAME'}/g;
$line =~ s/\$servicename/$servicename/g;
$line =~ s/\$header/$header/g;
$line =~ s/\$footer/$footer/g;
$line =~ s/\$et/$et/g;

# all html resorting shoud go before this line
if ($html ne ""){
$temp = "$html$line";
$html = "$temp";
}

if ($html eq ""){
$html = "$line";
}

} # end html resorting and compilation, ready to output $html to browser

print <<end_of_html;
Content-type: text/html

$html

end_of_html
exit;
}

# No errors were found
# Begin creating user data files

open (COUNTERDATA,">$path/$in{'username'}.dat");
print COUNTERDATA qq~$in{'password'}|$in{'email'}|6~;
close (COUNTERDATA);

open (DATA,">$path/$in{'username'}.log");
print DATA qq~~;
close (DATA);

open (DATA,">$path/$in{'username'}.cnt");
print DATA qq~0~;
close (DATA);

`chmod 777 $path/$in{'username'}.dat`;
`chmod 777 $path/$in{'username'}.log`;
`chmod 777 $path/$in{'username'}.cnt`;

# gets the html code from the html file and fixes script variables
open(HTML, "<$hpath/signup_complete.html") || die("Can't open $hpath/signup_complete.html\n");
@html=<HTML>;
close(HTML);

&htmlver;

foreach $line (@html){
$line =~ s/\$ENV{'FILENAME'}/$ENV{'FILENAME'}/g;
$line =~ s/\$servicename/$servicename/g;
$line =~ s/\$header/$header/g;
$line =~ s/\$footer/$footer/g;
$line =~ s/\$in{'username'}/$in{'username'}/g;
$line =~ s/\$in{'password'}/$in{'password'}/g;

# all html resorting shoud go before this line
if ($html ne ""){
$temp = "$html$line";
$html = "$temp";
}

if ($html eq ""){
$html = "$line";
}

} # end html resorting and compilation, ready to output $html to browser

print <<end_of_html;
Content-type: text/html

$html

end_of_html
exit;
}

if ($ENV{'QUERY_STRING'} eq "signup"){
# gets the html code from the html file and fixes script variables
open(HTML, "<$hpath/signup.html") || die("Can't open $hpath/signup.html\n");
@html=<HTML>;
close(HTML);

&htmlver;

foreach $line (@html){
$line =~ s/\$ENV{'FILENAME'}/$ENV{'FILENAME'}/g;
$line =~ s/\$servicename/$servicename/g;
$line =~ s/\$header/$header/g;
$line =~ s/\$footer/$footer/g;

# all html resorting shoud go before this line
if ($html ne ""){
$temp = "$html$line";
$html = "$temp";
}

if ($html eq ""){
$html = "$line";
}

} # end html resorting and compilation, ready to output $html to browser

print <<end_of_html;
Content-type: text/html

$html

end_of_html
exit;
}

print <<end_of_html;
Content-type: text/html

$header

ERROR: Unknown command specified!

$footer

end_of_html
exit;

# End if ($in{'id'} eq "")
}

print "Content-type: text/html\n\n";

unless (-e "$path/$in{'id'}.dat"){
print qq~document.write\('<b>COUNTER SERVER ERROR: Cannot find $in{'id'}</b>'\)\;~;
exit;
}

if ($allowlogs eq "1"){

@months=('01','02','03','04','05','06','07','08','09','10','11','12');
@days=('Sun','Mon','Tues','Wed','Thurs','Fri','Sat');
($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
 if ($sec < 10) { $sec = "0$sec";}
 if ($min < 10) { $min = "0$min";}
 if ($hour < 10) { $hour = "0$hour";}
 if ($mday < 10) { $mday = "0$mday";}

substr($year, 0, 1) = $nothing;
$date = "$mday/$months[$mon]/$year";
$time = "$hour:$min";

open (FILE,"$path/$in{'id'}.log");
$logfile=<FILE>;
close (FILE);

$new_log = "$date|$time|$ENV{'REMOTE_ADDR'}|$ENV{'REMOTE_HOST'}|$ENV{'HTTP_USER_AGENT'}+$logfile";
(@logs) = split(/\+/,$new_log);

$currlog = "0";
$new_log = "";
foreach $log (@logs){
if ($currlog ne "$loglimit"){
$currlog ++;
$new_log = "$new_log+$log";
}
}

substr($new_log, 0, 1) = $nothing;

open (FILE,">$path/$in{'id'}.log");
print FILE qq~$new_log~;
close (FILE);
}

open (FILE,"$path/$in{'id'}.dat");
$userdata=<FILE>;
close (FILE);

($password,$email,$dtype,$img0,$img1,$img2,$img3,$img4,$img5,$img6,$img7,$img8,$img9) = split(/\|/,$userdata);

open (FILE,"$path/$in{'id'}.cnt");
$count=<FILE>;
close (FILE);

$count ++;

open (COUNTER,">$path/$in{'id'}.cnt");
print COUNTER qq~$count~;
close (COUNTER);

if (length($count) < '6'){
$null = 6 - length($count);
for ($i=0;$i<$null;$i++){
$count = '0'.$count;
}
}

if ($dtype eq "text"){			#######################################################
$output = $count;				#######################################################
}								#######################################################
else{							#######################################################
if ($dtype eq "custom"){
$img{'0'} = "$img0";
$img{'1'} = "$img1";
$img{'2'} = "$img2";
$img{'3'} = "$img3";
$img{'4'} = "$img4";
$img{'5'} = "$img5";
$img{'6'} = "$img6";
$img{'7'} = "$img7";
$img{'8'} = "$img8";
$img{'9'} = "$img9";
}

if ($dtype ne "custom"){
$img{'0'} = "$url2digits/$dtype/0.gif";
$img{'1'} = "$url2digits/$dtype/1.gif";
$img{'2'} = "$url2digits/$dtype/2.gif";
$img{'3'} = "$url2digits/$dtype/3.gif";
$img{'4'} = "$url2digits/$dtype/4.gif";
$img{'5'} = "$url2digits/$dtype/5.gif";
$img{'6'} = "$url2digits/$dtype/6.gif";
$img{'7'} = "$url2digits/$dtype/7.gif";
$img{'8'} = "$url2digits/$dtype/8.gif";
$img{'9'} = "$url2digits/$dtype/9.gif";
}


(@count) = split(//,$count);

foreach $number (@count){
$output = "$output<img border=0 src\=\"$img{$number}\">";
}
}								##########################################################

if ($link_text ne "" && $link_url ne ""){
$done = "1";
print qq~document.write\('<a href="$link_url" target="_blank">$output<br>$link_text</a>'\)\;~;
}
else {
print qq~document.write\('$output'\)\;~;
}
exit;