<?php

/*
 *
 * NetBeat Redirect Script
 * Autor: Johannes Schritz <js@netbeat.de>
 * (c) Copyright 2006
 *
 */
define('APP_DIR','exec/internal/cronjobs/');
define('APP_URL','');

#phpinfo(); die();
require("../includes/init.inc.php");

$host = preg_replace("/\.$/","",str_replace(":80","",strtolower($_SERVER['HTTP_HOST'])));
$searchterm="";

foreach ($TLDS as $tld) {
//	$file = fopen("/tmp/test.tmp","a");
//	fputs($file,substr($host,-1*(strlen($tld)+1))."==".".".$tld."?\n");
	if (substr($host,-1*(strlen($tld)+1))==".".$tld) {
		$index = substr($host,0,-1*(strlen($tld)+1));
		$parts = explode(".",$index);
		$sld = array_pop($parts);
		if (substr($sld,0,4)=="xn--") {

// fputs($file,"sld=".$sld."--------".idn_to_utf8($sld,0,INTL_IDNA_VARIANT_UTS46)."\n");

//			$sld = idn_punycode_decode(substr($sld,4),"UTF-8");
			$sld = idn_to_utf8($sld,0,INTL_IDNA_VARIANT_UTS46);


		}
		if ((count($parts) == 1 && $parts[0]=="www") || (count($parts)==0))
			$searchterm = $sld . "." . $tld;
		else {
			
			$is_twin  = 0;
			foreach ($TLDS as $twin)
			{
			    if($twin == $sld.".".$tld)
			    {
				$is_twin  = 1;
				$tld = $sld.".".$tld;
				$sld = array_pop($parts);
				break;
			    }
			}
			
			if($is_twin)
			{
			    if ((count($parts) == 1 && $parts[0]=="www") || (count($parts)==0))
			    {
    				$searchterm = $sld . "." . $tld;
				break;
			    }
			}
			
			$searchterm = implode(".",$parts) . "|" . $sld . "." . $tld;
		}
		
//		fputs($file,"searchterm=".$searchterm."\n");
		
		break;
	}
//	fclose($file);
}

#if ($searchterm == "") { throw new Exception("Unbekannte Top-Level-Domain in '".$host."'"); };
if ($searchterm == "") { header("Location: https://www.netbeat.de/fourohfour.html");exit();};


$sth = $db->query("SELECT * FROM redirects WHERE domain='".$db->real_escape_string($searchterm)."'");

if ($res = $sth->fetch_assoc()) {
	$keywords = htmlentities($res['keywords']);
	$description = htmlentities($res['description']);
	$title = htmlentities($res['title']);
	$ziel = $res['ziel'];
	if (substr($ziel,0,7) != "http://" && substr($ziel,0,8) != "https://") {
		$ziel = "http://".$ziel;
	}
//file_put_contents("rd.txt",$ziel."\n",FILE_APPEND);	
	switch ($res['typ']) {
	case "R":
//		header("Location: ".$ziel);
		header("Location: ".$ziel, true, 301);
		break;
	case "F":
		echo <<<EOT
<?xml version="1.0" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "XHTML1-f.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>$title</title>
<meta name="keywords" content="$keywords" />
<meta name="description" content="$description" />
<meta name="viewport" content="width=device-width" /> 
</head>
<frameset rows="100%,*" border="0">
  <frame marginwidth="0" marginheight="0" frameborder="0" noresize="noresize" scrolling="auto" src="$ziel" />
  <frame frameborder="0" scrolling="no" noresize="noresize" marginwidth="0" marginheight="0" />
  <noframes>
	<body>
	Bitte klicken Sie hier: <a href="$ziel">$ziel</a>.
	</body>
  </noframes>
</frameset>
</html>
EOT;
	break;
	case "D":
	default:
		header("Location: https://www.netbeat.de/fourohfour.html");
	}
} else {
	header("Location: https://www.netbeat.de/fourohfour.html");
}
?>
