<?php
define('THIS_SCRIPT', 'indivgroup_index');
require_once 'global.php';
global $userid,$membername;
$lang = load_lang($lang, 'indivgroup_index' );
$groupid= intval($_GET["groupid"]);
$groupboardid= intval($_GET["groupboardid"]);
$groupmaster= false;							//是否群组管理员

include_once INC_PATH.'DV_Encoding.class.php';
$objenc =& DV_Encoding::GetEncoding($charset);

if ($groupid > 0){
	$gettitle = $lang['template.strings4'];
}

if( $fsetting['leftbar'] == 1 && $targeturl == 'left' ){
	$query=$db->query("Select id boardid,boardname boardtype,parentid,ishidden From {$dv}board Order By id");
	head();
	require_once printout('left');
	footer(false);
	exit;
}elseif ( $fsetting['leftbar'] == 1 && $targeturl != '' ){
	$istoptable = 0;
	$targeturl = strtolower(urldecode($targeturl));
	head();
	require_once printout('main');
	footer(false);
	exit;
}
if($fsetting['indivgroup_show'] == 0)
{
	head();
	showmsg($lang['template.strings5']);
	footer();
}
$showboardlist=0;
$activetime = get_cookie("activetime");
if ($groupid==0){
	head();
	showmsg($lang['template.strings6']);
}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,groupface from {$dv}group_name where id={$groupid}")){
		if ($arr=$db->fetch_array($query)){
			$groupinfo=$arr;
			$groupinfo['groupinfo']= str_replace("\r","<br>",$groupinfo['groupinfo']);
		}
	}
	$db->free_result($query);

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

	if(!$master)
		if($groupinfo["stats"] == '0')			//检查群组状态
		{
			head();
			showmsg($lang['template.strings8']);
		}

	if(!($master || $groupmaster)){
		if($groupinfo["stats"] == '3')
		{
			head();
			showmsg($lang['template.strings9']);
		}
		if($groupinfo["viewflag"] == '1'){
			if($userid == '')
			{
				head();
				showmsg($lang['template.strings10']);
			}
			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.strings11']);
				}
			}
		}
	}

	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);

		$colorstr = array('','','red','blue','green');

		$query2=$db->query("Select /*!40001 SQL_CACHE */ topicid,groupid,boardid,title,postusername,postuserid,dateandtime,child,hits,lastpost,lastposttime,istop,isbest,locktopic,expression,mode,topicmode From {$dv}group_topic Where boardid<>-1 and groupid={$groupid} Order By lastposttime Desc Limit 10");

		$result = false;
		while( true ){
			$result = $db->fetch_array($query2);
			if( $result === false ){
				break;
			}

			$result['istop'] = 0;

			$result['title'] = ChkBadWords($result['title']);
			$result['locktopic'] = htmlspecialchars($result['title']);
			if( $result['topicmode'] == 0 ){
				$result['topicmode'] = '';
			}elseif($result['topicmode'] > 1){
				$result['topicmode'] = ' style="color:' .$colorstr[$result['topicmode']]. ';"';
			}elseif($result['topicmode'] == 1){//暂时关闭html
			}else{
				$result['topicmode'] = '';
			}
			if ( $objenc->StrLength($result['title']) > intval($board_settings[59]) ){
				$result['title'] = substr($result['title'],0,$board_settings[59]).'...';
			}

			if($board_settings[65] && $timestamp - $result['dateandtime'] < $board_settings[64] * 60) {
				$result['newsign'] = '<img src="'.$board_settings[65].'" />';
			} else {
				$result['newsign'] = '';
			}
			$_topics[] = $result;
		}
	}
	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}");

		$groupboardnum= mysql_num_rows($query);
		if ($groupboardnum==0)
		{
			head();
			showmsg($lang['template.strings12']);
		}
	}

	while($forum =& $db->fetch_array($query)) {
		$forum_topic += $forum['topicnum'];//主题数;
		$forum_posts += $forum['postnum'];//贴子数;
		$forum_today += $forum['todaynum'];
		if ($groupboardid==0){
			if ( $objenc->StrLength($forum['boardinfo']) > 20){
				$forum['boardinfo'] = $objenc->SubString(strip_tags($forum['boardinfo']), 0, 20).'...';
			}

			$_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']));
}

if(!empty($membername))
	$mygrouplink= '<a href="indivgroup_list.php?action=mygrouplist">'.$lang['template.strings2'].'</a>';
else
	$mygrouplink='';
head(1,0,0,$title_num,$mygrouplink);

$forum_today = $forum_topic = $forum_posts = 0;

	//最新加入用户
	$userids= '';
	$sql= "select /*!40001 SQL_CACHE */ userid from {$dv}group_user where groupid={$groupid} and islock<>0 order by id desc limit 12";
	if($query=$db->query($sql)){
		while ($arr=$db->fetch_array($query)){
			if($userids == '')
				$userids= $arr['userid'];
			else
				$userids.= ','.$arr['userid'];
		}
	}

	if($userids != ''){
		$sql= "select /*!40001 SQL_CACHE */ userid,username,userface,userphoto from {$dv}user where userid in({$userids})";
		if($query=$db->query($sql)){
			$user_list= array();
			while ($arr=$db->fetch_array($query)){
				if ( $objenc->StrLength($arr['username']) > 4){
					$arr['username'] = $objenc->SubString(strip_tags($arr['username']), 0, 4).'...';
				}
				$arrpic= explode("|",$arr['userface']);
				is_array($arrpic) AND $arr['userface']= $arrpic[count($arrpic)-1];
				$user_list[]=$arr;
			}
		}
	}

	$db->free_result($query);

if(($showboardlist == 1)){
	//管理员列表
	$sql= "select /*!40001 SQL_CACHE */ userid,username from {$dv}group_user where groupid={$groupid} and islock=2";
	if($query=$db->query($sql)){
		while ($arr=$db->fetch_array($query)){
			if ( $objenc->StrLength($arr['username']) > 4){
				$arr['username'] = $objenc->SubString(strip_tags($arr['username']), 0, 4).'...';
			}
			$admin_list[]=$arr;
		}
	}
	require_once printout('indivgroup_index');
}

if ($groupboardid > 0){
	$lang = load_lang($lang, 'indivgroup_index.list' );
	$colorstr = array('','','red','blue','green');

	if ($page == '' || !is_numeric($page) || $page <= 0){
		$page = 1;
	}

	if ($page == 1){
		$result1_hasmore = true;
		$query1=$db->query("Select /*!40001 SQL_CACHE */ topicid,groupid,boardid,title,postusername,postuserid,dateandtime,child,hits,lastpost,lastposttime,istop,isbest,locktopic,expression,mode,topicmode,topicfont From {$dv}group_topic Where istop>0 AND groupid={$groupid} AND boardid={$groupboardid} Order By lastposttime Desc");

		$boardtopcount = $db->num_rows($query1);
	}else{
		$boardtopcount = 0;
		$result1_hasmore = false;
	}
	if ($board_settings[60] <= 0) {
		$board_settings[60] = 30;
	}
	if ($page == 1){
		$query2=$db->query("Select /*!40001 SQL_CACHE */ topicid,groupid,boardid,title,postusername,postuserid,dateandtime,child,hits,lastpost,lastposttime,istop,isbest,locktopic,expression,mode,topicmode,topicfont From {$dv}group_topic Where istop=0 AND groupid={$groupid} And boardid={$groupboardid} Order By lastposttime Desc Limit ".$board_settings[60]);
	}else{
		$query2=$db->query("Select /*!40001 SQL_CACHE */ topicid,groupid,boardid,title,postusername,postuserid,dateandtime,child,hits,lastpost,lastposttime,istop,isbest,locktopic,expression,mode,topicmode,topicfont From {$dv}group_topic Where istop=0 AND groupid={$groupid} And boardid={$groupboardid} Order By lastposttime Desc",array('absolutePage'=>$page,'pageSize'=>$board_settings[60]));
	}
	$result = false;
	while( true ){
		if( $result1_hasmore === true && $page == 1 ){
			$result = $db->fetch_array($query1);
		}
		if( $result === false ){
			$result1_hasmore = false;
		}
		if( $result1_hasmore === false ){
			$result = $db->fetch_array($query2);
		}
		if( $result === false ){
			break;
		}
		if ($result['istop'] > 0){
			switch($result['istop']){
			case 1:
				$result['istop'] = 0;
				break;
			case 2:
				$result['istop'] = 1;
				break;
			case 3:
				$result['istop'] = 2;
				break;
			}
		}else{
			if (intval($result['isbest']) == 1){
				$result['istop'] = 3;
			}elseif($result['locktopic'] == 1){
				$result['istop'] = 4;
			}elseif($result['isvote'] == 1){
				$result['istop'] = 5;
			}elseif($result['child'] > $fsetting['hottopicreply']){
				$result['istop'] = 6;
			}else{
				$result['istop'] = 7;
			}
		}
		$result['title'] = ChkBadWords($result['title']);
		$result['locktopic'] = htmlspecialchars($result['title']);
		$result['topicmode']= ShowCodeTitle($result['topicmode'],$result['topicfont'],1);

		if ( $objenc->StrLength($result['title']) > $board_settings[59]){
			$result['title'] = $objenc->SubString(strip_tags($result['title']), 0, $board_settings[59]).'...';
		}

		if($board_settings[65] && $timestamp - $result['dateandtime'] < $board_settings[64] * 60) {
			$result['newsign'] = '<img src="'.$board_settings[65].'" />';
		} else {
			$result['newsign'] = '';
		}
		$_topics[] = $result;
	}

	//管理员列表
	$userids= '';
	$sql= "select /*!40001 SQL_CACHE */ userid from {$dv}group_user where groupid={$groupid} and islock=2";
	if($query=$db->query($sql)){
		while ($arr=$db->fetch_array($query)){
			if($userids == '')
				$userids= $arr['userid'];
			else
				$userids.= ','.$arr['userid'];
		}
	}

	if($userids != ''){
		$sql= "select /*!40001 SQL_CACHE */ userid,username,userface,userphoto from {$dv}user where userid in({$userids})";
		if($query=$db->query($sql)){
			$$admin_list= array();
			while ($arr=$db->fetch_array($query)){
				if ( $objenc->StrLength($arr['username']) > 4){
					$arr['username'] = $objenc->SubString(strip_tags($arr['username']), 0, 4).'...';
				}
				$arrpic= explode("|",$arr['userface']);
				is_array($arrpic) AND $arr['userface']= $arrpic[count($arrpic)-1];
				$admin_list[]=$arr;
			}
		}
	}

	require_once printout('indivgroup_index.list');
}

set_cookie('leftbarsign', '', -31536000);

footer();

?>