#!/usr/local/bin/perl
##-----------------------------------------------------------------##
##                                                                 ##
##                                                                 ##
## © Copyright Mr Lyle R Hopkins 1999. All rights reserved. No part##
## of this or any of the attached documents shall be               ##
## reproduced/stored in any way whatsoever without written         ##
## permission from the Copyright holder.                           ##
##   The Copyright holder holds no responsibility for errors or    ##
## omissions. No liability is assumed in any way for damages       ##
## resulting from the use of this document/program.                ##
##                                                                 ##
## Have a nice day.                                                ##
##                                                                 ##
##                                                                 ##
##-----------------------------------------------------------------##

## By Lyle Hopkins ##



##################################################
######################## Define variables
##################################################

$programurl = "http://ukzone.hypermart.net/scripts/demos/cgi-bin";
$resettimer = 1; ## 1 for by day, 0 for by month. Resets site hits.
$resetpage = 1; ## How often to update the page, 1 for half hour, 0 for hour.
$pagename = "toplist.html"; ## The toplist file to create
$pagetopromote = "http://ukzone.hypermart.net/scripts/demos/cgi-bin/toplist.html"; ## Location of your toplist page

$sloganfont = "Impact";
$slogancolour = "#0000ff";
$slogansize  = "+1";
$hitsfont = "Ariel";
$hitscolour = "#ff0000";
$hitssize = "+1";


##################################################
######################## Other variables
##################################################

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); ## Get date

if ($mon < 10) {
      $mon = "0$mon";
   }
if ($mday < 10) {
      $mday = "0$mday";
   }
$month = ($mon + 1);

$date = "$month¦$mday¦$year"; ## Get date

chop($date) if ($date =~ /\n$/); ## clean up date

@months = ("january","february","march","april","may","june","july","august","september","october","november","december");

## use $months[$mon] to get month name

@values = split(/\&/,$ENV{'QUERY_STRING'});
foreach $i (@values) {
($varname, $mydata) = split(/=/,$i); ## 50
$WHATWANT{$varname} = $mydata;
}

$hitsb = 0;
$userb = "";
$urlb = "";
$nameb = "";
$sloganb = "";


##################################################
######################## Reset Check
##################################################

if ($resettimer) {
open (LASTTIME,"<timer.txt");
@lastday = <LASTTIME>;
close (LASTTIME);
$lastday = join('',@lastday);
chomp($lastday);
if ($date ne $lastday) {

## Save new date

open(OUTF,">timer.txt"); ##  Banner file for appending

print OUTF
"$date\n"; ## Write user fields

close(OUTF); ## Close file

&resetlist();

}
} ## End if resettimer
else {
open (LASTTIME,"<timer.txt");
@lastday = <LASTTIME>;
close (LASTTIME);
$lastday = join('',@lastday);
chomp($lastday);
$month2 = split(/¦/,$lastday);
if ($month ne $month2) {

## Save new date

open(OUTF,">timer.txt"); ##  Banner file for appending

print OUTF
"$date\n"; ## Write user fields

close(OUTF); ## Close file

&resetlist();

}
} ## End else


##################################################
######################## See what to do
##################################################

if ($WHATWANT{'user'} ne "") {
      &addhits();
   }
if ($WHATWANT{'run'} eq "showlist") {
      &showlist();
   }
if ($WHATWANT{'run'} eq "addsite") {
	print "Content-type: text/html\n\n";
      &addsite();
   }
if ($WHATWANT{'run'} eq "usermodify") {
	print "Content-type: text/html\n\n";
      &usermodify();
   }
if ($WHATWANT{'run'} eq "admin") {
	print "Content-type: text/html\n\n";
      &admin();
   }
if ($WHATWANT{'run'} eq "remove") {
	print "Content-type: text/html\n\n";
      &remove();
   }
if ($WHATWANT{'run'} eq "enteradmin") {
	print "Content-type: text/html\n\n";
   ## Get user input
   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;
   $FORM{$name} = $value;
   }
        ## Get file Username and password
   open(INF,"adminpass.txt"); ## Open read file
   $userpass = <INF>; ## Put into an array
   close(INF); ## Close file
   chomp($userpass);
   ($username, $password) = split(/:/, $userpass);
   $enterpasscrypt = crypt($FORM{'password'}, substr($password, 0, 2));
        ## If they match
   if ($FORM{'username'} eq $username && $enterpasscrypt eq $password){
      &enteradmin();
   } ## End if
   }
exit;


##################################################
######################## Actions to take
##################################################

##################################################
######################## Add to site hits
##################################################

