<?php

(!defined('IN_PowerBB')) ? die() : '';

$CALL_SYSTEM					=	array();
$CALL_SYSTEM['SECTION'] 		= 	true;
$CALL_SYSTEM['SUBJECT'] 		= 	true;

include('common.php');

define('CLASS_NAME','PowerBBSitemapMOD');

class PowerBBSitemapMOD
{
	function run()
	{
		global $PowerBB;


	 if ($PowerBB->_GET['sitemaps'])
		{
		global $PowerBB;
		$PowerBB->_GET['count'] = (!isset($PowerBB->_GET['count'])) ? 0 : $PowerBB->_GET['count'];
		$PowerBB->_GET['count'] = $PowerBB->functions->CleanVariable($PowerBB->_GET['count'],'intval');

		$GetSubjectNumber = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT * FROM " . $PowerBB->table['subject'] . " WHERE review_subject<>1 AND sec_subject<>1 AND delete_topic<>1 "));
		$SubjectArr = array();
		$SubjectArr['where'] 				= 	array();
		$SubjectArr['where'][0] 			= 	array();
		$SubjectArr['where'][0]['name'] 	= 	'review_subject<>1 AND sec_subject<>1 AND delete_topic';
		$SubjectArr['where'][0]['oper'] 	= 	'<>';
		$SubjectArr['where'][0]['value'] 	= 	'1';

		$SubjectArr['order'] 			= 	array();
		$SubjectArr['order']['field'] 	= 	'write_time';
		$SubjectArr['order']['type'] 	= 	'DESC';

		$SubjectArr['proc'] 			= 	array();
		// Ok Mr.XSS go to hell !
		$SubjectArr['proc']['*'] 		= 	array('method'=>'clean','param'=>'html');

		// Pager setup
		$SubjectArr['pager'] 				= 	array();
		$SubjectArr['pager']['total']		= 	$GetSubjectNumber;
		$SubjectArr['pager']['perpage'] 	= 	$PowerBB->_CONF['info_row']['subject_perpage'];
		$SubjectArr['pager']['count'] 		= 	$PowerBB->_GET['count'];
		$SubjectArr['pager']['location'] 	= 	'index.php?page=sitemap&amp;sitemaps=1';
		$SubjectArr['pager']['var'] 		= 	'count';

		$SubjectList = $PowerBB->subject->GetSubjectList($SubjectArr);

		$size 	= 	sizeof($SubjectList);
		$x		=	0;
		$charset1                =   $PowerBB->_CONF['info_row']['content_dir'];
		$links = $PowerBB->_CONF['info_row']['rewriterule'];
	    	if ($links == 0 ){
	    	$url = "index.php?page=topic&amp;show=1&amp;id=";
		$extention = "";
	    	} else {
	    	$url = "t";
		$extention = ".html";
		}
			echo "<html dir='$charset1'>";
			echo '<head>';
			echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
         	echo '	<title>' .$PowerBB->_CONF['template']['lang']['Sitemap_title'] . ' - ' .$PowerBB->_CONF['info_row']['title'] . ' Sitemape</title>';
			echo '</head>';
             echo '<ol>';
            echo  '<li><a target="_blank" href="'.$PowerBB->functions->GetForumAdress().'">'.$PowerBB->_CONF['info_row']['title'].'</a><br />';
            echo '<font color="#008000" size="-1">'.$PowerBB->functions->GetForumAdress().'</font><br />';
            echo $PowerBB->_CONF['info_row']['description'].'</li>';
			echo '<hr>';
		while ($x < $size)
		{
			echo '<li>';
            echo  '<a target="_blank" href="'.$PowerBB->functions->GetForumAdress() . $url . $SubjectList[$x]['id'].$extention.'">'.$PowerBB->_CONF['info_row']['title'].' - '.$SubjectList[$x]['title'].'</a><br />';
			echo '<font color="#008000" size="-1">'.$PowerBB->functions->GetForumAdress() . $url . $SubjectList[$x]['id'].$extention.'</font><br />';
			echo '<hr>';
			echo '</li>';
			echo '</html>';
			$x += 1;
		}
		echo '</ol>';

		if ($GetSubjectNumber > $PowerBB->_CONF['info_row']['subject_perpage'])
		{
		print($PowerBB->pager->show());
		}
	  }


		if ($PowerBB->_GET['subject'])
		{
		    $forum_url              =   $PowerBB->functions->GetForumAdress();
			$charset                =   $PowerBB->_CONF['info_row']['charset'];
			header('Content-Type: text/xml');
	        echo "<?xml version=\"1.0\" encoding=\"$charset\"?><?xml-stylesheet type=\"text/xsl\" href=\"$forum_url"."sitemap.xsl\"?><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/0.9/sitemap.xsd\" xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n\n";
			echo '	<url>'."\n";
			echo '		<loc>' . $forum_url . '</loc>'."\n";
			echo '		<changefreq>daily</changefreq>'."\n";
			echo '		<priority>1.0</priority>'."\n";
			echo '	</url>'."\n";
			$this->_SubjectSitemap();
		}
		elseif ($PowerBB->_GET['section'])
		{
			$this->_SectionSitemap();
		}
         echo '</urlset>';


	}

