<?php
/*============================================================================
 ||	#######################################################################	||
 ||																			||
 ||	Powered by Dvbbs.PHP 1.0.0 2000 - 2006 Cndw.Com					||
 ||	author:			xiaodu@dv												||
 ||	date:			2006-09-18												||
 ||	modified date:	2006-09-30												||
 ||																			||
 ||	#######################################################################	||
 ============================================================================*/
if( !defined('ISDVBBS') ){
	header('HTTP/1.0 400 Bad Request');
	exit;
}
!defined('TABLE_PREFIX') AND define('TABLE_PREFIX', $dv);
if (!defined('USER_ID')) {
	if ($userid <= 0) {
		$userid = 0;
	}
	define('USER_ID', $userid);
}
!defined('MEMBER_NAME') AND define('MEMBER_NAME', $membername);
if (!defined('ROOT_ID')) {
	if ($id <= 0) {
		$id = 0;
	}
	define('ROOT_ID', $id);
}
if (!defined('BOARD_ID')) {
	if ($boardid <= 0) {
		$boardid = 0;
	}
	define('BOARD_ID', $boardid);
}
if (!defined('PAGE_SIZE')) {
	if ($board_settings[61] <= 0) {
		$board_settings[61] = 10;
	}
	define('PAGE_SIZE', $board_settings[61]);
}
!defined('FILE_CACHE_TOPIC_INFO') AND define('FILE_CACHE_TOPIC_INFO', true);
!defined('TIME_NOW') AND define('TIME_NOW', time());

$lang = load_lang($lang, 'argue_mode' );
!defined('USER_FLD_LIST') AND define('USER_FLD_LIST', 'u.userid,u.username,u.lockuser,u.usergroupid,u.userhidden,u.usersex');
!defined('ARGUE_TOPIC_FLD_LIST') AND define('ARGUE_TOPIC_FLD_LIST', 'at.boardid,at.rootid,at.title,at.title_obverse,at.title_reversed,at.title_third,at.content,at.creation_date,at.start_time,end_time,at.postuserid,at.postusername,at.child,at.child_obverse,at.child_obverse_post,at.child_obverse_reply,at.child_reversed,at.child_reversed_post,at.child_reversed_reply,at.child_third,at.child_third_post,at.child_third_reply,at.vote,at.vote_obverse,at.vote_reversed,at.vote_third,at.hits,at.ip,at.locktopic,at.ubblist,at.isbest');
$selectcontent = '';
global $db;
if (true === $master OR true === $superboardmaster) {
	$selectcontent = "(CASE argue.locktopic WHEN '0' THEN argue.content ELSE CONCAT(argue.content, '".$db->escape_string($lang['argue_mod.tpl.str2'])."') END) AS content,";
} else {
	$selectcontent = "(CASE argue.locktopic WHEN '2' THEN '".$db->escape_string($lang['argue_mod.tpl.str3'])."' ELSE argue.content END) AS content,";
}
!defined('ARGUE_FLD_LIST') AND define('ARGUE_FLD_LIST', 'argue.argueid,argue.boardid,argue.rootid,argue.arguetype,argue.parentid,argue.child,argue.vote,argue.expression,argue.title,'.$selectcontent.'argue.creation_date,argue.postusername,argue.postuserid,argue.ip,argue.length,argue.locktopic,argue.ubblist,argue.isbest');

require_once INC_PATH.'dv_code.php';

function _debug($str)
{
	print '<pre>'.htmlspecialchars(print_r($str, true)).'</pre>';
}//end function _debug