sub addhits {

open(INF,"toplist.txt"); ## Open read file
@userdata = <INF>; ## Put into an array
close(INF); ## Close file
foreach $line (@userdata) {
chomp($line);
($hitsb, $userb, $urlb, $nameb, $sloganb, $emailb, $passwordb) = split(/¦/, $line);
$TOPLISTUSER{$userb} = "$hitsb¦$userb¦$urlb¦$nameb¦$sloganb¦$emailb¦$passwordb";
}

if ($TOPLISTUSER{$WHATWANT{'user'}} ne "") {

($hitsb, $userb, $urlb, $nameb, $sloganb, $emailb, $passwordb) = split(/¦/, $TOPLISTUSER{$WHATWANT{'user'}});

$hitsb++;

$TOPLISTUSER{$WHATWANT{'user'}} = "$hitsb¦$userb¦$urlb¦$nameb¦$sloganb¦$emailb¦$passwordb";

open(OUTF,">toplist.txt"); ##  File for appending

foreach $key (keys(%TOPLISTUSER)) {
print OUTF
"$TOPLISTUSER{$key}\n"; ## Write user fields
} ## End loop

close(OUTF); ## Close file

} ## End if

&showlist(); ## Show topsite page

} ## End sub


##################################################
######################## Display toplist
##################################################

sub showlist {


######################## Write page ###########################

if ($min < 30) { 
$mintimer = 1;
}
else {
$mintimer = 0;
}

if ($resetpage) {
open (LASTTIME,"<pagetimer.txt");
@lastday = <LASTTIME>;
close (LASTTIME);
$lastday = join('',@lastday);
chomp($lastday);
($min2, $hour2) = split(/¦/,$lastday);
if ($min2 ne $mintimer) {

## Save new date

open(OUTF,">pagetimer.txt"); ##  Banner file for appending

print OUTF
"$mintimer¦$hour\n"; ## Write user fields

close(OUTF); ## Close file

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

} ## End if
if ($min2 eq $mintimer) {
print "Location: $pagetopromote\n\n";
exit;
} ## End if
} ## End if resettimer
if ($resetpage == 0) {
open (LASTTIME,"<pagetimer.txt");
@lastday = <LASTTIME>;
close (LASTTIME);
$lastday = join('',@lastday);
chomp($lastday);
($min2, $hour2) = split(/¦/,$lastday);
if ($hour2 ne $hour) {

## Save new date

open(OUTF,">pagetimer.txt"); ##  Banner file for appending

print OUTF
"$mintimer¦$hour\n"; ## Write user fields

close(OUTF); ## Close file

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

}
if ($hour2 ne $hour) {
print "Location: $pagetopromote\n\n";
exit;
} ## End if
} ## End if


$linenum = 1;
$mosthits = 0;
open(INF,"toplist.txt"); ## Open read file
@userdata = <INF>; ## Put into an array
close(INF); ## Close file
foreach $line (@userdata) {
chomp($line);
($hitsb, $userb, $urlb, $nameb, $sloganb, $emailb, $passwordb) = split(/¦/, $line);
$SCORELIST .= "¦$hitsb";
$TOPLISTLINE{$linenum} = $line;
$linenum++;
}

&top();

print <<EndHTML;

<center><table BORDER WIDTH="100%" >

EndHTML
;

$wholepage .= "<center><table BORDER WIDTH=\"100%\">";

@SCORELIST = split(/¦/, $SCORELIST);

@SCORELIST = sort { $a <=> $b }@SCORELIST;
@SCORELIST = reverse(@SCORELIST);


foreach $hitnumber (@SCORELIST) { ## Start main loop

$HITLISTRUN = 0;
%HITLIST = "";
if ($hitnumber ne 0) {
  foreach $key (keys(%TOPLISTLINE)) {
  ($hitsb, $userb, $urlb, $nameb, $sloganb, $emailb, $passwordb) = split(/¦/, $TOPLISTLINE{$key});
    if ($hitsb == $hitnumber) {
      if ($TOPLISTLINE{$key} ne "") {
	  &style1();
        $TOPLISTLINE{$key} = "";
      }
    }
  } ## End loop
} ## End if
} ## End loop


print <<EndHTML;

</table></center>

EndHTML
;

$wholepage .= "</table></center>";

&bottom();


} ## End sub


##################################################
######################## Top and Tail
##################################################

sub top {
        open (FILE, "top.txt");
        @top = <FILE>;
        close (FILE);
        foreach $line (@top) {
        print "$line";
	  $wholepage .= "$line\n";
        }
}


