40 function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive = 0)
51 if (!is_numeric($ftp_port)) {
52 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToFTPServer", $ftp_server, $ftp_port);
57 $connecttimeout = (empty($conf->global->FTP_CONNECT_TIMEOUT) ? 40 : $conf->global->FTP_CONNECT_TIMEOUT);
58 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
59 dol_syslog(
'Try to connect with ssh2_connect');
60 $tmp_conn_id = ssh2_connect($ftp_server, $ftp_port);
61 } elseif (!empty($conf->global->FTP_CONNECT_WITH_SSL)) {
62 dol_syslog(
'Try to connect with ftp_ssl_connect');
63 $connect_id = ftp_ssl_connect($ftp_server, $ftp_port, $connecttimeout);
66 $connect_id = ftp_connect($ftp_server, $ftp_port, $connecttimeout);
68 if (!empty($connect_id) || !empty($tmp_conn_id)) {
70 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
71 dol_syslog(
'Try to authenticate with ssh2_auth_password');
72 if (ssh2_auth_password($tmp_conn_id, $ftp_user, $ftp_password)) {
77 $newsectioniso = utf8_decode($section);
79 $connect_id = ssh2_sftp($tmp_conn_id);
81 dol_syslog(
'Failed to connect to SFTP after sssh authentication', LOG_DEBUG);
82 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToSFTPAfterSSHAuthentication");
87 dol_syslog(
'Failed to connect to FTP with login '.$ftp_user, LOG_DEBUG);
88 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToFTPServerWithCredentials");
93 if (ftp_login($connect_id, $ftp_user, $ftp_password)) {
96 ftp_pasv($connect_id,
true);
100 $newsectioniso = utf8_decode($section);
101 ftp_chdir($connect_id, $newsectioniso);
103 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToFTPServerWithCredentials");
110 dol_syslog(
'FailedToConnectToFTPServer '.$ftp_server.
' '.$ftp_port, LOG_ERR);
111 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToFTPServer", $ftp_server, $ftp_port);
116 $arrayresult = array(
'conn_id'=>$connect_id,
'ok'=>$ok,
'mesg'=>$mesg,
'curdir'=>$section,
'curdiriso'=>$newsectioniso);
131 if (@ftp_chdir($connect_id, $dir)) {
132 ftp_cdup($connect_id);
152 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
153 } elseif (!empty($conf->global->FTP_CONNECT_WITH_SSL)) {
154 return ftp_close($connect_id);
156 return ftp_close($connect_id);
174 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
175 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
180 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
181 $newremotefileiso = utf8_decode($remotefile);
184 dol_syslog(
"ftp/index.php ftp_delete ".$newremotefileiso);
185 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
186 return ssh2_sftp_unlink($connect_id, $newremotefileiso);
188 return @ftp_delete($connect_id, $newremotefileiso);
206 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
207 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
212 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
213 $newremotefileiso = utf8_decode($remotefile);
215 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
216 return fopen(
'ssh2.sftp://'.intval($connect_id).$newremotefileiso,
'r');
218 return ftp_get($connect_id, $localfile, $newremotefileiso, FTP_BINARY);
236 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
237 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
242 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
243 $newremotefileiso = utf8_decode($remotefile);
245 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
246 return ssh2_scp_send($connect_id, $localfile, $newremotefileiso, 0644);
248 return ftp_put($connect_id, $newremotefileiso, $localfile, FTP_BINARY);
265 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
266 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
271 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
272 $newremotefileiso = utf8_decode($remotefile);
274 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
275 return ssh2_sftp_rmdir($connect_id, $newremotefileiso);
277 return @ftp_rmdir($connect_id, $newremotefileiso);
295 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
296 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
300 $newremotefileiso = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$newdir;
301 $newremotefileiso = utf8_decode($newremotefileiso);
303 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
304 return ssh2_sftp_mkdir($connect_id, $newremotefileiso, 0777);
306 return @ftp_mkdir($connect_id, $newremotefileiso);