<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Vod extends CI_Controller {

	function __construct(){
		parent::__construct();
        	$this->load->helper('url');
        	$this->load->helper('form');
                $this->load->library('session'); //加载session
		$this->load->model('CsdjDB');
		$this->load->model('CsdjSkins');
		$this->load->model('CsdjUser');
		$this->CsdjUser->User_Login();
	}

	public function index()
	{
                $data='';
                $yid = intval($this->input->get('yid', TRUE));   //yid，1为分享，2为待审核，3为回收站
                $cid = intval($this->input->get('cid', TRUE));   //cid
                $page = intval($this->input->get('page', TRUE));   //page
                if($page==0) $page=1;


                $this->load->get_user_templates();
		$template=$this->load->view('vod-list.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","视频 - 会员管理中心",$template);
		$Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		$Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		$Mark_Text=str_replace("[vod:cid]",$cid,$Mark_Text); //CID
		$Mark_Text=str_replace("[vod:yid]",$yid,$Mark_Text); //YID

		//预先除了分页
		$pagenum=$this->CsdjSkins->GetPageNum($Mark_Text);
		preg_match_all('/{cscms:vod(.*?pagesize=([\S]+).*?)}([\s\S]+?){\/cscms:vod}/',$Mark_Text,$page_arr);//判断是否有分页标识
		if(!empty($page_arr) && !empty($page_arr[2])){

                        $sqlstr="select * from ".CS_SqlPrefix."vod where CS_User='".$this->session->userdata('cs_name')."'";
                        if($yid==3){
                             $sqlstr.=" and cs_hid=1";
                        }else{
                             $sqlstr.=" and cs_hid=0";
                        }
                        if($yid==1 || $yid==2){
                            $yid=$yid-1;
                            $sqlstr.=" and cs_yid='$yid'";
                        }
                        if($cid>0){
                            $sqlstr.=" and cs_cid='$cid'";
                        }

                        $sqlstr.=" order by CS_AddTime desc";
                        $data_content='';
			$Arr=$this->CsdjSkins->SpanUsersPage($sqlstr,$page_arr[2][0],$pagenum,'user/vod?cid='.$cid.'&yid='.$yid,$page);//sql,每页显示条数
			$result=$this->CsdjDB->db->query($Arr[2]);
			$recount=$result->num_rows();
			if($recount==0){
				$data_content="<div align=center><strong><font style=font-size:12px;>暂无数据！</font></strong></div>";
			}else{
				if($result){
					$sorti=1;
					foreach ($result->result() as $row2) {
						$datatmp=$this->CsdjSkins->DataVod($page_arr[0][0],$page_arr[3][0],$row2,$sorti);
						$sorti=$sorti+1;
						$data_content.=$datatmp;
					}
				}
			}// end if recount
			$Mark_Text=$this->CsdjSkins->Page_Mark($Mark_Text,$Arr);	
			$Mark_Text=str_replace($page_arr[0][0],$data_content,$Mark_Text);
			unset($Arr);
                }
		unset($page_arr);
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
	        $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
                $this->CsdjSkins->skins($Mark_Text,$data);
        }

	public function add()
	{
                $data='';
                $this->load->get_user_templates();
		$template=$this->load->view('vod-add.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","视频 - 会员管理中心",$template);
		$Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		$Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		$Mark_Text=str_replace("[user:vodaddsave]",site_url('user/vod/add_save'),$Mark_Text); //发表连接
        $token=md5(uniqid(rand(), true));
		$this->session->set_userdata('token',$token);
		$Mark_Text=str_replace("[user:token]",$token,$Mark_Text);

                $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');

                if($row[0]->CS_Vip==0 && User_VodNum>0){
		       $Mark_Text=str_replace("[user:vodaddcount]",User_VodNum,$Mark_Text); //普通会员限制视频数量
                }else{
		       $Mark_Text=str_replace("[user:vodaddcount]",'不限制',$Mark_Text); //VIP会员限制视频数量
                }
		$Mark_Text=str_replace("[user:dayaddcount]",$this->CsdjSkins->getusercount($row[0]->CS_Name,'vod','day'),$Mark_Text); //会员今日上传数量
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
                $this->CsdjSkins->skins($Mark_Text,$data);
        }

	public function add_save()
	{
                $vod['cs_name']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_name', TRUE));   //名称
                $vod['cs_cid']=intval($this->input->post('cs_cid', TRUE));   //分类
                $vod['cs_tid']=intval($this->input->post('cs_tid', TRUE));   //专集
                $vod['cs_tags']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_tags', TRUE));   //关键词
                $vod['cs_cion']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_cion', TRUE));   //金币
                $vod['cs_pic']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_pic', TRUE));   //图片
                $vod['cs_yany']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_yany', TRUE));   //主演
                $vod['cs_daoy']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_daoy', TRUE));   //导演
                $vod['cs_yuyan']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_yuyan', TRUE));   //语言
                $vod['cs_year']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_year', TRUE));   //年份
                $vod['cs_laiy']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_laiyuan', TRUE));   //来源
                $vod['cs_content']=$this->CsdjSkins->uhtml($this->input->post('cs_content'));   //介绍
                $cs_playurl=$this->CsdjSkins->str_checkhtml($this->input->post('cs_playurl', TRUE));   //地址

				//token check
                $token=$this->input->post('token', TRUE);
				if(!$this->session->userdata('token') || $token!=$this->session->userdata('token'))  $this->CsdjSkins->Msg_url('非法提交数据!','javascript:history.back();'); 

                if(empty($vod['cs_name']))  $this->CsdjSkins->Msg_url('视频名称不能为空!','javascript:history.back();'); 
                if(empty($vod['cs_pic']))  $this->CsdjSkins->Msg_url('请选上传图片!','javascript:history.back();'); 
                if($vod['cs_cid']==0)  $this->CsdjSkins->Msg_url('请选择视频分类!','javascript:history.back();'); 
                if(empty($vod['cs_laiy']))  $this->CsdjSkins->Msg_url('请选择视频来源!','javascript:history.back();'); 
                if(empty($cs_playurl))  $this->CsdjSkins->Msg_url('请选上传歌曲!','javascript:history.back();'); 
				if(!preg_match("/^[a-z | 0-9 | A-Z| \.\_\/]+$/",$vod['cs_pic']))  $this->CsdjSkins->Msg_url('请先上传图片!','javascript:history.back();'); 


                //判断视频地址来源
                if($vod['cs_laiy']!='flv' && $vod['cs_laiy']!='media' && $vod['cs_laiy']!='qvod' && $vod['cs_laiy']!='yyxf' && $vod['cs_laiy']!='xgvod' && $vod['cs_laiy']!='jjvod' && $vod['cs_laiy']!='bdhd' && $vod['cs_laiy']!='swf'){
		              $this->load->model('CsdjAdmin');
                      $swfurl=$this->CsdjUser->vodcaiji($cs_playurl);
                      if($swfurl=='no'){
                           $this->CsdjSkins->Msg_url('该视频地址有错误或不支持直接播放：'.$cs_playurl.'!','javascript:history.back();'); 
                      }else{
                           $cs_playurl=$swfurl;
                      }
                }
                //构造视频地址
                $vod['cs_playurl']='第1集$'.$cs_playurl.'$'.$vod['cs_laiy'];

                //判断视频是否存在，如果存在则增加数据播放
                $sql="SELECT CS_ID,CS_PlayUrl FROM ".CS_SqlPrefix."vod where cs_name='".$vod['cs_name']."'";
	            $row=$this->CsdjDB->get_all($sql); 
	            if($row){
                       $zu=0;
                       $ji='1';
                       $Arr=explode('#cscms#',$row[0]->CS_PlayUrl);
                       for($i=0;$i<count($Arr);$i++){
                               $Arr2=explode('$',$Arr[$i]);
                               if($Arr2[2]==$vod['cs_laiy']){  //相同来源
                                      $zu=$i;
                                      $ji=count($Arr2);
                                      break;
                               }
                       }
                       $newszu=$Arr[$zu]."  \r\n";
                       $newszu.='第'.$ji.'集$'.$cs_playurl.'$'.$vod['cs_laiy'];
                       $vod['cs_playurl']=str_replace($Arr[$zu],$newszu,$row[0]->CS_PlayUrl);
                }


                $this->load->helper('text');
                $vod['CS_Intro']=sub_str($vod['cs_content'],200);  //视频导读

                if(empty($vod['cs_tid'])) $vod['cs_tid']=0;
                if(empty($vod['cs_cion']) || $vod['cs_cion']<0)  $vod['cs_cion']=0;
                $vod['CS_User']=$this->session->userdata('cs_name');
                $vod['CS_AddTime']=date('Y-m-d H:i:s');

                //判断审核
                if(User_VodFun==1){
                    $title='，请等待管理员审核';
                    $vod['CS_YID']=1;
                    $url=site_url('user/vod').'?yid=2';
                }else{
                    $title='';
                    $vod['CS_YID']=0;
                    $url=site_url('user/vod');
                }

                //获取当天上传成功数量
                $daycount=$this->CsdjSkins->getusercount($this->session->userdata('cs_name'),'vod','day');
                if(User_VodNum>0 && $daycount >= User_VodNum){
                     $this->CsdjSkins->Msg_url('今天已经上传了('.$daycount.')部视频,明天在传吧!','javascript:history.back();'); 
                }

                $res=$this->CsdjDB->get_insert('vod',$vod);
                if($res>0){

                       if($daycount < Cion_Num && Cion_UpVod>0 && User_VodFun==2){  //奖励金币
                              $title.='，系统奖励给您'.Cion_UpVod.'个金币';
                              $this->db->query("update ".CS_SqlPrefix."user set CS_Cion=CS_Cion+".Cion_UpVod." where CS_ID='".$this->session->userdata('cs_id')."'");
                       }

                       //写入动态
                       $dt['CS_CID']=2;
                       $dt['CS_DID']=$res;
                       $dt['CS_YID']=$vod['CS_YID'];
                       $dt['CS_User']=$this->session->userdata('cs_name');
                       $dt['CS_Title']=$vod['cs_name'];
                       $dt['CS_AddTime']=date('Y-m-d H:i:s');
                       $this->CsdjDB->get_insert('dt',$dt);
                       $this->session->unset_userdata('token');
                       $this->CsdjSkins->Msg_url('恭喜您，视频发表成功'.$title.'!',$url); 

                }else{
                       $this->CsdjSkins->Msg_url('抱歉，视频发表失败，请联系管理员!','javascript:history.back();'); 
                }
        }

	public function del()
	{
                $id=intval($this->input->get('id', TRUE));   //id
                $hid=intval($this->input->get('hid', TRUE));   //hid 等于1时为还原
                if($id==0) $this->CsdjSkins->Msg_url('抱歉，参数错误，ID为空!','javascript:history.back();'); 
 
                if($hid==1){
                     $this->db->query("update ".CS_SqlPrefix."vod set cs_hid=0 where cs_user='".$this->session->userdata('cs_name')."' and cs_id='".$id."'");
                     $this->CsdjSkins->Msg_url('恭喜您，还原成功!',@$_SERVER['HTTP_REFERER']);  
                }else{
                    $this->db->query("update ".CS_SqlPrefix."vod set cs_hid=1 where cs_user='".$this->session->userdata('cs_name')."' and cs_id='".$id."'");
                    $this->CsdjSkins->Msg_url('恭喜您，删除成功!',@$_SERVER['HTTP_REFERER']);  
                }
        }
}


