Skip to content

Commit

Permalink
[core] fix #1706 configurable to limit max number of selectable items
Browse files Browse the repository at this point in the history
Added an option `maxTargets` of connector main option.
  • Loading branch information
nao-pon committed Oct 17, 2016
1 parent 4b0dea6 commit 99ff869
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 30 deletions.
49 changes: 36 additions & 13 deletions js/elFinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,10 @@ window.elFinder = function(node, opts) {
self.netDrivers = response.netDrivers;
}

if (response.maxTargets) {
self.maxTargets = response.maxTargets;
}

if (isOpen && !!data.init) {
self.uplMaxSize = self.returnBytes(response.uplMaxSize);
self.uplMaxFile = !!response.uplMaxFile? parseInt(response.uplMaxFile) : 20;
Expand Down Expand Up @@ -1575,11 +1579,6 @@ window.elFinder = function(node, opts) {
}
};

if (!cmd) {
return dfrd.reject('errCmdReq');
}

defdone && dfrd.done(done);
dfrd.fail(function(error, xhr, response) {
self.trigger(cmd + 'fail', response);
if (error) {
Expand All @@ -1588,6 +1587,17 @@ window.elFinder = function(node, opts) {
syncOnFail && self.sync();
})

if (!cmd) {
syncOnFail = false;
return dfrd.reject('errCmdReq');
}

if (self.maxTargets && data.targets && data.targets.length > self.maxTargets) {
syncOnFail = false;
return dfrd.reject(['errMaxTargets', self.maxTargets]);
}

defdone && dfrd.done(done);
if (notify.type && notify.cnt) {
if (cancel) {
notify.cancel = dfrd;
Expand Down Expand Up @@ -2045,7 +2055,7 @@ window.elFinder = function(node, opts) {
* @return Array
*/
this.clipboard = function(hashes, cut) {
var map = function() { return $.map(clipboard, function(f) { return f.hash }); }
var map = function() { return $.map(clipboard, function(f) { return f.hash }); };

if (hashes !== void(0)) {
clipboard.length && this.trigger('unlockfiles', {files : map()});
Expand Down Expand Up @@ -2750,7 +2760,20 @@ window.elFinder = function(node, opts) {
selected = [];
})
.select(function(e) {
selected = $.map(e.data.selected || e.data.value|| [], function(hash) { return files[hash] ? hash : null; });
var cnt = 0,
unselects = [];
selected = $.map(e.data.selected || e.data.value|| [], function(hash) {
if (unselects.length || (self.maxTargets && ++cnt > self.maxTargets)) {
unselects.push(hash);
return null;
} else {
return files[hash] ? hash : null;
}
});
if (unselects.length) {
self.trigger('unselectfiles', {files: unselects, inselect: true});
self.error(['errMaxTargets', self.maxTargets]);
}
})
.error(function(e) {
var opts = {
Expand Down Expand Up @@ -5625,20 +5648,20 @@ elFinder.prototype = {
for (i = start; i< arguments.length; i++) {
m = arguments[i];

if (typeof m == 'string') {
input.push(message(m));
} else if ($.isArray(m)) {
if ($.isArray(m)) {
for (j = 0; j < m.length; j++) {
if (typeof m[j] == 'string') {
input.push(message(m[j]));
} else if (m[j] instanceof jQuery) {
if (m[j] instanceof jQuery) {
// jQuery object is HTML element
input.push(m[j]);
} else if (typeof m[j] !== 'undefined'){
input.push(message('' + m[j]));
}
}
} else if (m instanceof jQuery) {
// jQuery object is HTML element
input.push(m[j]);
} else if (typeof m !== 'undefined'){
input.push(message('' + m));
}
}

Expand Down
3 changes: 2 additions & 1 deletion js/i18n/elfinder.LANG.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ if (elFinder && elFinder.prototype && typeof(elFinder.prototype.i18) == 'object'
'errConvUTF8' : 'Not convertible to UTF-8', // from v2.1 added 08.04.2014
'errFolderUpload' : 'Try Google Chrome, If you\'d like to upload the folder.', // from v2.1 added 26.6.2015
'errSearchTimeout' : 'Timed out while searching "$1". Search result is partial.', // from v2.1 added 12.1.2016
'errReauthRequire' : 'Re-authorization is required.', // from v2.1.10 added 3.24.2016
'errReauthRequire' : 'Re-authorization is required.', // from v2.1.10 added 24.3.2016
'errMaxTargets' : 'Max number of selectable items is $1.', // from v2.1.17 added 17.10.2016

/******************************* commands names ********************************/
'cmdarchive' : 'Create archive',
Expand Down
3 changes: 2 additions & 1 deletion js/i18n/elfinder.en.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ if (elFinder && elFinder.prototype && typeof(elFinder.prototype.i18) == 'object'
'errConvUTF8' : 'Not convertible to UTF-8', // from v2.1 added 08.04.2014
'errFolderUpload' : 'Try Google Chrome, If you\'d like to upload the folder.', // from v2.1 added 26.6.2015
'errSearchTimeout' : 'Timed out while searching "$1". Search result is partial.', // from v2.1 added 12.1.2016
'errReauthRequire' : 'Re-authorization is required.', // from v2.1.10 added 3.24.2016
'errReauthRequire' : 'Re-authorization is required.', // from v2.1.10 added 24.3.2016
'errMaxTargets' : 'Max number of selectable items is $1.', // from v2.1.17 added 17.10.2016

/******************************* commands names ********************************/
'cmdarchive' : 'Create archive',
Expand Down
3 changes: 2 additions & 1 deletion js/i18n/elfinder.jp.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ if (elFinder && elFinder.prototype && typeof(elFinder.prototype.i18) == 'object'
'errConvUTF8' : 'UTF-8 に変換できませんでした', // from v2.1 added 08.04.2014
'errFolderUpload' : 'フォルダをアップロードしたいのであれば、Google Chrome を使用してください', // from v2.1 added 26.6.2015
'errSearchTimeout' : '"$1"を検索中にタイムアウトしました。検索結果は部分的です。', // from v2.1 added 12.1.2016
'errReauthRequire' : '再認可が必要です', // from v2.1.10 added 3.24.2016
'errReauthRequire' : '再認可が必要です', // from v2.1.10 added 24.3.2016
'errMaxTargets' : '選択可能な最大アイテム数は $1 個です', // from v2.1.17 added 17.10.2016

/******************************* commands names ********************************/
'cmdarchive' : 'アーカイブ作成',
Expand Down
13 changes: 10 additions & 3 deletions js/ui/cwd.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,15 @@ $.fn.elfindercwd = function(fm, options) {

selectCheckbox && selectAllCheckbox.find('input').prop('checked', true);
fm.lazy(function() {
var files;
cwd.find('[id]:not(.'+clSelected+'):not(.elfinder-cwd-parent)').trigger(evtSelect);
selectedFiles = (incHashes || cwdHashes).concat();
if (fm.maxTargets && (incHashes || cwdHashes).length > fm.maxTargets) {
files = $.map(incHashes || cwdHashes, function(hash) { return fm.file(hash) || null });
files = fm.sortFiles(files);
selectedFiles = $.map(files, function(f) { return f.hash; });
} else {
selectedFiles = (incHashes || cwdHashes).concat();
}
trigger();
selectCheckbox && selectAllCheckbox.data('pending', false);
cwd.addClass('elfinder-cwd-allselected');
Expand Down Expand Up @@ -2153,7 +2160,7 @@ $.fn.elfindercwd = function(fm, options) {
}
if (event === evtSelect || event === evtUnselect) {
add = (event === evtSelect),
sels = selectedFiles.concat();
sels = add? selectedFiles.concat() : selectedFiles;
$.each(files, function(i, hash) {
var idx = $.inArray(hash, sels);
if (idx === -1) {
Expand All @@ -2167,7 +2174,7 @@ $.fn.elfindercwd = function(fm, options) {
while (l--) {
$('#'+fm.cwdHash2Id(files[l])).trigger(event);
}
trigger();
! e.data.inselect && trigger();
}
if (wrapper.data('dropover') && parents.indexOf(wrapper.data('dropover')) !== -1) {
ctr = e.type !== 'lockfiles';
Expand Down
30 changes: 19 additions & 11 deletions php/elFinder.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ class elFinder {
**/
protected $uploadDebug = '';

/**
* Max allowed numbar of @var targets (0 - no limit)
*
* @var integer
*/
public $maxTargets = 1000;

/**
* Errors from PHP
*
Expand Down Expand Up @@ -302,18 +309,17 @@ class elFinder {
const ERROR_NETMOUNT = 'errNetMount';
const ERROR_NETUNMOUNT = 'errNetUnMount';
const ERROR_NETMOUNT_NO_DRIVER = 'errNetMountNoDriver';
const ERROR_NETMOUNT_FAILED = 'errNetMountFailed';

const ERROR_SESSION_EXPIRES = 'errSessionExpires';

const ERROR_CREATING_TEMP_DIR = 'errCreatingTempDir';
const ERROR_FTP_DOWNLOAD_FILE = 'errFtpDownloadFile';
const ERROR_FTP_UPLOAD_FILE = 'errFtpUploadFile';
const ERROR_FTP_MKDIR = 'errFtpMkdir';
const ERROR_ARCHIVE_EXEC = 'errArchiveExec';
const ERROR_EXTRACT_EXEC = 'errExtractExec';
const ERROR_NETMOUNT_FAILED = 'errNetMountFailed';
const ERROR_SESSION_EXPIRES = 'errSessionExpires';
const ERROR_CREATING_TEMP_DIR = 'errCreatingTempDir';
const ERROR_FTP_DOWNLOAD_FILE = 'errFtpDownloadFile';
const ERROR_FTP_UPLOAD_FILE = 'errFtpUploadFile';
const ERROR_FTP_MKDIR = 'errFtpMkdir';
const ERROR_ARCHIVE_EXEC = 'errArchiveExec';
const ERROR_EXTRACT_EXEC = 'errExtractExec';
const ERROR_SEARCH_TIMEOUT = 'errSearchTimeout'; // 'Timed out while searching "$1". Search result is partial.'
const ERROR_REAUTH_REQUIRE = 'errReauthRequire'; // 'Re-authorization is required.'
const ERROR_REAUTH_REQUIRE = 'errReauthRequire'; // 'Re-authorization is required.'
const ERROR_MAX_TARGTES = 'errMaxTargets'; // 'Max number of selectable items is $1.'

/**
* Constructor
Expand Down Expand Up @@ -371,6 +377,7 @@ public function __construct($opts) {
$this->timeout = (isset($opts['timeout']) ? $opts['timeout'] : 0);
$this->uploadTempPath = (isset($opts['uploadTempPath']) ? $opts['uploadTempPath'] : '');
$this->callbackWindowURL = (isset($opts['callbackWindowURL']) ? $opts['callbackWindowURL'] : '');
$this->maxTargets = (isset($opts['maxTargets']) ? intval($opts['maxTargets']) : $this->maxTargets);
elFinder::$commonTempPath = (isset($opts['commonTempPath']) ? $opts['commonTempPath'] : './.tmp');
if (!is_writable(elFinder::$commonTempPath)) {
elFinder::$commonTempPath = '';
Expand Down Expand Up @@ -1119,6 +1126,7 @@ protected function open($args) {
$result['uplMaxSize'] = ini_get('upload_max_filesize');
$result['uplMaxFile'] = ini_get('max_file_uploads');
$result['netDrivers'] = array_keys(self::$netDrivers);
$result['maxTargets'] = $this->maxTargets;
if ($volume) {
$result['cwd']['root'] = $volume->root();
}
Expand Down
6 changes: 6 additions & 0 deletions php/elFinderConnector.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ public function run() {
$_REQUEST = $this->input_filter(array_merge_recursive($src, $_REQUEST));
}
}

if (isset($src['targets']) && $this->elFinder->maxTargets && count($src['targets']) > $this->elFinder->maxTargets) {
$error = $this->elFinder->error(elFinder::ERROR_MAX_TARGTES);
$this->output(array('error' => $this->elFinder->error(elFinder::ERROR_MAX_TARGTES)));
}

$cmd = isset($src['cmd']) ? $src['cmd'] : '';
$args = array();

Expand Down

0 comments on commit 99ff869

Please sign in to comment.