<?php
//发表评论
function AddPl($username,$password,$nomember,$key,$saytext,$id,$classid,$repid,$add){
	global $empire,$dbtbpre,$public_r,$class_r,$level_r,$emod_r;
	//验证本时间允许操作
	eCheckTimeCloseDo('pl');
	//验证IP
	eCheckAccessDoIp('pl');
	$id=(int)$id;
	$repid=(int)$repid;
	$classid=(int)$classid;
	//验证码
	$keyvname='checkplkey';
	if($public_r['plkey_ok'])
	{
		ecmsCheckShowKey($keyvname,$key,1);
	}
	$username=RepPostVar($username);
	$password=RepPostVar($password);
	$muserid=(int)getcvar('mluserid');
	$musername=RepPostVar(getcvar('mlusername'));
	$mgroupid=(int)getcvar('mlgroupid');
	$mrnd=RepPostVar(getcvar('mlrnd'));
	$ur=array();
	$cklgr=array();
	if($muserid)//已登陆
	{
		$cklgr=qCheckLoginAuthstr();
		if($cklgr['islogin'])
		{
			$ur=$empire->fetch1("select ".eReturnSelectMemberF('userid,username,checked,registertime,groupid,userfen,userdate,zgroupid,ingid,agid,isern,isot')." from ".eReturnMemberTable()." where ".egetmf('userid')."='$muserid' and ".egetmf('username')."='$musername' and ".egetmf('rnd')."='$mrnd'".do_dblimit_one());
			if(empty($ur['userid']))
			{
				printerror("NotSingleLogin",'',1);
			}
			if(empty($ur['isot']))
			{
				printerror("NotSingleLogin",'',1);
			}
			if($ur['checked']==0)
			{
				printerror("NotCheckedUser",'',1);
			}
			$username=$musername;
			$muserid=$ur['userid'];
			$mgroupid=$ur['groupid'];
		}
		else
		{
			$muserid=0;
			$mgroupid=0;
		}
	}
	else
	{
		if(!$nomember&&$public_r['plcanlogin'])//非匿名
		{
			if(!$username||!$password)
			{
				printerror("FailPassword","history.go(-1)",1);
			}
			$ur=$empire->fetch1("select ".eReturnSelectMemberF('userid,username,salt,password,checked,registertime,groupid,userfen,userdate,zgroupid,ingid,agid,isern,isot')." from ".eReturnMemberTable()." where ".egetmf('username')."='$username'".do_dblimit_one());
			if(empty($ur['userid']))
			{
				printerror("FailPassword","history.go(-1)",1);
			}
			if(!eDoCkMemberPw($password,$ur['password'],$ur['salt']))
			{
				printerror("FailPassword","history.go(-1)",1);
			}
			if($ur['checked']==0)
			{
				printerror("NotCheckedUser",'',1);
			}
			$muserid=$ur['userid'];
			$mgroupid=$ur['groupid'];
		}
		else
		{
			$muserid=0;
			$mgroupid=0;
		}
	}
	if($public_r['plgroupid'])
	{
		if(!$muserid)
		{
			printerror("GuestNotToPl","history.go(-1)",1);
		}
		if(empty($ur['userid']))
		{
			printerror("GuestNotToPl","history.go(-1)",1);
		}
		$mvgresult=eMember_ReturnCheckMVGroup($ur,$public_r['plgroupid']);
		if($mvgresult<>'empire.cms')
		{
			printerror("NotLevelToPl","history.go(-1)",1);
		}
	}
	if($level_r[$mgroupid]['plchecked']==-1)
	{
		printerror('MGNotLevel','',1);
	}
	//新会员评论验证
	if($public_r['plnewaddtime'])
	{
		if(!$muserid)
		{
			printerror("GuestNotToPl","history.go(-1)",1);
		}
		if(empty($ur['userid']))
		{
			printerror("GuestNotToPl","history.go(-1)",1);
		}
		$registertime=eReturnMemberIntRegtime($ur['registertime']);
		if(time()-$registertime<=$public_r['plnewaddtime']*60)
		{
			printerror('NewMemberAddPlError','',1);
		}
	}
	//实名验证
	eCheckHaveTruename('pl',$ur['userid'],$ur['username'],$ur['isern'],$ur['checked'],0);

	//专题
	$doaction=$add['doaction'];
	if($doaction=='dozt')
	{
		if(!trim($saytext)||!$classid)
		{
			printerror("EmptyPl","history.go(-1)",1);
		}
		//是否关闭评论
		$r=$empire->fetch1("select ztid,closepl,checkpl,restb from {$dbtbpre}enewszt where ztid='$classid'");
		if(!$r['ztid'])
		{
			printerror("ErrorUrl","history.go(-1)",1);
		}
		if($r['closepl'])
		{
			printerror("CloseClassPl","history.go(-1)",1);
		}
		//审核
		if($r['checkpl'])
		{$checked=1;}
		else
		{$checked=0;}
		$restb=$r['restb'];
		$pubid='-'.$classid;
		$id=0;
		$pagefunr=eReturnRewritePlUrl($classid,$id,'dozt',0,0,1);
		$returl=$pagefunr['pageurl'];
	}
	else//信息
	{
		if(!trim($saytext)||!$id||!$classid)
		{
			printerror("EmptyPl","history.go(-1)",1);
		}
		//表存在
		if(empty($class_r[$classid]['tbname'])||InfoIsInTable($class_r[$classid]['tbname']))
		{
			printerror("ErrorUrl","history.go(-1)",1);
		}
		//是否关闭评论
		$r=$empire->fetch1("select classid,stb,restb from {$dbtbpre}ecms_".$class_r[$classid]['tbname']." where id='$id'".do_dblimit_one());
		if(!$r['classid']||$r['classid']!=$classid)
		{
			printerror("ErrorUrl","history.go(-1)",1);
		}
		if($class_r[$r['classid']]['openpl'])
		{
			printerror("CloseClassPl","history.go(-1)",1);
		}
		//单信息关闭评论
		$pubid=ReturnInfoPubid($classid,$id);
		$finfor=$empire->fetch1("select closepl from {$dbtbpre}ecms_".$class_r[$classid]['tbname']."_data_".$r['stb']." where id='$id'".do_dblimit_one());
		if($finfor['closepl'])
		{
			printerror("CloseInfoPl","history.go(-1)",1);
		}
		//审核
		if($class_r[$classid]['checkpl'])
		{$checked=1;}
		else
		{$checked=0;}
		$restb=$r['restb'];
		$pagefunr=eReturnRewritePlUrl($classid,$id,'doinfo',0,0,1);
		$returl=$pagefunr['pageurl'];
	}
	//设置参数
	$plsetr=$empire->fetch1("select pltime,plsize,plincludesize,plclosewords,plmustf,plf,plmaxfloor,plquotetemp from {$dbtbpre}enewspl_set".do_dblimit_one());
	if(strlen($saytext)>$plsetr['plsize'])
	{
		$GLOBALS['setplsize']=$plsetr['plsize'];
		printerror("PlSizeTobig","history.go(-1)",1);
	}
	$time=time();
	$saytime=$time;
	$pltime=intval(getcvar('lastpltime'));
	if($pltime)
	{
		if($time-$pltime<$plsetr['pltime'])
		{
			$GLOBALS['setpltime']=$plsetr['pltime'];
			printerror("PlOutTime","history.go(-1)",1);
		}
	}
	$sayip=egetip();
	$eipport=egetipport();
	$username=str_replace("\r\n","",$username);
	$username=dgdbe_rpstr($username);
	$saytext=nl2br(RepFieldtextNbsp(dgdbe_rpstr($saytext)));
	if($repid)
	{
		$saytext=RepPlTextQuote($repid,$saytext,$plsetr,$restb);
		CkPlQuoteFloor($plsetr['plmaxfloor'],$saytext);//验证楼层
	}
	//过滤字符
	$saytext=ReplacePlWord($plsetr['plclosewords'],$saytext);
	if(!eckDbStrlen_char($saytext,2))
	{
		printerror('PlOutMaxFloor','history.go(-1)',1);
	}
	if($level_r[$mgroupid]['plchecked'])
	{
		$checked=0;
	}
	$restb=(int)$restb;
	$checked=(int)$checked;
	$muserid=(int)$muserid;
	
	$ret_r=ReturnPlAddF($add,$plsetr,0);
	//主表
	$sql=$empire->updatesql("insert into {$dbtbpre}enewspl_".$restb."(pubid,username,sayip,saytime,id,classid,checked,zcnum,fdnum,userid,isgood,saytext,eipport".$ret_r['fields'].") values('$pubid','".$username."','$sayip','$saytime','$id','$classid','$checked',0,0,'$muserid',0,'".addslashes($saytext)."','$eipport'".$ret_r['values'].");","ins");
	$plid=$empire->lastid($dbtbpre.'enewspl_'.$restb,'plid');
	if($doaction!='dozt')
	{
		//评论时间
		$mid=(int)$class_r[$classid]['modid'];
		$tbmainf=$emod_r[$mid]['tbmainf'];
		$addupf='';
		if(strstr($tbmainf,',elpltime,'))
		{
			$addupf.=",elpltime='".time()."'";
		}
		//信息表加1
		$usql=$empire->query("update {$dbtbpre}ecms_".$class_r[$classid]['tbname']." set plnum=plnum+1".$addupf." where id='$id'".do_dblimit_upone());
	}
	//更新新评论数
	DoUpdateAddDataNum('pl',$restb,1);
	//设置最后发表时间
	$set1=esetcookie("lastpltime",time(),time()+3600*24);
	ecmsEmptyShowKey($keyvname);//清空验证码
	if($sql)
	{
		$reurl=DoingReturnUrl($returl,$_POST['ecmsfrom']);
		printerror("AddPlSuccess",$reurl,1);
	}
	else
	{printerror("DbError","history.go(-1)",1);}
}

