<%
include("language.html")

if _SESSION["username"] ~= nil and _SESSION["currentpath"] ~= nil then
%>
<noscript><center><H2><%=LANG["error_nojavascript"]%></H2></center></noscript>
<html>
<head>
<title>Wing FTP Server - Uploader</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache, must-revalidate" />
<meta name="viewport" content="width=320, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<link rel="apple-touch-icon" href="#">
<link rel="Shortcut Icon" href="images/logo.ico" type="image/x-icon">
<script language="javascript">

var isEdge = navigator.userAgent.toLowerCase().indexOf("edge") > -1 && !isIE;
var isIE11 = navigator.userAgent.toLowerCase().indexOf("trident") > -1;

function orientationChange()
{
	switch(window.orientation) 
	{
		case 0:
		case 180:
			isPortrait = true;
		break;
		case -90:
		case 90:
			isPortrait = false;
		break;
	}
}
window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", orientationChange, false);
orientationChange();

</script>
</head>

<link rel="stylesheet" type="text/css" href="webuploader/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="webuploader/webuploader.css">
<style type="text/css">
<!--
body {
    margin-top: 50px;
	background-color: #fff;
    font-size: 16px;
    font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

.uploaderDiv {
    position: relative;
    padding: 10px;
    background-color: #fff;
}

#picker {
    display: inline-block;
    vertical-align: middle;
    padding: 5px;
	max-width: 220px;
	_width: 220px;
}

#btnUploadFolder {
    display: inline-block;
    vertical-align: middle;
    padding: 5px;
	max-width: 220px;
	_width: 220px;
}
-->
</style>
<script language="javascript" src="include/common.js"></script>
<script language="javascript" src="webuploader/jquery.js"></script>
<script language="javascript" src="webuploader/bootstrap.min.js"></script>
<script language="javascript" src="webuploader/webuploader.min.js?v=2"></script>

<body style="margin:0px;padding:0px;">

<div id="swfTester" style="display:none;">
<embed id="swfObj" src='webuploader/Uploader.swf' width='100%' play='true' loop='false' quality='high' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.adobe.com/go/getflashplayer'></embed>
</div>

<div id="uploader" class="uploaderDiv">
	<div class="queueList">
		<div id="filelist" class="uploader-list"></div>

		<div id="dndArea" class="placeholder">
			<div id="picker">&nbsp;<%=LANG["str_browse"]%>&nbsp;</div>
			<div id="btnUploadFolder" style="display:none;">&nbsp;<%=LANG["str_browse_folder"]%>&nbsp;</div>
			<button id="cancelButton" class="btn btn-default" style="height: 30px;display:none;" title="<%=LANG["str_closewindow"]%>"> <%=LANG["create_cancel"]%> </button>
            <p id="dragdropLabel"><%=LANG["str_dragdrop_files"]%></p>
			<p id="infoDiv"></p>
		</div>
	</div>
</div>

</body>
</html>
<script>
function CheckUploadError(filename, filesize)
{
	var xmlhttpObj = isIE ? new ActiveXObject('Microsoft.XMLHTTP'):new XMLHttpRequest();
	xmlhttpObj.open("GET","upload_err.html?filename="+urlEncode(filename)+"&filesize="+urlEncode(filesize)+"&r="+Math.random(), false);
	xmlhttpObj.send("");

	return trim(xmlhttpObj.responseText);
}

if ( !WebUploader.Uploader.support() ) 
{
	alert('<%=LANG["str_webuploader_tips"]%>');
}

