dolibarr  18.0.0-alpha
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  global $conf;
169 
170  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
171  $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
172  }
173 
174  // Remote file
175  $filename = $file;
176  $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
177  $newremotefileiso = utf8_decode($remotefile);
178 
179  //print "x".$newremotefileiso;
180  dol_syslog("ftp/index.php ftp_delete ".$newremotefileiso);
181  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
182  return ssh2_sftp_unlink($connect_id, $newremotefileiso);
183  } else {
184  return @ftp_delete($connect_id, $newremotefileiso);
185  }
186 }
187 
197 function dol_ftp_get($connect_id, $localfile, $file, $newsection)
198 {
199  global $conf;
200 
201  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
202  $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
203  }
204 
205  // Remote file
206  $filename = $file;
207  $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
208  $newremotefileiso = utf8_decode($remotefile);
209 
210  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
211  return fopen('ssh2.sftp://'.intval($connect_id).$newremotefileiso, 'r');
212  } else {
213  return ftp_get($connect_id, $localfile, $newremotefileiso, FTP_BINARY);
214  }
215 }
216 
226 function dol_ftp_put($connect_id, $file, $localfile, $newsection)
227 {
228  global $conf;
229 
230  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
231  $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
232  }
233 
234  // Remote file
235  $filename = $file;
236  $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
237  $newremotefileiso = utf8_decode($remotefile);
238 
239  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
240  return ssh2_scp_send($connect_id, $localfile, $newremotefileiso, 0644);
241  } else {
242  return ftp_put($connect_id, $newremotefileiso, $localfile, FTP_BINARY);
243  }
244 }
245 
254 function dol_ftp_rmdir($connect_id, $file, $newsection)
255 {
256  global $conf;
257 
258  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
259  $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
260  }
261 
262  // Remote file
263  $filename = $file;
264  $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
265  $newremotefileiso = utf8_decode($remotefile);
266 
267  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
268  return ssh2_sftp_rmdir($connect_id, $newremotefileiso);
269  } else {
270  return @ftp_rmdir($connect_id, $newremotefileiso);
271  }
272 }
273 
274 
283 function dol_ftp_mkdir($connect_id, $newdir, $newsection)
284 {
285  global $conf;
286 
287  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
288  $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
289  }
290 
291  // Remote file
292  $newremotefileiso = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$newdir;
293  $newremotefileiso = utf8_decode($newremotefileiso);
294 
295  if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
296  return ssh2_sftp_mkdir($connect_id, $newremotefileiso, 0777);
297  } else {
298  return @ftp_mkdir($connect_id, $newremotefileiso);
299  }
300 }
dol_ftp_get
dol_ftp_get($connect_id, $localfile, $file, $newsection)
Download a FTP file.
Definition: ftp.lib.php:197
dol_ftp_rmdir
dol_ftp_rmdir($connect_id, $file, $newsection)
Remove FTP directory.
Definition: ftp.lib.php:254
dol_ftp_connect
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_close
dol_ftp_close($connect_id)
Tell if an entry is a FTP directory.
Definition: ftp.lib.php:143
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1639
ftp_isdir
ftp_isdir($connect_id, $dir)
Tell if an entry is a FTP directory.
Definition: ftp.lib.php:127
dol_ftp_put
dol_ftp_put($connect_id, $file, $localfile, $newsection)
Upload a FTP file.
Definition: ftp.lib.php:226
dol_ftp_mkdir
dol_ftp_mkdir($connect_id, $newdir, $newsection)
Remove FTP directory.
Definition: ftp.lib.php:283
dol_ftp_delete
dol_ftp_delete($connect_id, $file, $newsection)
Delete a FTP file.
Definition: ftp.lib.php:166