From ce87eea3ec842358de9664a42eb53717a8c78af3 Mon Sep 17 00:00:00 2001 From: Anthony Berton Date: Thu, 28 Jul 2022 18:18:04 +0200 Subject: [PATCH 1/6] ErrorFTPNodisconnect --- htdocs/core/lib/ftp.lib.php | 153 +++++++++++++++++++++++++++++++++ htdocs/ftp/admin/ftpclient.php | 2 +- htdocs/ftp/index.php | 126 ++------------------------- htdocs/langs/en_US/ftp.lang | 15 ++++ htdocs/langs/en_US/other.lang | 15 ---- 5 files changed, 176 insertions(+), 135 deletions(-) create mode 100644 htdocs/core/lib/ftp.lib.php create mode 100644 htdocs/langs/en_US/ftp.lang diff --git a/htdocs/core/lib/ftp.lib.php b/htdocs/core/lib/ftp.lib.php new file mode 100644 index 00000000000..1730a439bcb --- /dev/null +++ b/htdocs/core/lib/ftp.lib.php @@ -0,0 +1,153 @@ + + * Copyright (C) 2022 Anthony Berton + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * or see https://www.gnu.org/ + */ + +/** + * \file htdocs/core/lib/ftp.lib.php + * \brief Set of functions used for FTP + * \ingroup core + */ + + + + +/** + * Connect to FTP server + * + * @param string $ftp_server Server name + * @param string $ftp_port Server port + * @param string $ftp_user FTP user + * @param string $ftp_password FTP password + * @param string $section Directory + * @param integer $ftp_passive Use a passive mode + * @return int <0 if OK, >0 if KO + */ +function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive = 0) +{ + global $langs, $conf; + + $ok = 1; + $error = 0; + $conn_id = null; + $newsectioniso = ''; + $mesg=""; + + if (!is_numeric($ftp_port)) { + $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port); + $ok = 0; + } + + if ($ok) { + $connecttimeout = (empty($conf->global->FTP_CONNECT_TIMEOUT) ? 40 : $conf->global->FTP_CONNECT_TIMEOUT); + if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { + dol_syslog('Try to connect with ssh2_connect'); + $tmp_conn_id = ssh2_connect($ftp_server, $ftp_port); + } elseif (!empty($conf->global->FTP_CONNECT_WITH_SSL)) { + dol_syslog('Try to connect with ftp_ssl_connect'); + $conn_id = ftp_ssl_connect($ftp_server, $ftp_port, $connecttimeout); + } else { + dol_syslog('Try to connect with ftp_connect'); + $conn_id = ftp_connect($ftp_server, $ftp_port, $connecttimeout); + } + if (!empty($conn_id) || !empty($tmp_conn_id)) { + if ($ftp_user) { + if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { + dol_syslog('Try to authenticate with ssh2_auth_password'); + if (ssh2_auth_password($tmp_conn_id, $ftp_user, $ftp_password)) { + // Turn on passive mode transfers (must be after a successful login + //if ($ftp_passive) ftp_pasv($conn_id, true); + + // Change the dir + $newsectioniso = utf8_decode($section); + //ftp_chdir($conn_id, $newsectioniso); + $conn_id = ssh2_sftp($tmp_conn_id); + if (!$conn_id) { + dol_syslog('Failed to connect to SFTP after sssh authentication', LOG_DEBUG); + $mesg = $langs->transnoentitiesnoconv("FailedToConnectToSFTPAfterSSHAuthentication"); + $ok = 0; + $error++; + } + } else { + dol_syslog('Failed to connect to FTP with login '.$ftp_user, LOG_DEBUG); + $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials"); + $ok = 0; + $error++; + } + } else { + if (ftp_login($conn_id, $ftp_user, $ftp_password)) { + // Turn on passive mode transfers (must be after a successful login + if ($ftp_passive) { + ftp_pasv($conn_id, true); + } + + // Change the dir + $newsectioniso = utf8_decode($section); + ftp_chdir($conn_id, $newsectioniso); + } else { + $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials"); + $ok = 0; + $error++; + } + } + } + } else { + dol_syslog('FailedToConnectToFTPServer '.$ftp_server.' '.$ftp_port, LOG_ERR); + $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port); + $ok = 0; + } + } + + $arrayresult = array('conn_id'=>$conn_id, 'ok'=>$ok, 'mesg'=>$mesg, 'curdir'=>$section, 'curdiriso'=>$newsectioniso); + return $arrayresult; +} + + +/** + * Tell if an entry is a FTP directory + * + * @param resource $connect_id Connection handler + * @param string $dir Directory + * @return int 1=directory, 0=not a directory + */ +function ftp_isdir($connect_id, $dir) +{ + if (@ftp_chdir($connect_id, $dir)) { + ftp_cdup($connect_id); + return 1; + } else { + return 0; + } +} + +/** + * Tell if an entry is a FTP directory + * + * @param resource $connect_id Connection handler + */ +function dol_ftp_close($connect_id) +{ + // Close FTP connection + if ($connect_id) { + if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { + } elseif (!empty($conf->global->FTP_CONNECT_WITH_SSL)) { + return ftp_close($connect_id); + } else { + return ftp_close($connect_id); + } + } +} \ No newline at end of file diff --git a/htdocs/ftp/admin/ftpclient.php b/htdocs/ftp/admin/ftpclient.php index c3d5509aa36..f914c835b37 100644 --- a/htdocs/ftp/admin/ftpclient.php +++ b/htdocs/ftp/admin/ftpclient.php @@ -25,7 +25,7 @@ require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php'; -$langs->loadLangs(array("admin", "other")); +$langs->loadLangs(array('ftp', "admin", "other")); $def = array(); $lastftpentry = 0; diff --git a/htdocs/ftp/index.php b/htdocs/ftp/index.php index 626703d4b68..54e66e845d4 100644 --- a/htdocs/ftp/index.php +++ b/htdocs/ftp/index.php @@ -27,9 +27,10 @@ require '../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/treeview.lib.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/ftp.lib.php'; // Load translation files required by the page -$langs->loadLangs(array('companies', 'other')); +$langs->loadLangs(array('ftp', 'companies', 'other')); // Security check if ($user->socid) { @@ -665,127 +666,14 @@ if (!function_exists('ftp_connect')) { print '
'; -// Close FTP connection -if ($conn_id) { - if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { - } elseif (!empty($conf->global->FTP_CONNECT_WITH_SSL)) { - ftp_close($conn_id); - } else { - ftp_close($conn_id); +if (!empty($conn_id)) { + $disconnect = dol_ftp_close($conn_id); + + if ($disconnect == false) { + setEventMessages($langs->trans("ErrorFTPNodisconnect"), null, 'errors'); } } // End of page llxFooter(); $db->close(); - - - -/** - * Connect to FTP server - * - * @param string $ftp_server Server name - * @param string $ftp_port Server port - * @param string $ftp_user FTP user - * @param string $ftp_password FTP password - * @param string $section Directory - * @param integer $ftp_passive Use a passive mode - * @return int <0 if OK, >0 if KO - */ -function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive = 0) -{ - global $langs, $conf; - - $ok = 1; - $error = 0; - $conn_id = null; - $newsectioniso = ''; - $mesg=""; - - if (!is_numeric($ftp_port)) { - $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port); - $ok = 0; - } - - if ($ok) { - $connecttimeout = (empty($conf->global->FTP_CONNECT_TIMEOUT) ? 40 : $conf->global->FTP_CONNECT_TIMEOUT); - if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { - dol_syslog('Try to connect with ssh2_connect'); - $tmp_conn_id = ssh2_connect($ftp_server, $ftp_port); - } elseif (!empty($conf->global->FTP_CONNECT_WITH_SSL)) { - dol_syslog('Try to connect with ftp_ssl_connect'); - $conn_id = ftp_ssl_connect($ftp_server, $ftp_port, $connecttimeout); - } else { - dol_syslog('Try to connect with ftp_connect'); - $conn_id = ftp_connect($ftp_server, $ftp_port, $connecttimeout); - } - if (!empty($conn_id) || !empty($tmp_conn_id)) { - if ($ftp_user) { - if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { - dol_syslog('Try to authenticate with ssh2_auth_password'); - if (ssh2_auth_password($tmp_conn_id, $ftp_user, $ftp_password)) { - // Turn on passive mode transfers (must be after a successful login - //if ($ftp_passive) ftp_pasv($conn_id, true); - - // Change the dir - $newsectioniso = utf8_decode($section); - //ftp_chdir($conn_id, $newsectioniso); - $conn_id = ssh2_sftp($tmp_conn_id); - if (!$conn_id) { - dol_syslog('Failed to connect to SFTP after sssh authentication', LOG_DEBUG); - $mesg = $langs->transnoentitiesnoconv("FailedToConnectToSFTPAfterSSHAuthentication"); - $ok = 0; - $error++; - } - } else { - dol_syslog('Failed to connect to FTP with login '.$ftp_user, LOG_DEBUG); - $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials"); - $ok = 0; - $error++; - } - } else { - if (ftp_login($conn_id, $ftp_user, $ftp_password)) { - // Turn on passive mode transfers (must be after a successful login - if ($ftp_passive) { - ftp_pasv($conn_id, true); - } - - // Change the dir - $newsectioniso = utf8_decode($section); - ftp_chdir($conn_id, $newsectioniso); - } else { - $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials"); - $ok = 0; - $error++; - } - } - } - } else { - dol_syslog('FailedToConnectToFTPServer '.$ftp_server.' '.$ftp_port, LOG_ERR); - $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port); - $ok = 0; - } - } - - $arrayresult = array('conn_id'=>$conn_id, 'ok'=>$ok, 'mesg'=>$mesg, 'curdir'=>$section, 'curdiriso'=>$newsectioniso); - return $arrayresult; -} - - -/** - * Tell if an entry is a FTP directory - * - * @param resource $connect_id Connection handler - * @param string $dir Directory - * @return int 1=directory, 0=not a directory - */ -function ftp_isdir($connect_id, $dir) -{ - if (@ftp_chdir($connect_id, $dir)) { - ftp_cdup($connect_id); - return 1; - } else { - return 0; - } -} - diff --git a/htdocs/langs/en_US/ftp.lang b/htdocs/langs/en_US/ftp.lang new file mode 100644 index 00000000000..26791d63918 --- /dev/null +++ b/htdocs/langs/en_US/ftp.lang @@ -0,0 +1,15 @@ +# FTP +FTPClientSetup=FTP or SFTP Client module setup +NewFTPClient=New FTP/SFTP connection setup +FTPArea=FTP/SFTP Area +FTPAreaDesc=This screen shows a view of an FTP et SFTP server. +SetupOfFTPClientModuleNotComplete=The setup of the FTP or SFTP client module seems to be incomplete +FTPFeatureNotSupportedByYourPHP=Your PHP does not support FTP or SFTP functions +FailedToConnectToFTPServer=Failed to connect to server (server %s, port %s) +FailedToConnectToFTPServerWithCredentials=Failed to login to server with defined login/password +FTPFailedToRemoveFile=Failed to remove file %s. +FTPFailedToRemoveDir=Failed to remove directory %s: check permissions and that the directory is empty. +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 diff --git a/htdocs/langs/en_US/other.lang b/htdocs/langs/en_US/other.lang index e533dc71fa5..2bb316f648f 100644 --- a/htdocs/langs/en_US/other.lang +++ b/htdocs/langs/en_US/other.lang @@ -310,18 +310,3 @@ ExternalSiteSetup=Setup link to external website ExternalSiteURL=External Site URL of HTML iframe content ExternalSiteModuleNotComplete=Module ExternalSite was not configured properly. ExampleMyMenuEntry=My menu entry - -# FTP -FTPClientSetup=FTP or SFTP Client module setup -NewFTPClient=New FTP/FTPS connection setup -FTPArea=FTP/FTPS Area -FTPAreaDesc=This screen shows a view of an FTP et SFTP server. -SetupOfFTPClientModuleNotComplete=The setup of the FTP or SFTP client module seems to be incomplete -FTPFeatureNotSupportedByYourPHP=Your PHP does not support FTP or SFTP functions -FailedToConnectToFTPServer=Failed to connect to server (server %s, port %s) -FailedToConnectToFTPServerWithCredentials=Failed to login to server with defined login/password -FTPFailedToRemoveFile=Failed to remove file %s. -FTPFailedToRemoveDir=Failed to remove directory %s: check permissions and that the directory is empty. -FTPPassiveMode=Passive mode -ChooseAFTPEntryIntoMenu=Choose a FTP/SFTP site from the menu... -FailedToGetFile=Failed to get files %s From f54b4baee0daa216c80a565eea7fc35fd5c17bde Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Thu, 28 Jul 2022 16:21:53 +0000 Subject: [PATCH 2/6] Fixing style errors. --- 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 1730a439bcb..d7ce0435c0d 100644 --- a/htdocs/core/lib/ftp.lib.php +++ b/htdocs/core/lib/ftp.lib.php @@ -150,4 +150,4 @@ function dol_ftp_close($connect_id) return ftp_close($connect_id); } } -} \ No newline at end of file +} From 77b3e511bedcaa04f4c550df326b4c3eff6462f8 Mon Sep 17 00:00:00 2001 From: Anthony Berton Date: Fri, 29 Jul 2022 08:51:57 +0200 Subject: [PATCH 3/6] Del file lang --- htdocs/ftp/admin/ftpclient.php | 2 +- htdocs/ftp/index.php | 2 +- htdocs/langs/en_US/ftp.lang | 15 --------------- htdocs/langs/en_US/other.lang | 16 ++++++++++++++++ 4 files changed, 18 insertions(+), 17 deletions(-) delete mode 100644 htdocs/langs/en_US/ftp.lang diff --git a/htdocs/ftp/admin/ftpclient.php b/htdocs/ftp/admin/ftpclient.php index f914c835b37..c3d5509aa36 100644 --- a/htdocs/ftp/admin/ftpclient.php +++ b/htdocs/ftp/admin/ftpclient.php @@ -25,7 +25,7 @@ require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php'; -$langs->loadLangs(array('ftp', "admin", "other")); +$langs->loadLangs(array("admin", "other")); $def = array(); $lastftpentry = 0; diff --git a/htdocs/ftp/index.php b/htdocs/ftp/index.php index 54e66e845d4..7fc13259cdc 100644 --- a/htdocs/ftp/index.php +++ b/htdocs/ftp/index.php @@ -30,7 +30,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/treeview.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/ftp.lib.php'; // Load translation files required by the page -$langs->loadLangs(array('ftp', 'companies', 'other')); +$langs->loadLangs(array('companies', 'other')); // Security check if ($user->socid) { diff --git a/htdocs/langs/en_US/ftp.lang b/htdocs/langs/en_US/ftp.lang deleted file mode 100644 index 26791d63918..00000000000 --- a/htdocs/langs/en_US/ftp.lang +++ /dev/null @@ -1,15 +0,0 @@ -# FTP -FTPClientSetup=FTP or SFTP Client module setup -NewFTPClient=New FTP/SFTP connection setup -FTPArea=FTP/SFTP Area -FTPAreaDesc=This screen shows a view of an FTP et SFTP server. -SetupOfFTPClientModuleNotComplete=The setup of the FTP or SFTP client module seems to be incomplete -FTPFeatureNotSupportedByYourPHP=Your PHP does not support FTP or SFTP functions -FailedToConnectToFTPServer=Failed to connect to server (server %s, port %s) -FailedToConnectToFTPServerWithCredentials=Failed to login to server with defined login/password -FTPFailedToRemoveFile=Failed to remove file %s. -FTPFailedToRemoveDir=Failed to remove directory %s: check permissions and that the directory is empty. -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 diff --git a/htdocs/langs/en_US/other.lang b/htdocs/langs/en_US/other.lang index 2bb316f648f..0918bf54ac7 100644 --- a/htdocs/langs/en_US/other.lang +++ b/htdocs/langs/en_US/other.lang @@ -310,3 +310,19 @@ ExternalSiteSetup=Setup link to external website ExternalSiteURL=External Site URL of HTML iframe content ExternalSiteModuleNotComplete=Module ExternalSite was not configured properly. ExampleMyMenuEntry=My menu entry + +# ftp +FTPClientSetup=FTP or SFTP Client module setup +NewFTPClient=New FTP/SFTP connection setup +FTPArea=FTP/SFTP Area +FTPAreaDesc=This screen shows a view of an FTP et SFTP server. +SetupOfFTPClientModuleNotComplete=The setup of the FTP or SFTP client module seems to be incomplete +FTPFeatureNotSupportedByYourPHP=Your PHP does not support FTP or SFTP functions +FailedToConnectToFTPServer=Failed to connect to server (server %s, port %s) +FailedToConnectToFTPServerWithCredentials=Failed to login to server with defined login/password +FTPFailedToRemoveFile=Failed to remove file %s. +FTPFailedToRemoveDir=Failed to remove directory %s: check permissions and that the directory is empty. +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 From 05c37bb1e9e3c4b41693dc79dbe05b51e7555ad5 Mon Sep 17 00:00:00 2001 From: Anthony Berton Date: Fri, 29 Jul 2022 10:05:54 +0200 Subject: [PATCH 4/6] Add functions --- htdocs/core/lib/ftp.lib.php | 117 ++++++++++++++++++++++++++++++++---- htdocs/ftp/index.php | 67 +++------------------ 2 files changed, 114 insertions(+), 70 deletions(-) diff --git a/htdocs/core/lib/ftp.lib.php b/htdocs/core/lib/ftp.lib.php index d7ce0435c0d..db4b6abccf0 100644 --- a/htdocs/core/lib/ftp.lib.php +++ b/htdocs/core/lib/ftp.lib.php @@ -39,11 +39,12 @@ */ function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive = 0) { + global $langs, $conf; $ok = 1; $error = 0; - $conn_id = null; + $connect_id = null; $newsectioniso = ''; $mesg=""; @@ -59,24 +60,24 @@ function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $sect $tmp_conn_id = ssh2_connect($ftp_server, $ftp_port); } elseif (!empty($conf->global->FTP_CONNECT_WITH_SSL)) { dol_syslog('Try to connect with ftp_ssl_connect'); - $conn_id = ftp_ssl_connect($ftp_server, $ftp_port, $connecttimeout); + $connect_id = ftp_ssl_connect($ftp_server, $ftp_port, $connecttimeout); } else { dol_syslog('Try to connect with ftp_connect'); - $conn_id = ftp_connect($ftp_server, $ftp_port, $connecttimeout); + $connect_id = ftp_connect($ftp_server, $ftp_port, $connecttimeout); } - if (!empty($conn_id) || !empty($tmp_conn_id)) { + if (!empty($connect_id) || !empty($tmp_conn_id)) { if ($ftp_user) { if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { dol_syslog('Try to authenticate with ssh2_auth_password'); if (ssh2_auth_password($tmp_conn_id, $ftp_user, $ftp_password)) { // Turn on passive mode transfers (must be after a successful login - //if ($ftp_passive) ftp_pasv($conn_id, true); + //if ($ftp_passive) ftp_pasv($connect_id, true); // Change the dir $newsectioniso = utf8_decode($section); - //ftp_chdir($conn_id, $newsectioniso); - $conn_id = ssh2_sftp($tmp_conn_id); - if (!$conn_id) { + //ftp_chdir($connect_id, $newsectioniso); + $connect_id = ssh2_sftp($tmp_conn_id); + if (!$connect_id) { dol_syslog('Failed to connect to SFTP after sssh authentication', LOG_DEBUG); $mesg = $langs->transnoentitiesnoconv("FailedToConnectToSFTPAfterSSHAuthentication"); $ok = 0; @@ -89,15 +90,15 @@ function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $sect $error++; } } else { - if (ftp_login($conn_id, $ftp_user, $ftp_password)) { + if (ftp_login($connect_id, $ftp_user, $ftp_password)) { // Turn on passive mode transfers (must be after a successful login if ($ftp_passive) { - ftp_pasv($conn_id, true); + ftp_pasv($connect_id, true); } // Change the dir $newsectioniso = utf8_decode($section); - ftp_chdir($conn_id, $newsectioniso); + ftp_chdir($connect_id, $newsectioniso); } else { $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials"); $ok = 0; @@ -112,7 +113,7 @@ function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $sect } } - $arrayresult = array('conn_id'=>$conn_id, 'ok'=>$ok, 'mesg'=>$mesg, 'curdir'=>$section, 'curdiriso'=>$newsectioniso); + $arrayresult = array('conn_id'=>$connect_id, 'ok'=>$ok, 'mesg'=>$mesg, 'curdir'=>$section, 'curdiriso'=>$newsectioniso); return $arrayresult; } @@ -126,6 +127,7 @@ function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $sect */ function ftp_isdir($connect_id, $dir) { + if (@ftp_chdir($connect_id, $dir)) { ftp_cdup($connect_id); return 1; @@ -138,9 +140,13 @@ function ftp_isdir($connect_id, $dir) * Tell if an entry is a FTP directory * * @param resource $connect_id Connection handler + * @return result */ function dol_ftp_close($connect_id) { + + global $conf; + // Close FTP connection if ($connect_id) { if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { @@ -151,3 +157,90 @@ function dol_ftp_close($connect_id) } } } + +/** + * Delete a FTP file + * + * @param resource $connect_id Connection handler + * @param string $file File + * @param string $newsection $newsection + */ +function dol_ftp_delete($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); + + //print "x".$newremotefileiso; + dol_syslog("ftp/index.php ftp_delete ".$newremotefileiso); + if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { + return ssh2_sftp_unlink($connect_id, $newremotefileiso); + } else { + var_dump($newremotefileiso); + return @ftp_delete($connect_id, $newremotefileiso); + } +} + +/** + * Download a FTP file + * + * @param resource $connect_id Connection handler + * @param string $file File + * @param string $newsection $newsection + */ +function dol_ftp_get($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_get($connect_id, $localfile, $newremotefileiso, FTP_BINARY); + } +} + +/** + * Remove FTP directory + * + * @param resource $connect_id Connection handler + * @param string $file File + * @param string $newsection $newsection + */ +function dol_ftp_rmdir($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)) { + $result = ssh2_sftp_rmdir($connect_id, $newremotefileiso); + } else { + $result = @ftp_rmdir($connect_id, $newremotefileiso); + } +} diff --git a/htdocs/ftp/index.php b/htdocs/ftp/index.php index 7fc13259cdc..324e3b27a38 100644 --- a/htdocs/ftp/index.php +++ b/htdocs/ftp/index.php @@ -160,24 +160,8 @@ if ($action == 'confirm_deletefile' && GETPOST('confirm') == 'yes') { if ($conn_id && $ok && !$mesg) { $newsection = $section; - if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { - $newsection = ssh2_sftp_realpath($conn_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169 - } + $result = dol_ftp_delete($conn_id, $file, $newsection); - $langs->load("other"); - - // Remote file - $filename = $file; - $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file; - $newremotefileiso = utf8_decode($remotefile); - - //print "x".$newremotefileiso; - dol_syslog("ftp/index.php ftp_delete ".$newremotefileiso); - if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { - $result = ssh2_sftp_unlink($conn_id, $newremotefileiso); - } else { - $result = @ftp_delete($conn_id, $newremotefileiso); - } if ($result) { setEventMessages($langs->trans("FileWasRemoved", $file), null, 'mesgs'); } else { @@ -185,8 +169,6 @@ if ($action == 'confirm_deletefile' && GETPOST('confirm') == 'yes') { setEventMessages($langs->trans("FTPFailedToRemoveFile", $file), null, 'errors'); } - //ftp_close($conn_id); Close later - $action = ''; } else { dol_print_error('', $mesg); @@ -206,25 +188,17 @@ if (GETPOST("const", 'array') && GETPOST("delete") && GETPOST("delete") == $lang if ($conn_id && $ok && !$mesg) { foreach (GETPOST('const', 'array') as $const) { - if ($const["check"]) { // Is checkbox checked + var_dump($const); + if (isset($const["check"])) { // Is checkbox checked $langs->load("other"); // Remote file $file = $const["file"]; $newsection = $const["section"]; - if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { - $newsection = ssh2_sftp_realpath($conn_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169 - } - $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file; - $newremotefileiso = utf8_decode($remotefile); + $newsection = $section; - //print "x".$newremotefileiso; - dol_syslog("ftp/index.php ftp_delete n files for ".$newremotefileiso); - if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { - $result = ssh2_sftp_unlink($conn_id, $newremotefileiso); - } else { - $result = @ftp_delete($conn_id, $newremotefileiso); - } + $result = dol_ftp_delete($conn_id, $file, $newsection); + var_dump($newsection); if ($result) { setEventMessages($langs->trans("FileWasRemoved", $file), null, 'mesgs'); } else { @@ -255,20 +229,9 @@ if ($action == 'confirm_deletesection' && $confirm == 'yes') { if ($conn_id && $ok && !$mesg) { $newsection = $section; - if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { - $newsection = ssh2_sftp_realpath($conn_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); + $result = dol_ftp_rmdir($connect_id, $file, $newsection); - if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { - $result = ssh2_sftp_rmdir($conn_id, $newremotefileiso); - } else { - $result = @ftp_rmdir($conn_id, $newremotefileiso); - } if ($result) { setEventMessages($langs->trans("DirWasRemoved", $file), null, 'mesgs'); } else { @@ -299,20 +262,10 @@ if ($action == 'download') { $localfile = tempnam($download_dir, 'dol_'); $newsection = $section; - if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { - $newsection = ssh2_sftp_realpath($conn_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); + $result = dol_ftp_get($connect_id, $file, $newsection); + - if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { - $result = fopen('ssh2.sftp://'.intval($conn_id).$newremotefileiso, 'r'); - } else { - $result = ftp_get($conn_id, $localfile, $newremotefileiso, FTP_BINARY); - } if ($result) { if (!empty($conf->global->MAIN_UMASK)) { @chmod($localfile, octdec($conf->global->MAIN_UMASK)); @@ -345,8 +298,6 @@ if ($action == 'download') { readfile($localfile); - ftp_close($conn_id); - exit; } else { setEventMessages($langs->transnoentitiesnoconv('FailedToGetFile', $remotefile), null, 'errors'); From cb59edb58b28ef015f6c50f873da1ddf7e1843dd Mon Sep 17 00:00:00 2001 From: Anthony Berton Date: Fri, 29 Jul 2022 10:06:19 +0200 Subject: [PATCH 5/6] Fix --- htdocs/core/lib/ftp.lib.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/htdocs/core/lib/ftp.lib.php b/htdocs/core/lib/ftp.lib.php index db4b6abccf0..19dacfdea76 100644 --- a/htdocs/core/lib/ftp.lib.php +++ b/htdocs/core/lib/ftp.lib.php @@ -164,6 +164,7 @@ function dol_ftp_close($connect_id) * @param resource $connect_id Connection handler * @param string $file File * @param string $newsection $newsection + * @return result */ function dol_ftp_delete($connect_id, $file, $newsection) { @@ -195,6 +196,7 @@ function dol_ftp_delete($connect_id, $file, $newsection) * @param resource $connect_id Connection handler * @param string $file File * @param string $newsection $newsection + * @return result */ function dol_ftp_get($connect_id, $file, $newsection) { @@ -223,6 +225,7 @@ function dol_ftp_get($connect_id, $file, $newsection) * @param resource $connect_id Connection handler * @param string $file File * @param string $newsection $newsection + * @return result */ function dol_ftp_rmdir($connect_id, $file, $newsection) { From 8e7fa102b85030e63ee7c50636d35e4f118e3716 Mon Sep 17 00:00:00 2001 From: Anthony Berton Date: Fri, 29 Jul 2022 11:14:28 +0200 Subject: [PATCH 6/6] Clean --- htdocs/core/lib/ftp.lib.php | 5 ++--- htdocs/ftp/index.php | 13 ++++--------- htdocs/langs/en_US/companies.lang | 2 +- htdocs/langs/fr_FR/companies.lang | 2 +- 4 files changed, 8 insertions(+), 14 deletions(-) diff --git a/htdocs/core/lib/ftp.lib.php b/htdocs/core/lib/ftp.lib.php index 19dacfdea76..42f5806fba5 100644 --- a/htdocs/core/lib/ftp.lib.php +++ b/htdocs/core/lib/ftp.lib.php @@ -185,7 +185,6 @@ function dol_ftp_delete($connect_id, $file, $newsection) if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { return ssh2_sftp_unlink($connect_id, $newremotefileiso); } else { - var_dump($newremotefileiso); return @ftp_delete($connect_id, $newremotefileiso); } } @@ -242,8 +241,8 @@ function dol_ftp_rmdir($connect_id, $file, $newsection) $newremotefileiso = utf8_decode($remotefile); if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { - $result = ssh2_sftp_rmdir($connect_id, $newremotefileiso); + return ssh2_sftp_rmdir($connect_id, $newremotefileiso); } else { - $result = @ftp_rmdir($connect_id, $newremotefileiso); + return @ftp_rmdir($connect_id, $newremotefileiso); } } diff --git a/htdocs/ftp/index.php b/htdocs/ftp/index.php index 324e3b27a38..cf647929ac8 100644 --- a/htdocs/ftp/index.php +++ b/htdocs/ftp/index.php @@ -188,17 +188,15 @@ if (GETPOST("const", 'array') && GETPOST("delete") && GETPOST("delete") == $lang if ($conn_id && $ok && !$mesg) { foreach (GETPOST('const', 'array') as $const) { - var_dump($const); if (isset($const["check"])) { // Is checkbox checked $langs->load("other"); // Remote file $file = $const["file"]; $newsection = $const["section"]; - $newsection = $section; $result = dol_ftp_delete($conn_id, $file, $newsection); - var_dump($newsection); + if ($result) { setEventMessages($langs->trans("FileWasRemoved", $file), null, 'mesgs'); } else { @@ -230,7 +228,7 @@ if ($action == 'confirm_deletesection' && $confirm == 'yes') { if ($conn_id && $ok && !$mesg) { $newsection = $section; - $result = dol_ftp_rmdir($connect_id, $file, $newsection); + $result = dol_ftp_rmdir($conn_id, $file, $newsection); if ($result) { setEventMessages($langs->trans("DirWasRemoved", $file), null, 'mesgs'); @@ -361,12 +359,12 @@ if (!function_exists('ftp_connect')) { if (!empty($ftp_server)) { // Confirm remove file if ($action == 'delete') { - print $form->formconfirm($_SERVER["PHP_SELF"].'?numero_ftp='.$numero_ftp.'§ion='.urlencode(GETPOST('section')).'&file='.urlencode(GETPOST('file')), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile', '', '', 1); + print $form->formconfirm($_SERVER["PHP_SELF"].'?numero_ftp='.$numero_ftp.'§ion='.urlencode(GETPOST('section')).'&file='.urlencode(GETPOST('file')), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile', GETPOST('file')), 'confirm_deletefile', '', '', 1); } // Confirmation de la suppression d'une ligne categorie if ($action == 'delete_section') { - print $form->formconfirm($_SERVER["PHP_SELF"].'?numero_ftp='.$numero_ftp.'§ion='.urlencode(GETPOST('section')).'&file='.urlencode(GETPOST('file')), $langs->trans('DeleteSection'), $langs->trans('ConfirmDeleteSection', $ecmdir->label), 'confirm_deletesection', '', '', 1); + print $form->formconfirm($_SERVER["PHP_SELF"].'?numero_ftp='.$numero_ftp.'§ion='.urlencode(GETPOST('section')).'&file='.urlencode(GETPOST('file')), $langs->trans('DeleteSection'), $langs->trans('ConfirmDeleteSection', GETPOST('file')), 'confirm_deletesection', '', '', 1); } print $langs->trans("Server").': '.$ftp_server.'
'; @@ -456,8 +454,6 @@ if (!function_exists('ftp_connect')) { } else { $buff = ftp_rawlist($conn_id, $newsectioniso); $contents = ftp_nlist($conn_id, $newsectioniso); // Sometimes rawlist fails but never nlist - //var_dump($contents); - //var_dump($buff); } $nboflines = count($contents); @@ -467,7 +463,6 @@ if (!function_exists('ftp_connect')) { while ($i < $nboflines && $i < 1000) { $vals = preg_split('@ +@', utf8_encode($buff[$i]), 9); //$vals=preg_split('@ +@','drwxr-xr-x 2 root root 4096 Aug 30 2008 backup_apollon1',9); - //var_dump($vals); $file = $vals[8]; if (empty($file)) { $rawlisthasfailed = true; diff --git a/htdocs/langs/en_US/companies.lang b/htdocs/langs/en_US/companies.lang index 71b16f24506..996c98c203a 100644 --- a/htdocs/langs/en_US/companies.lang +++ b/htdocs/langs/en_US/companies.lang @@ -444,7 +444,7 @@ AddAddress=Add address SupplierCategory=Vendor category JuridicalStatus200=Independent DeleteFile=Delete file -ConfirmDeleteFile=Are you sure you want to delete this file? +ConfirmDeleteFile=Are you sure you want to delete this file %s? AllocateCommercial=Assigned to sales representative Organization=Organization FiscalYearInformation=Fiscal Year diff --git a/htdocs/langs/fr_FR/companies.lang b/htdocs/langs/fr_FR/companies.lang index 889f16b3f75..3359131aa51 100644 --- a/htdocs/langs/fr_FR/companies.lang +++ b/htdocs/langs/fr_FR/companies.lang @@ -443,7 +443,7 @@ AddAddress=Créer adresse SupplierCategory=Catégorie du fournisseur JuridicalStatus200=Indépendant DeleteFile=Suppression d'un fichier -ConfirmDeleteFile=Êtes-vous sûr de vouloir supprimer ce fichier ? +ConfirmDeleteFile=Êtes-vous sûr de vouloir supprimer ce fichier %s? AllocateCommercial=Affecter un commercial Organization=Organisme FiscalYearInformation=Exercice fiscal