#!/usr/bin/perl
# NOTE: the above line must be changed to show the path to the
# Perl interpreter on your system! Leave the #! as is, but
# the path may have to be changed if yours is different.
# DO NOT CHANGE the LINE BELOW.
$| = 1;
#########################################################################
# SFEBanner v1.0 #
# A very simple banner rotation program. It it intended for use with #
# Commission Junction or friends' banners where there is either no #
# need for clickthrough statistics or where clickthrough statistics #
# are maintained by the affiliate company. This program rotates banners #
# only, and does not maintain any statistics whatsoever. #
# Copyright 2002, Kristina L. Pfaff-Harris #
# All rights reserved. #
# #
#########################################################################
##### Licensing: #
##### #
##### This program may be used free of charge under the following #
##### conditions: #
##### #
##### 1. All instructions and Copyright lines must remain unchanged. #
##### #
##### 2. All pages generated by the program must contain one of the #
##### following pieces of HTML code: #
##### #
##### SFEBanner can be found at: #
##### #
##### http://www.tesol.net/scripts #
##### OR: #
##### #
##### #
##### #
##### 3. You may not sell or distribute this program. You may charge #
##### a reasonable fee for installing it for a client as long as #
##### you make it clear that you are not the author, and you are #
##### not selling the program to them: only charging for installing #
##### it. #
##### #
##### 4. You agree that this program is offered without warranty of #
##### any kind, including warranty of fitness for a particular #
##### purpose. You further agree that the author and all sites #
##### associated in any way with this program are not liable for #
##### any damage or loss incurred as a result of using this program.#
#########################################################################
##### #
##### IMPORTANT INSTRUCTIONS: #
##### #
##### In this program, I have put **CHANGE** in all the places where #
##### you will need to modify the program to run on your server, so #
##### that you can easily find all the places where changes are #
##### necessary. This program must be chmod 755 or 775 in order to #
##### work, and some files may need to be chmod 766. #
##### #
#########################################################################
# #
# IMPORTANT: If you FTPed this program to your server in "binary" mode, #
# it will NOT work! If you're not sure, please go back and FTP it #
# to your server again and make sure you use "ascii" mode. If you get #
# "500 Server Error", this is almost always the cause. :) #
# #
# Using the program: #
# This program requires the file "sfebanner.cgi" at a minimum in order #
# to work. Please read the README.sfebanner file for more information. #
# #
# I hope this program proves useful to you! Please contact me #
# (http://tesol.net/scriptmail.html) for any bugs or feedback. #
#########################################################################
%data = &get_data();
print "Content-type: text/html\n\n";
#########################################################################
##### #
##### BEGIN SECTION WHERE YOU WILL NEED TO CHANGE THINGS: #
##### #
##### In this section, there are several places where you'll need #
##### to make changes. Please read all instructions carefully #
##### before you make the changes. #
##### #
#########################################################################
# $path_to_banner_file is the full system path to the text file where
# you've put the HTML banner links you want to display. This should NOT
# begin with "http://". On Unix-type systems, it usually looks like
# "/home/data/yourlogin/cgi-bin" and on Windows-type systems, it usually
# looks something like "c:/inetpub/files/yourlogin/". If you have your
# banner file in the same folder with this script, then what I have
# below may work. You must ask your web hosting provider what the full
# system path to your files is if you don't know, since this is different
# on each web server.
$path_to_banner_file = "./banners.txt";
# $I_have_SSI is whether or not you can use Server Side Includes on your
# site. If you do not have server side includes, then this will attempt
# to work with JavaScript, but that doesn't necessarily work on all
# browsers. (Ask your web hosting provider if you have server side
# includes.) If you have server side includes, set
# $I_have_SSI = 1;
# Otherwise, set
# $I_have_SSI = 0;
$I_have_SSI = 0;
# $number_file is just where we keep track of the last banner we
# showed so that we can go through each banner one by one. This is
# the full system path to banner_numbers.txt, similar to $path_to_banner_file
# above.
$number_file = "./banner_numbers.txt";
# $password is a password you'd like to use to add and remove banners
# from the system.
$password = 'password';
# $cgi_url is the full web address (URL) of this script. This should
# probably begin with "http://"
$cgi_url = "http://www.tesol.net/scripts/SFEBanner/sfebanner.cgi";
#########################################################################
##### #
##### END SECTION WHERE THINGS NEED TO BE CHANGED. #
##### #
##### You should not HAVE to change anything beyond this point, #
##### although you might want to read through it to see what it does. #
##### If you're interested in how CGI programs work, then this may #
##### act as its own tutorial of sorts. You definitely shouldn't #
##### change anything beyond this point unless you know what you're #
##### doing, though. :) #
##### #
#########################################################################
$fa = $data{'FA'};
if($fa eq "Add Banner"){
&authenticate();
&add_banner;
exit();
}
elsif ($fa eq "Delete Banner"){
&authenticate();
&delete_banner;
exit();
}
elsif ($fa eq "Log In"){
&authenticate();
&admin_screen;
exit();
}
elsif ($fa eq "admin"){
&show_login_screen;
exit();
}
else {
&show_banner();
exit();
}
sub delete_banner {
if($data{'FA2'} eq "Delete This"){
$/ = "\n\n";
open(FILE, "+<$path_to_banner_file") ||
&debug("Could not open banner file ($path_to_banner_file) $!");
if($^O =~ /win/i){ binmode(FILE);}
else { flock(FILE, 2);}
$i = 0;
while(){
if($i != $data{'bannernum'}){ $newfile .= "$_"; }
$i++;
}
truncate(FILE, length($newfile));
seek(FILE, 0, 0);
print FILE "$newfile";
close(FILE);
$/ = "\n";
print "Banner Deleted. ";
print "";
}
else {
$/ = "\n\n";
open(FILE, "<$path_to_banner_file") ||
&debug("Could not open banner file ($path_to_banner_file): $!");
print "
";
$i = 0;
while(){
print "
";
$i++;
}
print "
";
}
}
sub add_banner {
if($data{'FA2'} ne "Add It" && $data{'FA2'} ne "Confirm Add"){
print "";
exit();
}
elsif($data{'FA2'} eq "Confirm Add"){
print "
$data{'AdCode'}
Above is what your ad will look like. Please click on it to make
sure it works. If not, make any changes you need. When you are
ready, click \"Add It.\". To view your changes, click \"Confirm
Add.\"
";
exit();
}
elsif ($data{'FA2'} eq "Add It"){
$code = $data{'AdCode'};
$code =~ s/\'/'/gs;
$code =~ s/\r\n/ /gs;
$code =~ s/\r/ /gs;
$code =~ s/\n/ /gs;
open(FILE, ">>$path_to_banner_file") ||
&debug("Could not write to banner file ($path_to_banner_file): $!");
print FILE "$code\n\n";
close(FILE);
print "
$data{'AdCode'}
Banner Added.
";
exit();
}
}
sub show_banner {
# First, get the ad code
my $adcode;
open(FILE, "<$number_file") ||
&debug("could not open banner number file ($number_file) $!");
$num = ;
close(FILE);
$/ = "\n\n";
# Gotta get number of entries. Pfeh.
open(FILE, "<$path_to_banner_file") ||
&debug("could not open banner file ($path_to_banner_file) $!");
$i = 0;
while(){ $i++; } close(FILE);
open(FILE, "<$path_to_banner_file") ||
&debug("could not open banner file ($path_to_banner_file) $!");
$j = 0;
while(){
if($j == $num){
$adcode = $_;
}
$j++;
# Javascript is bad if we're doing this with Javascript
if($I_have_SSI != 1){
if($adcode =~ /onmouseover|onmouseout|onblur|onclick|return\s+true|return\s+false/is){
$adcode = ""; next;
}
elsif($adcode ne "") {
last;
}
}
elsif($adcode ne "") {
last;
}
} # end while()
if($adcode eq ""){
$adcode = "Error: cannot display banners with JavaScript when running".
" in Javascript mode.";
$num = 0;
}
close(FILE);
$/ = "\n";
if($j + 1 > $i){$num = 0;}
else { $num = $j; }
open(FILE, ">$number_file") ||
&debug("could not open banner number file ($number_file) $!");
print FILE $num;
close(FILE);
$adcode .= "Powered by SFEBanner";
$adcode =~ s/\r\n/ /sg;
$adcode =~ s/\r/ /sg;
$adcode =~ s/\n/ /sg;
if($I_have_SSI == 1){
print $adcode;
exit();
}
# Try Javascript method.
else {
print "document.write('$adcode');\n\n";
exit();
}
}
sub get_data {
local($string);
# get data
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
$string = $ENV{'QUERY_STRING'};
}
else { read(STDIN, $string, $ENV{'CONTENT_LENGTH'}); }
# split data into name=value pairs
@data = split(/&/, $string);
# split into name=value pairs in associative array
foreach (@data) {
split(/=/, $_);
$_[0] =~ s/\+/ /g; # plus to space
$_[0] =~ s/%00//g; # We don' need no steenking nulls :)
$_[0] =~ s/%0a/newline/g;
$_[0] =~ s/%(..)/pack("c", hex($1))/ge; # hex to alphanumeric
if(defined($data{$_[0]})){
$data{$_[0]} .= "\0";
$data{$_[0]} .= "$_[1]";
}
else {
$data{"$_[0]"} = $_[1];
}
}
# translate special characters
foreach (keys %data) {
$data{"$_"} =~ s/\+/ /g; # plus to space
$data{"$_"} =~ s/%00//g; # We don' need no steenking nulls :)
$data{"$_"} =~ s/%0a/newline/g;
$data{"$_"} =~ s/%(..)/pack("c", hex($1))/ge; # hex to alphanumeric
}
%data; # return associative array of name=value
}
sub authenticate {
if($data{'Password'} ne $password ||
$data{'Password'} eq "" ||
$password eq ""){
$error = "Error: Incorrect password. ";
&show_login_screen();
exit();
}
}
sub show_login_screen {
if($error eq ""){
$error = "Please enter your password for banner maintenance: ";
}
print "";
exit();
}
sub admin_screen {
print "";
exit();
}
sub debug {
if($debugging == 1){
print "DEBUGGING INFO: $_[0] ";
}
}