dolibarr  17.0.4
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 
43  global $langs, $conf;
44 
45  $ok = 1;
46  $error = 0;
47  $connect_id = null;
48  $newsectioniso = '';
49  $mesg="";
50 
51  if (!is_numeric($ftp_port)) {
52  $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port);
53  $ok = 0;
54  }
55 
56  if ($ok) {
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);
64  } else {
65  dol_syslog('Try to connect with ftp_connect');
66  $connect_id = ftp_connect($ftp_server, $ftp_port, $connecttimeout);
67  }
68  if (!empty($connect_id) || !empty($tmp_conn_id)) {
69  if ($ftp_user) {
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)) {
73  // Turn on passive mode transfers (must be after a successful login
74  //if ($ftp_passive) ftp_pasv($connect_id, true);
75 
76  // Change the dir
77  $newsectioniso = utf8_decode($section);
78  //ftp_chdir($connect_id, $newsectioniso);
79  $connect_id = ssh2_sftp($tmp_conn_id);
80  if (!$connect_id) {
81  dol_syslog('Failed to connect to SFTP after sssh authentication', LOG_DEBUG);
82  $mesg = $langs->transnoentitiesnoconv("FailedToConnectToSFTPAfterSSHAuthentication");
83  $ok = 0;
84  $error++;
85  }
86  } else {
87  dol_syslog('Failed to connect to FTP with login '.$ftp_user, LOG_DEBUG);
88  $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
89  $ok = 0;
90  $error++;
91  }
92  } else {
93  if (ftp_login($connect_id, $ftp_user, $ftp_password)) {
94  // Turn on passive mode transfers (must be after a successful login
95  if ($ftp_passive) {
96  ftp_pasv($connect_id, true);
97  }
98 
99  // Change the dir
100  $newsectioniso = utf8_decode($section);
101  ftp_chdir($connect_id, $newsectioniso);
102  } else {
103  $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
104  $ok = 0;
105  $error++;
106  }
107  }
108  }
109  } else {
110  dol_syslog('FailedToConnectToFTPServer '.$ftp_server.' '.$ftp_port, LOG_ERR);
111  $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port);
112  $ok = 0;
113  }
114  }
115 
116  $arrayresult = array('conn_id'=>$connect_id, 'ok'=>$ok, 'mesg'=>$mesg, 'curdir'=>$section, 'curdiriso'=>$newsectioniso);
117  return $arrayresult;
118 }
119 
120 
128 function ftp_isdir($connect_id, $dir)
129 {
130 
131  if (@ftp_chdir($connect_id, $dir)) {
132  ftp_cdup($connect_id);
133  return 1;
134  } else {
135  return 0;
136  }
137 }
138 
145 function dol_ftp_close($connect_id)
146 {
147 
148  global $conf;
149 
150  // Close FTP connection
151  if ($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);
155  } else {
156  return ftp_close($connect_id);
157  }
158  }
159 }
160 
169 function dol_ftp_delete($connect_id, $file, $newsection)
170 {
171 
172  global $conf;
173 
174  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
175  $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
176  }
177 
178  // Remote file
179  $filename = $file;
180  $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
181  $newremotefileiso = utf8_decode($remotefile);
182 
183  //print "x".$newremotefileiso;
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);
187  } else {
188  return @ftp_delete($connect_id, $newremotefileiso);
189  }
190 }
191 
201 function dol_ftp_get($connect_id, $localfile, $file, $newsection)
202 {
203 
204  global $conf;
205 
206  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
207  $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
208  }
209 
210  // Remote file
211  $filename = $file;
212  $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
213  $newremotefileiso = utf8_decode($remotefile);
214 
215  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
216  return fopen('ssh2.sftp://'.intval($connect_id).$newremotefileiso, 'r');
217  } else {
218  return ftp_get($connect_id, $localfile, $newremotefileiso, FTP_BINARY);
219  }
220 }
221 
231 function dol_ftp_put($connect_id, $file, $localfile, $newsection)
232 {
233 
234  global $conf;
235 
236  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
237  $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
238  }
239 
240  // Remote file
241  $filename = $file;
242  $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
243  $newremotefileiso = utf8_decode($remotefile);
244 
245  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
246  return ssh2_scp_send($connect_id, $localfile, $newremotefileiso, 0644);
247  } else {
248  return ftp_put($connect_id, $newremotefileiso, $localfile, FTP_BINARY);
249  }
250 }
251 
260 function dol_ftp_rmdir($connect_id, $file, $newsection)
261 {
262 
263  global $conf;
264 
265  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
266  $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
267  }
268 
269  // Remote file
270  $filename = $file;
271  $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
272  $newremotefileiso = utf8_decode($remotefile);
273 
274  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
275  return ssh2_sftp_rmdir($connect_id, $newremotefileiso);
276  } else {
277  return @ftp_rmdir($connect_id, $newremotefileiso);
278  }
279 }
280 
281 
290 function dol_ftp_mkdir($connect_id, $newdir, $newsection)
291 {
292 
293  global $conf;
294 
295  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
296  $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
297  }
298 
299  // Remote file
300  $newremotefileiso = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$newdir;
301  $newremotefileiso = utf8_decode($newremotefileiso);
302 
303  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
304  return ssh2_sftp_mkdir($connect_id, $newremotefileiso, 0777);
305  } else {
306  return @ftp_mkdir($connect_id, $newremotefileiso);
307  }
308 }
dol_ftp_mkdir($connect_id, $newdir, $newsection)
Remove FTP directory.
Definition: ftp.lib.php:290
ftp_isdir($connect_id, $dir)
Tell if an entry is a FTP directory.
Definition: ftp.lib.php:128
dol_ftp_close($connect_id)
Tell if an entry is a FTP directory.
Definition: ftp.lib.php:145
dol_ftp_delete($connect_id, $file, $newsection)
Delete a FTP file.
Definition: ftp.lib.php:169
dol_ftp_rmdir($connect_id, $file, $newsection)
Remove FTP directory.
Definition: ftp.lib.php:260
dol_ftp_put($connect_id, $file, $localfile, $newsection)
Upload a FTP file.
Definition: ftp.lib.php:231
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:201
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.