<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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->model('CsdjAdmin');
                $this->load->library('ftp');
                $this->load->helper('string');
	}


	public function duo()
	{
	    $this->CsdjAdmin->Admin_Login();
            $this->CsdjAdmin->Admin_Qx('13');
            $ac = $this->input->get('ac', TRUE);
            $fhurl = $this->input->get('fh', TRUE);
            if(empty($fhurl)) $fhurl="CS_PlayUrl";

            if($ac=="dance"){
                $data['types']='*.flv;*.f4a;*.mp4;*.mp3;*.wma';
                $data['limit']='1';
                $data['size']='200 MB';
                $data['title']='音乐文件';
            }else{
                $data['types']='*.flv;*.swf;*.mp4;*.wmv;*.asf;*.avi';
                $data['limit']='50';
                $data['size']='2000 MB';
                $data['title']='视频文件';
            }

            $data['ac']=$ac;
            $data['fhurl']=$fhurl;
			$key = sys_auth($this->session->userdata('admin_id').'-cscms-'.$this->session->userdata('admin_name').'-cscms-'.$this->session->userdata('admin_pass'),'E');
            if(FTP_Fun==1){  //开启远程附件
                   $data['uplink']=site_url('admin/upload/ftp_save').'?ac='.$ac.'&fhurl='.$fhurl;
                   $this->load->view('upload_ftp',$data);
            }else{
                   $data['uplink']=site_url('admin/upload/upduo/').'?key='.$key.'&ac='.$ac;
                   $this->load->view('admin/upload_duo',$data);
            }
        }

        public function index() {  //图片
	        $this->CsdjAdmin->Admin_Login();
            $this->CsdjAdmin->Admin_Qx('13');
            $ac   = $this->security->xss_clean($this->input->get('ac', TRUE));
            $op   = $this->security->xss_clean($this->input->get('op', TRUE));

            if($ac=='up'){

                 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=='singer'){  //歌手图片
                        $config['upload_path'] = './attachment/singer/'.date('Ymd').'/';
                       $config['allowed_types'] = 'gif|jpg|png';
                 }elseif($op=='music'){  //音乐图片
                        $config['upload_path'] = './attachment/tp/'.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);

                             }

                             if($op=='skinspic') $op='pic';
			     exit("<script type=\"text/javascript\">
				     window.parent.document.getElementById('CS_Pic').value= '{$targetFile}';
				     parent.Dialog.close();
				     parent.Dialog.close();
		                  </script>");
                 }
            
            }else{
	          $data['ac']=$ac;
                  $this->load->view('admin/upload_pic', $data);
            }
        }


        public function upduo() {  //多文件

            $key   = $this->input->get_post('key');
            $ac   = $this->input->get_post('ac');
			
	        $this->CsdjAdmin->Admin_Login($key);
            //允许动作
            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);
                    }

	            //新文件名
	            $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');
                    }
	            @move_uploaded_file($tempFile,$targetFile);
	            echo "/attachment/" . $ac . "/".date('Ym')."/".date('d')."/".$new_file_name."=cscms=".$file_size;
            }
        }


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

	        $this->CsdjAdmin->Admin_Login();
            $this->CsdjAdmin->Admin_Qx('13');

            $ac   = $this->input->get_post('ac', TRUE);
            $fhurl = $this->security->xss_clean($this->input->get_post('fhurl', TRUE));   //返回URL框

            //允许动作
            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;
                    }

	            //新文件名
	            $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].'/';
                              $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."';";
                              echo "alert('恭喜您，上传成功~!');";
			      echo "parent.Dialog.close();";
		              echo "</script>";
	            }else{
			      echo "<script type=\"text/javascript\">";
                              echo "alert('抱歉上传失败，本地文件夹没有写入权限~!');";
			      echo "parent.Dialog.close();";
		              echo "</script>";
                    }
            }
            
        }

}

