<?
include ($_SERVER['DOCUMENT_ROOT']."/caching_functions.php");

$cachefile = start_caching("sitemap.xml");

include ($_SERVER['DOCUMENT_ROOT']."/functions.php");

$pageInfo = $mysqli->query("SELECT address, UNIX_TIMESTAMP(last_modified) as last_modified FROM pages ORDER BY id ASC");

$postInfo = $mysqli->query("SELECT id, title, UNIX_TIMESTAMP(modified) as modified FROM posts ORDER BY modified DESC");

$content = "";

header ("Content-type: text/xml");

$content .= <<<XML
<?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">
XML;

while($pageObject = $pageInfo->fetch_object()) {
	if ($pageObject->address == "index") {
		$url = htmlentities("http://{$_SERVER['SERVER_NAME']}/");		
	} else {
		$url = htmlentities("http://{$_SERVER['SERVER_NAME']}/{$pageObject->address}/");		
	}
	
	$lastmod = date(DATE_ATOM,$pageObject->last_modified);

$content .= <<<XML
<url>
	<loc>$url</loc>
	<lastmod>$lastmod</lastmod>
</url>
XML;

}

while($postObject = $postInfo->fetch_object()) {

if(function_exists("custom_blog_url")) {
	$loc = "http://".$_SERVER['SERVER_NAME'].custom_blog_url($postObject->id, $postObject->title);
} else {
	$loc = "http://".$_SERVER['SERVER_NAME'].blog_url($postObject->id, $postObject->title);
}
$lastmod = date(DATE_ATOM, $postObject->modified);

$content .= <<<XML
<url>
	<loc>$loc</loc>
	<lastmod>$lastmod</lastmod>
</url>
XML;
}

$content .= "</urlset>";

echo $content;

end_caching($cachefile);
?>