#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
$basepath = '/var/www/www.letterville.com/htdocs/csBanner';
#
# csBanner - v1.86 - 052002
#
# Some Windows based servers required the following.
# push(@INC,"/path/to/csBanner/directory");
#####################################################################
#                                                                   #
#    Copyright � 1999-2001 CGISCRIPT.NET - All Rights Reserved     #
#                                                                   #
#####################################################################
#                                                                   #
#          THIS COPYRIGHT INFORMATION MUST REMAIN INTACT            #
#                AND MAY NOT BE MODIFIED IN ANY WAY                 #
#                                                                   #
#####################################################################
#
# When you downloaded this script you agreed to accept the terms 
# of this Agreement. This Agreement is a legal contract, which 
# specifies the terms of the license and warranty limitation between 
# you and CGISCRIPT.NET. You should carefully read the following 
# terms and conditions before installing or using this software.  
# Unless you have a different license agreement obtained from 
# CGISCRIPT.NET, installation or use of this software indicates 
# your acceptance of the license and warranty limitation terms
# contained in this Agreement. If you do not agree to the terms of this
# Agreement, promptly delete and destroy all copies of the Software.
#
# Versions of the Software 
# Only one copy of the registered version of CGISCRIPT.NET 
# may used on one web site.
# 
# License to Redistribute
# Distributing the software and/or documentation with other products
# (commercial or otherwise) or by other than electronic means without
# CGISCRIPT.NET's prior written permission is forbidden.
# All rights to the CGISCRIPT.NET software and documentation not expressly
# granted under this Agreement are reserved to CGISCRIPT.NET.
#
# Disclaimer of Warranty
# THIS SOFTWARE AND ACCOMPANYING DOCUMENTATION ARE PROVIDED "AS IS" AND
# WITHOUT WARRANTIES AS TO PERFORMANCE OF MERCHANTABILITY OR ANY OTHER
# WARRANTIES WHETHER EXPRESSED OR IMPLIED.   BECAUSE OF THE VARIOUS HARDWARE
# AND SOFTWARE ENVIRONMENTS INTO WHICH CGISCRIPT.NET MAY BE USED, NO WARRANTY 
# OF FITNESS FOR A PARTICULAR PURPOSE IS OFFERED.  THE USER MUST ASSUME THE
# ENTIRE RISK OF USING THIS PROGRAM.  ANY LIABILITY OF CGISCRIPT.NET WILL BE
# LIMITED EXCLUSIVELY TO PRODUCT REPLACEMENT OR REFUND OF PURCHASE PRICE.
# IN NO CASE SHALL CGISCRIPT.NET BE LIABLE FOR ANY INCIDENTAL, SPECIAL OR
# CONSEQUENTIAL DAMAGES OR LOSS, INCLUDING, WITHOUT LIMITATION, LOST PROFITS
# OR THE INABILITY TO USE EQUIPMENT OR ACCESS DATA, WHETHER SUCH DAMAGES ARE
# BASED UPON A BREACH OF EXPRESS OR IMPLIED WARRANTIES, BREACH OF CONTRACT,
# NEGLIGENCE, STRICT TORT, OR ANY OTHER LEGAL THEORY. THIS IS TRUE EVEN IF
# CGISCRIPT.NET IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. IN NO CASE WILL
# CGISCRIPT.NET' LIABILITY EXCEED THE AMOUNT OF THE LICENSE FEE ACTUALLY PAID
# BY LICENSEE TO CGISCRIPT.NET.
#
# Credits:
# Andy Angrick - Programmer - angrick@cgiscript.net
# Mike Barone - Design - mbarone@cgiscript.net
#
# For information about this script or other scripts see 
# http://www.cgiscript.net
#
# Thank you for trying out our script.
# If you have any suggestions or ideas for a new innovative script
# please direct them to suggest@cgiscript.net.  Thanks.
#
########################################################################
#                       Config Variables                               #
########################################################################

(! -e "$basepath/setup.cgi")?($nosetup=1):(require("$basepath/setup.cgi"));
$flock=1;

########################################################################
#                       End Config Variables                           #
########################################################################
require("$basepath/libs.cgi");
$in{'cgiurl'} = $cgiurl.'/csBanner.cgi';
(!$htmlpath)&&($htmlpath=$cgipath);
(!$htmlurl)&&($htmlurl=$cgiurl);
$in{'htmlurl'} = $htmlurl;

$edd = "default%2edb";
$dd="default";
$in{'cinfo'} = qq|
<p align="center"><font size=2 face=verdana><b><a href="http://www.cgiscript.net"><font color="#3366FF" size="1">Powered
        by csBanner - � 2000,2001 CGIScript.net</font></a></b></font>
|;

$| = 1;	
&main;	
exit; 

sub main{
($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data/i)?(&getdata(1)):(&getdata());
($in{'command'} eq 'click')&&(&ClickThrough);
print "Content-type: text/html\n\n";
$in{'database'} =~ s/%(..)/pack("c",hex($1))/ge;
$in{'database'} =~ s/([^\w&=])/'%'.sprintf("%.2x",ord($1))/ge;
($in{'command'} eq '')&&($nosetup)&&(&Setup);
($in{'command'} eq 'savesetup')&&(($nosetup)?(&SaveSetup):(&PError("Error. Permission denied.")));
($in{'command'} eq "login")&&(&Login);
($in{'command'} eq "")&&(&ShowBanners);
&GetLogin;
($in{'command'} eq "adddb")&&(&AddDB);
($in{'command'} eq "showupload")&&(&ShowUpload);
($in{'command'} eq "upload")&&(&Upload);
($in{'command'} eq "add")&&(&Add);
($in{'command'} eq "showlinks")&&(&ShowLinks);
($in{'command'} eq "showadd")&&(&ShowAdd);
($in{'command'} eq "manage")&&(&ShowList);
($in{'command'} eq "showlinks")&&(&ShowLinks);
($in{'command'} eq "vld")&&(&ViewLogDetail);
($in{'command'} eq "resetlogs")&&(&ResetLogs);
($in{'command'} eq "setstyles")&&(&SetStyles);
($in{'command'} eq "reorder")&&(&Reorder);
($in{'command'} eq "showedit")&&(&ShowEdit);
($in{'command'} eq "savechanges")&&(&SaveChanges);
($in{'command'} eq "delete")&&(&Delete);
($in{'command'} eq "view")&&(&ViewOne);
($in{'command'} eq "showadv")&&(&ShowAdv);
($in{'command'} eq "deletedb")&&(&DeleteDB);
($in{'command'} eq "showadddb")&&(&ShowAddDB);
($in{'command'} eq "showcolor")&&(&ShowColor);
($in{'command'} eq "setcolor")&&(&SetColor);
($in{'command'} eq "refresh")&&(&Refresh);
}


