<?php
require_once(dirname(__FILE__)."/global.php");

if(!table_field("{$pre}comment","quoteid")){
	$db->query("ALTER TABLE  `{$pre}comment` ADD  `quoteid` INT( 10 ) NOT NULL ;");		
}

header('Content-Type: text/html; charset='.WEB_LANG);
$cid=intval($cid);
/***/
if($action=='del'){
	$rs=$db->get_one("SELECT * FROM `{$pre}comment` WHERE cid='$cid'");
	if($web_admin||($lfjuid&&$lfjuid==$rs[uid]) ){
		$db->query("DELETE FROM `{$pre}comment` WHERE cid='$cid'");
		$db->query("DELETE FROM `{$pre}comment` WHERE quoteid='$cid'");
	}
}
/***/
if($action=='agree_comment'){
	$thiscookiename='agree_comment_'.$cid;
	if($_COOKIE[$thiscookiename]){
		die('norepeat');
	}
	$db->query("UPDATE {$pre}comment SET agree=agree+1 WHERE cid='$cid'");
	@extract($db->get_one("SELECT agree FROM {$pre}comment WHERE cid='$cid'"));
	setcookie($thiscookiename,1);
	die($agree);
}	
/***/
if($action=='disagree_comment'){
	$thiscookiename='disagree_comment_'.$cid;
	if($_COOKIE[$thiscookiename]){
		die('norepeat');
	}
	$db->query("UPDATE {$pre}comment SET disagree=disagree+1 WHERE cid='$cid'");
	@extract($db->get_one("SELECT disagree FROM {$pre}comment WHERE cid='$cid'"));
	setcookie($thiscookiename,1);
	die($disagree);
}
/***/
if($action=="count_comment"){
	@extract($db->get_one("SELECT COUNT(*) AS comment_num FROM {$pre}comment WHERE aid='$aid' AND quoteid=0"));
	die($comment_num);
}
/***/
if($action=='repaly'){
	if(!$cid){
		die('noinfo');
	}else{
		$listdb=get_comment_repaly($cid);
		$listreplay='';
		foreach($listdb AS $key=>$rs){
			$listreplay.="<div class='listrepaly'>\r\n";
			$listreplay.="<div class='img'><A target='_blank' HREF='$webdb[www_url]/member/userinfo.php?uid=$rs[uid]'><img src='$rs[icon]' onerror=\"this.src='$webdb[www_url]/images/default/nobody.gif'\"/></A></div>\r\n";
			$listreplay.="<div class='word'><div class='time'><A target='_blank' HREF='$webdb[www_url]/member/userinfo.php?uid=$rs[uid]'>$rs[username]</A> $rs[posttime]</div>\r\n<p>$rs[content]</p>\r\n</div>\r\n";
			$listreplay.="</div>\r\n";
		}
	}
	die($listreplay);
}
/**
*发表评论
**/
if($action=="post"){
	if(!$lfjid){
		die("nologin");//内容不能为空
	}
	if(!$content){
		die("nocontent");//内容不能为空
	}
	//权限判断是否允许发表评论
	//禁止全部人评论
	if($webdb[forbidComment]){
		$allow=0;
	}
	//会员可以评论,但游客不能评论
	elseif(!$webdb[allowGuestComment]&&!$lfjid){
		$allow=0;
	}
	//全部人可以评论
	else{
		$allow=1;
	}
	//评论自动通过审核的判断
	//全部人的评论自动通过审核
	if($webdb[allowGuestCommentPass]){
		$yz=1;
	}
	//只有会员的才自动通过审核
	elseif($webdb[allowMemberCommentPass]&&$lfjid){
		$yz=1;
	}
	//都不能自动通过审核
	else{
		$yz=0;
	}
	$username=filtrate($username);
	$content=filtrate($content);
	$content=str_replace("@@br@@","<br>",$content);

	//过滤不健康的字
	$username=replace_bad_word($username);
	$content=replace_bad_word($content);

	if(!$lfjdb){
		$username="游客";
	}else{
		$username=$lfjid;
	}
	$erp=get_id_table($aid);
	$rss=$db->get_one(" SELECT A.*,B.allowcomment AS Fallowcomment FROM {$pre}article$erp A LEFT JOIN {$pre}sort B ON A.fid=B.fid WHERE A.aid='$aid' ");
	if(!$rss){
		die("noinfo");//原数据不存在
	}
	if(!$webdb[showComment]){
		die("sitenocomment");//系统关闭了评论功能
	}elseif(!$rss[Fallowcomment]){
		die("sortnocomment");//本栏目关闭了评论功能
	}elseif($rss[forbidcomment]){
		die("thisnocomment");//本文关闭了评论功能
	}

	$username || $username=$lfjid;

	/*如果系统做了限制,那么有的评论将不给提交成功,但没做提示评论失败*/
	if($allow)
	{
		if(is_utf8($content)||is_utf8($username)){
			$content=utf82gbk($content);
			$username=utf82gbk($username);
		}
		if(WEB_LANG=='utf-8'){
			$content=gbk2utf8($content);
			$username=gbk2utf8($username);
		}elseif(WEB_LANG=='big5'){
			require_once(ROOT_PATH."inc/class.chinese.php");
			$cnvert = new Chinese("GB2312","BIG5",$content,ROOT_PATH."./inc/gbkcode/");
			$content = $cnvert->ConvertIT();

			$cnvert = new Chinese("GB2312","BIG5",$username,ROOT_PATH."./inc/gbkcode/");
			$username = $cnvert->ConvertIT();
		}
		$db->query("INSERT INTO `{$pre}comment` (`aid` , `fid` , `uid` , `username` , `posttime` , `content` , `ip` , `icon` , `yz` ,`authorid`,`quoteid`) VALUES ('$aid', '$fid', '$lfjuid', '$username', '$timestamp', '$content', '$onlineip', '$commentface', '$yz','$rss[uid]','$cid')");
		$db->query("UPDATE {$pre}article$erp SET comments=comments+1 WHERE aid='$aid' ");
		if($webdb[comment_sendmail]){
			$u = $userDB->get_info($rss[uid]);
			send_mail($u[email],"你发布的信息“{$rss[title]}”有人给你评论了","<a href='$webdb[www_url]/bencandy.php?fid=$rss[fid]&id=$rss[aid]' target='_blank'>点击查看详情：$webdb[www_url]/bencandy.php?fid=$rss[fid]&id=$rss[aid]</a>");
		}
	}
}

