Gossamer Forum
Home : General : Perl Programming :

topsites script?

Quote Reply
topsites script?
can alex write a topsite script? i bet it will rock all other topsites scripts out there
Quote Reply
Re: topsites script? In reply to
I got one from somewhere on the 'net. Modified it a little... take a look at
http://find.virtualave.net/topsites/

pretty cool, huh? Smile
Quote Reply
Re: topsites script? In reply to
Would you be willing to post the sourcecode?
Quote Reply
Re: topsites script? In reply to
topsites.cgi Smile
=============================================
#!/usr/bin/perl
#

require "variables.pl";


$footerstuff = "$variables{'footerstuff'}";
$num_to_show = "$variables{'num_to_show'}";
$num_banners = "$variables{'num_banners'}";
$topsite_location="$variables{'topsite_location'}";
$topsite_name="$variables{'topsite_name'}";
$use_flock = "$variables{'use_flock'}";
$path_to_topsites = "$variables{'path_to_topsites'}";
$update_time = "$variables{'update_time'}";
$topsite_location_cgi="$variables{'topsite_location_cgi'}";

$pasha_vab = qq~
I use this space to show Virtualave.net banners ...
~;

$pasha_site_menu = qq~
I use this space to show some menu on the left ...
~;


$current_time = time();

$temp=$ENV{'QUERY_STRING'};
@pairs=split(/&/,$temp);
foreach $item(@pairs) {
($key,$content)=split (/=/,$item,2);
$content=~tr/+/ /;
$content=~ s/%(..)/pack("c",hex($1))/ge;
$fields{$key}=$content;
}
$r=0;
if ($fields{'ID'} == 0) {
open (DAT,"<members.db");
if ($use_flock) {
flock DAT, 2;
}
@database_array = <DAT>;
close (DAT);
&thehtml;
}
unless ($fields{'ID'}) {

print "Location: $topsite_location/index.htm\n\n";
exit;
}

open (DAT,"<members.db");
if ($use_flock) {
flock DAT, 2;
}
@database_array = <DAT>;
close (DAT);


&member_in;
exit;

##### READS MEMBER ID AND UPDATES HIT COUNT

sub member_in {

open (DAT,"+<members.db");
if ($use_flock) {
flock DAT, 2;
}
@database_array = <DAT>;

$member = $fields{'ID'};
$found=0;
$checkid = $member;
$a=0;
foreach (@database_array) {
@member_array = split(/\|/,$database_array[$a]);
if ($member_array[9] == $checkid) {
@members_array = @member_array;
$found=1;
$member = $a;
}
$a++;
}
if ($found) {
if ($members_array[7] == 0) {
$members_array[7] = $current_time;
}
$members_array[1]++;
$database_array[$member] = join("\|",@members_array);

seek (DAT, 0, 0);
print DAT @database_array;
close (DAT);

}
else {
close (DAT);
}

open (LASTTIME,"<time.txt");
if ($use_flock) {
flock LASTTIME, 2;
}
$last = <LASTTIME>;
close (LASTTIME);
$doit = $current_time - $last;
if ($doit > $update_time) {
open (NEWTIME,">time.txt") &#0124; &#0124; &error("unable to write to time.txt");
if ($use_flock) {
flock NEWTIME, 2;
}
print NEWTIME "$current_time";
close (NEWTIME);
&thehtml
}
print "Location: $topsite_location/index.htm\n\n";
exit;
}


##### Updates hits per day when before creating html

sub update {
open (DAT,">members.db");
if ($use_flock) {
flock DAT, 2;
}
print (DAT @database_array);
close (DAT);

$updated=0;
$current_time = time();
foreach (@database_array) {
@member_array = split(/\|/,$database_array[$updated]);
if ($member_array[7] != 0) {
$total_time = $current_time - $member_array[7];
$total_time = $total_time /86400;
if ($total_time > 1) {
$member_array[0] = $member_array[1] / $total_time;
$database_array[$updated] = join("\|",@member_array);
}
}
$updated++;
}

open (DAT,">members.db") &#0124; &#0124; &error("unable to write to members.db");
if ($use_flock) {
flock DAT, 2;
}
print (DAT @database_array);
close (DAT);
}

