Skip to content

Commit d463e3f

Browse files
Add check that the file exists when uploading it (#15264)
* Added check that file exists when uploading it * grunt build Co-authored-by: Jason Coward <[email protected]>
1 parent c3847ff commit d463e3f

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

_build/data/transport.core.system_settings.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,6 +1748,15 @@
17481748
'area' => 'file',
17491749
'editedon' => null,
17501750
], '', true, true);
1751+
$settings['upload_file_exists']= $xpdo->newObject('modSystemSetting');
1752+
$settings['upload_file_exists']->fromArray([
1753+
'key' => 'upload_file_exists',
1754+
'value' => true,
1755+
'xtype' => 'combo-boolean',
1756+
'namespace' => 'core',
1757+
'area' => 'file',
1758+
'editedon' => null,
1759+
], '', true, true);
17511760
$settings['upload_images']= $xpdo->newObject(modSystemSetting::class);
17521761
$settings['upload_images']->fromArray([
17531762
'key' => 'upload_images',

core/lexicon/en/setting.inc.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,9 @@
723723
$_lang['setting_upload_files'] = 'Uploadable File Types';
724724
$_lang['setting_upload_files_desc'] = 'Here you can enter a list of files that can be uploaded into \'assets/files/\' using the Resource Manager. Please enter the extensions for the filetypes, seperated by commas.';
725725

726+
$_lang['setting_upload_file_exists'] = 'Check if uploaded file exists';
727+
$_lang['setting_upload_file_exists_desc'] = 'When enabled an error will be shown when uploading a file that already exists with the same name. When disabled, the existing file will be quietly replaced with the new file.';
728+
726729
$_lang['setting_upload_images'] = 'Uploadable Image Types';
727730
$_lang['setting_upload_images_desc'] = 'Here you can enter a list of files that can be uploaded into \'assets/images/\' using the Resource Manager. Please enter the extensions for the image types, separated by commas.';
728731

core/src/Revolution/Sources/modMediaSource.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,17 @@ public function createContainer($name, $parentContainer)
632632
}
633633

634634

635+
/**
636+
* Checks `upload_file_exists` system setting to allow or disallow overwriting files with the same name
637+
*
638+
* @return boolean
639+
*/
640+
public function checkFileExists()
641+
{
642+
return (bool)$this->xpdo->getOption('upload_file_exists', null, true);
643+
}
644+
645+
635646
/**
636647
* Create a file
637648
*
@@ -655,7 +666,7 @@ public function createObject($path, $name, $content)
655666
}
656667

657668
try {
658-
if ($this->filesystem->has($path)) {
669+
if ($this->checkFileExists() && $this->filesystem->has($path)) {
659670
$this->addError('name', sprintf($this->xpdo->lexicon('file_err_ae'), $name));
660671

661672
return false;
@@ -945,7 +956,7 @@ public function renameObject($oldPath, $newName)
945956
$this->addError('name', $this->xpdo->lexicon('file_err_invalid'));
946957

947958
return false;
948-
} elseif ($this->filesystem->has($newPath)) {
959+
} elseif ($this->checkFileExists() && $this->filesystem->has($newPath)) {
949960
$this->addError('name', sprintf($this->xpdo->lexicon('file_err_ae'), $newName));
950961

951962
return false;
@@ -1076,6 +1087,11 @@ public function uploadObjectsToContainer($container, array $objects = [])
10761087

10771088
$newPath = $container . $this->sanitizePath($file['name']);
10781089
try {
1090+
if ($this->checkFileExists() && $this->filesystem->has($newPath)) {
1091+
$this->addError('path', sprintf($this->xpdo->lexicon('file_err_ae'), $file['name']));
1092+
1093+
return false;
1094+
}
10791095
if (!$this->filesystem->put($newPath, file_get_contents($file['tmp_name']))) {
10801096
$this->addError('path', $this->xpdo->lexicon('file_err_upload'));
10811097
continue;

manager/assets/modext/modx.jsgrps-min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manager/assets/modext/util/multiuploaddialog.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
var errors = '';
187187
for (var i in this.errors) {
188188
if (this.errors.hasOwnProperty(i)) {
189-
errors += '- <b>' + i + '</b>: ' + this.errors[i] + '<br>';
189+
errors += this.errors[i] + '<br>';
190190
}
191191
}
192192
if (errors != '') {

0 commit comments

Comments
 (0)