sub bottom {
        open (FILE, "bottom.txt");
        @bottom = <FILE>;
        close (FILE);
        foreach $line (@bottom) {
        print "$line";
	  $wholepage .= "$line\n";
        }


print <<EndHTML;

<center><table BORDER WIDTH="150" >
<tr>
<td>
<center><font face="Comic Sans MS"><font color="#000099"><font size=-2>This
script &amp; more available from</font></font></font>
<br><font face="Comic Sans MS"><font color="#FF0000"><font size=-2>-Lyles
Script archive-</font></font></font>
<br><font face="Comic Sans MS"><font color="#000099"><font size=-2><a href="http://ukzone.hypermart.net/scripts">Click
here</a></font></font></font></center>
</td>
</tr>
</table></center>

EndHTML
;

$wholepage .= "<center><table BORDER WIDTH=\"150\" >
<tr>
<td>
<center><font face=\"Comic Sans MS\"><font color=\"#000099\"><font size=-2>This
script &amp; more available from</font></font></font>
<br><font face=\"Comic Sans MS\"><font color=\"#FF0000\"><font size=-2>-Lyles
Script archive-</font></font></font>
<br><font face=\"Comic Sans MS\"><font color=\"#000099\"><font size=-2><a href=\"http://ukzone.hypermart.net/scripts\">Click
here</a></font></font></font></center>
</td>
</tr>
</table></center>
";

open(OUTF,">$pagename"); ##  Banner file for appending

print OUTF
"$wholepage"; ## Write user fields

close(OUTF); ## Close file

}


##################################################
######################## Style 1
##################################################

sub style1 {

print <<EndHTML;
<tr>
<td><font face="$sloganfont"><font color="$slogancolour"><font size=$slogansize><a href="$urlb">$nameb</a>
$sloganb</font></font></font></td>

<td><font face="$hitsfont"><font color="$hitscolour"><font size=$hitssize>$hitsb</font></font></font></td>
</tr>
EndHTML
;

## Page maker

$wholepage .= "<tr>
<td><font face=\"$sloganfont\"><font color=\"$slogancolour\"><font size=$slogansize><a href=\"$urlb\">$nameb</a>
$sloganb</font></font></font></td>

<td><font face=\"$hitsfont\"><font color=\"$hitscolour\"><font size=$hitssize>$hitsb</font></font></font></td>
</tr>";

} ## End sub


##################################################
######################## Add site
##################################################

sub addsite {

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;
$FORM{$name} = $value;
}

$pwd = (getpwuid($<))[1];
$new_password = crypt($FORM{'password'},substr($pwd, 0, 2)); ## encrypt password

open(OUTF,">>toplist.txt"); ##  File for appending

print OUTF
"0¦$FORM{'username'}¦$FORM{'url'}¦$FORM{'name'}¦$FORM{'slogan'}¦$FORM{'email'}¦$new_password\n"; ## Write user fields
close(OUTF); ## Close file

} ## End sub


##################################################
######################## User modify
##################################################

sub usermodify {

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;
$FORM{$name} = $value;
}

open(INF,"toplist.txt"); ## Open read file
@userdata = <INF>; ## Put into an array
close(INF); ## Close file
foreach $line (@userdata) {
chomp($line);
($hitsb, $userb, $urlb, $nameb, $sloganb, $emailb, $passwordb) = split(/¦/, $line);
$TOPLISTHITS{$userb} = "$hitsb¦$userb¦$urlb¦$nameb¦$sloganb¦$emailb¦$passwordb";
}


$test_passwd = crypt($FORM{'password'}, substr($passwordb, 0, 2)); ## see if input password matches one on file
if ($test_passwd ne $passwordb) {
print "Incorrect password\n";
exit;
}

print <<EndHTML;

<p align="center"><font color="#0000FF" size="4"
face="Comic Sans MS">Modify your info</font></p>
<p align="center">&nbsp;</p>
<form action="$programurl/topsite.cgi?run=usermodify2" method="POST">
    <p align="center"><font color="#0000FF" size="4"
    face="Comic Sans MS">Site name</font><input type="text"
    size="112" maxlength="20" name="name" value="$nameb"></p>
    <p align="center"><font color="#0000FF" size="4"
    face="Comic Sans MS">Site slogan</font><input type="text"
    size="112" maxlength="60" name="slogan" value="$sloganb"></p>
    <p align="center"><font color="#0000FF" size="4"
    face="Comic Sans MS">Site URL</font><input type="text"
    size="112" name="url" value="$urlb"></p>
    <p align="center"><font color="#0000FF" size="4"
    face="Comic Sans MS">E-mail</font><input type="text"
    size="54" name="email" value="$emailb"></p>
   <input type="hidden" size="54" name="user" value="$userb">
    <p align="center"><input type="submit" value="Submit"><input
    type="reset" value="Reset"></p>
</form>

EndHTML
;

} ## End sub


##################################################
######################## User modify2
##################################################

sub usermodify2 {

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;
$FORM{$name} = $value;
}

open(INF,"toplist.txt"); ## Open read file
@userdata = <INF>; ## Put into an array
close(INF); ## Close file
foreach $line (@userdata) {
chomp($line);
($hitsb, $userb, $urlb, $nameb, $sloganb, $emailb, $passwordb) = split(/¦/, $line);
$TOPLISTHITS{$userb} = "$hitsb¦$userb¦$urlb¦$nameb¦$sloganb¦$emailb¦$passwordb";
}