function &get_argumentation_topicinfo($rootid = 0, $boardid = 0, $opts = array())	//	读取辩论主题数据
{
	static $topicinfo;
	static $cachefileexists = true;
	if ($rootid <= 0) {
		if (ROOT_ID <= 0) {
			$topicinfo = false;
			return $topicinfo;
		} else {
			$rootid = ROOT_ID;
		}
	}
	if ($boardid <= 0) {
		$boardid = BOARD_ID;
	}
	$mustupdate = $mustupdate2 = false;
	(!isset($opts['hits_auto_increment']) OR !is_bool($opts['hits_auto_increment'])) AND ($opts['hits_auto_increment'] = false);
	if (!isset($topicinfo)) {
		$topicinfo = false;
		if (true === FILE_CACHE_TOPIC_INFO) {
			$cachefile = CACHE_PATH.'argumentation/cache_'.$rootid.'.php';
			if ($isfile = is_file($cachefile)) {
				require_once $cachefile;
				$topicinfo = $_DCACHE['argumentation'];
				if ($_DCACHE['Page_Size'] != PAGE_SIZE) {
					$mustupdate2 = true;
				}
			} else {
				$mustupdate = true;
				$cachefileexists = false;
			}
		}
		global $db;
		if (empty($topicinfo)) {
			$mustupdate = true;
			$rs =& $db->getResultSet("SELECT /*!40001 SQL_CACHE */ ".ARGUE_TOPIC_FLD_LIST.",".USER_FLD_LIST." FROM ".TABLE_PREFIX."argue_topic AS at INNER JOIN ".TABLE_PREFIX."user AS u ON at.postuserid=u.userid WHERE at.boardid='".$boardid."' AND at.rootid='".$rootid."' AND at.postuserid=u.userid", array('pageSize'=>1));
			$topicinfo = $rs[0];
			unset($rs);
		}
		if (!empty($topicinfo)) {
			/**
			 *	$topicinfo['content'] = array(
			 *		'ArgueIDList'=>array(
			 *			0=>代表正方观点的以逗号分隔的 argueid 列表,
			 *			1=>代表反方观点的以逗号分隔的 argueid 列表,
			 *			2=>代表第三方观点的以逗号分隔的 argueid 列表,
			 *		),
			 *		'VoteOptions'=>array(
			 *			0=>'限制投票文章数', 1=>'限制投票金钱数', 2=>'限制投票经验数',
			 *			3=>'限制投票魅力数', 4=>' *限制投票威望数'
			 *		)
			 *		'context_info'=>背景资料,
			 *	);
			 */
			!$isfile AND !empty($topicinfo['content']) AND !($topicinfo['content'] = unserialize($topicinfo['content'])) AND ($topicinfo['content'] = array('ArgueIDList'=>array(0=>'', 1=>'', 2=>''), 'VoteUser'=>array(0=>0, 1=>0, 2=>0, 3=>0, 4=>0), 'context_info'=>''));
			
			$stmt = '';
			if ($mustupdate2 OR ((PAGE_SIZE != $topicinfo['child_obverse_post']) AND (PAGE_SIZE <= $topicinfo['child_obverse']))) {
				if ($mustupdate2 OR (PAGE_SIZE > $topicinfo['child_obverse_post'])) {
					$mustupdate = true;
					$stmt .= "(SELECT /*!40001 SQL_CACHE */ 1,@ArgueIDList1:=CONCAT(@ArgueIDList1, ',', argueid),'','' FROM ".TABLE_PREFIX."argue WHERE boardid='".$boardid."' AND rootid='".$rootid."' AND arguetype='1' AND parentid=argueid LIMIT ".PAGE_SIZE.")";
				} else {
					$arr = explode(',', str_replace(',0,', '', $topicinfo['content']['ArgueIDList'][0]));
					$topicinfo['content']['ArgueIDList'][0] = implode(',', array_slice($arr, 0, PAGE_SIZE));
				}
			}
			if ($mustupdate2 OR ((PAGE_SIZE != $topicinfo['child_reversed_post']) AND (PAGE_SIZE <= $topicinfo['child_reversed']))) {
				if ($stmt !== '') {
					$stmt .= ' UNION ALL ';
				}
				if ($mustupdate2 OR (PAGE_SIZE > $topicinfo['child_reversed_post'])) {
					$mustupdate = true;
					$stmt .= "(SELECT /*!40001 SQL_CACHE */ 2,'',@ArgueIDList2:=CONCAT(@ArgueIDList2, ',', argueid),'' FROM ".TABLE_PREFIX."argue WHERE boardid='".$boardid."' AND rootid='".$rootid."' AND arguetype='2' AND parentid=argueid LIMIT ".PAGE_SIZE.")";
				} else {
					$arr = explode(',', str_replace(',0,', '', $topicinfo['content']['ArgueIDList'][1]));
					$topicinfo['content']['ArgueIDList'][1] = implode(',', array_slice($arr, 0, PAGE_SIZE));
				}
			}
			if ($mustupdate2 OR ((PAGE_SIZE != $topicinfo['child_third_post']) AND (PAGE_SIZE <= $topicinfo['child_third']))) {
				if ($stmt !== '') {
					$stmt .= ' UNION ALL ';
				}
				if ($mustupdate2 OR (PAGE_SIZE > $topicinfo['child_third_post'])) {
					$mustupdate = true;
					$stmt .= "(SELECT /*!40001 SQL_CACHE */ 3,'','',@ArgueIDList3:=CONCAT(@ArgueIDList3, ',', argueid) FROM ".TABLE_PREFIX."argue WHERE boardid='".$boardid."' AND rootid='".$rootid."' AND arguetype='3' AND parentid=argueid LIMIT ".PAGE_SIZE.")";
				} else {
					$arr = explode(',', str_replace(',0,', '', $topicinfo['content']['ArgueIDList'][2]));
					$topicinfo['content']['ArgueIDList'][2] = implode(',', array_slice($arr, 0, PAGE_SIZE));
				}
			}
			if (!empty($stmt)) {
				$stmt = "(SELECT /*!40001 SQL_CACHE */ 10000 AS ArgueType,@ArgueIDList1:=',0' AS ArgueIDList1,@ArgueIDList2:=',0' AS ArgueIDList2,@ArgueIDList3:=',0' AS ArgueIDList3) UNION ALL ".$stmt." UNION ALL (SELECT /*!40001 SQL_CACHE */ 0 ,@ArgueIDList1, @ArgueIDList2, @ArgueIDList3) ORDER BY ArgueType ASC LIMIT 1";
				$lastinfo = false;
				if (($query = $db->query($stmt)) AND ($lastinfo =& $db->fetch_row($query))) {		
					$lastinfo[1] = (',0' == $lastinfo[1]) ? '' : str_replace(',0,', '', $lastinfo[1]);
					$lastinfo[2] = (',0' == $lastinfo[2]) ? '' : str_replace(',0,', '', $lastinfo[2]);			
					$lastinfo[3] = (',0' == $lastinfo[3]) ? '' : str_replace(',0,', '', $lastinfo[3]);
				}
				if (!empty($lastinfo) AND is_array($lastinfo)) {
					!empty($lastinfo[1]) AND ($topicinfo['content']['ArgueIDList'][0] = $lastinfo[1]);
					!empty($lastinfo[2]) AND ($topicinfo['content']['ArgueIDList'][1] = $lastinfo[2]);
					!empty($lastinfo[3]) AND ($topicinfo['content']['ArgueIDList'][2] = $lastinfo[3]);
				}
			}
		}
	}
	if (!empty($topicinfo)) {
		if (true === $opts['hits_auto_increment']) {
			if ($db->version() >= '4.0.4') {
				$db->query("UPDATE ".TABLE_PREFIX."argue_topic AS at,".TABLE_PREFIX."topic AS t SET at.hits=at.hits+1, t.hits=t.hits+1 WHERE at.boardid='".$boardid."' AND at.rootid='".$topicinfo['rootid']."' AND t.topicid='".$topicinfo['rootid']."'");
			} else {
				$db->query("UPDATE ".TABLE_PREFIX."argue_topic SET hits=hits+1 WHERE boardid='".$boardid."' AND rootid='".$topicinfo['rootid']."'");
				$db->query("UPDATE ".TABLE_PREFIX."topic SET hits=hits+1 WHERE topicid='".$topicinfo['rootid']."'");
			}
			if (true === FILE_CACHE_TOPIC_INFO AND false === $cachefileexists) {
				$topicinfo['hits']++;
			} else {
				$topicinfo['hits'] = $db->scalar("SELECT /*!40001 SQL_NO_CACHE */ hits FROM ".TABLE_PREFIX."argue_topic WHERE boardid='".$boardid."' AND rootid='".$topicinfo['rootid']."'");
			}
		}
		if (false === $cachefileexists OR true === $mustupdate) {
			update_argumentation_topicinfo($topicinfo);
		}
	}
	return $topicinfo;
}//end function get_argumentation_topicinfo
function update_argumentation_topicinfo(&$topicinfo, $opts = array())	//	更新辩论主题数据
{
	(empty($topicinfo) OR !is_array($topicinfo)) AND ($topicinfo =& get_argumentation_topicinfo());
	if (empty($topicinfo) OR !is_array($topicinfo)) {
		return false;
	}
	global $db;
	$rootid = $topicinfo['rootid'];
	$content = $topicinfo['content'];
	$topicinfo['content'] = serialize($topicinfo['content']);
	$stmt = "UPDATE ".TABLE_PREFIX."argue_topic SET ";
	$comma = '';
	$fldlist = explode(',', str_replace('at.', '', ARGUE_TOPIC_FLD_LIST));
	foreach ($fldlist AS $index => $fldname) {
		$fldval = $topicinfo[$fldname];
		$stmt .= $comma.$fldname."='".$db->escape_string($fldval)."'";
		$comma = ',';
	}//end foreach
	$stmt .= " WHERE boardid='".$topicinfo['boardid']."' AND rootid='".$rootid."'";
	$db->query($stmt);
	$topicinfo['content'] = $content;
	//Start 更新dv_topic表的相关字段
		if (!empty($opts['update_dv_topic']) AND is_array($opts['update_dv_topic'])) {
			$arrtmp =& $opts['update_dv_topic'];
			(!isset($arrtmp['escaped']) OR !is_bool($arrtmp['escaped'])) AND ($arrtmp['escaped'] = false);
			(!isset($arrtmp['cssproced']) OR !is_bool($arrtmp['cssproced'])) AND ($arrtmp['cssproced'] = false);
			(!isset($arrtmp['child_auto_increment']) OR !is_bool($arrtmp['child_auto_increment'])) AND ($arrtmp['child_auto_increment'] = false);
			(!isset($arrtmp['vote_auto_increment']) OR !is_bool($arrtmp['vote_auto_increment'])) AND ($arrtmp['vote_auto_increment'] = false);
			(!isset($arrtmp['hits_sync']) OR !is_bool($arrtmp['hits_sync'])) AND ($arrtmp['hits_sync'] = false);
			!isset($arrtmp['lastpost']) AND ($arrtmp['lastpost'] = '');
			!isset($arrtmp['lastposttime']) AND ($arrtmp['lastposttime'] = '');
			$comma = $updatelist = '';
			(true === $arrtmp['child_auto_increment']) AND ($updatelist .= $comma.'child='.$topicinfo['child']) AND ($comma = ',');
			(true === $arrtmp['vote_auto_increment']) AND ($updatelist .= $comma.'votetotal='.$topicinfo['vote']) AND ($comma = ',');
			(true === $arrtmp['hits_sync']) AND ($updatelist .= $comma."hits=".intval($topicinfo['hits'])) AND ($comma = ',');
			!empty($arrtmp['lastpost']) AND ($tmplastpost = escapestring($arrtmp['lastpost'], $arrtmp['escaped'], !$arrtmp['cssproced'])) AND ($updatelist .= $comma."lastpost='".str_replace('$dv_argumentation$', '$$', $tmplastpost)."'") AND ($comma = ',');
			!empty($arrtmp['lastposttime']) AND ($updatelist .= $comma."lastposttime='".escapestring($arrtmp['lastposttime'], true, false)."'") AND ($comma = ',');
			!empty($updatelist) AND $db->query("UPDATE ".TABLE_PREFIX."topic SET ".$updatelist." WHERE topicid='".$rootid."'") AND $db->query("UPDATE ".TABLE_PREFIX."board SET lastpost='".$tmplastpost."' WHERE boardid='".$topicinfo['boardid']."'");
		}
	//End 更新dv_topic表的相关字段
	if (true === FILE_CACHE_TOPIC_INFO) {	//	更新缓存文件
		$cachefile = CACHE_PATH.'argumentation/cache_'.$rootid.'.php';
		require_once INC_PATH.'FileHandler.class.php';
		$filecontents = "<?php\r\n\$_DCACHE['argumentation'] = ".FileHandler::_var_export($topicinfo, true).";\r\n\$_DCACHE['Page_Size']='".PAGE_SIZE."';\r\n?>";
		FileHandler::file_put_contents($cachefile, $filecontents);
	}
	return true;
}//end function update_argumentation_topicinfo
function &get_top_argumentation_info($rootid = 0, $boardid = 0, $opts = array())	//	读取第一页的辩论数据
{
	static $cacheinfo;
	if (!isset($cacheinfo)) {
		$cacheinfo = false;
		if ($topicinfo =& get_argumentation_topicinfo($rootid, $boardid, $opts['argumentation_topic_opts'])) {
			$cacheinfo['topicinfo'] =& $topicinfo;
			$cacheinfo['argueinfo'] = array();
			global $db;
			$argueidlist = '0';
			!empty($topicinfo['content']['ArgueIDList'][0]) AND ($argueidlist .= ','.$topicinfo['content']['ArgueIDList'][0]);
			!empty($topicinfo['content']['ArgueIDList'][1]) AND ($argueidlist .= ','.$topicinfo['content']['ArgueIDList'][1]);
			!empty($topicinfo['content']['ArgueIDList'][2]) AND ($argueidlist .= ','.$topicinfo['content']['ArgueIDList'][2]);
			if ($argueidlist !== '0') {
				$stmt = "SELECT /*!40001 SQL_CACHE */ (CASE WHEN argue.argueid=argue.parentid OR argue.parentid=0 THEN 'PostInfo' ELSE 'ReplyInfo' END) AS _KeyName,".ARGUE_FLD_LIST.",".USER_FLD_LIST." FROM ".TABLE_PREFIX."argue AS argue INNER JOIN ".TABLE_PREFIX."user AS u ON argue.postuserid=u.userid WHERE argue.boardid='".$boardid."' AND argue.rootid='".$rootid."' AND argue.arguetype IN('1','2','3') AND argue.parentid IN(".$argueidlist.") AND argue.postuserid=u.userid ORDER BY argue.boardid ASC, argue.rootid ASC, argue.arguetype ASC, argue.parentid ASC";
				//print $stmt;
				if ($query = $db->query($stmt)) {
					while ($dbinfo =& $db->fetch_array($query)) {
						$cacheinfo['argueinfo'][$dbinfo['arguetype']][$dbinfo['parentid']][$dbinfo['_KeyName']][] =& $dbinfo;
					}//end while
				}
			}
		}
	}
	return $cacheinfo;
}//end function get_top_argumentation_info

