<?php
require_once(dirname(__FILE__)."/../../inc/common.php");
AjaxHead();
$key_time="zz_ltime";
$key_count="zz_rtime";
$timeSpan=commentTime;			//评论间隔的秒数

$ac=getForm("action","both");$captcha=getForm("captcha","both");
$id=getForm("gid","both");$cparent=getForm("cparent","post");$itype=getForm("ctype","post");$anony=getForm("anony","post");$vote=ifthen(getForm("vote","post")=="1",1,0);$ppath=getForm("ppath","post");$utmpname=getForm("utmpname","post");
switch ($ac) {
	case "1":addComment();break;
	case "2":addAgree();break;
	case "3":addAnti();break;
	case "4":addVote();break;
}

function CallBack($ok,$fn,$msg){
	echo "<script>try{parent.onSendBack('$ok','$fn','$msg')}catch(e){alert('未定义上传回调函数onSendBack(boolean uped,string filename,string msg)')}</script>";
}

function addAgree(){
	global $id;
	if (cookie::get("m_agree$id") == "ok"){ CallBack(false,"","你已经顶踩过了！");exit();}
	DB::query("update ".DB::table('review')." set `m_agree`=`m_agree`+1 where `m_id`=$id");
	cookie::set("m_agree$id", "ok", 3600 * 24);
}

function addAnti(){
	global $id;
	if (cookie::get("m_anti$id") == "ok"){ CallBack(false,"","你已经顶踩过了！");exit();}
	DB::query("update ".DB::table('review')." set `m_anti`=`m_anti`+1 where `m_id`=$id");
	cookie::set("m_anti$id", "ok", 3600 * 24);
}

function addVote(){
	global $id;
	DB::query("update ".DB::table('review')." set `m_vote`=`m_vote`+1 where `m_id`=$id");
}

function CheckBanWords($m_content)
{
	$BanWords = BanWords;
	if (empty($BanWords) || empty($m_content)) return "";
	$Ban = explode(",",$BanWords);$l = count($Ban);
	for ($i = 0;$i < $l;$i++) {
		if (strpos(" ".$m_content,$Ban[$i])>0){ CallBack(false,"","系统繁忙，请稍候再试！");exit();}
	}
}

function CheckBanIP($m_ip)
{
	$BanIPS=banIPS;
	if (empty($BanIPS) || empty($m_ip)) return "";
	$Ban = explode(",",replaceStr($BanIPS," ",""));$l=count($Ban);
	if (strpos(" ,".$BanIPS.",",",".$m_ip.",")>0){ exit();}
	for ($i = 0;$i < $l;$i++) {
		if(strpos(" ".$Ban[$i], "*") > 0) {
			$x = explode(".",$Ban[$i]);
			$y = explode(".",$m_ip);
			if (count($x) >= 3) {
				if (($x[0]==$y[0] || $x[0]=="*") && ($x[1]==$y[1] || $x[1]=="*") && ($x[2]==$y[2] || $x[2]=="*") && ($x[3]==$y[3] || $x[3]=="*")) {
					CallBack(false,"","系统繁忙，请稍候再试！");exit();
		        }
		    }
		} else if ($Ban[$i] == $m_ip) {
			CallBack(false,"","系统繁忙，请稍候再试！");exit();
		}
	}
}

function addComment(){
	global $id,$key_count,$key_time,$utmpname,$cparent,$itype,$anony,$vote,$ppath,$timeSpan,$captcha;
	session_start();
	if ("".$_SESSION["GetCode"] != $captcha || empty($captcha)){
		CallBack(false,"","验证码错误！");exit();
	}
	$vote=ifthen($vote=="1",1,0);
	$m_ip = getIp();
	if (computeStrLen($m_ip)>15) $m_ip=getStrByLen($m_ip,15);
	CheckBanIP($m_ip);
	$m_content=trim(encodeHtml(filterDirty(getForm("talkwhat","post"))));
	if (computeStrLen($m_content)<3){ exit();}
	if (computeStrLen($m_content)>500) $m_content=getStrByLen($m_content,500);
	CheckBanWords($m_content);$m_content=replacedirtyWords($m_content);
	if ("".$anony=="1"){
		$utmpname=trim(encodeHtml(filterDirty($utmpname)));
		if ($utmpname!=""){
			CheckBanWords($utmpname);$m_author=replacedirtyWords($utmpname);
        }else{
			$m_author="";
        }
    }else{
		$m_author="";
    }
	if (computeStrLen($m_author)>20) $m_author=getStrByLen($m_author,20);
	//检查评论间隔时间；
	$dtime = time();
	if(!empty($timeSpan)) {
		$row = DB::fetch_first("SELECT m_addtime FROM ".DB::table('review')." WHERE `m_ip`='$m_ip' ORDER BY `m_id` DESC ");
		if ($dtime - $row['m_addtime'] < $timeSpan) {
			CallBack(false,"","系统繁忙，请稍候再试！");exit();
		}
	}
	$reviewarr = array(
		'm_reply' => $cparent,
		'm_author' => $m_author,
		'm_type' => $itype,
		'm_videoid' => $id,
		'm_content' => $m_content,
		'm_ip' => $m_ip,
		'm_addtime' => time(),
		'm_agree' => 0,
		'm_anti' => 0,
		'm_pic' => replaceStr($ppath,"'",""),
		'm_vote' => $vote,
		'm_check' => ifthen($ppath!="","0","1"),
	);
	DB::insert('review', $reviewarr);
	//db::query("UPDATE {pre}data SET m_reply=m_reply+1 WHERE m_id=$gid");
	CallBack(true,"","");
}
?>