<?php
mb_internal_encoding('UTF-8');
define('LOCAL_SERVER', (preg_match("/^(127|192)\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $_SERVER['REMOTE_ADDR'])) ? true : false);
$_magic_quotes_gpc = ini_get('magic_quotes_gpc');
if (empty($_magic_quotes_gpc)) {
  $_magic_quotes_gpc = 'off';
} else {
  $_magic_quotes_gpc = strtolower($_magic_quotes_gpc);
}

define('MAGIC_QUOTES_GPC', $_magic_quotes_gpc);

/*
  if (preg_match("!^www\.([a-zöäü0-9\-]+\.[a-z]{2,6})$!i", $_SERVER['HTTP_HOST'], $match) && !LOCAL_SERVER) {
  header("HTTP/1.1 301 Moved Permanently");
  header("Status: 301 Moved Permanently");
  header('location: http://' . $match[1] . $_SERVER['REQUEST_URI']);
  exit;
  }
 */

setlocale(LC_COLLATE, 'de_DE@euro', 'de_DE', 'de', 'ge', 'deu_deu');
setlocale(LC_CTYPE, 'de_DE@euro', 'de_DE', 'de', 'ge', 'deu_deu');
setlocale(LC_TIME, 'de_DE@euro', 'de_DE', 'de', 'ge', 'deu_deu');

session_start();
$session = session_name() . '=' . session_id();
$sid = session_id();

require_once('tn-core/config.php');
require_once('tn-core/connect.php');

require('tn-core/smarty/Smarty.class.php');
$smarty = new Smarty;
$smarty->setTemplateDir('tn-template/');
$smarty->setCompileDir('tn-cache/');
$smarty->setConfigDir('tn-core/smarty/configs/');
$smarty->setCacheDir('tn-core/smarty/cache/');
$smarty->setPluginsDir('tn-core/smarty/plugins/');
require('tn-core/smarty/SmartyPaginate.class.php');
$smarty->caching = 0;
$smarty->escape_html = true;
Smarty::muteExpectedErrors(); //TODO prüfen ob andere Fehler noch angezeigt werden
$domain_daten = $mysqli->query("SELECT `domain`, `art`, `ziel`, `index`, `portal` FROM `" . $TAB_Liste['domains'] . "` WHERE `domain` = '" . $_SERVER['SERVER_NAME'] . "'");
if ($domain_daten->num_rows == 1) {
  $domain = $domain_daten->fetch_object();
  if ($domain->art == 2 && !empty($domain->ziel)) {
	header("HTTP/1.1 302 Found");
	header("Status: 302 Found");
	header('location: ' . $domain->ziel . $_SERVER['REQUEST_URI']);
	exit;
  } else if ($domain->art == 2 && empty($domain->ziel) && $domain->portal == 0) {
	$domain_daten = $mysqli->query("SELECT `domain` FROM `" . $TAB_Liste['domains'] . "` WHERE `index` = '" . $domain->index . "' AND `art` = '1'");
	$domain = $domain_daten->fetch_object();

	header("HTTP/1.1 301 Moved Permanently");
	header("Status: 301 Moved Permanently");
	header('location: http://' . $domain->domain . $_SERVER['REQUEST_URI']);
	exit;
  } elseif ($domain->art == 1) {
	$config_daten = $mysqli->query("SELECT `mobil` FROM `" . $TAB_Liste['config'] . "` WHERE `id` = '" . $domain->index . "'");
	$_config = $config_daten->fetch_object();
	if (!empty($_config->mobil)) {
	  $mdomain_daten = $mysqli->query("SELECT `domain` FROM `" . $TAB_Liste['domains'] . "` WHERE `index` = '" . $_config->mobil . "' AND `art` = '1'");
	  $mdomain = $mdomain_daten->fetch_object();
	  require_once('tn-core/mobile_device_detect.php');

	  if (!LOCAL_SERVER || (LOCAL_SERVER && preg_match("!\.dicke\-kreativ\.de$!i", $_SERVER['HTTP_HOST']))) {
		$mobileredirect = $mdomain->domain;
		$desktopredirect = $domain->domain;
		mobile_device_detect($mobileredirect, $desktopredirect, $desktopredirect);
	  }
	}
	if ($domain->portal == 0) {
	  if (!LOCAL_SERVER || empty($_GET['index'])) {
		$_GET['index'] = $domain->index;
	  }
	  $heimaturl[] = "//" . $domain->domain;
	  $heimaturl[] = "//server2:8080";
	  $heimaturl[] = "//server2";
	}
  }
} else { //Domain nicht zugewiesen
  header('content-type: text/plain; charset=utf-8');
  die('Die Adresse "' . $_SERVER['SERVER_NAME'] . '" konnte nicht zugewiesen werden.');
}

if (isset($_GET['index']) && !is_numeric($_GET['index'])) {
  unset($_GET['index']);
}

include('tn-core/functions.php');

$_GET = check_magic_quotes_gpc($_GET);
$_POST = check_magic_quotes_gpc($_POST);

if (!isset($_GET['lng'])) {
  $_GET['lng'] = getPreferredLanguage();
}

if (!empty($_GET['index'])) {

  $homepage_status_daten = $mysqli->query("SELECT `homepage_status` FROM " . $TAB_Liste['config'] . " WHERE `id` = " . $_GET['index'] . ";");
  $homepage_status = $homepage_status_daten->fetch_object();
  $homepage_status = $homepage_status->homepage_status;

  $BEuser_status_daten = $mysqli->query("SELECT `sid` FROM " . $TAB_Liste['user'] . " WHERE `sid` = '" . $sid . "';");
  $BEuser_status = $BEuser_status_daten->num_rows;

  if (($homepage_status == 0) && ($BEuser_status == 1)) {
	if (file_exists('tn-misc/offline/offline.php')) {
	  require_once('tn-misc/offline/offline.php');
	} else {
	  ?>
	  <!DOCTYPE html>
	  <html class="no-js" lang="de">

	    <head>
	      <meta charset="utf-8">
	      <title>Achtung - Die Seite ist derzeit OFFLINE</title>
	      <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	  	<link rel="stylesheet" type="text/css" href="tn-core/extension/google/googlefonts.css" media="all" />
	  	<link rel="stylesheet" type="text/css" href="tn-core/extension/bootstrap/bootstrap.min.css" media="all" />
	  	<link rel="stylesheet" type="text/css" href="tn-template/frontend/tn-coretemplate.min.css" media="screen" />
	    </head>

	    <body class="text-center">
	  	<div class="offline-container d-flex w-100 h-100 p-3 mx-auto flex-column">
	        <header class="mb-auto">
	          <div class="inner">
				<?php echo getContentBox('logo', 'div', array('class' => 'navbar-brand')) ?>
	          </div>
	        </header>

	        <main role="main" class="inner cover">
			  <?php echo getContentBox('offline-info', 'div', array('class' => 'offline-info')) ?>
	        </main>

	        <footer class="mt-auto">
	          <div class="inner">
				<?php echo getContentBox('offline-footer', 'div', array('class' => 'offline-footer')) ?>
	          </div>
	        </footer>
	      </div>
	    </body>

	  </html>
	  <?php
	}
  } elseif ($homepage_status == 2) {
	if (($BEuser_status == 0) || (($BEuser_status == 1) && LOCAL_SERVER)) {
	  if (file_exists('tn-misc/offline/offline.php')) {
		require_once('tn-misc/offline/offline.php');
	  } else {
		?>
		<!DOCTYPE html>
		<html class="no-js" lang="de">

		  <head>
		    <meta charset="utf-8">
		    <title>Willkommen</title>
		    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
			<link rel="stylesheet" type="text/css" href="tn-core/extension/google/googlefonts.css" media="all" />
			<link rel="stylesheet" type="text/css" href="tn-core/extension/bootstrap/bootstrap.min.css" media="all" />
			<link rel="stylesheet" type="text/css" href="tn-template/frontend/tn-coretemplate.min.css" media="screen" />
		  </head>

		  <body class="text-center">
			<div class="onepage-container d-flex w-100 h-100 p-3 mx-auto flex-column">
		      <header class="mb-auto">
		        <div class="inner">
				  <?php echo getContentBox('logo', 'div', array('class' => 'navbar-brand')) ?>
		        </div>
		      </header>

		      <main role="main" class="inner cover">
				<?php echo getContentBox('onepage-info', 'div', array('class' => 'onepage-info')) ?>
		      </main>

		      <footer class="mt-auto">
		        <div class="inner">
				  <?php echo getContentBox('onepage-footer', 'div', array('class' => 'onepage-footer')) ?>
		        </div>
		      </footer>
		    </div>
		  </body>

		</html>
		<?php
	  }
	}
  } else {
	if ((($homepage_status == 0) && ($BEuser_status == 1)) || (($homepage_status == 2) && ($BEuser_status == 1))) {
	  define("OFFLINEMODUS", true);
	} else {
	  define("OFFLINEMODUS", false);
	}
	if (file_exists('tn-core/header_' . $_GET['index'] . '.php')) {
	  require_once('tn-core/header_' . $_GET['index'] . '.php');
	} else {
	  require_once('tn-core/header.php');
	}

	if (file_exists('tn-core/content_' . $_GET['index'] . '.php')) {
	  require_once('tn-core/content_' . $_GET['index'] . '.php');
	} else {
	  require_once('tn-core/content.php');
	}

	if (file_exists('tn-core/footer_' . $_GET['index'] . '.php')) {
	  require_once('tn-core/footer_' . $_GET['index'] . '.php');
	} else {
	  require_once('tn-core/footer.php');
	}
  }
} else {
  $_GET['index'] = '0';
  ?>
  test
  <?php
}
?>