// upload file
jQuery(function() {
    var $ = jQuery,
        $list = $('#filelist'),
        state = 'pending',
        uploader,
		uploader2;

	if($('#swfTester').height() >= 50)
	{
		$('#swfObj').height("100%");
	}

	uploader = WebUploader.create({
		auto: true,
		compress: false,
		threads: 1,
		swf: '/webuploader/Uploader.swf',
		server: '/uploaded.html',
		dnd: top.document.body,
		paste: document.body,
		disableGlobalDnd: true,
		duplicate: true
	});

	uploader2 = WebUploader.create({
		auto: true,
		compress: false,
		threads: 1,
		swf: '/webuploader/Uploader.swf',
		server: '/uploaded.html',
		//dnd: '#uploader .queueList',
		dnd: $("*"),
		paste: document.body,
		disableGlobalDnd: true,
		duplicate: true,
		pick: '#picker'
	});


	$('#picker div:eq(1)').attr('style','position: absolute; top: 0px; left: 0px; width: 82px; height: 39px; overflow: hidden; bottom: auto; right: auto;');


	isIphone = navigator.userAgent.toLowerCase().indexOf('iphone')==-1? 0:1;
	isAndroid = navigator.userAgent.toLowerCase().indexOf('android')==-1? 0:1;
	var isSafari = navigator.userAgent.toLowerCase().indexOf('safari')==-1? 0:1;
	var isIpad = isSafari && !isIphone && 'ontouchend' in document;
	var uploadedFiles = 0;

	if(!isIphone && !isIpad && !isAndroid)
	{
		$('#btnUploadFolder').css("display","");

		uploader2.addButton({
			id: '#btnUploadFolder'
		});

		uploader2.on('uploadBeforeSend', function(object, data, headers) {
			data.rpath = object.file.source.source.webkitRelativePath ? object.file.source.source.webkitRelativePath : '';
		});
	}

    // file Queued
    uploader.on( 'fileQueued', function( file ) {
        $list.append( '<div id="' + file.id + '" style="width:95%;padding-left:10px;">' +
            '<h4 class="info"><img src="images/cancel.gif" class="remove-this" title="<%=LANG["create_cancel"]%>" style="cursor:default;margin:3px;"> ' + file.name + ' (' + fileSize(file.size) + ')</h4>' +
        '</div>' );

		var $li = $( '#'+file.id );
		$li.on('click', '.remove-this', function() {
			uploader.cancelFile( file );
			$li.remove();
		})
    });

    uploader.on( 'uploadStart', function( file ) {
		//WIN10 edge browser can't trigger uploadProgress
		if(isEdge && uploader.predictRuntimeType() != "flash")
		{
			var $li = $( '#'+file.id );
			var $percent = $li.find('.progress .progress-bar');

			if ( !$percent.length ) {
				$li.append('<div class="progress progress-striped active"><div class="progress-bar" role="progressbar" style="width:100%; text-align:left; padding-left:10px; color:#333; word-break:keep-all;"><%=LANG["upload_ready"]%>...</div></div>');
			}
		}
    });

    // upload Progress
    uploader.on( 'uploadProgress', function( file, percentage ) {
        var $li = $( '#'+file.id );
		var $percent = $li.find('.progress .progress-bar');

        if ( !$percent.length ) {
			$li.append('<div class="progress progress-striped active"><div class="progress-bar" role="progressbar" style="text-align:left; padding-left:10px; color:#333; word-break:keep-all;"></div></div>');
			$percent = $li.find('.progress .progress-bar');
        }

		var strText = "<%=LANG['uploadstat_nowsize']%>"+round(percentage * 100, 0)+"%"
        $percent.text(strText);
        $percent.css( 'width', round(percentage * 100, 0) + '%' );
    });

    uploader.on( 'uploadSuccess', function( file ) {
		uploadedFiles++;
        $( '#'+file.id ).find('.progress .progress-bar').text('<%=LANG["upload_ok"]%>');
		$( '#'+file.id ).fadeOut(1000);
		//if(isAndroid)
		setTimeout(function(){$( '#'+file.id ).remove();}, 1000);
    });

    uploader.on( 'uploadError', function( file, reason ) {
		var result = CheckUploadError(file.name, file.size);
		if(result == "1")
		{
			$( '#'+file.id ).find('.progress .progress-bar').text('<%=LANG["upload_ok"]%>');
			$( '#'+file.id ).fadeOut(1000);
			setTimeout(function(){$( '#'+file.id ).remove();}, 1000);
		}
		else
		{
			var strError = "<%=LANG['upload_failed']%>";
			if(result == "-1")
			{
				strError = "<%=LANG['error_upload']%>";
			}
			else if(result == "-2")
			{
				strError = "<%=LANG['upload_failed']..' '..RESULT_STR[-3]%>";
			}
			else if(result == "-3")
			{
				strError = "<%=LANG['error_upload3']%>";
			}
			else if(result == "-4")
			{
				strError = "<%=LANG['error_upload4']%>";
			}
			else if(result == "-5")
			{
				strError = "<%=LANG['session_expire']%>";
			}
			else if(result == "-6")
			{
				strError = "<%=LANG['upload_failed']..' '..RESULT_STR[-6]%>";
			}
			else if(result == "-11")
			{
				strError = "<%=LANG['upload_failed']..' '..RESULT_STR[-11]%>";
			}
			else if(result == "0")
			{
				strError = "<%=LANG['upload_failed']..' '..RESULT_STR[-1]%>";
			}
			$( '#'+file.id ).find('.progress .progress-bar').addClass("progress-bar-danger");
			$( '#'+file.id ).find('.progress .progress-bar').css("color","#FFF");
			$( '#'+file.id ).find('.progress .progress-bar').text(strError);
			$( '#'+file.id ).fadeTo(3000, 1);
			$( '#'+file.id ).fadeOut(2000);
			setTimeout(function(){$( '#'+file.id ).remove();}, 5000);
		}
    });

    uploader.on( 'startUpload', function() {
		//$('#cancelButton').attr('disabled', false);
		$('#cancelButton').attr('title', '<%=LANG["str_cancelupload"]%>');
		$('#infoDiv').html('');
		uploadedFiles = 0;
    });

    uploader.on( 'uploadFinished', function() {
		try
		{
			//$('#cancelButton').attr('disabled', true);

			if(uploadedFiles > 0)
				$('#infoDiv').html("<span style='color:#1F7FF8'><%=LANG["str_complete"]%> <%=LANG["str_totalfile"]%>: "+uploadedFiles+"</span>");

			if(uploader.isInProgress() == false && uploader2.isInProgress() == false)
			{
				var nTimeout = 0;
				$("div").find('.progress .progress-bar').each(function(){
					if(this.innerHTML != '<%=LANG["upload_ok"]%>')
					{
						nTimeout = 5000;
						return false;
					}
				});
				setTimeout(function(){$list.html(""); $('#cancelButton').attr('title', '<%=LANG["str_closewindow"]%>'); top.ajaxRequest("dir", "");}, nTimeout);
			}
		}
		catch(e){}
    });

	uploader.on( 'fileQueued', function( file ) {
		try 
		{
			var result = top.checkOverwrite(file.name, uploader.getFiles("inited", "queued").length);
			if(result == "0")
			{
				$( '#'+file.id ).remove();
				uploader.removeFile(file, true);
			}
		}
		catch(e){}
    });

    uploader.on( 'uploadComplete', function( file ) {
		//$( '#'+file.id ).fadeOut();
    });

	$('#cancelButton').on( 'click', function() {
		if(uploader.isInProgress() == true)
		{
			if(confirm("<%=LANG['uploadclose_confirm']%>"))
			{
				var allFiles = uploader.getFiles();
				for(var i=0; i<allFiles.length; i++)
				{
					$( '#'+allFiles[i].id ).remove();
					uploader.removeFile(allFiles[i], true);
				}
				return true;
			}
			else
			{
				return false;
			}
		}

		if(uploader2.isInProgress() == true)
		{
			if(confirm("<%=LANG['uploadclose_confirm']%>"))
			{
				var allFiles = uploader2.getFiles();
				for(var i=0; i<allFiles.length; i++)
				{
					$( '#'+allFiles[i].id ).remove();
					uploader2.removeFile(allFiles[i], true);
				}
				return true;
			}
			else
			{
				return false;
			}
		}

		try
		{
			top.closeUploader();
		}
		catch(e){}
    });



    // file Queued
    uploader2.on( 'fileQueued', function( file ) {
        $list.append( '<div id="' + file.id + '" style="width:95%;padding-left:10px;">' +
            '<h4 class="info"><img src="images/cancel.gif" class="remove-this" title="<%=LANG["create_cancel"]%>" style="cursor:default;margin:3px;"> ' + file.name + ' (' + fileSize(file.size) + ')</h4>' +
        '</div>' );

		var $li = $( '#'+file.id );
		$li.on('click', '.remove-this', function() {
			uploader2.cancelFile( file );
			$li.remove();
		})
    });

    uploader2.on( 'uploadStart', function( file ) {
		//WIN10 edge browser can't trigger uploadProgress
		if(isEdge && uploader2.predictRuntimeType() != "flash")
		{
			var $li = $( '#'+file.id );
			var $percent = $li.find('.progress .progress-bar');

			if ( !$percent.length ) {
				$li.append('<div class="progress progress-striped active"><div class="progress-bar" role="progressbar" style="width:100%; text-align:left; padding-left:10px; color:#333; word-break:keep-all;"><%=LANG["upload_ready"]%>...</div></div>');
			}
		}
    });

    // upload Progress
    uploader2.on( 'uploadProgress', function( file, percentage ) {
        var $li = $( '#'+file.id );
		var $percent = $li.find('.progress .progress-bar');

        if ( !$percent.length ) {
			$li.append('<div class="progress progress-striped active"><div class="progress-bar" role="progressbar" style="text-align:left; padding-left:10px; color:#333; word-break:keep-all;"></div></div>');
			$percent = $li.find('.progress .progress-bar');
        }

		var strText = "<%=LANG['uploadstat_nowsize']%>"+round(percentage * 100, 0)+"%"
        $percent.text(strText);
        $percent.css( 'width', round(percentage * 100, 0) + '%' );
    });

    uploader2.on( 'uploadSuccess', function( file ) {
		uploadedFiles++;
        $( '#'+file.id ).find('.progress .progress-bar').text('<%=LANG["upload_ok"]%>');
		$( '#'+file.id ).fadeOut(1000);
		//if(isAndroid)
		setTimeout(function(){$( '#'+file.id ).remove();}, 1000);
    });

    uploader2.on( 'uploadError', function( file, reason ) {
		var result = CheckUploadError(file.name, file.size);
		if(result == "1")
		{
			$( '#'+file.id ).find('.progress .progress-bar').text('<%=LANG["upload_ok"]%>');
			$( '#'+file.id ).fadeOut(1000);
			setTimeout(function(){$( '#'+file.id ).remove();}, 1000);
		}
		else
		{
			var strError = "<%=LANG['upload_failed']%>";
			if(result == "-1")
			{
				strError = "<%=LANG['error_upload']%>";
			}
			else if(result == "-2")
			{
				strError = "<%=LANG['upload_failed']..' '..RESULT_STR[-3]%>";
			}
			else if(result == "-3")
			{
				strError = "<%=LANG['error_upload3']%>";
			}
			else if(result == "-4")
			{
				strError = "<%=LANG['error_upload4']%>";
			}
			else if(result == "-5")
			{
				strError = "<%=LANG['session_expire']%>";
			}
			else if(result == "-6")
			{
				strError = "<%=LANG['upload_failed']..' '..RESULT_STR[-6]%>";
			}
			else if(result == "-11")
			{
				strError = "<%=LANG['upload_failed']..' '..RESULT_STR[-11]%>";
			}
			else if(result == "0")
			{
				strError = "<%=LANG['upload_failed']..' '..RESULT_STR[-1]%>";
			}
			$( '#'+file.id ).find('.progress .progress-bar').addClass("progress-bar-danger");
			$( '#'+file.id ).find('.progress .progress-bar').css("color","#FFF");
			$( '#'+file.id ).find('.progress .progress-bar').text(strError);
			$( '#'+file.id ).fadeTo(3000, 1);
			$( '#'+file.id ).fadeOut(2000);
			setTimeout(function(){$( '#'+file.id ).remove();}, 5000);
		}
    });

    uploader2.on( 'startUpload', function() {
		//$('#cancelButton').attr('disabled', false);
		$('#cancelButton').attr('title', '<%=LANG["str_cancelupload"]%>');
		$('#infoDiv').html('');
		uploadedFiles = 0;
    });

    uploader2.on( 'uploadFinished', function() {
		try
		{
			if(uploadedFiles > 0)
				$('#infoDiv').html("<span style='color:#1F7FF8'><%=LANG["str_complete"]%> <%=LANG["str_totalfile"]%>: "+uploadedFiles+"</span>");

			top.resetOverwriteFolders();
			//$('#cancelButton').attr('disabled', true);
			if(uploader.isInProgress() == false && uploader2.isInProgress() == false)
			{
				var nTimeout = 0;
				$("div").find('.progress .progress-bar').each(function(){
					if(this.innerHTML != '<%=LANG["upload_ok"]%>')
					{
						nTimeout = 5000;
						return false;
					}
				});
				setTimeout(function(){$list.html(""); $('#cancelButton').attr('title', '<%=LANG["str_closewindow"]%>'); top.ajaxRequest("dir", "");}, nTimeout);
			}
		}
		catch(e){}
    });

	uploader2.on( 'fileQueued', function( file ) {
		try 
		{
			if(file.source.source.webkitRelativePath == null || file.source.source.webkitRelativePath == "")
			{
				var result = top.checkOverwrite(file.name, uploader2.getFiles("inited", "queued").length);
				if(result == "0")
				{
					$( '#'+file.id ).remove();
					uploader2.removeFile(file, true);
				}
			}
			else
			{
				var result = top.checkFolderOverwrite(file.source.source.webkitRelativePath);
				if(result == "0")
				{
					$( '#'+file.id ).remove();
					uploader2.removeFile(file, true);
				}
			}
		}
		catch(e){}
    });

    uploader2.on( 'uploadComplete', function( file ) {
		//$( '#'+file.id ).fadeOut();
    });

});


document.oncontextmenu = function(){
	return false;
}
</script>
<% 
else
	print("<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>")
	print("<script>top.location='login.html';</script>")
end 
%>