<?php
include "../include/conn.php";
include "../include/function.php";
include "../include/userset.php";
date_default_timezone_set('PRC');
checkip();
if(isset($_COOKIE["S_Name"])){
	$S_Name=$_COOKIE["S_Name"];
}
else{
	$S_Name="";
}
if(isset($_COOKIE["S_PWD"])){
	$S_PWD=$_COOKIE["S_PWD"];
}
else{
	$S_PWD="";
}
$S_ID=$_GET["id"];
$art=$_GET["art"];
$tishi="";
if(S_VipFun==1){
	if($art==1){
		$sqlv="select * from s_article where S_ID=".$S_ID;
	}
	else{
		$sqlv="select * from s_video where S_ID=".$S_ID;
	}
	
	$resultv=$db->query($sqlv);
	if($rowv=$db->fetch_array($resultv)){
		if($rowv["S_PurView"]==""){
			recordvideo($S_Name,$S_ID,0,$art);
		}
		else{
			if(substr($rowv["S_PurView"],0,1)==0){
				recordvideo($S_Name,$S_ID,0,$art);
			}
			else{
				if(($S_Name=="")||($S_PWD=="")){
					?>alert('请先登陆');window.location='<?php echo S_SiteDomain.S_SitePath ?>';<?php
				}
				else{///////////////////////////////////////
					$sql="select * from s_users where S_Name='".$S_Name."'";
					$result=$db->query($sql);
					if($row=$db->fetch_array($result)){
						if($row["S_PassWord"]!=$S_PWD){
							?>alert('请重新登陆');window.location='<?php echo S_SiteDomain.S_SitePath ?>';<?php
						}
						else{
							if($row["S_State"]==0){
								$sqlug="select * from s_usersgroup where S_ID=".$row["S_Type"];
								$resultug=$db->query($sqlug);
								$rowug=$db->fetch_array($resultug);
								if($art==1){
									$S_PurViewUG=$rowug["S_PurViewA"];
								}
								else{
									$S_PurViewUG=$rowug["S_PurView"];
								}
								if(($S_PurViewUG==0)||($S_PurViewUG=="")){
									?>alert('您所在的会员组不能观看该分类!');window.location='<?php echo S_SiteDomain.S_SitePath ?>';<?php
								}
								else{
									$S_PurViewV=explode(",",$rowv["S_PurView"]);
									for($i=0;$i<count($S_PurViewV);$i++){
										if($S_PurViewV[$i]==$row["S_Type"]){
											$tishi="a";	
										}
									}
									if($tishi=="a"){
										$tishi="";
										$S_PurViewUGArr=explode(",",$S_PurViewUG);
										for($i=0;$i<count($S_PurViewUGArr);$i++){
											if($S_PurViewUGArr[$i]==$rowv["S_ClassID"]){
												$tishi="b";
											}
										}
										if($tishi=="b"){
											if(($rowug["S_PurViewT"]==0)||($rowug["S_PurViewT"]=="")){
												?>alert('您所在的会员组不能在该时段观看!');window.location='<?php echo S_SiteDomain.S_SitePath ?>';<?php
											}
											else{
												//die($rowug["S_PurViewT"]);
												$tishi="";
												$S_PurViewT=explode(",",$rowug["S_PurViewT"]);
												for($i=0;$i<count($S_PurViewT);$i++){
													if($S_PurViewT[$i]==date('G')){
														$tishi="c";
													}
												}
												if($tishi=="c"){
													if($row["S_UseType"]==0){//点卡会员
														if($row["S_Coin"]>=$rowug["S_DisCount"]){
															if($row["S_Group"]==1){
																if($row["S_StartIP"]!=getenv('REMOTE_ADDR')){
																	?>alert('IP错误，请确认您是在网吧内观看!');window.location='<?php echo S_SiteDomain.S_SitePath ?>';<?php
																}
																else{
																	recordvideo($S_Name,$S_ID,$rowug["S_DisCount"],$art);
																}
															}
															else{
																recordvideo($S_Name,$S_ID,$rowug["S_DisCount"],$art);
															}
														}
														else{
															?>alert('您的余额不足，请充值!');window.location='<?php echo S_SiteDomain.S_SitePath ?>';<?php
														}
													}
													else{//月卡会员
														$time1=$row["S_UseDate"];
														$time2=date('Y-m-d');
														$diff=(strtotime($time1)-strtotime($time2))/(60*60*24);
														if($diff>=0){
															if($row["S_Group"]==1){
																if($row["S_StartIP"]!=getenv('REMOTE_ADDR')){
																	?>alert('IP错误，请确认您是在网吧内观看!');window.location='<?php echo S_SiteDomain.S_SitePath ?>';<?php
																}
																else{
																	recordvideo($S_Name,$S_ID,0,$art);
																}
															}
															else{
																recordvideo($S_Name,$S_ID,0,$art);
															}
		
														}
														else{
															?>alert('您的帐号已到期，请充值!');window.location='<?php echo S_SiteDomain.S_SitePath ?>';<?php
														}
													}
												}
												else{
													?>alert('您所在的会员组不能在该时段观看!');window.location='<?php echo S_SiteDomain.S_SitePath ?>';<?php
												}
											}
										}
										else{
											?>alert('您所在的会员组不能观看该分类!');window.location='<?php echo S_SiteDomain.S_SitePath ?>';<?php
										}
									}
									else{
										?>alert('您所在的会员组不能观看!');window.location='<?php echo S_SiteDomain.S_SitePath ?>';<?php
									}
								}
							}
							else{
								?>alert('对不起，您的账号被锁定!');window.location='<?php echo S_SiteDomain.S_SitePath ?>';<?php
							}
						}
					}
					else{
						?>alert('请先登陆!');window.location='<?php echo S_SiteDomain.S_SitePath ?>';<?php
					}

				}///////////////////////////////////////////
			}
		}
	}
	else{
		?>alert('数据不存在!');window.location='<?php echo S_SiteDomain.S_SitePath ?>';<?php
	}
}


