<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once CSCMSPATH.'lib/Cs_Ftp.php';
class Saomiao extends CI_Controller {

	function __construct(){
		parent::__construct();
        	$this->load->helper('url');
                $this->load->library('session'); //加载session
		$this->load->model('CsdjDB');
		$this->load->model('CsdjAdmin');
		$this->load->model('CsdjSkins');
                $this->db->cache_off();
	        $this->CsdjAdmin->Admin_Login();
                $this->load->helper('file');
                $this->load->library('ftp');
	}

	public function yp()
	{
            $data['sid']=1;
            $data['hz']="mp3|wma|mp4|flv|avi";
            $this->load->view('admin/saomiao_yb',$data);
        }

	public function ftp()
	{
            $data['sid']=1;
            $data['hz']="mp3|wma|mp4|flv|avi";
            $this->load->view('admin/saomiao_ftp',$data);
        }

	public function ftpedit()
	{
            $SM_Server=$this->input->post('SM_Server');
            $SM_Port=$this->input->post('SM_Port');
            $SM_Name=$this->input->post('SM_Name');
            $SM_Pass=$this->input->post('SM_Pass');
            $SM_Ive=$this->input->post('SM_Ive');

                if(substr(SM_Pass,0,1)."********".substr(SM_Pass,-1)==$SM_Pass){
                     $SM_Pass=SM_Pass;
                }

                if(empty($SM_Port)) $SM_Port="21";   
                if(empty($SM_Ive)) $SM_Ive="TRUE";  

	        $strs="<?php"."\r\n";
	        $strs.="define(\"SM_Server\",\"".$SM_Server."\");      //远程FTP服务器IP  \r\n";
	        $strs.="define(\"SM_Port\",\"".$SM_Port."\");      //远程FTP端口  \r\n";
	        $strs.="define(\"SM_Name\",\"".$SM_Name."\");      //远程FTP帐号  \r\n";
	        $strs.="define(\"SM_Pass\",\"".$SM_Pass."\");      //远程FTP密码  \r\n";
	        $strs.="define(\"SM_Ive\",\"".$SM_Ive."\");      //是否使用被动模式  \r\n";

                //写文件
                if (!write_file('./csdj/lib/Cs_Ftp.php', $strs)){
                          $this->CsdjAdmin->Admin_Url('<font color=red>操作失败，文件没有写入权限！</font>',site_url('admin/saomiao/ftp/'));
                }else{
                          $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，修改成功！</font>',site_url('admin/saomiao/ftp/'),'ok');
                }
        }

