Edit File by line
/home/barbar84/www/wp-conte.../plugins/file-man.../applicat.../library/js/proxy
File: elFinderSupportVer1.js
/**
[0] Fix | Delete
* elFinder transport to support old protocol.
[1] Fix | Delete
*
[2] Fix | Delete
* @example
[3] Fix | Delete
* $('selector').elfinder({
[4] Fix | Delete
* ....
[5] Fix | Delete
* transport : new elFinderSupportVer1()
[6] Fix | Delete
* })
[7] Fix | Delete
*
[8] Fix | Delete
* @author Dmitry (dio) Levashov
[9] Fix | Delete
**/
[10] Fix | Delete
window.elFinderSupportVer1 = function(upload) {
[11] Fix | Delete
"use strict";
[12] Fix | Delete
var self = this,
[13] Fix | Delete
dateObj, today, yesterday,
[14] Fix | Delete
getDateString = function(date) {
[15] Fix | Delete
return date.replace('Today', today).replace('Yesterday', yesterday);
[16] Fix | Delete
};
[17] Fix | Delete
[18] Fix | Delete
dateObj = new Date();
[19] Fix | Delete
today = dateObj.getFullYear() + '/' + (dateObj.getMonth() + 1) + '/' + dateObj.getDate();
[20] Fix | Delete
dateObj = new Date(Date.now() - 86400000);
[21] Fix | Delete
yesterday = dateObj.getFullYear() + '/' + (dateObj.getMonth() + 1) + '/' + dateObj.getDate();
[22] Fix | Delete
[23] Fix | Delete
this.upload = upload || 'auto';
[24] Fix | Delete
[25] Fix | Delete
this.init = function(fm) {
[26] Fix | Delete
this.fm = fm;
[27] Fix | Delete
this.fm.parseUploadData = function(text) {
[28] Fix | Delete
var data;
[29] Fix | Delete
[30] Fix | Delete
if (!$.trim(text)) {
[31] Fix | Delete
return {error : ['errResponse', 'errDataEmpty']};
[32] Fix | Delete
}
[33] Fix | Delete
[34] Fix | Delete
try {
[35] Fix | Delete
data = JSON.parse(text);
[36] Fix | Delete
} catch (e) {
[37] Fix | Delete
return {error : ['errResponse', 'errDataNotJSON']};
[38] Fix | Delete
}
[39] Fix | Delete
[40] Fix | Delete
return self.normalize('upload', data);
[41] Fix | Delete
};
[42] Fix | Delete
};
[43] Fix | Delete
[44] Fix | Delete
[45] Fix | Delete
this.send = function(opts) {
[46] Fix | Delete
var self = this,
[47] Fix | Delete
fm = this.fm,
[48] Fix | Delete
dfrd = $.Deferred(),
[49] Fix | Delete
cmd = opts.data.cmd,
[50] Fix | Delete
args = [],
[51] Fix | Delete
_opts = {},
[52] Fix | Delete
data,
[53] Fix | Delete
xhr;
[54] Fix | Delete
[55] Fix | Delete
dfrd.abort = function() {
[56] Fix | Delete
if (xhr.state() == 'pending') {
[57] Fix | Delete
xhr.quiet = true;
[58] Fix | Delete
xhr.abort();
[59] Fix | Delete
}
[60] Fix | Delete
};
[61] Fix | Delete
[62] Fix | Delete
switch (cmd) {
[63] Fix | Delete
case 'open':
[64] Fix | Delete
opts.data.tree = 1;
[65] Fix | Delete
break;
[66] Fix | Delete
case 'parents':
[67] Fix | Delete
case 'tree':
[68] Fix | Delete
return dfrd.resolve({tree : []});
[69] Fix | Delete
case 'get':
[70] Fix | Delete
opts.data.cmd = 'read';
[71] Fix | Delete
opts.data.current = fm.file(opts.data.target).phash;
[72] Fix | Delete
break;
[73] Fix | Delete
case 'put':
[74] Fix | Delete
opts.data.cmd = 'edit';
[75] Fix | Delete
opts.data.current = fm.file(opts.data.target).phash;
[76] Fix | Delete
break;
[77] Fix | Delete
case 'archive':
[78] Fix | Delete
case 'rm':
[79] Fix | Delete
opts.data.current = fm.file(opts.data.targets[0]).phash;
[80] Fix | Delete
break;
[81] Fix | Delete
case 'extract':
[82] Fix | Delete
case 'rename':
[83] Fix | Delete
case 'resize':
[84] Fix | Delete
opts.data.current = fm.file(opts.data.target).phash;
[85] Fix | Delete
break;
[86] Fix | Delete
case 'duplicate':
[87] Fix | Delete
_opts = $.extend(true, {}, opts);
[88] Fix | Delete
[89] Fix | Delete
$.each(opts.data.targets, function(i, hash) {
[90] Fix | Delete
$.ajax(Object.assign(_opts, {data : {cmd : 'duplicate', target : hash, current : fm.file(hash).phash}}))
[91] Fix | Delete
.fail(function(error) {
[92] Fix | Delete
fm.error(fm.res('error', 'connect'));
[93] Fix | Delete
})
[94] Fix | Delete
.done(function(data) {
[95] Fix | Delete
data = self.normalize('duplicate', data);
[96] Fix | Delete
if (data.error) {
[97] Fix | Delete
fm.error(data.error);
[98] Fix | Delete
} else if (data.added) {
[99] Fix | Delete
fm.trigger('add', {added : data.added});
[100] Fix | Delete
}
[101] Fix | Delete
});
[102] Fix | Delete
});
[103] Fix | Delete
return dfrd.resolve({});
[104] Fix | Delete
[105] Fix | Delete
case 'mkdir':
[106] Fix | Delete
case 'mkfile':
[107] Fix | Delete
opts.data.current = opts.data.target;
[108] Fix | Delete
break;
[109] Fix | Delete
case 'paste':
[110] Fix | Delete
opts.data.current = opts.data.dst;
[111] Fix | Delete
if (! opts.data.tree) {
[112] Fix | Delete
$.each(opts.data.targets, function(i, h) {
[113] Fix | Delete
if (fm.file(h) && fm.file(h).mime === 'directory') {
[114] Fix | Delete
opts.data.tree = '1';
[115] Fix | Delete
return false;
[116] Fix | Delete
}
[117] Fix | Delete
});
[118] Fix | Delete
}
[119] Fix | Delete
break;
[120] Fix | Delete
[121] Fix | Delete
case 'size':
[122] Fix | Delete
return dfrd.resolve({error : fm.res('error', 'cmdsupport')});
[123] Fix | Delete
case 'search':
[124] Fix | Delete
return dfrd.resolve({error : fm.res('error', 'cmdsupport')});
[125] Fix | Delete
[126] Fix | Delete
case 'file':
[127] Fix | Delete
opts.data.cmd = 'open';
[128] Fix | Delete
opts.data.current = fm.file(opts.data.target).phash;
[129] Fix | Delete
break;
[130] Fix | Delete
}
[131] Fix | Delete
// cmd = opts.data.cmd
[132] Fix | Delete
[133] Fix | Delete
xhr = $.ajax(opts)
[134] Fix | Delete
.fail(function(error) {
[135] Fix | Delete
dfrd.reject(error);
[136] Fix | Delete
})
[137] Fix | Delete
.done(function(raw) {
[138] Fix | Delete
data = self.normalize(cmd, raw);
[139] Fix | Delete
dfrd.resolve(data);
[140] Fix | Delete
});
[141] Fix | Delete
[142] Fix | Delete
return dfrd;
[143] Fix | Delete
};
[144] Fix | Delete
[145] Fix | Delete
// fix old connectors errors messages as possible
[146] Fix | Delete
// this.errors = {
[147] Fix | Delete
// 'Unknown command' : 'Unknown command.',
[148] Fix | Delete
// 'Invalid backend configuration' : 'Invalid backend configuration.',
[149] Fix | Delete
// 'Access denied' : 'Access denied.',
[150] Fix | Delete
// 'PHP JSON module not installed' : 'PHP JSON module not installed.',
[151] Fix | Delete
// 'File not found' : 'File not found.',
[152] Fix | Delete
// 'Invalid name' : 'Invalid file name.',
[153] Fix | Delete
// 'File or folder with the same name already exists' : 'File named "$1" already exists in this location.',
[154] Fix | Delete
// 'Not allowed file type' : 'Not allowed file type.',
[155] Fix | Delete
// 'File exceeds the maximum allowed filesize' : 'File exceeds maximum allowed size.',
[156] Fix | Delete
// 'Unable to copy into itself' : 'Unable to copy "$1" into itself.',
[157] Fix | Delete
// 'Unable to create archive' : 'Unable to create archive.',
[158] Fix | Delete
// 'Unable to extract files from archive' : 'Unable to extract files from "$1".'
[159] Fix | Delete
// }
[160] Fix | Delete
[161] Fix | Delete
this.normalize = function(cmd, data) {
[162] Fix | Delete
var self = this,
[163] Fix | Delete
fm = this.fm,
[164] Fix | Delete
files = {},
[165] Fix | Delete
filter = function(file) { return file && file.hash && file.name && file.mime ? file : null; },
[166] Fix | Delete
getDirs = function(items) {
[167] Fix | Delete
return $.grep(items, function(i) {
[168] Fix | Delete
return i && i.mime && i.mime === 'directory'? true : false;
[169] Fix | Delete
});
[170] Fix | Delete
},
[171] Fix | Delete
getTreeDiff = function(files) {
[172] Fix | Delete
var dirs = getDirs(files);
[173] Fix | Delete
treeDiff = fm.diff(dirs, null, ['date', 'ts']);
[174] Fix | Delete
if (treeDiff.added.length) {
[175] Fix | Delete
treeDiff.added = getDirs(treeDiff.added);
[176] Fix | Delete
}
[177] Fix | Delete
if (treeDiff.changed.length) {
[178] Fix | Delete
treeDiff.changed = getDirs(treeDiff.changed);
[179] Fix | Delete
}
[180] Fix | Delete
if (treeDiff.removed.length) {
[181] Fix | Delete
var removed = [];
[182] Fix | Delete
$.each(treeDiff.removed, function(i, h) {
[183] Fix | Delete
var item;
[184] Fix | Delete
if ((item = fm.file(h)) && item.mime === 'directory') {
[185] Fix | Delete
removed.push(h);
[186] Fix | Delete
}
[187] Fix | Delete
});
[188] Fix | Delete
treeDiff.removed = removed;
[189] Fix | Delete
}
[190] Fix | Delete
return treeDiff;
[191] Fix | Delete
},
[192] Fix | Delete
phash, diff, isCwd, treeDiff;
[193] Fix | Delete
[194] Fix | Delete
if ((cmd == 'tmb' || cmd == 'get')) {
[195] Fix | Delete
return data;
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
// if (data.error) {
[199] Fix | Delete
// $.each(data.error, function(i, msg) {
[200] Fix | Delete
// if (self.errors[msg]) {
[201] Fix | Delete
// data.error[i] = self.errors[msg];
[202] Fix | Delete
// }
[203] Fix | Delete
// });
[204] Fix | Delete
// }
[205] Fix | Delete
[206] Fix | Delete
if (cmd == 'upload' && data.error && data.cwd) {
[207] Fix | Delete
data.warning = Object.assign({}, data.error);
[208] Fix | Delete
data.error = false;
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
[212] Fix | Delete
if (data.error) {
[213] Fix | Delete
return data;
[214] Fix | Delete
}
[215] Fix | Delete
[216] Fix | Delete
if (cmd == 'put') {
[217] Fix | Delete
[218] Fix | Delete
phash = fm.file(data.target.hash).phash;
[219] Fix | Delete
return {changed : [this.normalizeFile(data.target, phash)]};
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
phash = data.cwd.hash;
[223] Fix | Delete
[224] Fix | Delete
isCwd = (phash == fm.cwd().hash);
[225] Fix | Delete
[226] Fix | Delete
if (data.tree) {
[227] Fix | Delete
$.each(this.normalizeTree(data.tree), function(i, file) {
[228] Fix | Delete
files[file.hash] = file;
[229] Fix | Delete
});
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
$.each(data.cdc||[], function(i, file) {
[233] Fix | Delete
var hash = file.hash,
[234] Fix | Delete
mcts;
[235] Fix | Delete
[236] Fix | Delete
if (files[hash]) {
[237] Fix | Delete
if (file.date) {
[238] Fix | Delete
mcts = Date.parse(getDateString(file.date));
[239] Fix | Delete
if (mcts && !isNaN(mcts)) {
[240] Fix | Delete
files[hash].ts = Math.floor(mcts / 1000);
[241] Fix | Delete
} else {
[242] Fix | Delete
files[hash].date = file.date || fm.formatDate(file);
[243] Fix | Delete
}
[244] Fix | Delete
}
[245] Fix | Delete
files[hash].locked = file.hash == phash ? true : file.rm === void(0) ? false : !file.rm;
[246] Fix | Delete
} else {
[247] Fix | Delete
files[hash] = self.normalizeFile(file, phash, data.tmb);
[248] Fix | Delete
}
[249] Fix | Delete
});
[250] Fix | Delete
[251] Fix | Delete
if (!data.tree) {
[252] Fix | Delete
$.each(fm.files(), function(hash, file) {
[253] Fix | Delete
if (!files[hash] && file.phash != phash && file.mime == 'directory') {
[254] Fix | Delete
files[hash] = file;
[255] Fix | Delete
}
[256] Fix | Delete
});
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
if (cmd == 'open') {
[260] Fix | Delete
return {
[261] Fix | Delete
cwd : files[phash] || this.normalizeFile(data.cwd),
[262] Fix | Delete
files : $.map(files, function(f) { return f; }),
[263] Fix | Delete
options : self.normalizeOptions(data),
[264] Fix | Delete
init : !!data.params,
[265] Fix | Delete
debug : data.debug
[266] Fix | Delete
};
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
if (isCwd) {
[270] Fix | Delete
diff = fm.diff($.map(files, filter));
[271] Fix | Delete
} else {
[272] Fix | Delete
if (data.tree && cmd !== 'paste') {
[273] Fix | Delete
diff = getTreeDiff(files);
[274] Fix | Delete
} else {
[275] Fix | Delete
diff = {
[276] Fix | Delete
added : [],
[277] Fix | Delete
removed : [],
[278] Fix | Delete
changed : []
[279] Fix | Delete
};
[280] Fix | Delete
if (cmd === 'paste') {
[281] Fix | Delete
diff.sync = true;
[282] Fix | Delete
}
[283] Fix | Delete
}
[284] Fix | Delete
}
[285] Fix | Delete
[286] Fix | Delete
return Object.assign({
[287] Fix | Delete
current : data.cwd.hash,
[288] Fix | Delete
error : data.error,
[289] Fix | Delete
warning : data.warning,
[290] Fix | Delete
options : {tmb : !!data.tmb}
[291] Fix | Delete
}, diff);
[292] Fix | Delete
[293] Fix | Delete
};
[294] Fix | Delete
[295] Fix | Delete
/**
[296] Fix | Delete
* Convert old api tree into plain array of dirs
[297] Fix | Delete
*
[298] Fix | Delete
* @param Object root dir
[299] Fix | Delete
* @return Array
[300] Fix | Delete
*/
[301] Fix | Delete
this.normalizeTree = function(root) {
[302] Fix | Delete
var self = this,
[303] Fix | Delete
result = [],
[304] Fix | Delete
traverse = function(dirs, phash) {
[305] Fix | Delete
var i, dir;
[306] Fix | Delete
[307] Fix | Delete
for (i = 0; i < dirs.length; i++) {
[308] Fix | Delete
dir = dirs[i];
[309] Fix | Delete
result.push(self.normalizeFile(dir, phash));
[310] Fix | Delete
dir.dirs.length && traverse(dir.dirs, dir.hash);
[311] Fix | Delete
}
[312] Fix | Delete
};
[313] Fix | Delete
[314] Fix | Delete
traverse([root]);
[315] Fix | Delete
[316] Fix | Delete
return result;
[317] Fix | Delete
};
[318] Fix | Delete
[319] Fix | Delete
/**
[320] Fix | Delete
* Convert file info from old api format into new one
[321] Fix | Delete
*
[322] Fix | Delete
* @param Object file
[323] Fix | Delete
* @param String parent dir hash
[324] Fix | Delete
* @return Object
[325] Fix | Delete
*/
[326] Fix | Delete
this.normalizeFile = function(file, phash, tmb) {
[327] Fix | Delete
var mime = file.mime || 'directory',
[328] Fix | Delete
size = mime == 'directory' && !file.linkTo ? 0 : file.size,
[329] Fix | Delete
mcts = file.date? Date.parse(getDateString(file.date)) : void 0,
[330] Fix | Delete
info = {
[331] Fix | Delete
url : file.url,
[332] Fix | Delete
hash : file.hash,
[333] Fix | Delete
phash : phash,
[334] Fix | Delete
name : file.name,
[335] Fix | Delete
mime : mime,
[336] Fix | Delete
ts : file.ts,
[337] Fix | Delete
size : size,
[338] Fix | Delete
read : file.read,
[339] Fix | Delete
write : file.write,
[340] Fix | Delete
locked : !phash ? true : file.rm === void(0) ? false : !file.rm
[341] Fix | Delete
};
[342] Fix | Delete
[343] Fix | Delete
if (! info.ts) {
[344] Fix | Delete
if (mcts && !isNaN(mcts)) {
[345] Fix | Delete
info.ts = Math.floor(mcts / 1000);
[346] Fix | Delete
} else {
[347] Fix | Delete
info.date = file.date || this.fm.formatDate(file);
[348] Fix | Delete
}
[349] Fix | Delete
}
[350] Fix | Delete
[351] Fix | Delete
if (file.mime == 'application/x-empty' || file.mime == 'inode/x-empty') {
[352] Fix | Delete
info.mime = 'text/plain';
[353] Fix | Delete
}
[354] Fix | Delete
[355] Fix | Delete
if (file.linkTo) {
[356] Fix | Delete
info.alias = file.linkTo;
[357] Fix | Delete
}
[358] Fix | Delete
[359] Fix | Delete
if (file.linkTo) {
[360] Fix | Delete
info.linkTo = file.linkTo;
[361] Fix | Delete
}
[362] Fix | Delete
[363] Fix | Delete
if (file.tmb) {
[364] Fix | Delete
info.tmb = file.tmb;
[365] Fix | Delete
} else if (info.mime.indexOf('image/') === 0 && tmb) {
[366] Fix | Delete
info.tmb = 1;
[367] Fix | Delete
[368] Fix | Delete
}
[369] Fix | Delete
[370] Fix | Delete
if (file.dirs && file.dirs.length) {
[371] Fix | Delete
info.dirs = true;
[372] Fix | Delete
}
[373] Fix | Delete
if (file.dim) {
[374] Fix | Delete
info.dim = file.dim;
[375] Fix | Delete
}
[376] Fix | Delete
if (file.resize) {
[377] Fix | Delete
info.resize = file.resize;
[378] Fix | Delete
}
[379] Fix | Delete
return info;
[380] Fix | Delete
};
[381] Fix | Delete
[382] Fix | Delete
this.normalizeOptions = function(data) {
[383] Fix | Delete
var opts = {
[384] Fix | Delete
path : data.cwd.rel,
[385] Fix | Delete
disabled : $.merge((data.disabled || []), [ 'search', 'netmount', 'zipdl' ]),
[386] Fix | Delete
tmb : !!data.tmb,
[387] Fix | Delete
copyOverwrite : true
[388] Fix | Delete
};
[389] Fix | Delete
[390] Fix | Delete
if (data.params) {
[391] Fix | Delete
opts.api = 1;
[392] Fix | Delete
opts.url = data.params.url;
[393] Fix | Delete
opts.archivers = {
[394] Fix | Delete
create : data.params.archives || [],
[395] Fix | Delete
extract : data.params.extract || []
[396] Fix | Delete
};
[397] Fix | Delete
}
[398] Fix | Delete
[399] Fix | Delete
if (opts.path.indexOf('/') !== -1) {
[400] Fix | Delete
opts.separator = '/';
[401] Fix | Delete
} else if (opts.path.indexOf('\\') !== -1) {
[402] Fix | Delete
opts.separator = '\\';
[403] Fix | Delete
}
[404] Fix | Delete
return opts;
[405] Fix | Delete
};
[406] Fix | Delete
};
[407] Fix | Delete
[408] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function