($hitsb, $userb, $urlb, $nameb, $sloganb, $emailb, $passwordb) = split(/¦/, $TOPLISTHITS{$FORM{'user'}});

$TOPLISTHITS{$FORM{'user'}} = "$hitsb¦$userb¦$FORM{'url'}¦$FORM{'name'}¦$FORM{'slogan'}¦$FORM{'email'}¦$passwordb";

open(OUTF,">toplist.txt"); ##  File for appending
foreach $key (keys(%TOPLISTHITS)) {
print OUTF
"$TOPLISTHITS{$key}\n"; ## Write user fields
} ## End loop
close(OUTF); ## Close file

print "Done";

} ## End sub


##################################################
######################## Enter Admin
##################################################

sub admin {

print <<EndHTML;

<p align="center"><font size="6">Enter password to enter Admin
area</font></p>

<p align="center">&nbsp;</p>

<form action="$programurl/topsite.cgi?run=enteradmin" method="POST">
    <p align="center">Username <input type="text" size="20"
    name="username"></p>
    <p align="center">Password <input type="text" size="20"
    name="password"></p>
    <p align="center"><input type="submit" value="Enter"></p>
</form>

EndHTML
;

exit;
} ## End sub


##################################################
######################## The page
##################################################

sub enteradmin {

$linenum = 0;
open(INF,"toplist.txt"); ## Open read file
@userdata = <INF>; ## Put into an array
close(INF); ## Close file
foreach $line (@userdata) {
chomp($line);
($hitsb, $userb, $urlb, $nameb, $sloganb, $emailb, $passwordb) = split(/¦/, $line);
$TOPLISTLINE{$linenum} = $line;
$linenum++;
}

print <<EndHTML;
<CENTER><B><FONT FACE="Arial,Helvetica"><FONT COLOR="#FF0000">Site's in
list</FONT></FONT></B></CENTER>
<CENTER>&nbsp;</CENTER>
EndHTML
;


foreach $key (keys(%TOPLISTLINE)) {

($hitsb, $userb, $urlb, $nameb, $sloganb, $emailb, $passwordb) = split(/¦/, $TOPLISTLINE{$key});

print <<EndHTML;
<CENTER><B><FONT FACE="Arial,Helvetica"><FONT COLOR="#FF0000"><FONT SIZE=+1>$key
</FONT>$nameb $sloganb $urlb $emailb</FONT></FONT></B></CENTER>
EndHTML
;

} ## End loop

print <<EndHTML;



<CENTER>&nbsp;</CENTER>
<CENTER>&nbsp;</CENTER>
<FORM method="POST" action="$programurl/topsite.cgi?run=remove">
<CENTER>Remove link number:-</CENTER>
<CENTER><INPUT type="text" size="4" name="looselink"></CENTER>
<CENTER><INPUT type="submit" value="Remove"></CENTER>
</FORM>



EndHTML
;

} ## End sub


##################################################
######################## Remove link
##################################################

sub remove {

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;
$FORM{$name} = $value;
}

$linenum = 0;
open(INF,"toplist.txt"); ## Open read file
@userdata = <INF>; ## Put into an array
close(INF); ## Close file
foreach $line (@userdata) {
chomp($line);
($hitsb, $userb, $urlb, $nameb, $sloganb, $emailb, $passwordb) = split(/¦/, $line);
$TOPLISTLINE{$linenum} = $line;
$linenum++;
}


open(OUTF,">toplist.txt"); ##  Banner file for appending

foreach $key (keys(%TOPLISTLINE)) {

if ($key ne $FORM{'looselink'}) {
print OUTF
"$TOPLISTLINE{$key}\n"; ## Write user fields
} ## End if

} ## End loop

close(OUTF); ## Close file

} ## End sub


##################################################
######################## Reset toplist
##################################################

sub resetlist {

$linenum = 0;
open(INF,"toplist.txt"); ## Open read file
@userdata = <INF>; ## Put into an array
close(INF); ## Close file
foreach $line (@userdata) {
chomp($line);
($hitsb, $userb, $urlb, $nameb, $sloganb, $emailb, $passwordb) = split(/¦/, $line);
$TOPLISTLINE{$linenum} = "0¦$userb¦$urlb¦$nameb¦$sloganb¦$emailb¦$passwordb";
$linenum++;
}


open(OUTF,">toplist.txt"); ##  Banner file for appending

foreach $key (keys(%TOPLISTLINE)) {

print OUTF
"$TOPLISTLINE{$key}\n"; ## Write user fields

} ## End loop

close(OUTF); ## Close file

} ## End sub