sub ShowLinks{
$ssi = $in{'cgiurl'};
$ssi =~ s/http:\/\/.*?\//\//i;
$in{'database'} =~ s/\%/\%25/g;
$in{'ssiurl'} = "
<!-- banner start -->
<!--#include virtual=\"$ssi?group=$in{'database'}\" -->
<!-- banner end -->
";
$in{'jsurl'} = "
<!-- banner start -->
<script language=javascript src=\"$in{'cgiurl'}?group=$in{'database'}&js=Y\"></script>
<!-- banner end -->
";
$in{'ssiurls'} = "
<!-- banner start -->
<!--#include virtual=\"$ssi?group=$in{'database'}&ss=Y\" -->
<!-- banner end -->
";
$in{'jsurls'} = "
<!-- banner start -->
<script language=javascript src=\"$in{'cgiurl'}?group=$in{'database'}&js=Y&ss=Y\"></script>
<!-- banner end -->
";
&PageOut("$htmlpath/t_show_links.htm");
exit;
}

sub ClickThrough{
my $sfile = &CheckSafe("$htmlpath/$in{'group'}");
open(DB,"$sfile");
$found = 0;
while(<DB>){
chop;
(@fields) = split(/\~/,$_);
($fields[0] eq $in{'id'})&&($found = 1)&&(last);
}
close DB;
if($found){
foreach $i (0..$#fields){
  $fields[$i] =~ s/&#(\d+);/pack("c",$1)/ge;
  $fields[$i] =~ s/\\n/\n/g;
  }
&LogMe(2);
print "Location: $fields[2]\n\n";
}
else{
  print "Content-type: text/html\n\n no record found";
  }
exit;
}

sub SlideShow{
(!$in{'group'})&&($in{'group'} = 'default%252edb');
(-e "$htmlpath/$in{'group'}.style")?(require "$htmlpath/$in{'group'}.style"):(require "$htmlpath/styles.pl");
$mgroup = $in{'group'};
$ac=0;

$in{'group'} =~ s/\%/\%25/g;
my $xgroup = $in{'group'};
$delay = $in{'d'}*1000;

my $sfile = &CheckSafe("$htmlpath/$mgroup");
open(DBO,"$sfile");

while(<DBO>){
chomp;
(@fields) = split(/\~/,$_);

foreach $i (0..$#fields){
  $fields[$i] =~ s/&#(\d+);/pack("c",$1)/ge;
  }
  
($fields[7] eq 'SP')&&($fields[3] = '_self');
($fields[7] eq 'NW')&&($fields[3] = '_blank');

&LogMe(1);

if($fields[8]){
  $fields[8] =~ s/\\n//g;
  $ohtml = $fields[8];
  $fields[8] =~ s/"/\\\\"/g;
  $html = $fields[8];
}
else{
  $html = qq|<img src=\\\\"$fields[4]\\\\" border=\\\\"$in{'imgBorder'}\\\\">|;
  $ohtml = qq|<img src=\"$fields[4]\" border=\"$in{'imgBorder'}\">|;
  }
  
if($fields[2]){
  $surl = qq|<a href=\\\\"$in{'cgiurl'}?command=click&group=$xgroup&id=$fields[0]\\\\" target=\\\\"$fields[3]\\\\">|;
  $eurl = '</a>';
  $surl2 = "<a href=\"$in{'cgiurl'}?command=click&group=$xgroup&id=$fields[0]\" target=\"$fields[3]\">";
  }

$ab .= qq|bannerArray[$ac] = "$surl$html$eurl";\n|;

$ac++;
}
close DBO;

(!$fb)&&($fb="$surl2$ohtml</a>");

if($in{'js'} ne 'Y'){
$ab =~ s/\\\\/\\/g;
}

$o = qq|
<ilayer name="l1"><layer name="l2"><div id="l1"><div id="l3">$fb</div></div></layer></ilayer>
<script language="JavaScript">
<!--

// ***********************************************
// AUTHOR: WWW.CGISCRIPT.NET, LLC
// URL: http://www.cgiscript.net
// Use the script, just leave this message intact.
// Download your FREE CGI/Perl Scripts today!
// ( http://www.cgiscript.net/scripts.htm )
// ***********************************************

var bannerArray = new Array();
var myCount=0;
// Banner Code Assignment
$ab

function bannerRotate() {

if(myCount > bannerArray.length-1){myCount=0;}

// Write out rotation
	if (document.all){			// it is IE
		document.all.l3.innerHTML=bannerArray[myCount];
	}

	else if (document.layers){	// it is NN
	document.l1.document.l2.document.write(bannerArray[myCount]);
	document.l1.document.l2.document.close();
	}
else{
   var myelement = document.getElementById("l3");
   myelement.innerHTML=bannerArray[myCount];
}
setTimeout("bannerRotate()", $delay);
myCount++;
}

setTimeout("bannerRotate()", $delay);
// -->
</script>

|;
  
if($ab){
  if($in{'js'}){
    &OutJS($o);
    }
  else{
    print $o;
    }
  }
else{
  print "\n";
  }

exit;
}


