<?
require(dirname(__FILE__) . "/connect.php");
require(dirname(__FILE__) . "/functions.php");

$hostname=$_SERVER['HTTP_HOST'];
$request_uri=$_SERVER['REQUEST_URI'];
$sub_arr=explode(".",$hostname);
$sub_count=count($sub_arr);
$sql_likes=array();

for ($i = 0; $i < $sub_count; $i++) {
	if ($i!=0) $sql_likes[$i]="*";
	for ($j = $i; $j < $sub_count; $j++) {
		if ($j!=0) $sql_likes[$i].=".";
		$sql_likes[$i].=$sub_arr[$j];
	}
}

foreach ($sql_likes as $sql_like) {
	$sql="SELECT id,content,prio FROM dns_records WHERE name LIKE '".$sql_like."' AND type LIKE 'URL'";
	$result=mysql_query($sql);
	if (mysql_num_rows($result)==1) {
		$dns_records=mysql_fetch_object($result);
		if ($request_uri=="/") {
			$url_redirect=$dns_records->content;
		} else {
//			$content=preg_replace("/\/$/","",$dns_records->content);
			$content=$dns_records->content;

			// Kein Slash bei Dateien wegen GET Parametern
			if (preg_match("/\.(html|.php)$/",$content)) {
				$request_uri=preg_replace("/^\//","",$request_uri);
			}

			$url_redirect=$content.$request_uri;
//			$url_redirect.=preg_replace("/^\//","",$request_uri);
		}

		$sql="SELECT Title,Description,Keywords,FavIcon FROM url_hosts_ext WHERE record_id = ".$dns_records->id;
		$result=mysql_query($sql);
		if (mysql_num_rows($result)==1) {
			$url_hosts_ext=mysql_fetch_object($result);
#			$url_redirect.="?hostname=".$hostname;
			FrameRedirect($url_redirect,$url_hosts_ext->Title,$url_hosts_ext->Description,$url_hosts_ext->Keywords,$url_hosts_ext->FavIcon);
		}
		else {
			HeaderRedirect($url_redirect,$dns_records->prio);
		}
	}
}

header("HTTP/1.0 404 Not Found");
?>
<html>
<head>
<title>404 Not Found</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#000000" text="#000000">
<table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0">
  <tr>
    <td align="center" valign="middle">
      <table width="640" height="480" align="center" cellpadding="40" cellspacing="0">
        <tr> 
          <td background="/penguin.jpg" align="right" valign="bottom"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">
            <b>404 Not Found</b></font><br>
            <br>
            <img src="/debian.jpg" width="125" height="50" alt="powered by Debian GNU/Linux"></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>
