<?php
define('THIS_SCRIPT', 'indivgroup_post');
require_once 'global.php';
$lang = load_lang($lang, 'indivgroup_post' );
if (!is_numeric($replyid)) {
	$replyid = 0;
}
if (!is_numeric($id)) {
	$id = 0;
}
if (!is_numeric($rootid)) {
	$rootid = 0;
}
if (!is_numeric($announceid)) {
	$announceid = 0;
}
if($fsetting['indivgroup_show'] == 0)
{
	head();
	showmsg($lang['tpl.str232']);
	footer();
}

global $userid,$membername;
global $title_num;




//检查发贴权限start
$groupid= intval($_GET["groupid"]);
$groupboardid= intval($_GET["groupboardid"]);
$groupmaster= false;							//是否群组管理员
if($userid <= 0)
	showmsg($lang['tpl.str233']);
if($fsetting['indivgroup_show'] == '0')
	showmsg($lang['tpl.str234']);

if (($groupid==0) || ($groupboardid == 0)){
	showmsg($lang['tpl.str235']);
}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)
		showmsg($lang['tpl.str236']);
	else{
		$groupmasterStr= $arr["appusername"];
		if(($groupmasterStr == $membername) && ($membername != ""))
			$groupmaster= true;
		$userresult=$db->query("select username 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')			//检查群组状态
			showmsg($lang['tpl.str237']);

	if(!($master || $groupmaster)){
		if($groupinfo["stats"] == '3')
			showmsg($lang['tpl.str238']);
		if($groupinfo["stats"] == '2')
			showmsg($lang['tpl.str239']);
		if($groupinfo["viewflag"] == '1'){
			if($userid == '')
				showmsg($lang['tpl.str240']);
			else{
				$usernum=intval($db->scalar("select count(1) from {$dv}group_user where userid={$userid} and groupid={$groupid} and islock<>0"));
				if($usernum==0)
					showmsg($lang['tpl.str241']);
			}
		}
	}


	$query=$db->query("select id boardid,rootid,boardname,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)
		showmsg($lang['tpl.str242']);
	else{
		$arr=$db->fetch_array($query);
		$groupboardinfo=$arr;
		if($groupboardinfo["boardstats"] == '0')
			showmsg($lang['tpl.str243']);
	}
	$db->free_result($query);
}
$isUser= intval($db->scalar("select count(1) from {$dv}group_user where groupid={$groupid} and islock<>0 and userid={$userid}"));
if(!$master)
	if($isUser == 0)
		showmsg($lang['tpl.str244']);
//检查发贴权限end


$signflag = 1;
$mailflag = 0;

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'=>$arr['boardtype'],'linkurl'=>'indivgroup_index.php?groupid='.$groupinfo['id'].'&groupboardid='.$arr['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);

PageLoad($groupinfo,$groupboardinfo,$groupboardid,$groupid);
footer();

function PageLoad($groupinfo,$groupboardinfo,$groupboardid,$groupid){
	global $ActionName,$userid,$stats,$TopicInfo,$_DCACHE,$membername,$lang,$fsetting,$board_settings,$groupsetting,$master,$boardmaster,$superboardmaster,$gettitle,$boardinfo;

	$ActionName = $lang['ActionName_'.$GLOBALS['action']];
	$gettitle = $ActionName;
	$stats = $ActionName.' - '.$groupboardinfo['boardname'];
	//$fsetting['indivgroup_show'].'bbbbb'.$fsetting['indivgroup_check']	//群组基本设置
	set_cookie('dvbbs', '', 0);
	set_cookie('dvbbs', '', $GLOBALS['user_selected_cookiedate']);
	switch($GLOBALS['action']){
		case 're':
			Show_Post_Body_re();
			break;
		case 'edit':
			Show_Post_Body_edit();
			break;
		case 'new':
		default:
			Show_Post_Body_new($groupid,$groupboardid);
			break;
	}
}//end function PageLoad()


//发帖：Action = 1
function Show_Post_Body_new($groupid,$groupboardid){
	global $ActionName,$userinfo,$userid,$membername,$memberword,$master,$boardmaster,$superboardmaster,$lang,$db,$dv,$boardid,$board_settings,$groupsetting,$fsetting,$boardinfo;
	$toaction = 'indivgroup_savepost.php?action=snew&groupid='.$groupid.'&groupboardid='.$groupboardid;
	$IsAudit = intval($boardinfo['ischeckout']);
	$Action = 1;
	require_once CACHE_PATH.'forum_pic.php';
	$empath = $_DCACHE['forum_emot'][0];

	require printout('indivgroup_post');
}//end function Show_Post_Body_new

//回帖：Action = 2
function Show_Post_Body_re(){
	global $userid,$membername,$memberword,$master,$boardmaster,$superboardmaster,$lang,$db,$dv,$groupid,$groupboardid,$board_settings,$groupsetting,$fsetting,$AnnounceID,$ReplyID,$totalusetable,$boardinfo,$userinfo;
	if( !is_numeric($GLOBALS['id']) ){
		head();
		showmsg($lang['Error.strings30']);
		exit;
	} else{
		$AnnounceID = intval($GLOBALS['id']);
	}
	if( !is_numeric($GLOBALS['replyid']) OR $GLOBALS['replyid'] <= 0 ){
		$ReplyID = $AnnounceID;
	} else{
		$ReplyID = intval($GLOBALS['replyid']);
	}
	if( $query = $db->query("SELECT topicid FROM {$dv}group_topic WHERE boardid='{$groupboardid}' AND topicid={$AnnounceID}") ){
		$tmpResult =& $db->fetch_row($query);
		$db->free_result($query);
	}
	if( $tmpResult === false ){
		head();
		showmsg($lang['Error.strings48']);
		exit;
	} else{
		$totalusetable = "{$dv}group_bbs1";
		unset($tmpResult);
	}
	$toaction = 'indivgroup_savepost.php?action=sre&method=Topic&groupid='.$groupid.'&groupboardid='.$groupboardid;
	$IsAudit = intval($boardinfo['ischeckout']);
	$Action = 2;
	if( $ReplyID === $AnnounceID ){
		if( $query = $db->query("SELECT announceid FROM {$dv}group_bbs1 WHERE rootid='{$AnnounceID}' ORDER BY announceid",array('pageSize'=>1)) ){
			$tmpResult =& $db->fetch_row($query);
		}
		if( $tmpResult === false ){
			head();
			showmsg($lang['Error.strings48']);
			exit;
		} else{
			$ReplyID = $tmpResult[0];
			unset($tmpResult);
		}
	}
	if( $GLOBALS['guest'] ){
		if( $query = $db->query("SELECT body,groupid,boardid,topic,locktopic,username,dateandtime,isbest,ubblist,signflag,ip FROM {$dv}group_bbs1 WHERE announceid={$ReplyID}") ){
			$_topicInfo =& $db->fetch_array($query);
		}
	} else{
		if( $query = $db->query("SELECT 	b.body,b.groupid,b.boardid,b.topic,b.locktopic,b.username,b.dateandtime,b.isbest,u.islock,b.ubblist,b.signflag,b.ip FROM {$dv}group_bbs1 b INNER JOIN {$dv}group_user u ON b.postuserid=u.userid WHERE b.announceid={$ReplyID}") ){
			$_topicInfo =& $db->fetch_array($query);
		}
	}
	if( empty($_topicInfo) ){
		head();
		showmsg($lang['Error.strings48']);
		exit;
	}
	if( $GLOBALS['guest'] ){
		list($ip1,$ip2) = explode('.',$_topicInfo['ip']);
		$postip = "{$ip1}.{$ip2}.*.*";
	} else{
		$lockuser = $_topicInfo['lockuser'];
	}
//得到回复或引用帖子的判断和相关信息
	require_once CACHE_PATH.'forum_pic.php';
	$empath = $_DCACHE['forum_emot'][0];
	require printout('indivgroup_post');
}//end function Show_Post_Body_re

//编辑 主体部分：Action = 4
function Show_Post_Body_edit(){
	global $userid,$membername,$memberword,$master,$groupmaster,$lang,$db,$dv,$groupid,$groupboardid,$board_settings,$groupsetting,$fsetting,$boardinfo,$usergroupid,$CanEditPost,$membername;
	$toaction = 'indivgroup_savepost.php?action=sedit&groupboardid='.$groupboardid.'&groupid='.$groupid;
	if( !is_numeric($GLOBALS['id']) ){
		head();
		showmsg($lang['Error.strings30']);
		exit;
	} else{
		$AnnounceID = intval($GLOBALS['id']);
	}
	if( !is_numeric($GLOBALS['replyid']) ){
		$ReplyID = $AnnounceID;
	} else{
		$ReplyID = intval($GLOBALS['replyid']);
	}
	if( $query = $db->query("SELECT topicid,topicmode,expression,topicfont FROM {$dv}group_topic WHERE topicid={$AnnounceID}") ){
		$tmpResult =& $db->fetch_row($query);
	}
	if( empty($tmpResult) ){
		head();
		showmsg($lang['Error.strings48']);
		exit;
	} else{
		$totalusetable = "{$dv}group_bbs1";
		$TopModeSelect = ( empty($tmpResult[1]) ? 0 : intval($tmpResult[1]) );
		$TopicMode = ( empty($tmpResult[1]) ? 0 : intval($tmpResult[1]) );
		$TopicFont =  ( empty($tmpResult[3]) ? 0 : trim($tmpResult[3]) );
		$styletitle = ShowCodeTitle($TopicMode,$TopicFont,1);

		$iMagicFace = explode('|',$tmpResult[2]);
		if( count($iMagicFace) == 2 ){
			$FoundUseMagic = $iMagicFace[0];
		}
		unset($tmpResult);
	}
	//判断用户是否有编辑权限且提取相关信息
	if( $query = $db->query("SELECT b.username,b.topic,b.body,b.dateandtime,b.signflag,b.ubbList,b.expression FROM ".strtolower($totalusetable)." b WHERE b.rootid={$AnnounceID} AND b.announceid={$ReplyID}") ){
		$tmpResult =& $db->fetch_array($query);
	}
	if( empty($tmpResult) ){
		head();
		showmsg($lang['Error.strings77']);
		exit;
	}
	/***************** added by netty at 2007-1-26 16:50  start ****************/
	$CanEditPost = false;
	if( $tmpResult['username'] == $membername ){
		$CanEditPost = true;
	}else{
		if($master === true || $groupmaster === true){
			$CanEditPost = true;
		} 
		else{
			showmsg($lang['Error.strings77']);
			$CanEditPost = false;
		}
	}
	/***************** added by netty at 2007-1-26 16:50 end ****************/
	$signflag = $tmpResult['signflag'];
	$Expression = $tmpResult['expression'];
	$topic = str_replace('amp;','',htmlspecialchars($tmpResult['topic']));
	$content =& $tmpResult['body'];
	unset($tmpResult);
	$Action = 4;
	require_once CACHE_PATH.'forum_pic.php';
	$empath = $_DCACHE['forum_emot'][0];
	require printout('indivgroup_post');
}//end function Show_Post_Body_edit

function GetFormID(){
	$id = session_id();
	if (!empty($id)) {
		$tmpstr = '';
		$len = strlen($id);
		for ($i = 1; $i <= $len ; $i++) {
			$tmpstr .= chr(ord($id{$i})+97);
		}//end for
	}
	return $id;
}//end function GetFormID

function Chk_Post(){
	global $userinfo,$userid,$lang,$fsetting,$board_settings,$groupsetting,$boardinfo;
	if( $boardinfo['allowpost'] == 0 ){
		head();
		showmsg($lang['Error.strings72']);
		exit;
	}
	if( $boardinfo['ishidden'] == 1 && $groupsetting[37] == 0 ){
		head();
		showmsg($lang['Error.strings26']);
		exit;
	}
	if( $userid > 0 ){
		if( $groupsetting[52] > 0 && dateDiff('s',$userinfo['joindate'],TIME_NOW) < intval($groupsetting[52])*60 ){
			head();
			showmsg(str_replace('{$timelimited}',$groupsetting[52],$lang['template.strings21']));
			exit;
		}
		if( $groupsetting[62] != 0 && $GLOBALS['action'] != 'edit' ){
			if( $userid <= 0 ){
				$UserToday = 0;
			} else{
				if( $userinfo['usertoday'] == '' ){
					$arrUserToday = array(0,0,0,0,0);
				} else{
					$arrUserToday = explode('|',$userinfo['usertoday']);
				}
				$UserToday = intval($arrUserToday[0]);//Dvbbs.UserToday(0)
			}
			if( $groupsetting[62] < $UserToday[0] ){
				head();
				showmsg(str_replace('{$topiclimited}',$groupsetting[62],$lang['template.strings27']));
				exit;
			}
		}
	}
	if( $groupsetting[3] == 0 && ( $GLOBALS['action'] == 'new' || $GLOBALS['action'] == 'vote' || $GLOBALS['action'] == 'argue_topic' ) ){
		head();
		showmsg($lang['template.strings28']);
		exit;
	}
	if( $groupsetting[5] == 0 && $GLOBALS['action'] == 're' ){
		head();
		showmsg($lang['template.strings29']);
		exit;
	}
}//end function Chk_Post

//编辑时用（对旧数据兼容）
function Ubb2Html($str){
	if( $str == '' || is_null($str) ) return '';
	global $lang,$groupsetting,$board_settings,$Dvbbs_Mode;
	$arrPattern = array('/(>)(?:\r|\n)+(<)/i');
	$arrRepl = array('$1$2');
	$Dvbbs_Mode = $groupsetting[67];
	if( $board_settings[66] == 0 ) $Dvbbs_Mode = 2;
	if (2 == $Dvbbs_Mode) {
		$arrPattern[] = '#&nbsp|&nbsp;#i';
		$arrRepl[] = ' ';
	} else {
		$arrPattern[] = '#\r\n|\n#i';
		$arrRepl[] = '<br />';
		$arrPattern[] = '#  #i';
		$arrRepl[] = '&nbsp;&nbsp;';
		$arrPattern[] = '#	#i';
		$arrRepl[] = '&nbsp;';
	}
	$arrPattern[] = '#<I><\/I>#i';
	$arrRepl[] = '';
	$arrPattern[] = '/<I><\/I>/i';
	$arrRepl[] = '';
	$arrPattern[] = '/<(\w+)(?:&nbsp|&nbsp;)+([^>]*)>/i';
	$arrRepl[] = '<$1 $2>';
	if( $GLOBALS['reply'] === 'true' ){
		$arrPattern[] = '/\<div class="quote"\>\<b\>'.$lang['tpl.str3'].'(?:.|\n)*\<\/div\>/i';
		$arrRepl[] = '';
		$arrPattern[] = '/\[quote\]\<b\>'.$lang['tpl.str3'].'(?:.|\n)*\[\/quote\]/i';
		$arrRepl[] = '';
		$arrPattern[] = '/\[quote\]\[b\]'.$lang['tpl.str3'].'(?:.|\n)*\[\/quote\]/i';
		$arrRepl[] = '';
	}
	return preg_replace( $arrPattern ,$arrRepl ,$str );
}//end function Ubb2Html

//截取指定字符
function cutStr($str,$strlen){
	//去掉所有HTML标记
	$str = preg_replace( '/<([^<>]*?)>/i' ,'' ,$str);
	$len = strlen($str);
	$t = 0;
	$returnVal = '';
	for( $i = 0 ; $i < $len ; $i++ ){
		$c = abs(ord($str{$i}));
		if( $c > 255 ){
			$t += 2;
		} else{
			$t++;
		}
		if( $t > $len ){
			$returnVal = substr($str,0,$i+1).'...';
			break;
		} else{
			$returnVal = $str;
		}
	}
	return str_replace("\r",'',str_replace("\n",'',$returnVal));
}//end function cutStr

//过滤不必要的 UBB
function reUBBCode(&$strContent)
{
	if ('' == $strContent) {
		return '';
	}
	global $lang;
	return preg_replace(array(0=>'#<\/div>#i', 1=>'#<div class="?quote"?><b>'.$lang['tpl.str3'].'#i', 2=>'#<div class="?quote"?>([^\x01\x02]*)\x02#i', 3=>'#\x01[^\x02]*\x02#i', 4=>'#\[quote\]((?:.|\n)*?)\[\/quote\]#i', 5=>'#\x02#i', 6=>'#<div align="?right"?><font color="?\#000066"?>(?:.|\n)*?<\/font><\/div>#i', 7=>'#\[align=right\]\[color=\#000066\](?:.|\n)*?\[\/color\]\[\/align\]#i', 8=>'#\[point=*([0-9]*)\](?:.|\n)*?\[\/point\]#i', 9=>'#\[post=*([0-9]*)\](?:.|\n)*?\[\/post\]#i', 10=>'#\[power=*([0-9]*)\](?:.|\n)*?\[\/power\]#i', 11=>'#\[usercp=*([0-9]*)\](?:.|\n)*?\[\/usercp\]#i', 12=>'#\[money=*([0-9]*)\](?:.|\n)*?\[\/money\]#i', 13=>'#\[replyview\](?:.|\n)*?\[\/replyview\]#i', 14=>'#\[usemoney=*([0-9]*)\](?:.|\n)*?\[\/usemoney\]#i', 15=>'#\[UserName=(.[^\[]*)\](?:.|\n)*?\[\/UserName\]#i', 16=>'#  #i', 17=>'#<I><\/I>#i'), array(0=>chr(2), 1=>chr(1), 2=>'[quote]$1[/quote]', 3=>'', 4=>'<div class="quote">$1</div>', 5=>'</div>', 6=>'', 7=>'', 8=>'', 9=>'', 10=>'', 11=>'', 12=>'', 13=>'', 14=>'', 15=>'', 16=>'&nbsp;&nbsp;', 17=>''), $strContent);
}//end function reUBBCode
?>