From e511291228fc47b8b978633eae2ba3d0b317f77a Mon Sep 17 00:00:00 2001 From: Anthony Berton Date: Wed, 3 Aug 2022 17:04:34 +0200 Subject: [PATCH 1/3] init function --- htdocs/core/lib/ftp.lib.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/htdocs/core/lib/ftp.lib.php b/htdocs/core/lib/ftp.lib.php index 42f5806fba5..52ff8263d07 100644 --- a/htdocs/core/lib/ftp.lib.php +++ b/htdocs/core/lib/ftp.lib.php @@ -246,3 +246,33 @@ function dol_ftp_rmdir($connect_id, $file, $newsection) return @ftp_rmdir($connect_id, $newremotefileiso); } } + + +/** + * Remove FTP directory + * + * @param resource $connect_id Connection handler + * @param string $newdir Dir create + * @param string $newsection $newsection + * @return result + */ +function dol_ftp_mkdir($connect_id, $newdir, $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 ssh2_sftp_mkdir($connect_id, $newremotefileiso); + } else { + return @ftp_mkdir($connect_id, $newremotefileiso); + } +} From 9955e51956fc5bc5bc5c9a2b520009fb7c2d12c1 Mon Sep 17 00:00:00 2001 From: Anthony Berton Date: Fri, 5 Aug 2022 09:39:31 +0200 Subject: [PATCH 2/3] NEW - FTP Function mkdir --- htdocs/core/lib/ftp.lib.php | 5 ++--- htdocs/ftp/index.php | 40 +++++++++++++++++++++++++++++++---- htdocs/langs/en_US/other.lang | 3 +++ 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/htdocs/core/lib/ftp.lib.php b/htdocs/core/lib/ftp.lib.php index 4a9f5a894d4..361904e3bd1 100644 --- a/htdocs/core/lib/ftp.lib.php +++ b/htdocs/core/lib/ftp.lib.php @@ -296,9 +296,8 @@ function dol_ftp_mkdir($connect_id, $newdir, $newsection) } // Remote file - $filename = $file; - $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file; - $newremotefileiso = utf8_decode($remotefile); + $newremotefileiso = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$newdir; + $newremotefileiso = utf8_decode($newremotefileiso); if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { return ssh2_sftp_mkdir($connect_id, $newremotefileiso); diff --git a/htdocs/ftp/index.php b/htdocs/ftp/index.php index 4df7e1512a6..20389ade79e 100644 --- a/htdocs/ftp/index.php +++ b/htdocs/ftp/index.php @@ -41,6 +41,7 @@ $result = restrictedArea($user, 'ftp', ''); // Get parameters $action = GETPOST('action', 'aZ09'); $section = GETPOST('section'); +$newfolder = GETPOST('newfolder'); if (!$section) { $section = '/'; } @@ -141,11 +142,8 @@ if ($action == 'uploadfile') { $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); + for ($i = 0; $i < $nbfile; $i++) { $newsection = $newsectioniso; $fileupload = $_FILES['userfile']['name'][$i]; $fileuploadpath = $_FILES['userfile']['tmp_name'][$i]; @@ -164,6 +162,30 @@ if ($action == 'uploadfile') { } } +if ($action == 'addfolder') { + // 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) { + $result = dol_ftp_mkdir($conn_id, $newfolder, $newsectioniso); + + if ($result) { + setEventMessages($langs->trans("FileWasCreateFolder", $newfolder), null, 'mesgs'); + } else { + dol_syslog("ftp/index.php ftp_delete", LOG_ERR); + setEventMessages($langs->trans("FTPFailedToCreateFolder", $newfolder), null, 'errors'); + } + $action = ''; + } else { + dol_print_error('', $mesg); + } +} + // Action ajout d'un rep if ($action == 'add' && $user->rights->ftp->setup) { $ecmdir->ref = GETPOST("ref"); @@ -633,6 +655,16 @@ if (!function_exists('ftp_connect')) { print ''; print ''; print ''; + print load_fiche_titre($langs->trans("AddFolder"), null, null); + print '
'; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print '
'; } } else { $foundsetup = false; diff --git a/htdocs/langs/en_US/other.lang b/htdocs/langs/en_US/other.lang index daa52aac9fe..8be37bd0f9a 100644 --- a/htdocs/langs/en_US/other.lang +++ b/htdocs/langs/en_US/other.lang @@ -328,3 +328,6 @@ FailedToGetFile=Failed to get files %s ErrorFTPNodisconnect=Error to disconnect FTP/SFTP server FileWasUpload=File %s was upload FTPFailedToUploadFile=Failed to upload file %s. +AddFolder=Create folder +FileWasCreateFolder=Folder %s was create +FTPFailedToCreateFolder=Failed to create folder %s. From 17483eb08222c6d50a04cc0e9f57352cb84d2449 Mon Sep 17 00:00:00 2001 From: Anthony Berton Date: Fri, 5 Aug 2022 10:47:00 +0200 Subject: [PATCH 3/3] Fix --- htdocs/core/lib/ftp.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/lib/ftp.lib.php b/htdocs/core/lib/ftp.lib.php index 361904e3bd1..118c85e9b74 100644 --- a/htdocs/core/lib/ftp.lib.php +++ b/htdocs/core/lib/ftp.lib.php @@ -300,7 +300,7 @@ function dol_ftp_mkdir($connect_id, $newdir, $newsection) $newremotefileiso = utf8_decode($newremotefileiso); if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { - return ssh2_sftp_mkdir($connect_id, $newremotefileiso); + return ssh2_sftp_mkdir($connect_id, $newremotefileiso, 0777); } else { return @ftp_mkdir($connect_id, $newremotefileiso); }