40function 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);
59 dol_syslog(
'Try to connect with ssh2_connect');
60 $tmp_conn_id = ssh2_connect($ftp_server, (
int) $ftp_port);
62 dol_syslog(
'Try to connect with ftp_ssl_connect');
63 $connect_id = ftp_ssl_connect($ftp_server, (
int) $ftp_port, $connecttimeout);
66 $connect_id = ftp_connect($ftp_server, (
int) $ftp_port, $connecttimeout);
68 if (!empty($connect_id) || !empty($tmp_conn_id)) {
71 dol_syslog(
'Try to authenticate with ssh2_auth_password');
72 if (!empty($tmp_conn_id) && ssh2_auth_password($tmp_conn_id, $ftp_user, $ftp_password)) {
77 $newsectioniso = mb_convert_encoding($section,
'ISO-8859-1');
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 (!empty($connect_id) && ftp_login($connect_id, $ftp_user, $ftp_password)) {
96 ftp_pasv($connect_id,
true);
100 $newsectioniso = mb_convert_encoding($section,
'ISO-8859-1');
101 if (!ftp_chdir($connect_id, $newsectioniso)) {
103 $mesg = $langs->transnoentitiesnoconv(
"FailedToChdirOnFTPServer");
107 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToFTPServerWithCredentials");
112 dol_syslog(
'FailedToConnectToFTPServer '.$ftp_server.
' '.$ftp_port, LOG_ERR);
113 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToFTPServer", $ftp_server, $ftp_port);
118 $arrayresult = array(
'conn_id' => $connect_id,
'ok' => $ok,
'mesg' => $mesg,
'curdir' => $section,
'curdiriso' => $newsectioniso);
168 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
172 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
173 $newremotefileiso = mb_convert_encoding($remotefile,
'ISO-8859-1');
176 dol_syslog(
"ftp/index.php ftp_delete ".$newremotefileiso);
178 return ssh2_sftp_unlink($connect_id, $newremotefileiso);
180 return @ftp_delete($connect_id, $newremotefileiso);
196 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
200 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
201 $newremotefileiso = mb_convert_encoding($remotefile,
'ISO-8859-1');
204 return fopen(
'ssh2.sftp://'.intval($connect_id).$newremotefileiso,
'r');
206 return ftp_get($connect_id, $localfile, $newremotefileiso, FTP_BINARY);
222 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
226 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
227 $newremotefileiso = mb_convert_encoding($remotefile,
'ISO-8859-1');
230 return ssh2_scp_send($connect_id, $localfile, $newremotefileiso, 0644);
232 return ftp_put($connect_id, $newremotefileiso, $localfile, FTP_BINARY);
247 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
251 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
252 $newremotefileiso = mb_convert_encoding($remotefile,
'ISO-8859-1');
255 return ssh2_sftp_rmdir($connect_id, $newremotefileiso);
257 return @ftp_rmdir($connect_id, $newremotefileiso);
273 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
277 $newremotefileiso = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$newdir;
278 $newremotefileiso = mb_convert_encoding($newremotefileiso,
'ISO-8859-1');
281 return ssh2_sftp_mkdir($connect_id, $newremotefileiso, 0777);
283 return @ftp_mkdir($connect_id, $newremotefileiso);
dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive=0)
Connect to FTP server.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.