<?php

// Версия 4.2.1	   20.02.2016
//////////////////////////////////////////////////

define('_AREA_', 'sitemap.xml');
require("admin/req/start.php");

function makeUrlString($urlString)
{
	return htmlentities($urlString, ENT_QUOTES, 'UTF-8');
}

function makeIso8601TimeStamp($dateTime)
{
	if(!$dateTime) $dateTime = date('Y-m-d H:i:s');
	if(is_numeric(substr($dateTime, 11, 1))) $isoTS = substr($dateTime, 0, 10) . "T" . substr($dateTime, 11, 8) . "+00:00";
	else $isoTS = substr($dateTime, 0, 10);
	return $isoTS;
}


function makeUrlTag ($url, $modifiedDateTime, $changeFrequency, $priority)
{
	GLOBAL $newLine;
	GLOBAL $indent;
	GLOBAL $isoLastModifiedSite;
	$urlOpen = "$indent<url>$newLine";
	$urlValue = "";
	$urlClose = "$indent</url>$newLine";
	$locOpen = "$indent$indent<loc>";
	$locValue = "";
	$locClose = "</loc>$newLine";
	$lastmodOpen = "$indent$indent<lastmod>";
	$lastmodValue = "";
	$lastmodClose = "</lastmod>$newLine";
	$changefreqOpen = "$indent$indent<changefreq>";
	$changefreqValue = "";
	$changefreqClose = "</changefreq>$newLine";
	$priorityOpen = "$indent$indent<priority>";
	$priorityValue = "";
	$priorityClose = "</priority>$newLine";

	$urlTag = $urlOpen;
	$urlValue = $locOpen .makeUrlString("$url") .$locClose;
	if($modifiedDateTime)
	{
		$urlValue .= $lastmodOpen . makeIso8601TimeStamp($modifiedDateTime) . $lastmodClose;
		// last modification of web site
		if(!$isoLastModifiedSite) $isoLastModifiedSite = makeIso8601TimeStamp($modifiedDateTime);
	}
	if($changeFrequency) $urlValue .= $changefreqOpen . $changeFrequency . $changefreqClose;
	if($priority) $urlValue .= $priorityOpen . $priority . $priorityClose;
	$urlTag .= $urlValue;
	$urlTag .= $urlClose;
	return $urlTag;
}

$isoLastModifiedSite = '';
$newLine = "\n";
$indent = " ";
$xmlHeader = '<'.'?xml version="1.0" encoding="UTF-8"?'.'>'.$newLine;
$urlsetOpen = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'.$newLine;
$urlsetValue = '';
$urlsetClose = '</urlset>'.$newLine;

header('Content-type: application/xml; charset="utf-8"',true);
$PATH = array();

	// Берём все меню, которые видимы
	$thm_menu = mysql_query("SELECT * FROM " . $db->GetPrefix() . "menu WHERE visible ORDER BY pos");
	// Если есть такие
	if(mysql_num_rows($thm_menu))
	{
		// Проходим по всем Меню
		while($myMenu = mysql_fetch_array($thm_menu))
		{
			// Проверили, если админ - ПРОПУСКАЕМ !!!
			if(is_integer(strpos($myMenu["options"],"admin"))) continue;

			$thm_page = mysql_query("SELECT id_page, name, last_mod, priority, changefreq, show_in_sitemap_xml, id_parent FROM " . $db->GetPrefix() . "page WHERE visible AND id_lang=".LANG." AND id_menu=".$myMenu["id_menu"]);
			// Проходим по всем Страницам
			while($myPage = mysql_fetch_array($thm_page))
			{
				// Проверили, если !show_in_sitemap_xml - ПРОПУСКАЕМ !!!
				if(!$myPage['show_in_sitemap_xml']) continue;

				// Получили значения
				$myPage['priority'] = sprintf("%01.1f", $myPage['priority']);

				$myPage['last_mod'] = ($myPage['last_mod'] != '0000-00-00 00:00:00') ? $myPage['last_mod'] : date("Y-m-d H:i:s");
				$myPage['changefreq'] = ($myPage['changefreq'])? $myPage['changefreq'] : $Setup['strPageDefChangefreq'];
				$isoLastModifiedSite = makeIso8601TimeStamp($myPage['last_mod']);

				// Получили ссылку
				$myPage['link'] = MAIN_URL.CreateLink($myPage['id_parent'],$myPage['name']).'/';
				$myPage['link'] = ($myPage['link'] == MAIN_URL.'index/') ? MAIN_URL : $myPage['link'];

				// Вывели результат
				$urlsetValue .= makeUrlTag ($myPage['link'], $isoLastModifiedSite, $myPage['changefreq'], $myPage['priority']);

				// Если на странице установлен Модуль, то проверяем наличие in_sitemap.php в папке модуля
				$thm_module = mysql_query("SELECT module_name FROM " . $db->GetPrefix() . "module WHERE id_page_".LANG."=".$myPage['id_page']." AND active AND visible");
				if(mysql_num_rows($thm_module))
				{
					$myModule = mysql_fetch_array($thm_module);
					if(is_file(MODULES_PATH.$myModule['module_name'].'/in_sitemap.php')) include MODULES_PATH.$myModule['module_name'].'/in_sitemap.php';
				}
			}
		}
	}


print "$xmlHeader
$urlsetOpen
$urlsetValue
$urlsetClose
";

?>