//判断是否显示全部评论
if(!$webdb[showNoPassComment]){
	$SQL=" AND A.yz=1 ";
}
$rows||$rows=$webdb[showCommentRows]?$webdb[showCommentRows]:8;
if($page<1){
	$page=1;
}
$min=($page-1)*$rows;
/*评论字数再多也只限制显示1000个字*/
$leng=10000;
if($webdb[CommentOrderType]==1){
	$orderSQL=" A.ifcom DESC, ";
}elseif($webdb[CommentOrderType]==2){
	$orderSQL=" A.agree DESC, ";
}else{
	$orderSQL="";
}
$query=$db->query("SELECT A.*,B.icon,A.icon AS img FROM `{$pre}comment` A LEFT JOIN {$pre}memberdata B ON A.uid=B.uid WHERE A.aid='$aid' $SQL AND A.quoteid=0 ORDER BY $orderSQL A.cid DESC LIMIT $min,$rows");
while( $rs=$db->fetch_array($query)){
	if(!$rs[username]){
		$detail=explode(".",$rs[ip]);
		$rs[username]="$detail[0].$detail[1].$detail[2].*";
	}
	if($rs[icon]){
		$rs[icon]=tempdir($rs[icon]);
	}else{
		$rs[icon]="$webdb[www_url]/images/default/nobody.gif";
	}
	$rs[posttime]=date("Y-m-d H:i:s",$rs[posttime]);
	$rs[title]=get_word($rs[title],50);
	$rs[content]=get_word($rs[content],$leng);
	$rs[content]=replace_bad_word($rs[content]);
	$rs[username]=replace_bad_word($rs[username]);
	@extract($db->get_one("SELECT COUNT(*) AS reply_num FROM {$pre}comment WHERE quoteid=$rs[cid]"));
	$rs[reply_num]=$reply_num;
	$listdb[]=$rs;
}
if(!$listdb){
	die();
}
require_once(html('comment_ajax1'));

function get_comment_repaly($cid){
	global $db,$pre;
	$query =$db->query("SELECT A.*,B.icon,A.icon AS img FROM `{$pre}comment` A LEFT JOIN {$pre}memberdata B ON A.uid=B.uid WHERE A.quoteid='$cid'");
	while($rs =$db->fetch_array($query)){
		if($rs[icon]){
			$rs[icon]=tempdir($rs[icon]);
		}else{
			$rs[icon]="$webdb[www_url]/images/default/nobody.gif";
		}
		$rs[posttime]=date("Y-m-d H:i:s",$rs[posttime]);
		$rs[content]=str_replace("\n","<br>",$rs[content]);
		$rs[content]=replace_bad_word($rs[content]);
		$rs[username]=replace_bad_word($rs[username]);
		$listdb[]=$rs;
	}
	return $listdb;
}
?>