function &get_argumentation_info($arguetype, $absolutepage, $rootid = 0, $boardid = 0, $opts = array())	//	读取第n页的辩论数据
{
	if ($boardid <= 0 OR $rootid <= 0 OR ($arguetype > 4 OR $arguetype < 0) OR $absolutepage <= 0) {
		$returnval = false;
		return $returnval;
	}
	static $cacheinfo;
	if (!isset($cacheinfo)) {
		$cacheinfo = false;
		global $db;
		$arguetype = intval($arguetype);
		$rowoffset = ($absolutepage - 1) * PAGE_SIZE;
		$stmt = "(SELECT 1 AS OrderNum, @ParentIDList:=',0') UNION ALL (SELECT 2, @ParentIDList:=CONCAT(@ParentIDList, ',', parentid) FROM ".TABLE_PREFIX."argue WHERE boardid='".$boardid."' AND rootid='".$rootid."' AND arguetype='".$arguetype."' AND parentid=argueid ORDER BY boardid ASC, rootid ASC, arguetype ASC, parentid ASC LIMIT ".$rowoffset.",".PAGE_SIZE.") UNION ALL (SELECT 0, @ParentIDList) ORDER BY OrderNum ASC LIMIT 1";
		if (($query = $db->query($stmt)) AND ($dbinfo =& $db->fetch_row($query))) {
			$parentidlist = str_replace(',0,', '', $dbinfo[1]);
			if (!empty($parentidlist)) {
				$stmt = "SELECT /*!40001 SQL_CACHE */ (CASE WHEN argue.argueid=argue.parentid OR argue.parentid=0 THEN 'PostInfo' ELSE 'ReplyInfo' END) AS _KeyName,".ARGUE_FLD_LIST.",".USER_FLD_LIST." FROM ".TABLE_PREFIX."argue AS argue INNER JOIN ".TABLE_PREFIX."user AS u ON u.userid=argue.postuserid WHERE argue.boardid='".$boardid."' AND argue.rootid='".$rootid."' AND argue.arguetype='".$arguetype."' AND parentid IN(".$db->escape_string($parentidlist).") AND u.userid=argue.postuserid ORDER BY argue.rootid ASC, argue.arguetype ASC, argue.parentid ASC";
				if ($query = $db->query($stmt)) {
					(!isset($opts['convert_ubb']) OR !is_bool($opts['convert_ubb'])) AND ($opts['convert_ubb'] = false);
					while ($dbinfo =& $db->fetch_array($query)) {
						//$dbinfo['CreationDate'] = gmdate('Y-m-d H:i:s', $dbinfo['creation_date'] + getUserTimeZoneOffset());
						$dbinfo['CreationDate'] = $dbinfo['creation_date'];
						if (true === $opts['convert_ubb']) {							
							if(false !== strpos($dbinfo['ubblist'],',39,')){
								$dbinfo['content'] =& Dv_CodeProcess($dbinfo['content'],$dbinfo,$dbinfo['ubblist'],1,0);
							} else{
								$dbinfo['content'] =& Dv_CodeProcess($dbinfo['content'],$dbinfo,$dbinfo['ubblist'],1,1);
							}
						}
						$cacheinfo[$dbinfo['parentid']][$dbinfo['_KeyName']][] =& $dbinfo;
					}//end while
					$db->free_result($query);
				}
			}
		}
	}
	return $cacheinfo;
}//end function get_argumentation_info


