40 function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive = 0)
50 if (!is_numeric($ftp_port)) {
51 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToFTPServer", $ftp_server, $ftp_port);
56 $connecttimeout = (empty($conf->global->FTP_CONNECT_TIMEOUT) ? 40 : $conf->global->FTP_CONNECT_TIMEOUT);
57 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
58 dol_syslog(
'Try to connect with ssh2_connect');
59 $tmp_conn_id = ssh2_connect($ftp_server, $ftp_port);
60 } elseif (!empty($conf->global->FTP_CONNECT_WITH_SSL)) {
61 dol_syslog(
'Try to connect with ftp_ssl_connect');
62 $connect_id = ftp_ssl_connect($ftp_server, $ftp_port, $connecttimeout);
65 $connect_id = ftp_connect($ftp_server, $ftp_port, $connecttimeout);
67 if (!empty($connect_id) || !empty($tmp_conn_id)) {
69 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
70 dol_syslog(
'Try to authenticate with ssh2_auth_password');
71 if (ssh2_auth_password($tmp_conn_id, $ftp_user, $ftp_password)) {
76 $newsectioniso = utf8_decode($section);
78 $connect_id = ssh2_sftp($tmp_conn_id);
80 dol_syslog(
'Failed to connect to SFTP after sssh authentication', LOG_DEBUG);
81 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToSFTPAfterSSHAuthentication");
86 dol_syslog(
'Failed to connect to FTP with login '.$ftp_user, LOG_DEBUG);
87 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToFTPServerWithCredentials");
92 if (ftp_login($connect_id, $ftp_user, $ftp_password)) {
95 ftp_pasv($connect_id,
true);
99 $newsectioniso = utf8_decode($section);
100 ftp_chdir($connect_id, $newsectioniso);
102 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToFTPServerWithCredentials");
109 dol_syslog(
'FailedToConnectToFTPServer '.$ftp_server.
' '.$ftp_port, LOG_ERR);
110 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToFTPServer", $ftp_server, $ftp_port);
115 $arrayresult = array(
'conn_id'=>$connect_id,
'ok'=>$ok,
'mesg'=>$mesg,
'curdir'=>$section,
'curdiriso'=>$newsectioniso);
129 if (@ftp_chdir($connect_id, $dir)) {
130 ftp_cdup($connect_id);
149 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
150 } elseif (!empty($conf->global->FTP_CONNECT_WITH_SSL)) {
151 return ftp_close($connect_id);
153 return ftp_close($connect_id);
170 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
171 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
176 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
177 $newremotefileiso = utf8_decode($remotefile);
180 dol_syslog(
"ftp/index.php ftp_delete ".$newremotefileiso);
181 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
182 return ssh2_sftp_unlink($connect_id, $newremotefileiso);
184 return @ftp_delete($connect_id, $newremotefileiso);
201 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
202 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
207 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
208 $newremotefileiso = utf8_decode($remotefile);
210 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
211 return fopen(
'ssh2.sftp://'.intval($connect_id).$newremotefileiso,
'r');
213 return ftp_get($connect_id, $localfile, $newremotefileiso, FTP_BINARY);
230 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
231 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
236 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
237 $newremotefileiso = utf8_decode($remotefile);
239 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
240 return ssh2_scp_send($connect_id, $localfile, $newremotefileiso, 0644);
242 return ftp_put($connect_id, $newremotefileiso, $localfile, FTP_BINARY);
258 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
259 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
264 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
265 $newremotefileiso = utf8_decode($remotefile);
267 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
268 return ssh2_sftp_rmdir($connect_id, $newremotefileiso);
270 return @ftp_rmdir($connect_id, $newremotefileiso);
287 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
288 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
292 $newremotefileiso = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$newdir;
293 $newremotefileiso = utf8_decode($newremotefileiso);
295 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
296 return ssh2_sftp_mkdir($connect_id, $newremotefileiso, 0777);
298 return @ftp_mkdir($connect_id, $newremotefileiso);