dolibarr 20.0.5
ftp.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2022-2025 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2022 Anthony Berton <bertonanthony@gmail.com>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
39function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive = 0)
40{
41 global $langs, $conf;
42
43 $ok = 1;
44 $error = 0;
45 $connect_id = null;
46 $newsectioniso = '';
47 $mesg = "";
48
49 if (!is_numeric($ftp_port)) {
50 $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port);
51 $ok = 0;
52 }
53
54 if ($ok) {
55 $connecttimeout = (!getDolGlobalString('FTP_CONNECT_TIMEOUT') ? 40 : $conf->global->FTP_CONNECT_TIMEOUT);
56 $tmp_conn_id = 0;
57 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
58 dol_syslog('Try to connect with ssh2_connect');
59 $tmp_conn_id = ssh2_connect($ftp_server, (int) $ftp_port);
60 } elseif (getDolGlobalString('FTP_CONNECT_WITH_SSL')) {
61 dol_syslog('Try to connect with ftp_ssl_connect');
62 $connect_id = ftp_ssl_connect($ftp_server, (int) $ftp_port, $connecttimeout);
63 } else {
64 dol_syslog('Try to connect with ftp_connect');
65 $connect_id = ftp_connect($ftp_server, (int) $ftp_port, $connecttimeout);
66 }
67 if (!empty($connect_id) || !empty($tmp_conn_id)) {
68 if ($ftp_user) {
69 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
70 dol_syslog('Try to authenticate with ssh2_auth_password');
71 if (!empty($tmp_conn_id) && 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 = mb_convert_encoding($section, 'ISO-8859-1');
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 (!empty($connect_id) && 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 = mb_convert_encoding($section, 'ISO-8859-1');
100 if (!ftp_chdir($connect_id, $newsectioniso)) {
101 $ok = 0;
102 $mesg = $langs->transnoentitiesnoconv("FailedToChdirOnFTPServer");
103 }
104 } else {
105 $ok = 0;
106 $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
107 }
108 }
109 }
110 } else {
111 dol_syslog('FailedToConnectToFTPServer '.$ftp_server.' '.$ftp_port, LOG_ERR);
112 $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port);
113 $ok = 0;
114 }
115 }
116
117 $arrayresult = array('conn_id' => $connect_id, 'ok' => $ok, 'mesg' => $mesg, 'curdir' => $section, 'curdiriso' => $newsectioniso);
118 return $arrayresult;
119}
120
121
129function ftp_isdir($connect_id, $dir)
130{
131 if (@ftp_chdir($connect_id, $dir)) {
132 ftp_cdup($connect_id);
133 return 1;
134 } else {
135 return 0;
136 }
137}
138
145function dol_ftp_close($connect_id)
146{
147 // Close FTP connection
148 if ($connect_id) {
149 if (!getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
150 return ftp_close($connect_id);
151 }
152 }
153 return true;
154}
155
164function dol_ftp_delete($connect_id, $file, $newsection)
165{
166 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
167 $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
168 }
169
170 // Remote file
171 $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
172 $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
173
174 //print "x".$newremotefileiso;
175 dol_syslog("ftp/index.php ftp_delete ".$newremotefileiso);
176 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
177 return ssh2_sftp_unlink($connect_id, $newremotefileiso);
178 } else {
179 return @ftp_delete($connect_id, $newremotefileiso);
180 }
181}
182
192function dol_ftp_get($connect_id, $localfile, $file, $newsection)
193{
194 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
195 $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
196 }
197
198 // Remote file
199 $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
200 $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
201
202 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
203 return fopen('ssh2.sftp://'.intval($connect_id).$newremotefileiso, 'r');
204 } else {
205 return ftp_get($connect_id, $localfile, $newremotefileiso, FTP_BINARY);
206 }
207}
208
218function dol_ftp_put($connect_id, $file, $localfile, $newsection)
219{
220 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
221 $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
222 }
223
224 // Remote file
225 $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
226 $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
227
228 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
229 return ssh2_scp_send($connect_id, $localfile, $newremotefileiso, 0644);
230 } else {
231 return ftp_put($connect_id, $newremotefileiso, $localfile, FTP_BINARY);
232 }
233}
234
243function dol_ftp_rmdir($connect_id, $file, $newsection)
244{
245 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
246 $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
247 }
248
249 // Remote file
250 $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
251 $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
252
253 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
254 return ssh2_sftp_rmdir($connect_id, $newremotefileiso);
255 } else {
256 return @ftp_rmdir($connect_id, $newremotefileiso);
257 }
258}
259
260
269function dol_ftp_mkdir($connect_id, $newdir, $newsection)
270{
271 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
272 $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
273 }
274
275 // Remote file
276 $newremotefileiso = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$newdir;
277 $newremotefileiso = mb_convert_encoding($newremotefileiso, 'ISO-8859-1');
278
279 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
280 return ssh2_sftp_mkdir($connect_id, $newremotefileiso, 0777);
281 } else {
282 return @ftp_mkdir($connect_id, $newremotefileiso);
283 }
284}
dol_ftp_mkdir($connect_id, $newdir, $newsection)
Remove FTP directory.
Definition ftp.lib.php:269
ftp_isdir($connect_id, $dir)
Tell if an entry is a FTP directory.
Definition ftp.lib.php:129
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:164
dol_ftp_rmdir($connect_id, $file, $newsection)
Remove FTP directory.
Definition ftp.lib.php:243
dol_ftp_put($connect_id, $file, $localfile, $newsection)
Upload a FTP file.
Definition ftp.lib.php:218
dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive=0)
Connect to FTP server.
Definition ftp.lib.php:39
dol_ftp_get($connect_id, $localfile, $file, $newsection)
Download a FTP file.
Definition ftp.lib.php:192
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.