/**
 *	$opts = array(
				 'boardid'=>0			--	此辩论主题所属的版面 ID
				,'topic_title'=>''		--	要发布的辩论的主题
				,'postuserid'=>0		--	建立此辩论的用户的userid
				,'postusername'=>null	--	建立此辩论的用户的名称
				,'context_info'=>''		--	背影资料
				,'title_obverse'=>''	--	正方观点的描述
				,'title_reversed'=>''	--	反方观点的描述
				,'title_third'=>''		--	第三方观点的描述
				,'start_time'=>null		--	此辩论主题的开始时间
				,'end_time'=>null		--	此辩论主题的开始时间
				,'expression'=>''		--	魔法表情
				,'lastpost'=>''			--	最后发表
				,'lastposttime'=>''		--	最后发表时间
			)
 */
function post_argumentation_topic($opts = array())	//	发布一个辩论主题
{
	global $db, $lang, $userid, $membername, $boardid;
	$returnval = array('execflag'=>false, 'execmsg'=>'', 'inserted_topicid'=>0);
	!isset($opts['postuserid']) AND ($opts['postuserid'] = USER_ID);
	if ($opts['postuserid'] <= 0) {	//	请登录后进行操作。
		$returnval['execmsg'] = $lang['MustLogin'];
		return $returnval;
	}
	!isset($opts['boardid']) AND ($opts['boardid'] = $boardid);
	if ($opts['boardid'] <= 0) {	//	参数错误——丢失版面参数boardid！。
		$returnval['execmsg'] = $lang['argue_mod.err.str7'];
		return $returnval;
	}
	!isset($opts['postusername']) AND ($opts['postusername'] = MEMBER_NAME);

	!isset($opts['topic_title']) AND ($opts['topic_title'] = '');
	if ('' === $opts['topic_title']) {	//	请填写辩论主题的标题！
		$returnval['execmsg'] = $lang['argue_mod.err.str0'];
		return $returnval;
	} else {
		$opts['topic_title'] = trim($opts['topic_title']);
	}
	!isset($opts['context_info']) AND ($opts['context_info'] = '');
	if ('' === $opts['context_info']) {	//	请填写辩论主题的背景资料！
		$returnval['execmsg'] = $lang['argue_mod.err.str1'];
		return $returnval;
	} else {
		$opts['context_info'] = trim($opts['context_info']);
	}
	!isset($opts['title_obverse']) AND ($opts['title_obverse'] = '');
	if ('' === $opts['title_obverse']) {	//	请设置辩论主题的正方观点描述！
		$returnval['execmsg'] = $lang['argue_mod.err.str2'];
		return $returnval;
	} else {
		$opts['title_obverse'] = trim($opts['title_obverse']);
	}
	!isset($opts['title_reversed']) AND ($opts['title_reversed'] = '');
	if ('' === $opts['title_reversed']) {	//	请设置辩论主题的反方观点描述！
		$returnval['execmsg'] = $lang['argue_mod.err.str3'];
		return $returnval;
	} else {
		$opts['title_reversed'] = trim($opts['title_reversed']);
	}
	$opts['title_third'] = '';	//	第三方观点没我标题描述，应该设为空字符串
	if (!isset($opts['start_time']) OR $opts['start_time'] <= 0) {
		$opts['start_time'] = 0;
	} elseif (isDate($opts['start_time'])) {
		$opts['start_time'] = dv_gmtimestamp($opts['start_time']) - getUserTimeZoneOffset();
	}
	if (!isset($opts['end_time']) OR $opts['end_time'] <= 0) {
		$opts['end_time'] = 0;
	} elseif (isDate($opts['end_time'])) {
		$opts['end_time'] = dv_gmtimestamp($opts['end_time']) - getUserTimeZoneOffset();
	}
	if ($opts['end_time'] < $opts['start_time']) {	//	您要发表的辩论主题的结束时间早于开始时间，请正确设置它们！
		$returnval['execmsg'] = $lang['argue_mod.err.str5'];
		return $returnval;
	} elseif ($opts['end_time'] == $opts['start_time']) {	//	您要发表的辩论主题的结束时间等于开始时间，请正确设置它们！
		$returnval['execmsg'] = $lang['argue_mod.err.str6'];
		return $returnval;
	}
	static $escaped;
	if (!isset($escaped)) {
		if (get_magic_quotes_gpc()) {
			$escaped = true;
		} else {
			$escaped = false;
		}
	}
	$opts['topic_title'] = escapestring(user_stripslashes($opts['topic_title']), false, true);
	$opts['postusername'] = escapestring($opts['postusername'], $escaped, false);
	//$datenow = date('Y-m-d H:i:s');
	$datenow = TIME_NOW;
	$opts['expression'] = '';
	//最后发帖信息，以“$”分隔，“用户名$帖子ID（Announceid）$发表时间$标题信息$帖子类型（可为空）$用户ID（UserID）$帖子主题ID（Topicid）$版块ID（Boardid）”。如“admin$1855846$2005-9-17 18:36:24$动网论坛说明文档$gif$33503$1042023$1”
	if ('' === ($opts['lastpost'] = trim($opts['lastpost']))) {
		$lastpost = $opts['postusername'].'$0$'.$datenow.'$'.$opts['topic_title'].'$$'.$opts['postuserid'].'$0$'.BOARD_ID;
	} else {
		$lastpost = $opts['lastpost'];
	}
	$opts['locktopic'] = 0;

	if ('' === ($opts['lastposttime'] = trim($opts['lastposttime']))) {
		$lastposttime = $datenow;
	} else {
		$lastposttime = $opts['lastposttime'];
	}
	if ($db->query("INSERT INTO ".TABLE_PREFIX."topic(title,boardid,postusername,postuserid,dateandtime,expression,lastpost,lastposttime,posttable,locktopic,isvote) VALUES('".($opts['topic_title'])."', '".$opts['boardid']."', '".$opts['postusername']."', '".$opts['postuserid']."', '".$datenow."', '".$opts['expression']."', '".$lastpost."', '".$lastposttime."', '".TABLE_PREFIX."argue', '".$opts['locktopic']."', '-1')")) {
		if (($insertedid = $db->insert_id()) > 0) {
			$returnval['inserted_topicid'] = $insertedid;
			global $usertrueip;
			if (empty($usertrueip)) {
				$usertrueip = forumusertrueip();
			}
			$opts['title_obverse'] = escapestring(user_stripslashes($opts['title_obverse']), false, true);
			$opts['title_reversed'] = escapestring(user_stripslashes($opts['title_reversed']), false, true);
			$opts['title_third'] = escapestring(user_stripslashes($opts['title_third']), false, true);
			$contextinfo_ubblist = Ubblist($opts['context_info']);
			$opts['context_info'] = escapestring(user_stripslashes($opts['context_info']), false, true);
			$content = $db->escape_string(serialize(array('ArgueIDList'=>array(0=>'', 1=>'', 2=>''), 'context_info'=>$opts['context_info'])));
			$stmt_insert = "INSERT INTO ".TABLE_PREFIX."argue_topic(boardid,rootid,title,title_obverse,title_reversed,title_third,content,creation_date,start_time,end_time,postuserid,postusername,ip,locktopic,ubblist) VALUES('".$opts['boardid']."', '".$insertedid."', '".$opts['topic_title']."', '".$opts['title_obverse']."', '".$opts['title_reversed']."', '".$opts['title_third']."', '".$content."', '".TIME_NOW."', '".$opts['start_time']."', '".$opts['end_time']."', '".$opts['postuserid']."', '".$opts['postusername']."', '".$db->escape_string($usertrueip)."', 0, '".$contextinfo_ubblist."')";
			if ($db->query($stmt_insert)) {
				//更新 dv_topic.lastpost 字段的信息
					if ('' === $opts['lastpost']) {
						$lastpost = $opts['postusername'].'$'.$insertedid.'$'.$datenow.'$'.$opts['topic_title'].'$$'.$opts['postuserid'].'$'.$insertedid.'$'.BOARD_ID;
					}
					$db->query("UPDATE ".TABLE_PREFIX."topic SET lastpost='".$lastpost."' WHERE topicid='".$insertedid."'");
				//更新相关版面的 dv_board.lastpost 信息
					if ('' === $opts['lastpost']) {
						$lastpost = $opts['postusername'].'$'.$insertedid.'$'.$datenow.'$'.$opts['topic_title'].'$dv_argumentation$'.$opts['postuserid'].'$'.$insertedid.'$'.BOARD_ID;
					}
					$db->query("UPDATE ".TABLE_PREFIX."board SET lastpost='".$lastpost."' WHERE boardid='".$opts['boardid']."'");
				//更新 dv_user.userpost
					$db->query("UPDATE ".TABLE_PREFIX."user SET userpost=userpost+1 WHERE userid='".$opts['postuserid']."'");
				$returnval['execflag'] = true;
				$returnval['execmsg'] = $lang['argue_mod.tpl.str0'];
			} else {
				$returnval['execmsg'] = $db->error();
				$db->query("DELETE FROM ".TABLE_PREFIX."topic WHERE topicid='".$insertedid."'", array('type'=>'SILENT'));
				$db->query("DELETE FROM ".TABLE_PREFIX."argue_topic WHERE rootid='".$insertedid."'", array('type'=>'SILENT'));
			}
		}
	}
	return $returnval;
}//end function post_argumentation_topic