//替换回复
function RepPlTextQuote($repid,$saytext,$pr,$restb){
	global $public_r,$empire,$dbtbpre,$fun_r;
	if($pr['plmaxfloor']==-2)
	{
		return $saytext;
	}
	$repid=(int)$repid;
	$restb=(int)$restb;
	$quotetemp=stripSlashes($pr['plquotetemp']);
	$r=$empire->fetch1("select plid,userid,username,saytime,checked,saytext from {$dbtbpre}enewspl_".$restb." where plid='$repid'");
	if(!$r['plid']||$r['checked'])
	{
		return $saytext;
	}
	if($pr['plmaxfloor']<0)
	{
		$r['saytext']=RepYPlQuote($r['saytext']);
		if($r['saytext']!=addslashes($r['saytext']))
		{
			return $saytext;
		}
	}
	if(empty($r['username']))
	{
		$r['username']=RepPostVar($fun_r['nomember']);
	}
	if($r['userid'])
	{
		$r['username']="<a href=\"".$public_r['newsurl']."e/space/?userid=".$r['userid']."\" target=\"_blank\">".$r['username']."</a>";
	}
	$quotetemp=str_replace('[!--plid--]',$repid,$quotetemp);
	$quotetemp=str_replace('[!--pltime--]',date('Y-m-d H:i:s',$r['saytime']),$quotetemp);
	$quotetemp=str_replace('[!--username--]',$r['username'],$quotetemp);
	$quotetemp=str_replace('[!--pltext--]',$r['saytext'],$quotetemp);
	$restr=$quotetemp.$saytext;
	return $restr;
}

