<?php
$cfg_needFilter = TRUE;

require_once './include/common.inc.php';
if(!isset($action)) $action = '';
if(empty($uid) && $action != 'rate')
{
	showmsgs('no_login','../member/login.php?gourl=../ask/');
	exit;
}

//词语过滤也应该在此处处理
$title = isset($title) ? ihtmlspecialchars(trim($title)) : '';
$brief = isset($brief) ? ihtmlspecialchars(trim($brief)) : '';
$content = isset($content) ? trim($content) : '';
$extra = isset($extra) ? trim($extra) : '';


if(empty($action))
{
	//问答分类
	$query = "select id, name, reid from {$cfg_dbprefix}asktype order by disorder desc";
	$dsql->Execute('me',$query);
	$tids = "var class_level_1=new Array( \n";
	$tid2s = "var class_level_2=new Array( \n";
	while($asktype = $dsql->getarray())
	{
		if($asktype['reid'] == 0)
		{
			$tids .= 'new Array("'.$asktype['id'].'","'.$asktype['name'].'"),'."\n";
		}else
		{
			$tid2s .= 'new Array("'.$asktype['reid'].'","'.$asktype['id'].'","'.$asktype['name'].'"),'."\n";
		}

	}
	$tids = substr($tids,0,-2)."\n";
	$tid2s = substr($tid2s,0,-2)."\n";
	$tids .= ');';
	$tid2s .= ');';

	$navtitle = $sitename.' 提问';
	$nav = '<a href="'.$indexname.'">'.$sitename.'</a> '.$symbols.' 提问';
  //print_r($tids);
	include DEDE.'./template/default/post.htm';
	$dsql->Close();
/*
function extra();
*/
}elseif($action == 'extra')
{//补充问题
	$dsql->Execute('me',"SELECT id, uid, dateline, expiredtime, solvetime, extra FROM {$cfg_dbprefix}ask WHERE id='$id' and status='0'");

	if($question = $dsql->getarray()) {
		if($question['uid'] != $uid) {
			showmsgs('unallowed_action');
		}elseif($question['expiredtime'] < $timestamp) {
			showmsgs('question_expired');
		}
	} else {
		showmsgs('question_nonexistence');
	}
	if(!empty($step)) {
		 	if(empty($extra)) {
				showmsgs('post_extra_isnull');
			}
			if(strlen($extra) > 10000) {
				showmsgs('post_extra_toolong');
			}

			$dsql->ExecuteNoneQuery("update {$cfg_dbprefix}ask set extra='$extra' where id='$id'");
			$dsql->Close();
			@iheader("Expires: 0");
			@iheader("Cache-Control: private, post-check=0, pre-check=0, max-age=0", FALSE);
			@iheader("Pragma: no-cache");

			echo '<script language="javascript">
				window.opener.location.reload();
				self.close();
			</script>
			';
	}else{
			include DEDE.'./template/default/post.extra.htm';
			$dsql->Close();
	}
/*
function upreward();
*/
}elseif($action == 'upreward')
{//提高悬赏
	$question = $dsql->getone("SELECT id, uid, dateline, solvetime, status, expiredtime
	FROM {$cfg_dbprefix}ask WHERE id='$id' and status='0'");

	if($question) {
		if($question['uid'] != $uid) {
			showmsgs('unallowed_action');
		}elseif($question['expiredtime'] < $timestamp) {
			showmsgs('question_expired');
		}
	} else {
		showmsgs('question_nonexistence');
	}

	if(empty($step)){
		include DEDE.'./template/default/post.upreward.htm';
	}else{
		$upreward = intval($upreward);
		$upreward = max(0,$upreward);

		if($upreward > $scores) {
			showmsgs('noscore');
		}

		$dsql->ExecuteNoneQuery("UPDATE {$cfg_dbprefix}member SET scores=scores-$upreward WHERE ID='$uid'");
		$cfg_ml->FushCache();
		$dsql->ExecuteNoneQuery("update {$cfg_dbprefix}ask set reward=reward+$upreward where id='$id'");
		$dsql->Close();
		@iheader("Expires: 0");
		@iheader("Cache-Control: private, post-check=0, pre-check=0, max-age=0", FALSE);
		@iheader("Pragma: no-cache");

		echo '<script language="javascript">
			window.opener.location.reload();
			self.close();
		</script>
		';

	}
/*
function modifyanswer();
*/
}elseif($action == 'modifyanswer')
{//修改答案
	$answer = $dsql->getone("SELECT answer.id, answer.uid, ask.dateline, ask.solvetime, ask.status, ask.expiredtime
	FROM {$cfg_dbprefix}askanswer answer left join {$cfg_dbprefix}ask ask on ask.id=answer.askid WHERE answer.id='$id'");

	if($answer) {
		if($answer['uid'] != $uid) {
			showmsgs('unallowed_action', $backurl);
		}elseif($answer['status'] != 0) {
			showmsgs('question_solved', $backurl);
		}elseif($answer['expiredtime'] < $timestamp) {
			showmsgs('question_expired', $backurl);
		}
	} else {
		showmsgs('question_nonexistence');
	}

	if(trim($content) == '') {
		showmsgs('post_answer_isnull');
	}
	if(strlen($content) > 10000) {
		showmsgs('post_answer_toolong');
	}
	if(strlen($brief) > 200) {
		showmsgs('post_brief_toolong', '-1');
	}

	if($dsql->ExecuteNoneQuery("update {$cfg_dbprefix}askanswer set content='$content', brief='$brief' where id='$id'")) {
		showmsgs('modifyanswer_success',$backurl);
	}else{
		showmsgs('modifyanswer_failed',$backurl);
	}
/*
function adopt();
*/
}elseif($action == 'adopt')
{//采纳答案
	$answer = $dsql->getone("SELECT answer.id, answer.askid, answer.uid as answeruid, ask.uid, ask.tid, ask.tid2,
	ask.reward, ask.dateline, ask.solvetime, ask.status, ask.expiredtime
	FROM {$cfg_dbprefix}askanswer answer left join {$cfg_dbprefix}ask ask on ask.id=answer.askid WHERE answer.id='$id'");

	if($step != 2){
		if($answer) {
			if($answer['uid'] != $uid) {
				showmsgs('unallowed_action', $backurl);
			}elseif($answer['status'] != 0) {
				showmsgs('question_solved', $backurl);
			}elseif($answer['expiredtime'] < $timestamp) {
				showmsgs('question_expired', $backurl);
			}
		} else {
			showmsgs('question_nonexistence');
		}
		$dsql->Close();
		include DEDE.'./template/default/post.adopt.htm';
	}else{
		$extrareward = intval($extrareward);
		$extrareward = max(0,$extrareward);
		$content = trim($content);
		if($extrareward > $scores) {
			showmsgs('noscore');
		}

		$reward = $extrareward + $answer['reward'] + $cfg_ask_bestanswer;//额外奖励+提问奖励+系统奖励

		$dsql->ExecuteNoneQuery("UPDATE {$cfg_dbprefix}member SET scores=scores+$reward WHERE ID='$answer[answeruid]'");
		$cfg_ml->FushCache();
		$dsql->ExecuteNoneQuery("update {$cfg_dbprefix}ask set solvetime='$timestamp', status='1', bestanswer='$answer[id]' where id='$answer[askid]'");

		$userip = getip();

		if($content != '') {//写入感谢语
			$dsql->ExecuteNoneQuery("insert into {$cfg_dbprefix}askanswer (askid, ifanswer, tid, tid2, uid, username, userip, dateline, content)
			values('$answer[askid]', '0', '$answer[tid]', '$answer[tid2]', '$uid', '$username', '$userip', '$timestamp', '$content')");
		}
		$dsql->Close();
		@iheader("Expires: 0");
		@iheader("Cache-Control: private, post-check=0, pre-check=0, max-age=0", FALSE);
		@iheader("Pragma: no-cache");

		echo '<script language="javascript">
			window.opener.location.reload();
			self.close();
		</script>
		';
	}
/*
function comment();
*/
}elseif($action == 'comment')
{//对最佳答案的评论
	if(trim($content) == '') {
		showmsgs('post_comment_isnull');
	}
	if(strlen($content) > 10000) {
		showmsgs('post_comment_toolong');
	}

	$digestanswer = $dsql->getone("select answer.askid, answer.tid, answer.tid2, ask.bestanswer
	from {$cfg_dbprefix}askanswer answer left join {$cfg_dbprefix}ask ask on ask.id=answer.askid where answer.id='$id'");
	if($digestanswer && $digestanswer['bestanswer'] == $id){
		$tid = $digestanswer['tid'];
		$askid = $digestanswer['askid'];
		$tid2 = $digestanswer['tid2'];
		$userip = getip();
	}else{
		showmsgs('unallowed_action');
	}

	$dsql->ExecuteNoneQuery("insert into {$cfg_dbprefix}askanswer (askid, ifanswer, tid, tid2, uid, username, userip, dateline, content)
	values('$askid', '0', '$tid', '$tid2', '$uid', '$username', '$userip', '$timestamp', '$content')");

	showmsgs('post_comment_succeed',"question.php?id=$askid");
/*
function rate();
*/
}elseif($action == 'rate')
{//对最佳答案的评价
	if($type == 'bad'){
		$rate = 'badrate';
	}else{
		$rate = 'goodrate';
	}
	$cookiename = 'rated'.$id;

	if(!isset(${$cookiename})){${$cookiename} = 0;}
	if((!${$cookiename} == $id)) {
		$dsql->ExecuteNoneQuery("update {$cfg_dbprefix}askanswer set $rate=$rate+1 where id='$id'");
		makecookie($cookiename,$id,3600);
	}
	$row = $dsql->getone("select goodrate, badrate from {$cfg_dbprefix}askanswer where id='$id'");

	$goodrate = $row['goodrate'];
	$badrate = $row['badrate'];
	if(($goodrate + $badrate) > 0){
		$goodrateper = ceil($goodrate*100/($badrate+$goodrate));
		$badrateper = 100-$goodrateper;
	}else{
		$goodrateper = $badrateper = 0;
	}
	$dsql->Close();
	AjaxHead();
?>
				<dl>
					<dt><strong>您觉得最佳答案好不好？ </strong><br>   目前有 <?php echo $row['goodrate']+$row['badrate']; ?> 个人评价</dt>
					<dd>
						<a href="#"  onCLick="rate('mark',<?php echo $id; ?>,'good')"><img src="template/default/images/mark_g.gif" width="14" height="16" />好</a>
						<span><?php echo $goodrateper; ?>% (<?php echo $goodrate; ?>)</span>
					</dd>
					<dd>
						<a href="#"  onCLick="rate('mark',<?php echo $id; ?>,'bad')"><img src="template/default/images/mark_b.gif" width="14" height="16" />不好</a>
						<span><?php echo $badrateper; ?>% (<?php echo $badrate; ?>)</span>
					</dd>
				</dl>
<?php
/*
function ask();
*/
}elseif($action == 'ask')
{//提问问题
	if($title == '') {
		showmsgs('post_title_isnull');
	}

	if(strlen($title) > 80) {
		return 'post_title_toolong';
	}
	if(strlen($content) > 10000) {
		showmsgs('post_askcontent_toolong');
	}
	$anonymous = !empty($anonymous) ? 1 : 0;
	$tid = $tid2 = 0;
	$tidname = $tid2name = '';
	$userip = getip();
	$reward = intval($reward);
	if($reward < 0){
		$reward = 0;
	}
	$needscore = $anonymous * 10 + $reward;
	if($scores < $needscore) {
		showmsgs('noscore','-1');
	}
	$ClassLevel1 = intval($ClassLevel1);
	if($ClassLevel1 < 1) {
		showmsgs('browser_error','-1');
	}
	$ClassLevel2 = intval($ClassLevel2);
	if($ClassLevel2 != 0)
	{
		$where = "id in ($ClassLevel1,$ClassLevel2)";
	}else{
		$where = "id='$ClassLevel1'";
	}
	$query = "select id, name, reid from {$cfg_dbprefix}asktype where $where";

	$dsql->Execute('me',$query);
	while($row = $dsql->getarray())
	{
		if($row['id'] == $ClassLevel1)
		{
			$tidname = $row['name'];
			$tid = $row['id'];
		}elseif($row['id'] == $ClassLevel2 && $row['reid'] == $ClassLevel1)
		{
			$tid2name = $row['name'];
			$tid2 = $row['id'];
		}
	}
	$expiredtime = $timestamp + 86400 * $cfg_ask_expiredtime;
if($cfg_ask_ifcheck == 'Y'){
		$dsql->ExecuteNoneQuery("insert into {$cfg_dbprefix}ask(tid, tidname, tid2, tid2name, uid, anonymous, status,
								title, reward, dateline, expiredtime, ip ,content, extra)
						values('$tid', '$tidname', '$tid2', '$tid2name', '$uid', '$anonymous', '-1', '$title', '$reward',
							'$timestamp', '$expiredtime', '$userip', '$content', '')");
	}else{
		$dsql->ExecuteNoneQuery("insert into {$cfg_dbprefix}ask(tid, tidname, tid2, tid2name, uid, anonymous,
								title, reward, dateline, expiredtime, ip ,content, extra)
						values('$tid', '$tidname', '$tid2', '$tid2name', '$uid', '$anonymous', '$title', '$reward',
							'$timestamp', '$expiredtime', '$userip', '$content', '')");
	}
	$dsql->ExecuteNoneQuery("UPDATE {$cfg_dbprefix}asktype SET asknum=asknum+1 WHERE id='$tid'");

	if($tid2 > 0){
		$dsql->ExecuteNoneQuery("UPDATE {$cfg_dbprefix}asktype SET asknum=asknum+1 WHERE id='$tid2'");
	}

	$dsql->ExecuteNoneQuery("UPDATE {$cfg_dbprefix}member SET scores=scores-$needscore WHERE ID='$uid'");
	$cfg_ml->FushCache();

	showmsgs('post_newask_succeed', "browser.php?tid=$tid");
/*
function answer();
*/
}elseif($action == 'answer')
{//回答问题
	$dsql->Execute('me',"SELECT id, tid, tid2, uid, dateline, expiredtime, solvetime FROM {$cfg_dbprefix}ask WHERE id='$id' and status='0'");

	if($question = $dsql->getarray())
	{
		if($question['uid'] == $uid) {
			showmsgs('asker_cannot_answer', '-1');
		}elseif($question['expiredtime'] < $timestamp) {
			showmsgs('question_expired');
		}
		$tid = $question['tid'];
		$tid2 = $question['tid2'];
		$askid = $question['id'];
		$userip = getip();
	} else {
		showmsgs('question_nonexistence');
	}

	$anonymous = $anonymous ? 1 : 0;

	if($content == '') {
		showmsgs('post_answer_isnull');
	}
	if(strlen($content) > 10000) {
		showmsgs('post_answer_toolong');
	}
	if(strlen($brief) > 200) {
		showmsgs('post_brief_toolong', '-1');
	}
	if($cfg_answer_ifcheck == 'Y'){
		$dsql->ExecuteNoneQuery("insert into {$cfg_dbprefix}askanswer (askid, ifanswer, tid, tid2, uid, username, anonymous, userip, brief, dateline, content, ifcheck)
			values('$askid', '1', '$tid', '$tid2', '$uid', '$username', '$anonymous', '$userip', '$brief', '$timestamp', '$content', '0')");
}else{
		$dsql->ExecuteNoneQuery("insert into {$cfg_dbprefix}askanswer (askid, ifanswer, tid, tid2, uid, username, anonymous, userip, brief, dateline, content)
			values('$askid', '1', '$tid', '$tid2', '$uid', '$username', '$anonymous', '$userip', '$brief', '$timestamp', '$content')");
}
	$dsql->ExecuteNoneQuery("update {$cfg_dbprefix}ask set replies=replies+1 where id='$askid'");
	$cfg_ask_answerscore = intval($cfg_ask_answerscore);
	$dsql->ExecuteNoneQuery("update {$cfg_dbprefix}member set scores=scores+$cfg_ask_answerscore where ID='$uid'"); //回答者先加分
	$cfg_ml->FushCache();
	showmsgs('post_answer_succeed',"question.php?id=$askid");
/*
function toend();
*/
}elseif($action == 'toend')
{//无满意答案，结束问题
	$dsql->ExecuteNoneQuery("update {$cfg_dbprefix}ask set solvetime='$timestamp', status='1' where uid='$uid' and id='$id'");
	$dsql->Close();
	@iheader("Expires: 0");
	@iheader("Cache-Control: private, post-check=0, pre-check=0, max-age=0", FALSE);
	@iheader("Pragma: no-cache");

	echo '<script language="javascript">
		window.opener.location.reload();
		self.close();
	</script>
	';
}


?>