<?php
$sc	= ereg_replace("\.xx", "", $SCRIPT_FILENAME);
$al	= getenv("HTTP_ACCEPT_LANGUAGE");
if (!$lang) {
  if	  (stristr($al, "nl"))	$lang = "nl";
  else if (stristr($al, "de"))	$lang = "de";
}
if (!$lang) {
    $ra = getenv("REMOTE_ADDR");
    exec("nslookup $ra", $output);
    while(list($k, $line) = each($output)) {
      if (eregi("^Name: ", $line)) $ad = trim(eregi_replace("^Name: ", "", $line));
    }
    if (eregi("nl$", $ad))	{$lang = "nl"; $tld = "nl";}
    if (eregi("be$", $ad))	{$lang = "nl"; $tld = "be";}
    if (eregi("de$", $ad))	{$lang = "de"; $tld = "de";}
    if (eregi("at$", $ad))	{$lang = "de"; $tld = "at";}
    if (eregi("ch$", $ad))	{$lang = "de"; $tld = "ch";}
    if (!$lang) {
	$whois = "http://ws.arin.net/cgi-bin/whois.pl?queryinput=";
	if (eregi(("^193|^194|^195|^212|^213|^217"), $ra)) $whois = "http://www.ripe.net/perl/whois?searchtext=";
	if (eregi(("^61|^202|^203|^210|^211|^218|^219|^220|^221"), $ra)) $whois = "http://www.apnic.net/apnic-bin/whois2.pl?searchtext=";
	if (eregi(("^200"), $ra)) $whois = "http://lacnic.net/cgi-bin/lacnic/whois?lg=EN&qr=";
	$ad = "";
	if (strstr($whois, "arin"))	exec("whois -h whois.arin.net $ra", $output);
	if (strstr($whois, "ripe"))	exec("whois -h whois.ripe.net $ra", $output);
	if (strstr($whois, "apnic"))	exec("whois -h whois.apnic.net $ra", $output);
	if (strstr($whois, "lacnic"))	exec("whois -h whois.lacnic.net $ra", $output);
	while(list($k, $line) = each($output)) {
	  if (eregi("^country:", $line)) $tld = strtolower(trim(eregi_replace("^country:", "", $line)));
	}
	if (strstr($whois, "arin")) {
	  if (eregi("ripe", $ad))	$whois2 = "http://www.ripe.net/perl/whois?searchtext=";
	  if (eregi("apnic", $ra))	$whois2 = "http://www.apnic.net/apnic-bin/whois2.pl?searchtext=";
	  if (eregi("lacnic", $ra))	$whois2 = "http://lacnic.net/cgi-bin/lacnic/whois?lg=EN&qr=";
	}
	if ($whois2) {
	  $whois = $whois2;
	  if (eregi("ripe", $ad))	exec("whois -h whois.ripe.net $ra", $output);
	  if (eregi("apnic", $ad))	exec("whois -h whois.apnic.net $ra", $output);
	  if (eregi("lacnic", $ad))	exec("whois -h whois.lacnic.net $ra", $output);
	  while(list($k, $line) = each($output)) {
	    if (eregi("^country:", $line)) $tld = strtolower(trim(eregi_replace("^country:", "", $line)));
	  }
	}
	if ($tld == "nl")		$lang = "nl";
	if ($tld == "be")		$lang = "nl";
	if ($tld == "de")		$lang = "de";
	if ($tld == "at")		$lang = "de";
	if ($tld == "ch")		$lang = "de";
    }
}
if	($lang && $lang=="nl")	$file = "$sc.nl";
else if ($lang && $lang=="de")	$file = "$sc.de";
else				$file = "$sc.en";
if (file_exists($file)) {
  header("Last-Modified: ".gmdate("D, d M Y H:i:s", filemtime($file))." GMT");
  if (!eregi("(\.js|\.php|\.html|index\.)", $file)) header("Accept-Ranges: bytes\nContent-Length: ".filesize($file));
  include $file;
  if (strstr($file, ".htm"))	echo "<!-- \$lang = $lang / \$ad = $ad / \$tld = $tld -->";
  if (strstr($file, ".js"))	echo "// \$lang = $lang / \$ad = $ad / \$tld = $tld;";
}
?>
