#!/usr/bin/perl
# 
# Calendar Script
#
# Matt Kruse
# http://www.mattkruse.com/
#

$|=1;
print "Content-type: text/html\n";

#####################################################################
# Main line code  (should be at the top to find it easier)
#####################################################################
&ReadParse;
$vars{"cgi"} = $ENV{'SCRIPT_NAME'};
&INITIALIZE;

# Generate general variables
# --------------------------
$vars{'month'} = $month;
$vars{'current_month'} = $month;
$vars{'year'} = $in{'Year'} || $year;
$vars{'current_year'} = $year;
if (!$vars{date} && $vars{month}==$month && $vars{year}==$year) {
	$vars{date} = $mday;
	}
$vars{'type'} = $DefaultType; 
@months=(Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
@weekdays=(Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday);
$vars{'monthname'} = @months[int($vars{'month'})-1];
$vars{'datestamp'} = sprintf("%4.4d%2.2d%2.2d",$vars{year},$vars{month},$vars{date});
                                $vars{loadcookie} = 0;

#####################################################################
#
# Decide what to do based on the ACTION parameter
#
#####################################################################
$vars{timesecs}=time();
$vars{timesecs} += 2592000;
($zsec,$zmin,$zhr,$zday,$zmt,$zyr,$zwkday,$zyrda,$zdalitesavings)=gmtime($vars{timesecs});
$vars{sec}=sprintf("%02d",$zsec);
$vars{min}=sprintf("%02d",$zmin);
$vars{hr} =sprintf("%02d",$zhr);
$vars{yr} =$zyr += 1900;
$vars{day}=sprintf("%02d",$zday);
#$vars{mt} =sprintf("%02d",$zmt+1);
$vars{mt}=$months[$zmt];
$vars{weekdays} =$weekdays[$zwkday];
$vars{cookiestring} = "$vars{weekdays}, $vars{day}-$vars{mt}-$vars{yr} $vars{hr}:$vars{min}:$vars{sec} GMT"; 
$vars{sc_cookie}="Set-Cookie:gtaffid=$in{gtaffid};expires=$vars{cookiestring}\n";        # Aaron
print "$vars{sc_cookie}\n\n";
$vars{gtaffid} = $in{gtaffid};
#print "$vars{sc_cookie}";
print '<html><meta http-equiv="refresh" content="0; url=http://www.globaltalker.com"></html>';
exit(0);
#####################################################################
# Parse input
#
#####################################################################
sub ReadParse {
 local (*in) = @_ if @_; 
 local ($i, $loc, $key, $val);
 if ($ENV{'REQUEST_METHOD'} eq "GET") { $in = $ENV{'QUERY_STRING'};} 
 elsif ($ENV{'REQUEST_METHOD'} eq "POST")
      {read(STDIN,$in,$ENV{'CONTENT_LENGTH'});}
 else {&Error("Can't Read And Parse");}
if ($in =~ /^-----/i) { #For Form Upload
print ("\n");
 @in = split(/-----/g,$in);
 foreach $i (0 .. $#in) {
  $in[$i] =~ s/\+/ /g;
    ($junk1, $key1, $junk2,$val) = split(/\n/,$in[$i],4);
        if ($key1 =~ /filename/si) {	
	    ($junk1, $key, $junk2, $val,$junk3) = split(/"/,$in[$i],5);
		$junk3 =~  s/Content-Type: application\/octet\-stream+//g;  #Clear Header for IE
		$junk3 =~  s/Content-Type: application\/x\-unknown\-content\-type\-Excel\.CSV+//g; #Clear Header for Netscape
	        $junk3 =~ s|[\r\n]+||s;
               $vars{upload_content} = $junk3;
	}
	else {
    		($junk1, $key, $junk2) = split(/"/,$key1,3);
	}

  $key =~ s/%(..)/pack("c",hex($1))/ge;
  $val =~ s/%(..)/pack("c",hex($1))/ge;
  $val =~ s/^ +//g; 
  $val =~ s/\n+//g;  
  $val =~ s/^\s+//g;           # remove leading spaces
  $val =~ s/\s+$//g;           # remove trailing spaces
  $val =~ s|[\r\n]+||gs;
  $in{$key} .= "\0" if (defined($in{$key}));
  $in{$key} .= $val;
  } 
}
else {
 @in = split(/&/,$in);
 foreach $i (0 .. $#in)
 { $in[$i] =~ s/\+/ /g;    ($key, $val) = split(/=/,$in[$i],2);
   $key =~ s/%(..)/pack("c",hex($1))/ge;
   $val =~ s/%(..)/pack("c",hex($1))/ge;
   $val =~ s/^\s+//g;           # remove leading spaces
   $val =~ s/\s+$//g;           # remove trailing spaces
   $val =~ s/  +/ /g;
   $in{$key} .= "\0" if (defined($in{$key})); 
   $in{$key} .= $val;  
 }  
}
 return 1; 
 }
#####################################################################
# Initialize variables
#####################################################################
sub INITIALIZE {
	my ($x,$i,$zneg_pos,$zhours);
        my ($ztime);

	# Current date and stuff

	$ztime = time;
	($zneg_pos,$zhours) = ($vars{time_offset} =~ /([+-])(\d+)/);
	if ($zneg_pos eq '+') {
		$ztime += (60*60 * $zhours);
		}
	else {
		$ztime -= (60*60 * $zhours);
		}
	($mday,$month,$year) = (localtime($ztime))[3,4,5];
	$month = $month+1;
	$year = $year+1900;
	$datestamp = sprintf("%4.4d%2.2d%2.2d",$year,$month,$mday);
	
	}
