<?php
define('THIS_SCRIPT', 'post');

require_once 'global.php';
$lang = load_lang($lang, '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;
}
$signflag = 1;
$mailflag = 0;

PageLoad();
footer();
function PageLoad(){
	global $ActionName,$userid,$stats,$TopicInfo,$_DCACHE,$boardid,$membername,$lang,$fsetting,$board_settings,$groupsetting,$master,$boardmaster,$superboardmaster,$gettitle,$boardinfo,$ActionName;
    
	if( $boardid <= 0 ){
		exit($lang['Error.strings0']);
	}
    
	if( $userid <= 0 && $groupsetting[3] == 0 ){
		$stats = $lang['Error.strings3'].' - '.$boardinfo['boardtype'];
		$arrNavMenu = array( 'linktitle' => $lang['Error.strings3'] );
		head(0,1,0,$arrNavMenu);
		showmsg($lang['Error.strings78']);//edit 2007-4-18
	}
	if( ( $master === false || $boardmaster === false ) && $boardinfo['allowpost'] == 2 ){
		$stats = $lang['Error.strings3'].' - '.$boardinfo['boardtype'];
		$arrNavMenu = array( 'linktitle' => $lang['Error.strings3'] );
		head(0,1,0,$arrNavMenu);
		showmsg($lang['Error.strings1']);
	}
	if( $master === false && IsReadonly() ){
		$stats = $lang['Error.strings3'].' - '.$boardinfo['boardtype'];
		$arrNavMenu = array( 'linktitle' => $lang['Error.strings3'] );
		head(0,1,0,$arrNavMenu);
		showmsg($lang['Error.strings2']);
	}
	$acName = 'ActionName_'.trim($GLOBALS['action']).trim($GLOBALS['stype']);
	$ActionName = $lang[$acName];
	$gettitle = $ActionName;
	$stats = $ActionName.' - '.$boardinfo['boardtype'];
	head();
	$GLOBALS['TotalUseTable'] = $fsetting['nowtablename'];
	Chk_Post();
	set_cookie('dvbbs', '', 0);
	set_cookie('dvbbs', '', $GLOBALS['user_selected_cookiedate']);
	switch($GLOBALS['action']){
		case 're':
			Show_Post_Body_re();
			break;
		case 'vote':
			Show_Post_Body_vote();
			break;
		case 'edit':
			Show_Post_Body_edit();
			break;
		case 'argue_topic':	//	发起一个辩论主题
			Show_Argue_Body_topic();
			break;
		case 'active_topic': // 发起一个活动主题
			Show_Active_topic();
			break;
		case 'argue_topic_edit':	//	编辑辩论主题
			Show_Argue_Body_topic_edit();
			break;
		case 'argue_reply':	//	发表一个观点/回复/评论
			Show_Argue_Body_reply();
			break;
		case 'argue_append':	//	对某个观点/回复/评论进行补充
			Show_Argue_Body_append();
			break;
		case 'new':
		default:
			Show_Post_Body_new();
			break;
	}
}//end function PageLoad()



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