//去掉原引用
function RepYPlQuote($text){
	$preg_str="/<div (.+?)<\/div>/is";
	$text=preg_replace($preg_str,"",$text);
	return $text;
}

//验证引用楼数
function CkPlQuoteFloor($plmaxfloor,$saytext){
	if($plmaxfloor<=0)
	{
		return '';
	}
	$fr=explode('<div',$saytext);
	$fcount=count($fr)-1;
	if($fcount>$plmaxfloor)
	{
		printerror('PlOutMaxFloor','history.go(-1)',1);
	}
}

//禁用字符
function ReplacePlWord($plclosewords,$text){
	global $empire,$dbtbpre;
	if(empty($text))
	{
		return $text;
	}
	toCheckCloseWord($text,$plclosewords,'HavePlCloseWords');
	return $text;
}

//返回字段
function ReturnPlAddF($add,$pr,$ecms=0){
	global $empire,$dbtbpre;
	$fr=explode(',',$pr['plf']);
	$count=count($fr)-1;
	$ret_r['fields']='';
	$ret_r['values']='';
	for($i=1;$i<$count;$i++)
	{
		$f=$fr[$i];
		$fval=$add[$f];
		if($f=='eipf')//ipfrom
		{
			$fval=egetipfrom();
		}
		$fval=dgdbe_rpstr($fval);
		//必填
		if(strstr($pr['plmustf'],','.$f.','))
		{
			if(!trim($fval))
			{
				$chfr=$empire->fetch1("select fname from {$dbtbpre}enewsplf where f='$f'".do_dblimit_one());
				$GLOBALS['msgmustf']=$chfr['fname'];
				printerror('EmptyPlMustF','',1);
			}
		}
		$fval=nl2br(RepFieldtextNbsp($fval));
		$ret_r['fields'].=",".$f;
		$ret_r['values'].=",'".addslashes($fval)."'";
	}
	return $ret_r;
}