	function _SubjectSitemap()
	{
		global $PowerBB;
		$SubjectArr = array();
		$SubjectArr['where'] 				= 	array();
		$SubjectArr['where'][0] 			= 	array();
		$SubjectArr['where'][0]['name'] 	= 	'review_subject<>1 AND sec_subject<>1 AND delete_topic';
		$SubjectArr['where'][0]['oper'] 	= 	'<>';
		$SubjectArr['where'][0]['value'] 	= 	'1';

		$SubjectArr['order'] 			= 	array();
		$SubjectArr['order']['field'] 	= 	'write_time';
		$SubjectArr['order']['type'] 	= 	'DESC';

		$SubjectArr['limit'] 			= 	'40';

		$SubjectArr['proc'] 			= 	array();
		// Ok Mr.XSS go to hell !
		$SubjectArr['proc']['*'] 		= 	array('method'=>'clean','param'=>'html');

		$SubjectList = $PowerBB->subject->GetSubjectList($SubjectArr);

		$size 	= 	sizeof($SubjectList);
		$x		=	0;

		while ($x < $size)
		{
		    $links = $PowerBB->_CONF['info_row']['rewriterule'];
	    	if ($links == 0 ){
	    	$url = "index.php?page=topic&amp;show=1&amp;id=";
		    $extention = "";
	    	} else {
	    	$url = "t";
	    	$extention = ".html";
	     	}
            $SubjectList[$x]['text'] = $PowerBB->Powerparse->replace($SubjectList[$x]['text']);
			$PowerBB->Powerparse->replace_smiles($SubjectList[$x]['text']);
			echo '	<url>'."\n";
			echo '		<loc>' . $PowerBB->functions->GetForumAdress() . $url . $SubjectList[$x]['id'] . $extention . '</loc>'."\n";
			echo '		<changefreq>monthly</changefreq>'."\n";
			echo '		<priority>0.2</priority>'."\n";
			echo '	</url>'."\n";

			$x += 1;
		}
	}