function checktimeout()	//	在发表/回复/评论之前检查目标是否已过期
{
	global $lang;
	$returnval = array('execflag'=>false, 'execmsg'=>'');
	$arguetopicinfo =& get_argumentation_topicinfo();
	if (empty($arguetopicinfo)) {	//	您要查找的数据不存在！
		$returnval['execmsg'] = $lang['argue_mod.err.str13'];
		return $returnval;
	}
	//Start	检查开始和结束时间
		$start_time = $arguetopicinfo['start_time'];
		$end_time = $arguetopicinfo['end_time'];
		if ($end_time <= 0 OR $start_time <= 0 OR $end_time <= $start_time) {
			$returnval['execmsg'] = $lang['argue_mod.err.str15'];
			return $returnval;
		}
		//$timestamp = strtotime('now');
		$timestamp = TIME_NOW;
		if ($start_time > $timestamp) {	//	此辩论还未正式开始，您不可以发表观点！
			$returnval['execmsg'] = $lang['argue_mod.err.str16'];
			return $returnval;
		}
		if ($end_time <= $timestamp) {	//	此辩论已结束！
			$returnval['execmsg'] = $lang['argue_mod.err.str17'];
			return $returnval;
		}
	//End	检查开始和结束时间
	$returnval['execflag'] = true;
	return $returnval;
}//end function checktimeout

function othercheck()	//	在发表观点/回复/评论之前检查用当前版面是否已被锁定、用户是否已被锁定/屏蔽等等
{
	global $db, $lang, $userid, $membername, $boardid, $boardinfo, $master, $superboardmaster, $boardmaster;
	$returnval = array('execflag'=>false, 'execmsg'=>'');
	
	if (false === $master AND false === $boardmaster AND false === $boardmaster) {
		$arguetopicinfo =& get_argumentation_topicinfo();
		if (1 == $arguetopicinfo['locktopic']) {	//	本主题已经锁定，不能发表回复。
			$returnval['execmsg'] = $lang['argue_mod.err.str26'];
			return $returnval;
		}
		if((false === $master OR false === $boardmaster) AND 2 == $boardinfo['allowpost']){	//	论坛已锁定
			$returnval['execmsg'] = $lang['argue_mod.err.str27'];
			return $returnval;
		}
		if (1 == $arguetopicinfo['lockuser']) {	//	您的帐号已被锁定
			$returnval['execmsg'] = $lang['argue_mod.err.str28'];
			return $returnval;
		} elseif (2 == $arguetopicinfo['lockuser']) {	//	您的帐号已被屏蔽
			$returnval['execmsg'] = $lang['argue_mod.err.str29'];
			return $returnval;
		}
	}
	$returnval['execflag'] = true;
	return $returnval;
}//end function othercheck

function checkpost()	//	在发表观点/回复/评论、参与投票之前执行检查操作
{
	$returnval = array('execflag'=>false, 'execmsg'=>'');
	$returnval = checktimeout();
	if (false === $returnval['execflag']) {
		return $returnval;
	}
	$returnval = othercheck();
	if (false === $returnval['execflag']) {
		return $returnval;
	}
	$returnval['execflag'] = true;
	return $returnval;
}//end function checkpost

function check_posted_argumentation($boardid, $rootid, $arguetype, $parentid, $postuserid)
{
	global $db, $lang;
	$returnval = array('execflag'=>false, 'execmsg'=>'');

	if ($parentid > 0) {
		$returnval['execflag'] = true;
		return $returnval;
	}
	
	$destarguetype = $db->scalar("SELECT arguetype FROM ".TABLE_PREFIX."argue WHERE boardid='".$boardid."' AND rootid='".$rootid."' AND arguetype<>'".$arguetype."' AND parentid=argueid AND postuserid='".$postuserid."'", array('pageSize'=>1));
	if (false !== $destarguetype AND $destarguetype >= 0) {	//	您已在其它
		if (1 == $destarguetype) {	//	您已在正方发表过观点！您可以尝试发表评论或回复！
			$returnval['execmsg'] = $lang['argue_mod.err.str30'];
			return $returnval;
		} elseif (2 == $destarguetype) {	//	您已在反方发表过观点！您可以尝试发表评论或回复！
			$returnval['execmsg'] = $lang['argue_mod.err.str31'];
			return $returnval;
		} elseif (3 == $destarguetype) {	//	您已在第方发表过观点！您可以尝试发表评论或回复！
			$returnval['execmsg'] = $lang['argue_mod.err.str32'];
			return $returnval;
		}
	}

	$returnval['execflag'] = true;
	return $returnval;
}//end function check_posted_argumentation
/**
 *	$opts = array(
				 'boardid'=>0			--	此辩论主题所属的版面 ID
				,'rootid'=>0			--	对应 dv_topic.topicid 的值
				,'arguetype'=>''		--	为辩论观点，值分别为0,1,2,3，分别表示背景资料、正方、反方和第三方的观点
				,'parentid'=>0			--	父信息的 argueid 的值
				,'argue_title'=>''		--	标题
				,'argue_content'=>''	--	内容
				,'postuserid'=>0		--	用户的userid
				,'postusername'=>null	--	用户的名称
				,'expression'=>''		--	发贴表情文件名信息，同时存储魔法表情信息，格式为：发贴表情文件名|魔法表情ID，等效于帖子的 expression
				,'upfileidlist'=>''			--	如果此值为一个以逗号分隔的数字列表，表明包含附件
			)
 */
