<?php
class ContentController extends Easy_Controller_Action {
	public function Init(){
		$this->_View->setViewRoot('templates');
		$this->_View->skin = $this->config['template'];
		$this->_View->Template = new Template();	//
		$this->movcfg = new Easy_Config();
		$this->movcfg->set(ROOT.'Config/movie.ini', 'area');
		$this->_View->moviearea = $this->movcfg->data;
		$this->_View->config = $this->config;
		if ($this->config['run'] == 1){
			exit(_('系统维护中.暂停服务...客服QQ:').$config['qq']);
		}elseif ($this->config['run'] == 0){
			exit(_('点播服务已经关闭'));
		}
	}
	public function categoryAction(){
		$hash = md5($_SERVER['REQUEST_URI']);
		if ($this->config['cache'] && Easy_Cache::isCache($hash) && 
		$this->_Request->getParam('make') !='yes'){
			Easy_Cache::getCache($hash);
		}else{
			$this->_View->orderby = $this->_Request->getParam('orderby') ? $this->_Request->getParam('orderby') : 'movid';
			$this->_View->classid = (int)$this->_Request->getParam('id');
			$cate = new CategoryModel();
			$row = $cate->getRow('id='.(int)$this->_Request->getParam('id'));
			$this->_View->parentid = array_shift(explode(',', $row['lable']));
			foreach($cate->getLocation((int)$this->_Request->getParam('id')) as $rs){
				$uri.= $uri ? ' &raquo; <a href="'.INSTALLDIR.'content/category/id/'.$rs['id'].'/">'.$rs['category'].'</a>' : '<a href="'.INSTALLDIR.'content/category/id/'.$rs['id'].'/">'.$rs['category'].'</a>';
			}
			$this->_View->location = $uri;
			$this->_View->category = $row;
			$this->_View->class = $row['category'];
			unset($uri, $row);
			$contents = $this->_View->render($this->_View->skin.'/list');
			if($this->config['cache']){
				Easy_Cache::set($hash, $contents);	//写入缓存
			}
			if ($this->_Request->getParam('make') == 'yes'){	//页面生成模式
				$IO = new Easy_Filesystem(ROOT);
				$srcname = str_replace('{id}', $this->_View->classid, $this->config['crewrite']);
				$i = (int)$_GET['page'];
				$IO->makedir($srcname.'page/'.$i.'/');
				$filename = ROOT.$srcname.'page/'.$i.'/index.htm';
				$IO->wFile($filename, $contents);
				if ($_GET['page']<2){
					copy($filename, ROOT.$srcname.'index.htm');
				}
				unset($contents, $row);
				echo _('第'.$i.'个页面生成完毕');
				echo '<script>parent.window.scrollTo(0,999);</script>';
				exit;
			}
			echo $contents;
			unset($contents);
			
		}
		exit;
	}
	/*影片内容介绍页面*/
	public function viewAction(){
		$hash = md5($_SERVER['REQUEST_URI']);
		if ($this->config['cache'] && Easy_Cache::isCache($hash) && $this->_Request->getParam('make') !='yes'){
			Easy_Cache::getCache($hash);
		}else{
			$cate = new CategoryModel();
			if (!$this->_Request->getParam('movid')){
				$this->_View->redirect('');
				exit;
			}
			$this->_View->movie = $this->_View->Template->getRow((int)$this->_Request->getParam('movid'));
			$this->_View->movie['lookgroup'] = $this->_View->temporary['movie']['lookgroup'] ? $this->_View->movie['lookgroup'] : '免费';
			$this->_View->movie['downgroup'] = $this->_View->temporary['movie']['downgroup'] ? $this->_View->movie['downgroup'] : '免费';
			$this->_View->classid = (int)$this->_View->movie['category'];
			foreach($cate->getLocation((int)$this->_View->classid) as $rs){
				$uri.= $uri ? ' &raquo; <a href="'.INSTALLDIR.'content/category/id/'.$rs['id'].'/">'.$rs['category'].'</a>' : '<a href="'.INSTALLDIR.'content/category/id/'.$rs['id'].'/">'.$rs['category'].'</a>';
			}
			$this->_View->location = $uri;unset($uri, $rs);
			$contents = $this->_View->render($this->_View->skin.'/display');
			if ($this->config['cache']){
				Easy_Cache::set($hash, $contents);	//写入缓存
			}
			if ($this->_Request->getParam('make') == 'yes'){
				$IO = new Easy_Filesystem(ROOT);
				$dir = str_replace('{movid}', $this->_Request->getParam('movid'), $this->config['mrewrite']);
				$IO->makedir($dir);
				$filename = ROOT.$dir.'/index.htm';
				$IO->wFile($filename, $contents);
				echo $filename.'页面生成成功!';
				exit;
			}
			echo $contents;
			unset($contents);
			
		}exit;
	}
	/*
	播放器地址显示
	*/
	public function frameurlAction(){
		$hash = md5($_SERVER['REQUEST_URI']);
		if ($this->config['cache'] && Easy_Cache::isCache($hash) && $this->_Request->getParam('make') !='yes'){
			Easy_Cache::getCache($hash);
		}else{
			$movid = (int)$this->_Request->getParam('movid');
			$this->_View->num = (int)$this->_Request->getParam('num') ? (int)$this->_Request->getParam('num') : 7;	//显示每多少列就换行
			$info = $this->_View->Template->getRow($movid);
			$this->_View->showurl = $this->_View->Template->getUrls($movid);	//获取影片所有地址
			$player = new PlayerModel();
			$info['player'] = str_replace(',', "','", $info['player']);
			$this->_View->movieplayer = $player->getAll("tag in ('".$info['player']."')");
			$server = new ServerModel();
			$sid = array_map('intval', explode(',', $info['sid']));	//将服务器ID全部数字格式化
			$this->_View->moviename = $info['title'];
			$this->_View->movieserver = $server->getList("locked=0 AND sid in (".implode(',', $sid).")");
			$this->_View->sid = intval($this->_Request->getParam('sid'));
			$contents = $this->_View->render('public/frameurl');
			if($this->config['cache']){
				Easy_Cache::set($hash, $contents);	//写入缓存
				//unset($contents);
			}
			echo $contents;
			unset($contents, $info);
		}
		exit;
	}
	/*下载地址显示*/
	public function downurlAction(){
		$movid = (int)$this->_Request->getParam('movid');
		$this->_View->num = (int)$this->_Request->getParam('num') ? (int)$this->_Request->getParam('num') : 7;	//显示每多少列就换行
		$info = $this->_View->Template->getRow($movid);
		$this->_View->showurl = $this->_View->Template->getUrls($movid);	//获取影片所有地址
		exit($this->_View->render('public/downurl'));
	}
	/*输出播放器页面*/
	public function playAction(){
		
		$urlid = (int)$this->_Request->getParam('urlid');
		require_once(ROOT.'Application/check.class.php');
		$check = new Check();
		$info = $check->checkplayer($urlid);
		if ($info==false){
			$this->_View->message = $check->errorinfo;
			exit($this->_View->render($this->_View->skin.'public/message'));
		}
		/*开始替换播放器标签*/
		if ($info['smil'] ){
			if ($info['tag'] == 'realplayer'){
			$file = INSTALLDIR.'content/smil/id/'.$info['smil'].'/filename/'.
				base64_encode(urlencode($info['server']['helixserver'].$info['url']));
			}elseif($this->_Request->getparam('showin') != 'true'){
				$Smil = new SmilModel();
				$this->_View->player =  $Smil->getPlay($info['smil']);//开始播放器广告
				$this->_View->player = str_replace('{basedir}', $this->config['basedir'], $this->_View->player);
				exit($this->_View->render('public/player'));
			}else{
				$file = $info['server']['helixserver'].$info['url'];
			}
		}else{
			$file = $info['server']['helixserver'].$info['url'];
		}
		//echo $file;
		$this->_View->title = $info['alt'];
		$this->_View->player = '<div id="vodcmsplayer">'.$info['player'].'</div>';
		$this->_View->player = str_replace('{url}', $info['url'], $this->_View->player);
		$this->_View->player = str_replace('{qvod}', $file, $this->_View->player);
		$this->_View->player = str_replace('&mid=', '&hallid=', $this->_View->player);
		$this->_View->player = str_replace('{server}', $info['server'], $this->_View->player);
		$this->_View->player = str_replace('{username}', $info['username'], $this->_View->player);
		$this->_View->player = str_replace('{file}', $file, $this->_View->player);
		$this->_View->player = str_replace('{alt}', $info['alt'], $this->_View->player);
		$this->_View->player = str_replace('{basedir}', $this->config['basedir'], $this->_View->player);
		$this->_View->player = str_replace('{installdir}', INSTALLDIR, $this->_View->player);
		$this->_View->online = $info['online'];
		//$this->_View->player = str_replace('{url}', $info['url'], $this->_View->player);
		/*替换播放器标签结束输出内容*/
		exit($this->_View->render('public/player'));
	}
	/*
	搜索控制输出
	*/
	public function searchAction(){
		$this->_View->location = '节目搜索';
		$this->_View->class = '搜索结果';
		$this->_View->type = $this->_Request->getParams('type');
		$this->_View->keyword = urldecode($this->_Request->getParams('keyword'));
		exit($this->_View->render($this->_View->skin.'/list'));
	}
	/**/
	public function FreeAction(){
		$this->config['sitename'] = _('免费片');
		exit($this->_View->render($this->_View->skin.'/free'));
	}
	/*影片评论*/
	public function commentAction(){
		$comment = new CommentsModel();
		$movie = new MovieModel();
		if ($this->_Request->getParam('delete') && $this->_Request->getCookie('admin')){
			$comment->delete($this->_Request->getParam('delete'));
			message(_('删除成功!'), 'history.back()');
			exit;
		}
		if ($this->_Request->getCookie('admin') && $this->_Request->getSession('id')){
			$this->_View->isadmin = true;
		}
		$this->_View->username = $this->_Request->getCookie('username');
		$this->_View->movid = (int)$this->_Request->getParam('movid');
		$row = $movie->getRow($this->_View->movid);
		$moviename = $row['title'];
		$this->_View->comments = $row['review'];
		if ($this->_Request->isPost()){
			
			$POST = $this->_Request->getPost();
			$POST['username'] = $this->_Request->getCookie('username') ? $this->_Request->getCookie('username') :  _('游客');
			$POST['movie'] = $moviename;
			$POST['locked'] = 0;
			$POST['userip'] = $_SERVER['REMOTE_ADDR'];
			if ($comment->Add($POST)===false){
				message($comment->error, 'history.back()');
				exit;
			}else{
				message(_('评论发布成功!'), 'history.back()');
				exit;
			}
		}else{
			$this->_View->comment = $comment->getAll(array('movid'=>$this->_View->movid));
			$this->_View->printpage = $comment->printPage;
		}
		exit($this->_View->render('public/comments'));
	}
	/*公告显示*/
	public function voteAction(){
		$id = (int)$this->_Request->getParam('id');
		$vote = new AfficheModel();
		$vote->setClick($id);
		$this->_View->vote = $vote->getRow(array('id'=>$id));
		exit($this->_View->render('public/vote'));
	}
	/*广告调用*/
	public function billAction(){
		$tag = $this->_Request->getParam('tag');
		if (empty($tag)===false){
			$bill = new BillModel();
			$row = $bill->getByTag($tag);
			print_r($row);
		}
		exit;
	}
	//*下载影片**/
	public function downAction(){
		$urlid = (int)$this->_Request->getParam('urlid');
		require_once(ROOT.'Application/check.class.php');
		$check = new Check();
		$info = $check->checkdown($urlid);
		if ($info==false){
			$this->_View->message = $check->errorinfo;
			exit($this->_View->render($this->_View->skin.'public/message'));
		}
		$this->_View->filename = $info['server']['ftpserver'].$info['url'];
		exit($this->_View->render('public/down'));
	}
	/*smil影片处理*/
	public function smilAction(){
		$id = (int)$this->_Request->getParam('id');
		$filename = urldecode(base64_decode($this->_Request->getParam('filename')));
		if ($id){
			header("Content-type: application/smil");
			$smil = new SmilModel();
			$row = $smil->getRow(array('id'=>$id));
			if ($row['type'] == 'video'){
				echo '<smil><head></head><body>';
				if ($row['position'] == 0){
					echo '<video src="'.$row['video'].'" dur="'.$row['end'].'s" />';
					echo '<video src="'.$filename.'" />';
				}elseif($row['position'] == 1){
					echo '<video src="'.$filename.'" />';
					echo '<video src="'.$row['video'].'" dur="'.$row['end'].'s" />';
				}elseif($row['position'] == 3){
					//echo '<par>';
					echo '<video id="a" src="'.$filename.'" dur="'.$row['start'].'s" />';
					echo '<video id="b" src="'.$row['video'].'" dur="'.$row['end'].'s" />';
					echo '<video id="c" src="'.$filename.'" clip-begin="'.($row['start']).'s" />';
					//echo '<video id="c" src="d'.$filename.'" begin="'.$row['start']+$row['end'].'s" />';
					//echo '</par>';
				}
				echo '</body>';
				echo '</smil>';
			}else{
				echo $filename;
			}
		}else{
			echo $filename;
		}
		exit;
	}
	public function asxAction(){
	}
	public function vipAction(){
		$User = new UsersModel();
		$uid = intval($this->_Request->getCookie('uid'));
		$row = $User->login($array);
		if ($row['usergroup'] == $this->config['vip']){
			exit($this->_View->render($this->_View->skin.'/vip'));
		}else{
			$this->_Request->redirect('/');
			exit;
		}
	}
	public function helpAction(){
		exit($this->_View->render('public/help'));
	}
}?>