From 07f9541efd0eed00ae5c447970ec97300dc6129e Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 10 Jul 2022 20:12:35 +0200 Subject: [PATCH 1/3] Fix missing migration --- htdocs/core/lib/ftp.lib.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/htdocs/core/lib/ftp.lib.php b/htdocs/core/lib/ftp.lib.php index 42f5806fba5..928a7e4a934 100644 --- a/htdocs/core/lib/ftp.lib.php +++ b/htdocs/core/lib/ftp.lib.php @@ -218,6 +218,35 @@ function dol_ftp_get($connect_id, $file, $newsection) } } +/** + * Upload a FTP file + * + * @param resource $connect_id Connection handler + * @param string $file File + * @param string $newsection $newsection + * @return result + */ +function dol_ftp_put($connect_id, $file, $newsection) +{ + + global $conf; + + if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { + $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169 + } + + // Remote file + $filename = $file; + $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file; + $newremotefileiso = utf8_decode($remotefile); + + if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { + return fopen('ssh2.sftp://'.intval($connect_id).$newremotefileiso, 'r'); + } else { + return ftp_put($connect_id, $localfile, $newremotefileiso, FTP_BINARY); + } +} + /** * Remove FTP directory * From 985d9b3624a8e0b4b9b97bda0558b08a1b88eaa2 Mon Sep 17 00:00:00 2001 From: Anthony Berton Date: Wed, 3 Aug 2022 15:59:53 +0200 Subject: [PATCH 2/3] Function --- htdocs/core/lib/ftp.lib.php | 11 +++++---- htdocs/ftp/index.php | 43 +++++++++++++++++++++++++++++++++++ htdocs/langs/en_US/other.lang | 4 +++- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/htdocs/core/lib/ftp.lib.php b/htdocs/core/lib/ftp.lib.php index 928a7e4a934..3d680de08a0 100644 --- a/htdocs/core/lib/ftp.lib.php +++ b/htdocs/core/lib/ftp.lib.php @@ -222,11 +222,12 @@ function dol_ftp_get($connect_id, $file, $newsection) * Upload a FTP file * * @param resource $connect_id Connection handler - * @param string $file File - * @param string $newsection $newsection + * @param string $file File name + * @param string $localfile The path to the local file + * @param string $newsection $newsection * @return result */ -function dol_ftp_put($connect_id, $file, $newsection) +function dol_ftp_put($connect_id, $file, $localfile, $newsection) { global $conf; @@ -241,9 +242,9 @@ function dol_ftp_put($connect_id, $file, $newsection) $newremotefileiso = utf8_decode($remotefile); if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { - return fopen('ssh2.sftp://'.intval($connect_id).$newremotefileiso, 'r'); + return ssh2_scp_send($connect_id, $localfile, $newremotefileiso, 0644); } else { - return ftp_put($connect_id, $localfile, $newremotefileiso, FTP_BINARY); + return ftp_put($connect_id, $newremotefileiso, $localfile, FTP_BINARY); } } diff --git a/htdocs/ftp/index.php b/htdocs/ftp/index.php index cf647929ac8..ccbc7d6a0df 100644 --- a/htdocs/ftp/index.php +++ b/htdocs/ftp/index.php @@ -131,6 +131,39 @@ if (GETPOST("sendit") && !empty($conf->global->MAIN_UPLOAD_DOC)) { } } +if ($action == 'uploadfile') { + // set up a connection or die + if (!$conn_id) { + $newsectioniso = utf8_decode($section); + $resultarray = dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $newsectioniso, $ftp_passive); + $conn_id = $resultarray['conn_id']; + $ok = $resultarray['ok']; + $mesg = $resultarray['mesg']; + } + if ($conn_id && $ok && !$mesg) { + // var_dump($_FILES['userfile']['name']); + $nbfile = count($_FILES['userfile']['name']); + $i = 0; + for (; $i < $nbfile; $i++) { + var_dump($i); + $newsection = $newsectioniso; + $fileupload = $_FILES['userfile']['name'][$i]; + $fileuploadpath = $_FILES['userfile']['tmp_name'][$i]; + $result = dol_ftp_put($conn_id, $fileupload, $fileuploadpath, $newsection); + + if ($result) { + setEventMessages($langs->trans("FileWasUpload", $fileupload), null, 'mesgs'); + } else { + dol_syslog("ftp/index.php ftp_delete", LOG_ERR); + setEventMessages($langs->trans("FTPFailedToUploadFile", $fileupload), null, 'errors'); + } + } + $action = ''; + } else { + dol_print_error('', $mesg); + } +} + // Action ajout d'un rep if ($action == 'add' && $user->rights->ftp->setup) { $ecmdir->ref = GETPOST("ref"); @@ -589,6 +622,16 @@ if (!function_exists('ftp_connect')) { print ''; print ""; + + print '
'; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print '
'; } else { $foundsetup = false; $MAXFTP = 20; diff --git a/htdocs/langs/en_US/other.lang b/htdocs/langs/en_US/other.lang index 0918bf54ac7..daa52aac9fe 100644 --- a/htdocs/langs/en_US/other.lang +++ b/htdocs/langs/en_US/other.lang @@ -325,4 +325,6 @@ FTPFailedToRemoveDir=Failed to remove directory %s: check permissions and FTPPassiveMode=Passive mode ChooseAFTPEntryIntoMenu=Choose a FTP/SFTP site from the menu... FailedToGetFile=Failed to get files %s -ErrorFTPNodisconnect=Error to disconnect FTP/SFTP server \ No newline at end of file +ErrorFTPNodisconnect=Error to disconnect FTP/SFTP server +FileWasUpload=File %s was upload +FTPFailedToUploadFile=Failed to upload file %s. From 259fe11634dccff7b90466cb0422affc0939cb9e Mon Sep 17 00:00:00 2001 From: Anthony Berton Date: Wed, 3 Aug 2022 16:18:43 +0200 Subject: [PATCH 3/3] Add title --- htdocs/ftp/index.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/htdocs/ftp/index.php b/htdocs/ftp/index.php index ccbc7d6a0df..4df7e1512a6 100644 --- a/htdocs/ftp/index.php +++ b/htdocs/ftp/index.php @@ -622,16 +622,18 @@ if (!function_exists('ftp_connect')) { print ''; print ""; - - print '
'; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print '
'; + if ($user->hasRight('ftp', 'write')) { + print load_fiche_titre($langs->trans("AttachANewFile"), null, null); + print '
'; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print '
'; + } } else { $foundsetup = false; $MAXFTP = 20;