Merge pull request #21615 from bb2a/NEW---FTP-lib-and-lang-file

NEW - FTP lib
This commit is contained in:
Laurent Destailleur 2022-07-29 15:05:52 +02:00 committed by GitHub
commit 9796ec22bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 269 additions and 186 deletions

248
htdocs/core/lib/ftp.lib.php Normal file
View File

@ -0,0 +1,248 @@
<?php
/* Copyright (C) 2022 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2022 Anthony Berton <bertonanthony@gmail.com>
*
* 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 <https://www.gnu.org/licenses/>.
* 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;
$connect_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');
$connect_id = ftp_ssl_connect($ftp_server, $ftp_port, $connecttimeout);
} else {
dol_syslog('Try to connect with ftp_connect');
$connect_id = ftp_connect($ftp_server, $ftp_port, $connecttimeout);
}
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($connect_id, true);
// Change the dir
$newsectioniso = utf8_decode($section);
//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;
$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($connect_id, $ftp_user, $ftp_password)) {
// Turn on passive mode transfers (must be after a successful login
if ($ftp_passive) {
ftp_pasv($connect_id, true);
}
// Change the dir
$newsectioniso = utf8_decode($section);
ftp_chdir($connect_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'=>$connect_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
* @return result
*/
function dol_ftp_close($connect_id)
{
global $conf;
// 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);
}
}
}
/**
* Delete a FTP file
*
* @param resource $connect_id Connection handler
* @param string $file File
* @param string $newsection $newsection
* @return result
*/
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 {
return @ftp_delete($connect_id, $newremotefileiso);
}
}
/**
* Download a FTP file
*
* @param resource $connect_id Connection handler
* @param string $file File
* @param string $newsection $newsection
* @return result
*/
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
* @return result
*/
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)) {
return ssh2_sftp_rmdir($connect_id, $newremotefileiso);
} else {
return @ftp_rmdir($connect_id, $newremotefileiso);
}
}

View File

@ -27,6 +27,7 @@ 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'));
@ -159,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 {
@ -184,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);
@ -205,25 +188,15 @@ 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
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);
//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);
if ($result) {
setEventMessages($langs->trans("FileWasRemoved", $file), null, 'mesgs');
} else {
@ -254,20 +227,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($conn_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 {
@ -298,20 +260,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));
@ -344,8 +296,6 @@ if ($action == 'download') {
readfile($localfile);
ftp_close($conn_id);
exit;
} else {
setEventMessages($langs->transnoentitiesnoconv('FailedToGetFile', $remotefile), null, 'errors');
@ -409,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.'&section='.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.'&section='.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.'&section='.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.'&section='.urlencode(GETPOST('section')).'&file='.urlencode(GETPOST('file')), $langs->trans('DeleteSection'), $langs->trans('ConfirmDeleteSection', GETPOST('file')), 'confirm_deletesection', '', '', 1);
}
print $langs->trans("Server").': <b>'.$ftp_server.'</b><br>';
@ -504,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);
@ -515,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;
@ -665,127 +612,14 @@ if (!function_exists('ftp_connect')) {
print '<br>';
// 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;
}
}

View File

@ -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 <b>%s</b>?
AllocateCommercial=Assigned to sales representative
Organization=Organization
FiscalYearInformation=Fiscal Year

View File

@ -311,10 +311,10 @@ ExternalSiteURL=External Site URL of HTML iframe content
ExternalSiteModuleNotComplete=Module ExternalSite was not configured properly.
ExampleMyMenuEntry=My menu entry
# FTP
# ftp
FTPClientSetup=FTP or SFTP Client module setup
NewFTPClient=New FTP/FTPS connection setup
FTPArea=FTP/FTPS Area
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
@ -325,3 +325,4 @@ FTPFailedToRemoveDir=Failed to remove directory <b>%s</b>: 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

View File

@ -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 <b>%s</b>?
AllocateCommercial=Affecter un commercial
Organization=Organisme
FiscalYearInformation=Exercice fiscal