##### CREATES THE HTML RANKING

sub thehtml {

open (DAT,"<members.db") &#0124; &#0124; &error("unable to open members.db");
if ($use_flock) {
flock DAT, 2;
}
@database_array = <DAT>;
close (DAT);
&update;

@sorted = sort { $a <=> $b }@database_array;
@sort = reverse(@sorted);
$x=0;
$z=0;
unless ($footerstuff) { &error("Unable to write to $path_to_topsites/index.htm"); }
open (HTML,">$path_to_topsites/index.htm") &#0124; &#0124; &error("unable to write to $path_to_topsites/index.htm");

print HTML"<HTML><HEAD><TITLE>$topsite_name</TITLE></HEAD><body background=\"/bg.gif\" text=black>$pasha_vab";
open (HEAD, "header.txt");

@head = <HEAD>;
close (HEAD);
foreach $line (@head) {
print HTML"$line";
}

#
print HTML"<table <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=2 COLS=2 WIDTH=\"600\"><tr><td VALIGN=TOP WIDTH=\"20%\"></td><td VALIGN=TOP WIDTH=\"80%\"><a href=\"/\"><img border=0 src=\"/logo.gif\"></a></td></tr><tr><td VALIGN=TOP><hr ALIGN=LEFT SIZE=1 NOSHADE WIDTH=\"95%\">";
print HTML"$pasha_site_menu";
print HTML"</td>";
print HTML"<td VALIGN=TOP><center>";
#

print HTML"<center><font size=+2>Top 10 Sites</font><br><table BORDER CELLSPACING=0 CELLPADDING=2 COLS=5 WIDTH=\"100%\"><tr BGCOLOR=#CCCCCC><th WIDTH=\"5%\"><a href=\"/topsites/topsites.cgi?ID=0\"><img border=0 alt=\"Click to update.\" src=\"/star.gif\"></a></th><th WIDTH=\"20%\"><font size=-1>Image</font></th><th WIDTH=\"49%\"><font size=-1>Description</font></th><th WIDTH=\"11%\"><font size=-1>Hits/Day</font></th><th WIDTH=\"15%\"><font size=-1>Total Hits</font></th></tr>";
foreach (@sorted) {
if ($z<$num_to_show) {

@members_array = split(/\|/,$sort[$x]);
$x++;
if ($members_array[0] != 0) {
$z++;
print HTML"<tr><td><center>$z</center></td><td><center><a href=\"$members_array[3]\"><img src=\"$members_array[4]\" width=\"88\" alt=\"Click here.\" border=0></a></center>";
# if ($z <= $num_banners){
# print HTML"<a href=\"$members_array[3]\">";
# if ($members_array[4]) {
# print HTML"<img src=\"$members_array[4]\" border=0>";
# }
# print HTML"</a>";
# }
print HTML"</td><td>$members_array[5]</td><td><center>",int($members_array[0]), "</center></td><td><center>$members_array[1]</center></td></tr>";
}

}
}

while ($z < $num_to_show) {
$z++;
print HTML "<tr bgcolor=white align=center><td><center>$z</center></td><td><center><a href=\"$topsite_location_cgi/newuser.cgi\">Your site here</a></center><br>";
print HTML "</td><td><center> - </center></td><td><center> - </center></td><td><center> - </center></td></tr>";

}

print HTML"</table>";

#
print HTML"<hr SIZE=1 NOSHADE WIDTH=\"100%\"> <!--#exec cgi=\"/cgi-bin/ad.cgi\" --> </td></tr><tr><td VALIGN=TOP><center><a href=\"http://usa.viewstat.nedstat.net/cgi-bin/viewstat?name=find\"><img SRC=\"http://usa.nedstat.net/cgi-bin/nedstat.gif?name=find\" ALT=\"Counter\" NOSAVE BORDER=0></a></center></td><td VALIGN=TOP><center><table BORDER=0 CELLSPACING=5 CELLPADDING=0 COLS=2 WIDTH=\"90%\" ><tr><td ALIGN=CENTER VALIGN=CENTER><font size=+1>Find!</font><br>Computers & Internet</td><td ALIGN=CENTER VALIGN=CENTER>Since December 1998</td></tr></table></center></td></tr></table>";
#

open (HEAD, "footer.txt");
@foot = <HEAD>;
close (HEAD);
foreach $line (@foot) {
print HTML"$line";
}
print HTML"</body></html>";
close (HTML);

}