sub ShowBanners{
($in{'ss'} eq 'Y')&&(&SlideShow);
(!$in{'group'})&&($in{'group'} = 'default%252edb');
(-e "$htmlpath/$in{'group'}.style")?(require "$htmlpath/$in{'group'}.style"):(require "$htmlpath/styles.pl");
my $sfile = &CheckSafe("$htmlpath/$in{'group'}.pool");
open(DB,"$sfile")||print "$!:$htmlpath/$in{'group'}.pool<br>";
while(<DB>){
chomp;
push(@b,$_);
}
close DB;
$t = $#b+1;
open(DB,"$htmlpath/$in{'group'}.count");
$w = <DB>;
close DB;
$w++;
($w > $t)&&($w=1);
open(DB,">$htmlpath/$in{'group'}.count");
print DB $w;
close DB;

$id = $b[$w-1];

&CheckSafe("$htmlpath/$in{'group'}");
open(DB,"$htmlpath/$in{'group'}");
$found = 0;
while(<DB>){
chop;
(@fields) = split(/\~/,$_);
($fields[0] eq $id)&&($found = 1)&&(last);
}
close DB;

if($found){
foreach $i (0..$#fields){
  $fields[$i] =~ s/&#(\d+);/pack("c",$1)/ge;
  $fields[$i] =~ s/\\n/\n/g;
  }

($fields[7] eq 'SP')&&($fields[3] = '_self');
($fields[7] eq 'NW')&&($fields[3] = '_blank');

&LogMe(1);  
$in{'group'} =~ s/\%/\%25/g;

if($fields[8]){
  $html = $fields[8];
  }
else{
  $html = "<img src=\"$fields[4]\" border=\"$in{'imgBorder'}\">";
  }
  
if($fields[2]){
$surl = "<a href=\"$in{'cgiurl'}?command=click&group=$in{'group'}&id=$fields[0]\" target=\"$fields[3]\">";
  $eurl = '</a>';
  }

if($in{'position'} eq 'TOP'){
  $top = "$surl<font face=\"$in{'TitleFace'}\" size=\"$in{'TitleSize'}\" color=\"#$in{'TitleTableText'}\">$fields[1]</font>$eurl";
  $bottom = "$surl$html$eurl";
  }
elsif($in{'position'} eq 'BOTTOM'){
  $bottom = "$surl<font face=\"$in{'TitleFace'}\" size=\"$in{'TitleSize'}\" color=\"#$in{'TitleTableText'}\">$fields[1]</font>$eurl";
  $top = "$surl$html$eurl";
  }
else{
  $top = "$surl$html$eurl";
  }
if($in{'js'}){
  $o = "<table border=0 cellpadding=0 cellspacing=0><tr><td align=center>$top</td></tr><tr><td align=center>$bottom</td></tr></table>";
  &OutJS($o);
}

else{
  print "<table border=0 cellpadding=0 cellspacing=0><tr><td align=center>$top</td></tr><tr><td align=center>$bottom</td></tr></table>";
  }
}
else{
print "Error. No id found.<br>";
}


exit;
}

sub GetLogin{
&GetCookies;
$in{'UserName'} = $cookie{'UserName'};
$in{'PassWord'} = $cookie{'PassWord'};
if(!$in{'UserName'}){
&PageOut("$htmlpath/t_login.htm");
exit;
}
else{
  if(($in{'UserName'} eq $username)&&($in{'PassWord'} eq $password)){
    #good to go.
    $in{'mpage'} = 'manager';
    return;
    }
 &PError("Error. Invalid username or password");
}
}

sub Login{
&GetDatabases;
&PageOut("$htmlpath/t_login.htm");
exit;
}


sub AddDB{
(!$in{'groupdb'})&&(&PError("Error. Please Enter a Group Name."));
$in{'groupdb'} .= '.db';
$in{'groupdb'} =~ s/([^\w&=])/'%'.sprintf("%.2x",ord($1))/ge;
(-e "$htmlpath/$in{'groupdb'}")&&(&PError("Error. Group already exists. Please use another name."));

&CheckSafe("${htmlpath}/$in{'groupdb'}");
open(DB,">${htmlpath}/$in{'groupdb'}");
close DB;
mkdir "${htmlpath}/banners/$in{'groupdb'}",0775;
print <<"EOF";
<script language=javascript>
var rndURL = (1000*Math.random());
alert("Banner Group $in{'database'} has been created");
window.opener.location = "$in{'cgiurl'}?command=manage&database=$in{'groupdb'}&rnd="+rndURL;
window.close();
</script>
EOF
}


sub Setup{
$cgipath = `pwd`;chomp $cgipath;
$cgiurl = "$ENV{'HTTP_HOST'}/$ENV{'SCRIPT_NAME'}";
$cgiurl =~ s/\/csBanner.cgi//i;
$cgiurl =~ s/\/\//\//g;
$cgiurl = "http://".$cgiurl;
$rooturl = "http://$ENV{'HTTP_HOST'}";
$rootpath = $ENV{'DOCUMENT_ROOT'};

$setup = "\$cgiurl = '$cgiurl';
\$cgipath = '$cgipath';
\$username='demo';
\$password='demo';
1;
";

print <<"EOF";
<font size=4 face=tahoma><b>csBanner Setup</b></font><hr>

<font size=2 face=tahoma><b>Current contents of your setup.cgi file</b><br>Please verify the information and modify if needed:
<form method=post action="csBanner.cgi">
<input type=hidden name=command value=savesetup>
<textarea rows=10 cols=80 name=setup wrap=off>
$setup
</textarea>
<hr>
<input type=submit value="-=Save Changes=-"> <input type=button value="-=Enter Management=-" onClick="window.location='$in{'cgiurl'}?command=manage';">
</form>
<p><font face="Tahoma" size="2"><b>Definitions:</b></font></p>
<p><font face="Tahoma" size="2">\$cgiurl = Full URL to the csBanner DIRECTORY<br>
\$cgipath = Full PATH to the csBanner DIRECTORY<br>
\$username = username to enter management screens<br>
\$password = password to enter management screens<br>
</p>
<p><font face="Tahoma" size="2"><b>Normal Installation Instructions:</b></font></p>
<p><font face="Tahoma" size="2">In most cases, the script is already configured.
Change the \$username and \$password variables to your liking and click 'Save'.
If the setup portion of the script cannot find your sites variables
automatically, you will might have to enter those in the above text area.</font></p>
<p><font face="Tahoma" size="2"><b>CGI-BIN Installation Instructions:</b></font></p>
<p><font face="Tahoma" size="2">If your hosting service <b>will not</b> let you
run scripts outside your <b>cgi-bin</b> directory, then follow these procedures:</font></p>
<p><font face="Tahoma" size="2">Copy all the *.cgi files to a directory in your
cgi-bin directory, making sure they are chmod'd to 755. For example, you could
create a /cgi-bin/csBanner/ directory and place csBanner.cgi, libs.cgi,
and setup.cgi if this file exists.</font></p>
<p><font face="Tahoma" size="2">Create a directory outside your cgi-bin
directory and copy all the remaining files and subdirectories there. For
example, you could create a /cgi-script/csBanner and place the files there.</font></p>
<p><font face="Tahoma" size="2">Edit the above variables (or manually edit
setup.cgi) to the following:<br>
\$cgiurl = URL to the csBanner directory INSIDE your cgi-bin directory (where
the script is installed).<br>
\$cgipath = FULL PATH to the csBanner directory INSIDE your cgi-bin directory
(where the script is installed).<br>
\$rooturl = Full URL to your website<br>
\$rootpath = Full PATH to your website directory<br>
<i><b>ADD THE FOLLOWING VARIABLES TO THE ABOVE CONFIGURATION OR MANUALLY EDIT
setup.cgi:<br>
</b></i>\$htmlurl =&nbsp; FULL URL to the csBanner directory OUTSIDE your cgi-bin
directory (where the remaining files where installed) <br>
\$htmlpath = FULL PATH to the csBanner directory OUTSIDE your cgi-bin
directory (where the remaining files where installed) <br>
For Example, your new setup.cgi file might look something like this:<br>
\$cgiurl='http://www.cgiscript.net/cgi-bin/csBanner';<br>
\$cgipath='/www/vhosts/cgiscript.net/cgi-bin/csBanner';<br>
\$htmlurl='http://www.cgiscript.net/cgi-script/csBanner';<br>
\$htmlpath='/www/vhosts/cgiscript.net/cgi-script/csBanner';<br>
\$username='myusername';<br>
\$password=',mypassword';<br>
1;</font></p>
<p><font face="Tahoma" size="2"><i>(note: the '1' at the end is to prevent
errors from perl if \$password was left empty)</i></font></p>

EOF

exit;
}

