<?php
if(!defined('empirecms'))
{
	exit();
}

//是否登陆
function ViewCheckLogin(){
	global $empire,$public_r,$user_tablename,$user_userid,$user_userfen,$user_group,$user_groupid,$user_rnd,$user_zgroup,$user_userdate,$eloginurl;
	$userid=(int)getcvar('mluserid');
	$rnd=getcvar('mlrnd');
	if($eloginurl)
	{
		$gotourl=$eloginurl;
	}
	else
	{
		$gotourl=$public_r['newsurl']."e/member/login";
	}
	if(!$userid)
	{
		if(!getcvar('returnurl'))
		{
			esetcookie("returnurl",$_SERVER['HTTP_REFERER'],0);
		}
		echo"<script>alert('您还没有登陆!');self.location.href='".$gotourl."';</script>";
		exit();
	}
	$cr=$empire->fetch1("select ".$user_userid.",".$user_group.",".$user_userfen.",".$user_userdate.",".$user_zgroup." from ".$user_tablename." where ".$user_userid."='$userid' and ".$user_rnd."='$rnd' limit 1");
	if(!$cr[$user_userid])
	{
		EmptyEcmsCookie();
		if(!getcvar('returnurl'))
		{
			esetcookie("returnurl",$_SERVER['HTTP_REFERER'],0);
		}
		echo"<script>alert('同一帐号只能一人在线!');self.location.href='".$gotourl."';</script>";
		exit();
	}
	//默认会员组
	if(empty($cr[$user_group]))
	{
		$usql=$empire->query("update ".$user_tablename." set ".$user_group."='$user_groupid' where ".$user_userid."='".$cr[$user_userid]."'");
		$cr[$user_group]=$user_groupid;
	}
	//是否过期
	if($cr[$user_userdate])
	{
		if($cr[$user_userdate]-time()<=0)
		{
			OutTimeZGroup($cr[$user_userid],$cr[$user_zgroup]);
			$cr[$user_userdate]=0;
			if($cr[$user_zgroup])
			{
				$cr[$user_group]=$cr[$user_zgroup];
				$cr[$user_zgroup]=0;
			}
		}
	}
	$re[userid]=$cr[$user_userid];
	$re[userfen]=$cr[$user_userfen];
	$re[groupid]=$cr[$user_group];
	$re[userdate]=$cr[$user_userdate];
	$re[zgroupid]=$cr[$user_zgroup];
	return $re;
}

//查看权限函数
function CheckShowNewsLevel($groupid,$fh,$userfen,$newsid,$classid){
	global $check_path,$level_r,$empire,$user_userfen,$user_userid,$user_tablename,$user_userdate;
	//是否登陆
	$user_r=ViewCheckLogin();
	//等于
	if($fh)
	{
		if($groupid<>$user_r[groupid])
		{
			echo"<script>alert('您所在的会员组，没有查看此信息的权限!');history.go(-1);</script>";
			exit();
		}
	}
	//大于等于
	else
	{
		if($level_r[$groupid][level]>$level_r[$user_r[groupid]][level])
		{
			echo"<script>alert('您的会员级别不足(".$level_r[$groupid][groupname].")，没有查看此信息的权限!');history.go(-1);</script>";
			exit();
		}
	}
	//扣点
	$viewnewsid=getcvar('doviewnewsid');
	if(!empty($userfen)&&$viewnewsid<>$newsid."n".$classid)
	{
		if($user_r[userdate]-time()>0)//包月
		{}
		else
		{
			if($user_r[userfen]<$userfen)
			{
				echo"<script>alert('您的点数不足(".$userfen." 点)，没有查看此信息的权限!');history.go(-1);</script>";
				exit();
			}
			//扣点
			$usql=$empire->query("update ".$user_tablename." set ".$user_userfen."=".$user_userfen."-".$userfen." where ".$user_userid."='$user_r[userid]'");
			esetcookie("doviewnewsid",$newsid."n".$classid);
		}
	}
}
//扣点
include($check_path."e/class/connect.php");
if(!defined('InEmpireCMS'))
{
	exit();
}
include($check_path."e/class/config.php");
include($check_path."e/class/db_sql.php");
include($check_path."e/class/user.php");
include($check_path."e/class/MemberLevel.php");
$link=db_connect();
$empire=new mysqlquery();
CheckShowNewsLevel($check_groupid,$check_fh,$check_userfen,$check_newsid,$check_classid);
db_close();
$empire=null;
?>