sub error{
print "Content-type: text/html\n\n";
$errors = $_[0] ;
print <<EOF;
<table cellspacing =1 bgcolor =#00416B border=0 cellpadding =8>
<TR bgcolor=#E4E4E4 align=center><TD><FONT SIZE="-1" FACE="Arial"><B>An error has occured in topsites.cgi</B><BR><BR>
The error is: <font color=red>$errors</font><br><br>
<font color=red>$!</FONT><p>If you see this, then probably something went wrong, please contact the webmaster!</TD></TR></TABLE>
EOF
&Bottom;
exit;
}

=============================================
newuser.cgi Smile
=============================================
#!/usr/bin/perl

require "variables.pl";


$topsite_name="$variables{'topsite_name'}";
$topsite_location="$variables{'topsite_location'}";
$topsite_location_cgi="$variables{'topsite_location_cgi'}";
$topsite_image="$variables{'topsite_image'}";
$image_width="$variables{'image_width'}";
$image_height="$variables{'image_height'}";
$your_email="$variables{'your_email'}";
$mail_prog="$variables{'mail_prog'}";
$use_flock="$variables{'use_flock'}";

$pasha_vab = qq~
I use this space to show Virtualave.net banners ...
~;

$pasha_site_menu = qq~
I use this space to show some menu on the left ...
~;


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;
if ($fields{$name}) { $fields{$name} = $fields{$name}.",".$value; }
else { $fields{$name} = $value; }
}

unless ($ENV{'CONTENT_LENGTH'}) {

print <<EOF;

<HTML>
<HEAD><TITLE>$topsite_name</TITLE>
</HEAD>
<BODY background="/bg.gif" text=black>$pasha_vab

<table BORDER=0 CELLSPACING=0 CELLPADDING=2 COLS=2 WIDTH="600"><tr><td VALIGN=TOP WIDTH="20%"></td><td VALIGN=TOP WIDTH="80%"><a href=\"/\"><img border=0 src=\"/logo.gif\"></a></td></tr>
<tr><td VALIGN=TOP><hr ALIGN=LEFT SIZE=1 NOSHADE WIDTH="95%">
$pasha_site_menu
</td><td VALIGN=TOP>
<center><font size=+5>Stop!</font><br>You <b>must</b> read all of the following, before submitting your application!</center>
<p>
<b>1.</b> Intro:<br>Only computer <font size=-2>(hardware, software, games, programming, etc.)</font>, internet, and technology related websites can join! Others will be deleted.<p>
<b>2.</b> Legal:<br>Absolutely no websites will be accepted, that have/contain any or some material and/or links to such material, as <i>warez, hacking, illegal mp3 files, pirated software/games, pornography of any kind, and discrimination of any kind.</i><p>
<b>3.</b> Email:<br>The information you submit, such as email address, is only used to send you the instructions. The email <b>will not</b> be given away, and you <b>will not</b> be signed to any mailing list. BTW: if you still don't want to reveal your email, just enter this "<i>none\@none.com</i>", to avoid errors.<p>
<b>4.</b> Images:<br>The banner you are going to use can be less, but not bigger than 88x31 and 10000 bytes. Yes, it can be animated. If the banner you submit will be bigger, then it will be resized to fit 88x31 size; and probably will not look really well. So, please do submit the 88x31 banners. If you will not use your image, then it will be left as a default: <img src="/website.gif">. If in any case you will want to change the information that you submitted, then you will have to conact the webmaster at "<a href="mailto:webmaster\@find.virtualave.net">webmaster\@find.virtualave.net</a>".<p>
<b>5.</b> Description:<br>Please, use English, if possible. You may only use 10 to 15 words. It is possible to have more than 99 words, but please don't spam, and use only 10 to 15. Words and numbers, that contain three or less charachters such as: <i>and, as, or, to, so, if, of, on, at, by, too, in, 123, 456, 78, 9, etc...</i> will not be counted as words. <b>No bad language!</b><p>
<b>6.</b> Text Links:<br>You can use any text link, as long as people understand, where they go when they click it. You can not fake the links or use <b>popup windows</b> to get hits.<p>
<b>7.</b> Our rights:<br>Yep, the webmaster has some rights too. For example, if you type your description in more than 15 words, the webmaster has all rights to make it 15 or less; you will also get a message from the webmaster. If your image and/or description and/or website will contain an offensive material that was stated in section <b>2</b> of this agreement, then your account will be terminated and banned from our database for ever; in such case you will not be notified of such action.<p>
Now, when you have read, understand, and agree to all of the above, you may join.<p><b>Welcome to Find! Top Sites!</b><p>

<FORM METHOD="POST" ACTION="newuser.cgi"><table BORDER CELLSPACING=0 COLS=2 WIDTH="100%" BGCOLOR="#FFFFCC">
<TR><TD>Short description: </TD><TD><INPUT TYPE="text" NAME="name"></TD></TR>
<TR><TD>The complete URL: </TD><TD><INPUT TYPE="text" NAME="url"></TD></TR>
<TR><TD>Your Email address: </TD><TD><INPUT TYPE="text" NAME="email"></TD></TR>
<TR><TD>The complete URL of the banner: </TD><TD><INPUT TYPE="text" NAME="banner"></TD></TR>
<TR><TD>Press only once, be patient, this might take a minute.</td><td><input type=submit value="Submit"></center></TD></TR></TABLE></form>
<hr SIZE=1 NOSHADE WIDTH="100%"> <!--#exec cgi="/cgi-bin/ad.cgi" --> </td></tr><tr><td VALIGN=TOP><center><a href="http://usa.viewstat.nedstat.net/cgi-bin/viewstat?name=find"><img SRC="http://usa.nedstat.net/cgi-bin/nedstat.gif?name=find" ALT="Counter" NOSAVE BORDER=0></a></center></td><td VALIGN=TOP><center><table BORDER=0 CELLSPACING=5 CELLPADDING=0 COLS=2 WIDTH="90%" ><tr><td ALIGN=CENTER VALIGN=CENTER><font size=+1>Find!</font><br>Computers & Internet</td><td ALIGN=CENTER VALIGN=CENTER>Since December 1998</td></tr></table></center></td></tr></table>
</BODY></HTML>

EOF

exit;

}


