Code:
#!/usr/bin/perl
use strict;
my $lib_path;BEGIN{my $path=sub{my $file=(caller)[1];my $idx=rindex($file,'/');$idx=rindex($file,'\\') if($idx==-1);return substr($file,0,$idx+1);};$lib_path=&$path;}
use lib "${lib_path}lib";
use dblib;
use banner;
use geoip;
$|=1;
my($zone_id)=$ENV{'QUERY_STRING'}=~/zone_id=(\d+)/i;
my $popup=0;my $iframe=0;my $img=0;my $refresh=0;my $kewords='';
if($ENV{'QUERY_STRING'}=~/popup/i){$popup=1};
if($ENV{'QUERY_STRING'}=~/iframe/i){$iframe=1};
if($ENV{'QUERY_STRING'}=~/mode=img/i){$img=1};
if($ENV{'QUERY_STRING'}=~/refresh=(\d+)/i){$refresh=$1};
if($ENV{'QUERY_STRING'}=~/&keywords=(.*?)$/i){$kewords=$1};
my($IIS,$yypath,$yyheader);if($ENV{'SERVER_SOFTWARE'}=~/IIS/){$IIS=1;$yyheader="HTTP/1.0";}else{$yyheader="Status:";}
my $dbh=$dblib::dbh;
my $ext_file=$dblib::ext_file;
my $server_url=$dblib::server_url;
my $cc=geoip::country_code_by_addr($ENV{REMOTE_ADDR});
my($sec,$min,$hour,$mday,$mon,$year,$wday)=localtime(time);$hour=sprintf("%02d",$hour);
my $str="";
if($kewords ne ''){$str=" AND b.t_keywords LIKE '%$kewords%' ";}
my $sql="SELECT a.camp_id,c.banner_id FROM ad_camp_zone AS a,ad_camp AS b,ad_camp_banner AS c WHERE a.camp_id=b.id AND a.camp_id=c.camp_id AND a.active=1 AND b.active=1 AND c.active=1 AND (b.t_geo='' OR b.t_geo IS NULL OR b.t_geo LIKE '%$cc%') AND (b.t_time='' OR b.t_time IS NULL OR b.t_time LIKE '%$hour%') AND a.zone_id='$zone_id'".$str."ORDER BY RAND() LIMIT 1";
my $sth=$dbh->prepare($sql);$sth->execute;
my($camp_id,$banner_id)=$sth->fetchrow_array;$sth->finish;
$sth=$dbh->prepare("SELECT client_id,title,banner,width,height,format,url,target,status_text,banner_type,f_color,f_size,f_under,b_color,text_banner,f_font,f_style,f_pos FROM ad_banner WHERE id=?");$sth->execute($banner_id);
my($client_id,$title,$banner,$width,$height,$format,$url,$target,$status_text,$banner_type,$f_color,$f_size,$f_under,$b_color,$text_banner,$f_font,$f_style,$f_pos)=$sth->fetchrow_array;$sth->finish;
banner::log_impression($dbh,($banner_id or 0),($zone_id or 0),($client_id or 0),($camp_id or 0),$ENV{HTTP_REFERER},$ENV{REMOTE_ADDR});
if($IIS!=1)
{
my $cook_policy_url=$server_url."/ad_images/w3c/p3p.xml";$cook_policy_url=~s/\/\//\//gi;$cook_policy_url=~s/http:\//http:\/\//gi;
print "P3P: policyref=\"$cook_policy_url\", CP=\"NOI PSA PSD OUR IND UNI NAV STA INT\"\n";
print "Set-Cookie: awp=$banner_id-$client_id-$url\n";
print "Cache-Control: no-cache, must-revalidate\n";
print "Cache-Control: post-check=0,pre-check=0\n";
print "Cache-Control: max-age=0\n";
print "Pragma: no-cache\n";
print "Connection: Close\n";
}
if($img)
{
my $image_url=$dblib::image_url;
if($format)
{
print "$yyheader 302 Moved\nLocation: ".$server_url.$image_url.$banner_id.".".$format."\n\n";
}
else
{
print "Content-type: image/gif\n\n";
print chr(0x47).chr(0x49).chr(0x46).chr(0x38).chr(0x39).chr(0x61).chr(0x01).chr(0x00).chr(0x01).chr(0x00).chr(0x80).chr(0x00).chr(0x00).chr(0x04).chr(0x02).chr(0x04).chr(0x00).chr(0x00).chr(0x00).chr(0x21).chr(0xF9).chr(0x04).chr(0x01).chr(0x00).chr(0x00).chr(0x00).chr(0x00).chr(0x2C).chr(0x00).chr(0x00).chr(0x00).chr(0x00).chr(0x01).chr(0x00).chr(0x01).chr(0x00).chr(0x00).chr(0x02).chr(0x02).chr(0x44).chr(0x01).chr(0x00).chr(0x3B);
}
exit;
}
print "Content-type: text/html\n\n";
if($banner_id)
{
$ENV{'SCRIPT_NAME'}=~s/advert\./adclick\./ig;
$url=$server_url.$ENV{'SCRIPT_NAME'}."?id=".$banner_id."&cid=".$client_id."&zid=".$zone_id."&cpid=".$camp_id."&target=".$url;
$url=~s/\/\//\//gi;$url=~s/http:\//http:\/\//gi;
my $output=banner::get_banner_template($banner_id,$title,$banner,$width,$height,$format,$url,$target,$status_text,$banner_type,$f_color,$f_size,$f_under,$b_color,$text_banner,$f_font,$f_style,$f_pos);
print "<html><head><title>$title</title><meta http-equiv=\"Content-Type\" content=\"text/html; \"><META HTTP-EQUIV='expires' CONTENT='Jan 01, 1994'><META HTTP-EQUIV='pragma' CONTENT='no-cache'>";
if($refresh){print "<META HTTP-EQUIV='refresh' CONTENT='$refresh'>";}
print "</head>";
print "<body style=\"background-color:transparent\" marginheight='0' marginwidth='0' leftmargin='0' topmargin='0'>";
print $output;
print "</body></html>";
}
else
{
my $sth=$dbh->prepare("SELECT show_default,width,height,font_color,back_color,show_stat,stat_today,hit_color,ptext FROM ad_zone WHERE id=?");$sth->execute($zone_id);
my($show_default,$width,$height,$font_color,$back_color,$show_stat,$stat_today,$hit_color,$ptext)=$sth->fetchrow_array;
if($show_default && $zone_id)
{
my $ptext2=$ptext;$ptext2=~s/<br>/\n/gi;$ptext2=~s/<p>/\n\n/gi;$ptext2=~s/<\/p>//gi;
use stats;
my $hits=stats::today_zone_imp($dbh,"views",$zone_id);
my $dest=$ENV{'SCRIPT_NAME'};$dest=~s/advert\./client\./ig;$dest=$server_url.$dest;
$ENV{'SCRIPT_NAME'}=~s/advert\./adclick\./ig;
$url=$server_url.$ENV{'SCRIPT_NAME'}."?zid=".$zone_id."&target=".$dest;
$url=~s/\/\//\//gi;$url=~s/http:\//http:\/\//gi;
my $width_d=$width-5;
my $placeholder="<div style='position:relative;left:0px;top:0px;background-color:$back_color;height:$height;width:$width'>";
$placeholder.="<layer bgcolor='$back_color' height=$height width=$width>";
$placeholder.="<a href='$url' target=_blank><img src='$server_url/ad_images/spacer.gif' width=$width height=$height border=0 alt='$ptext2'></a>";
$placeholder.="<div style='position:absolute;left:5px;top:5px;width:".$width_d."px;font-family:verdana;font-size:10px;font-weight:bold;color:$font_color'><a href='$url' target=_blank style='text-decoration:none;color:$font_color'>";
if($show_stat eq 'true' or $show_stat==1)
{
$placeholder.="Views: <b style='color:$hit_color'>$hits</b><br><br><center>$ptext</center>";
}
else
{
$placeholder.="<center>$ptext</center>";
}
$placeholder.="</a></div></layer></div>";
print "<html><head><title>$ptext</title></head>";
print "<body marginheight=0 style=\"background-color:transparent\" marginwidth=0 leftmargin=0 topmargin=0>";
print $placeholder;
print "</body></html>";
}
}
$dbh->disconnect;
1;