<?php
define('THIS_SCRIPT', 'indivgroup_dispbbs');
require_once 'global.php';
require_once INC_PATH.'DV_Encoding.class.php';
$lang = load_lang($lang, 'indivgroup_dispbbs' );
if($fsetting['indivgroup_show'] == 0)
{
	head();
	showmsg($lang['template.strings33']);
	footer();
}
$showboardlist=0;
$activetime = get_cookie("activetime");
if ($groupid==0){
	head();
	showmsg($lang['template.strings34']);
	exit;
}else{
	$groupinfo= array();
	if($query=$db->query("select id,groupname,groupinfo,appuserid,appusername,usernum,stats,postnum,topicnum,todaynum,yesterdaynum,limituser,appdate,passdate,visidate,locked,viewflag from {$dv}group_name where id={$groupid}")){
		if ($arr=$db->fetch_array($query)){
			$groupinfo=$arr;
		}
	}
	$db->free_result($query);

	if(count($groupinfo)==0){
		head();
		showmsg($lang['template.strings35']);
		exit;
	}
	else{
		$groupmasterStr= $arr["appusername"];
		if(($groupmasterStr == $membername) && ($membername != ""))
		{
			$groupmaster= true;
		}

		$userresult=$db->query("select username,islock from {$dv}group_user where islock=2 and groupid=".$groupid);	
		while ($userarr=$db->fetch_array($userresult)){
			if((trim($userarr["username"]) != "") && (trim($userarr["username"]) != $arr["appusername"])){
				$groupmasterStr.= "|".trim($userarr["username"]);
				if(($userarr["username"] == $membername) && ($membername != ""))
				{
					$groupmaster= true;
				}
			}
		}
		$db->free_result($userresult);
	}

	if(!$master)
	{
		if($groupinfo["stats"] == '0'){			//检查群组状态
			head();
			showmsg($lang['Error.strings36']);
			exit;
		}
	}
	else
		$groupmaster= true;

	if(!($master || $groupmaster)){
		if($groupinfo["stats"] == '3'){
			head();
			showmsg($lang['template.strings37']);
			exit;
		}
		if($groupinfo["viewflag"] == '1'){
			if($userid == ''){
				head();
				showmsg($lang['template.strings38']);
				exit;
			}
			else{
				$usernum=intval($db->scalar("select count(1) from {$dv}group_user where userid={$userid} and groupid={$groupid}"));
				if($usernum==0){
					head();
					showmsg($lang['template.strings39']);
					exit;
				}
			}
		}
	}

	if($groupboardid == 0){
		$query=$db->query("select id boardid,rootid,boardname boardtype,boardinfo,indeximg,postnum,topicnum,todaynum,lastpost,boardstats,rules from {$dv}group_board where rootid={$groupid} order by id");
		$showboardlist = 1;

		$groupboardnum= mysql_num_rows($query);
	}
	else{
		$query=$db->query("select id boardid,rootid,boardname boardtype,boardinfo,indeximg,postnum,topicnum,todaynum,lastpost,boardstats,rules from {$dv}group_board where rootid={$groupid} and id={$groupboardid}");
		$showboardlist = 1;

		$groupboardnum= mysql_num_rows($query);
		if ($groupboardnum==0){
			head();
			showmsg($lang['tpl.str216']);
			exit;
		}
	}

	while($forum =& $db->fetch_array($query)) {
		$forum_topic += $forum['topicnum'];//主题数;
		$forum_posts += $forum['postnum'];//贴子数;
		$forum_today += $forum['todaynum'];
		if ($groupboardid==0)
			$_forum[] =& $forum;
		else
			$_forum =& $forum;
	}
}
global $title_num;

if($groupboardid > 0){
	$title_num = array(0=>array('linktitle'=>$lang['template.strings0'],'linkurl'=>'indivgroup.php'),1=>array('linktitle'=>$groupinfo['groupname'],'linkurl'=>'indivgroup_index.php?groupid='.$groupinfo['id']),2=>array('linktitle'=>$_forum['boardtype'],'linkurl'=>'indivgroup_index.php?groupid='.$groupinfo['id'].'&groupboardid='.$_forum['boardid']),3=>array('linktitle'=>$lang['template.strings3']));
}
else{
	$title_num = array(0=>array('linktitle'=>$lang['template.strings0'],'linkurl'=>'indivgroup.php'),1=>array('linktitle'=>$groupinfo['groupname'],'linkurl'=>'indivgroup_index.php?groupid='.$groupinfo['id']),2=>array('linktitle'=>$lang['template.strings1']));
}
global $mygrouplink;
if(!empty($membername)){
	$mygrouplink= '<a href="indivgroup_list.php?action=mygrouplist">'.$lang['template.strings2'].'</a>';
}
else{
	$mygrouplink='';
}