function post_argumentation($opts = array())	//	新增一个观点/回复/评论
{
	global $db, $lang, $userid, $membername, $boardid, $boardinfo, $master, $boardmaster;
	$returnval = array('execflag'=>false, 'execmsg'=>'');

	!isset($opts['postuserid']) AND ($opts['postuserid'] = USER_ID);
	if ($opts['postuserid'] <= 0) {	//	请登录后进行操作。
		$returnval['execmsg'] = $lang['MustLogin'];
		return $returnval;
	}
	!isset($opts['boardid']) AND ($opts['boardid'] = BOARD_ID);
	if ($opts['boardid'] <= 0) {	//	参数错误——丢失版面参数boardid！。
		$returnval['execmsg'] = $lang['argue_mod.err.str7'];
		return $returnval;
	}
	(!isset($opts['rootid']) OR !is_numeric($opts['rootid'])) AND ($opts['rootid'] = ROOT_ID);
	if ($opts['rootid'] <= 0) {	//	参数错误——丢失主题参数rootid！
		$returnval['execmsg'] = $lang['argue_mod.err.str8'];
		return $returnval;
	} else {
		$opts['rootid'] += 0;
	}
	if (!isset($opts['arguetype']) OR $opts['arguetype'] < 0 OR $opts['arguetype'] > 4) {	//	参数错误——丢失类型参数arguetype！
		$returnval['execmsg'] = $lang['argue_mod.err.str9'];
		return $returnval;
	} else {
		$opts['arguetype'] += 0;
	}
	if (!isset($opts['parentid']) OR $opts['parentid'] < 0) {	//	参数错误——丢失上一级的主题参数parentid！
		$returnval['execmsg'] = $lang['argue_mod.err.str10'];
		return $returnval;
	} else {
		$opts['parentid'] += 0;
	}

	$arguetopicinfo =& get_argumentation_topicinfo();
	if (empty($arguetopicinfo)) {	//	您要查找的数据不存在！
		$returnval['execmsg'] = $lang['argue_mod.err.str13'];
		return $returnval;
	}

	//	在发表观点/回复/评论、参与投票之前执行检查操作
		$rv = checkpost();
		if (false === $rv['execflag']) {
			$returnval['execmsg'] = $rv['execmsg'];
			return $returnval;
		}

	//	判断用户是否正在发表观点，而不是回复或评论
		if ($opts['parentid'] <= 0) {
			$rv = check_posted_argumentation($opts['boardid'], $opts['rootid'], $opts['arguetype'], $opts['parentid'], $opts['postuserid']);
			if (false === $rv['execflag']) {
				$returnval['execmsg'] = $rv['execmsg'];
				return $returnval;
			}
		}
	
	!isset($opts['postusername']) AND ($opts['postusername'] = MEMBER_NAME);
	!isset($opts['argue_content']) AND ($opts['argue_content'] = '');
	if ('' === $opts['argue_content']) {	//	内容不能为空
		$returnval['execmsg'] = $lang['argue_mod.err.str11'];
		return $returnval;
	} else {
		$opts['argue_content'] = trim($opts['argue_content']);
	}
	static $escaped;
	if (!isset($escaped)) {
		if (get_magic_quotes_gpc()) {
			$escaped = true;
		} else {
			$escaped = false;
		}
	}
	(!isset($opts['argue_title']) AND ($opts['argue_title'] = '')) OR ($opts['argue_title'] = htmlspecialchars(trim($opts['argue_title'])));
	$opts['argue_title'] = escapestring($opts['argue_title'], $escaped, false);
	$content_ubblist = Ubblist($opts['argue_content']);
	$opts['argue_content'] = escapestring(user_stripslashes($opts['argue_content']), false, true);
	$opts['expression'] = escapestring($opts['expression'], $escaped, false);
	$contextinfo_ubblist = Ubblist($opts['argue_content']);
	$opts['postusername'] = escapestring($opts['postusername'], $escaped, false);
	if (isset($opts['upfileidlist']) AND !checkIDList($opts['upfileidlist'])) {
		$opts['upfileidlist'] = '';
		$opts['isupload'] = 0;
	} else {
		$opts['isupload'] = 1;
	}
	$stmt_insert = "INSERT INTO ".TABLE_PREFIX."argue(boardid,rootid,arguetype,parentid,creation_date,postusername,postuserid,`length`,title,content,expression,ubblist,isupload) VALUES('".$opts['boardid']."', '".$opts['rootid']."', '".$opts['arguetype']."', '".$opts['parentid']."', '".TIME_NOW."', '".$opts['postusername']."', '".$opts['postuserid']."', LENGTH('".$opts['argue_content']."'), '".$opts['argue_title']."', '".$opts['argue_content']."', '".$opts['expression']."', '{$contextinfo_ubblist}', '".$opts['isupload']."')";
	if ($db->query($stmt_insert)) {
		$returnval['inserted_argueid'] = $insertedid = $db->insert_id();
		if (1 == intval($opts['isupload'])) {
			$db->query("UPDATE ".TABLE_PREFIX."upfile SET f_announceid='".$opts['rootid']."|".$insertedid."',f_readme='".str_replace("'","''",$opts['argue_title'])."',f_flag=0 WHERE f_id IN(".$opts['upfileidlist'].")");
		}
		$arguetopicinfo['child']++;
		$lastposttime = TIME_NOW;
		$lastpost = $opts['postusername'].'$'.$insertedid.'$'.$lastposttime.'$'.$arguetopicinfo['title'].'$dv_argumentation$'.$opts['postuserid'].'$'.$arguetopicinfo['rootid'].'$'.$opts['boardid'];
		$updateargs = array('update_dv_topic' => array('escaped'=>true, 'cssproced'=>true, 'child_auto_increment'=>true, 'lastpost'=>$lastpost, 'lastposttime'=>$lastposttime, 'hits_sync'=>true));
		if (1 == intval($opts['arguetype'])) {
			$arguetopicinfo['child_obverse']++;
			(0 == $opts['parentid']) AND ($arguetopicinfo['child_obverse_post']++);
			($opts['parentid'] > 0) AND ($arguetopicinfo['child_obverse_reply']++);
			if (empty($arguetopicinfo['content']['ArgueIDList'][0])) {
				$comma = '';
			} else {
				$comma = ',';
			}
			$arguetopicinfo['content']['ArgueIDList'][0] .= $comma.$insertedid;
			update_argumentation_topicinfo($arguetopicinfo, $updateargs);
		} elseif (2 == intval($opts['arguetype'])) {
			$arguetopicinfo['child_reversed']++;
			(0 == $opts['parentid']) AND ($arguetopicinfo['child_reversed_post']++);
			($opts['parentid'] > 0) AND ($arguetopicinfo['child_reversed_reply']++);
			if (empty($arguetopicinfo['content']['ArgueIDList'][1])) {
				$comma = '';
			} else {
				$comma = ',';
			}
			$arguetopicinfo['content']['ArgueIDList'][1] .= $comma.$insertedid;
			update_argumentation_topicinfo($arguetopicinfo, $updateargs);
		} elseif (3 == intval($opts['arguetype'])) {
			$arguetopicinfo['child_third']++;
			(0 == $opts['parentid']) AND ($arguetopicinfo['child_third_post']++);
			($opts['parentid'] > 0) AND ($arguetopicinfo['child_third_reply']++);
			if (empty($arguetopicinfo['content']['ArgueIDList'][2])) {
				$comma = '';
			} else {
				$comma = ',';
			}
			$arguetopicinfo['content']['ArgueIDList'][2] .= $comma.$insertedid;
			update_argumentation_topicinfo($arguetopicinfo, $updateargs);
		}
		if ($insertedid > 0) {
			if (0 == $opts['parentid']) {
				$db->query("UPDATE ".TABLE_PREFIX."argue SET parentid='".$insertedid."' WHERE argueid='".$insertedid."'");
			} else {
				$db->query("UPDATE ".TABLE_PREFIX."argue SET child=child+1 WHERE argueid='".$opts['parentid']."'");
			}
		}
		$returnval['execflag'] = true;
		$returnval['execmsg'] = $lang['argue_mod.tpl.str1'];
	} else {
		$returnval['execflag'] = false;
		$returnval['execmsg'] = $db->error;
	}
	return $returnval;
}//end function post_argumentation

