;
close (COUNT);
foreach $line (@adcount_lines) { chop ($line) if ($line =~ /\n$/); }
#Calculate averages;
$max = @adcount_lines - 1;
@adcount = @adcount_lines[1..$max];
$total_income = 0;
$total_uniques = 0;
foreach $advertiser (@adcount) {
&read_display_vars($advertiser);
# $ad_gif_names{$advertiser} = $image; #SurfLog uses this!
$uniques{$advertiser} = &countUniques($advertiser);
$income{$advertiser} = $cents * $uniques{$advertiser};
$total_income = $total_income + $income{$advertiser};
$total_uniques = $total_uniques + $uniques{$advertiser};
#print "$advertiser $uniques{$advertiser} $income{$advertiser} $total_income\n";
}
$total_average = ($total_income/$numdays);
# print "Projected Income Per Day : $total_average\n ";
}
sub printReport {
print "Content-type: text/html\n\n";
print "Advert Admin Page";
# Optional Refreshment
print "\n";
print "All Adverts Summary
";
print "Projected Income Per Day : ";
printf "%10.2f",$total_average;
print "
";
print "Projected Income This Month : ";
printf "%10.2f", ($total_average*30);
print "";
#print "Average exposure-to-dollar ratio: $total_ratio_text
\n";
#print "Average exposure-to-dollar ratio for rotating banners: $rot_ratio_text
\n";
#print "Total advert exposures : ",&commas($total_exposures),"
";
print "Uniques per day : ",&commas(int($total_uniques/$numdays)),"
\n";
print "Average income per unique: ";
printf "%10.5f",($total_income/$total_uniques);
print "
\n";
#print "Total income : ",&commas($total_income),"
";
($sec, $min, $hour, $day, $startmon, $year, $junk, $junk, $junk) = localtime($startTime);
print "For the past $numdays days, or since: $day/$MoY[$startmon]/$year $hour:$min:$sec
";
print "
\n";
print "| Advert | ";
print "Daily Avg. Income | ";
print "Unique Clicks Per Day | ";
#print "Raw Clicks | ";
#print "Exposures:Dollar Ratio | ";
#print "Income To Date | ";
#print "Exposures To Date | ";
print "
\n";
# Print each line of table
foreach $advertiser ( sort { $income{$b} <=> $income{$a} } keys %income ) {
#foreach $advertiser (@adcount) {
# $ratio = 0;
# $ratio_text = "N/A";
#
# if (($exposures{$advertiser} != 0) && ($income{$advertiser} != 0)) {
# $ratio = (($shown/$income)+.5001);
# $ratio_text = $ratio;
# $ratio_text =~ s/(\d+)\.\d.*/$1/;
# $ratio_text = $ratio_text." : 1";
# }
$average = ($income{$advertiser}/$numdays);
print "";
print"| ";
print "$advertiser<\/A>";
print" | ";
print "";
printf "%10.2f",$average;
print " | ";
print"",&commas(int($uniques{$advertiser}/$numdays))," | ";
# print "",&commas($visits{$advertiser})," | ";
# print "";
# if ($ratio > $rot_ratio) {print "";}
# print "$ratio_text";
# if ($ratio > $rot_ratio) {print "";}
# print " | ";
# print "$income{$advertiser} | ";
# print "",&commas($exposures{$advertiser})," | ";
print "
\n";
}
print "
\n";
}
################################### EDIT ADS
sub edit {
$INPUT{'editad'} =~ s/[ \/\\]//g;
print "Content-type: text/html\n\n";
if (-e "$ads_dir/$INPUT{'editad'}.txt") {
&read_display_vars( $INPUT{'editad'});
}
print "Advert Add/Edit Page";
print "\n";
print "\n";
&Footer;
exit;
}
#- Size Gifs ----------------------------------------------------------------#
sub gifHeight {
# return a string of height and width tags for a gif.
# this code "adapted" from:
# http://rajiv.org/programming/gifsize.txt
my ($GIF) = @_;
my ($w, $w2, $h, $h2, $gifwidth, $gifsize, $type) = () ;
read ($GIF, $type, 3);
seek ($GIF, 6, 0);
read ($GIF, $w, 1);
read ($GIF, $w2, 1);
read ($GIF, $h, 1);
read ($GIF, $h2, 1);
$height = ord ($h) + ord ($h2) * 256;
return $height;
}
sub gifWidth {
# return a string of height and width tags for a gif.
# this code "adapted" from:
# http://rajiv.org/programming/gifsize.txt
my ($GIF) = @_;
my ($w, $w2, $h, $h2, $gifwidth, $gifsize, $type) = () ;
read ($GIF, $type, 3);
seek ($GIF, 6, 0);
read ($GIF, $w, 1);
read ($GIF, $w2, 1);
read ($GIF, $h, 1);
read ($GIF, $h2, 1);
$width = ord ($w) + ord ($w2) * 256;
return $width;
}
#----------------------------------------------------------------------------#
#----------------------------------------------------------------------------#
# jpegsize : gets the width and height (in pixels) of a jpeg file
# Andrew Tong, werdna@ugcs.caltech.edu February 14, 1995
# modified slightly by alex@ed.ac.uk
sub jpegHeight {
my ($JPEG) = @_;
my ($done) = 0;
my ($size) = "";
read($JPEG, $c1, 1); read($JPEG, $c2, 1);
if( !((ord($c1) == 0xFF) && (ord($c2) == 0xD8))){
print "This is not a JPEG!";
$done=1;
}
while (ord($ch) != 0xDA && !$done) {
# Find next marker (JPEG markers begin with 0xFF)
# This can hang the program!!
while (ord($ch) != 0xFF) { read($JPEG, $ch, 1); }
# JPEG markers can be padded with unlimited 0xFF's
while (ord($ch) == 0xFF) { read($JPEG, $ch, 1); }
# Now, $ch contains the value of the marker.
if ((ord($ch) >= 0xC0) && (ord($ch) <= 0xC3)) {
read ($JPEG, $junk, 3); read($JPEG, $s, 4);
($a,$b,$c,$d)=unpack("C"x4,$s);
$height=$a<<8|$b;
$done=1;
} else {
# We **MUST** skip variables, since FF's within variable names are
# NOT valid JPEG markers
read ($JPEG, $s, 2);
($c1, $c2) = unpack("C"x2,$s);
$length = $c1<<8|$c2;
if( ($length < 2) ){
print "Erroneous JPEG marker length";
$done=1;
} else {
read($JPEG, $junk, $length-2);
}
}
}
return $height;
}
sub jpegWidth {
my ($JPEG) = @_;
my ($done) = 0;
my ($size) = "";
read($JPEG, $c1, 1); read($JPEG, $c2, 1);
if( !((ord($c1) == 0xFF) && (ord($c2) == 0xD8))){
# print "This is not a JPEG!";
$done=1;
}
while (ord($ch) != 0xDA && !$done) {
# Find next marker (JPEG markers begin with 0xFF)
# This can hang the program!!
while (ord($ch) != 0xFF) { read($JPEG, $ch, 1); }
# JPEG markers can be padded with unlimited 0xFF's
while (ord($ch) == 0xFF) { read($JPEG, $ch, 1); }
# Now, $ch contains the value of the marker.
if ((ord($ch) >= 0xC0) && (ord($ch) <= 0xC3)) {
read ($JPEG, $junk, 3); read($JPEG, $s, 4);
($a,$b,$c,$d)=unpack("C"x4,$s);
$width= $c<<8|$d;
$done=1;
} else {
# We **MUST** skip variables, since FF's within variable names are
# NOT valid JPEG markers
read ($JPEG, $s, 2);
($c1, $c2) = unpack("C"x2,$s);
$length = $c1<<8|$c2;
if( ($length < 2) ){
print "Erroneous JPEG marker length";
$done=1;
} else {
read($JPEG, $junk, $length-2);
}
}
}
return $width;
}
#----------------------------------------------------------------------------#
sub editfinal {
$editad = $INPUT{'editad'};
$cents = $INPUT{'cents'};
$url = $INPUT{'url'};
$text = $INPUT{'text'};
$image = $INPUT{'image'};
open(GRAPHIC, "$banner_local$image") || return_error(300,"getHeight", "Can't open graphic $banner_local$image $!");
binmode(GRAPHIC); # necessary for non UNIX perl. just leave it.
if ($image =~ /\.gif$/i) {
$height = &gifHeight(GRAPHIC);
} elsif ($image =~ /\.jpg$|.jpeg$/i) {
$height = &jpegHeight(GRAPHIC);
}
close (GRAPHIC);
open(GRAPHIC, "$banner_local$image") || return_error( 300,"getWidth", "Can't open graphic $banner_local$image $!");
binmode(GRAPHIC); # necessary for non UNIX perl. just leave it.
if ($image =~ /\.gif$/i) {
$width= &gifWidth(GRAPHIC);
} elsif ($image =~ /\.jpg$|.jpeg$/i) {
$width = &jpegWidth(GRAPHIC);
}
close (GRAPHIC);
$image = "$banner_url$image";
#$target = $INPUT{'target'};
$target = "view";
$javascript = $INPUT{'javascript'};
$statsurl = $INPUT{'statsurl'};
$userid= $INPUT{'userid'};
$passcode = $INPUT{'passcode'};
$bannersurl = $INPUT{'bannersurl'};
print "Content-type: text/html\n\n";
unless ( $url && $cents ) {
print "Incomplete Advert";
print "\n";
print "You didn't provide all of the necessary information to allow ";
print "addition of the $editad advert!
\n";
&Footer;
exit;
}
if (!(-e "$ads_dir/$editad.txt")) {
open (COUNT, ">>$adcount");
# &LockFile(COUNT)|| return_error(300,"editfinal","Could not lock $adcount");
print COUNT "$editad\n";
# &UnlockFile(COUNT);
close (COUNT);
}
&write_display_vars($editad);
system("touch $logs_dir\/$editad.log");
chmod 0666, "$logs_dir\/$editad.log";
print "Advert Updated";
print "\n";
#Now add it to the rotation, or remove it
chmod 0666, $adbanners;
open (BANNERS, "<$adbanners") || return_error(300,"open in editfinal","Could not open $adbanners for reading");
@lines = ;
close BANNERS;
foreach $line (@lines) { chop ($line) if ($line =~ /\n$/);}
open (BANNERS, ">$adbanners") || return_error(300,"open in editfinal","Could not open $adbanners for writing");
&LockFile(BANNERS);
seek(BANNERS, 0, 0);
foreach $line (@lines) {
if (($line) && ($line ne $editad)) {print BANNERS "$line\n"; print "$line
";}}
if ($INPUT{'rotate'} eq "yes") {print BANNERS "$editad\n"; print " $editad
";}
&UnlockFile(BANNERS);
close (BANNERS);
print "The $editad advert will appear as shown below:
\n";
if ($image) {
print "
";
print "
";
print "
";
print "$text
\n";
}
else {
print "$text";
}
print "\n";
&Footer;
exit;
}
########################## DELETE ADS
sub del {
print "Content-type: text/html\n\n";
unless (-e "$ads_dir/$INPUT{'delad'}.txt") {
print "Invalid Advert Name";
print "\n";
print "There is no advert with the name $INPUT{'delad'} ";
print " on the list!
\n";
&Footer;
exit;
}
print "Advert Delete Page";
print "\n";
print "\n";
&Footer;
exit;
}
sub delfinal {
print "Content-type: text/html\n\n";
print "Deleting...";
print "\n";
$delad = $INPUT{'delad'};
unlink ("$ads_dir/$delad.txt");
print "Deleted :$ads_dir/$delad.txt
\n";
open (COUNT, "$adcount");
@lines = ;
&UnlockFile(COUNT);
close (COUNT);
foreach $line (@lines) { chop ($line) if ($line =~ /\n$/); }
$| = 1;
chmod 0666,$adcount;
open (COUNT, ">$adcount");
$lockerror = &LockFile(COUNT);
if ($lockerror) {
print "[ File Lock Error ($adcount)! ]";
exit;
}
seek(COUNT, 0, 0);
foreach $line (@lines) {
if (($line) && ($line ne $delad) ){
print COUNT "$line\n";
}
}
&UnlockFile(COUNT);
close (COUNT);
chmod 0666,$adbanners;
open (BANNERS, "$adbanners");
@lines = ;
close (BANNERS);
foreach $line (@lines) { chop ($line) if ($line =~ /\n$/); }
unlink ("$adbanners");
open (BANNERS, ">$adbanners");
$lockerror = &LockFile(BANNERS);
if ($lockerror) {
print "[ File Lock Error ($adbanners)! ]";
exit;
}
seek(BANNERS, 0, 0);
foreach $line (@lines) {
if (($line) && ($line ne $delad) ){
print BANNERS "$line\n";
}
}
&UnlockFile(BANNERS);
close (BANNERS);
print "\n";
&Footer;
}
sub Footer {
print "\n";
}
sub return_error
{
local ($status, $keyword, $message) = @_;
print "Content-type: text/html\n";
print "Status: $status $keyword\n\n";
print <
CGI Program Error
$keyword
$message
End_of_Error
exit(1);
}
sub read_display_vars {
local ($filename) = @_;
open (DISPLAY, "<$ads_dir/$filename.txt") || &return_error (500, "Ad Log Error", "Could not open ad file $ads_dir/$filename.txt in read_display_vars .");
# Read in the whole DISPLAY file. Chop return chars if necessary.
@lines = ;
close (DISPLAY);
foreach $line (@lines) { chop ($line) if ($line =~ /\n$/); }
# Name each of the variables in the DISPLAY file.
($cents,$url,$text,$image,$height,
$width,$target,$javascript,$statsurl,$userid,$passcode,$bannersurl ) = @lines;
}
sub write_display_vars {
local ($filename) = @_;
open (DISPLAY, ">$ads_dir/".$filename.".txt") || &return_error (500, "Ad Log Error", "Could not open ad file $ads_dir/$filename.txt in write_display_vars .");
&LockFile(DISPLAY);
seek(DISPLAY,0,0);
print DISPLAY "$cents\n";
print DISPLAY "$url\n";
print DISPLAY "$text\n";
print DISPLAY "$image\n";
print DISPLAY "$height\n";
print DISPLAY "$width\n";
print DISPLAY "$target\n";
print DISPLAY "$javascript\n";
print DISPLAY "$statsurl\n";
print DISPLAY "$userid\n";
print DISPLAY "$passcode\n";
print DISPLAY "$bannersurl\n";
&UnlockFile(DISPLAY);
close (DISPLAY);
}
sub LockFile {
local(*FILE) = @_;
local($TrysLeft) = 10;
if ($UseLocking) {
while ($TrysLeft--) {
$lockresult = eval("flock(FILE,$exclusive_lock)");
if ($@) {
$UseLocking = 0;
last;
}
if (!$lockresult) {
select(undef,undef,undef,0.1);
}
else {
last;
}
}
}
if ($TrysLeft >= 0) {
return 0;
}
else {
return -1;
}
}
sub UnlockFile {
local(*FILE) = @_;
if ($UseLocking) {
flock(FILE,$unlock_lock);
}
}
sub commas {
local($_)=@_;
1 while s/(.*\d)(\d\d\d)/$1,$2/;
$_;
}