38function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive = 0)
48 if (!is_numeric($ftp_port)) {
49 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToFTPServer", $ftp_server, $ftp_port);
54 $connecttimeout = (empty($conf->global->FTP_CONNECT_TIMEOUT) ? 40 : $conf->global->FTP_CONNECT_TIMEOUT);
55 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
56 dol_syslog(
'Try to connect with ssh2_connect');
57 $tmp_conn_id = ssh2_connect($ftp_server, $ftp_port);
58 } elseif (!empty($conf->global->FTP_CONNECT_WITH_SSL)) {
59 dol_syslog(
'Try to connect with ftp_ssl_connect');
60 $connect_id = ftp_ssl_connect($ftp_server, $ftp_port, $connecttimeout);
63 $connect_id = ftp_connect($ftp_server, $ftp_port, $connecttimeout);
65 if (!empty($connect_id) || !empty($tmp_conn_id)) {
67 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
68 dol_syslog(
'Try to authenticate with ssh2_auth_password');
69 if (ssh2_auth_password($tmp_conn_id, $ftp_user, $ftp_password)) {
74 $newsectioniso = utf8_decode($section);
76 $connect_id = ssh2_sftp($tmp_conn_id);
78 dol_syslog(
'Failed to connect to SFTP after sssh authentication', LOG_DEBUG);
79 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToSFTPAfterSSHAuthentication");
84 dol_syslog(
'Failed to connect to FTP with login '.$ftp_user, LOG_DEBUG);
85 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToFTPServerWithCredentials");
90 if (ftp_login($connect_id, $ftp_user, $ftp_password)) {
93 ftp_pasv($connect_id,
true);
97 $newsectioniso = utf8_decode($section);
98 ftp_chdir($connect_id, $newsectioniso);
100 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToFTPServerWithCredentials");
107 dol_syslog(
'FailedToConnectToFTPServer '.$ftp_server.
' '.$ftp_port, LOG_ERR);
108 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToFTPServer", $ftp_server, $ftp_port);
113 $arrayresult = array(
'conn_id'=>$connect_id,
'ok'=>$ok,
'mesg'=>$mesg,
'curdir'=>$section,
'curdiriso'=>$newsectioniso);
169 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
170 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
175 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
176 $newremotefileiso = utf8_decode($remotefile);
179 dol_syslog(
"ftp/index.php ftp_delete ".$newremotefileiso);
180 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
181 return ssh2_sftp_unlink($connect_id, $newremotefileiso);
183 return @ftp_delete($connect_id, $newremotefileiso);
200 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
201 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
206 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
207 $newremotefileiso = utf8_decode($remotefile);
209 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
210 return fopen(
'ssh2.sftp://'.intval($connect_id).$newremotefileiso,
'r');
212 return ftp_get($connect_id, $localfile, $newremotefileiso, FTP_BINARY);
229 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
230 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
235 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
236 $newremotefileiso = utf8_decode($remotefile);
238 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
239 return ssh2_scp_send($connect_id, $localfile, $newremotefileiso, 0644);
241 return ftp_put($connect_id, $newremotefileiso, $localfile, FTP_BINARY);
257 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
258 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
263 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
264 $newremotefileiso = utf8_decode($remotefile);
266 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
267 return ssh2_sftp_rmdir($connect_id, $newremotefileiso);
269 return @ftp_rmdir($connect_id, $newremotefileiso);
286 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
287 $newsection = ssh2_sftp_realpath($connect_id,
".").
'/./';
291 $newremotefileiso = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$newdir;
292 $newremotefileiso = utf8_decode($newremotefileiso);
294 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
295 return ssh2_sftp_mkdir($connect_id, $newremotefileiso, 0777);
297 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.