/**
 *	$opts = array(
				 'argueid'=>0			--	要进行补充的观点/回复/评论的 ID
				,'boardid'=>0			--	此辩论主题所属的版面 ID
				,'argue_title'=>''		--	要补充的标题
				,'argue_content'=>''	--	要补充的内容
				,'postuserid'=>0		--	用户的userid
				,'postusername'=>null	--	用户的名称
				,'expression'=>''		--	发贴表情文件名信息，同时存储魔法表情信息，格式为：发贴表情文件名|魔法表情ID，等效于帖子的 expression
				,'upfileidlist'=>''		--	如果此值为一个以逗号分隔的数字列表，表明包含附件
			)
 */
function append_argumentation($opts = array())	//	由于编辑是不合理的，因此只能对所发表的观点/回复/评论进行补充
{
	global $db, $lang, $userid, $membername, $boardid, $master;
	$returnval = array('execflag'=>false, 'execmsg'=>'');

	!isset($opts['postuserid']) AND ($opts['postuserid'] = USER_ID);
	if ($opts['postuserid'] <= 0) {	//	请登录后进行操作。
		$returnval['execmsg'] = $lang['MustLogin'];
		return $returnval;
	}
	if (true !== $master AND $opts['postuserid'] <= 0) {	//	您没有权限编辑别人发表的辩论信息！
		$returnval['execmsg'] = $lang['argue_mod.err.str14'];
		return $returnval;
	}
	!isset($opts['boardid']) AND ($opts['boardid'] = BOARD_ID);
	if ($opts['boardid'] <= 0) {	//	参数错误——丢失版面参数boardid！。
		$returnval['execmsg'] = $lang['argue_mod.err.str7'];
		return $returnval;
	}
	if (!isset($opts['argueid']) OR $opts['argueid'] <= 0) {	//	参数错误——丢失参数argueid！
		$returnval['execmsg'] = $lang['argue_mod.err.str12'];
		return $returnval;
	} else {
		$opts['argueid'] += 0;
	}

	$arguetopicinfo =& get_argumentation_topicinfo();
	if (empty($arguetopicinfo)) {	//	您要查找的数据不存在！
		$returnval['execmsg'] = $lang['argue_mod.err.str13'];
		return $returnval;
	}

	//	在发表观点/回复/评论、参与投票之前执行检查操作
		$rv = checkpost();
		if (false === $rv['execflag']) {
			$returnval['execmsg'] = $rv['execmsg'];
			return $returnval;
		}
	

	!isset($opts['postusername']) AND ($opts['postusername'] = MEMBER_NAME);
	!isset($opts['argue_content']) AND ($opts['argue_content'] = '');
	if ('' === $opts['argue_content']) {	//	内容不能为空
		$returnval['execmsg'] = $lang['argue_mod.err.str11'];
		return $returnval;
	} else {
		$opts['argue_content'] = trim($opts['argue_content']);
	}
	static $escaped;
	if (!isset($escaped)) {
		if (get_magic_quotes_gpc()) {
			$escaped = true;
		} else {
			$escaped = false;
		}
	}
	$rs =& $db->getResultSet("SELECT /*!40001 SQL_CACHE */ title, content, expression FROM ".TABLE_PREFIX."argue WHERE argueid='".$opts['argueid']."' AND boardid='".$opts['boardid']."'");
	if (false === $rs) {	//	您要查找的数据不存在！
		$returnval['execmsg'] = $lang['argue_mod.err.str13'];
		return $returnval;
	}
	$argueinfo =& $rs[0];
	$opts['argue_content'] = cross_site_html_proc(user_stripslashes($opts['argue_content']));
	$argueinfo['content'] .= $db->escape_string(str_replace(array('{$TPL_USERNAME}', '{$TPL_DATE}', '{$TPL_APPEND_CONTENT}'), array($opts['postusername'], '[lastedittime]'.TIME_NOW.'[/lastedittime]', $opts['argue_content']), $lang['argue_mod.tpl.append_separator']));
	$content_ubblist = Ubblist($argueinfo['content']);
	$argueinfo['expression'] = $opts['expression'];
	if (isset($opts['upfileidlist']) AND !checkIDList($opts['upfileidlist'])) {
		$opts['upfileidlist'] = '';
		$opts['isupload'] = 0;
	} else {
		$opts['isupload'] = 1;
	}
	$stmt_insert = "UPDATE ".TABLE_PREFIX."argue SET content='".$argueinfo['content']."', expression='".$argueinfo['expression']."', `length`=LENGTH(content), ubblist='".$content_ubblist."',isupload='".$opts['isupload']."' WHERE argueid='".$opts['argueid']."' AND boardid='".$opts['boardid']."'";
	if ($db->query($stmt_insert)) {
		if (1 == intval($opts['isupload'])) {
			$db->query("UPDATE ".TABLE_PREFIX."upfile SET f_announceid='".$opts['rootid']."|".$insertedid."',f_readme='".str_replace("'","''",$opts['argue_title'])."',f_flag=0 WHERE f_id IN(".$opts['upfileidlist'].")");
		}
		$returnval['execflag'] = true;
		$returnval['execmsg'] = $lang['argue_mod.tpl.str1'];
	} else {
		$returnval['execflag'] = false;
		$returnval['execmsg'] = $db->error;
	}
	return $returnval;
}//end function append_argumentation

