<?php
require("../../class/connect.php");
require("../../class/q_functions.php");
require("../../member/class/user.php");
require("../../data/dbcache/class.php");
require("../../data/dbcache/MemberLevel.php");
require("../class/DownSysFun.php");
eCheckCloseMods('movie');//關閉模塊
$link=db_connect();
$empire=new mysqlquery();
$editor=1;
$ecmsreurl=2;
//驗證IP
eCheckAccessDoIp('onlineinfo');
$id=(int)$_GET['id'];
$pathid=(int)$_GET['pathid'];
$classid=(int)$_GET['classid'];

//扣點函數
function ViewOnlineKFen($showdown_r,$u,$userid,$classid,$id,$pathid,$r){
	global $level_r,$class_r,$dbtbpre,$public_r,$empire,$have_bak,$have_fen,$emod_pubr;
	$userid=(int)$userid;
	$classid=(int)$classid;
	$id=(int)$id;
	$pathid=(int)$pathid;
	if($showdown_r[2])
	{
		$u['groupid']=(int)$u['groupid'];
		$u['username']=RepPostVar($u['username']);
		$r['title']=RepPostStr($r['title']);
		//下載次數限制
		$setuserday="";
		if($level_r[$u['groupid']]['daydown'])
		{
			$setuserday=DoCheckMDownNum($userid,$u['groupid'],1);
		}
		//點數是否足夠
		$showdown_r[3]=intval($showdown_r[3]);
		if($showdown_r[3])
		{
			if($have_fen==1)
			{
				//去除點數
				$usql=$empire->query("update ".eReturnMemberTable()." set ".egetmf('userfen')."=".egetmf('userfen')."-".$showdown_r[3]." where ".egetmf('userid')."='$userid'");
			}
			if($have_bak==0)
			{
				//備份下載記錄
				$utfusername=$u['username'];
				BakDown($classid,$id,$pathid,$userid,$utfusername,$r['title'],$showdown_r[3],1);
			}
		}
		//更新用戶下載次數
		if($setuserday)
		{
			$usql=$empire->query($setuserday);
		}
	}
	//總下載數據增一
	//年月日統計
	$ymdtotaled='';
	$tid=(int)$class_r[$classid]['tid'];
	if(strstr($emod_pubr['tbidsed'],','.$tid.','))
	{
		$ymdtotaled=eReturnYmdTotalf($r,1);
	}
    $usql=$empire->query("update {$dbtbpre}ecms_".$class_r[$classid]['tbname']." set totaldown=totaldown+1".$ymdtotaled." where id='$id'");
}

