<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
@set_time_limit(0);
class Upload 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->library('ftp');
                $this->load->helper('string');
		$this->load->model('CsdjSkins');
		$this->load->model('CsdjUser');
        $this->load->helper('qiniu');
	}

	public function index()
	{
	      $this->CsdjUser->User_Login();
              $data['ac'] = $this->input->get_post('ac', TRUE);   //ac，dance为歌曲，vod为视频
              $data['fhurl'] = $this->input->get_post('fhurl', TRUE);   //返回URL框
              $data['dxurl'] = $this->input->get_post('dxurl', TRUE);   //返回URL框
              $data['yzurl'] = $this->input->get_post('yzurl', TRUE);   //返回URL框
              $data['scurl'] = $this->input->get_post('scurl', TRUE);   //返回URL框
              $data['msgurl'] =$this->input->get_post('msgurl', TRUE);   //返回提示框
              if($data['ac']=='dance'){
                   $data['types']="*.flv;*.f4a;*.m4a;*.mp4;*.mp3;*.wma";
                   $data['size']="200MB";
                   $data['title']="歌曲文件";
                   $data['limit']=1;
              }else{
                   $data['types']="*.flv;*.swf;*.mp4;*.wmv;*.asf;*.avi";
                   $data['size']="2000MB";
                   $data['title']="视频文件";
                   $data['limit']=1;
              }
              if(empty($data['fhurl'])) $data['fhurl']='cs_playurl';

              if(FTP_Fun==1){  //开启远程附件
                   $data['uplink']=site_url('user/upload/ftp_save').'?ac='.$data['ac'].'&fhurl='.$data['fhurl'].'&dxurl='.$data['dxurl'].'&scurl='.$data['scurl'].'&yzurl='.$data['yzurl'].'&msgurl='.$data['msgurl'];
                   $this->load->view('upload_ftp',$data);

			  }elseif(CS_Qn_Mode==1){  //七牛

			         $data['token']=get_token();
                     $this->load->view('upload_qiniu',$data);


              }else{
                   $data['uplink']=site_url('user/upload/upload_save').'?ac='.$data['ac'];
                   $this->load->view('upload_duo',$data);
              }
        }

	public function pic()
	{
	      $this->CsdjUser->User_Login();
              $data['ac'] = $this->input->get_post('ac', TRUE);   //ac，topic为专集，vod为视频，pic为图片
              $data['msgurl'] = $this->input->get_post('msgurl', TRUE);   //返回提示框
              $this->load->view('upload_pic', $data);
        }

	public function pic_save()
	{
	         $this->CsdjUser->User_Login();
                 $op   = $this->input->get('ac', TRUE);
                 $msgurl   = $this->input->get('msgurl', TRUE);

                 if($op=='topic'){  //专集图片
                       $config['upload_path'] = './attachment/topic/'.date('Ymd').'/';
                       $config['allowed_types'] = 'gif|jpg|png';
                 }elseif($op=='vod'){  //视频图片
                       $config['upload_path'] = './attachment/vod/'.date('Ymd').'/';
                       $config['allowed_types'] = 'gif|jpg|png';
                 }elseif($op=='pic'){  //图片
                       $config['upload_path'] = './attachment/pic/'.date('Ymd').'/';
                       $config['allowed_types'] = 'gif|jpg|png';
                 }elseif($op=='music'){  //歌曲图片
                       $config['upload_path'] = './attachment/music/'.date('Ymd').'/';
                       $config['allowed_types'] = 'gif|jpg|png';
                 }elseif($op=='bgpic'){  //主页背景
                       $config['upload_path'] = './attachment/bgpic/'.date('Ymd').'/';
                       $config['allowed_types'] = 'gif|jpg|png';
                 }elseif($op=='toppic'){  //主页背景
                       $config['upload_path'] = './attachment/toppic/'.date('Ymd').'/';
                       $config['allowed_types'] = 'gif|jpg|png';
                 }else{  //文章图片
                       $config['upload_path'] = './attachment/news/'.date('Ymd').'/';
                       $config['allowed_types'] = 'gif|jpg|png';
                 }


	         !is_dir($config['upload_path']) && mkdir($config['upload_path'], 0755, true);  //创建目录
                 $config['encrypt_name'] = TRUE;  //重命名
                 $this->load->library('upload', $config);
                 if ( ! $this->upload->do_upload()){
                            $error = array('error' => $this->upload->display_errors());
                            exit($error['error']);
                 } else{
                            $data = $this->upload->data();
                            if(!file_exists($config['upload_path'].'index.html')){
	                         @file_put_contents($config['upload_path'].'index.html','cscms');
                            }
                            $targetFile=date('Ymd')."/".$data['file_name'];
                           
                            //判断水印
                            if(CS_WaterMark==1){
		                   $this->load->model('CsdjPic');
                                   $this->CsdjPic->imageWaterMark($data['full_path']);
                            }

                            if(FTP_Fun==1){  //开启远程附件

                                  $FTPUP['hostname'] = FTP_Server;
                                  $FTPUP['port']     = FTP_Port;
                                  $FTPUP['username'] = FTP_Name;
                                  $FTPUP['password'] = FTP_Pass;
                                  $FTPUP['passive']  = FTP_Ive;
                                  $this->ftp->connect($FTPUP);


                                  //----------创建目录------------------------
                                  $Dirs=FTP_Dir;
                                  if(substr($Dirs,0,1)=='/') $Dirs=substr($Dirs,1);
                                  if(substr($Dirs,-1)=='/') $Dirs=substr($Dirs,0,-1);
                                  if(!empty($Dirs)) $Dirs.='/';
                                  $All=explode("/",$config['upload_path']);
                                  $Dir1='./'.$Dirs.$All[1].'/';
                                  $this->ftp->mkdir($Dir1, DIR_WRITE_MODE); 
                                  $Dir2=$Dir1.$All[2].'/';
                                  $this->ftp->mkdir($Dir2, DIR_WRITE_MODE); 
                                  $Dir3=$Dir2.$All[3].'/';
                                  $this->ftp->mkdir($Dir3, DIR_WRITE_MODE); 

                                  //----------上传文件------------------------
                                  $tmp_name=$config['upload_path'].$data['file_name'];
                                  $this->ftp->mkdir($config['upload_path'], DIR_WRITE_MODE); 
                                  $this->ftp->upload($config['upload_path'].$data['file_name'], $Dir3.$data['file_name']);
                                  $this->ftp->close();
                                  //删除本地附件
                                  @unlink($tmp_name);

                             }


                            $targetFile=date('Ymd')."/".$data['file_name'];
                            if(empty($msgurl) || $msgurl=='undefined'){
			          exit("<script type=\"text/javascript\">
				     window.parent.document.getElementById('cs_pic').value= '{$targetFile}';
				     parent.Dialog.close();
		                  </script>");
                            }else{
			          exit("<script type=\"text/javascript\">
				     window.parent.document.getElementById('cs_pic').value= '{$targetFile}';
				     window.parent.document.getElementById('".$msgurl."').innerHTML='<font color=#0000ff>恭喜您，图片上传成功!</font>';
				     parent.Dialog.close();
		                  </script>");
                            }
                 }
        }

        public function upload_save() {  //多文件

            $ac   = $this->input->get_post('ac', TRUE);
            //允许动作
            if($ac!='dance' && $ac!='video') $ac='dance';
            if (!empty($_FILES)) {

	            $tempFile = $_FILES['Filedata']['tmp_name'];
	            $targetPath = './attachment/' . $ac . '/'.date('Ym').'/'.date('d').'/';
	            $file_name = $_FILES['Filedata']['name'];
                    $file_size = @filesize($_FILES['Filedata']['tmp_name']);

	            //获得文件扩展名
	            $file_ext = strtolower(trim(substr(strrchr($file_name, '.'), 1)));

                    if($file_ext=='mp3' || $file_ext=='wma' || $file_ext=='m4a' || $file_ext=='f4a' || $file_ext=='mp4' || $file_ext=='flv' || $file_ext=='avi' || $file_ext=='asf' || $file_ext=='wmv' || $file_ext=='swf'){  //判断后缀
                    }else{
                            @unlink($tempFile);
                    }

                    //---------获取歌曲属性------------------
                    $djinfo=$this->CsdjSkins->djinfo($tempFile);
                    if($djinfo){
                       $info=explode("|",$djinfo);
                       $dx =$info[0];
                       $yz =$info[1];
                       $sc =$info[2];
                    } else {
                       $dx ='';
                       $yz ='';
                       $sc ='';
                    }

	            //新文件名
	            $new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
	            $targetFile =  str_replace('//','/',$targetPath) . $new_file_name;
	            !is_dir(str_replace('//','/',$targetPath)) && mkdir(str_replace('//','/',$targetPath), 0755, true);
                if(!file_exists($targetPath.'index.html')){
	                  @file_put_contents($targetPath.'index.html','cscms');
				}
	            if(move_uploaded_file($tempFile,$targetFile)){

	                       echo "/attachment/" . $ac . "/".date('Ym')."/".date('d')."/".$new_file_name."=cscms=".$dx."=cscms=".$yz."=cscms=".$sc;
				}else{
                     @unlink($tempFile);
				}
            }
            
        }

        public function ftp_save() {  //远程文件

            $ac   = $this->input->get_post('ac', TRUE);
            $fhurl = $this->input->get_post('fhurl', TRUE);   //返回URL框
            $dxurl = $this->input->get_post('dxurl', TRUE);   //返回URL框
            $yzurl = $this->input->get_post('yzurl', TRUE);   //返回URL框
            $scurl = $this->input->get_post('scurl', TRUE);   //返回URL框
            $msgurl = $this->input->get_post('msgurl', TRUE);   //返回提示框
            //允许动作
            if($ac!='dance' && $ac!='video') $ac='dance';
            if (!empty($_FILES)) {

	            $tempFile = $_FILES['userfile']['tmp_name'];
	            $file_name = $_FILES['userfile']['name'];
                    $file_size = @filesize($_FILES['userfile']['tmp_name']);

	            //获得文件扩展名
	            $file_ext = strtolower(trim(substr(strrchr($file_name, '.'), 1)));
                    if($file_ext=='mp3' || $file_ext=='wma' || $file_ext=='m4a' || $file_ext=='f4a' || $file_ext=='mp4' || $file_ext=='flv' || $file_ext=='avi' || $file_ext=='asf' || $file_ext=='wmv' || $file_ext=='swf'){  //判断后缀
                    }else{
                            @unlink($tempFile);
			    echo "<script type=\"text/javascript\">";
                            echo "alert('抱歉不支持的文件类型~!');";
			    echo "parent.Dialog.close();";
		            echo "</script>";
                            exit;
                    }

                    //---------获取歌曲属性------------------
                    $djinfo=$this->CsdjSkins->djinfo($tempFile);
                    if($djinfo){
                       $info=explode("|",$djinfo);
                       $dx =$info[0];
                       $yz =$info[1];
                       $sc =$info[2];
                    } else {
                       $dx ='';
                       $yz ='';
                       $sc ='';
                    }

	            //新文件名
	            $new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
	            $targetFile = './attachment/' . $ac . '/'.$new_file_name;
	            if(move_uploaded_file($tempFile,$targetFile)){

                              $FTPUP['hostname'] = FTP_Server;
                              $FTPUP['port']     = FTP_Port;
                              $FTPUP['username'] = FTP_Name;
                              $FTPUP['password'] = FTP_Pass;
                              $FTPUP['passive']  = FTP_Ive;
                              $this->ftp->connect($FTPUP);

                              $targetPath = './attachment/' . $ac . '/'.date('Ym').'/'.date('d').'/';
                              //----------创建目录------------------------
                              $Dirs=FTP_Dir;
                              if(substr($Dirs,0,1)=='/') $Dirs=substr($Dirs,1);
                              if(substr($Dirs,-1)=='/') $Dirs=substr($Dirs,0,-1);
                              if(!empty($Dirs)) $Dirs.='/';
                              $All=explode("/",$targetPath);
                              $Dir1='./'.$Dirs.$All[1].'/';
							  $Dir1 = str_replace('//','/',$Dir1);
                              $this->ftp->mkdir($Dir1, DIR_WRITE_MODE); 
                              $Dir2=$Dir1.$All[2].'/';
                              $this->ftp->mkdir($Dir2, DIR_WRITE_MODE); 
                              $Dir3=$Dir2.$All[3].'/';
                              $this->ftp->mkdir($Dir3, DIR_WRITE_MODE); 
                              $Dir4=$Dir3.$All[4].'/';
                              $this->ftp->mkdir($Dir4, DIR_WRITE_MODE); 

                              //----------上传文件------------------------
                              $this->ftp->upload($targetFile, $Dir4.$new_file_name);
                              $this->ftp->close();
                              //删除本地附件
                              @unlink($targetFile);
			     echo "<script type=\"text/javascript\">";
                             echo "window.parent.document.getElementById('".$fhurl."').value='/attachment/" . $ac . "/".date('Ym')."/".date('d')."/".$new_file_name."';";
                             if (!empty($dxurl) && $dxurl!='undefined'){
		                  echo "window.parent.document.getElementById('".$dxurl."').value='".$dx."';";
                             }
                             if (!empty($yzurl) && $yzurl!='undefined'){
		                  echo "window.parent.document.getElementById('".$yzurl."').value='".$yz."';";
                             }
                             if (!empty($scurl) && $scurl!='undefined'){
		                  echo "window.parent.document.getElementById('".$scurl."').value='".$sc."';";
                             }
                             if (!empty($msgurl) && $msgurl!='undefined'){
		                  echo "window.parent.document.getElementById('".$msgurl."').innerHTML='<font color=#0000ff>恭喜您，文件上传成功!</font>';";
                             }
			     echo "parent.Dialog.close();";
		             echo "</script>";
	            }else{
			     echo "<script type=\"text/javascript\">";
                             if (!empty($msgurl) && $msgurl!='undefined'){
		                  echo "window.parent.document.getElementById('".$msgurl."').innerHTML='<font color=red>抱歉上传失败，本地文件夹没有写入权限~!</font>';";
                             }else{
                                  echo "alert('抱歉上传失败，本地文件夹没有写入权限~!');";
                             }
			     echo "parent.Dialog.close();";
		             echo "</script>";
                    }
            }
            
        }
}