//回帖：Action = 2
function Show_Post_Body_re(){
	global $userid,$membername,$memberword,$master,$boardmaster,$superboardmaster,$lang,$db,$dv,$boardid,$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 posttable,getmoneytype FROM {$dv}topic WHERE boardid='{$boardid}' AND topicid={$AnnounceID}") ){
		$tmpResult =& $db->fetch_row($query);
		$db->free_result($query);
	}
	if( $tmpResult === false ){
		head();
		showmsg($lang['Error.strings48']);
		exit;
	} else{
		$totalusetable = $tmpResult[0];
		$GetMoneyType = $tmpResult[1];
		unset($tmpResult);
	}
	$toaction = 'savepost.php?action=sre&method=Topic&boardid='.$boardid;
	$IsAudit = intval($boardinfo['ischeckout']);
	$Action = 2;
	if( $ReplyID === $AnnounceID ){
		if( $query = $db->query("SELECT announceid FROM {$totalusetable} 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,topic,locktopic,username,dateandtime,isbest,ubblist,postbuyuser,getmoneytype,signflag,emailflag,ip FROM {$totalusetable} WHERE announceid={$ReplyID} AND postuserid=0") ){
			$_topicInfo =& $db->fetch_array($query);
		}
	} else{
		if( $query = $db->query("SELECT b.body,b.topic,b.locktopic,b.username,b.dateandtime,b.isbest,u.lockuser,u.usergroupid,b.ubblist,b.postbuyuser,b.getmoneytype,b.signflag,emailflag,b.ip FROM {$totalusetable} b INNER JOIN {$dv}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('post');
}//end function Show_Post_Body_re

//投票：Action = 3
function Show_Post_Body_vote(){
	global $userid,$membername,$memberword,$master,$boardmaster,$superboardmaster,$lang,$db,$dv,$boardid,$board_settings,$groupsetting,$fsetting,$boardinfo;
	$toaction = 'savepost.php?action=svote&boardid='.$boardid;
	$IsAudit = intval($boradinfo['ischeckout']);
	$Action = 3;
	require_once CACHE_PATH.'forum_pic.php';
	$empath = $_DCACHE['forum_emot'][0];
	require printout('post');
}//end function Show_Post_Body_vote

//编辑 主体部分：Action = 4
function Show_Post_Body_edit(){
	global $userid,$membername,$memberword,$master,$boardmaster,$superboardmaster,$lang,$db,$dv,$boardid,$board_settings,$groupsetting,$fsetting,$boardinfo,$usergroupid,$CanEditPost,$membername,$fontstr,$colorstr;
	$toaction = 'savepost.php?action=sedit&boardid='.$boardid;
	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 posttable,topicmode,expression,topicfont FROM {$dv}topic WHERE topicid={$AnnounceID}") ){
		$tmpResult =& $db->fetch_row($query);
	}
	if( empty($tmpResult) ){
		head();
		showmsg($lang['Error.strings48']);
		exit;
	} else{
		$totalusetable = $tmpResult[0];
		$TopModeSelect = ( empty($tmpResult[1]) ? 0 : intval($tmpResult[1]) );
		$iMagicFace = explode('|',$tmpResult[2]);
		if( count($iMagicFace) == 2 ){
			$FoundUseMagic = $iMagicFace[0];
		}
		$TopicMode = ( empty($tmpResult[1]) ? 0 : intval($tmpResult[1]) );
		$TopicFont =  ( empty($tmpResult[3]) ? 0 : trim($tmpResult[3]) );
		$styletitle = ShowCodeTitle($TopicMode,$TopicFont,1);

		unset($tmpResult);
	}
	//判断用户是否有编辑权限且提取相关信息
	if( $query = $db->query("SELECT b.username,b.topic,b.body,b.dateandtime,u.usergroupid,b.signflag,b.emailflag,b.ubbList,b.expression,b.usetools FROM ".strtolower($totalusetable)." b LEFT OUTER JOIN {$dv}user u ON b.postuserid=u.userid WHERE b.rootid={$AnnounceID} AND b.announceid={$ReplyID}") ){
		$tmpResult =& $db->fetch_array($query);
		//$membername = $tmpResult['username'];
	}
	if( empty($tmpResult) ){
		head();
		showmsg($lang['Error.strings48']);		
		exit;
	}
	/***************** added by netty at 2007-1-26 16:50  start ****************/
	$CanEditPost = false;
	if( $tmpResult['username'] == $membername ){
		if( $groupsetting[10] == 0 ){
			$CanEditPost = false;
			head();
			showmsg($lang['Error.strings74']);
			exit;
		} else{
			$CanEditPost = true;
		}
	}else{
		if( ($master === true || $superboardmaster || $boardmaster ) && $groupsetting[23] == 1 ){
			$CanEditPost = true;
		} else{
			$CanEditPost = false;
		}
		global $usergroupid,$founduserper;
		if( $usergroupid > 3 && $groupsetting[23] == 1 ){
			$CanEditPost = true;
		}
		if( $groupsetting[23] == 1 && $founduserper === true ){
			$CanEditPost = true;
		} elseif( $groupsetting[23] == 0 && $founduserper === true ){
			$CanEditPost = false;
		}
		if( $usergroupid < 4 && $usergroupid == $tmpResult['usergroupid'] ){
			head();
			showmsg($lang['Error.strings75']);			
			exit;
		} elseif( $usergroupid < 4 && $usergroupid > $tmpResult['usergroupid'] ){
			head();
			showmsg($lang['Error.strings76']);
			exit;
		}
		if( $CanEditPost === false ){
			head();
			showmsg($lang['Error.strings77']);
			exit;
		}
	}
	/***************** added by netty at 2007-1-26 16:50 end ****************/
	$mailflag = $tmpResult['emailflag'];
	$signflag = $tmpResult['signflag'];
	$Notanony = (false !== strpos($tmpResult['usetools'], '17'));
	$Expression = $tmpResult['expression'];
	$topic = str_replace('amp;','',htmlspecialchars($tmpResult['topic']));
	$content =& $tmpResult['body'];
	unset($tmpResult);
	if( $fsetting['plus_magicface'] == 1 && $board_settings[56] == 1 && $groupsetting[69] == 1 ){
		if( $FoundUseMagic > 0 ){
			if( $query = $db->query("SELECT tmoney,tticket FROM {$dv}plus_tools_magicface WHERE magicface_s={$FoundUseMagic}") ){
				$tmpResult =& $db->fetch_row($query);
			}
			if( empty($tmpResult) ){
				$FoundUseMagic = 0;
			} else{
				$tMagicMoney = $tmpResult[0];
				$tMagicTicket = $tmpResult[1];
			}
			$firstmagicface = $FoundUseMagic;
			$magicmoney = $tMagicMoney;
			$magicticket = $tMagicTicket;
			$isselect = ' selected ';
		} else{
			if( $query = $db->query("SELECT magicface_s,tmoney,tticket FROM {$dv}plus_tools_magicface ORDER BY id",array('pageSize'=>1)) ){
				$tmpResult =& $db->fetch_row($query);
			}
			if( empty($tmpResult) ){
				$firstmagicface = 0;
				$magicmoney = 0;
				$magicticket = 0;
			} else{
				$firstmagicface = $tmpResult[0];
				$magicmoney = $tmpResult[1];
				$magicticket = $tmpResult[2];
			}
			$isselect = '';
		}
	}
	$IsAudit = intval($boardinfo['ischeckout']);
	$Action = 4;
	require_once CACHE_PATH.'forum_pic.php';
	$empath = $_DCACHE['forum_emot'][0];
	require printout('post');
}//end function Show_Post_Body_edit

function Show_Argue_Body_topic()	//	发起一个辩论主题：$Action = 5
{
	global $ActionName,$userinfo,$userid,$membername,$memberword,$master,$boardmaster,$superboardmaster,$lang,$db,$dv,$boardid,$board_settings,$groupsetting,$fsetting,$boardinfo;
	$toaction = 'savepost.php?action=argue_topic&boardid='.$boardid;
	$IsAudit = intval($boardinfo['ischeckout']);
	$Action = 5;
	require_once CACHE_PATH.'forum_pic.php';
	require printout('post');
}//end function Show_Argue_Body_topic

function Show_Argue_Body_topic_edit()	//	发起一个辩论主题：$Action = 6
{
	global $ActionName,$userinfo,$userid,$membername,$memberword,$master,$boardmaster,$superboardmaster,$lang,$db,$dv,$boardid,$board_settings,$groupsetting,$fsetting,$boardinfo;
	$toaction = 'savepost.php?action=argue_topic_edit&boardid='.$boardid;
	$IsAudit = intval($boardinfo['ischeckout']);
	$Action = 6;
	require_once CACHE_PATH.'forum_pic.php';
	require printout('post');
}//end function Show_Argue_Body_topic_edit

function Show_Argue_Body_reply()	//	发表一个观点/回复/评论：$Action = 7
{
	global $ActionName,$userinfo,$userid,$membername,$memberword,$master,$boardmaster,$superboardmaster,$lang,$db,$dv,$boardid,$board_settings,$groupsetting,$fsetting,$boardinfo,$id,$arguetype,$parentid;
	require_once INC_PATH.'argue_mod.functions.php';
	$rv = check_posted_argumentation($boardid, $id, $arguetype, $parentid, $userid);
	if (false === $rv['execflag']) {
		showmsg($rv['execmsg']);
		exit;
	}
	$toaction = 'savepost.php?action=argue_reply&amp;arguetype='.$arguetype.'&amp;id='.$id.'&amp;parentid='.$parentid.'&amp;boardid='.$boardid;
	$IsAudit = intval($boardinfo['ischeckout']);
	$Action = 7;
	require_once CACHE_PATH.'forum_pic.php';
	$empath = $_DCACHE['forum_emot'][0];
	require printout('post');
}//end function Show_Argue_Body_reply

function Show_Argue_Body_append()	//	对某个观点/回复/评论进行补充：$Action = 8
{
	global $ActionName,$userinfo,$userid,$membername,$memberword,$master,$boardmaster,$superboardmaster,$lang,$db,$dv,$boardid,$board_settings,$groupsetting,$fsetting,$boardinfo,$id,$arguetype,$parentid,$argueid;
	$toaction = 'savepost.php?action=argue_append&amp;id='.$id.'&amp;argueid='.$argueid.'&amp;parentid='.$parentid.'&amp;boardid='.$boardid;
	$IsAudit = intval($boardinfo['ischeckout']);
	$Action = 8;
	require_once CACHE_PATH.'forum_pic.php';
	$empath = $_DCACHE['forum_emot'][0];
	require printout('post');
}//end function Show_Argue_Body_append

function Show_Active_topic() // 发起一个活动主题：$Action = 9
{
	global $userid,$membername,$memberword,$master,$boardmaster,$superboardmaster,$lang,$db,$dv,$boardid,$board_settings,$groupsetting,$fsetting,$boardinfo;
	$toaction = 'savepost.php?action=active_topic&boardid='.$boardid;
	$IsAudit = intval($boradinfo['ischeckout']);
	$Action = 9;
	require_once CACHE_PATH.'forum_pic.php';
	$empath = $_DCACHE['forum_emot'][0];
	require printout('post');
}//end function Show_Active_topic
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 ){
				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)|(?:\n))(<)/i' ,'/(>)(?:(?:(?:\r\n)|(?:\n)){2})(<)/i' );
	//$arrRepl = array( '$1$2' ,'$1$2' );
	$arrPattern = array('/(>)(?:\r|\n)+(<)/i');
	$arrRepl = array('$1$2');
	$Dvbbs_Mode = $groupsetting[67];//echo '<!--'.$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
?>