sub SaveSetup{
(-e "$basepath/setup.cgi")&&(&PError("Error. Access Denied"));
$in{'setup'} =~ s/\r*\n/\n/g;
open(SETUP,">$basepath/setup.cgi");
print SETUP $in{'setup'};
print SETUP "\n";
close SETUP;
print <<"EOF";
<script language=javascript>
alert("Setup.cgi reconfigured");
window.location = "csBanner.cgi?command=login";
</script>
EOF
exit;
}



sub DeleteDB{
(!$in{'database'})&&(&PError("Error. No group specified."));
$vdb = $in{'database'};
$vdb =~ s/%(..)/pack("c",hex($1))/ge; 
unlink("$htmlpath/$in{'database'}%2edb");
unlink("$htmlpath/$in{'database'}%2edb.style");
opendir(DIR,"$htmlpath/banners/$in{'database'}%2edb");
@files = readdir(DIR);
close DIR;
foreach $i (@files){
  next if ($i =~ /^\./);
  unlink("$htmlpath/banners/$in{'database'}%2edb/$i");
}
rmdir("$htmlpath/banners/$in{'database'}%2edb");
print <<"EOF";
<script language=javascript>
var rndURL = (1000*Math.random());
alert("Banner Group $vdb has been deleted");
window.location = "$in{'cgiurl'}?command=manage&rnd="+rndURL;
</script>
EOF
}


sub Refresh{
&SetStyle;
&ShowAdv;
exit;
}

sub ShowUpload{
&PageOut("$htmlpath/t_upload.htm");
exit;
}

sub Upload{
(-e "$htmlpath/$in{'database'}.style")?(require "$htmlpath/$in{'database'}.style"):(require "$htmlpath/styles.pl");
$rn = &GetRealName($in{'file'});
&CheckExt($rn);
$orn = &GetRealName($in{'oldf'});
unlink("$htmlpath/banners/$in{'database'}/$orn");

($ext) = $rn =~ /.*\.(\w*)$/;
&SaveFile($in{'file'},"$htmlpath/banners/$in{'database'}/$rn");

print <<"EOF";
<script language=javascript>
var rndURL = (1000*Math.random());
alert("Banner Uploaded.");
window.opener.document.form1.BannerURL.value="$in{'htmlurl'}/banners/"+escape("$in{'database'}")+"/"+escape("$rn");
window.close();
</script>
EOF

}



sub ShowColor{
$in{'c'.$in{'cc'}} = checked;
&PageOut("$htmlpath/color_selector.htm");
exit;
}

sub SetColor{
(-e "$htmlpath/$in{'database'}.style")?($dbs =  "$htmlpath/$in{'database'}.style"):($dbs = "${htmlpath}/styles.pl");
&CheckSafe("${dbs}");
open(ADV,"${dbs}")||die print "$! - ${dbs}";
&CheckSafe("$htmlpath/$in{'database'}.style.tmp");
open(TMP,">$htmlpath/$in{'database'}.style.tmp");
while(<ADV>){
(!/'\$in{'$in{'fieldname'}'}'/)&&(!/1;/)&&(print TMP $_); 
}
$in{'colorselect'} =~ s/\\//g;
$in{'colorselect'} =~ s/\"//g;
print TMP "\$in{'$in{'fieldname'}'} = \"$in{'colorselect'}\";\n1;\n";
close ADV;
close TMP;

@s = stat("$htmlpath/$in{'database'}.style.tmp");
($s[7]>0)&&(rename("$htmlpath/$in{'database'}.style.tmp","$htmlpath/$in{'database'}.style"));

print <<"EOF";
<script language=javascript>
window.opener.document.form1.$in{'fieldname'}.value = "$in{'colorselect'}";
window.opener.document.form1.command.value="refresh";
window.opener.document.form1.submit();
window.close();
</script>
EOF

}

sub ShowAddDB{
&PageOut("${htmlpath}/t_adddb.htm");
exit;
}

sub SetStyle{
&CheckSafe("$htmlpath/$in{'database'}.style");
open(STYLE,">$htmlpath/$in{'database'}.style");
foreach $i (sort keys(%in)){
$in{$i} =~ s/&#(\d+);/pack("c",$1)/ge;
next if (($i eq 'command')||($i eq 'cgiurl'));
next if (($i eq 'database')||($i eq 'imagedir'));
next if (($i eq 'format')||($i eq 'managementname'));
next if (($i eq 'UserName')||($i eq 'PassWord'));
next if (($i eq 'htmlurl')||($i eq 'htmlpath'));
next if (($i eq 'cgiurl')||($i eq 'cgipath'));
$in{$i} =~ s/\\//g;
$in{$i} =~ s/@/\\@/g;
$in{$i} =~ s/"/\\"/g;
print STYLE "\$in{'$i'}=\"$in{$i}\";\n";
}
print STYLE "1;\n";
close STYLE;
}

sub SetStyles{
&SetStyle;
print <<"EOF";
<script language=javascript>
var rndURL = (1000*Math.random());
alert("Style has been successfully changes.");
window.location = "$in{'cgiurl'}?command=manage&database=$in{'database'}&rnd="+rndURL;
</script>
EOF
exit;
}