	function _SectionSitemap()
	{
		global $PowerBB;

		// Clean id from any strings
		$PowerBB->_GET['id'] = $PowerBB->functions->CleanVariable($PowerBB->_GET['id'],'intval');

		// No _GET['id'] , so ? show a small error :)
		if (empty($PowerBB->_GET['id']))
		{
			echo $PowerBB->_CONF['template']['lang']['path_not_true'];
		}
		else
		{
			// Get section information and set it in $this->Section
			$SecArr 			= 	array();
			$SecArr['where'] 	= 	array('id',$PowerBB->_GET['id']);

			$Section = $PowerBB->section->GetSectionInfo($SecArr);

			// Clear section information from any denger
			$PowerBB->functions->CleanVariable($Section,'html');

			// Temporary array to save the parameter of GetSectionGroupList() in nice way
			$SecGroupArr 						= 	array();
			$SecGroupArr['where'] 				= 	array();

			$SecGroupArr['where'][0]			=	array(	'name' 	=> 'section_id',
															'oper'	=>	'=',
															'value'	=>	$Section['id']);

			$SecGroupArr['where'][1]			=	array();
			$SecGroupArr['where'][1]['con']		=	'AND';
			$SecGroupArr['where'][1]['name']	=	'group_id';
			$SecGroupArr['where'][1]['oper']	=	'=';
			$SecGroupArr['where'][1]['value']	=	$PowerBB->_CONF['group_info']['id'];


			// Ok :) , the permssion for this visitor/member in this section
			$SectionGroup = $PowerBB->group->GetSectionGroupInfo($SecGroupArr);

			// This section isn't exists
			if (!$Section)
			{
				$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Section_does_not_exist']);
			}

			// This member can't view this section
			if ($SectionGroup['view_section'] != 1)
			{
				echo $PowerBB->_CONF['template']['lang']['You_are_not_allowed_access_to_the_contents_of_this_forum'];

				return 0;
			}

			// This is main section , so we can't get subjects list from it
			if ($Section['main_section'])
			{
				echo $PowerBB->_CONF['template']['lang']['thes_main_section'];
				return 0;
			}

			if (!empty($Section['section_password']))
			{
			    	echo $PowerBB->_CONF['template']['lang']['This_forum_is_password_protected'];

				return 0;
			}


			$SubjectArr = array();

			$SubjectArr['where'] 				= 	array();

			$SubjectArr['where'][0] 			= 	array();
			$SubjectArr['where'][0]['name'] 	= 	'section';
			$SubjectArr['where'][0]['oper'] 	= 	'=';
			$SubjectArr['where'][0]['value'] 	= 	$PowerBB->_GET['id'];

	        $SubjectArr['where'][1] 			= 	array();
			$SubjectArr['where'][1]['con']		=	'AND';
	        $SubjectArr['where'][1]['name'] 	= 	'review_subject<>1 AND sec_subject<>1 AND delete_topic';
			$SubjectArr['where'][1]['oper'] 	= 	'<>';
			$SubjectArr['where'][1]['value'] 	= 	'1';

			$SubjectArr['order'] 			= 	array();
			$SubjectArr['order']['field'] 	= 	'write_time';
			$SubjectArr['order']['type'] 	= 	'DESC';

			$SubjectArr['limit'] 			= 	'100';

			$SubjectArr['proc'] 			= 	array();
			// Ok Mr.XSS go to hell !
			$SubjectArr['proc']['*'] 		= 	array('method'=>'clean','param'=>'html');

			$SubjectList = $PowerBB->subject->GetSubjectList($SubjectArr);

			$size 	= 	sizeof($SubjectList);
			$x		=	0;

		while ($x < $size)
		{
		    $links = $PowerBB->_CONF['info_row']['rewriterule'];
	    	if ($links == 0 ){
	    	$url = "index.php?page=topic&amp;show=1&amp;id=";
		    $extention = "";
	    	} else {
	    	$url = "t";
	    	$extention = ".html";
	     	}
            $SubjectList[$x]['text'] = $PowerBB->Powerparse->replace($SubjectList[$x]['text']);
			$PowerBB->Powerparse->replace_smiles($SubjectList[$x]['text']);
			echo '	<url>'."\n";
			echo '		<loc>' . $PowerBB->functions->GetForumAdress() . $url . $SubjectList[$x]['id'] . $extention . '</loc>'."\n";
			echo '		<changefreq>monthly</changefreq>'."\n";
			echo '		<priority>0.2</priority>'."\n";
			echo '	</url>'."\n";

			$x += 1;
		}
		}


	}



}

?>
