* elFinder transport to support old protocol.
* $('selector').elfinder({
* transport : new elFinderSupportVer1()
* @author Dmitry (dio) Levashov
window.elFinderSupportVer1 = function(upload) {
dateObj, today, yesterday,
getDateString = function(date) {
return date.replace('Today', today).replace('Yesterday', yesterday);
today = dateObj.getFullYear() + '/' + (dateObj.getMonth() + 1) + '/' + dateObj.getDate();
dateObj = new Date(Date.now() - 86400000);
yesterday = dateObj.getFullYear() + '/' + (dateObj.getMonth() + 1) + '/' + dateObj.getDate();
this.upload = upload || 'auto';
this.init = function(fm) {
this.fm.parseUploadData = function(text) {
return {error : ['errResponse', 'errDataEmpty']};
return {error : ['errResponse', 'errDataNotJSON']};
return self.normalize('upload', data);
this.send = function(opts) {
dfrd.abort = function() {
if (xhr.state() == 'pending') {
return dfrd.resolve({tree : []});
opts.data.current = fm.file(opts.data.target).phash;
opts.data.current = fm.file(opts.data.target).phash;
opts.data.current = fm.file(opts.data.targets[0]).phash;
opts.data.current = fm.file(opts.data.target).phash;
_opts = $.extend(true, {}, opts);
$.each(opts.data.targets, function(i, hash) {
$.ajax(Object.assign(_opts, {data : {cmd : 'duplicate', target : hash, current : fm.file(hash).phash}}))
fm.error(fm.res('error', 'connect'));
data = self.normalize('duplicate', data);
fm.trigger('add', {added : data.added});
opts.data.current = opts.data.target;
opts.data.current = opts.data.dst;
$.each(opts.data.targets, function(i, h) {
if (fm.file(h) && fm.file(h).mime === 'directory') {
return dfrd.resolve({error : fm.res('error', 'cmdsupport')});
return dfrd.resolve({error : fm.res('error', 'cmdsupport')});
opts.data.current = fm.file(opts.data.target).phash;
data = self.normalize(cmd, raw);
// fix old connectors errors messages as possible
// 'Unknown command' : 'Unknown command.',
// 'Invalid backend configuration' : 'Invalid backend configuration.',
// 'Access denied' : 'Access denied.',
// 'PHP JSON module not installed' : 'PHP JSON module not installed.',
// 'File not found' : 'File not found.',
// 'Invalid name' : 'Invalid file name.',
// 'File or folder with the same name already exists' : 'File named "$1" already exists in this location.',
// 'Not allowed file type' : 'Not allowed file type.',
// 'File exceeds the maximum allowed filesize' : 'File exceeds maximum allowed size.',
// 'Unable to copy into itself' : 'Unable to copy "$1" into itself.',
// 'Unable to create archive' : 'Unable to create archive.',
// 'Unable to extract files from archive' : 'Unable to extract files from "$1".'
this.normalize = function(cmd, data) {
filter = function(file) { return file && file.hash && file.name && file.mime ? file : null; },
getDirs = function(items) {
return $.grep(items, function(i) {
return i && i.mime && i.mime === 'directory'? true : false;
getTreeDiff = function(files) {
var dirs = getDirs(files);
treeDiff = fm.diff(dirs, null, ['date', 'ts']);
if (treeDiff.added.length) {
treeDiff.added = getDirs(treeDiff.added);
if (treeDiff.changed.length) {
treeDiff.changed = getDirs(treeDiff.changed);
if (treeDiff.removed.length) {
$.each(treeDiff.removed, function(i, h) {
if ((item = fm.file(h)) && item.mime === 'directory') {
treeDiff.removed = removed;
phash, diff, isCwd, treeDiff;
if ((cmd == 'tmb' || cmd == 'get')) {
// $.each(data.error, function(i, msg) {
// if (self.errors[msg]) {
// data.error[i] = self.errors[msg];
if (cmd == 'upload' && data.error && data.cwd) {
data.warning = Object.assign({}, data.error);
phash = fm.file(data.target.hash).phash;
return {changed : [this.normalizeFile(data.target, phash)]};
isCwd = (phash == fm.cwd().hash);
$.each(this.normalizeTree(data.tree), function(i, file) {
$.each(data.cdc||[], function(i, file) {
mcts = Date.parse(getDateString(file.date));
if (mcts && !isNaN(mcts)) {
files[hash].ts = Math.floor(mcts / 1000);
files[hash].date = file.date || fm.formatDate(file);
files[hash].locked = file.hash == phash ? true : file.rm === void(0) ? false : !file.rm;
files[hash] = self.normalizeFile(file, phash, data.tmb);
$.each(fm.files(), function(hash, file) {
if (!files[hash] && file.phash != phash && file.mime == 'directory') {
cwd : files[phash] || this.normalizeFile(data.cwd),
files : $.map(files, function(f) { return f; }),
options : self.normalizeOptions(data),
diff = fm.diff($.map(files, filter));
if (data.tree && cmd !== 'paste') {
diff = getTreeDiff(files);
options : {tmb : !!data.tmb}
* Convert old api tree into plain array of dirs
this.normalizeTree = function(root) {
traverse = function(dirs, phash) {
for (i = 0; i < dirs.length; i++) {
result.push(self.normalizeFile(dir, phash));
dir.dirs.length && traverse(dir.dirs, dir.hash);
* Convert file info from old api format into new one
* @param String parent dir hash
this.normalizeFile = function(file, phash, tmb) {
var mime = file.mime || 'directory',
size = mime == 'directory' && !file.linkTo ? 0 : file.size,
mcts = file.date? Date.parse(getDateString(file.date)) : void 0,
locked : !phash ? true : file.rm === void(0) ? false : !file.rm
if (mcts && !isNaN(mcts)) {
info.ts = Math.floor(mcts / 1000);
info.date = file.date || this.fm.formatDate(file);
if (file.mime == 'application/x-empty' || file.mime == 'inode/x-empty') {
info.mime = 'text/plain';
info.alias = file.linkTo;
info.linkTo = file.linkTo;
} else if (info.mime.indexOf('image/') === 0 && tmb) {
if (file.dirs && file.dirs.length) {
info.resize = file.resize;
this.normalizeOptions = function(data) {
disabled : $.merge((data.disabled || []), [ 'search', 'netmount', 'zipdl' ]),
opts.url = data.params.url;
create : data.params.archives || [],
extract : data.params.extract || []
if (opts.path.indexOf('/') !== -1) {
} else if (opts.path.indexOf('\\') !== -1) {