<?
//die();
header("Content-Type:text/xml");

if( get_magic_quotes_gpc() ) {
	function stripslashes_deep($value) {
		$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
		return $value;
	}
	
	$_POST		= array_map('stripslashes_deep', $_POST);
	$_GET		= array_map('stripslashes_deep', $_GET);
	$_COOKIE	= array_map('stripslashes_deep', $_COOKIE);
	$_REQUEST	= array_map('stripslashes_deep', $_REQUEST);
}

require_once 'includes/config.php';
require_once 'lib/autoload.php';

$mysql = new mysql(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME);
if( mysqli_connect_errno() )
	die("No database connection possible.");

// Frontend-Benutzer
$feuser = new feuser($mysql);

$netArtist = new netArtist($mysql,false);
$langs = languages::loadAll();
$sitemap = new navigator($mysql);
blogitem::setMysql($mysql);



echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xhtml="http://www.w3.org/1999/xhtml">
<?

// Seiten
$rootpages = array(
	4,	// Hauptnavigation
	12	// Meta
);

foreach( $rootpages as $rootpage ) {
	$sitemap->setRootPage($rootpage);
	$sitemap->setWithRootPage(false);
	$sitemap->setNavigationDepth(Navigator::ALL_SUBPAGES);
	//$sitemap->setShowAllPages(true);
	$sitemap->setStyle("arrayToSort");
	$sitemap->generate();
	foreach( $sitemap->getArray() as $site ) {
		if( $site['visible'] && !$site['forward'] ) {
			echo "      <url>\n";
			foreach( $langs as $lang ) {
				$query = "SELECT p.id, DATE(p.lastchange) AS lastchange, l.siteName
					FROM ".NA_TABLE_PAGES." as p
					LEFT JOIN ".NA_TABLE_PAGES_LANG." as l ON (p.id = l.page_id AND l.lang_id = ".$lang->getId().")
					WHERE p.id='".$site['id']."'";
				if( $result = $mysql->query($query) )
					if( $row = $result->fetch_assoc() )
						if( !empty($row['siteName']) )
							$site['siteName'] = $row['siteName'];
				// Wenn die Hauptsprache dann <loc>
				if( $lang->getId() == 1 ) {
					if( SEO_LINKS )
						echo "            <loc>".(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? "https" : "http")."://".($lang->getDomain() ? "www.".$lang->getDomain() : $_SERVER['HTTP_HOST'])."/".(isset($site['siteName']) && $lang->getId() > 1 && !$lang->getDomain() ? $lang->getShortName()."/" : "").(isset($site['siteName']) ? $site['siteName'].".".SEO_LINKS_EOF : "index.php?id=".$site['id'].($lang->getId() > 1 ? "&amp;language=".$lang->getId() : ""))."</loc>\n";
					else
						echo "            <loc>".(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? "https" : "http")."://".($lang->getDomain() ? "www.".$lang->getDomain() : $_SERVER['HTTP_HOST'])."/index.php?id=".$site['id'].($lang->getId() > 1 && !$lang->getDomain() ? "&amp;language=".$lang->getId() : "")."</loc>\n";
				// Wenn nicht die Hauptsprache dann <xhtml:link />
				} else {
					if( SEO_LINKS )
						echo "            <xhtml:link rel=\"alternate\" hreflang=\"".$lang->getShortName()."\" href=\"".(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? "https" : "http")."://".($lang->getDomain() ? "www.".$lang->getDomain() : $_SERVER['HTTP_HOST'])."/".(isset($site['siteName']) && $lang->getId() > 1 && !$lang->getDomain() ? $lang->getShortName()."/" : "").(isset($site['siteName']) ? $site['siteName'].".".SEO_LINKS_EOF : "index.php?id=".$site['id'].($lang->getId() > 1 ? "&amp;language=".$lang->getId() : ""))."\" />\n";
					else
						echo "            <xhtml:link rel=\"alternate\" hreflang=\"".$lang->getShortName()."\" href=\"".(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? "https" : "http")."://".($lang->getDomain() ? "www.".$lang->getDomain() : $_SERVER['HTTP_HOST'])."/index.php?id=".$site['id'].($lang->getId() > 1 && !$lang->getDomain() ? "&amp;language=".$lang->getId() : "")."\" />\n";
				}
				echo '            <lastmod>'.$row['lastchange'].'</lastmod>';
			}
			echo "      </url>\n";
		}
	}
}

// Blogs | BlogID => "Seitenname des Blog Frontends"
$blogs = array(
	5 => "hersteller",
	1 => "aktuelles-termine-beschlagtechnik" 
);

foreach( $blogs as $blog_ID => $blog_name ) {
	$blog_items = blogitem::loadForSitemap($blog_ID,$lang->getId());
	foreach( $blog_items as $blog_item ) {
		echo "<url>\n";
		if( SEO_LINKS ) {
			echo "<loc>".(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? "https" : "http")."://".$_SERVER['HTTP_HOST']."/".(isset($blog_item['siteName']) && $lang->getId() > 1 ? $lang->getShortName()."/".$blog_name."/" : $blog_name."/").(isset($blog_item['siteName']) ? (!empty($blog_item['siteName']) ? $blog_item['siteName'] : $blog_item['id']).".".SEO_LINKS_EOF : "index.php?id=8&amp;blogItemId=".$blog_item['id'].($lang->getId() > 1 ? "&amp;language=".$lang->getId() : ""))."</loc>\n";
		} else {
			echo "<loc>".(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? "https" : "http")."://".$_SERVER['HTTP_HOST']."/index.php?id=8&blogItemId=".$blog_item['id'].($lang->getId() > 1 ? "&language=".$lang->getId() : "")."</loc>\n";
		}
		echo "</url>\n";
	}
}


$mysql->close();
?>
</urlset>
