#!/usr/bin/perl

# language_cookie.cgi ... RJM Programming - April, 2014 ... redirect to Joomla website

$doreset = "";
$ourlang = "";
$oururl = "";
$yourorigin = $ENV{'HTTP_REFERER'};
$ourdomain = $ENV{SERVER_NAME};

if ($yourorigin =~ m#^http://localhost# || $yourorigin =~ m#^http://www.rjmprogramming.com.au/#) {   # Please be accessing the proper way ... 

 if ($ENV{'REQUEST_METHOD'} eq "GET") {    # GET reset mode ... 
  $ourqs = $ENV{'QUERY_STRING'};
  if ($ENV{'QUERY_STRING'} =~ m/_clear/) {
    $ourqs =~ s/_clear//g;
    $doreset = "y";
  }
  @yourpairs = split(/&/, $ourqs);
  foreach $yourpair (@yourpairs) {
     ($yourname, $yourvalue) = split(/=/, $yourpair);
     $yourvalue =~ tr/+/ /;
     $yourvalue =~ s/%([\dA-Fa-f]{2})/pack("C", hex($1))/eg;
     $getdata{$yourname} = $yourvalue;
     if ($yourname eq "language") {
      $ourlang = $getdata{$yourname};
      if ($doreset eq "y") {
        my $ozzyfn = &one_year_from_now;
        print "Set-Cookie:language=$ourlang; expires=$ozzyfn; domain=$ourdomain; path=/\n";  # set cookie for future visit usage
        $oururl = 'http://translate.google.com/translate?sl=en&js=n&prev=_t&hl=en&ie=UTF-8&layout=2&eotf=1&u=http%3A%2F%2Fwww.rjmprogramming.com.au%2Fmyfavourites&tl=' . $ourlang; 
      }
      if ($ourlang eq "") {
       if ($ENV{'HTTP_COOKIE'}) {
        @ourcookies = split(/;/, $ENV{'HTTP_COOKIE'});
        foreach $thiscookie (@ourcookies) {
         ($ourname, $ourvalue) = split(/=/, $thiscookie);
         $ourcrumbs{$ourname} = $ourvalue; 
         if ($ourname eq "language") {
           $ourlang = $ourcrumbs{$ourname};
           $oururl = 'http://translate.google.com/translate?sl=en&js=n&prev=_t&hl=en&ie=UTF-8&layout=2&eotf=1&u=http%3A%2F%2Fwww.rjmprogramming.com.au%2Fmyfavourites&tl=' . $ourlang; 
         }
        }
       }
      } else {  # set up redirect to Google Translate of Joomla website
       if ($ENV{'HTTP_COOKIE'} && $doreset eq "") {
        @ourcookies = split(/;/, $ENV{'HTTP_COOKIE'});
        foreach $thiscookie (@ourcookies) {
         ($ourname, $ourvalue) = split(/=/, $thiscookie);
         $ourcrumbs{$ourname} = $ourvalue; 
         if ($ourname eq "language") {
           $oururl = 'http://translate.google.com/translate?sl=en&js=n&prev=_t&hl=en&ie=UTF-8&layout=2&eotf=1&u=http%3A%2F%2Fwww.rjmprogramming.com.au%2Fmyfavourites&tl=' . $ourcrumbs{$ourname}; 
         }
        }
       }
       if ($oururl eq "") {   # Javascript must have cleared the cookie ready for this new cookie
        my $ozyfn = &one_year_from_now;
        print "Set-Cookie:language=$ourlang; expires=$ozyfn; domain=$ourdomain; path=/\n";  # set cookie for future visit usage
        $oururl = 'http://translate.google.com/translate?sl=en&js=n&prev=_t&hl=en&ie=UTF-8&layout=2&eotf=1&u=http%3A%2F%2Fwww.rjmprogramming.com.au%2Fmyfavourites&tl=' . $ourlang; 
       } else {
        $oururl = 'http://translate.google.com/translate?sl=en&js=n&prev=_t&hl=en&ie=UTF-8&layout=2&eotf=1&u=http%3A%2F%2Fwww.rjmprogramming.com.au%2Fmyfavourites&tl=' . $ourlang; 
       }
      }
     }
  }
 } else {  # POST from a form mode of use ...

  if ($ENV{'HTTP_COOKIE'}) {   # cookie from previous visit perhaps ...
   @ourcookies = split(/;/, $ENV{'HTTP_COOKIE'});
   foreach $thiscookie (@ourcookies) {
    ($ourname, $ourvalue) = split(/=/, $thiscookie);
    $ourcrumbs{$ourname} = $ourvalue; 
    if ($ourname eq "language") {
      $ourlang = $ourcrumbs{$ourname};
      $oururl = 'http://translate.google.com/translate?sl=en&js=n&prev=_t&hl=en&ie=UTF-8&layout=2&eotf=1&u=http%3A%2F%2Fwww.rjmprogramming.com.au%2Fmyfavourites&tl=' . $ourlang; 
    }
   }
  }

  if ($ourlang eq "") {  # no relevant cookies but form data can be used ...
   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
   @ourpairs = split(/&/, $buffer);

   foreach $ourpair (@ourpairs) {
     ($ourname, $ourvalue) = split(/=/, $ourpair);
     $ourvalue =~ tr/+/ /;
     $ourvalue =~ s/%([\dA-Fa-f]{2})/pack("C", hex($1))/eg;
     $formdata{$ourname} = $ourvalue;
   }
   
   ($oursec, $ourmin, $ourhour, $ourday, $ourmon, $ouryr, $ourwday, $ouryday, $ourisdat) = localtime(time);
   $ourmin = sprintf("%02d", $ourmin);
   $oursec = sprintf("%02d", $oursec);

   $ourlang = $formdata{'language'};
   my $oyfn = &one_year_from_now;
   print "Set-Cookie:language=$ourlang; expires=$oyfn; domain=$ourdomain; path=/\n";  # set cookie for future visit usage
  }
 }
 print "Content-type:text/html\n\n";
 if ($oururl eq "") {
   if ("1" eq "1") {  # set up redirect for Joomla website in language of choice
    if ($ourlang eq "") {
     $oururl = 'http://www.rjmprogramming.com.au/myfavourites'; 
    } elsif ($ourlang eq "en") {
     $oururl = 'http://www.rjmprogramming.com.au/myfavourites'; 
    } else {
     $oururl = 'http://translate.google.com/translate?sl=en&js=n&prev=_t&hl=en&ie=UTF-8&layout=2&eotf=1&u=http%3A%2F%2Fwww.rjmprogramming.com.au%2Fmyfavourites&tl=' . $ourlang; 
    }
   } else {  # old tutorial welcome method
    $oururl = "http://translate.google.com/#auto/" . $ourlang . "/We%20hope%20you%20enjoy%20our%20tutorials%20and%20please%20keep%20the%20cards%20and%20letters%20flowing%20in.%20%20The%20time%20for%20our%20server%20is%20" . $ourhour . ":" . $ourmin . ":" . $oursec . ".";
   }
 } elsif ($ourlang eq "en") {
   $oururl = 'http://www.rjmprogramming.com.au/myfavourites'; 
 }
 print "<META HTTP-EQUIV=refresh CONTENT=\"1;URL=" . $oururl . "\">\n";  # redirect
 
} else {    # Bad access options below ...
 print "Content-type:text/html\n\n";
 
 if ($ENV{'REQUEST_METHOD'} eq "GET") {    # Bad access but okay method, maybe, so, see ...
  @ourpairs = split(/&/, $ENV{'QUERY_STRING'});
  foreach $ourpair (@ourpairs) {
     ($ourname, $ourvalue) = split(/=/, $ourpair);
     $ourvalue =~ tr/+/ /;
     $ourvalue =~ s/%([\dA-Fa-f]{2})/pack("C", hex($1))/eg;
     $getdata{$ourname} = $ourvalue;
     if ($ourname eq "language") {
      $ourlang = $getdata{$ourname};
      if ($ourlang eq "") {
       if ($ENV{'HTTP_COOKIE'}) {
        @ourcookies = split(/;/, $ENV{'HTTP_COOKIE'});
        foreach $thiscookie (@ourcookies) {
         ($ourname, $ourvalue) = split(/=/, $thiscookie);
         $ourcrumbs{$ourname} = $ourvalue; 
         if ($ourname eq "language") {
           $ourlang = $ourcrumbs{$ourname};
           $oururl = 'http://translate.google.com/translate?sl=en&js=n&prev=_t&hl=en&ie=UTF-8&layout=2&eotf=1&u=http%3A%2F%2Fwww.rjmprogramming.com.au%2Fmyfavourites&tl=' . $ourlang; 
         }
        }
       }
      } elsif ($ourlang eq "en") {  # set up redirect to Google Translate of Joomla website
        $oururl = 'http://www.rjmprogramming.com.au/myfavourites'; 
      } else {  # set up redirect to Google Translate of Joomla website
        $oururl = 'http://translate.google.com/translate?sl=en&js=n&prev=_t&hl=en&ie=UTF-8&layout=2&eotf=1&u=http%3A%2F%2Fwww.rjmprogramming.com.au%2Fmyfavourites&tl=' . $ourlang; 
      }
      if ($ourlang eq "") {
        $oururl = 'http://www.rjmprogramming.com.au/myfavourites'; # no cookies and GET reset mode ... use English
      }
      print "<META HTTP-EQUIV=refresh CONTENT=\"1;URL=" . $oururl . "\">\n"; # redirect
     }
  }
 } else {  # bad access ... and no cookies yet
  if ($ourlang eq "") {
   $oururl = 'http://www.rjmprogramming.com.au/myfavourites'; # no cookies and GET reset mode ... use English
  } elsif ($ourlang eq "en") {
   $oururl = 'http://www.rjmprogramming.com.au/myfavourites'; # no cookies and GET reset mode ... use English
  } else {
   $oururl = 'http://translate.google.com/translate?sl=en&js=n&prev=_t&hl=en&ie=UTF-8&layout=2&eotf=1&u=http%3A%2F%2Fwww.rjmprogramming.com.au%2Fmyfavourites&tl=ca' . $ourlang; 
  }
  print "<META HTTP-EQUIV=refresh CONTENT=\"1;URL=" . $oururl . "\">\n";
  if ("1" eq "2") {   # below was old welcome from previous tutorial
   print "<head><META HTTP-EQUIV=refresh CONTENT=\"8;URL=http://www.rjmprogramming.com.au/HTMLCSS/picklanguage_cookie.html\">\n</head><body><p>Are you accessing from the right place?</p></body>";
  }
 }
}


sub one_year_from_now {  # thanks to http://www.iro.umontreal.ca/~lapalme/ift3225/Dynamique/showXML.cgi?preferences/preferences.cgi
	
my @days = ("Sun","Mon","Tues","Wed","Thu","Fri","Sat");
my @months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

#gmtime returns like localtime but in GMT
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=gmtime;

$year+=1900; #current year

#augment week day by 1 to reflect the next year 
if($wday<6){

	$wday++;
}
else {
	$wday=0;
}

#if it's a leap year, augment week day by an extra 1 for any month after Feb
#day of month, mday, stays the same unless its leap year in which case Feb 29 becomes 28


if(($year % 4) == 0) { #leap year
	if($mon>1) {
		if($wday == 6){
			$wday++;
		}
		else {
			$wday=0;
		}
	}
	if(($mon == 1)&&($mday == 29)) {
		$mday=28;
	}
}


$year++;  #add 1 to the year

return "$days[$wday], $mday-$months[$mon]-$year $hour:$min:$sec GMT";
}