function checkip(){
	global $db;
	$ip=getenv('REMOTE_ADDR');	
	$sql="select * from s_users where S_Group=1 and S_StartIP='".$ip."'";
	$result=$db->query($sql);
	if($row=$db->fetch_array($result)){
		setcookie("S_Name",$row["S_Name"],time()+3600);
		setcookie("S_PWD",$row["S_PassWord"],time()+3600);
	}
	else{
	
	}
}

function recordvideo($S_Name,$S_ID,$S_DisCount,$art){//保存观看记录
	if($S_Name!=""){
		global $db;
		$tishi="";
		if($art==1){
			$S_ID="a".$S_ID;
		}
		else{
			$S_ID="v".$S_ID;
		}
		$sql="select * from s_users where S_Name='".$S_Name."'";
		$result=$db->query($sql);
		$row=$db->fetch_array($result);
		$S_Coin=$row["S_Coin"] - $S_DisCount;
			if($row["S_WatchRec"]==""){
				$sqlup="update s_users set S_WatchRec='".$S_ID."',S_Coin=".$S_Coin." where S_Name='".$S_Name."'";
				$db->query($sqlup);
			}
			else{
				$S_WatchRecArr=explode(",",$row["S_WatchRec"]);
				
				for($i=0;$i<count($S_WatchRecArr);$i++){
					if($S_ID==$S_WatchRecArr[$i]){
						$tishi="e";
					}
				}
				if($tishi=="e"){
				}
				else{
					if(count($S_WatchRecArr)>=199){
	$S_WatchRec=substr($row["S_WatchRec"],0,strpos($row["S_WatchRec"],$S_WatchRecArr[199])-3);
						$S_WatchRec=$S_ID.",".$S_WatchRec;  //记录观看记录
					}
					else{
						$S_WatchRec=$S_ID.",".$row["S_WatchRec"];  //记录观看记录
					}
					$sqlup="update s_users set S_WatchRec='".$S_WatchRec."',S_Coin=".$S_Coin." where S_Name='".$S_Name."'";
					$db->query($sqlup);
				}
			}
	}
}
?>