#!/usr/bin/perl
#
# install counter.

$header = "<HTML><HEAD><TITLE>Planetmind Counter</TITLE>
<SCRIPT LANGUAGE=JavaScript>
<!--
             function OpenWindow( url, width, height, options, name )
            {
             if ( ! width ) width = 420;
             if ( ! height ) height = 420;
             if ( ! options ) options = 
\"scrollbars=yes,menubar=yes,toolbar=yes,location=yes,status=yes,resizable=yes\";
             if ( ! name ) name = \"outsideSiteWindow\";
             var newWin = window.open( url, name, \"width=\" + width + \",height=\" 
+ height + \",\" + options );
             }
             // -->
</SCRIPT>
</HEAD>
<BODY background=http://planetmind.net/images/hunabku_bkg3.gif>
<IMG SRC=http://planetmind.net/images/planetwomanlogo.gif WIDTH=212 HEIGHT=54><p>";

$root_dir = $ENV{'SERVER_NAME'};
$root_dir =~ s/\.com|\.net|\.org//i;
$root_dir =~ s/www.//i;
$docroot = $root_dir;
$root_dir = "/www/htdocs/".$root_dir;

$uid = (stat($root_dir))[4];
$owner = (getpwuid($uid))[0];
$script_url = "http://".$ENV{'SERVER_NAME'}."/cgi-bin/cgiwrap/$owner/counter_install.cgi";

$authfile = "/usr/local/etc/Counter/conf/count.cfg";
$datadir = "/usr/local/etc/Counter/data";

# Get the form input
  read(STDIN, $input, $ENV{'CONTENT_LENGTH'});

	@pairs = split(/&/, $input);

    foreach $pair (@pairs) {
	($name, $value) = split(/=/, $pair);
	
	$name =~ tr/+/ /;
	$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

   $FORM{$name} = $value;
		}


if ($FORM{'submit'}) {

$file = $FORM{'file'};
$absfile = $root_dir.$FORM{'file'};
$style = $FORM{'style'};
$color = $FORM{'color'};
$color =~ s/#//;
$thickness = $FORM{'thickness'};

$fname = $file =~ m/\//g; 
$slash = (pos($fname))+1;
$filename = substr($file,$slash);

print "Content-type: text/html\n\n";
print $header;

print qq!
<H2>Install Hit Counter on Web Page</H2>
Here's what your counter will look like: <P>
<img src="/cgi-bin/Count.cgi?ft=$thickness|dd=$style|frgb=$color;&df=$docroot-$filename.dat">
<P>
Counter will be placed at the bottom of $file. <P>

<FORM ACTION="$script_url" METHOD="Post">
<INPUT NAME="thickness" TYPE="hidden" VALUE="$thickness">
<INPUT NAME="color" TYPE="hidden" VALUE="$color">
<INPUT NAME="file" TYPE="hidden" VALUE="$file">
<INPUT NAME="style" TYPE="hidden" VALUE="$style">

<INPUT NAME="confirm" TYPE="submit" VALUE="Confirm"><P>
<INPUT NAME="confirm" TYPE="button" VALUE="Go back and make changes" onClick='window.history.back();'>
</FORM>
</body</html>!;
exit;
}

if ($FORM{'confirm'}) {

$file = $FORM{'file'};
$absfile = $root_dir.$FORM{'file'};
$style = $FORM{'style'};
$color = $FORM{'color'};
$color =~ s/#//;
$thickness = $FORM{'thickness'};
$url = "http://".$ENV{'SERVER_NAME'}.$file;


$fname = $file =~ m/\//g; 
$slash = (pos($fname))+1;
$filename = substr($file,$slash);
$servername = $ENV{'SERVER_NAME'};
$servername =~ s/www.//;


print "Content-type: text/html\n\n";

open(AUTH, "$authfile") || print "I can't open $authfile\n";
@lines = <AUTH>;
close(AUTH);


$numlines = @lines;
$numlines = $numlines-1;

if ("@lines" !~ /$servername}/) {
 $lines[$numlines] = $servername."\n";
 $numlines++;
 $lines[$numlines] = "www.".$servername."\n";
 $numlines++;
 $lines[$numlines] = "}";

 open(AUTH, ">$authfile") || print "I can't open $authfile\n";
  foreach $line(@lines) {
   print AUTH $line;
  }
 close(AUTH);

 open(HTML, ">>$absfile") || print "I can't open $absfile\n";
  print HTML "<P><center><img src=\"/cgi-bin/Count.cgi?ft=$thickness|dd=$style|frgb=$color;&df=$docroot-$filename.dat\"></center>";
 close(HTML);

}

print $header;

print qq!
<H2>Install Hit Counter on Web Page</H2>
Counter has been successfully installed on <a href="$url">$url</a>\! <P>
<P>
<HR><P>[<A HREF="http://$ENV{'SERVER_NAME'}/admin/">back to Admin Page</A>]
</body</html>!;
exit;




}






print "Content-type: text/html\n\n";
print $header;

print qq!
<H2>Install Hit Counter on Web Page</H2>
<FORM ACTION="$script_url" METHOD="Post">
<B>Choose the file you'd like the counter to appear on:</B> <INPUT NAME="file" TYPE="text"> 
<input type=button onClick='ifield = document.forms[0].file; chooser = window.open("/cgi-bin/chooser.cgi?type=0&chroot=$root_dir&file="+ifield.value, "chooser", "toolbar=no,menubar=no,scrollbar=no,width=400,height=300"); chooser.ifield = ifield' value="Browse...">
<P>
<B>Choose a style:</B><P>
<INPUT NAME="style" TYPE="radio" VALUE="A"><IMG SRC="/icons/style_a.gif" WIDTH=100 HEIGHT=30 BORDER=0><P>
<INPUT NAME="style" TYPE="radio" VALUE="B"><IMG SRC="/icons/style_b.gif" WIDTH=100 HEIGHT=30 BORDER=0><P>
<INPUT NAME="style" TYPE="radio" VALUE="C"><IMG SRC="/icons/style_c.gif" WIDTH=100 HEIGHT=30 BORDER=0><P>
<INPUT NAME="style" TYPE="radio" VALUE="D"><IMG SRC="/icons/style_d.gif" WIDTH=64 HEIGHT=23 BORDER=0><P>
Choose a color for the border of the counter: <INPUT NAME="color" TYPE="text" SIZE=8,1 >
Click <input type=button value="Click here to choose" ONCLICK="OpenWindow('/icons/color_picker.html', 320, 340, 'scrollbars=yes, menubar=no, toolbar=no, location=no, status=yes, resizable=yes');return false;" >
<P>
Enter a value for the thickness of the border (1=none, 8=thick): <INPUT type=text NAME="thickness" size=2 maxlength=2><P>


<INPUT NAME="submit" TYPE="submit" VALUE="Submit">
</FORM>
</body</html>
!;

