#!/usr/bin/perl

&call_log;
&get_data;
$view=$data{"View"};
$img=$data{"img"};
$x=$data{"x"};
$y=$data{"y"};
$sescnt=$data{"z"};
$ipaddress =$ENV{'REMOTE_ADDR'};
@nvpairs=split(/; /, $ENV{'HTTP_COOKIE'});

 foreach $pair (@nvpairs) {
         ($name, $value) = split(/=/, $pair);
         $cookie{$name} = $value;
 }

$session = $cookie{'session'};

if ($sescnt eq $session) {$img="";}

print "Set-Cookie\:session=$sescnt\n";
print "Content\-type\:text\/html\n\n";  
$sescnt++;

@color=("254\|254\|254", "73\|62\|40", "234\|56\|28", "179\|135\|196", "246\|159\|90", "255\|213\|13",  "169\|126\|143", "56\|69\|101", "217\|225\|176", "254\|254\|254", "146\|162\|63", "71\|117\|186", "40\|112\|220", "73\|106\|51", "215\|42\|44");




open (FILE, "paintdb");
@paint=<FILE>;
close FILE;

if ($view eq "yes") {&view;exit;}

if ($img ne "") {&add;}



$screen=pop @paint;
chomp $screen;
@screen=split(/\|/,$screen);
@code[0]="fill";
@code[1]=int(rand(15))+1;@code[4]=int(rand(15))+1; @code[3]=int(rand(15))+1; @code[2]=int(rand(15))+1;

if ($img ne "") {@code[1]=$img;}
while ( @code[2] == @code[1]) {
 @code[2]=int(rand(15))+1; }
while (@code[3] == @code[1] || @code[3] ==  @code[2]) {
@code[3]=int(rand(15))+1; }
while (@code[4] == @code[1] || @code[4] == @code[2] || @code[4] == @code[3]) {
@code[4]=int(rand(15))+1; }

$ip=shift @screen;


foreach $box (@screen) {$co=$box-1;
($thisred, $thisblue, $thisgreen)=split(/\|/,@color[$co]);
$allred+=$thisred;
$allblue+=$thisblue;
$allgreen+=$thisgreen;
}

$red=sprintf "%1x", int($allred/96);
$green=sprintf "%1x", int($allgreen/96);
$blue=sprintf "%1x", int($allblue/96);

$redtext=sprintf "%1x", int($allred/384);
$greentext=sprintf "%1x", int($allgreen/384);
$bluetext=sprintf "%1x", int($allblue/384);


$redhi=sprintf "%1x", int($allred/111);
$greenhi=sprintf "%1x", int($allgreen/111);
$bluehi=sprintf "%1x", int($allblue/111);


$w=0;
print <<TABLETOP;
<html><killbanner><head><title>COMPAINT - Community art</title>
<style>
a{text-decoration: none;}</style>
</head><body bgcolor=$red$blue$green link=$redtext$bluetext$greentext vlink=$redtext$bluetext$greentext alink=$redtext$bluetext$greentext>

<center><table height=100% width=100% border=0><tr><td valign=middle>
<center><table width=600 border=0 bgcolor=ffffff  cellpadding=0 cellspacing=0><tr>

TABLETOP


&get_maps;





foreach $box (@screen) {


if ($w == 64 || $w == 65 || $w == 66 || $w == 67) {
$r++;
print <<CLICK;

<td height=50 width=50 valign=center><center><img src=@code[$r].jpg height=48 width=48 usemap=#map$r border=0></center></td>

CLICK


}


else {

print <<ONEBOX;

<td height=50 width=50><img src=$box.jpg height=50 width=50></td>

ONEBOX
}

if ($w%12 == 11) {print "</tr><tr>";}

$w++;
}

print "</tr></table>";


if ($sescnt > 5) {

print <<VIEW;
</tr><tr><td bgcolor=$redhi$bluehi$greenhi>
<centeR>
<a href="compaint.cgi?View=yes"><font size=2 face="Arial, Verdana, Helvetica" color=$redtext$bluetext$greentext><b>view all<b></font></a><p>
VIEW

}

print <<GRAPH;


</td></tr>
</table>




GRAPH






