2
0
forked from Wavyzz/dolibarr

Add functions

This commit is contained in:
Anthony Berton
2022-07-29 10:05:54 +02:00
parent f10dd33440
commit 05c37bb1e9
2 changed files with 114 additions and 70 deletions

View File

@@ -39,11 +39,12 @@
*/ */
function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive = 0) function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive = 0)
{ {
global $langs, $conf; global $langs, $conf;
$ok = 1; $ok = 1;
$error = 0; $error = 0;
$conn_id = null; $connect_id = null;
$newsectioniso = ''; $newsectioniso = '';
$mesg=""; $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); $tmp_conn_id = ssh2_connect($ftp_server, $ftp_port);
} elseif (!empty($conf->global->FTP_CONNECT_WITH_SSL)) { } elseif (!empty($conf->global->FTP_CONNECT_WITH_SSL)) {
dol_syslog('Try to connect with ftp_ssl_connect'); 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 { } else {
dol_syslog('Try to connect with ftp_connect'); 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 ($ftp_user) {
if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
dol_syslog('Try to authenticate with ssh2_auth_password'); dol_syslog('Try to authenticate with ssh2_auth_password');
if (ssh2_auth_password($tmp_conn_id, $ftp_user, $ftp_password)) { if (ssh2_auth_password($tmp_conn_id, $ftp_user, $ftp_password)) {
// Turn on passive mode transfers (must be after a successful login // 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 // Change the dir
$newsectioniso = utf8_decode($section); $newsectioniso = utf8_decode($section);
//ftp_chdir($conn_id, $newsectioniso); //ftp_chdir($connect_id, $newsectioniso);
$conn_id = ssh2_sftp($tmp_conn_id); $connect_id = ssh2_sftp($tmp_conn_id);
if (!$conn_id) { if (!$connect_id) {
dol_syslog('Failed to connect to SFTP after sssh authentication', LOG_DEBUG); dol_syslog('Failed to connect to SFTP after sssh authentication', LOG_DEBUG);
$mesg = $langs->transnoentitiesnoconv("FailedToConnectToSFTPAfterSSHAuthentication"); $mesg = $langs->transnoentitiesnoconv("FailedToConnectToSFTPAfterSSHAuthentication");
$ok = 0; $ok = 0;
@@ -89,15 +90,15 @@ function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $sect
$error++; $error++;
} }
} else { } 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 // Turn on passive mode transfers (must be after a successful login
if ($ftp_passive) { if ($ftp_passive) {
ftp_pasv($conn_id, true); ftp_pasv($connect_id, true);
} }
// Change the dir // Change the dir
$newsectioniso = utf8_decode($section); $newsectioniso = utf8_decode($section);
ftp_chdir($conn_id, $newsectioniso); ftp_chdir($connect_id, $newsectioniso);
} else { } else {
$mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials"); $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
$ok = 0; $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; 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) function ftp_isdir($connect_id, $dir)
{ {
if (@ftp_chdir($connect_id, $dir)) { if (@ftp_chdir($connect_id, $dir)) {
ftp_cdup($connect_id); ftp_cdup($connect_id);
return 1; return 1;
@@ -138,9 +140,13 @@ function ftp_isdir($connect_id, $dir)
* Tell if an entry is a FTP directory * Tell if an entry is a FTP directory
* *
* @param resource $connect_id Connection handler * @param resource $connect_id Connection handler
* @return result
*/ */
function dol_ftp_close($connect_id) function dol_ftp_close($connect_id)
{ {
global $conf;
// Close FTP connection // Close FTP connection
if ($connect_id) { if ($connect_id) {
if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { 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);
}
}

View File

@@ -160,24 +160,8 @@ if ($action == 'confirm_deletefile' && GETPOST('confirm') == 'yes') {
if ($conn_id && $ok && !$mesg) { if ($conn_id && $ok && !$mesg) {
$newsection = $section; $newsection = $section;
if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { $result = dol_ftp_delete($conn_id, $file, $newsection);
$newsection = ssh2_sftp_realpath($conn_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
}
$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) { if ($result) {
setEventMessages($langs->trans("FileWasRemoved", $file), null, 'mesgs'); setEventMessages($langs->trans("FileWasRemoved", $file), null, 'mesgs');
} else { } else {
@@ -185,8 +169,6 @@ if ($action == 'confirm_deletefile' && GETPOST('confirm') == 'yes') {
setEventMessages($langs->trans("FTPFailedToRemoveFile", $file), null, 'errors'); setEventMessages($langs->trans("FTPFailedToRemoveFile", $file), null, 'errors');
} }
//ftp_close($conn_id); Close later
$action = ''; $action = '';
} else { } else {
dol_print_error('', $mesg); dol_print_error('', $mesg);
@@ -206,25 +188,17 @@ if (GETPOST("const", 'array') && GETPOST("delete") && GETPOST("delete") == $lang
if ($conn_id && $ok && !$mesg) { if ($conn_id && $ok && !$mesg) {
foreach (GETPOST('const', 'array') as $const) { 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"); $langs->load("other");
// Remote file // Remote file
$file = $const["file"]; $file = $const["file"];
$newsection = $const["section"]; $newsection = $const["section"];
if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) { $newsection = $section;
$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; $result = dol_ftp_delete($conn_id, $file, $newsection);
dol_syslog("ftp/index.php ftp_delete n files for ".$newremotefileiso); var_dump($newsection);
if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
$result = ssh2_sftp_unlink($conn_id, $newremotefileiso);
} else {
$result = @ftp_delete($conn_id, $newremotefileiso);
}
if ($result) { if ($result) {
setEventMessages($langs->trans("FileWasRemoved", $file), null, 'mesgs'); setEventMessages($langs->trans("FileWasRemoved", $file), null, 'mesgs');
} else { } else {
@@ -255,20 +229,9 @@ if ($action == 'confirm_deletesection' && $confirm == 'yes') {
if ($conn_id && $ok && !$mesg) { if ($conn_id && $ok && !$mesg) {
$newsection = $section; $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 $result = dol_ftp_rmdir($connect_id, $file, $newsection);
$filename = $file;
$remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
$newremotefileiso = utf8_decode($remotefile);
if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
$result = ssh2_sftp_rmdir($conn_id, $newremotefileiso);
} else {
$result = @ftp_rmdir($conn_id, $newremotefileiso);
}
if ($result) { if ($result) {
setEventMessages($langs->trans("DirWasRemoved", $file), null, 'mesgs'); setEventMessages($langs->trans("DirWasRemoved", $file), null, 'mesgs');
} else { } else {
@@ -299,20 +262,10 @@ if ($action == 'download') {
$localfile = tempnam($download_dir, 'dol_'); $localfile = tempnam($download_dir, 'dol_');
$newsection = $section; $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 $result = dol_ftp_get($connect_id, $file, $newsection);
$filename = $file;
$remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
$newremotefileiso = utf8_decode($remotefile);
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 ($result) {
if (!empty($conf->global->MAIN_UMASK)) { if (!empty($conf->global->MAIN_UMASK)) {
@chmod($localfile, octdec($conf->global->MAIN_UMASK)); @chmod($localfile, octdec($conf->global->MAIN_UMASK));
@@ -345,8 +298,6 @@ if ($action == 'download') {
readfile($localfile); readfile($localfile);
ftp_close($conn_id);
exit; exit;
} else { } else {
setEventMessages($langs->transnoentitiesnoconv('FailedToGetFile', $remotefile), null, 'errors'); setEventMessages($langs->transnoentitiesnoconv('FailedToGetFile', $remotefile), null, 'errors');