	public function ftps()
	{
 	    $path = $this->input->get_post('path');
 	    $hz = $this->input->get_post('hz');
 	    $sid = $this->input->get_post('sid');
 	    $page = $this->input->get('page');
            if(empty($page)) $page=1;
            if(empty($sid)) $sid=1;

            if(empty($path)) $path="/";
            if(substr($path,0,1)!="/") $path="/".$path;
            if(substr($path,-1)!="/") $path=$path."/";

            $path=str_replace("\\","/",$path);
            $path=str_replace("//","/",$path);
            $paths=".".$path;

            $FTPUP['hostname'] = SM_Server;
            $FTPUP['port']     = SM_Port;
            $FTPUP['username'] = SM_Name;
            $FTPUP['password'] = SM_Pass;
            $FTPUP['debug']    = TRUE;
            $FTPUP['passive']  = SM_Ive;
            $this->ftp->connect($FTPUP);

                echo '<LINK href="'.base_url().'csdj/admin/images/admin.css" type="text/css" rel="stylesheet"><br><table width="90%" align="center" border="0" style="border-collapse:collapse;">';

		$data['CS_Color']=$this->input->post('CS_Color');
		$data['CS_CID']=$this->input->post('CS_CID');
		$data['CS_TID']=$this->input->post('CS_TID');
		$data['CS_Year']=$this->input->post('CS_Year');
		$data['CS_Singer']=$this->input->post('CS_Singer');
		$data['CS_User']=$this->input->post('CS_User');
		$data['CS_PID']=$this->input->post('CS_PID');
		$data['CS_FID']=$this->input->post('CS_FID');
		$data['CS_Reco']=$this->input->post('CS_Reco');
		$data['CS_Cion']=$this->input->post('CS_Cion');
                $data['CS_Scion']=$this->input->post('CS_Scion');
		$data['CS_Vip']=$this->input->post('CS_Vip');
		$data['CS_Template']=$this->input->post('CS_Template');
		$data['CS_Title']=$this->input->post('CS_Title');
		$data['CS_Keywords']=$this->input->post('CS_Keywords');
		$data['CS_Description']=$this->input->post('CS_Description');

                if(empty($data['CS_User'])){
                        exit('<font color=red>会员、不能为空！</font>');
                }

                $sql="SELECT * FROM ".CS_SqlPrefix."user where cs_name='".$data['CS_User']."'";
	        $row=$this->CsdjDB->get_all($sql); 
	        if(!$row){
                     exit('<font color=red>抱歉，该会员不存在！</font>');
                }

                if(!empty($data['CS_Singer'])){
                     $sql="SELECT * FROM ".CS_SqlPrefix."singer where cs_name='".$data['CS_Singer']."'";
	             $row=$this->CsdjDB->get_all($sql); 
	             if(!$row){
                           $singer['CS_Name']=$data['CS_Singer'];
                           $this->CsdjDB->get_insert('singer',$singer);
                           $sqls="SELECT * FROM ".CS_SqlPrefix."singer where cs_name='".$data['CS_Singer']."'";
	                   $rows=$this->CsdjDB->get_all($sqls); 
                           $data['CS_SingerID']=$rows[0]->CS_ID;
                     }else{
                           $data['CS_SingerID']=$row[0]->CS_ID;
                     }
                }

                if(empty($data['CS_Cion'])) $data['CS_Cion']=0;
                if(empty($data['CS_Scion'])) $data['CS_Scion']=0;
                if(empty($data['CS_Template'])) $data['CS_Template']='dance-play.html';
                $data['CS_AddTime']=date('Y-m-d H:i:s');

                $arrs = $this->ftp->list_files($paths);
                $this->ftp->close(); //关闭FTP

                //----------给后缀加上点--------------------
                $hzs=explode("/",$hz);
                $xhz="";
                for($h=0;$h<count($hzs);$h++){
                   $xhz.=".".$hzs[$h];
                }
                //------------------------------------------

                if(!empty($arrs)){
                                  $i=0;
                                  foreach ($arrs as $value) {  
                                      if(is_int(strpos($xhz,substr($value,-4))) !== false && $value!='.'){  //只入库要扫描的文件

                                            $value=addslashes($value);
                                            $name=explode("/",$value);
                                            $j=count($name)-1;
                                            $values=$name[$j];

		                            $data['CS_Name']=substr($values,0,-4);
		                            $data['CS_PlayUrl'] =$path.$value;
		                            $data['CS_DownUrl1']=$path.$value;

                                            $sql="SELECT * FROM ".CS_SqlPrefix."dance where CS_PlayUrl='".$data['CS_PlayUrl']."'";
	                                    $row=$this->CsdjDB->get_all($sql); 
	                                    if(!$row){
                                                $this->CsdjDB->get_insert('dance',$data);
                                                echo "<tr><td align='left'>&nbsp;<font style=font-size:10pt;>&nbsp;&nbsp;<font color=#0000ff>".$data['CS_Name']."</font>入库完成...</font></td></tr>";
                                            }else{
                                                echo "<tr><td align='left'>&nbsp;<font style=font-size:10pt;>&nbsp;&nbsp;<font color=red>".$data['CS_Name']."，已经存在，</font>入库失败...</font></td></tr>";
                                            }
                                       $i=$i+1;
                                      }
                                  }
                      die("<tr><td>&nbsp;<font style=color:red;font-size:10pt;><b>所有数据入库完毕,共入库<font color=red>".$i."</font>条记录．．．．</b></font></td></tr></table>");
                }else{
                      exit('<font color=red>抱歉，该目录下没有要入库的文件！</font>');
                }
        }