sub add {

$onelast=pop @paint;
chomp $onelast;
push (@paint, $onelast);
@last=split(/\|/,$onelast);
$coord=(($y-1)*12)+$x;
@last[0]=$ipaddress;
@last[$coord]=$img;
$newlast=join("\|",@last);
push (@paint, $newlast);
$newlast.="\n";
open (FILE, ">>paintdb");
print FILE $newlast;
close FILE;

}

sub get_data {
    local($string);

    # get data
    if ($ENV{'REQUEST_METHOD'} eq 'GET') {
        $_ = $string = $ENV{'QUERY_STRING'};
        tr/\"~;/_/;
        $string = $_;

    }                           
    else { read(STDIN, $string, $ENV{'CONTENT_LENGTH'});
        $_ = $string;
        $OK_CHARS='a-zA-Z0-9=&%\n\/_\-\.@';
        tr/\"~;/_/;
        $string = $_;
           }

    # 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/%(..)/pack("c", hex($1))/ge; # hex to alphanumeric
        $data{"$_[0]"} = $_[1];
    }

    # translate special characters
    foreach (keys %data) {
        $data{"$_"} =~ s/\+/ /g; # plus to space
        $data{"$_"} =~ s/%(..)/pack("c", hex($1))/ge; # hex to alphanumeric
    }

    %data;                      # return associative array of name=value
}




sub call_log {

$logfileaxs = 'log.txt';
$domainaxs = 'http://www.blacksunn.net/';
$headeraxs = "Content-type: text/html\n\n";
$ENV{'REMOTE_HOST'} =~ tr/[A-Z]/[a-z]/;

$logline = "|$ENV{'REMOTE_HOST'}|$ENV{'REMOTE_ADDR'}|";
$ENV{'HTTP_REFERER'} =~ s/\|//g;
$logline .= "$ENV{'HTTP_REFERER'}|";
$logline .= "$domainaxs$ENV{'DOCUMENT_URI'}|";
$logline .= "$ENV{'HTTP_USER_AGENT'}|";
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime(time);
$logline .= "$sec|$min|$hour|$mday|$mon|$year|$wday|$yday|";
# $logline .= "export|";
$logline .= "\n";
open(LOG,">>$logfileaxs");
print LOG $logline;
close(LOG);

}





sub get_maps {


@coordmap= ("1,1,4,6", "5,1,8,6", "9,1,13,6", "14,1,17,6", "18,1,21,6" , "22,1,25,6", "26,1,29,6", "30,1,33,6", "34,1,37,6", "38,1,41,6", "42,1,45,6", "46,1,50,6", "1,6,4,12", "5,6,8,12", "9,6,13,12", "14,6,17,12", "18,6,21,12", "22,6,25,12", "26,6,29,12", "30,6,33,12", "34,6,37,12", "38,6,41,12", "42,6,45,12", "46,6,50,12", "1,13,4,19", "5,13,8,19", "9,13,13,19", "14,13,17,19", "18,13,21,19", "22,13,25,19", "26,13,29,19", "30,13,33,19", "34,13,37,19", "38,14,41,19", "42,14,45,19", "46,14,50,19", "1,20,4,26", "5,20,8,26", "9,20,13,26", "14,20,17,26", "18,20,21,26", "22,20,25,26", "26,20,29,26", "30,20,33,26", "34,20,37,26", "38,20,41,26", "42,20,45,26", "46,20,50,26", "1,27,4,32", "5,27,8,32", "9,27,13,32", "14,27,17,32", "18,27,21,32", "22,27,25,32", "26,27,29,32", "30,27,33,32", "34,27,37,32", "38,27,41,32", "42,27,45,32", "46,27,50,32", "1,33,4,38", "5,33,8,38", "9,33,13,38", "14,33,17,38", "18,33,21,38", "22,33,25,38", "26,33,29,38", "30,33,33,38", "34,33,37,38", "38,33,41,38", "42,33,45,38", "46,33,50,38", "1,39,4,44", "5,39,8,44", "9,39,13,44", "14,39,17,44", "18,39,21,44", "22,39,25,44", "26,39,29,44", "30,39,33,44", "34,39,37,44", "38,39,41,44", "42,39,45,44", "46,39,50,44", "1,45,4,50", "5,45,8,50", "9,45,13,50", "14,45,17,50", "18,45,21,50", "22,45,25,50", "26,45,29,50", "30,45,33,50", "34,45,37,50", "38,45,41,50", "42,45,45,50", "46,45,50,50");



@code1[1]=int(rand(4))+1;@code1[4]=int(rand(4))+1; @code1[3]=int(rand(4))+1; @code1[2]=int(rand(4))+1;
@code1[0]="fill";
while ( @code1[2] == @code1[1]) {
 @code1[2]=int(rand(4))+1; }
while (@code1[3] == @code1[1] || @code1[3] ==  @code1[2]) {
@code1[3]=int(rand(4))+1; }
while (@code1[4] == @code1[1] || @code1[4] == @code1[2] || @code1[4] == @code1[3]) {
@code1[4]=int(rand(4))+1; }

foreach $i (1...4) {

@xarray=();
@yarray=();

print "<map name\=\"map$i\">";

if (@code1[$i] == 1)  {
@xarray=("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12");
@yarray=("1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8");
}


if (@code1[$i] == 2)  {

@xarray=("12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1");
@yarray=("8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1");

}



if (@code1[$i] == 3)  {


@xarray=("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12");
@yarray=("8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1");
}


if (@code1[$i] == 4)  {
@xarray=("12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1");
@yarray=("1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8");
}


foreach $j (0...95) {



print <<EACHLINE;
<area href="compaint.cgi?img=@code[$i]&x=@xarray[$j]&y=@yarray[$j]&z=$sescnt" shape=rect coords="@coordmap[$j]">

EACHLINE

}
print "<\/map>";
}

}




