<?php
define('THIS_SCRIPT', 'rssfeed');
require_once 'global.php';
$lang = load_lang($lang, 'rssfeed' );

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

if( $rssdatamode == '' OR !is_numeric($rssdatamode) ){
	$rssdatamode = 0;
}
$rsshomepageurl = getScriptUrl();
if ($rssdatamode != 0) {
	require_once CACHE_PATH.'forum_pic.php';
	$empath = $_DCACHE['forum_emot'][0];
}
if ($rssid == '' OR !is_numeric($rssid)) {
	$rssid = 0;
}

$pagesize = 20;
$stmt = 'SELECT /*!40001 SQL_CACHE */ topicid,title,postusername,postuserid,dateandtime,boardid,posttable,getmoneyType,hidename ';
$datenow = gmdate('Y-m-d H:i:s');
require_once CACHE_PATH.'board/board_all.php';
switch ($rssid) {
	case 0:
		$rsstitle = $lang['tpl.rsstitle']['default'];
		$TPL_msginfo_loopstr = '';
		if (!empty($_DCACHE['all_forums']) AND is_array($_DCACHE['all_forums'])) {
			foreach ($_DCACHE['all_forums'] AS $currboardid => $currboardinfo) {
				$TPL_msginfo_loopstr .= str_replace(array('{$TPL_boardtype}', '{$TPL_RssHomePageUrl}', '{$TPL_boardid}'), array($currboardinfo['boardtype'], $rsshomepageurl, $currboardid), $lang['tpl.msginfo_0.loopstr']);
			}
		}
		$msginfo = str_replace(array('{$TPL_RssHomePageUrl}', '{$TPL_msginfo_loopstr}'), array($rsshomepageurl, $TPL_msginfo_loopstr), $lang['tpl.msginfo_0']);
		$TPL_items .= <<<EOT
		<item>
			<title>{$fsetting['forumname']}--{$lang['tpl.rsstitle']['default']}</title>
			<link>{$fsetting['forumurl']}</link>
			<author>{$fsetting['forumname']}</author>
			<pubDate>{$datenow}</pubDate>
			<description><![CDATA[{$msginfo}]]></description>
		</item>
EOT;
		break;
	case 1:
		$rsstitle = $lang['tpl.rsstitle']['1'];
		$stmt .= " FROM {$dv}topic /*!32312 USE INDEX(rssindex) */ WHERE boardid<>444 AND boardid<>777 ORDER BY dateandtime DESC";
		break;
	case 2:
		$rsstitle = $lang['tpl.rsstitle']['2'];
		if ($boardid <= 0) {
			$stmt .= " FROM {$dv}topic /*!32312 USE INDEX(rssindex) */ WHERE boardid<>4444 AND boardid<>777 AND isbest='1' ORDER BY dateandtime DESC";
		} else {
			$stmt .= " FROM {$dv}topic /*!32312 USE INDEX(rssindex) */ WHERE '{$boardid}'<>4444 AND '{$boardid}'<>777 AND boardid='{$boardid}' AND isbest='1' ORDER BY dateandtime DESC";
		}
		break;
	case 3:
		$rsstitle = $lang['tpl.rsstitle']['3'];
		$time1 = dv_gmtimestamp(gmdate('Y-m-d 00:00:00'));
		$time2 = $time1 + 24*3600;
		$stmt .= " FROM {$dv}topic /*!32312 USE INDEX(rssindex) */ WHERE (dateandtime>=".$time1." AND dateandtime<".$time2.") AND (boardid<>444 AND boardid<>777) ORDER BY hits DESC";
		break;
	case 4:
		$rsstitle = $lang['tpl.rsstitle']['4'];
		if ($boardid <= 0) {
			$stmt .= " FROM {$dv}topic /*!32312 USE INDEX(rssindex) */ WHERE boardid<>444 AND boardid<>777 ORDER BY dateandtime DESC";
		} else {
			$stmt .= " FROM {$dv}topic /*!32312 USE INDEX(rssindex) */ WHERE boardid='{$boardid}' ORDER BY dateandtime DESC";
		}
		break;
	case 5:
		$rsstitle = $lang['tpl.rsstitle']['5'];
		if ($boardid <= 0) {
			$stmt .= " FROM {$dv}topic /*!32312 USE INDEX(rssindex) */ WHERE boardid<>4444 AND boardid<>777 AND isbest='1' ORDER BY dateandtime DESC";
		} else {
			$stmt .= " FROM {$dv}topic /*!32312 USE INDEX(rssindex) */ WHERE '{$boardid}'<>4444 AND '{$boardid}'<>777 AND boardid='{$boardid}' AND isbest='1' ORDER BY dateandtime DESC";
		}
		break;
	case 6:
		$time1 = dv_gmtimestamp(gmdate('Y-m-d 00:00:00'));
		$time2 = $time1 + 24*3600;
		if ($boardid <= 0) {
			$rsstitle = $lang['tpl.rsstitle']['6_1'];
			$stmt .= " FROM {$dv}topic /*!32312 USE INDEX(rssindex) */ WHERE boardid<>444 AND boardid<>777 AND (dateandtime>={$time1} AND dateandtime<{$time2}) ORDER BY hits DESC";
		} else {
			$rsstitle = $lang['tpl.rsstitle']['6_2'];
			$stmt .= " FROM {$dv}topic /*!32312 USE INDEX(rssindex) */ WHERE (dateandtime>={$time1} AND dateandtime<{$time2}) AND ('{$boardid}'<>444 AND '{$boardid}'<>777 AND boardid='{$boardid}') ORDER BY hits DESC";
		}
		break;
	case 7:
		if ($userid <= 0) {
			$rsstitle = $lang['tpl.rsstitle']['7_1'];
		} else {
			$rsstitle = $lang['tpl.rsstitle']['7_2'];
		}
		break;
	case 8:
		break;
	case 9:
		break;
	default:
		$rsstitle = $lang['tpl.rsstitle']['default'];
		break;
}
if ($rssid == 0 OR $rssid == 7) {
} elseif (!empty($stmt)) {
	$dbinfo =& $db->getResultSet($stmt, array('pageSize'=>$pagesize));
	if ($dbinfo === false) {
		$msginfo = str_replace(']]>', ']]&gt;', $lang['tpl.str0']);
		$TPL_items .= <<<EOT
		<item>
			<title>{$msginfo}</title>
			<link>{$fsetting['forumurl']}</link>
			<author>{$fsetting['forumname']}</author>
			<pubDate>{$datenow}</pubDate>
			<description><![CDATA[{$msginfo}]]></description>
		</item>
EOT;
	} else {
		require_once INC_PATH.'dv_code.php';
		foreach ($dbinfo AS $topicinfo) {
			if ($rssid == 5) {
				$currlink = "{$rsshomepageurl}dispbbs.php?boardid={$topicinfo['boardid']}&amp;id={$topicinfo['topicid']}&amp;page=1&amp;replyid={$topicinfo['posttable']}&amp;skin=1";
			} else {
				$currlink = "{$rsshomepageurl}dispbbs.php?boardid={$topicinfo['boardid']}&amp;id={$topicinfo['topicid']}&amp;page=1";
			}
			if ($boardid != 0) {
				if ($topicinfo['hidename'] == 1 AND $board_settings[58] == 1 AND $boardmaster === false) {
					$currauthor = $lang['tpl.str'];
				} else {
					$currauthor = $topicinfo['postusername'];
				}
			} else {
				if ($topicinfo['hidename'] == 1 AND ($master === false OR $superboardmaster === false)) {
					if (board_setting68($topicinfo['boardid']) == 1) {
						$currauthor = $lang['tpl.str'];
					} else {
						$currauthor = $topicinfo['postusername'];
					}
				} else {
					$currauthor = $topicinfo['postusername'];
				}
			}
			if ($rssdatamode == 0) {
				$currmsginfo = $lang['tpl.str2'];
			} else {
				if ($topicinfo['getmoneytype'] == 3) {
					$currmsginfo = $lang['tpl.str3'];
				} else {
					if ($query = $db->query("SELECT t.body,t.ubblist,u.lockuser,u.usergroupid,t.isbest,t.boardid FROM {$topicinfo['posttable']} AS t INNER JOIN {$dv}user AS u ON t.postuserid=u.userid WHERE t.rootid='{$topicinfo['topicid']}' AND t.boardid<>444 AND t.boardid<>777 ORDER BY t.announceid DESC", array('pageSize'=>1))) {
						if ($postinfo = $db->fetch_array($query)) {
							$db->free_result($query);
							if ($boardid != 0) {
								if ($postinfo['lockuser'] == 0) {
									if (intval($postinfo['isbest']) == 0 OR $groupsetting[41] == 1) {
										$ubblist = $postinfo['ubblist'];
										$currmsginfo =& Dv_CodeProcess($postinfo['body'], $postinfo, $ubblist, 2, 0);
									} else {
										$currmsginfo = $lang['tpl.str5'];
									}
								} else {
									$currmsginfo = $lang['tpl.str6'];
								}
							} else {
								if (GetSetting($postinfo['boardid']) === true) {
									if ($postinfo['lockuser'] == 0) {
										if (intval($postinfo['isbest']) == 0 OR $groupsetting[41] == 1) {
											$ubblist = $postinfo['ubblist'];
											$currmsginfo =& Dv_CodeProcess($postinfo['body'], $postinfo, $ubblist, 2, 0);
										} else {
											$currmsginfo = $lang['tpl.str5'];
										}
									} else {
										$currmsginfo = $lang['tpl.str6'];
									}
								} else {
									$currmsginfo = $lang['tpl.str7'];
								}
							}
						} else {
							$currmsginfo = $lang['tpl.str4'];
						}
					}
				}
			}
			$currmsginfo = str_replace(']]>', ']]&gt;', $currmsginfo);
			$topicinfo['dateandtime'] = gmdate('Y-m-d H:i:s', $topicinfo['dateandtime']);
			$TPL_items .= <<<EOT
		<item>
			<title>{$topicinfo['title']}</title>
			<link>{$currlink}</link>
			<author>{$currauthor}</author>
			<pubDate>{$topicinfo['dateandtime']}</pubDate>
			<description><![CDATA[{$currmsginfo}]]></description>
		</item>
EOT;
		}
	}
	@$db->close();
} else {
	exit;
}

