From 965bdf733dca4a2ad9b87d86b928f128473182dc Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Fri, 17 Apr 2020 13:10:18 +0200 Subject: [PATCH] FIX Can overwrite file if exists in file manager --- htdocs/core/actions_linkedfiles.inc.php | 7 ++++--- htdocs/core/class/html.formfile.class.php | 8 +++++--- htdocs/core/lib/files.lib.php | 10 +++++++--- htdocs/langs/en_US/link.lang | 1 + 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/htdocs/core/actions_linkedfiles.inc.php b/htdocs/core/actions_linkedfiles.inc.php index f8ac8ab0be0..2542cf23268 100644 --- a/htdocs/core/actions_linkedfiles.inc.php +++ b/htdocs/core/actions_linkedfiles.inc.php @@ -52,15 +52,16 @@ if (GETPOST('sendit', 'alpha') && !empty($conf->global->MAIN_UPLOAD_DOC)) { // Define if we have to generate thumbs or not $generatethumbs = 1; - if (GETPOST('section_dir')) $generatethumbs = 0; + if (GETPOST('section_dir', 'alpha')) $generatethumbs = 0; + $allowoverwrite = (GETPOST('overwritefile', 'int') ? 1 : 0); if (!empty($upload_dirold) && !empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) { - $result = dol_add_file_process($upload_dirold, 0, 1, 'userfile', GETPOST('savingdocmask', 'alpha'), null, '', $generatethumbs); + $result = dol_add_file_process($upload_dirold, $allowoverwrite, 1, 'userfile', GETPOST('savingdocmask', 'alpha'), null, '', $generatethumbs); } elseif (!empty($upload_dir)) { - $result = dol_add_file_process($upload_dir, 0, 1, 'userfile', GETPOST('savingdocmask', 'alpha'), null, '', $generatethumbs); + $result = dol_add_file_process($upload_dir, $allowoverwrite, 1, 'userfile', GETPOST('savingdocmask', 'alpha'), null, '', $generatethumbs); } } } diff --git a/htdocs/core/class/html.formfile.class.php b/htdocs/core/class/html.formfile.class.php index 52e852188d6..4a50525c88a 100644 --- a/htdocs/core/class/html.formfile.class.php +++ b/htdocs/core/class/html.formfile.class.php @@ -76,7 +76,7 @@ class FormFile * @param integer $linkfiles 1=Also add form to link files, 0=Do not show form to link files * @param string $htmlname Name and id of HTML form ('formuserfile' by default, 'formuserfileecm' when used to upload a file in ECM) * @param string $accept Specifies the types of files accepted (This is not a security check but an user interface facility. eg '.pdf,image/*' or '.png,.jpg' or 'video/*') - * @param string $sectiondir If upload must be done inside a particular directory (is sectiondir defined, sectionid must not be) + * @param string $sectiondir If upload must be done inside a particular directory (if sectiondir defined, sectionid must not be) * @param int $usewithoutform 0=Default, 1=Disable
and style to use in existing area * @return int <0 if KO, >0 if OK */ @@ -108,8 +108,6 @@ class FormFile return 1; } - $maxlength = $size; - $out = "\n\n".'
'."\n"; if (empty($title)) $title = $langs->trans("AttachANewFile"); @@ -174,6 +172,10 @@ class FormFile $out .= (!empty($accept) ? ' accept="'.$accept.'"' : ' accept=""'); $out .= '>'; $out .= ' '; + if ($sectionid) { // Show overwrite if exists for ECM module only + $langs->load('link'); + $out .= ''; + } $out .= 'global->MAIN_UPLOAD_DOC) || empty($perm) ? ' disabled' : ''); $out .= '>'; diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php index a39e48e3a60..9746c1047b8 100644 --- a/htdocs/core/lib/files.lib.php +++ b/htdocs/core/lib/files.lib.php @@ -1598,13 +1598,18 @@ function dol_add_file_process($upload_dir, $allowoverwrite = 0, $donotupdatesess $formmail->add_attached_files($destfull, $destfile, $TFile['type'][$i]); } - // Update table of files + // Update index table of files (llx_ecm_files) if ($donotupdatesession == 1) { $result = addFileIntoDatabaseIndex($upload_dir, basename($destfile), $TFile['name'][$i], 'uploaded', 0); if ($result < 0) { - setEventMessages('FailedToAddFileIntoDatabaseIndex', '', 'warnings'); + if ($allowoverwrite) { + // Do not show error message. We can have an error due to DB_ERROR_RECORD_ALREADY_EXISTS + } + else { + setEventMessages('FailedToAddFileIntoDatabaseIndex', '', 'warnings'); + } } } @@ -1760,7 +1765,6 @@ function addFileIntoDatabaseIndex($dir, $file, $fullpathorig = '', $mode = 'uplo return $result; } - /** * Delete files into database index using search criterias. * diff --git a/htdocs/langs/en_US/link.lang b/htdocs/langs/en_US/link.lang index fdcf07aeff4..1ffcd41a18b 100644 --- a/htdocs/langs/en_US/link.lang +++ b/htdocs/langs/en_US/link.lang @@ -8,3 +8,4 @@ LinkRemoved=The link %s has been removed ErrorFailedToDeleteLink= Failed to remove link '%s' ErrorFailedToUpdateLink= Failed to update link '%s' URLToLink=URL to link +OverwriteIfExists=Overwrite file if exists