<?php
$randnum=date('YmdHis',time()).rand(2,pow(2,24));
if(empty($_COOKIE['CD_AdminID']) || empty($_COOKIE['CD_Login']) || $_COOKIE['CD_Login']!==md5($_COOKIE['CD_AdminID'].$_COOKIE['CD_AdminUserName'].$_COOKIE['CD_AdminPassWord'].$_COOKIE['CD_Permission'])){exit(iframe_message("未登录或登录已过期，请重新登录管理中心！"));}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>远程上传</title>
<link href="source/plugin/ftp/api.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function ReturnValue(reimg){
	<?php if($_GET['ac']=="song"){ ?>
	this.parent.document.form.CD_Url.value=reimg;
	this.parent.document.form.CD_DownUrl.value=reimg;
	this.parent.document.form.CD_Server.value=0;
	this.parent.asyncbox.tips("恭喜，音频上传成功！", "success", 3000);
	<?php }elseif($_GET['ac']=="video"){ ?>
	this.parent.document.form2.CD_Play.value=reimg;
	this.parent.asyncbox.tips("恭喜，视频上传成功！", "success", 3000);
	<?php } ?>
	this.parent.layer.closeAll();
}
function ReturnError(msg){
	this.parent.asyncbox.tips(msg, "error", 3000);
	this.parent.layer.closeAll();
}
function checkftp(){
        var up=document.getElementById("upload").value;
        var ext=up.substr(up.lastIndexOf('.')+1,up.length);
	if(document.form.upload.value==""){
	        document.getElementById("loading").innerHTML="<font color='#9BC9E3'>文件不能为空，请选择！</font>";
	        document.form.upload.focus();
	        return false;
	}
	<?php if($_GET['ac']=="song"){ ?>
	if(ext=="mp3" || ext=="m4a"){
	<?php }elseif($_GET['ac']=="video"){ ?>
	if(ext=="flv" || ext=="f4v" || ext=="mp4"){
	<?php } ?>
	        document.getElementById("loading").innerHTML="<font color='#CEE2F2'>正在上传，请稍等...</font>";
	        return true;
	}
	else {
	        <?php if($_GET['ac']=="song"){ ?>
	        document.getElementById("loading").innerHTML="<font color='#9BC9E3'>只允许选择音频类文件，请更换！</font>";
	        <?php }elseif($_GET['ac']=="video"){ ?>
	        document.getElementById("loading").innerHTML="<font color='#9BC9E3'>只允许选择视频类文件，请更换！</font>";
	        <?php } ?>
	        document.form.upload.focus();
	        return false;
	}
}
</script>
</head>
<body>
<?php
class ftp {
        public $off;
        public $conn_id;
        function __construct($FTP_HOST,$FTP_PORT,$FTP_USER,$FTP_PASS) {
                $this->conn_id = @ftp_connect($FTP_HOST,$FTP_PORT) or die("<script type=\"text/javascript\">ReturnError('FTP服务器连接失败！');</script>");
                @ftp_login($this->conn_id,$FTP_USER,$FTP_PASS) or die("<script type=\"text/javascript\">ReturnError('FTP服务器登录失败！');</script>");
                @ftp_pasv($this->conn_id,1);
        }
        function up_file($path,$root,$newpath,$type=cd_ftptype) {
                if($type==1) $this->dir_mkdirs($path,$root.$newpath);
                $this->off = @ftp_put($this->conn_id,$root.$newpath,$path,FTP_BINARY);
                if(!$this->off) {
                        @unlink($path);
                        echo "<script type=\"text/javascript\">ReturnError('文件上传失败，请检查权限及路径是否正确！');</script>";
                } else {
                        @unlink($path);
                        echo "<script type=\"text/javascript\">ReturnValue('".cd_ftpdomain.$newpath."');</script>";
                }
        }
        function dir_mkdirs($unlink,$path) {
                $path_arr = explode('/',$path);
                $file_name = array_pop($path_arr);
                $path_div = count($path_arr);
                foreach($path_arr as $val) {
                        if(@ftp_chdir($this->conn_id,$val) == FALSE) {
                                $tmp = @ftp_mkdir($this->conn_id,$val);
                                if($tmp == FALSE) {
                                        @unlink($unlink);
                                        exit("<script type=\"text/javascript\">ReturnError('目录创建失败，请检查权限及路径是否正确！');</script>");
                                }
                                @ftp_chdir($this->conn_id,$val);
                        }
                }
                for($i=1;$i<=$path_div;$i++) {
                        @ftp_cdup($this->conn_id);
                }
        }
        function close() {
                @ftp_close($this->conn_id);
        }
}
$ftp = new ftp(cd_ftphost,cd_ftpport,cd_ftpuser,cd_ftppass);
if(isset($_POST['submit'])) {
        if(!empty($_FILES['upload']['name'])) {
                move_uploaded_file($_FILES['upload']['tmp_name'],"data/attachment/".basename($_FILES['upload']['tmp_name']));
                $ftp->up_file("data/attachment/".basename($_FILES['upload']['tmp_name']),cd_ftproot,cd_ftpdir.$randnum.".".fileext($_FILES['upload']['name']));
                $ftp->close();
        }
}
?>
<div class="uploadbox" style="width:595px; margin:20px auto;padding: 3px !important;border: 0px;!important;height:30px !important;position:relative;overflow:hide;background-image: url(source/plugin/ftp/bg.jpg);">
        <form method="post" name="form" enctype="multipart/form-data">
	        <div class="blue" style="float:left;width:514px;height:26px;line-height:26px;">
		        <div class="attachment-list">&nbsp;<input type="file" id="upload" name="upload" />&nbsp;<span id="loading">允许的格式：<?php if($_GET['ac']=="song"){ ?>mp3*m4a<?php }elseif($_GET['ac']=="video"){ ?>flv*f4v*mp4<?php } ?></span></div>
	        </div>
	        <div style="float:right;">
		        <input type="submit" name="submit" id="btupload" value="开始上传" onclick="return checkftp();" />
	        </div>
        </form>
</div>
</body>
</html>