if ($rssdatamode != 0) {
	$rsstitle .= $lang['tpl.rsstitle.postfix'];
}
$rsstitle = $fsetting['forumname'].'--'.$rsstitle;
$tmpstr1 = '<?';
$tmpstr2 = '?>';
print <<<EOT
{$tmpstr1}xml version="1.0" encoding="{$charset}" {$tmpstr2}
<rss version="2.0">
	<channel>
		<title>{$rsstitle}</title>
		<link>{$fsetting['forumurl']}</link>
		<language>{$lang['tpl.rsslanguage']}></language>
		<description>{$fsetting['forumname']}</description>
		<copyright>{$fsetting['homeurl']}</copyright>
		<generator>{$lang['tpl.rssgenerator']}</generator>
		<webMaster>{$fsetting['mastermail']}</webMaster>
{$TPL_items}
	</channel>
</rss>
EOT;

ob_end_flush();

function board_setting68($argboardid)
{
	global $_DCACHE;
	static $boardsetting = null;
	if (empty($boardsetting)) {
		$boardsetid = $_DCACHE['all_forums']["{$argboardid}"]['setting_id'];
		$boardsetting = $_DCACHE['forum_settings']["{$boardsetid}"]['setting'];
	}
	return $boardsetting[58];
}

function GetSetting($argboardid)
{
	global $dv, $db, $_DCACHE, $usergroupid, $userinfo, $userid, $boardid, $userpermission, $groupsetting, $founduserper, $membername;
	if ($argboardid <= 0) {
		return false;
	}
	static $arrayboardinfo = array();
	$boardinfo = null;
	if (empty($arrayboardinfo[$argboardid])) {
		$arrayboardinfo[$argboardid] =& $_DCACHE['all_forums'][$argboardid];
		if (empty($arrayboardinfo[$argboardid])) {
			if ($query = $db->query("SELECT boardid,boarduser,board_ads,board_user,isgroupsetting,rootid,sid,cid,rules,ishidden,ischeckout FROM {$dv}board WHERE boardid='{$argboardid}'", array('pageSize'=>1))) {
				if ($arrayboardinfo[$argboardid] = $db->fetch_array($query)) {
				} else {
					return false;
				}
			}
		}
	}
	$boardinfo =& $arrayboardinfo[$argboardid];
	$boardsetid = $_DCACHE['all_forums']["{$argboardid}"]['setting_id'];
	$PART_boardsetting = $_DCACHE['forum_settings']["{$boardsetid}"]['setting'];
	$isgroupsetting = $boardinfo['isgroupsetting'].'';
	$boarduser = $boardinfo['boarduser'].'';
	$ishidden = $boardinfo['ishidden'];
	$ischeckout = $boardinfo['ischeckout'];
	if ($isgroupsetting !== '') {
		$isgroupsetting = ",{$isgroupsetting},";
		if (strpos($isgroupsetting,",{$usergroupid},") !== false) {
			static $psetting = null;
			if ($psetting === null) {
				$psetting = $db->scalar("SELECT psetting FROM {$dv}boardpermission WHERE boardid='{$boardid}' AND groupid='{$usergroupid}'");
			}
			if (!empty($psetting)) {
				$PART_groupsetting = explode(',',$psetting);
			}
		}
		if ($userid > 0 AND strpos($isgroupsetting, ',0,') !== false) {
			static $ucsetting = null;
			if ($ucsetting === null) {
				$ucsetting = $db->scalar("SELECT uc_setting FROM {$dv}useraccess WHERE uc_boardid='{$boardid}' AND uc_userid='{$userid}'");
			}
			if (!empty($ucsetting)) {
				$groupsetting = $userpermission = explode(',',$ucsetting);
				$founduserper = true;
			}
		}
	}
	if ($ishidden == 1 AND $groupsetting[37] == 0) {
		return false;
	}
	if ($groupsetting[0] == 0) {
		//showmsg('您没有权限进入本论坛！');
		return false;
	}
	//访问论坛限制(包括文章、积分、金钱、魅力、威望、精华、被删数、注册时间)
	//Start 文章处理部分
	if (trim($PART_boardsetting[30] !== '0' AND is_numeric($PART_boardsetting[30]))) {
		if ($userid == 0) {
			return false;
		}
		if ($userinfo['userpost'] < $PART_boardsetting[30]) {
			return false;
		}
	}
	//End 文章处理部分
	//Start 积分处理部分
	if (trim($PART_boardsetting[31] !== '0' AND is_numeric($PART_boardsetting[31]))) {
		if ($userid == 0) {
			return false;
		}
		if ($userinfo['userep'] < $PART_boardsetting[31]) {
			return false;
		}
	}
	//End 积分处理部分
	//Start 金钱处理部分
	if (trim($PART_boardsetting[32] !== '0' AND is_numeric($PART_boardsetting[32]))) {
		if ($userid == 0) {
			return false;
		}
		if ($userinfo['userwealth'] < $PART_boardsetting[32]) {
			return false;
		}
	}
	//End 金钱处理部分
	//Start 魅力处理部分
	if (trim($PART_boardsetting[33] !== '0' AND is_numeric($PART_boardsetting[33]))) {
		if ($userid == 0) {
			return false;
		}
		if ($userinfo['usercp'] < $PART_boardsetting[33]) {
			return false;
		}
	}
	//End 魅力处理部分
	//Start 威望处理部分
	if (trim($PART_boardsetting[34] !== '0' AND is_numeric($PART_boardsetting[34]))) {
		if ($userid == 0) {
			return false;
		}
		if ($userinfo['userpower'] < $PART_boardsetting[34]) {
			return false;
		}
	}
	//End 威望处理部分
	//Start 精华处理部分
	if (trim($PART_boardsetting[35] !== '0' AND is_numeric($PART_boardsetting[35]))) {
		if ($userid == 0) {
			return false;
		}
		if ($userinfo['userisbest'] < $PART_boardsetting[35]) {
			return false;
		}
	}
	//End 精华处理部分
	//Start 删帖处理部分
	if (trim($PART_boardsetting[36] !== '0' AND is_numeric($PART_boardsetting[36]))) {
		if ($userid == 0) {
			return false;
		}
		if ($userinfo['userdel'] > $PART_boardsetting[36]) {
			return false;
		}
	}
	//End 删帖处理部分
	//Start 注册时间处理部分
	if (trim($PART_boardsetting[37] !== '0' AND is_numeric($PART_boardsetting[37]))) {
		if ($userid == 0) {
			return false;
		}
		if (time() - $userinfo['joindate'] < $PART_boardsetting[37] * 60) {
			return false;
		}
	}
	//End 注册时间处理部分
	if (intval($ischeckout) == 1) {//认证版块判断 $PART_boardsetting[2]
		if ($userid <= 0) {
			return false;
		} else {
			if (!is_array($boarduser) OR count($boarduser) <= 0) {
				return false;
			} else {
				foreach ($boarduser AS $val) {
					if (trim(strtolower($val)) == trim(strtolower($membername))) {
						return true;
					}
				}
			}
		}
	}
	return true;
}

?>