<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your opinion) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

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

//===========================================
//Database: Connect
//===========================================
require_once("inc/inc_config.php");
require_once("inc/inc_database.php");

$dblink = new database();
$dblink->connect();

//===========================================
//TIMEZONE SETTING + DATABASE CHECK
//===========================================
$timezonerow = $dblink->get_row("SELECT timezone FROM memht_config") or die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Attention:</b> Cannot find database tables!</td></tr></table>");
$siteConfig['timezone'] = intval($timezonerow['timezone']);
$tzNOW = "DATE_ADD(NOW(),INTERVAL ".$siteConfig['timezone']." HOUR)";

require_once("inc/inc_login.php");
require_once("inc/inc_functions.php");
require_once("inc/inc_readConfig.php");
require_once("inc/inc_getinfo.php");
require_once("inc/inc_ban.php");

if (file_exists("lang/".$siteConfig['language'].".php")) {
	include_once("lang/".$siteConfig['language'].".php");
} else {
	include_once("lang/".$siteConfig['default_language'].".php");
}

if (!defined("_LANG_CHARSET_")) { define("_LANG_CHARSET_","utf-8"); }

$now = date(Y."-".m."-".d);

echo "<?xml version=\"1.0\" encoding=\""._LANG_CHARSET_."\"?>\n";
echo "<?xml-stylesheet type=\"text/xsl\" href=\"sitemap.xsl\"?>\n";
echo "<urlset 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/09/sitemap.xsd\" xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
//===========================================

//Index
echo "<url>\n";
	if ($siteConfig['modrewrite']) { echo "<loc>".$siteConfig['site_url']."/index.html</loc>\n"; } else { echo "<loc>".$siteConfig['site_url']."/index.php</loc>\n"; }
	echo "<lastmod>$now</lastmod>\n";
	echo "<changefreq>daily</changefreq>\n";
	echo "<priority>1.0</priority>\n";
echo "</url>\n";

$pages = $dblink->get_list("SELECT * FROM memht_pagine WHERE enabled=1 AND rank=0 ORDER BY nome");
foreach ($pages as $prow) {
	$page = outCode($prow['nome']);
	
	if (!stristr("banners",$page) AND !stristr("error",$page) AND !stristr("messages",$page) AND !stristr("submission",$page)) {
		echo "<url>\n";
			if ($siteConfig['modrewrite']) {
				echo "<loc>".$siteConfig['site_url']."/$page.html</loc>\n";
			} else {
				echo "<loc>".$siteConfig['site_url']."/index.php?page=$page</loc>\n";
			}
			echo "<lastmod>$now</lastmod>\n";
			echo "<changefreq>weekly</changefreq>\n";
			echo "<priority>0.5</priority>\n";
		echo "</url>\n";
		
		switch ($page) {
			case "articles":
				articles();
			break;
			case "blog":
				blog();
			break;
			case "download":
				download();
			break;
			case "faq":
				faq();
			break;
			case "forum":
				forum();
			break;
			case "guide":
				guide();
			break;
			case "mypage":
				mypage();
			break;
			case "news":
				news();
			break;
		}
	}
}

if ($siteConfig['virtualpages']==1) {
	$result = $dblink->get_list("SELECT name,title FROM memht_virtualpages WHERE enabled=1 AND rank=0 ORDER BY title");
	foreach ($result as $row) {
		$name = outCode($row['name']);
		$title = outCode($row['title']);
		
		echo "<url>\n";
			echo "<loc>".$siteConfig['site_url']."/index.php?page=$name</loc>\n";
			echo "<lastmod>$now</lastmod>\n";
			echo "<changefreq>weekly</changefreq>\n";
			echo "<priority>0.5</priority>\n";
		echo "</url>\n";
	}
}

