function GetURLParameter(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
function checkSession() {
var ret = true;
var request = new XMLHttpRequest();
request.open('GET', 'modules/litefm/SessionCheck.php', false);
request.onload = function(e) {
var session = JSON.parse(this.responseText);
if(!session.valid) {
alert("Your session has expired.");
window.location = "index.php";
ret = false;
}
}
request.send(null);
return ret;
}
function detectIE() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ');
var trident = ua.indexOf('Trident/');
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}
if (trident > 0) {
// IE 11 (or newer) => return version number
var rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}
// other browser
return false;
}
var ie = detectIE();
if (!Date.now) {
Date.now = function() { return new Date().getTime(); }
}
function indexedDBOk() {
return "indexedDB" in window;
}
function downloadFile(home_id, file_id, file_size, file_name)
{
if(checkSession() == false) { return; }
var fileNode = document.getElementById('fileid'+file_id);
var aLink = document.getElementById('jsDwl'+file_id);
if (aLink.getAttribute('disabled') == 'disabled') {
return;
}
aLink.setAttribute('disabled','disabled');
var did = Date.now();
var bytesDownloaded = 0;
var progressbar = document.createElement('DIV');
var progress = document.createElement('DIV');
var percentage = document.createElement("SPAN");
progressbar.setAttribute('id','progress_'+file_id);
progressbar.setAttribute('style', 'background:white;'+
'height:4px;'+
'width:400px;'+
'border:1px solid black;'+
'position:absolute;'+
'z-index:2;');
percentage.setAttribute('id','percentage_'+file_id);
progressbar.appendChild(progress);
fileNode.appendChild(progressbar);
fileNode.appendChild(percentage);
progressbar = document.getElementById('progress_'+file_id);
progress = progressbar.firstChild;
percentage = document.getElementById('percentage_'+file_id);
var key = 1;
var db;
var memoryStore;
function failure() {
progressbar.parentNode.removeChild(progressbar);
percentage.parentNode.removeChild(percentage);
window.onbeforeunload = false;
window.onunload = false;
}
function init() {
if(ie)
{
if(ie >= 10)
{
memoryStore = [];
getFilePart();
}
else
{
failure();
alert('Your browser does not support this function.');
return;
}
}
else
{
if( !indexedDBOk() )
{
failure();
alert('Your browser does not support this function.');
return;
}
var openRequest = indexedDB.open(did,1);
openRequest.onupgradeneeded = function(e) {
var thisDB = e.target.result;
if(!thisDB.objectStoreNames.contains("dl_parts")) {
thisDB.createObjectStore("dl_parts");
}
}
openRequest.onsuccess = function(e) {
db = e.target.result;
getFilePart();
}
openRequest.onerror = function(e) {
failure();
cleanUp();
alert(e.target.error.name);
return;
}
}
}
function addBuffer(arraybuffer, key) {
if(ie)
{
// Store the blob in memory
memoryStore.push(new Blob([arraybuffer]));
}
else
{
// Store the blob in indexedDB
var transaction = db.transaction(["dl_parts"],"readwrite");
var store = transaction.objectStore("dl_parts");
var request = store.add(new Blob([arraybuffer]), key);
request.onerror = function(e) {
failure();
cleanUp();
alert(e.target.error.name);
return;
}
}
}
function getAllParts() {
if(ie)
{
navigator.msSaveOrOpenBlob(new Blob(memoryStore), file_name);
}
else
{
var trans = db.transaction(["dl_parts"],"readonly");
var store = trans.objectStore("dl_parts");
var parts = [];
trans.oncomplete = function(evt) {
try
{
var fileUrl = (window.URL || window.webkitURL).createObjectURL(new Blob(parts));
var a = document.createElement('a');
a.href = fileUrl;
a.target = '_blank';
a.download = file_name || fileUrl;
var evt=new MouseEvent('click', {'view':window,'bubbles':true,'cancelable':true});
a.dispatchEvent(evt);
// Otherwise Firefox fails starting the download.
setTimeout(function() { window.URL.revokeObjectURL(a.href); }, 1);
parts = [];
}
catch(e)
{
console.log(e);
}
};
var cursorRequest = store.openCursor();
cursorRequest.onerror = function(error) {
failure();
cleanUp();
alert(error);
return;
};
cursorRequest.onsuccess = function(evt) {
var cursor = evt.target.result;
if (cursor) {
parts.push(cursor.value);
cursor.continue();
}
};
}
}
function cleanUp() {
if(ie)
{
memoryStore = [];
}
else
{
db.close();
indexedDB.deleteDatabase(did);
}
var xhr = new XMLHttpRequest();
xhr.open('POST', "home.php?m=litefm&home_id="+home_id+"&p=get&remove_did="+did+"&type=cleared", false);
xhr.send(null);
window.onunload = false;
}
function getFilePart()
{
var xhr = new XMLHttpRequest();
xhr.open('POST', "home.php?m=litefm&home_id="+home_id+"&item="+file_id+"&name="+file_name+"&p=get&type=cleared&size="+file_size+"&did="+did, true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
if(this.response.byteLength != 0)
{
var lapTime = Date.now() - did; // milliseconds from download start
// Uncompress and calculate the statistics.
var compressed = new Uint8Array(this.response);
var inflate = new Zlib.Inflate(compressed);
var plain = inflate.decompress();
bytesDownloaded += plain.byteLength;
var multiplier = 1000 / lapTime;
var MBs = (bytesDownloaded / 1000000) * multiplier;
var value = bytesDownloaded * 100 / file_size;
progress.setAttribute('style', 'background:blue;'+
'height:4px;'+
'width:'+value+'%;');
percentage.innerHTML = " "+(Math.round(value * 100) / 100)+"% - "+MBs.toFixed(2)+"MB/s";
addBuffer(plain, key++);
// Get the next part
getFilePart();
}
else
{
getAllParts();
// Otherwise Firefox fails starting the download.
setTimeout(function() { cleanUp(); }, 60000);
// element.remove(); fails on IE
progressbar.parentNode.removeChild(progressbar);
percentage.parentNode.removeChild(percentage);
aLink.removeAttribute('disabled');
window.onbeforeunload = false;
}
};
xhr.send(null);
}
window.onbeforeunload = function(){
return 'A file download is in progress';
};
window.onunload = function(){
cleanUp();
}
init();
}
$(document).ready(function(){
/* translation & info */
var select_at_least_one_item = $('#dialog').attr('data-select_at_least_one_item'),
ask_delete = $('#dialog').attr('data-ask_delete'),
ask_rename = $('#dialog').attr('data-ask_rename'),
ask_move = $('#dialog').attr('data-ask_move'),
ask_copy = $('#dialog').attr('data-ask_copy'),
ask_compress = $('#dialog').attr('data-ask_compress'),
ask_uncompress = $('#dialog').attr('data-ask_uncompress'),
archive_name = $('#dialog').attr('data-archive_name'),
archive_type = $('#dialog').attr('data-archive_type'),
file_name = $('#dialog').attr('data-file_name'),
folder_name = $('#dialog').attr('data-folder_name'),
compresses_files_separately = $('#dialog').attr('data-compresses_files_separately'),
to = $('#dialog').attr('data-to'),
yes = $('#dialog').attr('data-yes'),
no = $('#dialog').attr('data-no'),
max_file_uploads = $('#dialog').attr('data-max_file_uploads'),
upload_to_web = $('#dialog').attr('data-upload_to_web'),
transfer_to_server = $('#dialog').attr('data-transfer_to_server'),
upload = $('#dialog').attr('data-upload'),
ask_change_attr = $('#dialog').attr('data-ask_change_attr'),
ask_send_by_email = $('#dialog').attr('data-ask_send_by_email'),
subject = $('#dialog').attr('data-subject'),
message = $('#dialog').attr('data-message'),
dest_email = $('#dialog').attr('data-dest_email'),
user_email = $('#dialog').attr('data-user_email');
// Home id
var home_id = GetURLParameter('home_id');
// Browse folder (when using "move/uncompress")
$('.folder').each(function(){
$(this).click(function(){
if(checkSession() == false) { return; }
var addpost = {};
addpost[ 'm' ] = 'user_games';
addpost[ 'p' ] = 'browser';
addpost[ 'home_id' ] = $('.levelup').attr('data-home-id');
addpost[ 'item' ] = $(this).attr('data-item');
addpost[ 'type' ] = 'cleared';
$.ajax({
type: "GET",
url: "home.php",
data: addpost,
success: function(data){
data = data.replace("user_games.js","litefm.js");
$( "#browser" ).html(data);
}
});
});
});
// Back to previous folder (when using "move/uncompress")
$('.levelup').click(function(){
if(checkSession() == false) { return; }
var addpost = {};
addpost[ 'm' ] = 'user_games';
addpost[ 'p' ] = 'browser';
addpost[ 'home_id' ] = $(this).attr('data-home-id');
addpost[ 'back' ] = 'back';
addpost[ 'type' ] = 'cleared';
$.ajax({
type: "GET",
url: "home.php",
data: addpost,
success: function(data){
data = data.replace("user_games.js","litefm.js");
$( "#browser" ).html(data);
}
});
});
// Create new folder (when using "move/uncompress")
$('#addfolder').click(function(){
if(checkSession() == false) { return; }
var addpost = {};
addpost[ 'm' ] = 'user_games';
addpost[ 'p' ] = 'browser';
addpost[ 'home_id' ] = home_id;
addpost[ 'create_folder' ] = 'create_folder';
addpost[ 'folder_name' ] = $('input[name=dirname]').val();
addpost[ 'type' ] = 'cleared';
$.ajax({
type: "GET",
url: "home.php",
data: addpost,
success: function(data){
data = data.replace("user_games.js","litefm.js");
$( "#browser" ).html(data);
}
});
});
// Switch checkboxes
$("#switch_check").click(function(){
var checkBoxes = $('input[class="item"]');
checkBoxes.prop("checked", !checkBoxes.prop("checked"));
});
// Remove
$("#remove.operations-button").click(function(){
if(checkSession() == false) { return; }
var addpost = {};
addpost.items = [];
addpost.remove = '';
var item = '';
var value = '';
var items = '';
$('input[class="item"]:checked').each(function(){
item = $(this).attr('data-item');
value = $(this).attr('value');
addpost.items.push(item);
items += "
"+value;
});
if(items != '')
{
$('#dialog').html(ask_delete.replace("%s", ":"+items));
$('#dialog').dialog({
autoOpen: true,
width: 450,
modal: true,
buttons: [{ text: yes, click: function(){
$.ajax({
type: "POST",
url: "home.php?m=litefm&home_id="+home_id+"&type=cleared",
data: addpost,
complete: function(){
window.location.href = window.location.href.replace('&back','');
}
});
$( this ).dialog( "close" );
}
},{ text: no, click: function(){
$( this ).dialog( "close" );
}
}],
close: function() {
$( this ).dialog( "close" );
}
});
}
else
{
alert(select_at_least_one_item);
}
});
// Rename
$("#rename.operations-button").click(function(){
if(checkSession() == false) { return; }
var addpost = {};
addpost.items = [];
addpost.values = [];
addpost.rename = '';
var value = '';
var items = '';
$('input[class="item"]:checked').each(function(){
item = $(this).attr('data-item');
value = $(this).attr('value').replace('"', """);
addpost.items.push(item);
items += "
";
});
if(items != '')
{
$('#dialog').html(ask_rename+":"+items);
$('#dialog').dialog({
autoOpen: true,
width: 450,
modal: true,
buttons: [{ text: yes, click: function(){
$('input[class="rename"]').each(function(){
value = $(this).val();
addpost.values.push(value);
});
$.ajax({
type: "POST",
url: "home.php?m=litefm&home_id="+home_id+"&type=cleared",
data: addpost,
complete: function(){
window.location.href = window.location.href.replace('&back','');
}
});
$( this ).dialog( "close" );
}
},{ text: no, click: function(){
$( this ).dialog( "close" );
}
}],
close: function() {
$( this ).dialog( "close" );
}
});
}
else
{
alert(select_at_least_one_item);
}
});
// Move
$("#move.operations-button").click(function(){
if(checkSession() == false) { return; }
var addpost = {};
addpost.items = [];
addpost.values = [];
addpost.move = '';
var value = '';
var items = '';
$('input[class="item"]:checked').each(function(){
item = $(this).attr('data-item');
value = $(this).attr('value');
addpost.items.push(item);
items += "
"+value;
});
if(items != '')
{
var addpostb = {};
addpostb[ 'm' ] = 'user_games';
addpostb[ 'p' ] = 'browser';
addpostb[ 'home_id' ] = home_id;
addpostb[ 'folder' ] = $('#dialog').attr('data-folder');
addpostb[ 'type' ] = 'cleared';
$.ajax({
type: "GET",
url: "home.php",
data: addpostb,
async: false,
success: function(data){
data = data.replace("user_games.js","litefm.js");
$('#dialog').html(ask_move+":"+items+"
"+to+":