<?php
/**
 * 播放详情页
 * @copyright reginx.com
 * $Id$
 */
class player_mod extends mogvs_mod{
    
    
    /**
     * 播放报错
     *
     */
    public function errorAct(){
        $vid = intval($this->get('vid'));
        $pid = intval($this->get('uid'));
        $player = $this->get('player');
        if($vid > 0 && isset(config_lib::$player[$player])){
            core::getobj('vod_tab')->where("id = $vid ")->set('err' , $pid+1)->save();
        }
    }

	/**
     * 播放页
     *
     * @param unknown_type $p1
     * @param unknown_type $p2
     */
	public function __call($p1, $p2){
		$p1 = substr($p1, 0, -3);
		list($sname , $player , $uid) = explode('__', $p1);
		$sname = filter::ult($sname);
		if(preg_match('/^[0-9a-zA-Z\_\-]+$/i', $sname)){
			if(is_numeric($sname)){
				$sname = intval($sname);
				$vod = core::getobj('vod_tab')->fields('id,name,sname,cat,desc,key,basename')->where("id = '{$sname}'")->get();
			}else{
				$vod = core::getobj('vod_tab')->fields('id,name,sname,cat,desc,key,basename')->where("sname = '{$sname}'")->get();
			}
			if(empty($vod)){
				$this->show404(1 , '该视频不存在 !');
			}
			$player = strtolower($player);
			$player = isset(config_lib::$player[$player]) ? $player : 'auto';
			$uid = intval($uid);
			$uid = $uid ? $uid : 1;
			$url = core::getobj('vod_url_tab')->where("vid = {$vod['id']}")->order("uid asc")->getall();
			$index = 0;
			$tmp = array();
			foreach($url as $k => $v){
			    $tmp[$v['player']][] = $v;
			    $player = $player == 'auto' ? $v['player'] : $player;
				if($v['uid'] == $uid &&  $player == $v['player']){
					$index = count($tmp[$v['player']]) - 1;
					$player = $v['player'];
					$this->assign('cur', $v);
				}
			}
			$plays = $this->get('plays' , 'C');
			if(!isset($plays[$vod['id']])){
			    core::getobj('vod_tab')->exec("update vod_tab set plays = plays +1 where id = {$vod['id']}");
			}
			setcookie ("plays[{$vod['id']}]", REQUEST_TIME , REQUEST_TIME + 86400);
			if(isset($tmp[$player][$index + 1])){
			    $vod['next'] = $tmp[$player][$index + 1]['uid'];
			}
			if(isset($tmp[$player][$index - 1])){
			    $vod['prev'] = $tmp[$player][$index - 1]['uid'];
			}
			$this->assign('vod', $vod);
			$this->assign('ptitle', '正在播放_' . $vod['name']);
			$this->assign('players', config_lib::$player);
			$this->assign('url', $tmp);
			$this->assign('skey', $vod['key']);
			$this->assign('sdesc', $vod['desc']);
			$this->assign('player' , $player);
			$this->assign('uid' , $uid);
			$this->display('vod/player.tpl.html');
		}
		$this->show404(1 , '该视频不存在 !');
	}
}
?>