	public function yps()
	{
 	    $dir = $this->input->get_post('dir');
 	    $path = $this->input->get_post('path');
 	    $hz = $this->input->get_post('hz');
 	    $sid = $this->input->get_post('sid');
 	    $page = $this->input->get('page');
            if(empty($page)) $page=1;
            if(empty($sid)) $sid=1;

            if(empty($path)) $path="/";

            if(empty($dir)){
                    $paths=".".$path;
            }else{
                    $paths=$dir.$path;
            }
            $hz=str_replace("|php","",$hz);
            $hz=str_replace("php|","",$hz);
            $paths=str_replace("\\","/",$paths);
            $paths=str_replace("//","/",$paths);
            if(empty($dir)){
                 $arrs=get_dir_file_info($paths, $top_level_only = TRUE);
            }else{
                 $arrs=get_dir_file_info($paths, $top_level_only = TRUE);
            }

            $data['pagesize']=25;
            $data['pagejs']=ceil((count($arrs)-1) / $data['pagesize']); //总页数
            $data['path']=$path;
            $data['dir']=$dir;
            $data['dirs']=$dir;
            $data['hz']=$hz;
            $data['sid']=$sid;
            $data['page']=$page;
            $data['url']=site_url('admin/saomiao/yps/')."?dir=".$dir."&hz=".$hz."&sid=".$sid."&path=".$path;
            $data['show']=array_slice($arrs,$data['pagesize']*(($page>$data['pagejs']?$data['pagejs']:$page)-1),$data['pagesize']);

            $data['uppage']="javascript:history.back();";
            $this->load->view('admin/saomiao_ybs',$data);
        }

	public function ruku()
	{
            $data['dir']=$this->input->get_post('dir');
            $data['dirs']=$this->input->get_post('dirs');
            $data['files']=$this->input->get_post('files');
            $data['sid']=$this->input->get_post('sid');
            $data['hz']=$this->input->get_post('hz');
            $mulu='';$wenjian='';
            if(is_array($data['dirs'])){
                foreach ($data['dirs'] as $value) {  //文件夹
                    $mulu.=$value."@cscms@";
                }
                $mulu.="]]]";
            }else{
                $mulu=$data['dirs'];
            }
            if(is_array($data['files'])){
                 foreach ($data['files'] as $value) {  //文件
                    $wenjian.=$value."@cscms@";
                 }
                 $wenjian.="]]]";
            }else{
                 $wenjian=$data['files'];
            }
            $mulu=str_replace("@cscms@]]]","",$mulu);
            $wenjian=str_replace("@cscms@]]]","",$wenjian);
            $mulu=str_replace("@cscms@","\r\n",$mulu);
            $wenjian=str_replace("@cscms@","\r\n",$wenjian);
            if(empty($mulu)){
                  $data['saomiao']=$wenjian;
            }elseif(empty($wenjian)){
                  $data['saomiao']=$mulu;
            }else{
                  $data['saomiao']=$mulu."\r\n".$wenjian;
            }
            $this->load->view('admin/saomiao_ruku',$data);
        }