//Articles
function articles() {
	global $siteConfig,$dblink,$now;
	
	$result = $dblink->get_list("SELECT *,DATE_FORMAT(data,'%Y-%m-%d') as date FROM memht_articoli WHERE enabled=1 ORDER BY id DESC LIMIT 200");
	foreach ($result as $row) {
		$id = intval($row['id']);
		$name = outCode($row['nome']);
		$date = $row['date'];
		if ($date=="0000-00-00") { $date = $now; }
				
		echo "<url>\n";
			if ($siteConfig['modrewrite']) {
				echo "<loc>".$siteConfig['site_url']."/articles_{$id}_".mem_urlencode($name).".html</loc>\n";
			} else {
				echo "<loc>".$siteConfig['site_url']."/index.php?page=articles&amp;op=readArticle&amp;id=$id&amp;title=".mem_urlencode($name)."</loc>\n";
			}
			echo "<lastmod>$date</lastmod>\n";
			echo "<changefreq>weekly</changefreq>\n";
			echo "<priority>0.8</priority>\n";
		echo "</url>\n";
	}
}

//Blog
function blog() {
	global $siteConfig,$dblink,$now;
	
	$result = $dblink->get_list("SELECT *,DATE_FORMAT(date,'%Y-%m-%d') as date FROM memht_blog_posts WHERE enabled=1 ORDER BY id DESC LIMIT 200");
	foreach ($result as $row) {
		$id = intval($row['id']);
		$title = outCode($row['title']);
		$date = $row['date'];
		if ($date=="0000-00-00") { $date = $now; }
				
		echo "<url>\n";
			if ($siteConfig['modrewrite']) {
				echo "<loc>".$siteConfig['site_url']."/blog_view_{$id}_".mem_urlencode($title).".html</loc>\n";
			} else {
				echo "<loc>".$siteConfig['site_url']."/index.php?page=blog&amp;id=$id&amp;title=".mem_urlencode($title)."</loc>\n";
			}
			echo "<lastmod>$date</lastmod>\n";
			echo "<changefreq>weekly</changefreq>\n";
			echo "<priority>0.8</priority>\n";
		echo "</url>\n";
	}
}

//Download
function download() {
	global $siteConfig,$dblink,$now;
	
	$result = $dblink->get_list("SELECT *,DATE_FORMAT(data,'%Y-%m-%d') as date FROM memht_download ORDER BY id DESC LIMIT 200");
	foreach ($result as $row) {
		$date = $row['date'];
		$id = intval($row['id']);
		$name = outCode($row['nome']);
		if ($date=="0000-00-00") { $date = $now; }
				
		echo "<url>\n";
			if ($siteConfig['modrewrite']) {
				echo "<loc>".$siteConfig['site_url']."/download_file_{$id}_".mem_urlencode($name).".html</loc>\n";
			} else {
				echo "<loc>".$siteConfig['site_url']."/index.php?page=download&amp;op=getFile&amp;id=$id&amp;title=".mem_urlencode($name)."</loc>\n";
			}
			echo "<lastmod>$date</lastmod>\n";
			echo "<changefreq>weekly</changefreq>\n";
			echo "<priority>0.8</priority>\n";
		echo "</url>\n";
	}
}

//Faq
function faq() {
	global $siteConfig,$dblink,$now;
	
	$result = $dblink->get_list("SELECT id FROM memht_faq ORDER BY id DESC LIMIT 500");
	foreach ($result as $row) {
		$id = intval($row['id']);
				
		echo "<url>\n";
			if ($siteConfig['modrewrite']) {
				echo "<loc>".$siteConfig['site_url']."/faq_$id.html</loc>\n";
			} else {
				echo "<loc>".$siteConfig['site_url']."/index.php?page=faq&amp;op=read&amp;id=$id</loc>\n";
			}
			echo "<lastmod>$now</lastmod>\n";
			echo "<changefreq>weekly</changefreq>\n";
			echo "<priority>0.7</priority>\n";
		echo "</url>\n";
	}
}