head(1,0,0,$title_num);

$stats = $lang['HeaderTitle'];
if( $groupsetting[2] == 0 ){
	$gettitle = str_replace('{$repl}', $_forum['boardtype'], $lang['Error.strings1']);
	head();
	showmsg($lang['Error.strings31']);
	exit;
}
if( !is_numeric($id) ){
	$id = 0;
}
if (!is_numeric($replyid)) {
	$replyid = 0;
}
if (!is_numeric($announceid)) {
	$announceid = 0;
}
if (!is_numeric($rootid)) {
	$rootid = 0;
}
$AnnounceID = $id;
if( $AnnounceID <= 0 ){
	$gettitle = str_replace('{$repl}',$_forum['boardtype'],$lang['Error.strings1']);
	head();
	showmsg($lang['Error.strings30']);
	exit;
}
if( !is_numeric($replyid) ) $replyid = 0;
if( $replyid <= 0 ) $replyid = $AnnounceID;
if( !is_numeric($star) ){
	$star = 1;
}
if( !is_numeric($skin) ){
	$skin = intval($board_settings[83]);
}
global $plus_index_value;
if(intval($plus_index_value['beplusvalue']['isuse'])==1){////edit 2007-04-25
	honortitle();
}
$SQL_CACHE = ' /*!40001 SQL_CACHE */ ';
//$SQL_CACHE = ' /*!40001 SQL_NO_CACHE */ ';
if( $query = $db->query("SELECT {$SQL_CACHE} CASE WHEN topicmode<>1 THEN REPLACE(title,'<','&lt;') ELSE title END AS title,istop,isbest,postusername,postuserid,hits,child,pollid,locktopic,groupid,boardid,topicmode,topicfont,dateandtime,expression,topicid,topicid AS replyid,topicid AS star,topicid AS page,topicid AS skin,@PostUserIDList:=NULL FROM {$dv}group_topic WHERE topicid={$AnnounceID} AND boardid={$groupboardid}",array('pageSize'=>1)) ){
	$TopicInfo =& $db->fetch_array($query);
}
if( empty($TopicInfo) ){
	$gettitle = strip_tags(str_replace('{$repl}',$_forum['boardtype'],$lang['Error.strings1']));
	head();
	showmsg($lang['Error.strings32']);
	exit;
} else{
	$db->free_result($query);
	$TotalUseTable = "{$dv}group_bbs1";;
	$T_GetMoneyType = $TopicInfo['getmoneytype'];
	$db->query("UPDATE {$dv}group_topic SET hits=(CASE WHEN hits IS NULL THEN 1 ELSE hits+1 END) {$setStmt} WHERE topicid={$AnnounceID}");
	$TopicInfo['title'] = ChkBadWords($TopicInfo['title']);
}

$CanReply = false;
if( $userid <= 0 && $groupsetting[3] == 0 ){
	$CanReply = false;
}
elseif($master === true||$groupmaster===true){
	$CanReply = true;
}
elseif($_forum['boardstats']==1&&$TopicInfo['locktopic']==0){
	$isUser= intval($db->scalar("select count(1) from {$dv}group_user where groupid={$groupid} and islock<>0 and userid={$userid}"));
	if($isUser == 1)
		$CanReply = true;
}