sub view {

print <<TOP;

<html><head><killbanner>





        <SCRIPT language="javascript">

        <!--

	BottomFill=new Image\(\);
	BottomFill.src="fillnav.gif";
	BottomNav=new Image\(\);
	BottomNav.src="viewnav.gif";
        var ii=1;        
        var tempSpeed=null;
        var timerID=null;


TOP
@gridnames = ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",  "aa", "bb", "cc", "dd", "ee", "ff", "gg",  "hh", "ii", "jj", "kk", "ll", "mm", "nn", "oo",  "pp", "qq", "rr", "ss", "tt", "uu", "vv", "ww", "xx", "yy", "zz", "aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn", "ooo", "ppp", "www", "rrr", "sss", "ttt", "uuu", "vvv", "www", "xxx", "yyy", "zzz", "aaaa", "bbbb", "cccc", "dddd", "eeee", "ffff", "gggg", "hhhh", "iiii", "jjjj", "kkkk", "llll", "mmmm", "nnnn", "oooo", "pppp", "qqqq", "rrrr", "ssss", "tttt", "uuuu", "vvvv", "wwww", "xxxx", "yyyy", "zzzz" );
$framenumber=@paint;

while ($framenumber > 150) {
shift @paint;
$framenumber=@paint;
}

@thing=@paint;
$start=shift @thing;
chomp $start;
@firstpage=split (/\|/,$start);

shift @firstpage;
$h=0;
$allred=0;
$allblue=0;
$allgreen=0;
foreach $eiueeeeee (@firstpage) {
$co=$eiueeeeee-1;
($thisred, $thisblue, $thisgreen)=split(/\|/,@color[$co]);
$allred+=$thisred;
$allblue+=$thisblue;
$allgreen+=$thisgreen;
}

$red1=sprintf "%1x", int($allred/96);
$green1=sprintf "%1x", int($allgreen/96);
$blue1=sprintf "%1x", int($allblue/96);
print "document\.bgColor\=\"$red1$blue1$green1\"\;";
print "document\.linkColor\=\"$red1$blue1$green1\"\;";
print "document\.vlinkColor\=\"$red1$blue1$green1\"\;";
print "document\.fgColor\=\"$red1$blue1$green1\"\;";




foreach $count (0...$framenumber-2)  {
$eeee=$count+1;
$thisone=@paint[$count]; chomp $thisone;
$nextone=@paint[$eeee]; chomp $nextone;
@this=split(/\|/,$thisone);
@next=split(/\|/,$nextone);
$thisip=@next[0];
($firstoc, $secoc, $thirdoc, $dump)=split(/\./,$thisip);
$thisip="$firstoc\.$secoc\.$thirdoc";
push @ips, $thisip;
$allred=0;
$allblue=0;
$allgreen=0;
foreach $eiueeeeee (@next) {
$co=$eiueeeeee-1;
($thisred, $thisblue, $thisgreen)=split(/\|/,@color[$co]);
$allred+=$thisred;
$allblue+=$thisblue;
$allgreen+=$thisgreen;
}

$red=sprintf "%1x", int($allred/96);
$green=sprintf "%1x", int($allgreen/96);
$blue=sprintf "%1x", int($allblue/96);
push @allcolors, "$red$blue$green";

$red=sprintf "%1x", int($allred/111);
$green=sprintf "%1x", int($allgreen/111);
$blue=sprintf "%1x", int($allblue/111);
push @allbgcolors, "$red$blue$green";

$red=sprintf "%1x", int($allred/150);
$green=sprintf "%1x", int($allgreen/150);
$blue=sprintf "%1x", int($allblue/150);
push @alltxtcolors, "$red$blue$green";

foreach $e (1...96) {

if (@this[$e] ne @next[$e]) {push @changes, @next[$e];push @location, $e;$rrrrr++;}

}

}
$rrrrr--;
print " \n               gridImg \= new Array\($rrrrr\)\n";
foreach $wwii (0...$rrrrr) {


print "                gridImg[$wwii] \= new Image\(\)\n";
print "                gridImg[$wwii]\.src\=\"@changes[$wwii]\.jpg\"\n";



}
print "\n                color \= new Array\($rrrrr\)\n";
foreach $wwiiiiie (0...$rrrrr) {


print "                color[$wwiiiiie] \= \"@allcolors[$wwiiiiie]\"\n";


}
print "\n                bgcolor \= new Array\($rrrrr\)\n";
foreach $wwiiiiiee (0...$rrrrr) {


print "                bgcolor[$wwiiiiiee]\=\"@allbgcolors[$wwiiiiiee]\"\n";


}print "\n                txtcolor \= new Array\($rrrrr\)\n";
foreach $wwiiiiieee (0...$rrrrr) {


print "                txtcolor[$wwiiiiieee]\=\"@alltxtcolors[$wwiiiiieee]\"\n";


}
print "\n                gridCoor \= new Array\($rrrrr\)\n";
foreach $wwiii (0...$rrrrr) {
$rfuirir=@location[$wwiii];

print "                gridCoor[$wwiii]\=\"@gridnames[$rfuirir]\"\n";


}







print <<SCRIPT;


                function upDate()
                        {
                        ii++;

SCRIPT


foreach $riuie (@gridnames) {

print <<IFTHEN;

if (gridCoor[ii] == "$riuie") {
                        document.$riuie.src=gridImg[ii].src; }
IFTHEN



}
$eiouenue=0;



print <<SCRIPT2;


		       document.navbar.src=BottomFill.src;
                       document.fgColor = txtcolor[ii];
                       document.linkColor = txtcolor[ii];
                       document.vlinkColor = txtcolor[ii];
                       document.bgColor = color[ii];

if (ii==$rrrrr) { document.navbar.src=BottomNav.src;}
else {
                       timerID=setTimeout("upDate()", 10); }
                        }
        //-->
        

</SCRIPT>

SCRIPT2

print <<TOPOFPAGE; 
<title>COMPAINT - Community art</title></head><body      onLoad="upDate()">

<center><table height=100% width=100% border=0><tr><td valign=middle><center>


<table width\=600 border\=0 cellpadding\=0 cellspacing\=0><tr>



TOPOFPAGE

foreach $gridbox (1...96) {
$eioueno=$gridbox%15+1;
$ieueibe=$gridbox-1;
print <<BOX;
<td height=50 width=50><img src=@firstpage[$ieueibe].jpg name="@gridnames[$gridbox]" height=50 width=50></td>
BOX


if ($gridbox%12 == 0) {print "</tr><tr>";}



}

print "<\/tr><\/table><p><center><img src\=fillnav\.gif name\=\"navbar\">";




print "<\/td><\/tr><\/table></body></html>";

}