//Forum
function forum() {
	global $siteConfig,$dblink,$now;
	
	require_once("pages/forum/inc_functions.php");
	
	$result = $dblink->get_list("SELECT *,DATE_FORMAT(date,'%Y-%m-%d') as date FROM memht_forum_posts WHERE parent=0 ORDER BY id DESC LIMIT 200");
	foreach ($result as $row) {
		$threadid = intval($row['id']);
		$thname = outCode($row['name']);
		$forum = intval($row['forum']);
		$date = $row['date'];
		
		if ($siteConfig['modrewrite']) {
			$link = "forum_thread_{$threadid}_".mem_urlencode($thname).".html";
		} else {
			$link = "index.php?page=forum&amp;op=viewThread&amp;id=$threadid&amp;title=".mem_urlencode($thname);
		}			
		
		if (isAuthorized($forum,"view") AND isAuthorized($forum,"read")) {
			echo "<url>\n";
				echo "<loc>".$siteConfig['site_url']."/$link</loc>\n";
				echo ($date!="0000-00-00") ? "<lastmod>$date</lastmod>\n" : "" ;
				echo "<changefreq>daily</changefreq>\n";
				echo "<priority>0.6</priority>\n";
			echo "</url>\n";
		}
	}
}

//Guide
function guide() {
	global $siteConfig,$dblink,$now;
	
	$result = $dblink->get_list("SELECT *,DATE_FORMAT(data,'%Y-%m-%d') as date FROM memht_guide WHERE enabled=1 ORDER BY id DESC LIMIT 200");
	foreach ($result as $row) {
		$id = intval($row['id']);
		$name = outCode($row['nome']);
		$date = $row['date'];
		if ($date=="0000-00-00") { $date = $now; }
				
		echo "<url>\n";
			if ($siteConfig['modrewrite']) {
				echo "<loc>".$siteConfig['site_url']."/guide_{$id}_".mem_urlencode($name).".html</loc>\n";
			} else {
				echo "<loc>".$siteConfig['site_url']."/index.php?page=guide&amp;op=readGuide&amp;id=$id&amp;title=".mem_urlencode($name)."</loc>\n";
			}
			echo "<lastmod>$date</lastmod>\n";
			echo "<changefreq>weekly</changefreq>\n";
			echo "<priority>0.8</priority>\n";
		echo "</url>\n";
	}
}

//MyPage
function mypage() {
	global $siteConfig,$dblink,$now;
	
	$myrank = myRank();
	$result = $dblink->get_list("SELECT id,title,DATE_FORMAT(date,'%Y-%m-%d') as date FROM memht_mypage WHERE public=1 AND enabled=1 AND rank<=$myrank ORDER BY id LIMIT 200");
	foreach ($result as $row) {
		$id = intval($row['id']);
		$title = outCode($row['title']);
		$date = $row['date'];
		if ($date=="0000-00-00") { $date = $now; }
		
		echo "<url>\n";
			if ($siteConfig['modrewrite']) {
				echo "<loc>".$siteConfig['site_url']."/mypage_{$id}_".mem_urlencode($title).".html</loc>\n";
			} else {
				echo "<loc>".$siteConfig['site_url']."/index.php?page=mypage&amp;op=openPage&amp;id=$id&amp;title=".mem_urlencode($title)."</loc>\n";
			}
			echo "<lastmod>$date</lastmod>\n";
			echo "<changefreq>weekly</changefreq>\n";
			echo "<priority>0.6</priority>\n";
		echo "</url>\n";
	}
}

//News
function news() {
	global $siteConfig,$dblink,$now;
	
	$result = $dblink->get_list("SELECT *,DATE_FORMAT(data,'%Y-%m-%d') as date FROM memht_news WHERE enabled=1 ORDER BY id DESC LIMIT 200");
	foreach ($result as $row) {
		$id = intval($row['id']);
		$name = outCode($row['nome']);
		$date = $row['date'];
		if ($date=="0000-00-00") { $date = $now; }
				
		echo "<url>\n";
			if ($siteConfig['modrewrite']) {
				echo "<loc>".$siteConfig['site_url']."/news_{$id}_".mem_urlencode($name).".html</loc>\n";
			} else {
				echo "<loc>".$siteConfig['site_url']."/index.php?page=news&amp;op=readNews&amp;id=$id&amp;title=".mem_urlencode($name)."</loc>\n";
			}
			echo "<lastmod>$date</lastmod>\n";
			echo "<changefreq>weekly</changefreq>\n";
			echo "<priority>0.9</priority>\n";
		echo "</url>\n";
	}
}

//===========================================
echo "</urlset>\n";

//===========================================
//Database: Disconnect
//===========================================
$dblink->disconnect();

?>