<?php

/**
 *	sitemap.xml
 *
 *	Copyright (c) 2010 Multisys Computers Ltd. All Rights Reserved.
 *
 *	Generates a sitemap XML document from current live articles.
 *	As this has a .xml extension, you will need the following in
 *	the .htaccess file to allow PHP to be parsed in XML files.
 *		AddType application/x-httpd-php .xml
 *
 *	You can also add the location of the sitemap.xml file to the
 *	robots.txt file as follows
 *		Sitemap: http://www.example.com/sitemap.xml
 *
 *	More information on XML Sitemaps at
 *	http://www.sitemaps.org/protocol.php
 *
 *	@copyright	2010 Multisys Computers Ltd. All Rights Reserved
 *	@version		1.01
 */

$rootpath = "./";
include $rootpath."includes/utils.php";
$db = connect_db();

$sitemapxml = false;

$getarticlesQuery = "select s.path, a.sectionindex, a.name, a.articleid, a.created, a.lastedited from `article` a inner join `sections` s on s.sectionid = a.sectionid where a.islabel <> '1' and a.active ='1' order by a.sectionid, a.articleid";

$getarticlesResult = mysql_query($getarticlesQuery);
if(mysql_num_rows($getarticlesResult) > 0)
{
	//	Articles found, generate the sitemap XML
	$sitemapxml .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	$sitemapxml .= "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
	$server = "http://".$_SERVER["HTTP_HOST"]."/";
	while($articledata = mysql_fetch_object($getarticlesResult))
	{
		$sitemapxml .= "\t<url>\n";

		if($articledata->sectionindex)
		{
			$loc = "index.php";
		}
		else
		{
			if($articledata->name)
			{
				$loc = $articledata->name;
			}
			else
			{
				$loc = "index.php?article=".$articledata->articleid;
			}
		}

		if($articledata->lastedited)
		{
			$lastedited = $articledata->lastedited;
		}
		else
		{
			$lastedited = $articledata->created;
		}
		$timeoffset = date("O", strtotime($lastedited));
		$timeoffsetstr = sprintf("%s:%s", substr($timeoffset, 0, 3), substr($timeoffset, 3, 2));
		$lastedited = date("Y-m-d", strtotime($lastedited))."T".date("H:i:s", strtotime($lastedited)).$timeoffsetstr;

		$sitemapxml .= "\t\t<loc>".htmlentities($server.$articledata->path.$loc)."</loc>\n";
		$sitemapxml .= "\t\t<lastmod>".$lastedited."</lastmod>\n";

		$sitemapxml .= "\t</url>\n";
	}

	$sitemapxml .= "</urlset>\n";

	header("Content-Type: application/xml");
	header("Content-Length: ".strlen($sitemapxml));
	echo $sitemapxml;
}
else
{
	//	No results, issue a 404 for now? Prob a better response can be found...
	header("HTTP/1.0 404 Not Found");
}

?>