<?php 
header ("Content-Type:text/xml");  
echo '<?xml version="1.0" encoding="UTF-8"?>'; 
?>
<urlset
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<?php

require('./config.php');
require('./config_sb.php');
require('./admin/libraries/mysql.php');

function findPages($parent,$prefix){
	global $urls;
	$subPages=dbMatrix('SELECT id,fullurl FROM pages WHERE parent_id="'.$parent.'" AND '.SANDBOX_ACTIVITY_ALL.' AND linked_url="";');
	if($subPages){
		foreach($subPages as $subPage){
			$urls[]=$prefix.$subPage['fullurl'].'/';
			findPages($subPage['id'],$prefix);
		}
	}
}

dbConnect();

$settings=dbSingle('SELECT * FROM settings;');
$urls=array();
for($i=1;isset($settings['language_'.$i]) && $settings['language_'.$i]!=0;$i++){
	$langNode=dbSingle('SELECT type_'.$i.' as url FROM translations WHERE keyword="language url";');
	if($langNode){
		$lang=$i;
		$langurl='http://'.$_SERVER['HTTP_HOST'].$langNode['url'].'/';
		if(trim($langNode['url'])==''){
			$langNode='http://'.$_SERVER['HTTP_HOST'].'/';
		} else {
			$langNode='http://'.$_SERVER['HTTP_HOST'].'/'.$langNode['url'].'/';
		}
		$root=dbSingle('SELECT id FROM pages WHERE type="'.$i.'" AND hook="'.SANDBOX_ROOT_KEY.'";');
		if($root){
			$urls[]=$langNode;
			findPages($root['id'],$langNode);
		}	
	}
}

$productspage=dbSingle('SELECT fullurl FROM pages WHERE hook="products" AND type='.$lang);

$categories=dbMatrix('SELECT fullurl_'.$lang.' AS url FROM product_categories WHERE active=1','url');
if(is_array($categories)){
	foreach($categories AS $category){
		$urls[]=$langurl.$productspage['fullurl'].'/'.$category;
	}
}

$products=dbMatrix('SELECT p.url_'.$lang.' AS url,pc.fullurl_'.$lang.' AS curl FROM products AS p, product_categories AS pc,products_categories AS pcs WHERE pcs.product_id=p.id AND pcs.category_id=pc.id AND pc.active=1 AND p.active=1');
if(is_array($products)){
	foreach($products AS $product){
		$urls[]=$langurl.$productspage['fullurl'].'/'.$product['curl'].'/'.$product['url'];
	}
}

$news=dbMatrix('SELECT url_'.$lang.' AS url FROM news WHERE active=1 AND type='.$lang);
if(is_array($news)){
	$newspage=dbSingle('SELECT fullurl FROM pages WHERE hook="news" AND type='.$lang);
	foreach($news AS $new){
		$urls[]=$langurl.$newspage['fullurl'].'/'.$new['url'];
	}
}

dbClose();

if(!empty($urls)){
	foreach($urls as $u){
		echo '<url><loc>'.$u.'</loc></url>';
	}
}

?>

</urlset>