<?php
/*
秀影6.0
验证用户点播权限以及下载权限
checkplayer() 验证用户是否有点播权限，并且完成点播扣费操作。成功后返回影片服务器地址信息数组
checkdown() 验证用户下载权限，并且完成扣费操作。成功返回服务器地址信息数组
*/
class check {
	public $DB;
	public $Server,$config,$PlayerLog,$Player,$Request;
	public function __construct(){
		$this->config = $GLOBALS['config'];
		$this->Server = new ServerModel();
		$this->PlayerLog = new PlaylogModel();
		$this->Player = new PlayerModel();
		$this->Request = new Easy_Controller_Request();
		$this->DB = $GLOBALS['DB'];
	}
	/*
	@param int $urlid
	*/
	public function checkplayer($urlid){
		
		$netbar = new NetbarModel();	//初始网吧模型
		
		$uid = (int)$this->Request->getCookie('uid');		//用户的UID
		$server = (int)$this->Request->getParam('server');	//服务器编号
		$player = trim($this->Request->getParam('player'));	//指定播放器
		$userip = $this->Request->strUserIp();
		$bar = $netbar->isAllow($userip);
		$select = $this->DB->select();
		$select->from(VODCMS_URL.' AS a', 'a.url as url, a.title as alt');
		$select->join(VODCMS_MOVIE.' AS b', 'a.movid=b.movid','b.movid as movid,b.title as title,b.lookgid as gid,
		 b.lookpoint as point,b.sid as sid,b.player as player,b.smil as smil');
		$select->join(VODCMS_USERGROUP.' AS c', 'b.lookgid=c.id', 'c.`flag` as flag, c.`maxlook` as maxlook, c.`repeat` as rep ');
		$where = 'b.locked=0 AND b.look=1 AND a.urlid='.(int)$urlid;
		$select->where($where);

		$sql = $select->toString();
		$row = $this->DB->fetRow($sql);
		if ($server == 0){	//未指定服务器时
			$server = explode(',', $row['sid']);
			$rand_keys = array_rand($server);
			$server = (int)$server[$rand_keys];	//随机取出一个服务器
		}
		if (empty($player)){
			$player = explode(',', $row['player']);
			$player = array_shift($player);
		}
		if ($player){
			$movie['tag'] = $player;
			$movie['player'] = $this->Player->getPlayer($player);
		}else{
			exit('影片未设置播放器');
		}
		$movie['alt'] = $row['title'].'-'.$row['alt'];
		$movie['gid'] = $row['gid'];
		$select = $this->DB->select();
		$select->from(VODCMS_MEMBERS.' AS a', 'a.*');
		$select->join(VODCMS_USERGROUP.' AS b', 'a.usergroup = b.id', 'b.*');
		$sql = $select->where('a.uid ='.(int)$uid)->limit(1)->toString();
		$user = $this->DB->fetRow($sql);
		$username = $user['username'];
		if ($user['uid']>0){	//用户已经登陆则需要判断用户权限
			$state =0;
			if ($user['flag'] < $row['flag'] || empty($user['usergroup'])){	//用户的观看权限小于影片的组权限则提示不能观看
				$this->errorinfo = _('您的用户权限无法观看这部影片!请升级您的账户组权限!');
				message($this->errorinfo,"location.href='".INSTALLDIR."member/payment/';window.close();");
				exit;
			}
			if ((int)$user['usertype'] == 1){	//判断用户是否是包月模式
				if ($user['lifetime']<=time()){	//用户账户的包月已时间到期
					$this->errorinfo = _('您的账户包月已到期!请续费！');
					message($this->errorinfo,"location.href='".INSTALLDIR."member/payment/';window.close();");
					exit;
				}else{
					if (date('Ymd',$user['playtime']) < date('Ymd')){
						$array = array();
						$array['plays'] = 1;
						$array['playtime'] = time();
						//更新会员点播时间
					}else{
						$array['plays'] = $user['plays'] + 1;
						$array['playtime'] = time();
					}
		
					if($row['maxlook'] <= $user['plays'] && $row['maxlook']>0 ){
						$this->errorinfo = _('您的今日点播影片数超过了组权限的最高单日点播数'.$row['repeat'].'!请续费！');
						message($this->errorinfo, "location.href='".INSTALLDIR."member/payment/';window.close();");
						exit;
					}
				}//end if 结束到期时间判断
			}else{
				if ( $row['point'] > $user['point']){
					$this->errorinfo = _('您的账户余额不足!请充值!');
					message($this->errorinfo, "location.href='".INSTALLDIR."member/payment/';window.close();");
					exit;
				}
				$array['point'] = $user['point']-$row['point'];
				$array['plays'] = $user['plays'] + 1;
				$array['playtime'] = time();
			}
			$this->DB->update(VODCMS_MEMBERS, $array, 'uid='. $user['uid']);
			//echo $this->DB->sql;
			unset($array);
		}elseif ( count($bar)>0 ){	//在网吧IP范围中
			$state = 1;
			$username = $bar['name'];
			print_r($bar);
			exit;
			if ($bar['maxnum'] >0){
				$Active = new PlaylogModel();
				if ($Active->checkNetbarMax($username, $bar['maxnum'])===false){
					$this->errorinfo = _('您的网吧点播用户数超过购买客户端数!请稍后刷新抢线');
					message($this->errorinfo, 'window.close();');
				}
			}
		}elseif ($this->config['auth'] == 0 || $row['gid'] == 0){
			//站点设置为免费模式或者影片本身为免费片时
			//echo $movie['gid'];
			$username = '游客'.mt_rand(10000,99999);
			$state = 2;
			
		}else{//要登陆模式
			$this->errorinfo = _('您还没有登陆!请登陆后点播');
			$uri = base64_encode($_SERVER['REQUEST_URI']);
			message($this->errorinfo,"location.href='".INSTALLDIR."member/validate/uri/".$uri."'");
			exit;
		}
		$this->DB->update(VODCMS_MOVIE, 'click=click+1', array('movid'=>$row['movid']));	//更新影片浏览数
		//echo $this->DB->sql;
		if ($this->config['clearlog']){	//开启日志功能时
			$array = array();	//开始插入点播记录
			$array['username'] = $username;
			$array['userip'] = $netbar->ip2long($userip);
			$array['urlid'] = $urlid;
			$array['addtime'] = time();
			$array['point'] = $point;
			$array['action'] = '点播';
			$array['server'] = $server;
			$this->PlayerLog->setlook($array);	//写入点播日志完成
		}
		if ($this->config['refreshplayer']){
			$array = array();
			$array['username'] = $username;
			$array['userip'] = $netbar->ip2long($userip);
			$array['firstime'] = time();
			$array['lastime'] = time();
			$array['urlid'] = $urlid;
			$array['server'] = $server;
			$id = $this->PlayerLog->setActive($array);	//写入当前点播记录表
			unset($array);
		}
		$movie['url'] = $row['url'];
		$movie['smil'] = $row['smil'];
		$movie['online'] = 'token/'.base64_encode('id='.$id.'&state='.$state.'&'.username.'='.urlencode($username));
		$row = $this->Server->getRow($server);
		$movie['server'] = $row;
		unset($row);
		return $movie;		
	}
	/*
	验证下载
	*/
	public function checkdown($urlid){
		$uid = (int)$this->Request->getCookie('uid');
		$server = (int)$this->Request->getParam('server');
		//$player = trim($this->Request->getParam('player'));
		$userip = $this->Request->strUserIp();
		$select = $this->DB->select();
		$select->from(VODCMS_URL.' AS a', 'a.url as url, a.title as alt');
		$select->join(VODCMS_MOVIE.' AS b', 'a.movid=b.movid',
		 'b.movid as movid,b.title as title,b.downgid as gid,
		 b.downpoint as point,b.sid as sid,b.player as player,b.smil as smil');
		 $select->join(VODCMS_USERGROUP.' AS c', 'b.downgid=c.id', 'c.`flag` as flag, c.`maxlook` as maxlook, c.`repeat` as rep ');
		$where = 'b.locked=0 AND b.down=1 AND a.urlid='.(int)$urlid;
		$select->where($where);
		$sql = $select->toString();
		$row = $this->DB->fetRow($sql);
		if ($server == 0){	//未指定服务器时
			$server = explode(',', $row['sid']);
			$rand_keys = array_rand($server);
			$server = (int)$server[$rand_keys];	//随机取出一个服务器
		}
		$movie['alt'] = $row['title'].'-'.$row['alt'];
		$movie['gid'] = $row['gid'];
		$select = $this->DB->select();
		$select->from(VODCMS_MEMBERS.' AS a', 'a.*');
		$select->join(VODCMS_USERGROUP.' AS b', 'a.usergroup = b.id', 'b.*');
		$sql = $select->where('a.uid ='.(int)$uid)->limit(1)->toString();
		$user = $this->DB->fetRow($sql);
		$username = $user['username'];
		if ($user['uid']>0){	//用户已经登陆则需要判断用户权限
			$state =0;
			if ($user['flag'] < $row['flag'] || empty($user['usergroup'])){	//用户的观看权限小于影片的组权限则提示不能观看
				$this->errorinfo = _('您的用户权限无法下载这部影片!请升级您的账户组权限!');
				message($this->errorinfo,"location.href='".INSTALLDIR."member/payment/';window.close();");
				exit;
			}
			if ((int)$user['usertype'] == 1){	//判断用户是否是包月模式
				if ($user['lifetime']<=time()){	//用户账户的包月已时间到期
					$this->errorinfo = _('您的账户包月已到期!请续费！');
					message($this->errorinfo,"location.href='".INSTALLDIR."member/payment/';window.close();");
					exit;
				}else{
					if (date('Ymd',$user['playtime']) < date('Ymd')){
						$array = array();
						$array['plays'] = 1;
						$array['playtime'] = time();
						//更新会员点播时间
					}else{
						$array['plays'] = $user['plays'] + 1;
						$array['playtime'] = time();
					}
		
					if($row['maxlook']<=$user['plays'] && $row['maxlook'] > 0){
						$this->errorinfo = _('您的今日下载影片数超过了组权限的最高单日下载数'.$row['repeat'].'!请续费！');
						message($this->errorinfo, "location.href='".INSTALLDIR."member/payment/';window.close();");
						exit;
					}
				}//end if 结束到期时间判断
			}else{
				if ( $row['point'] > $user['point']){
					$this->errorinfo = _('您的账户余额不足!请充值!');
					message($this->errorinfo, "location.href='".INSTALLDIR."member/payment/';window.close();");
					exit;
				}
				$array['point'] = $user['point']-$row['point'];
				$array['plays'] = $user['plays'] + 1;
				$array['playtime'] = time();
			}
			$this->DB->update(VODCMS_MEMBERS, $array, 'uid='. $user['uid']);
			//echo $this->DB->sql;
			unset($array);
		}elseif ($this->config['auth'] == 0 || $row['gid'] == 0){
			//站点设置为免费模式或者影片本身为免费片时
			//echo $movie['gid'];
			$username = '游客'.mt_rand(10000,99999);
			$state = 2;
			
		}else{//要登陆模式
			$this->errorinfo = _('您还没有登陆!请登陆后下载');
			$uri = base64_encode($_SERVER['REQUEST_URI']);
			message($this->errorinfo,"location.href='".INSTALLDIR."member/validate/uri/".$uri."'");
			exit;
		}
		$this->DB->update(VODCMS_MOVIE, 'click=click+1', array('movid'=>$row['movid']));	//更新影片浏览数
		//echo $this->DB->sql;
		if ($this->config['clearlog']){	//开启日志功能时
			$array = array();	//开始插入点播记录
			$array['username'] = $username;
			$array['userip'] = sprintf('%u', ip2long($userip));
			$array['urlid'] = $urlid;
			$array['addtime'] = time();
			$array['point'] = $point;
			$array['action'] = '下载';
			$array['server'] = $server;
			$this->PlayerLog->setlook($array);	//写入点播日志完成
		}
		$movie['url'] = $row['url'];
		$movie['smil'] = $row['smil'];
		$movie['online'] = 'token/'.base64_encode('id='.$id.'&state='.$state.'&'.username.'='.urlencode($username));
		$row = $this->Server->getRow($server);
		$movie['server'] = $row;
		unset($row);
		return $movie;		
	}
}