function argumentation_post_vote($argueid, $voteuserid = 0, $rootid = 0, $boardid = 0)	//	执行投票操作
{
	global $db, $lang, $membername, $boardid, $userinfo, $master, $superboardmaster, $boardmaster;
	$returnval = array('execflag'=>false, 'execmsg'=>'');

	(!is_numeric($voteuserid) OR ($voteuserid <= 0)) AND ($voteuserid = USER_ID);
	if ($voteuserid <= 0) {	//	请登录后进行操作。
		$returnval['execmsg'] = $lang['MustLogin'];
		return $returnval;
	} else {
		$voteuserid += 0;
	}
	if (!is_numeric($argueid) OR ($argueid <= 0)) {	//	参数错误——丢失参数argueid！
		$returnval['execmsg'] = $lang['argue_mod.err.str12'];
		return $returnval;
	}
	(!is_numeric($rootid) OR ($rootid <= 0)) AND ($rootid = ROOT_ID);
	if ($rootid <= 0) {	//	参数错误——丢失主题参数rootid！
		$returnval['execmsg'] = $lang['argue_mod.err.str8'];
		return $returnval;
	} else {
		$rootid += 0;
	}
	!isset($opts['boardid']) AND ($opts['boardid'] = BOARD_ID);
	if ($opts['boardid'] <= 0) {	//	参数错误——丢失版面参数boardid！。
		$returnval['execmsg'] = $lang['argue_mod.err.str7'];
		return $returnval;
	}
	$topicinfo =& get_argumentation_topicinfo($rootid, $boardid);
	if (empty($topicinfo)) {	//	您要查找的数据不存在！
		$returnval['execmsg'] = $lang['argue_mod.err.str13'];
		return $returnval;
	}

	//	在发表观点/回复/评论、参与投票之前执行检查操作
		$rv = checkpost();
		if (false === $rv['execflag']) {
			$returnval['execmsg'] = $rv['execmsg'];
			return $returnval;
		}

	//初始化投票相关参数
		$uarticle = intval($topicinfo['content']['VoteOptions'][0]);	//	限制投票文章数
		$uwealth = intval($topicinfo['content']['VoteOptions'][1]);		//	限制投票金钱数
		$uep = intval($topicinfo['content']['VoteOptions'][2]);			//	限制投票经验数
		$ucp = intval($topicinfo['content']['VoteOptions'][3]);			//	限制投票魅力数
		$upower = intval($topicinfo['content']['VoteOptions'][4]);		//	限制投票威望数

	//Start 检查文章、金钱、经验、魅力、威望
		if (false === $master AND false === $superboardmaster AND false === $boardmaster) {
			if ($uarticle > $userinfo['userpost']) { // 检查文章
				$returnval['execmsg'] = str_replace('{$Tpl_UArticle}', $uarticle, $lang['err.str20']);
				return $returnval;
			}
			if ($uwealth > $userinfo['userwealth']) { // 检查金钱
				$returnval['execmsg'] = str_replace('{$Tpl_UWealth}', $uwealth, $lang['err.str21']);
				return $returnval;
			}
			if ($uep > $userinfo['userep']) { // 检查经验
				$returnval['execmsg'] = str_replace('{$Tpl_UEP}', $userep, $lang['err.str22']);
				return $returnval;
			}
			if ($ucp > $userinfo['usercp']) { // 检查魅力
				$returnval['execmsg'] = str_replace('{$Tpl_UCP}', $usercp, $lang['err.str23']);
				return $returnval;
			}
			if ($upower > $userinfo['userpower']) { // 检查威望
				$returnval['execmsg'] = str_replace('{$Tpl_UPower}', $userpower, $lang['err.str24']);
				return $returnval;
			}
		}
	//End 检查文章、金钱、经验、魅力、威望

	$rs =& $db->getResultSet("SELECT postuserid,voteid,arguetype,vote,(CASE WHEN title IS NOT NULL AND title<>'' THEN title ELSE content END) AS title FROM ".TABLE_PREFIX."argue /*!32312 USE INDEX(PRIMARY) */ WHERE argueid='".$argueid."' AND rootid='".$rootid."' AND arguetype IN('0', '1', '2', '3') AND parentid='".$argueid."' AND boardid='".$boardid."'");
	if (empty($rs)) {	//	您要查找的数据不存在！
		$returnval['execmsg'] = $lang['argue_mod.err.str13'];
		return $returnval;
	}
	$argueinfo =& $rs[0];
	unset($rs);
	if ($voteuserid == $argueinfo['postuserid']) {	//	不可以给自己投票！
		$returnval['execmsg'] = $lang['argue_mod.err.str25'];
		return $returnval;
	}
	$returnval['arguetype'] = $argueinfo['arguetype'];
	if ($argueinfo['voteid'] <= 0) {
		//$timeout = date('Y-m-d H:i:s', $topicinfo['end_time']);
		$timeout = $topicinfo['end_time'];
		$db->query("INSERT INTO ".TABLE_PREFIX."vote(voteid,vote,votenum,votetype,timeout,uarticle,uwealth,uep,ucp,upower) VALUES(NULL, '".$db->escape_string($argueinfo['title'])."', '', '0', '".$db->escape_string($timeout)."', '".$uarticle."', '".$uwealth."', '".$uep."', '".$ucp."', '".$upower."')");
		$voteid = $db->insert_id();
	} else {
		$voteid = $argueinfo['voteid'];
		$voteoption = $db->scalar("SELECT voteoption FROM ".TABLE_PREFIX."voteuser WHERE voteid='".$voteid."' AND userid='".$voteuserid."'");
		if (false !== $voteoption AND (!is_numeric($voteoption) OR $voteoption <= 0)) {	//	您已经投过票了。
			$returnval['execmsg'] = $lang['argue_mod.err.str19'];
			return $returnval;
		}
	}
	//更新 dv_argue 表的 vote相关字段
		$db->query("UPDATE ".TABLE_PREFIX."argue SET vote=vote+1, voteid='".$voteid."' WHERE argueid='".$argueid."'");
		$argueinfo['vote']++;
	$returnval['vote_personal'] = $argueinfo['vote'];
	//	插入数据到 dv_voteuser 表
		$db->query("INSERT INTO ".TABLE_PREFIX."voteuser(voteid,userid,voteoption,votedate) VALUES('".$voteid."', '".$voteuserid."', '0', '".TIME_NOW."')");
	$topicinfo['vote']++;
	$arguetype = $argueinfo['arguetype'];
	(1 == intval($arguetype)) AND ($topicinfo['vote_obverse'] += 1);
	(2 == intval($arguetype)) AND ($topicinfo['vote_reversed'] += 1);
	(3 == intval($arguetype)) AND ($topicinfo['vote_third'] += 1);
	//	更新辩论主题表的投票相关字段
		//$db->query("UPDATE ".TABLE_PREFIX."argue_topic SET vote=vote+1, vote_obverse='".$topicinfo['vote_obverse']."', vote_reversed='".$topicinfo['vote_reversed']."', vote_third='".$topicinfo['vote_third']."' WHERE rootid='".$rootid."'");
		update_argumentation_topicinfo($topicinfo);
	//	更新 dv_topic.votetotal 字段
		$db->query("UPDATE ".TABLE_PREFIX."topic SET votetotal=votetotal+1 WHERE topicid='".$topicinfo['rootid']."'");
	$returnval['execflag'] = true;
	return $returnval;
}//end function argumentation_post_vote

function &escapestring(&$str, $escaped = false, $usecssproc = true)
{
	global $db;
	$str = trim($str);
	if ('' === $str) {
		return $str;
	}
	if (true === $usecssproc) {
		$errcode = $errstr = '';
		if (true === $escaped) {
			$str = stripslashes($str);
			$escaped = false;
		}
		$str =& cross_site_html_proc($str, $errcode, $errstr);
	}
	if (false === $escaped) {
		$str = $db->escape_string($str);
	}
	return $str;
}//end function escapestring

function user_stripslashes(&$str, $escaped = null)
{
	global $magic_quotes_gpc;
	!is_bool($escaped) AND ($escaped = true);
	if (true === $escaped) {
		return stripslashes($str);
	} else {
		return $str;
	}
}//end function user_stripslashes

?>