//支持/反对评论
function DoForPl($add){
	global $empire,$dbtbpre,$class_r;
	$classid=(int)$add['classid'];
	$id=(int)$add['id'];
	$plid=(int)$add['plid'];
	$dopl=(int)$add['dopl'];
	$doajax=(int)$add['doajax'];
	//专题
	$doaction=$add['doaction'];
	if($doaction=='dozt')
	{
		if(!$classid||!$plid)
		{
			$doajax==1?ajax_printerror('','','ErrorUrl',1):printerror('ErrorUrl','',1);
		}
		$infor=$empire->fetch1("select ztid,restb from {$dbtbpre}enewszt where ztid='$classid'");
		if(!$infor['ztid'])
		{
			$doajax==1?ajax_printerror('','','ErrorUrl',1):printerror('ErrorUrl','',1);
		}
		$pubid='-'.$classid;
	}
	else//信息
	{
		if(!$classid||!$id||!$plid||!$class_r[$classid]['tbname']||InfoIsInTable($class_r[$classid]['tbname']))
		{
			$doajax==1?ajax_printerror('','','ErrorUrl',1):printerror('ErrorUrl','',1);
		}
		$infor=$empire->fetch1("select classid,restb from {$dbtbpre}ecms_".$class_r[$classid]['tbname']." where id='$id'".do_dblimit_one());
		if(!$infor['classid'])
		{
			$doajax==1?ajax_printerror('','','ErrorUrl',1):printerror('ErrorUrl','',1);
		}
		$pubid=ReturnInfoPubid($classid,$id);
	}
	//连续发表
	if(getcvar('lastforplid'.$plid))
	{
		$doajax==1?ajax_printerror('','','ReDoForPl',1):printerror('ReDoForPl','',1);
	}
	if($dopl==1)
	{
		$f='zcnum';
		$msg='DoForPlGSuccess';
	}
	else
	{
		$f='fdnum';
		$msg='DoForPlBSuccess';
	}
	$sql=$empire->query("update {$dbtbpre}enewspl_".$infor['restb']." set ".$f."=".$f."+1 where plid='$plid' and pubid='$pubid'");
	if($sql)
	{
		esetcookie('lastforplid'.$plid,$plid,time()+30*24*3600);	//最后发布
		if($doajax==1)
		{
			$nr=$empire->fetch1("select ".$f." from {$dbtbpre}enewspl_".$infor['restb']." where plid='$plid' and pubid='$pubid'");
			ajax_printerror($nr[$f],RepPostVar($add['ajaxarea']),$msg,1);
		}
		else
		{
			printerror($msg,EcmsGetReturnUrl(),1);
		}
	}
	else
	{
		$doajax==1?ajax_printerror('','','DbError',1):printerror('DbError','',1);
	}
}
?>