Listing of trailer.pl
#!/usr/bin/perl -w
chdir "/home/mjfrazer/public_html/cgi-bin";
#require "ssincludes.pl";
## begin ssincludes.pl
#!/opt/bin/perl
sub pdie
{
print @_;
die @_;
}
sub CheckForSSEnvironment
{
if(!$ENV {'SERVER_NAME'}) { &pdie ("SERVER_NAME not defined\n"); }
if(!$ENV {'DOCUMENT_URI'}) { &pdie ("DOCUMENT_URI not defined\n"); }
if(!$ENV {'DOCUMENT_NAME'}) { &pdie ("DOCUMENT_NAME not defined\n"); }
if(!$ENV {'DATE_LOCAL'}) { &pdie ("DATE_LOCAL not defined\n"); }
if(!$ENV {'REMOTE_ADDR'}) { &pdie ("REMOTE_ADDR not defined\n"); }
if(!$ENV {'DATE_GMT'}) { &pdie ("DATE_GMT not defined\n"); }
if(!$ENV {'LAST_MODIFIED'}) { &pdie ("LAST_MODIFIED not defined\n"); }
if(!$ENV {'SERVER_SOFTWARE'}) { &pdie ("SERVER_SOFTWARE not defined\n");}
#if(!$ENV {'REMOTE_HOST'}) { &pdie ("REMOTE_HOST not defined\n"); }
if(!$ENV {'HTTP_HOST'}) { $ENV{'HTTP_HOST'} = $ENV{'SERVER_NAME'};}
}
#1;
## end ssincludes.pl
sub CreateLogDir
{
mkdir($logfiledir, 0755);
symlink("../address.html", "$logfiledir/address.html");
symlink("../cgi-bin", "$logfiledir/cgi-bin");
}
sub CreateLogFile
{
if(!open(LOGFILEHANDLE, ">$logfile")) {
&pdie ("Bummer: Couldn't open $logfile since $! <br>");
}
$today = localtime();
#chop($today);
select(LOGFILEHANDLE);
print <<EOLOGFILE;
<html>
<head><title>Logfile for $documentpath</title>
<meta name="robots" content="noindex,nofollow">
</head>
<body bgcolor="EEEEEE">
Referrers to <a href="$documenturl">$documentpath</a>
as of $today.
<hr>
<ul>
</ul>
<!--#include file="./address.html" -->
</body>
</html>
EOLOGFILE
select(STDOUT);
close(LOGFILEHANDLE);
}
sub InsertIntoLogFile
{
local ($valueToInsert, $lineRead, $referrer);
local ($inputLine) = 0;
local (@theFile);
local ($LOCK_EX,$LOCK_UN);
$LOCK_EX = 2;
$LOCK_UN = 8;
if(!open(LOGFILEHANDLE, "+<$logfile"))
{
&pdie ("Bummer: Couldn't open log file for read.<br>");
}
flock(LOGFILEHANDLE,$LOCK_EX);
@theFile = <LOGFILEHANDLE>;
if($ENV{'HTTP_REFERER'})
{
$_ = $ENV{'HTTP_REFERER'};
s/#[^\/]*$//;
$referrer = $_;
$valueToInsert = "<li><a href=\"".${referrer}."\">"
.${referrer}."</a>\n";
while(($lineRead = $theFile[$inputLine++]) && ($lineRead ne "<ul>\n"))
{}
while(($lineRead = $theFile[$inputLine++])
&& ($lineRead ne "</ul>\n")
&& ($lineRead lt $valueToInsert)) {}
if($lineRead ne $valueToInsert)
{
if(!seek(LOGFILEHANDLE,0,0))
{
&pdie("Bummer: Couldn't seek back to start of log file.<br>");
}
local ($outputLine) = 0;
while($outputLine < $inputLine - 1)
{
print LOGFILEHANDLE $theFile[$outputLine++];
}
print LOGFILEHANDLE $valueToInsert;
while($outputLine <= $#theFile)
{
print LOGFILEHANDLE $theFile[$outputLine++];
}
$backlinkcount++;
}
}
flock(LOGFILEHANDLE,$LOCK_UN);
close(LOGFILEHANDLE);
local ($i) = 0;
for($i = 0; $i <= $#theFile; $i++)
{
if( $theFile[$i] =~ /^<li>/ ) { $backlinkcount++; }
if( $theFile[$i] =~ /^as of/ ) {
$asofdate = $theFile[$i];
$asofdate =~ s/^as of //;
$asofdate =~ s/\.\n$//;
}
}
return $backlinkcount;
}
sub PrintTrailer
{
print "<hr>\n";
print "Document: $documentpath<br>\n";
print "Last Update: $ENV{'LAST_MODIFIED'}<br>\n";
if(exists $ENV{'REMOTE_HOST'}) {
print "Served From: $ENV{'SERVER_NAME'} to $ENV{'REMOTE_HOST'}\n";
}
else {
print "Served From: $ENV{'SERVER_NAME'} to $ENV{'REMOTE_ADDR'}\n";
}
print " on $ENV{'DATE_LOCAL'}<br>\n";
print "Off-site <a href=\"$logfileurl\">Backlinks</a>: $backlinkcount\n";
print "(as of $asofdate)<br>\n";
}
#########################################################################
## MAIN
&CheckForSSEnvironment();
umask (022);
$_ = $ENV {'DOCUMENT_URI'};
s#/~mark/##;
s#/~mjfrazer/##;
/^(\S+)$/; # untaint the document name so I can use it to open files
$documentpath = $1;
$documenturl = "http://".$ENV{'HTTP_HOST'}."/~mjfrazer/".$documentpath;
$_ = $documentpath;
s/[^\/]*$//;
$documentdir = $_;
$_ = $documentpath;
s/.*\///;
s/\.html$//;
$documentname = $_;
$logfiledir = "/home/mjfrazer/public_html/".$documentdir.".referrers/";
$logfile = $logfiledir.$documentname.".html";
$logfileurl = "http://".$ENV{'HTTP_HOST'}."/~mjfrazer/"
.$documentdir.".referrers/".$documentname.".html";
$backlinkcount = 0;
if(! -d $logfiledir) { &CreateLogDir(); }
if(! -r $logfile) { &CreateLogFile(); }
&InsertIntoLogFile();
&PrintTrailer();
__END__
Mark Frazer -- mjfrazer@gmail.com