<?php
header('Content-Type: application/xml; charset=utf-8');
$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,strpos( $_SERVER["SERVER_PROTOCOL"],'/'))).'://';
$fullurl = (htmlentities($_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'], ENT_QUOTES, "UTF-8"));
$siteurl1 = substr($fullurl, 0, strrpos( $fullurl, '/'));
$siteurl = substr($siteurl1, strrpos($siteurl1, '/') + 1);
$sitename = substr($siteurl, 0, strrpos( $siteurl, '.'));

if (strpos($fullurl, '/sites/') === false) {
require_once 'includes/connect.php';
} else {
require_once 'sites/'.$sitename.'/includes/connect.php';
}

$result = $mysqli->query("SELECT * FROM siteinfo");
$row = $result->fetch_array();
$domain=$row['domain'];
echo '<?xml version="1.0" encoding="UTF-8" ?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php
$result1 = $mysqli->query("SELECT * FROM lang WHERE active=1 ORDER BY main desc, priority asc");
while($row = $result1->fetch_array()) {
$langid=$row['id'];
$dir=$row['dir'];
$main=$row['main'];
$lpriority=$row['priority'];
if ($main==1) { 
$dir="/";
$langpriority=1;
} else {
$dir="/".$dir."/";
$langpriority=0.9;
}
?>
<url><loc><?php echo $protocol ?>www.<?php echo $domain ?><?php echo $dir ?></loc><changefreq>weekly</changefreq><priority><?php echo $langpriority ?></priority></url>
<?php
$result50 = $mysqli->query("SELECT insert_id, page, name, type FROM categories WHERE level=1 AND type<3 AND insert_id>1 AND lang_id=$langid AND publish='yes' ORDER BY priority");
while($row = $result50->fetch_array()) {
$ncatid1=$row['insert_id'];
$npage1=$row['page'];
$ncategory1=$row['name'];
$type1=$row['type'];

$result51 = $mysqli->query("SELECT id, insert_id FROM categories WHERE parent=$ncatid1 AND lang_id=$langid AND publish='yes'");
$nrrows51 = mysqli_num_rows($result51);
$row = $result51->fetch_array();
$ncatid12=$row['insert_id'];
	
$result52 = $mysqli->query("SELECT id FROM items WHERE category_id=$ncatid1 AND section_id=0 AND lang_id=$langid AND publish='yes'");
$nrrows52 = mysqli_num_rows($result52);

$result52b = $mysqli->query("SELECT id FROM items WHERE (category_id=$ncatid1 OR category_id IN (SELECT insert_id FROM categories WHERE parent=$ncatid1)) AND section_id>0 AND lang_id=$langid AND publish='yes'");
$nrrows52b = mysqli_num_rows($result52b);

if ($nrrows52b==0) {

if ($nrrows51>0) {
echo "<url><loc>".$protocol."www.".$domain.$dir.$npage1."</loc><changefreq>weekly</changefreq><priority>0.8</priority></url>
";
$result53 = $mysqli->query("SELECT insert_id, page, name, image FROM categories WHERE parent=$ncatid1 AND lang_id=$langid AND publish='yes' ORDER BY priority");
while($row = $result53->fetch_array()) {
$ncatid2=$row['insert_id'];
$npage2=$row['page'];
$ncategory2=$row['name'];
$nimage2=$row['image'];

echo "<url><loc>".$protocol."www.".$domain.$dir.$npage2."</loc><changefreq>weekly</changefreq><priority>0.8</priority></url>
";
}
}else{
echo "<url><loc>".$protocol."www.".$domain.$dir.$npage1."</loc><changefreq>weekly</changefreq><priority>0.8</priority></url>
";
}
}

}

$resections = $mysqli->query("SELECT COUNT(id) FROM sections WHERE lang_id=$langid AND publish='yes'");
$rows = $resections->fetch_row();
$nrsections = $rows[0];
if ($nrsections>0) {

$result60 = $mysqli->query("SELECT insert_id, page, name FROM sections WHERE lang_id=$langid AND publish='yes' ORDER BY level, priority");
while($row = $result60->fetch_array()) {
$secid1=$row['insert_id'];
$pagesection1=$row['page'];
$section1=$row['name'];

$result61 = $mysqli->query("SELECT insert_id, page, name FROM categories WHERE level=1 AND type<3 AND insert_id>1 AND lang_id=$langid AND publish='yes' ORDER BY priority");
while($row = $result61->fetch_array()) {
$mcatid1=$row['insert_id'];
$mpage1=$row['page'];
$mcategory1=$row['name'];

$result63 = $mysqli->query("SELECT COUNT(id) FROM items WHERE category_id IN (SELECT insert_id FROM categories WHERE insert_id='$mcatid1' OR parent='$mcatid1') AND section_id IN (SELECT insert_id FROM sections WHERE insert_id='$secid1' OR parent='$secid1') AND lang_id=$langid AND publish='yes'");
$rows = $result63->fetch_row();
$nrrows63 = $rows[0];

if ($nrrows63>0) {
echo "<url><loc>".$protocol."www.".$domain.$dir.$pagesection1.".".$mpage1."</loc><changefreq>weekly</changefreq><priority>0.8</priority></url>
";
}

$result64 = $mysqli->query("SELECT insert_id, page, name FROM sections WHERE parent='$secid1' AND lang_id=$langid AND publish='yes' ORDER BY priority");
while($row = $result64->fetch_array()) {
$secid2=$row['insert_id'];
$pagesection2=$row['page'];
$section2=$row['name'];

$result65 = $mysqli->query("SELECT COUNT(id) FROM items WHERE category_id='$mcatid1' AND section_id='$secid2' AND lang_id=$langid AND publish='yes'");
$rows = $result65->fetch_row();
$nrrows65 = $rows[0];

if ($nrrows65>0) {
echo "<url><loc>".$protocol."www.".$domain.$dir.$pagesection1."-".$pagesection2.".".$mpage1."</loc><changefreq>weekly</changefreq><priority>0.7</priority></url>
";
}
}

$result66 = $mysqli->query("SELECT insert_id, page, name FROM categories WHERE parent='$mcatid1' AND lang_id=$langid AND publish='yes' ORDER BY priority");
while($row = $result66->fetch_array()) {
$mcatid2=$row['insert_id'];
$mpage2=$row['page'];
$mcategory2=$row['name'];

$result67 = $mysqli->query("SELECT COUNT(id) FROM items WHERE category_id='$mcatid2' AND section_id='$secid1' AND lang_id=$langid AND publish='yes'");
$rows = $result67->fetch_row();
$nrrows67 = $rows[0];

if ($nrrows67>0) {
echo "<url><loc>".$protocol."www.".$domain.$dir.$pagesection1.".".$mpage2."</loc><changefreq>weekly</changefreq><priority>0.8</priority></url>
";
}

$result68 = $mysqli->query("SELECT insert_id, page, name FROM sections WHERE parent='$secid1' AND lang_id=$langid AND publish='yes' ORDER BY priority");
while($row = $result68->fetch_array()) {
$secid2=$row['insert_id'];
$pagesection2=$row['page'];
$section2=$row['name'];

$result69 = $mysqli->query("SELECT COUNT(id) FROM items WHERE category_id='$mcatid2' AND section_id='$secid2' AND lang_id=$langid AND publish='yes'");
$rows = $result69->fetch_row();
$nrrows69 = $rows[0];

if ($nrrows69>0) {
echo "<url><loc>".$protocol."www.".$domain.$dir.$pagesection1."-".$pagesection2.".".$mpage2."</loc><changefreq>weekly</changefreq><priority>0.7</priority></url>
";
}
}
}
}
}

}
$result70 = $mysqli->query("SELECT insert_id, page_id, page FROM items WHERE showpage=1 AND page_id>0 AND lang_id=$langid AND publish='yes' ORDER BY page_id, priority");
while($row = $result70->fetch_array()) {
$pid=$row['page_id'];
$itpage=$row['page'];
echo "<url><loc>".$protocol."www.".$domain.$dir.$itpage.".".$pid."</loc><changefreq>weekly</changefreq><priority>0.6</priority></url>
";
}
$result71 = $mysqli->query("SELECT id FROM members WHERE publish='yes'");
$nrrows71 = mysqli_num_rows($result71);
if ($nrrows71>0) {
echo "<url><loc>".$protocol."www.".$domain.$dir."reviews"."</loc><changefreq>weekly</changefreq><priority>0.5</priority></url>
";
}
$result72 = $mysqli->query("SELECT id FROM prices");
$nrrows72 = mysqli_num_rows($result72);
if ($nrrows72>0) {
echo "<url><loc>".$protocol."www.".$domain.$dir."rates"."</loc><changefreq>weekly</changefreq><priority>0.5</priority></url>
";
}
?>
<url><loc><?php echo $protocol ?>www.<?php echo $domain ?><?php echo $dir ?>contact</loc><changefreq>weekly</changefreq><priority>0.5</priority></url>
<?php } ?>
</urlset>