<?php
require_once(dirname(__FILE__)."/../include/common.inc.php");
require_once(DEDEINC."/filter.inc.php");
require_once(DEDEINC."/channelunit.func.php");
if(!isset($action))
{
	$action = '';
}
//兼容舊的JS代碼
if($action == 'good' || $action == 'bad')
{
	if(!empty($aid)) $id = $aid;
	require_once(dirname(__FILE__).'/digg_ajax.php');
	exit();
}


function GetOnebook($aid)
{
	global $dsql,$title;
	$aid = trim(preg_replace('#[^0-9]#i','',$aid));
	$reArr = array();

	$nquery = "Select * From `#@__story_books` where bid='$aid' ";
	
	$arcRow = $dsql->GetOne($nquery);
	if(!is_array($arcRow)) {
		return $reArr;
	}
	$reArr = $arcRow;
	$reArr['bid']    = $aid;
	$reArr['arctitle'] = $arcRow['bookname'];
	$title = $arcRow['bookname'];
	//$reArr['arcurl'] = GetFileUrl($aid,$arcRow['typeid'],$arcRow['senddate'],$reArr['bookname '],$arcRow['ismake'],$arcRow['arcrank'],$arcRow['namerule'],
	//$arcRow['typedir'],$arcRow['money'],$arcRow['filename'],$arcRow['moresite'],$arcRow['siteurl'],$arcRow['sitepath']);
	return $reArr;

}


$cfg_formmember = isset($cfg_formmember) ? true : false;
$ischeck = $cfg_feedbackcheck=='Y' ? 0 : 1;
$aid = (isset($aid) && is_numeric($aid)) ? $aid : 0;
$fid = (isset($fid) && is_numeric($fid)) ? $fid : 0;
if(empty($aid) && empty($fid))
{
	ShowMsg('文檔id不能為空!','-1');
	exit();
}

include_once(DEDEINC."/memberlogin.class.php");
$cfg_ml = new MemberLogin();

if($action=='goodfb')
{
	AjaxHead();
	$fid = intval($fid);
	$dsql->ExecuteNoneQuery("Update `#@__bookfeedback` set good = good+1 where id='$fid' ");
	$row = $dsql->GetOne("Select good From `#@__bookfeedback` where id='$fid' ");
	echo "<a onclick=\"postBadGood('goodfb',{$aid})\">支持</a>[{$row['good']}]";
	exit();
}
else if($action=='badfb')
{
	AjaxHead();
	$fid = intval($fid);
	$dsql->ExecuteNoneQuery("Update `#@__bookfeedback` set bad = bad+1 where id='$fid' ");
	$row = $dsql->GetOne("Select bad From `#@__bookfeedback` where id='$fid' ");
	echo "<a onclick=\"postBadGood('badfb',{$aid})\">反對</a>[{$row['bad']}]";
	exit();
}
//查看評論
/*
function __ViewFeedback(){ }
*/
//-----------------------------------
else if($action=='' || $action=='show')
{
	//讀取文檔信息
	$arcRow = GetOnebook($aid);
	if(empty($arcRow['bid']))
	{
		ShowMsg('無法查看未知文檔的評論!','-1');
		exit();
	}
	extract($arcRow, EXTR_SKIP);	
	include_once(DEDEINC.'/datalistcp.class.php');
	$dlist = new DataListCP();
	$dlist->pageSize = 20;

	if(empty($ftype) || ($ftype!='good' && $ftype!='bad' && $ftype!='feedback'))
	{
		$ftype = '';
	}
	$wquery = $ftype!='' ? " And ftype like '$ftype' " : '';

	//評論內容列表
	$querystring = "select fb.*,mb.userid,mb.face as mface,mb.spacesta,mb.scores from `#@__bookfeedback` fb
                 left join `#@__member` mb on mb.mid = fb.mid
                 where fb.aid='$aid' and fb.ischeck='1' $wquery order by fb.id desc";
	$dlist->SetParameter('aid',$aid);
	$dlist->SetParameter('action','show');
	$dlist->SetTemplate($cfg_basedir.$cfg_templets_dir.'/plus/bookfeedback_templet.htm');
	$dlist->SetSource($querystring);
	$dlist->Display();
	exit();
}

