<?php
/**
 * @version $Id: plugin.pbmap.php v1.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

if (!defined('IN_PBDIGG'))
{
	exit('Access Denied!');
}
define('PBMAP', TRUE);
require_once(loadPluginLang('pbmap', TRUE));
require_once(PBDIGG_ROOT.'plugins/pbmap/include/pbmap.func.php');
$jumpurl = 'admincp.php?action=plugin&plugin=pbmap';

if ($submitted)
{
	$sql = $baidu = $google = $baidumap = $googlemap = '';
	if (!isset($sitemap))
	{
		showMsg($plugin_lang['pbmap_illegal_maptype'], $jumpurl);
	}
	if (is_array($sitemap))
	{
		$sitemap = implode(',', $sitemap);
	}
	if ($mapcate)
	{
		if (is_array($mapcate))
		{
			positiveInteger($mapcate);
			$mapcate = implode(',', $mapcate);
		}
		$sql = ' WHERE cid IN ('.$mapcate.')';
	}
	if (strpos($sitemap, 'baidu') !== FALSE) $baidu = TRUE;
	if (strpos($sitemap, 'google') !== FALSE) $google = TRUE;
	$count = 100;
	$page = $page ? (int)$page : 1;
	$start = ($page - 1) * $count;
	$page++;
	$DB->db_query("SELECT tid, cid, subject, postdate, commentdate FROM {$db_prefix}threads $sql LIMIT $start, $count");
	if ($DB->db_num())
	{
		if (!$start)
		{
			if ($baidu)
			{
				if (strtolower($db_charset) == 'gbk')
				{
					$mapcharset = 'GB2312';
				}
				else
				{
					$mapcharset = $db_charset;
				}
				$baidumap = '<?xml version="1.0" encoding="'.$mapcharset.'"?><document xmlns:bbs ="http://www.baidu.com/search/bbs_sitemap.xsd"><webSite>'.$_SERVER['SERVER_NAME'].'</webSite><webMaster>'.$pb_adminmail.'</webMaster><updatePeri>12</updatePeri><updatetime>'.gdate($timestamp, 'Y-m-d H:i').'</updatetime><version>PBDIGG'.$_PBENV['PB_VERSION'][0].'</version>';
				PWriteFile(PBDIGG_ROOT.'sitemap_baidu.xml', '', 'wb');
			}
			if ($google)
			{
				$googlemap = '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">';
				PWriteFile(PBDIGG_ROOT.'sitemap_google.xml', '', 'wb');
			}
		}
		while ($rs = $DB->db_fetch_array())
		{
			if ($baidu)
			{
				if (!file_exists(PBDIGG_ROOT.'sitemap_baidu.xml') || filesize(PBDIGG_ROOT.'sitemap_baidu.xml') < 10485760)
				{
					$baidumap .= '<item><link>'.$_PBENV['PB_URL'].'show.php?tid='.$rs['tid'].'</link><title>'.str_replace('&', '&amp;', $rs['subject']).'</title><pubDate>'.gdate($rs['postdate']).'</pubDate><bbs:lastDate>'.gdate($rs['commentdate']).'</bbs:lastDate></item>';
				}
			}
			if ($google)
			{
				$googlemap .= '<url><loc>'.$_PBENV['PB_URL'].'show.php?tid='.$rs['tid'].'</loc><lastmod>'.mapdate($rs['postdate']).'</lastmod><changefreq>daily</changefreq><priority>0.0</priority></url>';
			}
		}
		if ($pb_rewrite)
		{
			rewrite_Tran($baidumap);
			rewrite_Tran($googlemap);
		}
		if ($baidu)
		{
			PWriteFile(PBDIGG_ROOT.'sitemap_baidu.xml', $baidumap);
		}
		if ($google)
		{
			PWriteFile(PBDIGG_ROOT.'sitemap_google.xml', $googlemap);
		}
		redirect($plugin_lang['pbmap_mkprocess'], $jumpurl.'&amp;submitted=1&amp;sitemap='.$sitemap.'&amp;mapcate='.$mapcate.'&amp;page='.$page.'&amp;end=1');
	}
	elseif ($end)
	{
		if ($baidu)
		{
			PWriteFile(PBDIGG_ROOT.'sitemap_baidu.xml', '</document>');
		}
		if ($google)
		{
			PWriteFile(PBDIGG_ROOT.'sitemap_google.xml', '</urlset>');
		}
	}
	showMsg($plugin_lang['pbmap_mksuccess'], $jumpurl);
}
else
{
	$cate = array();
	foreach ($categories as $cid => $cvalue)
	{
		$cate[$cid] = $cvalue['name'];
	}
	$TPL->assign('mapcate', html_select($cate, 'mapcate[]', '', 'size="8" id="cate" multiple="multiple"'));
}
?>