#!/usr/local/bin/perl -w require "timelocal.pl"; # SET THE FOLLOWING VARIABLES: $numdays = 3; $mydir = "webvert"; $baseref = "/home/web/glamazon/cgi-bin/$mydir"; $gotoad = "http://www.glamazon.com/cgi-bin/$mydir/ads.pl"; $admincgi = "http://www.glamazon.com/cgi-bin/$mydir/rip.pl"; $adcount = $baseref ."/adcount.txt"; $adbanners = $baseref ."/adbanners.txt"; $ads_dir = $baseref ."/ads"; $logs_dir = $baseref ."/logs"; $banner_url = "http://www.glamazon.com/banners/"; $banner_local = "/home/web/glamazon/docs/banners/"; $UseLocking = 1; $exclusive_lock = 2; $unlock_lock = 8; @MoY = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); 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; $INPUT{$name} = $value; } if ($INPUT{'edit'}) { &edit; } elsif ($INPUT{'del'}) { &del; } elsif ($INPUT{'editfinal'}) { &editfinal; } elsif ($INPUT{'delfinal'}) { &delfinal; } elsif ($INPUT{'reviewall'}) { &reviewall; } else { &reviewall; } sub reviewall { $now = time(); $startTime = $now - ($numdays * 24 * 60 * 60); &getIncome; &printReport; print "

The following options are available:\n"; print "

\n"; print "

"; print "\n"; print "

"; print "\n"; print "

\n"; &Footer; exit; } sub countUniques { $logname = $_[0]; $unique_count = 0; open (UNIQUELOG, "<$logs_dir\/$logname.log")|| return 0; while () { ($junk, $ip_time_logged, $junk) = split(' ', $_ ,3); #print "time logged $ip_time_logged startTime $startTime\n"; if ($ip_time_logged > $startTime) {$unique_count++;} } close UNIQUELOG; return $unique_count; } sub getIncome { open (COUNT, "$adcount"); @adcount_lines = ; 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 ""; print ""; print ""; #print ""; #print ""; #print ""; #print ""; 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 ""; print""; # print ""; # print ""; # print ""; # print ""; print "\n"; } print "
AdvertDaily Avg. IncomeUnique Clicks Per DayRaw ClicksExposures:Dollar RatioIncome To DateExposures To Date
"; print "$advertiser<\/A>"; print""; printf "%10.2f",$average; print "",&commas(int($uniques{$advertiser}/$numdays)),"",&commas($visits{$advertiser}),""; # if ($ratio > $rot_ratio) {print "";} # print "$ratio_text"; # if ($ratio > $rot_ratio) {print "";} # print "$income{$advertiser}",&commas($exposures{$advertiser}),"

\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"; print "

Info for "; print "$INPUT{'editad'} advert:

\n"; print "

\n"; print "\n"; print "\n"; print "\n"; $_ = $image; s/^$banner_url//o; #strip banner url from image (will put it back it editfinal) $image = $_; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; #Now add a checkbox - if checked we want to add it to the rotation. $rotating = 0; open (BANNERS, "$adbanners"); @lines = ; close (BANNERS); foreach $line (@lines) { chop ($line) if ($line =~ /\n$/); if ($line eq $editad) {$rotating = 1;} } print "\n"; print "
Cents Per Click:
Site URL:
Text:
Banner Name:
Target:
Javascript:
Stats Page:
User ID:
Passcode:
Banners:
Put in Rotation?

\n"; print "

\n"; 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 "\"$text\""; print "
"; print "$text

\n"; } else { print "$text"; } print "
\n"; print" \n"; 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"; print "

Are you sure you want to delete the "; print "$INPUT{'delad'} advert?\n"; print "\n"; 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"; print" \n"; 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/; $_; }