//引用評論
//------------------------------------
/*
function __Quote(){ }
*/
else if($action=='quote')
{
	$row = $dsql->GetOne("Select * from `#@__bookfeedback` where id ='$fid'");
	require_once(DEDEINC.'/dedetemplate.class.php');
	$dtp = new DedeTemplate();
	$dtp->LoadTemplate($cfg_basedir.$cfg_templets_dir.'/plus/bookfeedback_quote.htm');
	$dtp->Display();
	exit();
}
//發表評論
//------------------------------------
/*
function __SendFeedback(){ }
*/
else if($action=='send')
{
	//讀取文檔信息
	$arcRow = GetOnebook($aid);
	if((empty($arcRow['bid']) || $arcRow['notpost']=='1')&&empty($fid))
	{
		ShowMsg('無法對該文檔發表評論!','-1');
		exit();
	}

	//是否加驗證碼重確認
	if(empty($isconfirm))
	{
		$isconfirm = '';
	}
	if($isconfirm!='yes' && $cfg_feedback_ck=='Y')
	{
		extract($arcRow, EXTR_SKIP);
		require_once(DEDEINC.'/dedetemplate.class.php');
		$dtp = new DedeTemplate();
		$dtp->LoadTemplate($cfg_basedir.$cfg_templets_dir.'/plus/bookfeedback_confirm.htm');
		$dtp->Display();
		exit();
	}
	//檢查驗證碼
	if($cfg_feedback_ck=='Y')
	{
		$validate = isset($validate) ? strtolower(trim($validate)) : '';
		$svali = strtolower(trim(GetCkVdValue()));
		if($validate != $svali || $svali=='')
		{
			ResetVdValue();
			ShowMsg('驗證碼錯誤！','-1');
			exit();
		}
	}

	//檢查用戶登錄
	if(empty($notuser))
	{
		$notuser=0;
	}

	//匿名發表評論
	if($notuser==1)
	{
		$username = $cfg_ml->M_ID > 0 ? '匿名' : '遊客';
	}

	//已登錄的用戶
	else if($cfg_ml->M_ID > 0)
	{
		$username = $cfg_ml->M_UserName;
	}

	//用戶身份驗證
	else
	{
		if($username!='' && $pwd!='')
		{
			$rs = $cfg_ml->CheckUser($username,$pwd);
			if($rs==1)
			{
				$dsql->ExecuteNoneQuery("Update `#@__member` set logintime='".time()."',loginip='".GetIP()."' where mid='{$cfg_ml->M_ID}'; ");
			}
			else
			{
				$username = '遊客';
			}
		}
		else
		{
			$username = '遊客';
		}
	}
	$ip = GetIP();
	$dtime = time();
	
	//檢查評論間隔時間；
	if(!empty($cfg_feedback_time))
	{
		//檢查最後發表評論時間，如果未登陸判斷當前IP最後評論時間
		if($cfg_ml->M_ID > 0)
		{
			$where = "WHERE `mid` = '$cfg_ml->M_ID'";
		}
		else
		{
			$where = "WHERE `ip` = '$ip'";
		}
		$row = $dsql->GetOne("SELECT dtime FROM `#@__bookfeedback` $where ORDER BY `id` DESC ");
		if($dtime - $row['dtime'] < $cfg_feedback_time)
		{
			ResetVdValue();
			ShowMsg('管理員設置了評論間隔時間，請稍等休息一下！','-1');
			exit();
		}
	}

	if(empty($face))
	{
		$face = 0;
	}
	$face = intval($face);
	extract($arcRow, EXTR_SKIP);
	$msg = cn_substrR(TrimMsg($msg),1000);
	$username = cn_substrR(HtmlReplace($username,2),20);
	if($feedbacktype!='good' && $feedbacktype!='bad')
	{
		$feedbacktype = 'feedback';
	}
	//保存評論內容
	if($comtype == 'comments')
	{
		$arctitle = addslashes($arcRow['arctitle']);
		$arctitle = $arcRow['arctitle'];
		if($msg!='')
		{
			$inquery = "INSERT INTO `#@__bookfeedback`(`aid`,`catid`,`username`,`arctitle`,`ip`,`ischeck`,`dtime`, `mid`,`bad`,`good`,`ftype`,`face`,`msg`)
	               VALUES ('$aid','$catid','$username','$bookname','$ip','$ischeck','$dtime', '{$cfg_ml->M_ID}','0','0','$feedbacktype','$face','$msg'); ";
			$rs = $dsql->ExecuteNoneQuery($inquery);
			if(!$rs)
			{
				echo $dsql->GetError();
				exit();
			}
		}
	}
	//引用回復
	elseif ($comtype == 'reply')
	{
		$row = $dsql->GetOne("Select * from `#@__bookfeedback` where id ='$fid'");
		$arctitle = $row['arctitle'];
		$aid =$row['aid'];
		$msg = $quotemsg.$msg;
		$msg = HtmlReplace($msg,2);
		$inquery = "INSERT INTO `#@__bookfeedback`(`aid`,`typeid`,`username`,`arctitle`,`ip`,`ischeck`,`dtime`,`mid`,`bad`,`good`,`ftype`,`face`,`msg`)
				VALUES ('$aid','$typeid','$username','$arctitle','$ip','$ischeck','$dtime','{$cfg_ml->M_ID}','0','0','$feedbacktype','$face','$msg')";
		$dsql->ExecuteNoneQuery($inquery);
	}


	if($cfg_ml->M_ID > 0)
	{
		#api{{
		if(defined('UC_APPID'))
		{
			include_once DEDEROOT.'/api/uc.func.php';
			$row = $dsql->GetOne("SELECT `scores`,`userid` FROM `#@__member` WHERE `mid`='".$cfg_ml->M_ID."'");
			uc_credit_note($row['userid'],$cfg_sendfb_scores);
		}
		#/aip}}
		$dsql->ExecuteNoneQuery("Update `#@__member` set scores=scores+{$cfg_sendfb_scores} where mid='{$cfg_ml->M_ID}' ");
	}
	//統計用戶發出的評論
	if($cfg_ml->M_ID > 0)
	{
		#api{{
		if(defined('UC_APPID'))
		{
			include_once DEDEROOT.'/api/uc.func.php';
			//推送事件
			$arcRow = GetOnebook($aid);
			$feed['icon'] = 'thread';
			$feed['title_template'] = '<b>{username} 在網站發表了評論</b>';
			$feed['title_data'] = array('username' => $cfg_ml->M_UserName);
			$feed['body_template'] = '<b>{subject}</b><br>{message}';
			$url = !strstr($arcRow['arcurl'],'http://') ? ($cfg_basehost.$arcRow['arcurl']) : $arcRow['arcurl'];		
			$feed['body_data'] = array('subject' => "<a href=\"".$url."\">$arcRow[arctitle]</a>", 'message' => cn_substr(strip_tags(preg_replace("/\[.+?\]/is", '', $msg)), 150));
			$feed['images'][] = array('url' => $cfg_basehost.'/images/scores.gif', 'link'=> $cfg_basehost);
			uc_feed_note($cfg_ml->M_LoginID,$feed); unset($arcRow);
		}
		#/aip}}	
		$row = $dsql->GetOne("SELECT COUNT(*) AS nums FROM `#@__bookfeedback` WHERE `mid`='".$cfg_ml->M_ID."'");
		$dsql->ExecuteNoneQuery("UPDATE `#@__member_tj` SET `feedback`='$row[nums]' WHERE `mid`='".$cfg_ml->M_ID."'");
	}
	$_SESSION['sedtime'] = time();
	if(empty($uid) && isset($cmtuser)) $uid = $cmtuser;
	$backurl = $cfg_formmember ? "index.php?uid={$uid}&action=viewarchives&aid={$aid}" : "bookfeedback.php?aid=$aid";
	if($ischeck==0)
	{
		ShowMsg("成功發表評論，但需審核後才會顯示你的評論!",$backurl);
	}elseif($ischeck==1)
	{
		ShowMsg("成功發表評論，現在轉到評論頁面!",$backurl);
	}
	exit();
}
?>