dolibarr 24.0.0-beta
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-2026 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
40function 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 = (!getDolGlobalString('FTP_CONNECT_TIMEOUT') ? 40 : $conf->global->FTP_CONNECT_TIMEOUT);
57 $tmp_conn_id = 0;
58 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
59 dol_syslog('Try to connect with ssh2_connect');
60 $tmp_conn_id = ssh2_connect($ftp_server, (int) $ftp_port);
61 } elseif (getDolGlobalString('FTP_CONNECT_WITH_SSL')) {
62 dol_syslog('Try to connect with ftp_ssl_connect');
63 $connect_id = ftp_ssl_connect($ftp_server, (int) $ftp_port, $connecttimeout);
64 } else {
65 dol_syslog('Try to connect with ftp_connect');
66 $connect_id = ftp_connect($ftp_server, (int) $ftp_port, $connecttimeout);
67 }
68 if (!empty($connect_id) || !empty($tmp_conn_id)) {
69 if ($ftp_user) {
70 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
71 dol_syslog('Try to authenticate with ssh2_auth_password');
72 if (!empty($tmp_conn_id) && 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 = mb_convert_encoding($section, 'ISO-8859-1');
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 (!empty($connect_id) && 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 = mb_convert_encoding($section, 'ISO-8859-1');
101 if (!ftp_chdir($connect_id, $newsectioniso)) {
102 $ok = 0;
103 $mesg = $langs->transnoentitiesnoconv("FailedToChdirOnFTPServer");
104 }
105 } else {
106 $ok = 0;
107 $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
108 }
109 }
110 }
111 } else {
112 dol_syslog('FailedToConnectToFTPServer '.$ftp_server.' '.$ftp_port, LOG_ERR);
113 $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port);
114 $ok = 0;
115 }
116 }
117
118 $arrayresult = array('conn_id' => $connect_id, 'ok' => $ok, 'mesg' => $mesg, 'curdir' => $section, 'curdiriso' => $newsectioniso);
119 return $arrayresult;
120}
121
122
130function ftp_isdir($connect_id, $dir)
131{
132 if (@ftp_chdir($connect_id, $dir)) {
133 ftp_cdup($connect_id);
134 return 1;
135 } else {
136 return 0;
137 }
138}
139
146function dol_ftp_close($connect_id)
147{
148 // Close FTP connection
149 if ($connect_id) {
150 if (!getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
151 return ftp_close($connect_id);
152 }
153 }
154 return true;
155}
156
165function dol_ftp_delete($connect_id, $file, $newsection)
166{
167 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
168 $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
169 }
170
171 // Remote file
172 $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
173 $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
174
175 //print "x".$newremotefileiso;
176 dol_syslog("ftp/index.php ftp_delete ".$newremotefileiso);
177 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
178 return ssh2_sftp_unlink($connect_id, $newremotefileiso);
179 } else {
180 return @ftp_delete($connect_id, $newremotefileiso);
181 }
182}
183
193function dol_ftp_get($connect_id, $localfile, $file, $newsection)
194{
195 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
196 $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
197 }
198
199 // Remote file
200 $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
201 $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
202
203 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
204 return fopen('ssh2.sftp://'.intval($connect_id).$newremotefileiso, 'r');
205 } else {
206 return ftp_get($connect_id, $localfile, $newremotefileiso, FTP_BINARY);
207 }
208}
209
219function dol_ftp_put($connect_id, $file, $localfile, $newsection)
220{
221 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
222 $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
223 }
224
225 // Remote file
226 $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
227 $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
228
229 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
230 return ssh2_scp_send($connect_id, $localfile, $newremotefileiso, 0644);
231 } else {
232 return ftp_put($connect_id, $newremotefileiso, $localfile, FTP_BINARY);
233 }
234}
235
244function dol_ftp_rmdir($connect_id, $file, $newsection)
245{
246 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
247 $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
248 }
249
250 // Remote file
251 $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
252 $newremotefileiso = mb_convert_encoding($remotefile, 'ISO-8859-1');
253
254 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
255 return ssh2_sftp_rmdir($connect_id, $newremotefileiso);
256 } else {
257 return @ftp_rmdir($connect_id, $newremotefileiso);
258 }
259}
260
261
270function dol_ftp_mkdir($connect_id, $newdir, $newsection)
271{
272 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
273 $newsection = ssh2_sftp_realpath($connect_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
274 }
275
276 // Remote file
277 $newremotefileiso = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$newdir;
278 $newremotefileiso = mb_convert_encoding($newremotefileiso, 'ISO-8859-1');
279
280 if (getDolGlobalString('FTP_CONNECT_WITH_SFTP')) {
281 return ssh2_sftp_mkdir($connect_id, $newremotefileiso, 0777);
282 } else {
283 return @ftp_mkdir($connect_id, $newremotefileiso);
284 }
285}
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_ftp_mkdir($connect_id, $newdir, $newsection)
Remove FTP directory.
Definition ftp.lib.php:270
ftp_isdir($connect_id, $dir)
Tell if an entry is a FTP directory.
Definition ftp.lib.php:130
dol_ftp_close($connect_id)
Tell if an entry is a FTP directory.
Definition ftp.lib.php:146
dol_ftp_delete($connect_id, $file, $newsection)
Delete a FTP file.
Definition ftp.lib.php:165
dol_ftp_rmdir($connect_id, $file, $newsection)
Remove FTP directory.
Definition ftp.lib.php:244
dol_ftp_put($connect_id, $file, $localfile, $newsection)
Upload a FTP file.
Definition ftp.lib.php:219
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:193
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.