	public function rksave()
	{

            $dir=$this->input->post('dir');
            $sid=$this->input->post('sid');
            $sid=$this->input->post('sid');
            $hz=$this->input->post('hz');
            $dance=$this->input->post('saomiao');
            if(empty($dance)) $this->CsdjAdmin->Admin_Url('<font color=red>歌曲数据为空！</font>','javascript:history.back();');
            $dir=str_replace("\\","/",$dir);
            $dance=str_replace($dir,"",$dance);

            if($sid==1){  //歌曲
		$data['CS_Color']=$this->input->post('CS_Color');
		$data['CS_CID']=$this->input->post('CS_CID');
		$data['CS_TID']=$this->input->post('CS_TID');
		$data['CS_Year']=$this->input->post('CS_Year');
		$data['CS_Singer']=$this->input->post('CS_Singer');
		$data['CS_User']=$this->input->post('CS_User');
		$data['CS_PID']=$this->input->post('CS_PID');
		$data['CS_FID']=$this->input->post('CS_FID');
		$data['CS_Reco']=$this->input->post('CS_Reco');
		$data['CS_Cion']=$this->input->post('CS_Cion');
                $data['CS_Scion']=$this->input->post('CS_Scion');
		$data['CS_Vip']=$this->input->post('CS_Vip');
		$data['CS_Template']=$this->input->post('CS_Template');
		$data['CS_Title']=$this->input->post('CS_Title');
		$data['CS_Keywords']=$this->input->post('CS_Keywords');
		$data['CS_Description']=$this->input->post('CS_Description');
		$playhz=$this->input->post('playhz');

                if(empty($data['CS_User'])){
                        $this->CsdjAdmin->Admin_Url('<font color=red>会员、不能为空！</font>','javascript:history.back();');
                }

                $sql="SELECT * FROM ".CS_SqlPrefix."user where cs_name='".$data['CS_User']."'";
	        $row=$this->CsdjDB->get_all($sql); 
	        if(!$row){
                     $this->CsdjAdmin->Admin_Url('<font color=red>抱歉，改会员不存在！</font>','javascript:history.back();');
                }

                if(!empty($data['CS_Singer'])){
                     $sql="SELECT * FROM ".CS_SqlPrefix."singer where cs_name='".$data['CS_Singer']."'";
	             $row=$this->CsdjDB->get_all($sql); 
	             if(!$row){
                           $singer['CS_Name']=$data['CS_Singer'];
                           $this->CsdjDB->get_insert('singer',$singer);
                           $sqls="SELECT * FROM ".CS_SqlPrefix."singer where cs_name='".$data['CS_Singer']."'";
	                   $rows=$this->CsdjDB->get_all($sqls); 
                           $data['CS_SingerID']=$rows[0]->CS_ID;
                     }else{
                           $data['CS_SingerID']=$row[0]->CS_ID;
                     }
                }

                if(empty($data['CS_Cion'])) $data['CS_Cion']=0;
                if(empty($data['CS_Scion'])) $data['CS_Scion']=0;
                if(empty($data['CS_Template'])) $data['CS_Template']='dance-play.html';
                $data['CS_AddTime']=date('Y-m-d H:i:s');

 		$Arr=explode("\r\n",$dance);
                      echo '<LINK href="'.base_url().'csdj/admin/images/admin.css" type="text/css" rel="stylesheet"><br><table width="90%" align="center" border="0" style="border-collapse:collapse;">';
                      for($i=0;$i<count($Arr);$i++){

                            if(is_dir($dir.$Arr[$i])){  //文件夹
                                  $strs=$this->CsdjAdmin->dirtofiles($dir.$Arr[$i],$hz);
                                  if(!empty($strs)){
                                  foreach ($strs as $value) {  
                                     if(!empty($value)){
                                         if(substr($value,0,2)=="./"){
                                               $value=substr($value,1);
                                               $pathss=$_SERVER['DOCUMENT_ROOT'].$value;
                                         }else{
                                               $pathss=$value;
                                         }

                                         $value=addslashes($value);
                                         $dance=str_replace($dir,"",$value);
                                         $name=explode("/",$value);
                                         $k=count($name)-1;
                                         $values=$name[$k];
		                         $data['CS_Name']=substr($values,0,-4);
                                         if(substr($data['CS_Name'],0,1)=="/") $data['CS_Name']=substr($data['CS_Name'],1);

		                         $data['CS_PlayUrl'] =$dance;
		                         $data['CS_DownUrl1']=$dance;

                                         //判断后缀
                                         if(!empty($playhz)){
                                              $data['CS_PlayUrl']=str_replace(substr($data['CS_PlayUrl'],-3),$playhz,$data['CS_PlayUrl']);
                                         }

                                         //---------获取歌曲属性------------------
                                         $djinfo=$this->CsdjSkins->djinfo($pathss);
                                         if($djinfo){
                                            $info=explode("|",$djinfo);
                                            $data['CS_Dx'] =$info[0];
                                            $data['CS_Yz'] =$info[1];
                                            $data['CS_Sc'] =$info[2];
                                         }

                                            $sql="SELECT * FROM ".CS_SqlPrefix."dance where CS_PlayUrl='".$value."'";
	                                    $row=$this->CsdjDB->get_all($sql); 
	                                    if(!$row){
                                                   $this->CsdjDB->get_insert('dance',$data);
                                                   echo "<tr><td align='left'>&nbsp;<font style=font-size:10pt;>&nbsp;&nbsp;<font color=#0000ff>".$value."</font>入库完成...</font></td></tr>";
                                            }else{
                                                   echo "<tr><td align='left'>&nbsp;<font style=font-size:10pt;>&nbsp;&nbsp;<font color=red>".$value."，已经存在，</font>入库失败...</font></td></tr>";
                                            }
                                     }
                                  }
                                  }
                            }else{
                                 if(substr($Arr[$i],0,2)=="./"){
                                               $Arr[$i]=substr($Arr[$i],1);
                                               $pathss=$_SERVER['DOCUMENT_ROOT'].$Arr[$i];
                                 }else{
                                               $pathss=$dir.$Arr[$i];
                                 }
                                 $Arr[$i]=addslashes($Arr[$i]);

                                 $sql="SELECT * FROM ".CS_SqlPrefix."dance where CS_PlayUrl='".$Arr[$i]."'";
	                         $row=$this->CsdjDB->get_all($sql); 
	                         if(!$row){
                                     $name=explode("/",$Arr[$i]);
                                     $j=count($name)-1;
                                     $values=$name[$j];

                                     //---------获取歌曲属性------------------
                                     $djinfo=$this->CsdjSkins->djinfo($pathss);
                                     if($djinfo){
                                        $info=explode("|",$djinfo);
                                        $data['CS_Dx'] =$info[0];
                                        $data['CS_Sc'] =$info[2];
                                        $data['CS_Yz'] =$info[1];
                                     }

		                     $data['CS_Name']=substr($values,0,-4);
                                     if(substr($data['CS_Name'],0,1)=="/") $data['CS_Name']=substr($data['CS_Name'],1);
		                     $data['CS_PlayUrl'] =$Arr[$i];
		                     $data['CS_DownUrl1']=$Arr[$i];

                                     //判断后缀
                                     if(!empty($playhz)){
                                         $data['CS_PlayUrl']=str_replace(substr($data['CS_PlayUrl'],-3),$playhz,$data['CS_PlayUrl']);
                                     }

                                     $this->CsdjDB->get_insert('dance',$data);
                                     echo "<tr><td align='left'>&nbsp;<font style=font-size:10pt;>&nbsp;&nbsp;<font color=#0000ff>".$Arr[$i]."</font>入库完成...</font></td></tr>";

                                 }else{
                                     echo "<tr><td align='left'>&nbsp;<font style=font-size:10pt;>&nbsp;&nbsp;<font color=red>".$Arr[$i]."，已经存在，</font>入库失败...</font></td></tr>";
                                 }
                            }
                      }
                      die("<tr><td>&nbsp;<font style=color:red;font-size:10pt;><b>所有数据入库完毕．．．．</b></font></td></tr></table><script>setTimeout('ReadGo();',3000);function ReadGo(){window.location.href ='javascript:history.go(-2);'}</script>");
            }else{
                //....
            }
        }
}