if ($fields{'url'}=~/\http:/) {
}
else {
$error_url =1;
}

if ($fields{'email'}=~/\@/) {
}
else {
$error_email =1;
}

if ($error_email==1 &#0124; &#0124; $error_url ==1) {


print "<HTML><BODY>$pasha_vab<b>";
print "You have errors in:<br><br>";
if ($error_email ==1) {
print "In your email address: $fields{'email'}<br>";
}
if ($error_url ==1) {
print "In your site url: $fields{'url'}<br>";
}

print "<BR>Please use your back button and fix these errors, Thank You<BR>";
exit;

}

open (ID,"counts.txt");

if ($use_flock) {
flock ID, 2;
}

$new_id = <ID> ;
$new_id++;
close (ID);

open (NEWID,">counts.txt") &#0124; &#0124; &error("unable to write to counts.txt");
if ($use_flock) {
flock NEWID, 2;
}
print NEWID "$new_id";
close (NEWID);

$newline = join ("\|",0,0,0,$fields{'url'},$fields{'banner'},$fields{'name'},$fields{'email'},0,0,$new_id);
$newline .= "\n";

open(DB, ">>members.db") &#0124; &#0124; &error("unable to write to members.db");
if ($use_flock) {
flock DB, 2;
}
print DB $newline;
close (DB);