sub ShowAd{
(-e "$htmlpath/$in{'database'}.style")?(require "$htmlpath/$in{'database'}.style"):(require "$htmlpath/styles.pl");

###full page
$in{"selfTitleText$in{'fTitleText'}"}='selected';
(!$in{'position'})&&($in{'BOTTOM'} = 'checked');
(!$in{'imgBorder'})&&($in{'imgBorder'} = '0');
$in{$in{'position'}} = "checked";
$in{'description'} =~ s/([<>"])/'&#'.ord($1).';'/ge;
$in{'URL'} =~ s/([<>"])/'&#'.ord($1).';'/ge;
$in{'target'} =~ s/([<>"])/'&#'.ord($1).';'/ge;
$in{'BannerURL'} =~ s/([<>"])/'&#'.ord($1).';'/ge;
}

sub ShowAdv{
&ShowAd;
&PageOut("$htmlpath/t_banner_advanced_settings.htm");
exit;
}

sub ShowList{
$flip=1;
&GetDatabases;
&GetLogs;
##get total count
&CheckSafe("$htmlpath/$in{'database'}");
open(DB,"$htmlpath/$in{'database'}");
while($line=<DB>){
$tc++;
}
close DB;
&CheckSafe("$htmlpath/$in{'database'}");
open(DB,"$htmlpath/$in{'database'}");
while($line=<DB>){
chomp $line;
$count ++;
(@fields) = split(/\~/,$line);

foreach $i (0..$#fields){
  $fields[$i] =~ s/&#(\d+);/pack("c",$1)/ge;
  $fields[$i] =~ s/([<>"])/'&#'.ord($1).';'/ge;
  }

##make select
for $i (1..$tc){
($i == $count)?($sel = 'selected'):($sel='');
$in{'rowsel'} .= "<option $sel>$i</option>";
}

($flip==1)?($bgc='#CCCCCC'):($bgc='#FFFFFF');
$flip=$flip * -1;
(!$imp{$fields[0]})&&($imp{$fields[0]}='0');
(!$clk{$fields[0]})&&($clk{$fields[0]}='0');
(!$per{$fields[0]})&&($per{$fields[0]}='0');
$desc = $fields[1];
$desc =~ s/([^\w])/'%'.sprintf("%.2x",ord($1))/ge;

if($fields[8]){
  $fields[8] =~ s/&#(\d+);/pack("c",$1)/ge;
  $fields[8] =~ s/\\n/\n/g;
  $html = $fields[8];
}
else{
  $html = "<img src=\"$fields[4]\">";
  }

$in{'line'} .= qq|
  <tr bgcolor="$bgc">
  <td valign=top align=left>
  <select size=1 name="order_$count" onChange="document.lowform.submit()">
  $in{'rowsel'}
  </select>
  </td>
    <td align="center" vAlign="top"><input class="button" type="button" value="Edit" onClick="javascript:Edit('$fields[0]');">��<input class="button" type="button" value="Delete" onClick="javascript:Delete('$fields[0]');"><br><br>
      <table border="1" cellspacing="0" bordercolorlight="#000000" bordercolordark="#FFFFFF">
        <tr>
          <td align="center"><font face="Verdana" size="1"><a href="$in{'cgiurl'}?command=vld&id=$fields[0]&database=$in{'database'}&type=1&desc=$desc" target="_blank">Impressions</a></font></td>
          <td align="center"><font face="Verdana" size="1"><a href="$in{'cgiurl'}?command=vld&id=$fields[0]&database=$in{'database'}&type=2&desc=$desc" target="_blank">Click-Thru</a></font></td>
          <td align="center"><font face="Verdana" size="1">%</font></td>
        </tr>
        <tr>
          <td align="center"><font face="Verdana" size="1">$imp{$fields[0]}</font></td>
          <td align="center"><font face="Verdana" size="1">$clk{$fields[0]}</font></td>
          <td align="center"><font face="Verdana" size="1">$per{$fields[0]}</font></td>
        </tr>
  </table>
    </td>
    <td><font face="verdana" size="2"><center>
    <a href="$fields[2]" target="_blank">$html</a><br>$fields[1]</center>
    </td>
  </tr>
|;
$in{'rowsel'}='';
}

close DB;
(!$in{'line'})&&($in{'line'}="<tr><td colspan=4><font size=2 face=verdana><b>No banners configured</b></td></tr>");
&PageOut("$htmlpath/t_manage.htm");
exit;
}

sub GetDatabases{
opendir(IMG,"$htmlpath/");
@dbfiles = grep(/edb$/,readdir(IMG));
closedir(IMG);
$dfound=0;
foreach $i (sort @dbfiles){
$seldb='';
$dbo = $i;
$dbo =~ s/%(..)/pack("c",hex($1))/ge;
$dbo =~ s/\.db//g;
(!$in{'database'})&&($seldb='selected')&&($in{'database'} = $i)&&($dfound=1);
($i eq $in{'database'})&&($seldb = 'selected');
  $in{'databases'} .= "<option value=\"$i\" $seldb>$dbo</option>\n";
  }
(!$dfound)&&(!$in{'databases'})&&($in{'database'} = $edd)&&($in{'databases'} = "<option value=\"$edd\">$dd</option>");
}

sub Delete{
$id = $in{'id'};
&CheckSafe("$htmlpath/$in{'database'}");
open(DB,"+<$htmlpath/$in{'database'}");
($flock)&&(flock(DB,2));
while(<DB>){
 (@fields) = split(/\~/,$_);
 ($fields[0] eq $id)&&($myfile = $fields[4]);
 push(@l,$_) unless ($fields[0] eq $id);
}


seek(DB, 0, 0);
  foreach (@l) {
    print DB $_;
    }
truncate(DB, tell(DB));

close DB;
($flock)&&(flock(TMP,8));

$myfile =~ s/&#(\d+);/pack("c",$1)/ge;
$myrn = &GetRealName($myfile);
($myrn)&&(unlink("$htmlpath/links/$in{'database'}/$myrn"));

&ReCalPool;
print <<"EOF";
<script language=javascript>
var rndURL = (1000*Math.random());
alert("Item deleted.");
window.location = "$in{'cgiurl'}?command=manage&database=$in{'database'}&rnd="+rndURL;
</script>
EOF
exit;
}

sub SaveChanges{
&CheckVars;
(-e "$htmlpath/$in{'database'}.style")?(require "$htmlpath/$in{'database'}.style"):(require "$htmlpath/styles.pl");
$id = $in{'id'};
(!$in{'weight'})&&($in{'weight'} = '1');
foreach $i (keys(%in)){
$in{$i} =~ s/&#(\d+);/pack("c",$1)/ge; 
}
&GetOutVars;

&CheckSafe("$htmlpath/$in{'database'}");
open(DB,"+<$htmlpath/$in{'database'}");
($flock)&&(flock(DB,2));
while(<DB>){
 (@fields) = split(/\~/,$_);
 ($fields[0] eq $id)?(push(@l,"$newentry\n")):(push(@l,$_));
}

if (@l) {
  seek(DB, 0, 0);
  foreach (@l) {
    print DB $_;
    }
  truncate(DB, tell(DB));
  }

close DB;
($flock)&&(flock(TMP,8));

&ReCalPool;

print <<"EOF";
<script language=javascript>
var rndURL = (1000*Math.random());
alert("Changes have been saved.");
window.location = "$in{'cgiurl'}?command=manage&database=$in{'database'}&rnd="+rndURL;
</script>
EOF

exit;
}

sub ShowEdit{
&CheckSafe("$htmlpath/$in{'database'}");
open(DB,"$htmlpath/$in{'database'}");
$found = 0;
while(<DB>){
chop;
(@fields) = split(/\~/,$_);
($fields[0] eq $in{'id'})&&($found = 1)&&(last);
}

if($found){
foreach $i (0..$#fields){
$fields[$i] =~ s/&#(\d+);/pack("c",$1)/ge;
$fields[$i] =~ s/\\n/\n/g;
}

foreach $i (0..$#fields){
$fields[$i] =~ s/([<>"])/'&#'.ord($1).';'/ge;
}
$in{'id'} = $fields[0];
$in{'description'} = $fields[1];
$in{'URL'} = $fields[2];
$in{'target'} = $fields[3];
$in{'BannerURL'} = $fields[4];
$in{'BannerHTML'} = $fields[8];
$in{'BannerHTML'} =~ s/\\n/\n/g;
$in{'weight'} = $fields[5];
$authorization = $fields[6];
$in{"a$authorization"} = 'checked';
$in{'sbutton'} = '    Save Changes    ';
$in{'command'} = 'savechanges';
(!$authorization)&&($in{'aY'} = 'checked');
$in{$fields[7]} = 'checked';
(!$in{$fields[7]})&&($in{'SP'} = 'checked');
close HTML;
&PageOut("$htmlpath/t_add_banner.htm");
}
else{
print "Error. No record found.";
}
exit;
}

sub Add{
&CheckVars;
(-e "$htmlpath/$in{'database'}.style")?(require "$htmlpath/$in{'database'}.style"):(require "$htmlpath/styles.pl");
(!$in{'weight'})&&($in{'weight'} = '1');
##get highest ID in DB for unique ID.
&CheckSafe("$htmlpath/$in{'database'}");
open(DB,"$htmlpath/$in{'database'}");
($flock)&&(flock(DB,2));
$id = 0;
while(<DB>){
($idt,@rest) = split("~",$_);
($idt > $id)&&($id = $idt);
}
close DB;
($flock)&&(flock(DB,8));
$id++;
&GetOutVars;

&CheckSafe("$htmlpath/$in{'database'}");
open(DB,"$htmlpath/$in{'database'}");
($flock)&&(flock(DB,2));
open(DBT,">$htmlpath/$in{'database'}.tmp");
($flock)&&(flock(DBT,2));
print DBT "$newentry\n";
while(<DB>){
print DBT $_;
}
close DB;
close DBT;
rename("$htmlpath/$in{'database'}.tmp","$htmlpath/$in{'database'}");
&ReCalPool;
print <<"EOF";
<script language=javascript>
var rndURL = (1000*Math.random());
alert("Banner successfully added.");
window.location = "$in{'cgiurl'}?command=manage&database=$in{'database'}&rnd="+rndURL;
</script>
EOF

exit;
}

sub GetOutVars{
$description = $in{'description'};
$description =~ s/([^\w\s])/'&#'.ord($1).';'/ge;
$url = $in{'URL'};
$url =~ s/([^\w\s])/'&#'.ord($1).';'/ge;
$target = $in{'target'};
$target =~ s/([^\w\s])/'&#'.ord($1).';'/ge;
$BannerURL = $in{'BannerURL'};
$BannerURL =~ s/([^\w\s])/'&#'.ord($1).';'/ge;
$BannerHTML = $in{'BannerHTML'};
$BannerHTML =~ s/([^\w\s])/'&#'.ord($1).';'/ge;
$BannerHTML =~ s/\r*\n/\\n/g;
$weight = $in{'weight'};
$weight =~ s/([^\w\s])/'&#'.ord($1).';'/ge;
$ptarg = $in{'ptarg'};
$authorization = $in{'authorization'};
$newentry = "$id~$description~$url~$target~$BannerURL~$weight~$authorization~$ptarg~$BannerHTML";
$newentry =~ s/\r\n/\\n/g;
$newentry =~ s/\n/\\n/g;
}



sub ShowAdd{
$in{'sbutton'} = '    Add Banner   ';
$in{'command'} = 'add';
(!$in{'authorization'})?($in{'aY'} = 'checked'):($in{'a'.$in{'authorization'}} = 'checked');
(!$in{'ptarg'})?($in{'SP'} = 'checked'):($in{$in{'ptarg'}} = 'checked');
#(!$in{'URL'})&&($in{'URL'} = "http://");
&PageOut("$htmlpath/t_add_banner.htm");
exit;
}

sub Reorder{
##get total count
&CheckSafe("$htmlpath/$in{'database'}");
open(DB,"$htmlpath/$in{'database'}");
while($line=<DB>){
$tc++;
@f=split("~",$line);
($in{'order_'.$tc} > $tc)&&($pre='2');
($in{'order_'.$tc} < $tc)&&($pre='0');
($in{'order_'.$tc} == $tc)&&($pre='1');

$o = sprintf("%.3d",$in{'order_'.$tc});
$l{$o.$pre.'-'.$f[0]} = $line;
}
close DB;

open(OUT,">$htmlpath/~$in{'database'}");
for $i (sort keys %l){
print OUT $l{$i};
}
close OUT;

$tc=0;
$in{'w'} = 'lowform';
@s = stat("$htmlpath/~$in{'database'}");
($s[7]>0)&&(rename("$htmlpath/~$in{'database'}","$htmlpath/$in{'database'}"));
&ShowList;
}


sub GetRealName{
local($filename) = @_;
    if ($filename =~ /\//) {
	@array = split(/\//, $filename);
	$real_name = pop(@array);
    } elsif ($filename =~ /\\/) {
	@array = split(/\\/, $filename);
	$real_name = pop(@array);
    } else {
	$real_name = "$filename";
    }
return $real_name;
}


sub SaveFile {
local($filename,$outfile)=@_;
$dm=0;
&CheckExt($outfile);
(! -e "$htmlpath/banners/$in{'database'}")&&(mkdir "$htmlpath/banners/$in{'database'}",0755);
&CheckSafe("$outfile");
    if (!open(OUTFILE, ">$outfile")) {
    &PError("Error. There was an error saving your attachment.");
    }
    binmode(OUTFILE);
    while ($bytesread = read($filename,$buffer,1024)) {
        $totalbytes += $bytesread;
        if(($in{'maxsize'})&&($totalbytes > ($in{'maxsize'} * 1000))){
          close OUTFILE;
          unlink("$outfile");
          &PError("Error. Maximum filesize exceeded.");
        }
        print OUTFILE $buffer;               
    }
    close($filename);
    close(OUTFILE);
}

sub CheckExt{
local($rn) = @_;
#check file extension.
if($in{'fta'}){
  ($ext) = $rn =~ /.*\.(\w*)$/;
  (@fx) = split(",",$in{'fta'});
    foreach $i (@fx){
      ($i =~ /$ext/i)&&($found=1);
      }
  (!$found)&&(&PError("Error. Only $in{'fta'} types are permitted"));
  }
$found=0;
if($in{'ftr'}){
  ($ext) = $rn =~ /.*\.(\w*)$/;
  (@fx) = split(",",$in{'ftr'});
    foreach $i (@fx){
      ($i =~ /$ext/i)&&($found=1);
      }
  ($found)&&(&PError("Error. $in{'ftr'} types are NOT permitted"));
  }
  
}


sub ReCalPool{
&CheckSafe("$htmlpath/$in{'database'}");
open(DB,"$htmlpath/$in{'database'}");
while(<DB>){
  (@f) = split("~",$_);
  next if ($f[6] eq 'N');
  $count++;
  $banner{$f[0]} = $f[5];
  }
close DB;

foreach $i (keys %banner){
  for $x (1..$banner{$i}){
    push(@b,$i);
    }
  }

srand(time|$$);
$t = $#b+1;

while(!$finished){
$r = int(rand($t));
next if ($u{$r});
$u{$r} = 1;
$mc++;
$random[$mc] = $b[$r];
#print "$mc = $b[$r];<br>";
($mc == $t)&&($finished=1);
($mc == 1000)&&($finished=1);
}

&CheckSafe("$htmlpath/$in{'database'}.pool");
open(DB,">$htmlpath/$in{'database'}.pool");
($flock)&&(flock(DB,2));
foreach $i (1..$#b+1){
next if (!$random[$i]);
print DB "$random[$i]\n";
}
close DB;
($flock)&&(flock(DB,8));
}


sub OutJS{
local($o) = @_;
$o =~ s/\r*\n/\n/g;
(@lines) = split("\n",$o);

foreach $i (@lines){
$i =~ s/\"/\\"/g;
$i =~ s/\(/\\(/g;
$i =~ s/\)/\\)/g;
$i =~ s/script/scr\"\+\"ipt/gsi;
print "document.write(\"$i\\n\");\n";
}
exit;
}

sub LogMe{
local($t) = @_;
(-e "$htmlpath/$in{'group'}.style")?(require "$htmlpath/$in{'group'}.style"):(require "$htmlpath/styles.pl");
if($in{'dl'}){
  &CheckSafe("$htmlpath/$in{'group'}.log");
  open(DB,">>$htmlpath/$in{'group'}.log");
  ($flock)&&(flock(DB,2));
  $mt = time;
  print DB "$mt\t$fields[0]\t$ENV{'HTTP_REFERER'}\t$ENV{'REMOTE_ADDR'}\t$t\n";
  close DB;
  ($flock)&&(flock(DB,2));
}
else{
  if($t==1){
    dbmopen(%imp,"$htmlpath/$in{'group'}.imp",0644);
    $imp{$fields[0]}++;
    dbmclose %imp;
    }
  else{
    dbmopen(%clk,"$htmlpath/$in{'group'}.clk",0644);
    $clk{$fields[0]}++;
    dbmclose %clk;
    }
  }
&AutoResetLogs;
}

sub GetLogs{
(-e "$htmlpath/$in{'database'}.style")?(require "$htmlpath/$in{'database'}.style"):(require "$htmlpath/styles.pl");
&GetNoIP;
dbmopen(%timp,"$htmlpath/$in{'database'}.imp",0644);
foreach $i (keys %timp){
  $imp{$i} = $timp{$i};
  }
dbmclose %timp;
dbmopen(%tclk,"$htmlpath/$in{'database'}.clk",0644);
foreach $i (keys %tclk){
  $clk{$i} = $tclk{$i};
  }
dbmclose %tclk;
&CheckSafe("$htmlpath/$in{'database'}.log");
open(DB,"$htmlpath/$in{'database'}.log");
while(<DB>){
chomp;
(@l) = split("\t",$_);
next if ($np{$l[3]});
($l[4] eq '1')&&($imp{$l[1]}++);
($l[4] eq '2')&&($clk{$l[1]}++);
}
close DB;

foreach $t (keys %imp){
($clk{$t})&&($per{$t} = sprintf("%.2f",($clk{$t}/$imp{$t})*100));
}

}

sub ViewLogDetail{
(-e "$htmlpath/$in{'database'}.style")?(require "$htmlpath/$in{'database'}.style"):(require "$htmlpath/styles.pl");
$id = $in{'id'};
$type = $in{'type'};
$desc = $in{'desc'};
&GetNoIP;
&GetLogs;
print <<"EOF";
<table border=1>
<tr><td colspan=2><font size=4 face=verdana><b>Lifetime Stats</b></td></tr>
<tr><td><font size=2 face=verdana><b>Impressions</td><td><font size=2 face=verdana>$imp{$id}</td></tr>
<tr><td><font size=2 face=verdana><b>Click-throughs</td><td><font size=2 face=verdana>$clk{$id}</td></tr>
</table><br><br>
EOF
&CheckSafe("$htmlpath/$in{'database'}.log");
open(DB,"$htmlpath/$in{'database'}.log");
($type == 1)?($title = 'Impressions'):($title = 'Click-throughs');
print <<"EOF";
<table border=1>
<tr><td colspan=3><font size=4 face=verdana><b>$title<br>$desc </b>(current log file)</td></tr>
<tr><td><font size=2 face=verdana><b>Date</td><td><font size=2 face=verdana><b>IP Address</td><td><font size=2 face=verdana><b>Referrer</td></tr>
EOF

while(<DB>){
chomp;
($ts,$myid,$referer,$ipaddr,$mytype) = split("\t",$_);
next if ($myid != $id);
next if ($mytype != $type);
next if ($np{$ipaddr});
(!$referer)&&($referer = 'none');
$ipz{$ipaddr}++;
$refz{$referer}++;
$date = &ctime($ts);
$mytotal++;
print qq|<tr><td><font face=verdana size=2>$date&nbsp;</td><td><font face=verdana size=2>$ipaddr&nbsp;</td><td><font face=verdana size=2>$referer&nbsp;</td></tr>|;
}
print qq|<tr><td colspan=2 align=right><font face=verdana size=2>Total:</td><td align=right><font face=verdana size=2><b>$mytotal</td></tr>|;

print "</table>";
close DB;

foreach $i (keys %ipz){
$idx = sprintf("%.9d",$ipz{$i});
$ipzt{$idx."\t".$i}=1;
}

foreach $i (keys %refz){
$idx = sprintf("%.9d",$refz{$i});
$refzt{$idx."\t".$i}=1;
}

print <<"EOF";
<br><br>
<table border=1>
<tr><td colspan=2><font size=4 face=verdana><b>Top 10 Referrers<br>$desc </b>(current log file)</td></tr>
<tr><td><font size=2 face=verdana><b>Referrer</td><td><font size=2 face=verdana><b>&nbsp;</td></tr>
EOF

foreach $i (sort {$b cmp $a} keys %refzt){
($i,$r) = split("\t",$i);
$i=$i+0;
($c++)&&($c==11)&&(last);
print qq|<tr><td><font size=2 face=verdana>$r</td><td><font size=2 face=verdana>$i</td></tr>|;
}
print "</table>";

print <<"EOF";
<br><br>
<table border=1>
<tr><td colspan=2><font size=4 face=verdana><b>Top 10 IP Addresses<br>$desc </b>(current log file)</td></tr>
<tr><td><font size=2 face=verdana><b>IP</td><td><font size=2 face=verdana><b>&nbsp;</td></tr>
EOF
$c=0;
foreach $i (sort {$b cmp $a} keys %ipzt){
($i,$r) = split("\t",$i);
$i=$i+0;
($c++)&&($c==11)&&(last);
print qq|<tr><td><font size=2 face=verdana>$r</td><td><font size=2 face=verdana>$i</td></tr>|;
}
@s = stat("$htmlpath/$in{'database'}.log");
print <<"EOF";
</table>
<hr><center><font face=verdana size=2>Log file size: $s[7] bytes
<form method=post action="$in{'cgiurl'}">
<input type=hidden name=command value=resetlogs>
<input type=hidden name=database value="$in{'database'}">
<input type=submit value="-= Clear Logs =-">
</form>
</center>
EOF

exit;
}

sub GetNoIP{
@noip = split(/\r*\n/,$in{'ipnotlog'});
foreach $i (@noip){
  $np{$i}=1;
  }
}

sub CheckVars{
(!$in{'description'})&&(&PError2("Error. Please enter a description."));
(!$in{'BannerURL'})&&(!$in{'BannerHTML'})&&(&PError2("Error. Please enter either a banner URL or HTML code."));
(!$in{'weight'})&&(&PError2("Error. Please enter a weight"));
(!$in{'ptarg'})&&(&PError2("Error. Please enter a target"));
($in{'BannerHTML'})&&($in{'BannerURL'} = $in{'BannerHTML'});
}

sub AutoResetLogs{

@s = stat("$htmlpath/$in{'group'}.log");

(!$in{'rl'})&&(return);
($s[7] < ($in{'rl'}*1000000))&&(return);

&GetLogs;
dbmopen(%timp,"$htmlpath/$in{'group'}.imp",0644);
foreach $i (keys %imp){
   $timp{$i} = $imp{$i};
  }
dbmclose %timp;
dbmopen(%tclk,"$htmlpath/$in{'group'}.clk",0644);
foreach $i (keys %clk){
  $tclk{$i}=$clk{$i};
  }
dbmclose %tclk;
($in{'group'})&&(unlink("$htmlpath/$in{'group'}.log"));
}

sub ResetLogs{
(!$in{'database'})&&(&PError("Error. No log file specified"));
&GetLogs;

dbmopen(%timp,"$htmlpath/$in{'database'}.imp",0644);
foreach $i (keys %imp){
   $timp{$i} = $imp{$i};
  }
dbmclose %timp;
dbmopen(%tclk,"$htmlpath/$in{'database'}.clk",0644);
foreach $i (keys %clk){
  $tclk{$i}=$clk{$i};
  }
dbmclose %tclk;

unlink("$htmlpath/$in{'database'}.log");
print <<"EOF";
<script language=javascript>
var rndURL = (1000*Math.random());
alert("Logs cleared.");
//window.location = "$in{'cgiurl'}?command=$in{'command'}&database=$in{'database'}&rnd="+rndURL;
window.close();
</script>
EOF
exit;
}

sub PError2{
local($mess) = @_;
($in{'command'} eq 'add')?($in{'command'} = 'showadd'):($in{'command'} = 'showedit');
print <<"EOF";
<script language=javascript>
alert("$mess");
var URL = "$in{'cgiurl'}?command=$in{'command'}&database=$in{'database'}&description="+escape("$in{'description'}");
URL = URL + "&URL="+escape("$in{'URL'}")+"&BannerURL="+escape("$in{'BannerURL'}")+"&authorization=$in{'authorization'}";
URL = URL + "&id=$in{'id'}&weight="+escape("$in{'weight'}")+"&ptarg="+escape("$in{'ptarg'}")+"&target="+escape("$in{'target'}");
window.location = URL
</script>
EOF
exit;
}

sub CheckSafe{
my ($file) = @_;
($file =~ /\.\./)&&(&PError("Error. Invalid filename"));
($file =~ /\|/)&&(&PError("Error. Invalid filename"));
return $file;
}
