<?
if (! isset($_GET['board']) OR ! is_numeric($_GET['board'])) {
	header("{$_SERVER['server_protocol']} 404 Not Found");
	exit;
}
$board = $_GET['board'];

require_once ('Settings.php');

mysql_connect($db_server,$db_user,$db_passwd);
mysql_select_db($db_name);
$query = mysql_query("SELECT t.ID_TOPIC, m.posterTime, m.modifiedTime, t.numReplies FROM smf_topics AS t, smf_messages AS m WHERE t.ID_BOARD='{$board}' AND t.ID_LAST_MSG=m.ID_MSG AND t.ID_BOARD NOT IN (13,14,26,39)");


if($query === FALSE) {
	header('HTTP/1.1 500 Internal Server Error');
	echo "<html><head><title>500 Internal Server Error</title></head><body>Internal server error. Please, come again later.</body><html>";
	exit;
}

if (mysql_num_rows($query) == 0) {
	header("{$_SERVER['server_protocol']} 404 Not Found");
	exit;
}

echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
<?
while($row = mysql_fetch_row($query)) {
	if (empty ($row[0])) continue;
	$ts = ($row[2] > $row[1]) ? date(DATE_W3C, $row[2]) : date(DATE_W3C, $row[1]);
	$pages = ceil($row[3] / 30);
	$freq = 'hourly';
	for (;$pages>=0;$pages--) {
?>
  <url>
    <loc>http://rock.ru/forum/index.php?topic=<?=$row[0]?>.<?=$pages*30?></loc>
    <lastmod><?=$ts?></lastmod>
    <changefreq><?=$freq?></changefreq>
</url>
<?
		$freq = 'daily';
	}
}
?>
</urlset>