print "<HTML><HEAD><TITLE>Member Added</TITLE></HEAD><BODY>$pasha_vab";
print "<br><a href=\"/\"><img border=0 src=\"/logo.gif\"></a><p><h3 align=center>Thank You for joining</h3>";
print "<h2 align=center>$topsite_name</h2><hr width=525 noshade size=1><center><table width=575><tr><td>";
print "<b>For your site to be in the ranked, you need to do three things.<BR><br>";
print "1. Put the following html code on your web pages. <BR><blockquote></b>";
print "<a href = \"$topsite_location_cgi/topsites.cgi?ID=$new_id\"><BR><img src =\"find.gif\" border=0 width=$image_width height=$image_height></a></blockquote><br>You can use this image: <img src=\"/find.gif\"><p>";
print "<b>2. Save the image on your server! Once the html code is on your page, click on it once to activate your account. Once activated your site will not show up on the ranks for 24 hours.<br><br>";
print "3. Get as many of your visitors as you can to click thru on the code, so your rankings can go up.<BR><BR>";
print "You will also recieve an email (if you entered the valid email address) with this same code and instructions welcoming you to $topsite_name, and the url of the $topsite_name rankings so you can see how your site is doing compared to others.";
print "<br><br>Any and all questions should be directed to <a href=\"mailto:$your_email\">$your_email</a><br><br>";
print "Goto the <a href=\"$topsite_location/index.htm\">$topsite_name</a> rankings.</Td></tr></table>";
print "<br> - - - <br>";
open(MAIL,"|$mail_prog -t");
print MAIL "To: $fields{'email'}\n";
print MAIL "From: $your_email\n";
print MAIL "Subject: Welcome to $topsite_name \n\n";
print MAIL "Thank you for submitting your site to $topsite_name \n\n";
print MAIL "You need to put the following html code on your web site:\n\n";
print MAIL"<a href=\"$topsite_location/topsites.cgi?ID=$new_id\">\n";
print MAIL"<img src=\"find.gif\" border=0 width=$image_width height=$image_height></a>\n\n";
print MAIL"Save the image on your server. Once this has been done, click on the image that appears to activate your account.\n\n";
print MAIL"You site will then be included in $topsite_name within 24 hours\n\n";
print MAIL"View the $topsite_name rankings at $topsite_location/index.htm\n\n";
print MAIL" - - - ";
print MAIL"\n\n";
close (MAIL);
exit;


sub error{
$errors = $_[0] ;
print <<EOF;
<table cellspacing =1 bgcolor =#00416B border=0 cellpadding =8>
<TR bgcolor=#E4E4E4 align=center><TD><FONT SIZE="-1" FACE="Arial"><B>An error has occured in newuser.cgi</B><BR><BR>
The error is: <font color=red>$errors</font><br><br>
<font color=red>$!</FONT>
</TD></TR></TABLE>

EOF
&Bottom;
exit;
}

=============================================
variables.pl Smile
=============================================
###################
# file chmod commands
###################
# topsites.cgi - 755
# newuser.cgi - 755
# counts.txt - 666
# members.db - 666
# time.txt - 666

# The very bottom of all pages
$variables{'footerstuff'}="<font size=-2>This page is updated every hour, and reset every 24 hours.</font>";

# Title
$variables{'topsite_name'}="Find! Top Sites!";

# Directory
$variables{'path_to_topsites'}="/home/find/public_html/topsites";

# URL of HTML directory
$variables{'topsite_location'}="http://find.virtualave.net/topsites";

# URL of CGI directory
$variables{'topsite_location_cgi'}="http://find.virtualave.net/topsites";

# An image used in topsites
$variables{'topsite_image'}="http://find.virtualave.net/topsites/find.gif";
$variables{'image_width'}="88";
$variables{'image_height'}="31";

# Webmaster's email address
$variables{'your_email'}="webmaster\@find.virtualave.net";

# Path to sendmail
$variables{'mail_prog'}="/usr/sbin/sendmail";

# Number of ranks to show
$variables{'num_to_show'} = "10";

# Number of banners to show
$variables{'num_banners'} = "10";

# Number of seconds between updates
$variables{'update_time'} = "3600";

# Use file locking
$variables{'use_flock'} = "0";

## END OF REQUIRED MODIFICATIONS
1;

=============================================
Ok, I assume that you know a little of Perl, so you could make this script work. If you can't install it, then visit http://find.virtualave.net/New/ for more information on how I can install it for you.
PS. sorry, Alex, for the huge post. Smile

Regards,

Pasha

------------------
-[ It's just another night; good morning Webmasters! ]-