Viewing file: counter.cgi (14.37 KB) -rwxr-xr-x Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
#!/usr/bin/perl # counter.cgi - universal graphical gif/digit based counter program. # Version 1.1
# Hurricane Electric # Jason Meyer # Copyright 1998
use IPC::Open2;
srand(time() ^ ($$ + ($$ << 15 )) );
$username = ( getpwuid($<) )[0];
if( $username eq "nobody" ) { $username = $ENV{DOCUMENT_ROOT}; $username =~ s/^\/home\///; $username =~ s/\/public_html$//; }
$dbase = "/home/$username/public_html/counter/database"; $digit_dir = "/usr/local/counter/digits"; $fly = "/usr/local/counter/bin/fly -q"; $mfile = "/home/$username/public_html/counter/digits/modify.disable";
&ReadParse; if( $in{digitdir} ) { $digitdir = $in{digitdir}; if ( -d "/home/$username/public_html/counter/digits/$digitdir" ) { $digit_dir = "/home/$username/public_html/counter/digits/$digitdir"; } else { $digit_dir = "/usr/local/counter/digits"; } }
if( $in{cmd} eq "list" ) { &stat_list; } if( $in{modify} ) { &modify; }
# set this to one if you want the gif generator to attempt and make # the created gif transpartent # one or zero $tp = 0;
&execute_check; &variables;
if ( $ENV{DOCUMENT_URI} ) { if ( $in{type} eq "t" ) { print "Content-type: text/html\n\n"; &text; } if ( !$in{var} ) { $rand = int(rand 9999); print "Content-type: text/html\n\n"; if ( $in{digitdir} ) { print "<IMG SRC=\"/counter/counter.cgi?digitdir=$in{digitdir}&var=$rand\" align=\"absmiddle\">"; } else { print "<IMG SRC=\"/counter/counter.cgi?var=$rand\" align=\"absmiddle\">"; } } } if ( !$ENV{DOCUMENT_URI} && $ENV{HTTP_REFERER} ) { &graphical; }
sub text { &load_count; print "$count[0]"; exit 0; }
sub graphical { &load_count; &get_image_info;
$num = $length = length($count[0]); while ($num > 0) { $DIGIT{$num} = chop($count[0]); $num--; } $img_width = ($width * $length); $img_height = ($height);
open2( \*RDR, \*WTR, "$fly" ); print WTR "new\n"; print WTR "size $img_width,$img_height\n"; if ($tp) { print WTR "transparent R,G,B\n"; } $a = 1; while ($a <= $length) { print WTR "copy $add_width,0,-1,-1,-1,-1,$digit_dir/$DIGIT{$a}\.gif\n"; $add_width = ($add_width + $width); $a++; } print WTR "end"; close(WTR);
$got = <RDR>;
print "Content-type: image/gif\n\n"; print "$got"; while (<RDR>) { print; } }
sub variables { if ( $ENV{HTTP_REFERER} ) { $ENV{HTTP_REFERER} =~ s/^http:\/\/|https:\/\///; $temp1 = $ENV{HTTP_REFERER}; $ENV{HTTP_REFERER} =~ s/~$username//; $ENV{HTTP_REFERER} =~ s/\W//g; $ENV{HTTP_REFERER} =~ s/^www//; $sname = $ENV{SERVER_NAME}; $sname =~ s/\W//g; $sname =~ s/^www//; if ($ENV{HTTP_REFERER} =~ /^$sname$/ ){$ENV{HTTP_REFERER} = "index";} $ENV{HTTP_REFERER} =~ s/^$sname//; if ($ENV{HTTP_REFERER} eq "indexhtml"){$ENV{HTTP_REFERER} = "index";} if ($ENV{HTTP_REFERER} eq "indexhtm"){$ENV{HTTP_REFERER} = "index";} $ENV{HTTP_REFERER} =~ s/indexhtml$|indexhtm$//g; } if ( $ENV{DOCUMENT_URI} ) { $temp2 = $ENV{DOCUMENT_URI}; $ENV{DOCUMENT_URI} =~ s/~$username//; $ENV{DOCUMENT_URI} =~ s/\W//g; $ENV{DOCUMENT_URI} =~ s/^www//; $sname = $ENV{SERVER_NAME}; $sname =~ s/\W//g; $sname =~ s/^www//; if ($ENV{DOCUMENT_URI} =~ /^$sname$/ ){$ENV{DOCUMENT_URI} = "index";} $ENV{DOCUMENT_URI} =~ s/^$sname//; if ($ENV{DOCUMENT_URI} eq "indexhtml"){$ENV{DOCUMENT_URI} = "index";} if ($ENV{DOCUMENT_URI} eq "indexhtm"){$ENV{DOCUMENT_URI} = "index";} $ENV{DOCUMENT_URI} =~ s/indexhtml$|indexhtm$//g; }
if ($ENV{DOCUMENT_URI}) { $referer = $temp2; $cfile = $ENV{DOCUMENT_URI}; } else { $referer = $temp1; $cfile = $ENV{HTTP_REFERER}; } }
sub load_count { if ( !-d "$dbase") { mkdir("$dbase",0755); } $age = ( stat "$dbase/$cfile.lock" )[10]; $age = ( time() - $age ); if ( $age > 60 ) { rmdir "$dbase/$cfile.lock"; } for( $attempt = 0; $attempt < 3; $attempt++ ) { if( mkdir( "$dbase/$cfile.lock", 0700 ) ){ open(COUNT,"$dbase/$cfile"); @count = <COUNT>; chop( $count[0] ); chop( $count[1] ); close(COUNT); $count[0]++; $count[1] = "$referer"; open(COUNT,">$dbase/$cfile"); print COUNT "$count[0]\n"; print COUNT "$count[1]\n"; close(COUNT); rmdir( "$dbase/$cfile.lock" ); $success=1; last; } sleep 1; } if ( !$success ) { open(COUNT,"$dbase/$cfile"); @count = <COUNT>; chop( $count[0] ); close(COUNT); $count[0]++; return 1; } }
sub get_image_info { open(IMAGE, "$digit_dir/0.gif"); sysread(IMAGE, $data, 10); close(IMAGE); $width = unpack('C4', ( split('',$data))[6] ); $width2 = unpack('C4', ( split('',$data))[7] ); $width2 = ($width2*255);
$height = unpack('C4', ( split('',$data))[8] ); $height2 = unpack('C4', ( split('',$data))[9] ); $height2 = ($height2*255);
$width = ($width + $width2); $height = ($height + $height2); }
sub execute_check { if ( !$ENV{HTTP_REFERER}&&!$ENV{DOCUMENT_URI} || !$ENV{SERVER_NAME} ) { print "Content-type: text/html\n\n"; print "Error<br>\n"; exit(0); } }
sub stat_list { print "Content-type: text/html\n\n"; print "<body bgcolor=#F0F0F0>\n"; print "<table border=1 cellpadding=2 cellspasing=1>\n"; print "<tr><td><font size=2><b>Page</b></td>\n"; print "<td align=right width=30><font size=2><b>Stats</b></td></tr>\n";
opendir(DIR,$dbase); @dirlist=readdir(DIR);
foreach $file (sort @dirlist) { next if "$file" eq "."; next if "$file" eq "..";
open(COUNT,"$dbase/$file"); @count = <COUNT>; close(COUNT); $count[0] =~ s/\n//g; $count[1] =~ s/\n//g; print "<tr><td><font size=2>$count[1]</font></td>";
if ( -f $mfile ) { print "<td align=right><font size=2>$count[0]</font></td></tr>\n"; } else { print "<td align=right><a href=\"counter.cgi?modify=$file\">"; print "<font size=2 style=\"text-decoration:none\">$count[0]</font>"; print "</a></td></tr>\n"; }
$total = ($total+$count[0]); }
print "<tr><td><font size=2>Total for all pages: </td>\n"; print "<td align=right><font size=2><b>$total</b></td></tr>\n"; print "</table>\n"; print "<p><br>\n";
if( -f $mfile ) { print "Modifications are disabled.<br><p>\n"; print "To re-enable them click <a href=\"counter.cgi?modify=en_dis\">"; print "here</a><br>\n"; } else { print "<li>You can click on a highlighted digit"; print " to modify or delete it.</li>\n"; print "<li>If you would like to disable the digit modification click "; print "<a href=\"counter.cgi?modify=en_dis\">here</a></li>\n"; }
exit 0; }
sub modify { if( $in{modify} eq "en_dis" ) { if( $in{enable_disable} ) { if( $in{enable_disable} eq "disable" ) { if( -f $mfile ) { print "Content-type: text/html\n\n"; print "Error: Password protection allready exists.\n"; exit 0; } if( !$in{mod_pass} ) { undef $in{mod_pass}; undef $in{enable_disable}; $in{modify} = "en_dis"; $message = "No password entered, Try again.\n"; &modify; return 1; } if( $in{mod_pass} ne $in{mod_pass2} ) { undef $in{mod_pass}; undef $in{enable_disable}; $in{modify} = "en_dis"; $message = "Password's don't match, Try again.\n"; &modify; return 1; } @saltchars=("a..z","A..Z","0..9",'.','/'); $salt=$saltchars[int(rand($#saltchars+1))]; $salt.=$saltchars[int(rand($#saltchars+1))]; $encrypt = crypt( $in{mod_pass}, $salt ); open(LOCK,">$mfile");print LOCK "$encrypt";close(LOCK); &stat_list; return 1; } if( $in{enable_disable} eq "enable" ) { open(LOCK,"$mfile");$old_pass=<LOCK>;close(LOCK); $old_pass =~ s/\n//g; if ( crypt( $in{mod_pass}, $old_pass ) eq $old_pass ) { unlink("$mfile"); &stat_list; return 1; } else { undef $in{mod_pass}; undef $in{enable_disable}; $in{modify} = "en_dis"; $message = "Password incorrect.<br><p>\n"; $message = "$message Try again, or remove"; $message = "$message the password file.\n"; &modify; return 1; } } }
print "Content-type: text/html\n\n"; print "<body bgcolor=#F0F0F0>\n";
print "<center><H3>This page allows you to enable or disable "; print "modifications.</H3></center><br><p>\n";
print "<form method=post action=counter.cgi?cmd=list>\n"; if ( -f $mfile ) { print "<input type=hidden name=modify value=en_dis>\n"; print "<input type=hidden name=enable_disable value=enable>\n"; print "If you don't know the password you can <b>ftp</b> into your"; print " account and remove the following file: "; print "<b>$mfile</b><br><p>\n"; print "<b>Enter re-enable password:</b> "; print "<input type=password name=mod_pass size=10><br>\n"; print "<p>\n<input type=submit value=Submit>\n"; print "</form>\n"; } else { print "<input type=hidden name=modify value=en_dis>\n"; print "<input type=hidden name=enable_disable value=disable>\n"; print "The password you enter below will be used to protect "; print "against someone easily re-enabling modifications.<br><p>\n"; print "<table>\n<tr><td><b>Enter disable password:</b></td>"; print "<td><input type=password name=mod_pass size=10></td></tr>\n"; print "<tr><td><b>Re-Enter disable password:</b></td>"; print "<td><input type=password name=mod_pass2 size=10></td></tr>\n"; print "</table>\n<p>\n<input type=submit value=Submit>\n"; print "</form>\n"; } if ( $message ) { print "<hr>$message"; } print "<hr><a href=counter.cgi?cmd=list>Go Back</a>"; exit 0; }
if( $in{modify} eq "modify_count" ) { if ( $in{new_count} =~ /\D/ ) { print "Content-type: text/html\n\n"; print "No words please.<br><br>\n"; print "Use your browsers back button to re-enter the count.<br>\n"; exit 0; }
if ( $in{new_count} == 0 ) { unlink("$dbase/$in{modify_file}"); unlink("$dbase/$in{modify_file}.lock"); &stat_list; return 1; } $age = ( (-M "$dbase/$in{modify_file}.lock") * 100000 ); if ( $age > 60 ) { unlink("$dbase/$in{modify_file}.lock"); } if ( -f "$dbase/$in{modify_file}.lock" ) { if ( $sleep_count > 3 ) { print "Content-type: text/html\n\n"; print "Halted: Your counter is under heavy load right"; print "now and couldn't be modified, try again later.<br>\n"; exit 0; } sleep 1; $sleep_count++; &modify; return 1; } if ( -f "$dbase/$in{modify_file}" ) { open(LOCK,">$dbase/$in{modify_file}.lock");print LOCK "";close(LOCK); open(COUNT,"$dbase/$in{modify_file}"); @count = <COUNT>; $count[1] =~ s/\n//g; close(COUNT); $count[0] = $in{new_count}; open(COUNT,">$dbase/$in{modify_file}"); print COUNT "$count[0]\n"; print COUNT "$count[1]\n"; close(COUNT); unlink("$dbase/$in{modify_file}.lock"); } else { print "Content-type: text/html\n\n"; print "Error: No database file existed, no changes made.<br><br>\n";
print "This error should not be generated. If you have received"; print "this message you should go back to the beginning and try "; print "again.<br>\n"; exit 0; }
&stat_list; return 1; }
print "Content-type: text/html\n\n"; print "<body bgcolor=#F0F0F0>\n";
open(COUNT,"$dbase/$in{modify}"); @count = <COUNT>; close(COUNT); $count[0] =~ s/\n//g; $count[1] =~ s/\n//g; print "<table>\n"; print "<tr><td align=right><b>Page:</b></td><td>$count[1]</td></tr>\n"; print "<tr><td align=right><b>Count:</b></td><td>$count[0]</td></tr>\n"; print "</table>\n";
print "<form method=post action=counter.cgi?cmd=list>\n"; print "<input type=hidden name=modify value=modify_count>\n"; print "<input type=hidden name=modify_file value=$in{modify}>\n"; print "<b>Enter new count:</b> "; print "<input type=text name=new_count size=10 value=$count[0]><br>\n"; print "<p>\n<input type=submit value=Submit>\n"; print "</form>\n";
print "<p><br>\n";
print "<li>If you enter a zero for the new count"; print " this entry will be deleted.</li>\n";
exit 0; }
# # CGI-LIB.PL Functions #
sub ReadParse { local (*in) = @_ if @_; local ($i, $key, $val);
if ($ENV{'REQUEST_METHOD'} eq "GET") { $in = $ENV{'QUERY_STRING'}; } elsif ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN,$in,$ENV{'CONTENT_LENGTH'}); }
@in = split(/&/,$in); foreach $i (0 .. $#in) { $in[$i] =~ s/\+/ /g; ($key, $val) = split(/=/,$in[$i],2); $key =~ s/%(..)/pack("c",hex($1))/ge; $val =~ s/%(..)/pack("c",hex($1))/ge; $in{$key} .= "\0" if (defined($in{$key})); $in{$key} .= $val; } return 1; }
|