//********************************
$gettitle = $TopicInfo['title'];
if ($gettitle != '') {
	$objenc =& DV_Encoding::GetEncoding($charset);
	$_topictitlelength = $objenc->StrLength($gettitle);
	if ($_topictitlelength > 100) {
		$gettitle = '<span title="'.strip_tags($gettitle).'">'.$objenc->SubString(strip_tags($gettitle), 0, 35).'</span>...';
	}
}
$TopicCount = $TopicInfo['child'] + 1;
$pagesize = $board_settings[61];
if ($pagesize < 1) {
	$pagesize = 30;
}
$PageCount = ceil( (float)$TopicCount / (float)$pagesize );
$star = (is_numeric($star)?$star:1);
$page = (is_numeric($page)?$page:1);
//if( $page > 0 ) $star = $page;
if( $star > $PageCount ) $star = $PageCount;
if( $star < 1 ) $star = 1;
require_once CACHE_PATH.'forum_pic.php';
$empath = $_DCACHE['forum_emot'][0];
//Start 过滤标题HTML
if( $TopicInfo['topicmode'] != 1 ){
	$topicStmt = "topic";
} else{
	$topicStmt = "topic";
}
//End 过滤标题HTML
if( $skin == 1 ){
	if( $replyid == $AnnounceID ){
		$stmt = "SELECT {$SQL_CACHE} announceid,groupid,boardid,username,{$topicStmt},dateandtime,body,expression,ip,rootid,signflag,isbest,postuserid,layer,isagree,isupload,ubblist,locktopic,parentid,@PostUserIDList:=COALESCE(CONCAT(@PostUserIDList,',',postuserid),postuserid) AS postuseridlist FROM {$dv}group_bbs1 WHERE boardid={$groupboardid} AND rootid={$AnnounceID}";
	} else{
		$pagesize = 1;
		$istar = $star;
		$star = 1;
		$stmt = "SELECT {$SQL_CACHE} announceid,groupid,boardid,username,{$topicStmt},dateandtime,body,expression,ip,rootid,signflag,isbest,postuserid,layer,isagree,isupload,ubblist,locktopic,parentid,@PostUserIDList:=COALESCE(CONCAT(@PostUserIDList,',',postuserid),postuserid) AS postuseridlist FROM {$dv}group_bbs1 WHERE announceid={$replyid} AND boardid={$groupboardid}";
	}
} else{
	$stmt = "SELECT {$SQL_CACHE} announceid,groupid,boardid,username,{$topicStmt},dateandtime,body,expression,ip,rootid,signflag,isbest,postuserid,layer,isagree,isupload,ubblist,locktopic,parentid,@PostUserIDList:=COALESCE(CONCAT(@PostUserIDList,',',postuserid),postuserid) AS postuseridlist FROM {$dv}group_bbs1 WHERE boardid={$groupboardid} AND rootid={$AnnounceID} ORDER BY dateandtime ASC, announceid ASC";
}
$NumRows = 0;
if( $query = $db->query($stmt,array('absolutePage'=>$star,'pageSize'=>$pagesize)) ){
	$postResultCount = 0;
	$refPostResult = array();
	$NumRows = $db->num_rows($query);
	while( $result =& $db->fetch_array($query) ){
		//过滤脏字
		$result['username'] = ChkBadWords($result['username']);
		$result['topic'] = ChkBadWords($result['topic']);
		$result['body'] = ChkBadWords($result['body']);
		$_PostResults[$postResultCount] =& $result;
		$refPostResult["{$result[postuserid]}"][] =& $result;
		$postResultCount++;
	}
	$db->free_result($query);
}
if( !empty($_PostResults) ){
	$nameglow = array( $_DCACHE['stylecss'][43] ,$_DCACHE['stylecss'][41] ,$_DCACHE['stylecss'][41] ,$_DCACHE['stylecss'][39] ,null ,null  ,$_DCACHE['stylecss'][40] );
	$TopicInfo['replyid'] = $_PostResults[0]['announceid'];
	$TopicInfo['star'] = $star;
	$TopicInfo['page'] = $page;
	$TopicInfo['skin'] = $skin;
	//如果顶楼是主题帖，并且是购买帖的时候，提取购买信息
		//主题金币帖子类型0=无，1=赠送金币帖，2=获赠金币帖，3=金币购买帖，4=回复赠送金币（回复表），5=已结帖（主题表）
	if( $_PostResults[0]['parentid'] == 0 && $TopicInfo['getmoneytype'] == 3 ){
		$_PostResults[0]['postbuyuser'] = trim($_PostResults[0]['postbuyuser']);
		$postbuyinfo = array( 'money' => 0 ,'maxbuy' => 0 ,'notvipbuy' => '' ,'buyuser' => '' );
		if ($_PostResults[0]['postbuyuser'] !== '') {
			$postbuyusers = explode('|||', $_PostResults[0]['postbuyuser']);
			//postbuyinfo[0] 收入 postbuyinfo[1] 购买限制 postbuyinfo[2] vip是否需要购买 postbuyinfo[3] 允许购买用户列表
			$postbuyinfo = explode('@@@',$postbuyusers[0]);
			if( count($postbuyinfo) > 3 ){
				$postbuyinfo = array( 'money' => $postbuyinfo[0] ,'maxbuy' => $postbuyinfo[1] ,'notvipbuy' => $postbuyinfo[2] ,'buyuser' => ",{$postbuyinfo[3]}," );
			}
		}
	}
	//发帖用户ID列表
	if( $postResultCount > 0 ) $postuseridlist = $_PostResults[$postResultCount-1]['postuseridlist'];
	if(intval($plus_index_value['beplusvalue']['isuse'])==1){//edit 2007-04-25
		$query_sql="SELECT {$SQL_CACHE} u.userid,u.username,u.useremail,u.userim,u.usermobile,u.usersign,u.userclass,u.usertitle,u.userwidth,u.userheight,u.userpost,u.userface,u.joindate,u.userwealth,u.userep,u.usercp,u.userbirthday,u.usersex,u.usergroup,u.lockuser,u.userpower,u.titlepic,CASE WHEN u.usergroupid IS NULL OR u.usergroupid<=0 THEN 7 ELSE u.usergroupid END AS usergroupid,u.lastlogin,u.userhidden,u.ischallenge,u.usermoney,u.userticket,u.useravasetting,u.userisava,u.userhonor FROM {$dv}user u WHERE u.userid IN ({$postuseridlist})";
	}
	else{
		$query_sql="SELECT {$SQL_CACHE} u.userid,u.username,u.useremail,u.userim,u.usermobile,u.usersign,u.userclass,u.usertitle,u.userwidth,u.userheight,u.userpost,u.userface,u.joindate,u.userwealth,u.userep,u.usercp,u.userbirthday,u.usersex,u.usergroup,u.lockuser,u.userpower,u.titlepic,CASE WHEN u.usergroupid IS NULL OR u.usergroupid<=0 THEN 7 ELSE u.usergroupid END AS usergroupid,u.lastlogin,u.userhidden,u.ischallenge,u.usermoney,u.userticket,u.useravasetting,u.userisava FROM {$dv}user u WHERE u.userid IN ({$postuseridlist})";
	}
	if( !empty($postuseridlist) ){
		if( $query = $db->query($query_sql) ){
			$forumad_topic = intval($fsetting['forumad_topic']);
			include_once INC_PATH.'dv_code.php';
			$nowtimes = TIME_NOW;
			while( $result =& $db->fetch_array($query) ){
				if (2 == $result['userhidden']) {
					if (is_numeric($result['lastlogin']) AND $result['lastlogin'] > 0) {
						if (dateDiff('s', $result['lastlogin'], $nowtimes) > $fsetting['onlinetimeout'] * 60) {
							$result['userhidden'] = 1;
						}
					} else {
						$result['userhidden'] = 1;
					}
				} else {
					$result['userhidden'] = 1;
				}
				$tmpRef =& $refPostResult["{$result[userid]}"];
				foreach( $tmpRef AS $key=>$val ){
					$result['current_replyid'] = $result['current_announceid'] = $tmpRef[$key]['announceid'];
					$result['current_rootid'] = $tmpRef[$key]['rootid'];
					$result['posttable'] = $TopicInfo['posttable'];
					$result['postbuyuser'] = $tmpRef[$key]['postbuyuser'];
					//Ubb转换
					if( strpos($tmpRef[$key]['ubblist'],',39,') !== false ){
						$tmpRef[$key]['body'] = Dv_CodeProcess($tmpRef[$key]['body'],$result,$tmpRef[$key]['ubblist'],1,0);
					} else{
						$tmpRef[$key]['body'] = Dv_CodeProcess($tmpRef[$key]['body'],$result,$tmpRef[$key]['ubblist'],1,1);
					}
					//利用ubblist节点传送广告数据
					if( $forumad_topic === 1 ){
						$tmpRef[$key]['ubblist'] = Topic_Ads();
					} else{
						$tmpRef[$key]['ubblist'] = '';
					}
				}
				$postUserResult["{$result['userid']}"] =& $result;
			}
		}
	}
}

if( $GLOBALS['CanReply'] ){
	$userinfo['canreply'] = true;
}
if( $board_settings[58] == 1 ){
	$userinfo['cananony'] = 1;
}

require printout('indivgroup_dispbbs');
//***************************************

footer();

?>