Skip to content

Added a check for the existence of a file when uploading it #15232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions core/model/modx/sources/modfilemediasource.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,25 @@ public function checkFiletype($filename) {
return true;
}

/**
* Check that an object (directory, file) exists
*
* @param string $objectPath The object path to check
* @param string $objectName The object name displayed in the error message
* @return bool
*/
protected function checkObjectExist($objectPath, $objectName) {
if (file_exists($objectPath)) {
if (is_dir($objectPath)) {
$this->addError('name', $this->xpdo->lexicon('file_folder_err_ae'));
return true;
}
$this->addError('name', sprintf($this->xpdo->lexicon('file_err_ae'), $objectName));
return true;
}
return false;
}

/**
* @param string $oldPath
* @param string $newName
Expand Down Expand Up @@ -622,13 +641,7 @@ public function renameObject($oldPath,$newName) {
$newPath = $this->fileHandler->sanitizePath($newName);
$newPath = dirname($oldPath).'/'.$newPath;

/* check to see if the new resource already exists */
if (file_exists($newPath)) {
if (is_dir($newPath)) {
$this->addError('name',$this->xpdo->lexicon('file_folder_err_ae'));
return false;
}
$this->addError('name',sprintf($this->xpdo->lexicon('file_err_ae'),$newName));
if ($this->checkObjectExist($newPath,$newName)) {
return false;
}

Expand Down Expand Up @@ -875,6 +888,10 @@ public function uploadObjectsToContainer($container,array $objects = array()) {
$newPath = $this->fileHandler->sanitizePath($file['name']);
$newPath = $directory->getPath().$newPath;

if ($this->checkObjectExist($newPath,$file['name'])) {
return false;
}

if (!move_uploaded_file($file['tmp_name'],$newPath)) {
$this->addError('path',$this->xpdo->lexicon('file_err_upload'));
continue;
Expand Down