//############################################## // MONSTA FTP by MONSTA APPS //############################################## // // This file is part of Monsta FTP. Please see index.php for copyright, // license and support details. //############################################## // AJAX NOTES //############################################## // // The "multiple" attribute for select lists is not supported. // Javascript file include and function calls must be included *after* the form html. // Javascript that is returned from the AJAX request cannot be executed by the browser. //############################################## // START AJAX //############################################## function ajaxStart() { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { alert(lang_no_xmlhttp); } return xmlhttp; } // ########################################### // DETECT current GET params // ########################################### function getUrlVars() { var vars = {}; var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { if(value.substring(value.length - 1) == "#") { vars[key] = value.substring(0, value.length - 1); } else { vars[key] = value; } }); return vars; } var urlWithID = 'userpanel.php?w=gs&d=wf&id=' + getUrlVars()["id"]; // ########################################### // DETECT BROWSER // ########################################### var globalBrowser; function detectBrowser() { var userAgent = navigator.userAgent.toLowerCase(); // IE version check (IE10+ supports file input onChange and drag & drop uploads) if (userAgent.indexOf("msie") !=-1) { if (parseFloat((userAgent.match(/.*(?:rv|ie)[\/: ](.+?)([ \);]|$)/) || [])[1]) >= 10) globalBrowser = "ie10+"; else globalBrowser = "ie9-"; } // Other browsers if (userAgent.indexOf("firefox") !=-1) globalBrowser = "firefox"; if (userAgent.indexOf("safari") !=-1) globalBrowser = "safari"; if (userAgent.indexOf("chrome") !=-1) globalBrowser = "chrome"; // Chrome check must follow Safari as userAgent for Chrome includes "Safari" if (userAgent.indexOf("opera") !=-1) globalBrowser = "opera"; } // ########################################### // DETECT OS // ########################################### var globalOs; function detectOs() { if (navigator.appVersion.indexOf("Win")!=-1) globalOs = "win"; if (navigator.appVersion.indexOf("Mac")!=-1) globalOs = "mac"; } //############################################## // INITIALISE EVENT LISTENERS (DRAG & DROP) //############################################## function listenDropFiles() { // Set the drag & drop div ID var dropFilesDiv = document.getElementById("ajaxContentWindow"); var dropFilesCheckDiv = document.getElementById("dropFilesCheckDiv"); var xmlhttp = new XMLHttpRequest(); // Check if upload is supported if (xmlhttp.upload) { // Add listener to file drop div dropFilesDiv.addEventListener("dragover", stopBrowserActions, false); dropFilesDiv.addEventListener("dragleave", stopBrowserActions, false); dropFilesDiv.addEventListener("drop", captureDropFiles, false); // D&D supported message //dropFilesCheckDiv.innerHTML = lang_support_drop; //dropFilesCheckDiv.className = 'dropFilesCheckPassColor'; } else { // D&D unsupported message //dropFilesCheckDiv.innerHTML = lang_no_support_drop; //dropFilesCheckDiv.className = 'dropFilesCheckFailColor'; } } //############################################### // INITIALISE EVENT LISTENERS (HIDE CONTEXT MENU) //############################################### function listenContextMenu() { if (globalBrowser == "ie") document.attachEvent("onclick", hideFileContextMenu); else document.addEventListener("click", hideFileContextMenu, true); } //############################################## // CAPTURE DROPPED FILES //############################################## function captureDropFiles(e) { stopBrowserActions(e); // Get the dropped files var globalFiles = e.target.files || e.dataTransfer.files; // firefox/safari || chrome // Upload files if (globalFiles.length > 0) processFileUploads(e,1,0,globalFiles); } //############################################## // PROCESS FILE UPLOADS //############################################## var globalFileUploadPreCount; var globalFileUploadCount; var globalFileUploadTotal; var globalFiles = new Array(); var globalPaths = new Array(); function processFileUploads(e,isDrop,isFolder,globalFiles) { var xmlhttp = ajaxStart(); // Check upload is supported if (xmlhttp.upload) { // Turn off repeat button if (isDrop == 1 || isFolder == 1) hideRepeatButton(); // CHROME - DRAG & DROP FOLDER if (globalBrowser == "chrome" && isDrop == 1) { //globalFileUploadCount=0; var items = e.dataTransfer.items; // Check there is at least 1 file if (items.length > 0) { // Resize file listing window setFileWindowSize("ajaxContentWindow",0,360); // Count files to upload globalFileUploadPreCount=0; for (var i=0; i 0) { // Resize file listing window setFileWindowSize("ajaxContentWindow",0,360); for (var i=0, j=0, file, filePath, fileName; file=folderContents[i]; i++) { // Check file is not a folder (represented by a dot) if (file.name != ".") { fileName = file.webkitRelativePath; displayTransferWindow(file,fileName,j); // display file in transfer window filePath = fileName.replace(file.name,""); globalFiles[j] = file; // record file to array globalPaths[j] = encodeURIComponent(filePath); // record path to array j++; } } // Upload the folder uploadFoldersChrome(globalFiles); } } // OTHER BROWSERS - DRAG & DROP (OR SELECT FILE/S (ALSO CHROME)) if (globalBrowser != "chrome" || (isFolder == 0 && isDrop == 0)) { // Reset this value for Chrome folder drops globalFileUploadPreCount=0; // Set the files input field if not drag & drop if (globalFiles == '') globalFiles = document.getElementById('uploadFile').files; // Check there is at least 1 file if (globalFiles.length > 0) { // Resize file listing window setFileWindowSize("ajaxContentWindow",0,360); // Open transfer window for (var i=0, file; file=globalFiles[i]; i++) { displayTransferWindow(file,file.name,i); } // Display indicator icon showIndicatorDiv(); // Upload first file (subsequent files upload as each one finishes) globalFileUploadCount=0; globalFileUploadTotal = globalFiles.length; fileUploader(globalFiles,"",globalFileUploadCount,0,isDrop); } } } else { // Submit to iframe if AJAX upload not supported submitToIframe("&ftpAction=iframe_upload"); } } //############################################## // UPLOAD FILES (CHROME DRAG & DROP) //############################################## function uploadFilesChrome(globalFolders) { globalFileUploadCount=0; // must be set as a var var filePath = globalPaths[0]; // parse the first path fileUploader(globalFiles,filePath,globalFileUploadCount,0,1); } //############################################## // UPLOAD FOLDER (CHROME) //############################################## function uploadFoldersChrome(globalFiles) { // This function exists parallel to uploadFilesChrome() because the // value of globalFiles gets reset between processFileUploads() and // uploadFilesChrome(). globalFileUploadCount=0; // must be set as a var globalFileUploadTotal = globalFiles.length; var filePath = globalPaths[0]; // parse the first path fileUploader(globalFiles,filePath,globalFileUploadCount,1,0); } //############################################## // TRAVERSE FILE TREE (CHROME) //############################################## function traverseFileTree(item,filePath) { // If File if (item.isFile) { item.file(function(file) { var fileName = filePath + file.name; displayTransferWindow(file,fileName,globalFileUploadPreCount); // display file in transfer window globalFiles[globalFileUploadPreCount] = file; // record file to array globalPaths[globalFileUploadPreCount] = encodeURIComponent(filePath); // record path to array globalFileUploadPreCount++; }); } // If Directory if (item.isDirectory) { var dirReader = item.createReader(); // Read list of files/folders and call this function on each dirReader.readEntries(function(entries) { for (var i=0; i'+lang_transfer_pending+'
'; // progress bar var cell5 = row.insertCell(4); cell5.innerHTML = '
'; // time elapsed var cell6 = row.insertCell(5); cell6.innerHTML = '
'; // size uploaded var cell7 = row.insertCell(6); cell7.innerHTML = '
'; // transfer rate var cell8 = row.insertCell(7); cell8.innerHTML = '
'; // time remaining var cell9 = row.insertCell(8); cell9.innerHTML = '
'; // close button (for rejects) // Set background color of even-numbered rows if (rowID%2 == 0) row.className='trBg1'; else row.className='trBg0'; } //############################################## // FILE UPLOADER //############################################## function fileUploader(globalFiles,filePath,rowID,isFolder,isDrop) { showIndicatorDiv(); // Get file from array file = globalFiles[rowID]; rowID++; // Add 1 for table headers // Check the file size doesn't exceed allowed limit if (file.size > upload_limit) { // Change progress to error message document.getElementById('progressParent'+rowID).innerHTML = ''+lang_file_size_error+'' // status // Add close button to row document.getElementById('close'+rowID).innerHTML = 'x' // status // Start next transfer globalFileUploadCount++; // Check if another file needs uploading if (globalFileUploadCount < globalFileUploadTotal || globalFileUploadCount < globalFileUploadPreCount) { // Set path (if exists) if (globalPaths[globalFileUploadCount] != undefined) filePath = globalPaths[globalFileUploadCount]; fileUploader(globalFiles,filePath,globalFileUploadCount,isFolder,isDrop); } else { // Reset arrays globalFiles = []; globalPaths = []; // Display repeat button if (isDrop == 0 && isFolder == 0) showRepeatButton(); // Reset form for Chrome if (isFolder == 1) { resetForm(); } // Hide indicator icon hidePopUp('indicatorDiv'); } } else { // File size accepted var xmlhttp = ajaxStart(); // Set action for change of state (listener) xmlhttp.onreadystatechange = function stateChanged() { // Check if upload has completed if(xmlhttp.readyState==4) { // Set the values of the progress fields to max (finished) document.getElementById('progress'+rowID).value = 100; // progress bar document.getElementById('percent'+rowID).innerHTML = '100%'; // percent document.getElementById('timeR'+rowID).innerHTML = formatSecondsToTime(0); // time remaining document.getElementById('progressParent'+rowID).innerHTML = ''+lang_transferring_to_ftp+'' // status document.getElementById('percent'+rowID).innerHTML = ""; // percent document.getElementById('timeE'+rowID).innerHTML = ""; // time elapsed //document.getElementById('uploaded'+rowID).innerHTML = formatFileSize(file.size); (commented out because by the time the function returns, the value has already been cleared) document.getElementById('uploaded'+rowID).innerHTML = ""; // uploaded document.getElementById('rate'+rowID).innerHTML = ""; // transfer rate document.getElementById('timeR'+rowID).innerHTML = ""; // time remaining // Refresh open folder (delay half second to complete progress display) setTimeout(function(){ openThisFolder(globalOpenFolder,0) },500) // Delete the progress row from table (delay half second to complete progress display) setTimeout(function(){ deleteProgressRow(rowID) },500) // Start next transfer globalFileUploadCount++; // Check if another file needs uploading if (globalFileUploadCount < globalFileUploadTotal || globalFileUploadCount < globalFileUploadPreCount) { // Set path (if exists) if (globalPaths[globalFileUploadCount] != undefined) filePath = globalPaths[globalFileUploadCount] fileUploader(globalFiles,filePath,globalFileUploadCount,isFolder,isDrop); } else { // Reset arrays globalFiles = []; globalPaths = []; // Display repeat button if (isDrop == 0 && isFolder == 0) showRepeatButton(); // Reset form for Chrome if (isFolder == 1) { resetForm(); } } } } // Create the progress bar document.getElementById('progressParent'+rowID).innerHTML = ''; var start = new Date().getTime(), elapsed = '0.0'; var time=0; var elapsed=0; var bytesPerSecond=0; var timeToUpload=0; var progress=0; var progressBar; // Update progress info xmlhttp.upload.onprogress = function(e) { if (e.lengthComputable) { // Get elapsed time time = new Date().getTime() - start; elapsed = Math.floor(time / 1000); // Set the elapsed time document.getElementById('timeE'+rowID).innerHTML = formatSecondsToTime(elapsed); // Set the uploaded amount document.getElementById('uploaded'+rowID).innerHTML = formatFileSize(e.loaded); // Get the transfer rate if (elapsed == 0) bytesPerSecond = e.loaded; // if less than 1s set xfer rate to file size else bytesPerSecond = e.loaded/elapsed; // Set the transfer rate document.getElementById('rate'+rowID).innerHTML = formatFileSize(bytesPerSecond) + '/s'; // Get remaining time timeToUpload = Math.round((e.total - e.loaded) / bytesPerSecond); // Set the remaining time document.getElementById('timeR'+rowID).innerHTML = formatSecondsToTime(timeToUpload); // Get the progress progressBar = document.getElementById('progress'+rowID); progressBar.value = (e.loaded / e.total) * 100; // Display %age complete document.getElementById('percent'+rowID).innerHTML = Math.round(progressBar.value) + '%'; } }; // Post form xmlhttp.open("POST", urlWithID + "&ftpAction=upload&filePath="+filePath, true); xmlhttp.setRequestHeader("Cache-Control", "no-cache"); xmlhttp.setRequestHeader("X-Filename", file.name); xmlhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest"); xmlhttp.setRequestHeader("X-File-Size", file.size); xmlhttp.setRequestHeader("X-File-Type", file.type); xmlhttp.send(file); } } //############################################## // STOP BROWSER ACTIONS //############################################## function stopBrowserActions(e) { e.stopPropagation(); e.preventDefault(); } //############################################## // PROCESS FORM ONCLICK //############################################## function processForm(vars) { // Display indicator icon showIndicatorDiv(); var xmlhttp = ajaxStart(); // Get form data vars = vars + generateVars(); // Add window dimensions vars = vars + "&windowWidth=" + window.innerWidth; vars = vars + "&windowHeight=" + window.innerHeight; // Return HTML from AJAX to div (when complete) xmlhttp.onreadystatechange = function stateChanged() { if(xmlhttp.readyState==4) { document.getElementById("ajaxContentWindow").innerHTML=xmlhttp.responseText; } } // Post form xmlhttp.open("POST",urlWithID,true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.send(vars); // Hide indicator icon hidePopUp('indicatorDiv'); } //############################################## // CREATE URL POST VARS //############################################## function generateVars() { var theForm = document.getElementById('ftpActionForm'); var vars = ''; for(i=0;i