/*
來源識別
if(!strstr($_SERVER['HTTP_REFERER'],$public_r['newsurl']))
{
	exit();
}
*/
if(!$classid||empty($class_r[$classid]['tbname'])||!$id||InfoIsInTable($class_r[$classid]['tbname']))
{
	echo"<script>alert('此視頻不存在');window.close();</script>";
	exit();
}
$mid=$class_r[$classid]['modid'];
$tbname=$class_r[$classid]['tbname'];
$query="select * from {$dbtbpre}ecms_".$tbname." where id='$id'".do_dblimit_one();
$r=$empire->fetch1($query);
if(!$r['id']||$r['classid']!=$classid)
{
	echo"<script>alert('此視頻不存在');window.close();</script>";
	exit();
}
//副表
$finfor=$empire->fetch1("select ".ReturnSqlFtextF($mid)." from {$dbtbpre}ecms_".$tbname."_data_".$r['stb']." where id='".$r['id']."'".do_dblimit_one());
$r=array_merge($r,$finfor);
//區分下載地址
$path_r=explode("\r\n",$r['onlinepath']);
if(!$path_r[$pathid])
{
	echo"<script>alert('此視頻不存在');window.close();</script>";
	exit();
}
$showdown_r=explode("::::::",$path_r[$pathid]);
$showdown_r[2]=(int)$showdown_r[2];
$showdown_r[3]=(int)$showdown_r[3];
$showdown_r[4]=(int)$showdown_r[4];
//下載權限
$have_bak=0;
$have_fen=0;
$setuserday="";
$nockpass='';
$user=array();
$u=array();
$downgroup=$showdown_r[2];
if($downgroup)
{
	$user=islogin();
	//取得會員資料
	$u=$empire->fetch1("select ".eReturnSelectMemberF('*')." from ".eReturnMemberTable()." where ".egetmf('userid')."='".$user['userid']."' and ".egetmf('rnd')."='".$user['rnd']."'".do_dblimit_one());
	if(empty($u['userid']))
	{
		echo"<script>alert('同一帳號，只能一人在線');window.close();</script>";
        exit();
    }
	if(empty($u['isot']))
	{
		echo"<script>alert('同一帳號，只能一人在線');window.close();</script>";
        exit();
    }
	$nockpass=qReturnLoginPassNoCK($user['userid'],$user['username'],$user['rnd'],0);
	//下載次數限制
	if($level_r[$u['groupid']]['daydown'])
	{
		$setuserday=DoCheckMDownNum($user['userid'],$u['groupid'],2);
		if($setuserday=='error')
		{
			echo"<script>alert('您的下載與觀看次數已超過系統限制(".$level_r[$u['groupid']]['daydown']." 次)!');window.close();</script>";
			exit();
		}
	}
	if($downgroup>0)//會員組
	{
		if($level_r[$downgroup]['level']>$level_r[$u['groupid']]['level'])
		{
			echo"<script>alert('您的會員級別不足(".$level_r[$downgroup]['groupname'].")，沒有觀看此視頻的權限!');window.close();</script>";
			exit();
		}
	}
	else//訪問組
	{
		$vgroupid=0-$downgroup;
		$ckvgresult=eMember_ReturnCheckViewGroup($u,$vgroupid);
		if($ckvgresult<>'empire.cms')
		{
			echo"<script>alert('您的會員級別不足，沒有觀看此視頻的權限!');window.close();</script>";
			exit();
		}
	}
	//點數是否足夠
	$have_bak=0;
	$have_fen=0;
	if($showdown_r[3])
	{
		//---------是否有歷史記錄
			$bakr=$empire->fetch1("select id,truetime from {$dbtbpre}enewsdownrecord where id='$id' and classid='$classid' and userid='".$user['userid']."' and pathid='$pathid' and online=1 order by truetime desc".do_dblimit_one());
			if($bakr['id']&&(time()-$bakr['truetime']<=$public_r['redodown']*3600))
			{
				$have_bak=1;
			}
			else
			{
				//包月卡
				if($u['userdate']-time()>0)
				{}
				//點數
				else
				{
			       if($showdown_r[3]>$u['userfen'])
			       {
					echo"<script>alert('您的點數不足 ".$showdown_r[3]." 點，無法觀看此視頻');window.close();</script>";
					exit();
			       }
				   $have_fen=1;
				}
			}
	}
}
//驗證碼
$ip=egetip();
$pass=md5(md5($classid."-!ecms!".$id."-!ecms!".$pathid).ReturnDownSysCheckIp()."wm_chief".$public_r['downpass'].$user['userid']);
$op=GetOnlinePass();
$url="../doaction.php?enews=GetSofturl&classid=$classid&id=$id&pathid=$pathid&pass=".$pass."&p=".$user['userid'].":::".$user['rnd'].":::".$nockpass."&onlinetime=".$op[0]."&onlinepass=".$op[1];
$trueurl=ReturnDSofturl($showdown_r[1],$showdown_r[4],'../../',0);//實際地址
$playerpass="wm_chief";
$r['playerid']=(int)$r['playerid'];
//自動識別播放器
$jwplayertype=',.flv,.mp4,';
$htmlvideotype=',.ogg,.mp4,.webm,';
$htmlaudiotype=',.ogg,.mp3,.wav,';
if(empty($r['playerid']))
{
	$ftype=GetFiletype($showdown_r[1]);
	if($ftype=='.swf')
	{
		@include("flasher.php");
	}
	elseif(stristr($htmlvideotype,','.$ftype.','))
	{
		@include("htmlvideo.php");
	}
	elseif(stristr($htmlaudiotype,','.$ftype.','))
	{
		@include("htmlaudio.php");
	}
	elseif(stristr($jwplayertype,','.$ftype.','))
	{
		@include("jwplayer.php");
	}
	elseif($ftype=='.flv')
	{
		@include("flver.php");
	}
	elseif(stristr($ecms_config['sets']['realplayertype'],','.$ftype.','))
	{
		@include("realplayer.php");
	}
	else
	{
		@include("mediaplayer.php");
	}
}
else
{
	$playerr=$empire->fetch1("select filename from {$dbtbpre}enewsplayer where id='".$r['playerid']."'");
	eCheckStrType(5,$playerr['filename'],1);
	if($playerr['filename'])
	{
		@include($playerr['filename']);
	}
}
db_close();
$empire=null;
?>