<?php
require("../class/connect.php");
include("../class/db_sql.php");
include("../data/dbcache/class.php");
require('../data/dbcache/MemberLevel.php');
$link=db_connect();
$empire=new mysqlquery();
$myuserid=(int)getcvar('mluserid');


$dopost=$_POST['dopost'];
if($dopost=='diggtop'){

if(!$myuserid)
{
	exit('{"status":0,"message":"请先登录"}');
}
$id=(int)$_POST['id'];
$cr=$empire->fetch1("select classid from {$dbtbpre}ecms_news_index where id='$id' limit 1");
$classid=$cr['classid'];
if($id&&$classid)
{
	$doajax=1;
	$editor=1;
	if(empty($class_r[$classid][tbname]))
	{
		$doajax==1?ajax_printerror('','','ErrorUrl',1):printerror('ErrorUrl','',1);
    }
	$checkid=$classid.'n'.$id;
	$checktime=time()+30*24*3600;
	//连续提交
	if(getcvar('lastdiggid')==$checkid)
	{
		//$doajax==1?ajax_printerror('','','ReDigg',1):printerror('ReDigg','',1);
		exit('{"status":0,"message":"你已经赞过了"}');
	}
	//字段
	$fnum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsf where tbname='".$class_r[$classid][tbname]."' and (f='diggtop' or f='diggdown')");
	if(empty($fnum))
	{
		//$doajax==1?ajax_printerror('','','ErrorUrl',1):printerror('ErrorUrl','',1);
		exit('{"status":0,"message":"链接错误"}');
	}
	//验证信息
	$num=$empire->gettotal("select count(*) as total from {$dbtbpre}ecms_".$class_r[$classid][tbname]." where id='$id' and classid='$classid' limit 1");
	if(empty($num))
	{
		//$doajax==1?ajax_printerror('','','ErrorUrl',1):printerror('ErrorUrl','',1);
		exit('{"status":0,"message":"链接错误"}');
	}
	//验证IP
	$ip=egetip();
	$ipr=$empire->fetch1("select classid,ips from {$dbtbpre}enewsdiggips where id='$id' and classid='$classid' limit 1");
	if(strstr($ipr['ips'],','.$ip.','))
	{
		//$doajax==1?ajax_printerror('','','ReDigg',1):printerror('ReDigg','',1);
		exit('{"status":0,"message":"你已经赞过了"}');
	}
	else
	{
		if(empty($ipr['classid']))
		{
			$newips=','.$ip.',';
			$usql=$empire->query("insert into {$dbtbpre}enewsdiggips(id,classid,ips) values('$id','$classid','$newips');");
		}
		else
		{
			$newips=$ipr['ips']?$ipr['ips'].$ip.',':','.$ip.',';
			$usql=$empire->query("update {$dbtbpre}enewsdiggips set ips='$newips' where id='$id' and classid='$classid' limit 1");
		}
	}
	$dotop=1;
	$f='diggtop';
	$n='+1';
	if($dotop)
	{
		$mess='DoDiggGSuccess';
	}
	else
	{
		if($fnum==2)
		{
			$f='diggdown';
		}
		else
		{
			$n='-1';
		}
		$mess='DoDiggBSuccess';
	}
	$sql=$empire->query("update {$dbtbpre}ecms_".$class_r[$classid][tbname]." set ".$f."=".$f.$n." where id='$id'");
	$ursql=$empire->query("update {$dbtbpre}enewsmemberadd set diggfen=diggfen+1 where userid='$myuserid' limit 1");
	if($sql)
	{
		esetcookie('lastdiggid',$checkid,$checktime);	//最后发布
		if($doajax==1)
		{
			$nr=$empire->fetch1("select ".$f." from {$dbtbpre}ecms_".$class_r[$classid][tbname]." where id='$id'");
			exit('{"status":'.$nr[$f].'}');
			//ajax_printerror($nr[$f],$_GET['ajaxarea'],$mess,1);
		}
		else
		{
			printerror($mess,$_SERVER['HTTP_REFERER'],1);
		}
    }
	else
	{
		//$doajax==1?ajax_printerror('','','DbError',1):printerror('DbError','',1);
		exit('{"status":0,"message":"数据库错误"}');
	}
}
}elseif($dopost=='fav'){
	
	if(!$myuserid)
{
	exit('{"status":0,"message":"请先登录"}');
}
$id=(int)$_POST['id'];
$cr=$empire->fetch1("select classid from {$dbtbpre}ecms_news_index where id='$id' limit 1");
$classid=$cr['classid'];
	//增加收藏
	$cid=0;
	if(empty($id)||empty($classid))
	{
		exit('{"status":0,"message":"链接错误"}');
    }
	//表不存在
	if(empty($class_r[$classid][tbname]))
	{
		exit('{"status":0,"message":"栏目错误"}');
	}
	$num=$empire->gettotal("select count(*) as total from {$dbtbpre}ecms_".$class_r[$classid][tbname]." where id='$id' and classid='$classid'");
	if(empty($num))
	{exit('{"status":0,"message":"链接错误"}');}
	//是否已收藏
	$newsnum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsfava where id='$id' and classid='$classid' and userid='$myuserid'");
	if($newsnum)
	{
		exit('{"status":0,"message":"您已经收藏过了"}');
	}
	$favanum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsfava where userid='$myuserid'");
	$groupid=(int)getcvar('mlgroupid');
	$username=(int)getcvar('mlusername');
	if($level_r[$groupid][favanum]<=$favanum)
	{
		//printerror("MoreFava","history.go(-1)",1);
		exit('{"status":0,"message":"您的收藏夹已满，增加收藏不成功"}');
		
	}
	$favatime=date("Y-m-d H:i:s");
	$sql=$empire->query("insert into {$dbtbpre}enewsfava(id,favatime,userid,username,classid,cid) values('$id','$favatime','$myuserid','$username','$classid','$cid');");
	$sql2=$empire->query("update {$dbtbpre}ecms_".$class_r[$classid][tbname]." set favnum=favnum+1 where id='$id'");
	if($sql)
	{
		//printerror("AddFavaSuccess",$from,1);
		$nr=$empire->fetch1("select favnum from {$dbtbpre}ecms_".$class_r[$classid][tbname]." where id='$id'");
		exit('{"status":'.$nr[favnum].'}');
	}
	else
	{
		exit('{"status":0,"message":"数据库错误"}');
	}


}elseif($dopost=='guestbook'){//空间留言

if(!$myuserid)
{
	exit('{"status":0,"message":"请先登录"}');
}

	//用户
	$userid=intval($_POST['userid']);
	$ur=$empire->fetch1("select userid from {$dbtbpre}enewsmember where userid='$userid' limit 1");
	if(empty($ur['userid']))
	{
		exit('{"status":0,"message":"无此会员"}');
	}
	//发表者
	$uid=(int)getcvar('mluserid');
	
	if($uid)
	{
		$uname=RepPostVar(getcvar('mlusername'));
	}
	else
	{
		exit('{"status":0,"message":"请登陆会员"}');
		$uid=0;
		$uname=trim($add['uname']);
	}
	$uname=RepPostStr($uname);
	$gbtext=RepPostStr($_POST['gbtext']);
	$gbtext=iconv("UTF-8", "gbk",  urldecode(urldecode($gbtext)));
	if(empty($uname)||!trim($gbtext))
	{
		exit('{"status":0,"message":"请填写留言内容。"}');
    }
	$isprivate=intval($add['isprivate']);
	$addtime=date("Y-m-d H:i:s");
	$ip=egetip();
	$sql=$empire->query("insert into {$dbtbpre}enewsmembergbook(userid,isprivate,uid,uname,ip,addtime,gbtext,retext) values($userid,$isprivate,$uid,'$uname','$ip','$addtime','$gbtext','');");
	//ecmsEmptyShowKey($keyvname);//清空验证码
	$addr=$empire->fetch1("select userpic from {$dbtbpre}enewsmemberadd where userid='$uid' limit 1");
	$userpic=$addr[userpic]?$addr[userpic]:'/e/data/images/nouserpic.gif';	
			
	if($sql)
	{
		//printerror("AddMemberGbookSuccess",$_SERVER['HTTP_REFERER'],1);
		exit('{"status":1,"message":"<dl><dt><div class=\"head\"><a href=\"/e/space/?userid='.$uid.'\"><img src=\"'.$userpic.'\" /></a></div><div class=\"name\"><a href=\"/e/space/?userid='.$uid.'\">'.$uname.'</a></div></dt><dd><div class=\"fb\"><div class=\"fm\"><div class=\"fc\"><p class=\"c\">'.$gbtext.'</p><span class=\"d\">'.$addtime.'</span></div></div></div></dd></dl>"}');
	}
	else
	{
		exit('{"status":0,"message":"数据库错误"}');
	}


}elseif($dopost=='vhistory'){//空间访客换一组
	//用户
	$userid=intval($_POST['userid']);
	$vpage=intval($_POST['vpage']);
	$num=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsmember_visitor where userid='$userid' and uid='$uid' and type='1'");
	if($num<12)
	{
		exit('{"status":0,"message":"已经没有了"}');
		 
	}else{
		$line=$vpage*12;
		$fksql=$empire->query("select * from {$dbtbpre}enewsmember_visitor where userid='".$userid."' and type='0' order by addtime desc limit $line,12");
		while($rs=$empire->fetch($fksql))
		{
	        if($rs[userid])
    	    {
				$userpic=$fkxxrs[userpic]?$fkxxrs[userpic]:'/e/data/images/nouserpic.gif';
            	$fklist.='<li><a title=\"'.$fku[username].'\" href=\"/e/space/index.php?userid='.$fkxxrs[userid].'\"><img src=\"'.$userpic.'\" alt=\"'.$fku[username].'\" /></a></li>';
	        }
		}
		$endpage=$vpage+1;
		exit('{"status":2,"vpage":'.$endpage.',"vhistory":"'.$fklist.'"}');
	}

}elseif($dopost=='spacezan'){//空间关注
if(!$myuserid)
{
	exit('{"status":0,"message":"请先登录"}');
}
	//用户
	$uid=getcvar('mluserid');
	$userid=intval($_POST['userid']);
	$fktime=time();
	$num=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsmember_visitor where userid='$userid' and uid='$uid' and type='1' limit 1");
	if($num)
	{
		exit('{"status":0,"message":"您已经关注过了"}');
		 
	}else{
		$sql=$empire->query("insert into {$dbtbpre}enewsmember_visitor(userid,uid,addtime,type) values($userid,$uid,'$fktime','1')");
		$sql=$empire->query("update {$dbtbpre}enewsmemberadd set gzfen=gzfen+1 where userid='$userid' limit 1");
		if($sql)
		{
			$addr=$empire->fetch1("select gzfen from {$dbtbpre}enewsmemberadd where userid='$userid' limit 1");
			exit('{"status":1}');
		}
		else
		{
			exit('{"status":0,"message":"数据库错误"}');
		}
	}

}elseif($dopost=='quickjingdianyuju'){
	if(!$myuserid)
{
	exit('{"status":0,"message":"请先登录"}');
}
	require("../class/functions.php");
	require("../class/t_functions.php");
	require("../data/dbcache/MemberLevel.php");
	require("../member/class/user.php");
	require("../class/qinfofun.php");
	$doetran=1;
	$_POST['MAddInfo']='enews';
	$_POST['classid']='70';
	$_POST['mid']='1';
	$_POST['newstext']=iconv("UTF-8//IGNORE","gbk", $_POST['newstext']);
	$_POST['title']=esub($_POST['newstext'],40);
	$_POST['smalltext']=$_POST['newstext'];
	$_POST['MAddInfo']='enews';
	$_POST['filepass']=time();
	DodoInfo($_POST,0);
}
db_close();
$empire=null;
?>