dolibarr  x.y.z
ftp.lib.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2022 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2022 Anthony Berton <bertonanthony@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  * or see https://www.gnu.org/
18  */
19 
40 function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive = 0)
41 {
42  global $langs, $conf;
43 
44  $ok = 1;
45  $error = 0;
46  $connect_id = null;
47  $newsectioniso = '';
48  $mesg="";
49 
50  if (!is_numeric($ftp_port)) {
51  $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port);
52  $ok = 0;
53  }
54 
55  if ($ok) {
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);
63  } else {
64  dol_syslog('Try to connect with ftp_connect');
65  $connect_id = ftp_connect($ftp_server, $ftp_port, $connecttimeout);
66  }
67  if (!empty($connect_id) || !empty($tmp_conn_id)) {
68  if ($ftp_user) {
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)) {
72  // Turn on passive mode transfers (must be after a successful login
73  //if ($ftp_passive) ftp_pasv($connect_id, true);
74 
75  // Change the dir
76  $newsectioniso = utf8_decode($section);
77  //ftp_chdir($connect_id, $newsectioniso);
78  $connect_id = ssh2_sftp($tmp_conn_id);
79  if (!$connect_id) {
80  dol_syslog('Failed to connect to SFTP after sssh authentication', LOG_DEBUG);
81  $mesg = $langs->transnoentitiesnoconv("FailedToConnectToSFTPAfterSSHAuthentication");
82  $ok = 0;
83  $error++;
84  }
85  } else {
86  dol_syslog('Failed to connect to FTP with login '.$ftp_user, LOG_DEBUG);
87  $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
88  $ok = 0;
89  $error++;
90  }
91  } else {
92  if (ftp_login($connect_id, $ftp_user, $ftp_password)) {
93  // Turn on passive mode transfers (must be after a successful login
94  if ($ftp_passive) {
95  ftp_pasv($connect_id, true);
96  }
97 
98  // Change the dir
99  $newsectioniso = utf8_decode($section);
100  ftp_chdir($connect_id, $newsectioniso);
101  } else {
102  $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
103  $ok = 0;
104  $error++;
105  }
106  }
107  }
108  } else {
109  dol_syslog('FailedToConnectToFTPServer '.$ftp_server.' '.$ftp_port, LOG_ERR);
110  $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port);
111  $ok = 0;
112  }
113  }
114 
115  $arrayresult = array('conn_id'=>$connect_id, 'ok'=>$ok, 'mesg'=>$mesg, 'curdir'=>$section, 'curdiriso'=>$newsectioniso);
116  return $arrayresult;
117 }
118 
119 
127 function ftp_isdir($connect_id, $dir)
128 {
129  if (@ftp_chdir($connect_id, $dir)) {
130  ftp_cdup($connect_id);
131  return 1;
132  } else {
133  return 0;
134  }
135 }
136 
143 function dol_ftp_close($connect_id)
144 {
145  global $conf;
146 
147  // Close FTP connection
148  if ($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);
152  } else {
153  return ftp_close($connect_id);
154  }
155  }
156 }
157 
166 function dol_ftp_delete($connect_id, $file, $newsection)
167 {
168 
169  global $conf;
170 
171  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
172  $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
173  }
174 
175  // Remote file
176  $filename = $file;
177  $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
178  $newremotefileiso = utf8_decode($remotefile);
179 
180  //print "x".$newremotefileiso;
181  dol_syslog("ftp/index.php ftp_delete ".$newremotefileiso);
182  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
183  return ssh2_sftp_unlink($connect_id, $newremotefileiso);
184  } else {
185  return @ftp_delete($connect_id, $newremotefileiso);
186  }
187 }
188 
198 function dol_ftp_get($connect_id, $localfile, $file, $newsection)
199 {
200 
201  global $conf;
202 
203  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
204  $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
205  }
206 
207  // Remote file
208  $filename = $file;
209  $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
210  $newremotefileiso = utf8_decode($remotefile);
211 
212  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
213  return fopen('ssh2.sftp://'.intval($connect_id).$newremotefileiso, 'r');
214  } else {
215  return ftp_get($connect_id, $localfile, $newremotefileiso, FTP_BINARY);
216  }
217 }
218 
228 function dol_ftp_put($connect_id, $file, $localfile, $newsection)
229 {
230  global $conf;
231 
232  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
233  $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
234  }
235 
236  // Remote file
237  $filename = $file;
238  $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
239  $newremotefileiso = utf8_decode($remotefile);
240 
241  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
242  return ssh2_scp_send($connect_id, $localfile, $newremotefileiso, 0644);
243  } else {
244  return ftp_put($connect_id, $newremotefileiso, $localfile, FTP_BINARY);
245  }
246 }
247 
256 function dol_ftp_rmdir($connect_id, $file, $newsection)
257 {
258  global $conf;
259 
260  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
261  $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
262  }
263 
264  // Remote file
265  $filename = $file;
266  $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
267  $newremotefileiso = utf8_decode($remotefile);
268 
269  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
270  return ssh2_sftp_rmdir($connect_id, $newremotefileiso);
271  } else {
272  return @ftp_rmdir($connect_id, $newremotefileiso);
273  }
274 }
275 
276 
285 function dol_ftp_mkdir($connect_id, $newdir, $newsection)
286 {
287  global $conf;
288 
289  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
290  $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
291  }
292 
293  // Remote file
294  $newremotefileiso = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$newdir;
295  $newremotefileiso = utf8_decode($newremotefileiso);
296 
297  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
298  return ssh2_sftp_mkdir($connect_id, $newremotefileiso, 0777);
299  } else {
300  return @ftp_mkdir($connect_id, $newremotefileiso);
301  }
302 }
dol_ftp_mkdir($connect_id, $newdir, $newsection)
Remove FTP directory.
Definition: ftp.lib.php:285
ftp_isdir($connect_id, $dir)
Tell if an entry is a FTP directory.
Definition: ftp.lib.php:127
dol_ftp_close($connect_id)
Tell if an entry is a FTP directory.
Definition: ftp.lib.php:143
dol_ftp_delete($connect_id, $file, $newsection)
Delete a FTP file.
Definition: ftp.lib.php:166
dol_ftp_rmdir($connect_id, $file, $newsection)
Remove FTP directory.
Definition: ftp.lib.php:256
dol_ftp_put($connect_id, $file, $localfile, $newsection)
Upload a FTP file.
Definition: ftp.lib.php:228
dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive=0)
Connect to FTP server.
Definition: ftp.lib.php:40
dol_ftp_get($connect_id, $localfile, $file, $newsection)
Download a FTP file.
Definition: ftp.lib.php:198
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.